John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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 | |
| 17 | package android.view; |
| 18 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 20 | import android.graphics.Rect; |
| 21 | import android.graphics.SurfaceTexture; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 22 | import android.os.SystemClock; |
| 23 | import android.os.Trace; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 24 | import android.view.Surface.OutOfResourcesException; |
| 25 | import android.view.View.AttachInfo; |
| 26 | |
| 27 | import java.io.PrintWriter; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Hardware renderer that proxies the rendering to a render thread. Most calls |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 31 | * are currently synchronous. |
| 32 | * TODO: Make draw() async. |
| 33 | * TODO: Figure out how to share the DisplayList between two threads (global lock?) |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 34 | * |
| 35 | * The UI thread can block on the RenderThread, but RenderThread must never |
| 36 | * block on the UI thread. |
| 37 | * |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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 | * |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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 | */ |
| 50 | public class ThreadedRenderer extends HardwareRenderer { |
| 51 | private static final String LOGTAG = "ThreadedRenderer"; |
| 52 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 53 | private static final Rect NULL_RECT = new Rect(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 54 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 55 | private int mWidth, mHeight; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 56 | private long mNativeProxy; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 57 | private boolean mInitialized = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 58 | private RenderNode mRootNode; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 59 | |
John Reck | 3dfe19f | 2013-12-13 14:25:19 -0800 | [diff] [blame] | 60 | ThreadedRenderer(boolean translucent) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 61 | long rootNodePtr = nCreateRootRenderNode(); |
| 62 | mRootNode = RenderNode.adopt(rootNodePtr); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 63 | mRootNode.setClipToBounds(false); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 64 | mNativeProxy = nCreateProxy(translucent, rootNodePtr); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | void destroy(boolean full) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 69 | mInitialized = false; |
| 70 | updateEnabledState(null); |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 71 | nDestroyCanvasAndSurface(mNativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 72 | } |
| 73 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 74 | private void updateEnabledState(Surface surface) { |
| 75 | if (surface == null || !surface.isValid()) { |
| 76 | setEnabled(false); |
| 77 | } else { |
| 78 | setEnabled(mInitialized); |
| 79 | } |
| 80 | } |
| 81 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 82 | @Override |
| 83 | boolean initialize(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 84 | mInitialized = true; |
| 85 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 86 | return nInitialize(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 91 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 92 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 96 | void pauseSurface(Surface surface) { |
| 97 | nPauseSurface(mNativeProxy, surface); |
| 98 | } |
| 99 | |
| 100 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 101 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 102 | destroyResources(view); |
| 103 | // TODO: GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 104 | } |
| 105 | |
| 106 | private static void destroyResources(View view) { |
| 107 | view.destroyHardwareResources(); |
| 108 | |
| 109 | if (view instanceof ViewGroup) { |
| 110 | ViewGroup group = (ViewGroup) view; |
| 111 | |
| 112 | int count = group.getChildCount(); |
| 113 | for (int i = 0; i < count; i++) { |
| 114 | destroyResources(group.getChildAt(i)); |
| 115 | } |
| 116 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 121 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 125 | boolean safelyRun(Runnable action) { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 126 | nRunWithGlContext(mNativeProxy, action); |
| 127 | return true; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | void setup(int width, int height) { |
| 132 | mWidth = width; |
| 133 | mHeight = height; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 134 | mRootNode.setLeftTopRightBottom(0, 0, mWidth, mHeight); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 135 | nSetup(mNativeProxy, width, height); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | int getWidth() { |
| 140 | return mWidth; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | int getHeight() { |
| 145 | return mHeight; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | void dumpGfxInfo(PrintWriter pw) { |
| 150 | // TODO Auto-generated method stub |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | long getFrameCount() { |
| 155 | // TODO Auto-generated method stub |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | boolean loadSystemProperties() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 161 | return false; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 162 | } |
| 163 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 164 | /** |
| 165 | * TODO: Remove |
| 166 | * Temporary hack to allow RenderThreadTest prototype app to trigger |
| 167 | * replaying a DisplayList after modifying the displaylist properties |
| 168 | * |
| 169 | * @hide */ |
| 170 | public void repeatLastDraw() { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 171 | } |
| 172 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 173 | private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 174 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
| 175 | |
| 176 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 177 | == View.PFLAG_INVALIDATED; |
| 178 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 179 | |
| 180 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 181 | HardwareCanvas canvas = mRootNode.start(mWidth, mHeight); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 182 | try { |
| 183 | callbacks.onHardwarePostDraw(canvas); |
| 184 | canvas.drawDisplayList(view.getDisplayList()); |
| 185 | callbacks.onHardwarePostDraw(canvas); |
| 186 | } finally { |
| 187 | mRootNode.end(canvas); |
| 188 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 189 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 190 | |
| 191 | view.mRecreateDisplayList = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks, Rect dirty) { |
| 196 | attachInfo.mIgnoreDirtyState = true; |
| 197 | attachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 198 | |
| 199 | updateRootDisplayList(view, callbacks); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 200 | |
John Reck | 6313b92 | 2014-04-16 18:59:21 -0700 | [diff] [blame] | 201 | attachInfo.mIgnoreDirtyState = false; |
| 202 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 203 | if (dirty == null) { |
| 204 | dirty = NULL_RECT; |
| 205 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 206 | nSyncAndDrawFrame(mNativeProxy, dirty.left, dirty.top, dirty.right, dirty.bottom); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 210 | void detachFunctor(long functor) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 211 | // no-op, we never attach functors to need to detach them |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 215 | void attachFunctor(AttachInfo attachInfo, long functor) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 216 | invokeFunctor(functor, true); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
Bo Liu | ae738a7 | 2014-04-27 16:22:04 -0700 | [diff] [blame] | 220 | void invokeFunctor(long functor, boolean waitForCompletion) { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 221 | nInvokeFunctor(mNativeProxy, functor, waitForCompletion); |
| 222 | } |
| 223 | |
| 224 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 225 | HardwareLayer createDisplayListLayer(int width, int height) { |
| 226 | long layer = nCreateDisplayListLayer(mNativeProxy, width, height); |
| 227 | return HardwareLayer.adoptDisplayListLayer(this, layer); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | HardwareLayer createTextureLayer() { |
| 232 | long layer = nCreateTextureLayer(mNativeProxy); |
| 233 | return HardwareLayer.adoptTextureLayer(this, layer); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | SurfaceTexture createSurfaceTexture(final HardwareLayer layer) { |
| 238 | final SurfaceTexture[] ret = new SurfaceTexture[1]; |
| 239 | nRunWithGlContext(mNativeProxy, new Runnable() { |
| 240 | @Override |
| 241 | public void run() { |
| 242 | ret[0] = layer.createSurfaceTexture(); |
| 243 | } |
| 244 | }); |
| 245 | return ret[0]; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { |
| 250 | return nCopyLayerInto(mNativeProxy, |
| 251 | layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | void pushLayerUpdate(HardwareLayer layer) { |
| 256 | // TODO: Remove this, it's not needed outside of GLRenderer |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | void onLayerCreated(HardwareLayer layer) { |
| 261 | // TODO: Is this actually useful? |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | void flushLayerUpdates() { |
| 266 | // TODO: Figure out what this should do or remove it |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | void onLayerDestroyed(HardwareLayer layer) { |
| 271 | nDestroyLayer(mNativeProxy, layer.getDeferredLayerUpdater()); |
| 272 | } |
| 273 | |
| 274 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 275 | void setName(String name) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 276 | } |
| 277 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 278 | @Override |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 279 | void fence() { |
| 280 | nFence(mNativeProxy); |
| 281 | } |
| 282 | |
| 283 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 284 | protected void finalize() throws Throwable { |
| 285 | try { |
| 286 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame] | 287 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 288 | } finally { |
| 289 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** @hide */ |
| 294 | public static native void postToRenderThread(Runnable runnable); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 295 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 296 | private static native long nCreateRootRenderNode(); |
| 297 | private static native long nCreateProxy(boolean translucent, long rootRenderNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 298 | private static native void nDeleteProxy(long nativeProxy); |
| 299 | |
| 300 | private static native boolean nInitialize(long nativeProxy, Surface window); |
| 301 | private static native void nUpdateSurface(long nativeProxy, Surface window); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 302 | private static native void nPauseSurface(long nativeProxy, Surface window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 303 | private static native void nSetup(long nativeProxy, int width, int height); |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 304 | private static native void nSetDisplayListData(long nativeProxy, long displayList, |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 305 | long newData); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 306 | private static native void nSyncAndDrawFrame(long nativeProxy, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 307 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom); |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 308 | private static native void nRunWithGlContext(long nativeProxy, Runnable runnable); |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 309 | private static native void nDestroyCanvasAndSurface(long nativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 310 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 311 | private static native void nInvokeFunctor(long nativeProxy, long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 312 | |
| 313 | private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height); |
| 314 | private static native long nCreateTextureLayer(long nativeProxy); |
| 315 | private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap); |
| 316 | private static native void nDestroyLayer(long nativeProxy, long layer); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 317 | |
| 318 | private static native void nFence(long nativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 319 | } |