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 | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 24 | import android.graphics.Point; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 28 | import android.os.ParcelFileDescriptor; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 31 | import android.os.Trace; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 32 | import android.util.Log; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 33 | import android.view.Surface.OutOfResourcesException; |
| 34 | import android.view.View.AttachInfo; |
| 35 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 36 | import com.android.internal.R; |
| 37 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 39 | import java.io.PrintWriter; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 40 | import java.lang.annotation.Retention; |
| 41 | import java.lang.annotation.RetentionPolicy; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Hardware renderer that proxies the rendering to a render thread. Most calls |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 45 | * are currently synchronous. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 46 | * |
| 47 | * The UI thread can block on the RenderThread, but RenderThread must never |
| 48 | * block on the UI thread. |
| 49 | * |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 50 | * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates |
| 51 | * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed |
| 52 | * by the lifecycle of the RenderProxy. |
| 53 | * |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 54 | * Note that although currently the EGL context & surfaces are created & managed |
| 55 | * by the render thread, the goal is to move that into a shared structure that can |
| 56 | * be managed by both threads. EGLSurface creation & deletion should ideally be |
| 57 | * done on the UI thread and not the RenderThread to avoid stalling the |
| 58 | * RenderThread with surface buffer allocation. |
| 59 | * |
| 60 | * @hide |
| 61 | */ |
| 62 | public class ThreadedRenderer extends HardwareRenderer { |
| 63 | private static final String LOGTAG = "ThreadedRenderer"; |
| 64 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 65 | // Keep in sync with DrawFrameTask.h SYNC_* flags |
| 66 | // Nothing interesting to report |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 67 | private static final int SYNC_OK = 0; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 68 | // Needs a ViewRoot invalidate |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 69 | private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 70 | // Spoiler: the reward is GPU-accelerated drawing, better find that Surface! |
| 71 | private static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 1 << 1; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 72 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 73 | private static final String[] VISUALIZERS = { |
| 74 | PROFILE_PROPERTY_VISUALIZE_BARS, |
| 75 | }; |
| 76 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 77 | private static final int FLAG_DUMP_FRAMESTATS = 1 << 0; |
| 78 | private static final int FLAG_DUMP_RESET = 1 << 1; |
| 79 | |
| 80 | @IntDef(flag = true, value = { |
| 81 | FLAG_DUMP_FRAMESTATS, FLAG_DUMP_RESET }) |
| 82 | @Retention(RetentionPolicy.SOURCE) |
| 83 | public @interface DumpFlags {} |
| 84 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 85 | // Size of the rendered content. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 86 | private int mWidth, mHeight; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 87 | |
| 88 | // Actual size of the drawing surface. |
| 89 | private int mSurfaceWidth, mSurfaceHeight; |
| 90 | |
| 91 | // Insets between the drawing surface and rendered content. These are |
| 92 | // applied as translation when updating the root render node. |
| 93 | private int mInsetTop, mInsetLeft; |
| 94 | |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 95 | // Whether the surface has insets. Used to protect opacity. |
| 96 | private boolean mHasInsets; |
| 97 | |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 98 | // Light and shadow properties specified by the theme. |
| 99 | private final float mLightY; |
| 100 | private final float mLightZ; |
| 101 | private final float mLightRadius; |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 102 | private final int mAmbientShadowAlpha; |
| 103 | private final int mSpotShadowAlpha; |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 104 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 105 | private long mNativeProxy; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 106 | private boolean mInitialized = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 107 | private RenderNode mRootNode; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 108 | private Choreographer mChoreographer; |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 109 | private boolean mRootNodeNeedsUpdate; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 110 | |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 111 | ThreadedRenderer(Context context, boolean translucent) { |
Alan Viverette | ed6f14a | 2014-08-26 14:53:28 -0700 | [diff] [blame] | 112 | final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0); |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 113 | mLightY = a.getDimension(R.styleable.Lighting_lightY, 0); |
| 114 | mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0); |
| 115 | mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0); |
Alan Viverette | ed6f14a | 2014-08-26 14:53:28 -0700 | [diff] [blame] | 116 | mAmbientShadowAlpha = |
| 117 | (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f); |
| 118 | mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f); |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 119 | a.recycle(); |
| 120 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 121 | long rootNodePtr = nCreateRootRenderNode(); |
| 122 | mRootNode = RenderNode.adopt(rootNodePtr); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 123 | mRootNode.setClipToBounds(false); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 124 | mNativeProxy = nCreateProxy(translucent, rootNodePtr); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 125 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 126 | ProcessInitializer.sInstance.init(context, mNativeProxy); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 127 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 128 | loadSystemProperties(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 132 | void destroy() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 133 | mInitialized = false; |
| 134 | updateEnabledState(null); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 135 | nDestroy(mNativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 138 | private void updateEnabledState(Surface surface) { |
| 139 | if (surface == null || !surface.isValid()) { |
| 140 | setEnabled(false); |
| 141 | } else { |
| 142 | setEnabled(mInitialized); |
| 143 | } |
| 144 | } |
| 145 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 146 | @Override |
| 147 | boolean initialize(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 148 | mInitialized = true; |
| 149 | updateEnabledState(surface); |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 150 | boolean status = nInitialize(mNativeProxy, surface); |
| 151 | surface.allocateBuffers(); |
| 152 | return status; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 157 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 158 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 162 | boolean pauseSurface(Surface surface) { |
| 163 | return nPauseSurface(mNativeProxy, surface); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 167 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 168 | destroyResources(view); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 169 | nDestroyHardwareResources(mNativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private static void destroyResources(View view) { |
| 173 | view.destroyHardwareResources(); |
| 174 | |
| 175 | if (view instanceof ViewGroup) { |
| 176 | ViewGroup group = (ViewGroup) view; |
| 177 | |
| 178 | int count = group.getChildCount(); |
| 179 | for (int i = 0; i < count; i++) { |
| 180 | destroyResources(group.getChildAt(i)); |
| 181 | } |
| 182 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 187 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 191 | void detachSurfaceTexture(long hardwareLayer) { |
| 192 | nDetachSurfaceTexture(mNativeProxy, hardwareLayer); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 196 | void setup(int width, int height, AttachInfo attachInfo, Rect surfaceInsets) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 197 | mWidth = width; |
| 198 | mHeight = height; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 199 | |
Alan Viverette | 3aa1ffb | 2014-10-30 12:22:08 -0700 | [diff] [blame] | 200 | if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0 |
| 201 | || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) { |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 202 | mHasInsets = true; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 203 | mInsetLeft = surfaceInsets.left; |
| 204 | mInsetTop = surfaceInsets.top; |
| 205 | mSurfaceWidth = width + mInsetLeft + surfaceInsets.right; |
| 206 | mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom; |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 207 | |
| 208 | // If the surface has insets, it can't be opaque. |
| 209 | setOpaque(false); |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 210 | } else { |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 211 | mHasInsets = false; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 212 | mInsetLeft = 0; |
| 213 | mInsetTop = 0; |
| 214 | mSurfaceWidth = width; |
| 215 | mSurfaceHeight = height; |
| 216 | } |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 217 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 218 | mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 219 | nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight, mLightRadius, |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 220 | mAmbientShadowAlpha, mSpotShadowAlpha); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 221 | |
| 222 | setLightCenter(attachInfo); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | void setLightCenter(AttachInfo attachInfo) { |
| 227 | // Adjust light position for window offsets. |
| 228 | final Point displaySize = attachInfo.mPoint; |
| 229 | attachInfo.mDisplay.getRealSize(displaySize); |
| 230 | final float lightX = displaySize.x / 2f - attachInfo.mWindowLeft; |
| 231 | final float lightY = mLightY - attachInfo.mWindowTop; |
| 232 | |
| 233 | nSetLightCenter(mNativeProxy, lightX, lightY, mLightZ); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 237 | void setOpaque(boolean opaque) { |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 238 | nSetOpaque(mNativeProxy, opaque && !mHasInsets); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 242 | int getWidth() { |
| 243 | return mWidth; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | int getHeight() { |
| 248 | return mHeight; |
| 249 | } |
| 250 | |
| 251 | @Override |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 252 | void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 253 | pw.flush(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 254 | int flags = 0; |
| 255 | for (int i = 0; i < args.length; i++) { |
| 256 | switch (args[i]) { |
| 257 | case "framestats": |
| 258 | flags |= FLAG_DUMP_FRAMESTATS; |
| 259 | break; |
| 260 | case "reset": |
| 261 | flags |= FLAG_DUMP_RESET; |
| 262 | break; |
| 263 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 264 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 265 | nDumpProfileInfo(mNativeProxy, fd, flags); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | boolean loadSystemProperties() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 270 | boolean changed = nLoadSystemProperties(mNativeProxy); |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 271 | if (changed) { |
| 272 | invalidateRoot(); |
| 273 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 274 | return changed; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 275 | } |
| 276 | |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 277 | private void updateViewTreeDisplayList(View view) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 278 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 279 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 280 | == View.PFLAG_INVALIDATED; |
| 281 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 282 | view.updateDisplayListIfDirty(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 283 | view.mRecreateDisplayList = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 284 | } |
| 285 | |
John Reck | 61375a8 | 2014-09-18 19:27:48 +0000 | [diff] [blame] | 286 | private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 287 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()"); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 288 | updateViewTreeDisplayList(view); |
| 289 | |
| 290 | if (mRootNodeNeedsUpdate || !mRootNode.isValid()) { |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 291 | DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 292 | try { |
Alan Viverette | dbed893 | 2014-08-06 17:54:52 -0700 | [diff] [blame] | 293 | final int saveCount = canvas.save(); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 294 | canvas.translate(mInsetLeft, mInsetTop); |
| 295 | callbacks.onHardwarePreDraw(canvas); |
Chris Craik | abedca3 | 2014-08-28 15:03:55 -0700 | [diff] [blame] | 296 | |
| 297 | canvas.insertReorderBarrier(); |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 298 | canvas.drawRenderNode(view.updateDisplayListIfDirty()); |
Chris Craik | abedca3 | 2014-08-28 15:03:55 -0700 | [diff] [blame] | 299 | canvas.insertInorderBarrier(); |
| 300 | |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 301 | callbacks.onHardwarePostDraw(canvas); |
Alan Viverette | dbed893 | 2014-08-06 17:54:52 -0700 | [diff] [blame] | 302 | canvas.restoreToCount(saveCount); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 303 | mRootNodeNeedsUpdate = false; |
| 304 | } finally { |
| 305 | mRootNode.end(canvas); |
| 306 | } |
| 307 | } |
| 308 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | void invalidateRoot() { |
| 313 | mRootNodeNeedsUpdate = true; |
| 314 | } |
| 315 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 316 | @Override |
John Reck | 61375a8 | 2014-09-18 19:27:48 +0000 | [diff] [blame] | 317 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) { |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 318 | attachInfo.mIgnoreDirtyState = true; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 319 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 320 | final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer; |
| 321 | choreographer.mFrameInfo.markDrawStart(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 322 | |
John Reck | 61375a8 | 2014-09-18 19:27:48 +0000 | [diff] [blame] | 323 | updateRootDisplayList(view, callbacks); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 324 | |
John Reck | 6313b92 | 2014-04-16 18:59:21 -0700 | [diff] [blame] | 325 | attachInfo.mIgnoreDirtyState = false; |
| 326 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 327 | // register animating rendernodes which started animating prior to renderer |
| 328 | // creation, which is typical for animators started prior to first draw |
| 329 | if (attachInfo.mPendingAnimatingRenderNodes != null) { |
| 330 | final int count = attachInfo.mPendingAnimatingRenderNodes.size(); |
| 331 | for (int i = 0; i < count; i++) { |
| 332 | registerAnimatingRenderNode( |
| 333 | attachInfo.mPendingAnimatingRenderNodes.get(i)); |
| 334 | } |
| 335 | attachInfo.mPendingAnimatingRenderNodes.clear(); |
| 336 | // We don't need this anymore as subsequent calls to |
| 337 | // ViewRootImpl#attachRenderNodeAnimator will go directly to us. |
| 338 | attachInfo.mPendingAnimatingRenderNodes = null; |
| 339 | } |
| 340 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 341 | final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo; |
| 342 | int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 343 | if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) { |
| 344 | setEnabled(false); |
John Reck | b13de07 | 2014-11-19 16:33:47 -0800 | [diff] [blame] | 345 | attachInfo.mViewRootImpl.mSurface.release(); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 346 | // Invalidate since we failed to draw. This should fetch a Surface |
| 347 | // if it is still needed or do nothing if we are no longer drawing |
| 348 | attachInfo.mViewRootImpl.invalidate(); |
| 349 | } |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 350 | if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) { |
| 351 | attachInfo.mViewRootImpl.invalidate(); |
| 352 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 353 | } |
| 354 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 355 | static void invokeFunctor(long functor, boolean waitForCompletion) { |
| 356 | nInvokeFunctor(functor, waitForCompletion); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 360 | HardwareLayer createTextureLayer() { |
| 361 | long layer = nCreateTextureLayer(mNativeProxy); |
| 362 | return HardwareLayer.adoptTextureLayer(this, layer); |
| 363 | } |
| 364 | |
| 365 | @Override |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 366 | void buildLayer(RenderNode node) { |
| 367 | nBuildLayer(mNativeProxy, node.getNativeDisplayList()); |
| 368 | } |
| 369 | |
| 370 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 371 | boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { |
| 372 | return nCopyLayerInto(mNativeProxy, |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 373 | layer.getDeferredLayerUpdater(), bitmap); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | void pushLayerUpdate(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 378 | nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 382 | void onLayerDestroyed(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 383 | nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 387 | void setName(String name) { |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 388 | nSetName(mNativeProxy, name); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 389 | } |
| 390 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 391 | @Override |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 392 | void fence() { |
| 393 | nFence(mNativeProxy); |
| 394 | } |
| 395 | |
| 396 | @Override |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 397 | void stopDrawing() { |
| 398 | nStopDrawing(mNativeProxy); |
| 399 | } |
| 400 | |
| 401 | @Override |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 402 | public void notifyFramePending() { |
| 403 | nNotifyFramePending(mNativeProxy); |
| 404 | } |
| 405 | |
| 406 | @Override |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 407 | void registerAnimatingRenderNode(RenderNode animator) { |
| 408 | nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode); |
| 409 | } |
| 410 | |
| 411 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 412 | protected void finalize() throws Throwable { |
| 413 | try { |
| 414 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame] | 415 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 416 | } finally { |
| 417 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 421 | static void trimMemory(int level) { |
| 422 | nTrimMemory(level); |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 425 | public static void overrideProperty(@NonNull String name, @NonNull String value) { |
| 426 | if (name == null || value == null) { |
| 427 | throw new IllegalArgumentException("name and value must be non-null"); |
| 428 | } |
| 429 | nOverrideProperty(name, value); |
| 430 | } |
| 431 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 432 | public static void dumpProfileData(byte[] data, FileDescriptor fd) { |
| 433 | nDumpProfileData(data, fd); |
| 434 | } |
| 435 | |
| 436 | private static class ProcessInitializer { |
| 437 | static ProcessInitializer sInstance = new ProcessInitializer(); |
| 438 | static IGraphicsStats sGraphicsStatsService; |
| 439 | private static IBinder sProcToken; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 440 | |
| 441 | private boolean mInitialized = false; |
| 442 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 443 | private ProcessInitializer() {} |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 444 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 445 | synchronized void init(Context context, long renderProxy) { |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 446 | if (mInitialized) return; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 447 | mInitialized = true; |
| 448 | initGraphicsStats(context, renderProxy); |
| 449 | initAssetAtlas(context, renderProxy); |
| 450 | } |
| 451 | |
| 452 | private static void initGraphicsStats(Context context, long renderProxy) { |
| 453 | IBinder binder = ServiceManager.getService("graphicsstats"); |
| 454 | if (binder == null) return; |
| 455 | |
| 456 | sGraphicsStatsService = IGraphicsStats.Stub.asInterface(binder); |
| 457 | sProcToken = new Binder(); |
| 458 | try { |
| 459 | final String pkg = context.getApplicationInfo().packageName; |
| 460 | ParcelFileDescriptor pfd = sGraphicsStatsService. |
| 461 | requestBufferForProcess(pkg, sProcToken); |
| 462 | nSetProcessStatsBuffer(renderProxy, pfd.getFd()); |
| 463 | pfd.close(); |
| 464 | } catch (Exception e) { |
| 465 | Log.w(LOG_TAG, "Could not acquire gfx stats buffer", e); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | private static void initAssetAtlas(Context context, long renderProxy) { |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 470 | IBinder binder = ServiceManager.getService("assetatlas"); |
| 471 | if (binder == null) return; |
| 472 | |
| 473 | IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder); |
| 474 | try { |
| 475 | if (atlas.isCompatible(android.os.Process.myPpid())) { |
| 476 | GraphicBuffer buffer = atlas.getBuffer(); |
| 477 | if (buffer != null) { |
| 478 | long[] map = atlas.getMap(); |
| 479 | if (map != null) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 480 | nSetAtlas(renderProxy, buffer, map); |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 481 | } |
| 482 | // If IAssetAtlas is not the same class as the IBinder |
| 483 | // we are using a remote service and we can safely |
| 484 | // destroy the graphic buffer |
| 485 | if (atlas.getClass() != binder.getClass()) { |
| 486 | buffer.destroy(); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } catch (RemoteException e) { |
| 491 | Log.w(LOG_TAG, "Could not acquire atlas", e); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 496 | static native void setupShadersDiskCache(String cacheFile); |
| 497 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 498 | private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 499 | private static native void nSetProcessStatsBuffer(long nativeProxy, int fd); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 500 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 501 | private static native long nCreateRootRenderNode(); |
| 502 | private static native long nCreateProxy(boolean translucent, long rootRenderNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 503 | private static native void nDeleteProxy(long nativeProxy); |
| 504 | |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 505 | private static native boolean nLoadSystemProperties(long nativeProxy); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 506 | private static native void nSetName(long nativeProxy, String name); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 507 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 508 | private static native boolean nInitialize(long nativeProxy, Surface window); |
| 509 | private static native void nUpdateSurface(long nativeProxy, Surface window); |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 510 | private static native boolean nPauseSurface(long nativeProxy, Surface window); |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 511 | private static native void nSetup(long nativeProxy, int width, int height, |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame^] | 512 | float lightRadius, int ambientShadowAlpha, int spotShadowAlpha); |
| 513 | private static native void nSetLightCenter(long nativeProxy, |
| 514 | float lightX, float lightY, float lightZ); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 515 | private static native void nSetOpaque(long nativeProxy, boolean opaque); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 516 | private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 517 | private static native void nDestroy(long nativeProxy); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 518 | private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 519 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 520 | private static native void nInvokeFunctor(long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 521 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 522 | private static native long nCreateTextureLayer(long nativeProxy); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 523 | private static native void nBuildLayer(long nativeProxy, long node); |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 524 | private static native boolean nCopyLayerInto(long nativeProxy, long layer, Bitmap bitmap); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 525 | private static native void nPushLayerUpdate(long nativeProxy, long layer); |
| 526 | private static native void nCancelLayerUpdate(long nativeProxy, long layer); |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 527 | private static native void nDetachSurfaceTexture(long nativeProxy, long layer); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 528 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 529 | private static native void nDestroyHardwareResources(long nativeProxy); |
| 530 | private static native void nTrimMemory(int level); |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 531 | private static native void nOverrideProperty(String name, String value); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 532 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 533 | private static native void nFence(long nativeProxy); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 534 | private static native void nStopDrawing(long nativeProxy); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 535 | private static native void nNotifyFramePending(long nativeProxy); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 536 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 537 | private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd, |
| 538 | @DumpFlags int dumpFlags); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 539 | private static native void nDumpProfileData(byte[] data, FileDescriptor fd); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 540 | } |