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