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