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