blob: 51fefe90f1aa44cd53fa52fe4729bafa577bd996 [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;
Alan Viverette58c42c32014-07-12 20:33:45 -0700105
John Reck4f02bf42014-01-03 18:09:17 -0800106 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -0700107 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -0700108 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -0700109 private Choreographer mChoreographer;
John Reck0a973302014-07-16 13:29:45 -0700110 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800111
John Reckb8802b12014-06-16 15:28:50 -0700112 ThreadedRenderer(Context context, boolean translucent) {
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700113 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700114 mLightY = a.getDimension(R.styleable.Lighting_lightY, 0);
115 mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0);
116 mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0);
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700117 mAmbientShadowAlpha =
118 (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f);
119 mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f);
Alan Viverette58c42c32014-07-12 20:33:45 -0700120 a.recycle();
121
John Recke45b1fd2014-04-15 09:50:16 -0700122 long rootNodePtr = nCreateRootRenderNode();
123 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700124 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700125 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700126
John Reck3b202512014-06-23 13:13:08 -0700127 AtlasInitializer.sInstance.init(context, mNativeProxy);
128
John Reckfe5e7b72014-05-23 17:42:28 -0700129 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800130 }
131
132 @Override
John Reckf47a5942014-06-30 16:20:04 -0700133 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700134 mInitialized = false;
135 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700136 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800137 }
138
John Reckf7d9c1d2014-04-09 10:01:03 -0700139 private void updateEnabledState(Surface surface) {
140 if (surface == null || !surface.isValid()) {
141 setEnabled(false);
142 } else {
143 setEnabled(mInitialized);
144 }
145 }
146
John Reckcec24ae2013-11-05 13:27:50 -0800147 @Override
148 boolean initialize(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700149 mInitialized = true;
150 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700151 boolean status = nInitialize(mNativeProxy, surface);
152 surface.allocateBuffers();
153 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800154 }
155
156 @Override
157 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700158 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800159 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800160 }
161
162 @Override
John Reck01a5ea32014-12-03 13:01:07 -0800163 boolean pauseSurface(Surface surface) {
164 return nPauseSurface(mNativeProxy, surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700165 }
166
167 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800168 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800169 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700170 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800171 }
172
173 private static void destroyResources(View view) {
174 view.destroyHardwareResources();
175
176 if (view instanceof ViewGroup) {
177 ViewGroup group = (ViewGroup) view;
178
179 int count = group.getChildCount();
180 for (int i = 0; i < count; i++) {
181 destroyResources(group.getChildAt(i));
182 }
183 }
John Reckcec24ae2013-11-05 13:27:50 -0800184 }
185
186 @Override
187 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800188 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800189 }
190
191 @Override
John Reck918ad522014-06-27 14:45:25 -0700192 void detachSurfaceTexture(long hardwareLayer) {
193 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800194 }
195
196 @Override
Alan Viverette58c42c32014-07-12 20:33:45 -0700197 void setup(int width, int height, Rect surfaceInsets) {
198 final float lightX = width / 2.0f;
John Reckcec24ae2013-11-05 13:27:50 -0800199 mWidth = width;
200 mHeight = height;
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700201 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
202 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700203 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700204 mInsetLeft = surfaceInsets.left;
205 mInsetTop = surfaceInsets.top;
206 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
207 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700208
209 // If the surface has insets, it can't be opaque.
210 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700211 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700212 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700213 mInsetLeft = 0;
214 mInsetTop = 0;
215 mSurfaceWidth = width;
216 mSurfaceHeight = height;
217 }
218 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700219 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
220 lightX, mLightY, mLightZ, mLightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700221 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800222 }
223
224 @Override
John Reck63a06672014-05-07 13:45:54 -0700225 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700226 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700227 }
228
229 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800230 int getWidth() {
231 return mWidth;
232 }
233
234 @Override
235 int getHeight() {
236 return mHeight;
237 }
238
239 @Override
John Reckba6adf62015-02-19 14:36:50 -0800240 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
John Reckfe5e7b72014-05-23 17:42:28 -0700241 pw.flush();
John Reckba6adf62015-02-19 14:36:50 -0800242 int flags = 0;
243 for (int i = 0; i < args.length; i++) {
244 switch (args[i]) {
245 case "framestats":
246 flags |= FLAG_DUMP_FRAMESTATS;
247 break;
248 case "reset":
249 flags |= FLAG_DUMP_RESET;
250 break;
251 }
John Reckfe5e7b72014-05-23 17:42:28 -0700252 }
John Reckba6adf62015-02-19 14:36:50 -0800253 nDumpProfileInfo(mNativeProxy, fd, flags);
John Reckcec24ae2013-11-05 13:27:50 -0800254 }
255
256 @Override
257 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700258 boolean changed = nLoadSystemProperties(mNativeProxy);
John Reck23d307c2014-10-27 12:38:48 -0700259 if (changed) {
260 invalidateRoot();
261 }
John Reckfe5e7b72014-05-23 17:42:28 -0700262 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800263 }
264
John Reck0a973302014-07-16 13:29:45 -0700265 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800266 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800267 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
268 == View.PFLAG_INVALIDATED;
269 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
John Reck0a973302014-07-16 13:29:45 -0700270 view.getDisplayList();
John Reckcec24ae2013-11-05 13:27:50 -0800271 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700272 }
273
John Reck61375a82014-09-18 19:27:48 +0000274 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
Chris Craik70850ea2014-11-18 10:49:23 -0800275 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
John Reck0a973302014-07-16 13:29:45 -0700276 updateViewTreeDisplayList(view);
277
278 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
279 HardwareCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
280 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700281 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700282 canvas.translate(mInsetLeft, mInsetTop);
283 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700284
285 canvas.insertReorderBarrier();
John Reck0a973302014-07-16 13:29:45 -0700286 canvas.drawRenderNode(view.getDisplayList());
Chris Craikabedca32014-08-28 15:03:55 -0700287 canvas.insertInorderBarrier();
288
John Reck0a973302014-07-16 13:29:45 -0700289 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700290 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700291 mRootNodeNeedsUpdate = false;
292 } finally {
293 mRootNode.end(canvas);
294 }
295 }
296 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
297 }
298
299 @Override
300 void invalidateRoot() {
301 mRootNodeNeedsUpdate = true;
302 }
303
John Reckbc0cc022014-04-11 16:08:14 -0700304 @Override
John Reck61375a82014-09-18 19:27:48 +0000305 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700306 attachInfo.mIgnoreDirtyState = true;
John Reckbc0cc022014-04-11 16:08:14 -0700307
John Reckba6adf62015-02-19 14:36:50 -0800308 final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer;
309 choreographer.mFrameInfo.markDrawStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700310
John Reck61375a82014-09-18 19:27:48 +0000311 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800312
John Reck6313b922014-04-16 18:59:21 -0700313 attachInfo.mIgnoreDirtyState = false;
314
John Reck119907c2014-08-14 09:02:01 -0700315 // register animating rendernodes which started animating prior to renderer
316 // creation, which is typical for animators started prior to first draw
317 if (attachInfo.mPendingAnimatingRenderNodes != null) {
318 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
319 for (int i = 0; i < count; i++) {
320 registerAnimatingRenderNode(
321 attachInfo.mPendingAnimatingRenderNodes.get(i));
322 }
323 attachInfo.mPendingAnimatingRenderNodes.clear();
324 // We don't need this anymore as subsequent calls to
325 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
326 attachInfo.mPendingAnimatingRenderNodes = null;
327 }
328
John Reckba6adf62015-02-19 14:36:50 -0800329 final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo;
330 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length);
John Reckaa95a882014-11-07 11:02:07 -0800331 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
332 setEnabled(false);
John Reckb13de072014-11-19 16:33:47 -0800333 attachInfo.mViewRootImpl.mSurface.release();
John Reckaa95a882014-11-07 11:02:07 -0800334 // Invalidate since we failed to draw. This should fetch a Surface
335 // if it is still needed or do nothing if we are no longer drawing
336 attachInfo.mViewRootImpl.invalidate();
337 }
John Reckf9be7792014-05-02 18:21:16 -0700338 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
339 attachInfo.mViewRootImpl.invalidate();
340 }
John Reckcec24ae2013-11-05 13:27:50 -0800341 }
342
John Reck3b202512014-06-23 13:13:08 -0700343 static void invokeFunctor(long functor, boolean waitForCompletion) {
344 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700345 }
346
347 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800348 HardwareLayer createTextureLayer() {
349 long layer = nCreateTextureLayer(mNativeProxy);
350 return HardwareLayer.adoptTextureLayer(this, layer);
351 }
352
353 @Override
John Reck3e824952014-08-20 10:08:39 -0700354 void buildLayer(RenderNode node) {
355 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
356 }
357
358 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800359 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
360 return nCopyLayerInto(mNativeProxy,
John Reckf4faeac2015-03-05 13:50:31 -0800361 layer.getDeferredLayerUpdater(), bitmap.getSkBitmap());
John Reck19b6bcf2014-02-14 20:03:38 -0800362 }
363
364 @Override
365 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700366 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800367 }
368
369 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800370 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700371 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800372 }
373
374 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800375 void setName(String name) {
John Reckb36016c2015-03-11 08:50:53 -0700376 nSetName(mNativeProxy, name);
John Reckcec24ae2013-11-05 13:27:50 -0800377 }
378
John Reck4f02bf42014-01-03 18:09:17 -0800379 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700380 void fence() {
381 nFence(mNativeProxy);
382 }
383
384 @Override
John Reckf47a5942014-06-30 16:20:04 -0700385 void stopDrawing() {
386 nStopDrawing(mNativeProxy);
387 }
388
389 @Override
John Recka5dda642014-05-22 15:43:54 -0700390 public void notifyFramePending() {
391 nNotifyFramePending(mNativeProxy);
392 }
393
394 @Override
John Reck119907c2014-08-14 09:02:01 -0700395 void registerAnimatingRenderNode(RenderNode animator) {
396 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
397 }
398
399 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800400 protected void finalize() throws Throwable {
401 try {
402 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700403 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800404 } finally {
405 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800406 }
407 }
408
John Reckf47a5942014-06-30 16:20:04 -0700409 static void trimMemory(int level) {
410 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700411 }
412
John Reck66f0be62014-05-13 13:39:31 -0700413 private static class AtlasInitializer {
414 static AtlasInitializer sInstance = new AtlasInitializer();
415
416 private boolean mInitialized = false;
417
418 private AtlasInitializer() {}
419
John Reck3b202512014-06-23 13:13:08 -0700420 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700421 if (mInitialized) return;
422 IBinder binder = ServiceManager.getService("assetatlas");
423 if (binder == null) return;
424
425 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
426 try {
427 if (atlas.isCompatible(android.os.Process.myPpid())) {
428 GraphicBuffer buffer = atlas.getBuffer();
429 if (buffer != null) {
430 long[] map = atlas.getMap();
431 if (map != null) {
John Reckb8802b12014-06-16 15:28:50 -0700432 // TODO Remove after fixing b/15425820
433 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700434 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700435 mInitialized = true;
436 }
437 // If IAssetAtlas is not the same class as the IBinder
438 // we are using a remote service and we can safely
439 // destroy the graphic buffer
440 if (atlas.getClass() != binder.getClass()) {
441 buffer.destroy();
442 }
443 }
444 }
445 } catch (RemoteException e) {
446 Log.w(LOG_TAG, "Could not acquire atlas", e);
447 }
448 }
John Reckb8802b12014-06-16 15:28:50 -0700449
450 private static void validateMap(Context context, long[] map) {
451 Log.d("Atlas", "Validating map...");
452 HashSet<Long> preloadedPointers = new HashSet<Long>();
453
454 // We only care about drawables that hold bitmaps
455 final Resources resources = context.getResources();
456 final LongSparseArray<Drawable.ConstantState> drawables = resources.getPreloadedDrawables();
457
458 final int count = drawables.size();
John Reckdad7d84c2014-12-09 12:33:26 -0800459 ArrayList<Bitmap> tmpList = new ArrayList<Bitmap>();
John Reckb8802b12014-06-16 15:28:50 -0700460 for (int i = 0; i < count; i++) {
John Reckdad7d84c2014-12-09 12:33:26 -0800461 drawables.valueAt(i).addAtlasableBitmaps(tmpList);
462 for (int j = 0; j < tmpList.size(); j++) {
John Reckf4faeac2015-03-05 13:50:31 -0800463 preloadedPointers.add(tmpList.get(j).getSkBitmap());
John Reckb8802b12014-06-16 15:28:50 -0700464 }
John Reckdad7d84c2014-12-09 12:33:26 -0800465 tmpList.clear();
John Reckb8802b12014-06-16 15:28:50 -0700466 }
467
468 for (int i = 0; i < map.length; i += 4) {
469 if (!preloadedPointers.contains(map[i])) {
470 Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i]));
471 map[i] = 0;
472 }
473 }
474 }
John Reck66f0be62014-05-13 13:39:31 -0700475 }
476
John Reck84a4c882014-05-30 14:34:03 -0700477 static native void setupShadersDiskCache(String cacheFile);
478
John Reck3b202512014-06-23 13:13:08 -0700479 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800480
John Recke45b1fd2014-04-15 09:50:16 -0700481 private static native long nCreateRootRenderNode();
482 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800483 private static native void nDeleteProxy(long nativeProxy);
484
John Recke4280ba2014-05-05 16:39:37 -0700485 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reckb36016c2015-03-11 08:50:53 -0700486 private static native void nSetName(long nativeProxy, String name);
John Reck18f16e62014-05-02 16:46:41 -0700487
John Reck4f02bf42014-01-03 18:09:17 -0800488 private static native boolean nInitialize(long nativeProxy, Surface window);
489 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reck01a5ea32014-12-03 13:01:07 -0800490 private static native boolean nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700491 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700492 float lightX, float lightY, float lightZ, float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700493 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700494 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckba6adf62015-02-19 14:36:50 -0800495 private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
John Reck17035b02014-09-03 07:39:53 -0700496 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700497 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800498
John Reck3b202512014-06-23 13:13:08 -0700499 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800500
John Reck19b6bcf2014-02-14 20:03:38 -0800501 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700502 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800503 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700504 private static native void nPushLayerUpdate(long nativeProxy, long layer);
505 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700506 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700507
John Reckf47a5942014-06-30 16:20:04 -0700508 private static native void nDestroyHardwareResources(long nativeProxy);
509 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700510
John Reck28ad7b52014-04-07 16:59:25 -0700511 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700512 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700513 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700514
John Reckba6adf62015-02-19 14:36:50 -0800515 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
516 @DumpFlags int dumpFlags);
John Reckcec24ae2013-11-05 13:27:50 -0800517}