ThreadedRenderer.java revision 0ed751dd22a98f0c1f637570c5775b4f5e97d895
1/* 2 * Copyright (C) 2013 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 17package android.view; 18 19import android.graphics.Bitmap; 20import android.graphics.Rect; 21import android.graphics.SurfaceTexture; 22import android.os.SystemClock; 23import android.os.Trace; 24import android.view.Surface.OutOfResourcesException; 25import android.view.View.AttachInfo; 26 27import java.io.PrintWriter; 28 29/** 30 * Hardware renderer that proxies the rendering to a render thread. Most calls 31 * are currently synchronous. 32 * TODO: Make draw() async. 33 * TODO: Figure out how to share the DisplayList between two threads (global lock?) 34 * 35 * The UI thread can block on the RenderThread, but RenderThread must never 36 * block on the UI thread. 37 * 38 * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates 39 * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed 40 * by the lifecycle of the RenderProxy. 41 * 42 * Note that although currently the EGL context & surfaces are created & managed 43 * by the render thread, the goal is to move that into a shared structure that can 44 * be managed by both threads. EGLSurface creation & deletion should ideally be 45 * done on the UI thread and not the RenderThread to avoid stalling the 46 * RenderThread with surface buffer allocation. 47 * 48 * @hide 49 */ 50public class ThreadedRenderer extends HardwareRenderer { 51 private static final String LOGTAG = "ThreadedRenderer"; 52 53 private static final Rect NULL_RECT = new Rect(-1, -1, -1, -1); 54 55 private int mWidth, mHeight; 56 private long mNativeProxy; 57 58 ThreadedRenderer(boolean translucent) { 59 mNativeProxy = nCreateProxy(translucent); 60 setEnabled(mNativeProxy != 0); 61 } 62 63 @Override 64 void destroy(boolean full) { 65 nDestroyCanvas(mNativeProxy); 66 } 67 68 @Override 69 boolean initialize(Surface surface) throws OutOfResourcesException { 70 return nInitialize(mNativeProxy, surface); 71 } 72 73 @Override 74 void updateSurface(Surface surface) throws OutOfResourcesException { 75 nUpdateSurface(mNativeProxy, surface); 76 } 77 78 @Override 79 void destroyHardwareResources(View view) { 80 destroyResources(view); 81 // TODO: GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); 82 } 83 84 private static void destroyResources(View view) { 85 view.destroyHardwareResources(); 86 87 if (view instanceof ViewGroup) { 88 ViewGroup group = (ViewGroup) view; 89 90 int count = group.getChildCount(); 91 for (int i = 0; i < count; i++) { 92 destroyResources(group.getChildAt(i)); 93 } 94 } 95 } 96 97 @Override 98 void invalidate(Surface surface) { 99 updateSurface(surface); 100 } 101 102 @Override 103 boolean safelyRun(Runnable action) { 104 nRunWithGlContext(mNativeProxy, action); 105 return true; 106 } 107 108 @Override 109 void setup(int width, int height) { 110 mWidth = width; 111 mHeight = height; 112 nSetup(mNativeProxy, width, height); 113 } 114 115 @Override 116 int getWidth() { 117 return mWidth; 118 } 119 120 @Override 121 int getHeight() { 122 return mHeight; 123 } 124 125 @Override 126 void dumpGfxInfo(PrintWriter pw) { 127 // TODO Auto-generated method stub 128 } 129 130 @Override 131 long getFrameCount() { 132 // TODO Auto-generated method stub 133 return 0; 134 } 135 136 @Override 137 boolean loadSystemProperties() { 138 return false; 139 } 140 141 /** 142 * TODO: Remove 143 * Temporary hack to allow RenderThreadTest prototype app to trigger 144 * replaying a DisplayList after modifying the displaylist properties 145 * 146 * @hide */ 147 public void repeatLastDraw() { 148 } 149 150 @Override 151 void setDisplayListData(long displayList, long newData) { 152 nSetDisplayListData(mNativeProxy, displayList, newData); 153 } 154 155 @Override 156 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks, Rect dirty) { 157 attachInfo.mIgnoreDirtyState = true; 158 attachInfo.mDrawingTime = SystemClock.uptimeMillis(); 159 view.mPrivateFlags |= View.PFLAG_DRAWN; 160 161 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) 162 == View.PFLAG_INVALIDATED; 163 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; 164 165 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); 166 RenderNode displayList = view.getDisplayList(); 167 Trace.traceEnd(Trace.TRACE_TAG_VIEW); 168 169 view.mRecreateDisplayList = false; 170 171 if (dirty == null) { 172 dirty = NULL_RECT; 173 } 174 nDrawDisplayList(mNativeProxy, displayList.getNativeDisplayList(), 175 dirty.left, dirty.top, dirty.right, dirty.bottom); 176 } 177 178 @Override 179 void detachFunctor(long functor) { 180 nDetachFunctor(mNativeProxy, functor); 181 } 182 183 @Override 184 void attachFunctor(AttachInfo attachInfo, long functor) { 185 nAttachFunctor(mNativeProxy, functor); 186 } 187 188 @Override 189 public void invokeFunctor(long functor, boolean waitForCompletion) { 190 nInvokeFunctor(mNativeProxy, functor, waitForCompletion); 191 } 192 193 @Override 194 HardwareLayer createDisplayListLayer(int width, int height) { 195 long layer = nCreateDisplayListLayer(mNativeProxy, width, height); 196 return HardwareLayer.adoptDisplayListLayer(this, layer); 197 } 198 199 @Override 200 HardwareLayer createTextureLayer() { 201 long layer = nCreateTextureLayer(mNativeProxy); 202 return HardwareLayer.adoptTextureLayer(this, layer); 203 } 204 205 @Override 206 SurfaceTexture createSurfaceTexture(final HardwareLayer layer) { 207 final SurfaceTexture[] ret = new SurfaceTexture[1]; 208 nRunWithGlContext(mNativeProxy, new Runnable() { 209 @Override 210 public void run() { 211 ret[0] = layer.createSurfaceTexture(); 212 } 213 }); 214 return ret[0]; 215 } 216 217 @Override 218 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { 219 return nCopyLayerInto(mNativeProxy, 220 layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap); 221 } 222 223 @Override 224 void pushLayerUpdate(HardwareLayer layer) { 225 // TODO: Remove this, it's not needed outside of GLRenderer 226 } 227 228 @Override 229 void onLayerCreated(HardwareLayer layer) { 230 // TODO: Is this actually useful? 231 } 232 233 @Override 234 void flushLayerUpdates() { 235 // TODO: Figure out what this should do or remove it 236 } 237 238 @Override 239 void onLayerDestroyed(HardwareLayer layer) { 240 nDestroyLayer(mNativeProxy, layer.getDeferredLayerUpdater()); 241 } 242 243 @Override 244 void setName(String name) { 245 } 246 247 @Override 248 void fence() { 249 nFence(mNativeProxy); 250 } 251 252 @Override 253 protected void finalize() throws Throwable { 254 try { 255 nDeleteProxy(mNativeProxy); 256 mNativeProxy = 0; 257 } finally { 258 super.finalize(); 259 } 260 } 261 262 /** @hide */ 263 public static native void postToRenderThread(Runnable runnable); 264 265 private static native long nCreateProxy(boolean translucent); 266 private static native void nDeleteProxy(long nativeProxy); 267 268 private static native boolean nInitialize(long nativeProxy, Surface window); 269 private static native void nUpdateSurface(long nativeProxy, Surface window); 270 private static native void nSetup(long nativeProxy, int width, int height); 271 private static native void nSetDisplayListData(long nativeProxy, long displayList, 272 long newData); 273 private static native void nDrawDisplayList(long nativeProxy, long displayList, 274 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom); 275 private static native void nRunWithGlContext(long nativeProxy, Runnable runnable); 276 private static native void nDestroyCanvas(long nativeProxy); 277 278 private static native void nAttachFunctor(long nativeProxy, long functor); 279 private static native void nDetachFunctor(long nativeProxy, long functor); 280 private static native void nInvokeFunctor(long nativeProxy, long functor, boolean waitForCompletion); 281 282 private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height); 283 private static native long nCreateTextureLayer(long nativeProxy); 284 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap); 285 private static native void nDestroyLayer(long nativeProxy, long layer); 286 287 private static native void nFence(long nativeProxy); 288} 289