blob: 0ee3b6a819aa0643c225c4ae9a9988f7e534ed69 [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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
17package android.view;
18
John Reckba6adf62015-02-19 14:36:50 -080019import android.annotation.IntDef;
John Reckb8802b12014-06-16 15:28:50 -070020import android.content.Context;
21import android.content.res.Resources;
Alan Viverette58c42c32014-07-12 20:33:45 -070022import android.content.res.TypedArray;
John Reck04fc5832014-02-05 16:38:25 -080023import android.graphics.Bitmap;
Alan Viveretteccb11e12014-07-08 16:04:02 -070024import android.graphics.Rect;
John Reckb8802b12014-06-16 15:28:50 -070025import android.graphics.drawable.Drawable;
John Reck66f0be62014-05-13 13:39:31 -070026import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.ServiceManager;
John Reckcec24ae2013-11-05 13:27:50 -080029import android.os.Trace;
John Reck66f0be62014-05-13 13:39:31 -070030import android.util.Log;
John Reckb8802b12014-06-16 15:28:50 -070031import android.util.LongSparseArray;
John Reckcec24ae2013-11-05 13:27:50 -080032import android.view.Surface.OutOfResourcesException;
33import android.view.View.AttachInfo;
34
John Reckba6adf62015-02-19 14:36:50 -080035import com.android.internal.R;
36
John Reckfe5e7b72014-05-23 17:42:28 -070037import java.io.FileDescriptor;
John Reckcec24ae2013-11-05 13:27:50 -080038import java.io.PrintWriter;
John Reckba6adf62015-02-19 14:36:50 -080039import java.lang.annotation.Retention;
40import java.lang.annotation.RetentionPolicy;
John Reckdad7d84c2014-12-09 12:33:26 -080041import java.util.ArrayList;
John Reckb8802b12014-06-16 15:28:50 -070042import java.util.HashSet;
John Reckcec24ae2013-11-05 13:27:50 -080043
44/**
45 * Hardware renderer that proxies the rendering to a render thread. Most calls
John Reck4f02bf42014-01-03 18:09:17 -080046 * are currently synchronous.
John Reckcec24ae2013-11-05 13:27:50 -080047 *
48 * The UI thread can block on the RenderThread, but RenderThread must never
49 * block on the UI thread.
50 *
John Reck4f02bf42014-01-03 18:09:17 -080051 * 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 Reckcec24ae2013-11-05 13:27:50 -080055 * 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 */
63public class ThreadedRenderer extends HardwareRenderer {
64 private static final String LOGTAG = "ThreadedRenderer";
65
John Reckf9be7792014-05-02 18:21:16 -070066 // Keep in sync with DrawFrameTask.h SYNC_* flags
67 // Nothing interesting to report
John Reckcd028f32014-06-24 08:44:29 -070068 private static final int SYNC_OK = 0;
John Reckf9be7792014-05-02 18:21:16 -070069 // Needs a ViewRoot invalidate
John Reckcd028f32014-06-24 08:44:29 -070070 private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0;
John Reckaa95a882014-11-07 11:02:07 -080071 // 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 Reckf9be7792014-05-02 18:21:16 -070073
John Reckfe5e7b72014-05-23 17:42:28 -070074 private static final String[] VISUALIZERS = {
75 PROFILE_PROPERTY_VISUALIZE_BARS,
76 };
77
John Reckba6adf62015-02-19 14:36:50 -080078 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 Viveretteccb11e12014-07-08 16:04:02 -070086 // Size of the rendered content.
John Reckcec24ae2013-11-05 13:27:50 -080087 private int mWidth, mHeight;
Alan Viveretteccb11e12014-07-08 16:04:02 -070088
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 Viverette57774a82014-07-15 15:49:55 -070096 // Whether the surface has insets. Used to protect opacity.
97 private boolean mHasInsets;
98
Alan Viverette58c42c32014-07-12 20:33:45 -070099 // Light and shadow properties specified by the theme.
100 private final float mLightY;
101 private final float mLightZ;
102 private final float mLightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700103 private final int mAmbientShadowAlpha;
104 private final int mSpotShadowAlpha;
John Reckba6adf62015-02-19 14:36:50 -0800105 private final float mDensity;
Alan Viverette58c42c32014-07-12 20:33:45 -0700106
John Reck4f02bf42014-01-03 18:09:17 -0800107 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -0700108 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -0700109 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -0700110 private Choreographer mChoreographer;
John Reck0a973302014-07-16 13:29:45 -0700111 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800112
John Reckb8802b12014-06-16 15:28:50 -0700113 ThreadedRenderer(Context context, boolean translucent) {
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700114 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700115 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 Viveretteed6f14a2014-08-26 14:53:28 -0700118 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 Viverette58c42c32014-07-12 20:33:45 -0700121 a.recycle();
John Reckba6adf62015-02-19 14:36:50 -0800122 mDensity = context.getResources().getDisplayMetrics().density;
Alan Viverette58c42c32014-07-12 20:33:45 -0700123
John Recke45b1fd2014-04-15 09:50:16 -0700124 long rootNodePtr = nCreateRootRenderNode();
125 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700126 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700127 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700128
John Reck3b202512014-06-23 13:13:08 -0700129 AtlasInitializer.sInstance.init(context, mNativeProxy);
130
John Reck18f16e62014-05-02 16:46:41 -0700131 // Setup timing
132 mChoreographer = Choreographer.getInstance();
133 nSetFrameInterval(mNativeProxy, mChoreographer.getFrameIntervalNanos());
John Reckfe5e7b72014-05-23 17:42:28 -0700134
135 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800136 }
137
138 @Override
John Reckf47a5942014-06-30 16:20:04 -0700139 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700140 mInitialized = false;
141 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700142 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800143 }
144
John Reckf7d9c1d2014-04-09 10:01:03 -0700145 private void updateEnabledState(Surface surface) {
146 if (surface == null || !surface.isValid()) {
147 setEnabled(false);
148 } else {
149 setEnabled(mInitialized);
150 }
151 }
152
John Reckcec24ae2013-11-05 13:27:50 -0800153 @Override
154 boolean initialize(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700155 mInitialized = true;
156 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700157 boolean status = nInitialize(mNativeProxy, surface);
158 surface.allocateBuffers();
159 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800160 }
161
162 @Override
163 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700164 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800165 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800166 }
167
168 @Override
John Reck01a5ea32014-12-03 13:01:07 -0800169 boolean pauseSurface(Surface surface) {
170 return nPauseSurface(mNativeProxy, surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700171 }
172
173 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800174 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800175 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700176 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800177 }
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 Reckcec24ae2013-11-05 13:27:50 -0800190 }
191
192 @Override
193 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800194 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800195 }
196
197 @Override
John Reck918ad522014-06-27 14:45:25 -0700198 void detachSurfaceTexture(long hardwareLayer) {
199 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800200 }
201
202 @Override
Alan Viverette58c42c32014-07-12 20:33:45 -0700203 void setup(int width, int height, Rect surfaceInsets) {
204 final float lightX = width / 2.0f;
John Reckcec24ae2013-11-05 13:27:50 -0800205 mWidth = width;
206 mHeight = height;
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700207 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
208 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700209 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700210 mInsetLeft = surfaceInsets.left;
211 mInsetTop = surfaceInsets.top;
212 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
213 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700214
215 // If the surface has insets, it can't be opaque.
216 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700217 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700218 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700219 mInsetLeft = 0;
220 mInsetTop = 0;
221 mSurfaceWidth = width;
222 mSurfaceHeight = height;
223 }
224 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700225 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
226 lightX, mLightY, mLightZ, mLightRadius,
John Reckba6adf62015-02-19 14:36:50 -0800227 mAmbientShadowAlpha, mSpotShadowAlpha, mDensity);
John Reckcec24ae2013-11-05 13:27:50 -0800228 }
229
230 @Override
John Reck63a06672014-05-07 13:45:54 -0700231 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700232 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700233 }
234
235 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800236 int getWidth() {
237 return mWidth;
238 }
239
240 @Override
241 int getHeight() {
242 return mHeight;
243 }
244
245 @Override
John Reckba6adf62015-02-19 14:36:50 -0800246 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
John Reckfe5e7b72014-05-23 17:42:28 -0700247 pw.flush();
John Reckba6adf62015-02-19 14:36:50 -0800248 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 Reckfe5e7b72014-05-23 17:42:28 -0700258 }
John Reckba6adf62015-02-19 14:36:50 -0800259 nDumpProfileInfo(mNativeProxy, fd, flags);
John Reckcec24ae2013-11-05 13:27:50 -0800260 }
261
262 @Override
263 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700264 boolean changed = nLoadSystemProperties(mNativeProxy);
John Reck23d307c2014-10-27 12:38:48 -0700265 if (changed) {
266 invalidateRoot();
267 }
John Reckfe5e7b72014-05-23 17:42:28 -0700268 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800269 }
270
John Reck0a973302014-07-16 13:29:45 -0700271 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800272 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800273 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
274 == View.PFLAG_INVALIDATED;
275 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
John Reck0a973302014-07-16 13:29:45 -0700276 view.getDisplayList();
John Reckcec24ae2013-11-05 13:27:50 -0800277 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700278 }
279
John Reck61375a82014-09-18 19:27:48 +0000280 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
Chris Craik70850ea2014-11-18 10:49:23 -0800281 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
John Reck0a973302014-07-16 13:29:45 -0700282 updateViewTreeDisplayList(view);
283
284 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
Chris Craikf6829a02015-03-10 10:28:59 -0700285 DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
John Reck0a973302014-07-16 13:29:45 -0700286 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700287 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700288 canvas.translate(mInsetLeft, mInsetTop);
289 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700290
291 canvas.insertReorderBarrier();
John Reck0a973302014-07-16 13:29:45 -0700292 canvas.drawRenderNode(view.getDisplayList());
Chris Craikabedca32014-08-28 15:03:55 -0700293 canvas.insertInorderBarrier();
294
John Reck0a973302014-07-16 13:29:45 -0700295 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700296 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700297 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 Reckbc0cc022014-04-11 16:08:14 -0700310 @Override
John Reck61375a82014-09-18 19:27:48 +0000311 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700312 attachInfo.mIgnoreDirtyState = true;
John Reckbc0cc022014-04-11 16:08:14 -0700313
John Reckba6adf62015-02-19 14:36:50 -0800314 final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer;
315 choreographer.mFrameInfo.markDrawStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700316
John Reck61375a82014-09-18 19:27:48 +0000317 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800318
John Reck6313b922014-04-16 18:59:21 -0700319 attachInfo.mIgnoreDirtyState = false;
320
John Reck119907c2014-08-14 09:02:01 -0700321 // 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 Reckba6adf62015-02-19 14:36:50 -0800335 final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo;
336 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length);
John Reckaa95a882014-11-07 11:02:07 -0800337 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
338 setEnabled(false);
John Reckb13de072014-11-19 16:33:47 -0800339 attachInfo.mViewRootImpl.mSurface.release();
John Reckaa95a882014-11-07 11:02:07 -0800340 // 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 Reckf9be7792014-05-02 18:21:16 -0700344 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
345 attachInfo.mViewRootImpl.invalidate();
346 }
John Reckcec24ae2013-11-05 13:27:50 -0800347 }
348
John Reck3b202512014-06-23 13:13:08 -0700349 static void invokeFunctor(long functor, boolean waitForCompletion) {
350 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700351 }
352
353 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800354 HardwareLayer createTextureLayer() {
355 long layer = nCreateTextureLayer(mNativeProxy);
356 return HardwareLayer.adoptTextureLayer(this, layer);
357 }
358
359 @Override
John Reck3e824952014-08-20 10:08:39 -0700360 void buildLayer(RenderNode node) {
361 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
362 }
363
364 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800365 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
366 return nCopyLayerInto(mNativeProxy,
John Reckf4faeac2015-03-05 13:50:31 -0800367 layer.getDeferredLayerUpdater(), bitmap.getSkBitmap());
John Reck19b6bcf2014-02-14 20:03:38 -0800368 }
369
370 @Override
371 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700372 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800373 }
374
375 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800376 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700377 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800378 }
379
380 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800381 void setName(String name) {
John Reckcec24ae2013-11-05 13:27:50 -0800382 }
383
John Reck4f02bf42014-01-03 18:09:17 -0800384 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700385 void fence() {
386 nFence(mNativeProxy);
387 }
388
389 @Override
John Reckf47a5942014-06-30 16:20:04 -0700390 void stopDrawing() {
391 nStopDrawing(mNativeProxy);
392 }
393
394 @Override
John Recka5dda642014-05-22 15:43:54 -0700395 public void notifyFramePending() {
396 nNotifyFramePending(mNativeProxy);
397 }
398
399 @Override
John Reck119907c2014-08-14 09:02:01 -0700400 void registerAnimatingRenderNode(RenderNode animator) {
401 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
402 }
403
404 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800405 protected void finalize() throws Throwable {
406 try {
407 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700408 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800409 } finally {
410 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800411 }
412 }
413
John Reckf47a5942014-06-30 16:20:04 -0700414 static void trimMemory(int level) {
415 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700416 }
417
John Reck66f0be62014-05-13 13:39:31 -0700418 private static class AtlasInitializer {
419 static AtlasInitializer sInstance = new AtlasInitializer();
420
421 private boolean mInitialized = false;
422
423 private AtlasInitializer() {}
424
John Reck3b202512014-06-23 13:13:08 -0700425 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700426 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 Reckb8802b12014-06-16 15:28:50 -0700437 // TODO Remove after fixing b/15425820
438 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700439 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700440 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 Reckb8802b12014-06-16 15:28:50 -0700454
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 Reckdad7d84c2014-12-09 12:33:26 -0800464 ArrayList<Bitmap> tmpList = new ArrayList<Bitmap>();
John Reckb8802b12014-06-16 15:28:50 -0700465 for (int i = 0; i < count; i++) {
John Reckdad7d84c2014-12-09 12:33:26 -0800466 drawables.valueAt(i).addAtlasableBitmaps(tmpList);
467 for (int j = 0; j < tmpList.size(); j++) {
John Reckf4faeac2015-03-05 13:50:31 -0800468 preloadedPointers.add(tmpList.get(j).getSkBitmap());
John Reckb8802b12014-06-16 15:28:50 -0700469 }
John Reckdad7d84c2014-12-09 12:33:26 -0800470 tmpList.clear();
John Reckb8802b12014-06-16 15:28:50 -0700471 }
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 Reck66f0be62014-05-13 13:39:31 -0700480 }
481
John Reck84a4c882014-05-30 14:34:03 -0700482 static native void setupShadersDiskCache(String cacheFile);
483
John Reck3b202512014-06-23 13:13:08 -0700484 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800485
John Recke45b1fd2014-04-15 09:50:16 -0700486 private static native long nCreateRootRenderNode();
487 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800488 private static native void nDeleteProxy(long nativeProxy);
489
John Reck18f16e62014-05-02 16:46:41 -0700490 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos);
John Recke4280ba2014-05-05 16:39:37 -0700491 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reck18f16e62014-05-02 16:46:41 -0700492
John Reck4f02bf42014-01-03 18:09:17 -0800493 private static native boolean nInitialize(long nativeProxy, Surface window);
494 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reck01a5ea32014-12-03 13:01:07 -0800495 private static native boolean nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700496 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700497 float lightX, float lightY, float lightZ, float lightRadius,
John Reckba6adf62015-02-19 14:36:50 -0800498 int ambientShadowAlpha, int spotShadowAlpha, float density);
John Reck63a06672014-05-07 13:45:54 -0700499 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckba6adf62015-02-19 14:36:50 -0800500 private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
John Reck17035b02014-09-03 07:39:53 -0700501 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700502 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800503
John Reck3b202512014-06-23 13:13:08 -0700504 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800505
John Reck19b6bcf2014-02-14 20:03:38 -0800506 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700507 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800508 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700509 private static native void nPushLayerUpdate(long nativeProxy, long layer);
510 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700511 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700512
John Reckf47a5942014-06-30 16:20:04 -0700513 private static native void nDestroyHardwareResources(long nativeProxy);
514 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700515
John Reck28ad7b52014-04-07 16:59:25 -0700516 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700517 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700518 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700519
John Reckba6adf62015-02-19 14:36:50 -0800520 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
521 @DumpFlags int dumpFlags);
John Reckcec24ae2013-11-05 13:27:50 -0800522}