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 | |
| 19 | import android.graphics.Rect; |
| 20 | import android.graphics.SurfaceTexture; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 21 | import android.os.SystemClock; |
| 22 | import android.os.Trace; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 23 | import android.view.Surface.OutOfResourcesException; |
| 24 | import android.view.View.AttachInfo; |
| 25 | |
| 26 | import java.io.PrintWriter; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Hardware renderer that proxies the rendering to a render thread. Most calls |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 30 | * are currently synchronous. |
| 31 | * TODO: Make draw() async. |
| 32 | * 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] | 33 | * |
| 34 | * The UI thread can block on the RenderThread, but RenderThread must never |
| 35 | * block on the UI thread. |
| 36 | * |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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 | * |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 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 | */ |
| 49 | public class ThreadedRenderer extends HardwareRenderer { |
| 50 | private static final String LOGTAG = "ThreadedRenderer"; |
| 51 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 52 | private static final Rect NULL_RECT = new Rect(-1, -1, -1, -1); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 53 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 54 | private int mWidth, mHeight; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 55 | private long mNativeProxy; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 56 | |
John Reck | 3dfe19f | 2013-12-13 14:25:19 -0800 | [diff] [blame] | 57 | ThreadedRenderer(boolean translucent) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 58 | mNativeProxy = nCreateProxy(translucent); |
| 59 | setEnabled(mNativeProxy != 0); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | void destroy(boolean full) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 64 | nDestroyCanvas(mNativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | boolean initialize(Surface surface) throws OutOfResourcesException { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 69 | return nInitialize(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 74 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | void destroyLayers(View view) { |
| 79 | throw new NoSuchMethodError(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 84 | // TODO: canvas.clearLayerUpdates() |
| 85 | destroyResources(view); |
| 86 | // TODO: GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 87 | } |
| 88 | |
| 89 | private static void destroyResources(View view) { |
| 90 | view.destroyHardwareResources(); |
| 91 | |
| 92 | if (view instanceof ViewGroup) { |
| 93 | ViewGroup group = (ViewGroup) view; |
| 94 | |
| 95 | int count = group.getChildCount(); |
| 96 | for (int i = 0; i < count; i++) { |
| 97 | destroyResources(group.getChildAt(i)); |
| 98 | } |
| 99 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 104 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | boolean validate() { |
| 109 | // TODO Remove users of this API |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | boolean safelyRun(Runnable action) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 115 | // TODO: |
| 116 | return false; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | void setup(int width, int height) { |
| 121 | mWidth = width; |
| 122 | mHeight = height; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 123 | nSetup(mNativeProxy, width, height); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | int getWidth() { |
| 128 | return mWidth; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | int getHeight() { |
| 133 | return mHeight; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | void dumpGfxInfo(PrintWriter pw) { |
| 138 | // TODO Auto-generated method stub |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | long getFrameCount() { |
| 143 | // TODO Auto-generated method stub |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | boolean loadSystemProperties() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 149 | return false; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | void pushLayerUpdate(HardwareLayer layer) { |
| 154 | throw new NoSuchMethodError(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | void cancelLayerUpdate(HardwareLayer layer) { |
| 159 | throw new NoSuchMethodError(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | void flushLayerUpdates() { |
| 164 | throw new NoSuchMethodError(); |
| 165 | } |
| 166 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 167 | /** |
| 168 | * TODO: Remove |
| 169 | * Temporary hack to allow RenderThreadTest prototype app to trigger |
| 170 | * replaying a DisplayList after modifying the displaylist properties |
| 171 | * |
| 172 | * @hide */ |
| 173 | public void repeatLastDraw() { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks, Rect dirty) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 178 | attachInfo.mIgnoreDirtyState = true; |
| 179 | attachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 180 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
| 181 | |
| 182 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 183 | == View.PFLAG_INVALIDATED; |
| 184 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 185 | |
| 186 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
| 187 | DisplayList displayList = view.getDisplayList(); |
| 188 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 189 | |
| 190 | view.mRecreateDisplayList = false; |
| 191 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 192 | if (dirty == null) { |
| 193 | dirty = NULL_RECT; |
| 194 | } |
| 195 | nDrawDisplayList(mNativeProxy, displayList.getNativeDisplayList(), |
| 196 | dirty.left, dirty.top, dirty.right, dirty.bottom); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | HardwareLayer createHardwareLayer(boolean isOpaque) { |
| 201 | throw new NoSuchMethodError(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque) { |
| 206 | throw new NoSuchMethodError(); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | SurfaceTexture createSurfaceTexture(HardwareLayer layer) { |
| 211 | throw new NoSuchMethodError(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture) { |
| 216 | throw new NoSuchMethodError(); |
| 217 | } |
| 218 | |
| 219 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 220 | void detachFunctor(long functor) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 221 | nDetachFunctor(mNativeProxy, functor); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 225 | void attachFunctor(AttachInfo attachInfo, long functor) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 226 | nAttachFunctor(mNativeProxy, functor); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | void setName(String name) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 231 | } |
| 232 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 233 | @Override |
| 234 | protected void finalize() throws Throwable { |
| 235 | try { |
| 236 | nDeleteProxy(mNativeProxy); |
| 237 | } finally { |
| 238 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /** @hide */ |
| 243 | public static native void postToRenderThread(Runnable runnable); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 244 | |
| 245 | private static native long nCreateProxy(boolean translucent); |
| 246 | private static native void nDeleteProxy(long nativeProxy); |
| 247 | |
| 248 | private static native boolean nInitialize(long nativeProxy, Surface window); |
| 249 | private static native void nUpdateSurface(long nativeProxy, Surface window); |
| 250 | private static native void nSetup(long nativeProxy, int width, int height); |
| 251 | private static native void nDrawDisplayList(long nativeProxy, long displayList, |
| 252 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom); |
| 253 | private static native void nDestroyCanvas(long nativeProxy); |
| 254 | |
| 255 | private static native void nAttachFunctor(long nativeProxy, long functor); |
| 256 | private static native void nDetachFunctor(long nativeProxy, long functor); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 257 | } |