ThreadedRenderer.java revision e4280baaa709c74d86cf6a389a4674ca665f5af6
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.Trace; 23import android.view.Surface.OutOfResourcesException; 24import android.view.View.AttachInfo; 25 26import java.io.PrintWriter; 27 28/** 29 * Hardware renderer that proxies the rendering to a render thread. Most calls 30 * are currently synchronous. 31 * TODO: Make draw() async. 32 * TODO: Figure out how to share the DisplayList between two threads (global lock?) 33 * 34 * The UI thread can block on the RenderThread, but RenderThread must never 35 * block on the UI thread. 36 * 37 * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates 38 * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed 39 * by the lifecycle of the RenderProxy. 40 * 41 * Note that although currently the EGL context & surfaces are created & managed 42 * by the render thread, the goal is to move that into a shared structure that can 43 * be managed by both threads. EGLSurface creation & deletion should ideally be 44 * done on the UI thread and not the RenderThread to avoid stalling the 45 * RenderThread with surface buffer allocation. 46 * 47 * @hide 48 */ 49public class ThreadedRenderer extends HardwareRenderer { 50 private static final String LOGTAG = "ThreadedRenderer"; 51 52 private static final Rect NULL_RECT = new Rect(); 53 54 private static final long NANOS_PER_MS = 1000000; 55 56 // Keep in sync with DrawFrameTask.h SYNC_* flags 57 // Nothing interesting to report 58 private static final int SYNC_OK = 0x0; 59 // Needs a ViewRoot invalidate 60 private static final int SYNC_INVALIDATE_REQUIRED = 0x1; 61 62 private int mWidth, mHeight; 63 private long mNativeProxy; 64 private boolean mInitialized = false; 65 private RenderNode mRootNode; 66 private Choreographer mChoreographer; 67 68 ThreadedRenderer(boolean translucent) { 69 long rootNodePtr = nCreateRootRenderNode(); 70 mRootNode = RenderNode.adopt(rootNodePtr); 71 mRootNode.setClipToBounds(false); 72 mNativeProxy = nCreateProxy(translucent, rootNodePtr); 73 74 // Setup timing 75 mChoreographer = Choreographer.getInstance(); 76 nSetFrameInterval(mNativeProxy, mChoreographer.getFrameIntervalNanos()); 77 } 78 79 @Override 80 void destroy(boolean full) { 81 mInitialized = false; 82 updateEnabledState(null); 83 nDestroyCanvasAndSurface(mNativeProxy); 84 } 85 86 private void updateEnabledState(Surface surface) { 87 if (surface == null || !surface.isValid()) { 88 setEnabled(false); 89 } else { 90 setEnabled(mInitialized); 91 } 92 } 93 94 @Override 95 boolean initialize(Surface surface) throws OutOfResourcesException { 96 mInitialized = true; 97 updateEnabledState(surface); 98 return nInitialize(mNativeProxy, surface); 99 } 100 101 @Override 102 void updateSurface(Surface surface) throws OutOfResourcesException { 103 updateEnabledState(surface); 104 nUpdateSurface(mNativeProxy, surface); 105 } 106 107 @Override 108 void pauseSurface(Surface surface) { 109 nPauseSurface(mNativeProxy, surface); 110 } 111 112 @Override 113 void destroyHardwareResources(View view) { 114 destroyResources(view); 115 // TODO: GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); 116 } 117 118 private static void destroyResources(View view) { 119 view.destroyHardwareResources(); 120 121 if (view instanceof ViewGroup) { 122 ViewGroup group = (ViewGroup) view; 123 124 int count = group.getChildCount(); 125 for (int i = 0; i < count; i++) { 126 destroyResources(group.getChildAt(i)); 127 } 128 } 129 } 130 131 @Override 132 void invalidate(Surface surface) { 133 updateSurface(surface); 134 } 135 136 @Override 137 boolean safelyRun(Runnable action) { 138 nRunWithGlContext(mNativeProxy, action); 139 return true; 140 } 141 142 @Override 143 void setup(int width, int height) { 144 mWidth = width; 145 mHeight = height; 146 mRootNode.setLeftTopRightBottom(0, 0, mWidth, mHeight); 147 nSetup(mNativeProxy, width, height); 148 } 149 150 @Override 151 int getWidth() { 152 return mWidth; 153 } 154 155 @Override 156 int getHeight() { 157 return mHeight; 158 } 159 160 @Override 161 void dumpGfxInfo(PrintWriter pw) { 162 // TODO Auto-generated method stub 163 } 164 165 @Override 166 long getFrameCount() { 167 // TODO Auto-generated method stub 168 return 0; 169 } 170 171 @Override 172 boolean loadSystemProperties() { 173 return nLoadSystemProperties(mNativeProxy); 174 } 175 176 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) { 177 view.mPrivateFlags |= View.PFLAG_DRAWN; 178 179 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) 180 == View.PFLAG_INVALIDATED; 181 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; 182 183 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); 184 HardwareCanvas canvas = mRootNode.start(mWidth, mHeight); 185 try { 186 callbacks.onHardwarePostDraw(canvas); 187 canvas.drawDisplayList(view.getDisplayList()); 188 callbacks.onHardwarePostDraw(canvas); 189 } finally { 190 mRootNode.end(canvas); 191 Trace.traceEnd(Trace.TRACE_TAG_VIEW); 192 } 193 194 view.mRecreateDisplayList = false; 195 } 196 197 @Override 198 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks, Rect dirty) { 199 attachInfo.mIgnoreDirtyState = true; 200 long frameTimeNanos = mChoreographer.getFrameTimeNanos(); 201 attachInfo.mDrawingTime = frameTimeNanos / NANOS_PER_MS; 202 203 updateRootDisplayList(view, callbacks); 204 205 attachInfo.mIgnoreDirtyState = false; 206 207 if (dirty == null) { 208 dirty = NULL_RECT; 209 } 210 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos, 211 dirty.left, dirty.top, dirty.right, dirty.bottom); 212 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) { 213 attachInfo.mViewRootImpl.invalidate(); 214 } 215 } 216 217 @Override 218 void detachFunctor(long functor) { 219 // no-op, we never attach functors to need to detach them 220 } 221 222 @Override 223 void attachFunctor(AttachInfo attachInfo, long functor) { 224 invokeFunctor(functor, true); 225 } 226 227 @Override 228 void invokeFunctor(long functor, boolean waitForCompletion) { 229 nInvokeFunctor(mNativeProxy, functor, waitForCompletion); 230 } 231 232 @Override 233 HardwareLayer createDisplayListLayer(int width, int height) { 234 long layer = nCreateDisplayListLayer(mNativeProxy, width, height); 235 return HardwareLayer.adoptDisplayListLayer(this, layer); 236 } 237 238 @Override 239 HardwareLayer createTextureLayer() { 240 long layer = nCreateTextureLayer(mNativeProxy); 241 return HardwareLayer.adoptTextureLayer(this, layer); 242 } 243 244 @Override 245 SurfaceTexture createSurfaceTexture(final HardwareLayer layer) { 246 final SurfaceTexture[] ret = new SurfaceTexture[1]; 247 nRunWithGlContext(mNativeProxy, new Runnable() { 248 @Override 249 public void run() { 250 ret[0] = layer.createSurfaceTexture(); 251 } 252 }); 253 return ret[0]; 254 } 255 256 @Override 257 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { 258 return nCopyLayerInto(mNativeProxy, 259 layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap); 260 } 261 262 @Override 263 void pushLayerUpdate(HardwareLayer layer) { 264 // TODO: Remove this, it's not needed outside of GLRenderer 265 } 266 267 @Override 268 void onLayerCreated(HardwareLayer layer) { 269 // TODO: Is this actually useful? 270 } 271 272 @Override 273 void flushLayerUpdates() { 274 // TODO: Figure out what this should do or remove it 275 } 276 277 @Override 278 void onLayerDestroyed(HardwareLayer layer) { 279 nDestroyLayer(mNativeProxy, layer.getDeferredLayerUpdater()); 280 } 281 282 @Override 283 void setName(String name) { 284 } 285 286 @Override 287 void fence() { 288 nFence(mNativeProxy); 289 } 290 291 @Override 292 protected void finalize() throws Throwable { 293 try { 294 nDeleteProxy(mNativeProxy); 295 mNativeProxy = 0; 296 } finally { 297 super.finalize(); 298 } 299 } 300 301 /** @hide */ 302 public static native void postToRenderThread(Runnable runnable); 303 304 private static native long nCreateRootRenderNode(); 305 private static native long nCreateProxy(boolean translucent, long rootRenderNode); 306 private static native void nDeleteProxy(long nativeProxy); 307 308 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos); 309 private static native boolean nLoadSystemProperties(long nativeProxy); 310 311 private static native boolean nInitialize(long nativeProxy, Surface window); 312 private static native void nUpdateSurface(long nativeProxy, Surface window); 313 private static native void nPauseSurface(long nativeProxy, Surface window); 314 private static native void nSetup(long nativeProxy, int width, int height); 315 private static native void nSetDisplayListData(long nativeProxy, long displayList, 316 long newData); 317 private static native int nSyncAndDrawFrame(long nativeProxy, long frameTimeNanos, 318 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom); 319 private static native void nRunWithGlContext(long nativeProxy, Runnable runnable); 320 private static native void nDestroyCanvasAndSurface(long nativeProxy); 321 322 private static native void nInvokeFunctor(long nativeProxy, long functor, boolean waitForCompletion); 323 324 private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height); 325 private static native long nCreateTextureLayer(long nativeProxy); 326 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap); 327 private static native void nDestroyLayer(long nativeProxy, long layer); 328 329 private static native void nFence(long nativeProxy); 330} 331