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