1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.layoutlib.bridge; 18 19 import com.android.ide.common.rendering.api.RenderSession; 20 import com.android.ide.common.rendering.api.ResourceReference; 21 import com.android.ide.common.rendering.api.ResourceValue; 22 import com.android.ide.common.rendering.api.Result; 23 import com.android.ide.common.rendering.api.ViewInfo; 24 import com.android.layoutlib.bridge.impl.RenderSessionImpl; 25 import com.android.tools.layoutlib.java.System_Delegate; 26 27 import android.annotation.NonNull; 28 import android.annotation.Nullable; 29 30 import java.awt.image.BufferedImage; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Map; 34 35 /** 36 * An implementation of {@link RenderSession}. 37 * 38 * This is a pretty basic class that does almost nothing. All of the work is done in 39 * {@link RenderSessionImpl}. 40 * 41 */ 42 public class BridgeRenderSession extends RenderSession { 43 44 @Nullable 45 private final RenderSessionImpl mSession; 46 @NonNull 47 private Result mLastResult; 48 49 @Override getResult()50 public Result getResult() { 51 return mLastResult; 52 } 53 54 @Override getImage()55 public BufferedImage getImage() { 56 return mSession != null ? mSession.getImage() : 57 new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 58 } 59 60 @Override getRootViews()61 public List<ViewInfo> getRootViews() { 62 return mSession != null ? mSession.getViewInfos() : Collections.emptyList(); 63 } 64 65 @Override getSystemRootViews()66 public List<ViewInfo> getSystemRootViews() { 67 return mSession != null ? mSession.getSystemViewInfos() : Collections.emptyList(); 68 } 69 70 @Override getDefaultNamespacedProperties()71 public Map<Object, Map<ResourceReference, ResourceValue>> getDefaultNamespacedProperties() { 72 return mSession != null ? mSession.getDefaultNamespacedProperties() : 73 Collections.emptyMap(); 74 } 75 76 @Override getDefaultNamespacedStyles()77 public Map<Object, ResourceReference> getDefaultNamespacedStyles() { 78 return mSession != null ? mSession.getDefaultNamespacedStyles() : Collections.emptyMap(); 79 } 80 81 @Override measure(long timeout)82 public Result measure(long timeout) { 83 if (mSession != null) { 84 try { 85 Bridge.prepareThread(); 86 mLastResult = mSession.acquire(timeout); 87 if (mLastResult.isSuccess()) { 88 mSession.invalidateRenderingSize(); 89 mLastResult = mSession.measure(); 90 } 91 } finally { 92 mSession.release(); 93 Bridge.cleanupThread(); 94 } 95 } 96 97 return mLastResult; 98 } 99 100 @Override render(long timeout, boolean forceMeasure)101 public Result render(long timeout, boolean forceMeasure) { 102 if (mSession != null) { 103 try { 104 Bridge.prepareThread(); 105 mLastResult = mSession.acquire(timeout); 106 if (mLastResult.isSuccess()) { 107 if (forceMeasure) { 108 mSession.invalidateRenderingSize(); 109 } 110 mLastResult = mSession.render(false /*freshRender*/); 111 } 112 } finally { 113 mSession.release(); 114 Bridge.cleanupThread(); 115 } 116 } 117 118 return mLastResult; 119 } 120 121 @Override setSystemTimeNanos(long nanos)122 public void setSystemTimeNanos(long nanos) { 123 System_Delegate.setNanosTime(nanos); 124 } 125 126 @Override setSystemBootTimeNanos(long nanos)127 public void setSystemBootTimeNanos(long nanos) { 128 System_Delegate.setBootTimeNanos(nanos); 129 } 130 131 @Override setElapsedFrameTimeNanos(long nanos)132 public void setElapsedFrameTimeNanos(long nanos) { 133 if (mSession != null) { 134 mSession.setElapsedFrameTimeNanos(nanos); 135 } 136 } 137 138 @Override dispose()139 public void dispose() { 140 if (mSession != null) { 141 mSession.dispose(); 142 } 143 } 144 BridgeRenderSession(@ullable RenderSessionImpl scene, @NonNull Result lastResult)145 /*package*/ BridgeRenderSession(@Nullable RenderSessionImpl scene, @NonNull Result lastResult) { 146 mSession = scene; 147 if (scene != null) { 148 mSession.setScene(this); 149 } 150 mLastResult = lastResult; 151 } 152 153 @Override getValidationData()154 public Object getValidationData() { 155 if (mSession != null) { 156 return mSession.getValidatorResult(); 157 } 158 return null; 159 } 160 } 161