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 | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 57 | |
John Reck | 3dfe19f | 2013-12-13 14:25:19 -0800 | [diff] [blame] | 58 | ThreadedRenderer(boolean translucent) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 59 | mNativeProxy = nCreateProxy(translucent); |
| 60 | setEnabled(mNativeProxy != 0); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | void destroy(boolean full) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 65 | nDestroyCanvas(mNativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | boolean initialize(Surface surface) throws OutOfResourcesException { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 70 | return nInitialize(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 75 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 79 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 99 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 103 | boolean safelyRun(Runnable action) { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 104 | nRunWithGlContext(mNativeProxy, action); |
| 105 | return true; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | void setup(int width, int height) { |
| 110 | mWidth = width; |
| 111 | mHeight = height; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 112 | nSetup(mNativeProxy, width, height); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 138 | return false; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 139 | } |
| 140 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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() { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 151 | void setDisplayListData(long displayList, long newData) { |
| 152 | nSetDisplayListData(mNativeProxy, displayList, newData); |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 156 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks, Rect dirty) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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"); |
John Reck | f666ad7 | 2014-03-14 16:24:57 -0700 | [diff] [blame] | 166 | RenderNode displayList = view.getDisplayList(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 167 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 168 | |
| 169 | view.mRecreateDisplayList = false; |
| 170 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 171 | if (dirty == null) { |
| 172 | dirty = NULL_RECT; |
| 173 | } |
| 174 | nDrawDisplayList(mNativeProxy, displayList.getNativeDisplayList(), |
| 175 | dirty.left, dirty.top, dirty.right, dirty.bottom); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 179 | void detachFunctor(long functor) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 180 | nDetachFunctor(mNativeProxy, functor); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 184 | void attachFunctor(AttachInfo attachInfo, long functor) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 185 | nAttachFunctor(mNativeProxy, functor); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 189 | public void invokeFunctor(long functor, boolean waitForCompletion) { |
| 190 | nInvokeFunctor(mNativeProxy, functor, waitForCompletion); |
| 191 | } |
| 192 | |
| 193 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 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 |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 244 | void setName(String name) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 245 | } |
| 246 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 247 | @Override |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 248 | void fence() { |
| 249 | nFence(mNativeProxy); |
| 250 | } |
| 251 | |
| 252 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 253 | protected void finalize() throws Throwable { |
| 254 | try { |
| 255 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame^] | 256 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 257 | } finally { |
| 258 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | /** @hide */ |
| 263 | public static native void postToRenderThread(Runnable runnable); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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); |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 271 | private static native void nSetDisplayListData(long nativeProxy, long displayList, |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 272 | long newData); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 273 | private static native void nDrawDisplayList(long nativeProxy, long displayList, |
| 274 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom); |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 275 | private static native void nRunWithGlContext(long nativeProxy, Runnable runnable); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 280 | private static native void nInvokeFunctor(long nativeProxy, long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 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); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 286 | |
| 287 | private static native void nFence(long nativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 288 | } |