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