blob: 5579c1382c1fbc0e5f264c2a87cc01b3560a0769 [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
Alan Viverette58c42c32014-07-12 20:33:45 -070019import com.android.internal.R;
20
John Reckb8802b12014-06-16 15:28:50 -070021import android.content.Context;
22import android.content.res.Resources;
Alan Viverette58c42c32014-07-12 20:33:45 -070023import android.content.res.TypedArray;
John Reck04fc5832014-02-05 16:38:25 -080024import android.graphics.Bitmap;
Alan Viveretteccb11e12014-07-08 16:04:02 -070025import android.graphics.Rect;
John Reckb8802b12014-06-16 15:28:50 -070026import android.graphics.drawable.Drawable;
John Reck66f0be62014-05-13 13:39:31 -070027import android.os.IBinder;
28import android.os.RemoteException;
29import android.os.ServiceManager;
John Reckfe5e7b72014-05-23 17:42:28 -070030import android.os.SystemProperties;
John Reckcec24ae2013-11-05 13:27:50 -080031import android.os.Trace;
John Reck66f0be62014-05-13 13:39:31 -070032import android.util.Log;
John Reckb8802b12014-06-16 15:28:50 -070033import android.util.LongSparseArray;
John Reck315c3292014-05-09 19:21:04 -070034import android.util.TimeUtils;
John Reckcec24ae2013-11-05 13:27:50 -080035import android.view.Surface.OutOfResourcesException;
36import android.view.View.AttachInfo;
37
John Reckfe5e7b72014-05-23 17:42:28 -070038import java.io.FileDescriptor;
John Reckcec24ae2013-11-05 13:27:50 -080039import java.io.PrintWriter;
John Reckb8802b12014-06-16 15:28:50 -070040import java.util.HashSet;
John Reckcec24ae2013-11-05 13:27:50 -080041
42/**
43 * Hardware renderer that proxies the rendering to a render thread. Most calls
John Reck4f02bf42014-01-03 18:09:17 -080044 * are currently synchronous.
John Reckcec24ae2013-11-05 13:27:50 -080045 *
46 * The UI thread can block on the RenderThread, but RenderThread must never
47 * block on the UI thread.
48 *
John Reck4f02bf42014-01-03 18:09:17 -080049 * 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 Reckcec24ae2013-11-05 13:27:50 -080053 * 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 */
61public class ThreadedRenderer extends HardwareRenderer {
62 private static final String LOGTAG = "ThreadedRenderer";
63
John Reckf9be7792014-05-02 18:21:16 -070064 // Keep in sync with DrawFrameTask.h SYNC_* flags
65 // Nothing interesting to report
John Reckcd028f32014-06-24 08:44:29 -070066 private static final int SYNC_OK = 0;
John Reckf9be7792014-05-02 18:21:16 -070067 // Needs a ViewRoot invalidate
John Reckcd028f32014-06-24 08:44:29 -070068 private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0;
John Reckaa95a882014-11-07 11:02:07 -080069 // Spoiler: the reward is GPU-accelerated drawing, better find that Surface!
70 private static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 1 << 1;
John Reckf9be7792014-05-02 18:21:16 -070071
John Reckfe5e7b72014-05-23 17:42:28 -070072 private static final String[] VISUALIZERS = {
73 PROFILE_PROPERTY_VISUALIZE_BARS,
74 };
75
Alan Viveretteccb11e12014-07-08 16:04:02 -070076 // Size of the rendered content.
John Reckcec24ae2013-11-05 13:27:50 -080077 private int mWidth, mHeight;
Alan Viveretteccb11e12014-07-08 16:04:02 -070078
79 // Actual size of the drawing surface.
80 private int mSurfaceWidth, mSurfaceHeight;
81
82 // Insets between the drawing surface and rendered content. These are
83 // applied as translation when updating the root render node.
84 private int mInsetTop, mInsetLeft;
85
Alan Viverette57774a82014-07-15 15:49:55 -070086 // Whether the surface has insets. Used to protect opacity.
87 private boolean mHasInsets;
88
Alan Viverette58c42c32014-07-12 20:33:45 -070089 // Light and shadow properties specified by the theme.
90 private final float mLightY;
91 private final float mLightZ;
92 private final float mLightRadius;
Chris Craik058fc642014-07-23 18:19:28 -070093 private final int mAmbientShadowAlpha;
94 private final int mSpotShadowAlpha;
Alan Viverette58c42c32014-07-12 20:33:45 -070095
John Reck4f02bf42014-01-03 18:09:17 -080096 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -070097 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -070098 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -070099 private Choreographer mChoreographer;
John Reckfe5e7b72014-05-23 17:42:28 -0700100 private boolean mProfilingEnabled;
John Reck0a973302014-07-16 13:29:45 -0700101 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800102
John Reckb8802b12014-06-16 15:28:50 -0700103 ThreadedRenderer(Context context, boolean translucent) {
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700104 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700105 mLightY = a.getDimension(R.styleable.Lighting_lightY, 0);
106 mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0);
107 mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0);
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700108 mAmbientShadowAlpha =
109 (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f);
110 mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f);
Alan Viverette58c42c32014-07-12 20:33:45 -0700111 a.recycle();
112
John Recke45b1fd2014-04-15 09:50:16 -0700113 long rootNodePtr = nCreateRootRenderNode();
114 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700115 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700116 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700117
John Reck3b202512014-06-23 13:13:08 -0700118 AtlasInitializer.sInstance.init(context, mNativeProxy);
119
John Reck18f16e62014-05-02 16:46:41 -0700120 // Setup timing
121 mChoreographer = Choreographer.getInstance();
122 nSetFrameInterval(mNativeProxy, mChoreographer.getFrameIntervalNanos());
John Reckfe5e7b72014-05-23 17:42:28 -0700123
124 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800125 }
126
127 @Override
John Reckf47a5942014-06-30 16:20:04 -0700128 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700129 mInitialized = false;
130 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700131 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800132 }
133
John Reckf7d9c1d2014-04-09 10:01:03 -0700134 private void updateEnabledState(Surface surface) {
135 if (surface == null || !surface.isValid()) {
136 setEnabled(false);
137 } else {
138 setEnabled(mInitialized);
139 }
140 }
141
John Reckcec24ae2013-11-05 13:27:50 -0800142 @Override
143 boolean initialize(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700144 mInitialized = true;
145 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700146 boolean status = nInitialize(mNativeProxy, surface);
147 surface.allocateBuffers();
148 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800149 }
150
151 @Override
152 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700153 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800154 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800155 }
156
157 @Override
John Reckf7d9c1d2014-04-09 10:01:03 -0700158 void pauseSurface(Surface surface) {
159 nPauseSurface(mNativeProxy, surface);
160 }
161
162 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800163 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800164 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700165 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800166 }
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 Reckcec24ae2013-11-05 13:27:50 -0800179 }
180
181 @Override
182 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800183 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800184 }
185
186 @Override
John Reck918ad522014-06-27 14:45:25 -0700187 void detachSurfaceTexture(long hardwareLayer) {
188 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800189 }
190
191 @Override
Alan Viverette58c42c32014-07-12 20:33:45 -0700192 void setup(int width, int height, Rect surfaceInsets) {
193 final float lightX = width / 2.0f;
John Reckcec24ae2013-11-05 13:27:50 -0800194 mWidth = width;
195 mHeight = height;
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700196 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
197 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700198 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700199 mInsetLeft = surfaceInsets.left;
200 mInsetTop = surfaceInsets.top;
201 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
202 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700203
204 // If the surface has insets, it can't be opaque.
205 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700206 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700207 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700208 mInsetLeft = 0;
209 mInsetTop = 0;
210 mSurfaceWidth = width;
211 mSurfaceHeight = height;
212 }
213 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700214 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
215 lightX, mLightY, mLightZ, mLightRadius,
216 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800217 }
218
219 @Override
John Reck63a06672014-05-07 13:45:54 -0700220 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700221 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700222 }
223
224 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800225 int getWidth() {
226 return mWidth;
227 }
228
229 @Override
230 int getHeight() {
231 return mHeight;
232 }
233
234 @Override
John Reckfe5e7b72014-05-23 17:42:28 -0700235 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) {
236 pw.flush();
237 nDumpProfileInfo(mNativeProxy, fd);
John Reckcec24ae2013-11-05 13:27:50 -0800238 }
239
John Reckfe5e7b72014-05-23 17:42:28 -0700240 private static int search(String[] values, String value) {
241 for (int i = 0; i < values.length; i++) {
242 if (values[i].equals(value)) return i;
243 }
244 return -1;
245 }
246
247 private static boolean checkIfProfilingRequested() {
248 String profiling = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY);
249 int graphType = search(VISUALIZERS, profiling);
250 return (graphType >= 0) || Boolean.parseBoolean(profiling);
John Reckcec24ae2013-11-05 13:27:50 -0800251 }
252
253 @Override
254 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700255 boolean changed = nLoadSystemProperties(mNativeProxy);
256 boolean wantProfiling = checkIfProfilingRequested();
257 if (wantProfiling != mProfilingEnabled) {
258 mProfilingEnabled = wantProfiling;
259 changed = true;
260 }
John Reck23d307c2014-10-27 12:38:48 -0700261 if (changed) {
262 invalidateRoot();
263 }
John Reckfe5e7b72014-05-23 17:42:28 -0700264 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800265 }
266
John Reck0a973302014-07-16 13:29:45 -0700267 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800268 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800269 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
270 == View.PFLAG_INVALIDATED;
271 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
John Reck0a973302014-07-16 13:29:45 -0700272 view.getDisplayList();
John Reckcec24ae2013-11-05 13:27:50 -0800273 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700274 }
275
John Reck61375a82014-09-18 19:27:48 +0000276 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
John Reck0a973302014-07-16 13:29:45 -0700277 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList");
278 updateViewTreeDisplayList(view);
279
280 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
281 HardwareCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
282 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700283 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700284 canvas.translate(mInsetLeft, mInsetTop);
285 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700286
287 canvas.insertReorderBarrier();
John Reck0a973302014-07-16 13:29:45 -0700288 canvas.drawRenderNode(view.getDisplayList());
Chris Craikabedca32014-08-28 15:03:55 -0700289 canvas.insertInorderBarrier();
290
John Reck0a973302014-07-16 13:29:45 -0700291 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700292 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700293 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 Reckbc0cc022014-04-11 16:08:14 -0700306 @Override
John Reck61375a82014-09-18 19:27:48 +0000307 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700308 attachInfo.mIgnoreDirtyState = true;
John Reck18f16e62014-05-02 16:46:41 -0700309 long frameTimeNanos = mChoreographer.getFrameTimeNanos();
John Reck315c3292014-05-09 19:21:04 -0700310 attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS;
John Reckbc0cc022014-04-11 16:08:14 -0700311
John Reckfe5e7b72014-05-23 17:42:28 -0700312 long recordDuration = 0;
313 if (mProfilingEnabled) {
314 recordDuration = System.nanoTime();
315 }
316
John Reck61375a82014-09-18 19:27:48 +0000317 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800318
John Reckfe5e7b72014-05-23 17:42:28 -0700319 if (mProfilingEnabled) {
320 recordDuration = System.nanoTime() - recordDuration;
321 }
322
John Reck6313b922014-04-16 18:59:21 -0700323 attachInfo.mIgnoreDirtyState = false;
324
John Reck119907c2014-08-14 09:02:01 -0700325 // register animating rendernodes which started animating prior to renderer
326 // creation, which is typical for animators started prior to first draw
327 if (attachInfo.mPendingAnimatingRenderNodes != null) {
328 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
329 for (int i = 0; i < count; i++) {
330 registerAnimatingRenderNode(
331 attachInfo.mPendingAnimatingRenderNodes.get(i));
332 }
333 attachInfo.mPendingAnimatingRenderNodes.clear();
334 // We don't need this anymore as subsequent calls to
335 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
336 attachInfo.mPendingAnimatingRenderNodes = null;
337 }
338
John Reckf9be7792014-05-02 18:21:16 -0700339 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos,
John Recke4267ea2014-06-03 15:53:15 -0700340 recordDuration, view.getResources().getDisplayMetrics().density);
John Reckaa95a882014-11-07 11:02:07 -0800341 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
342 setEnabled(false);
343 // Invalidate since we failed to draw. This should fetch a Surface
344 // if it is still needed or do nothing if we are no longer drawing
345 attachInfo.mViewRootImpl.invalidate();
346 }
John Reckf9be7792014-05-02 18:21:16 -0700347 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
348 attachInfo.mViewRootImpl.invalidate();
349 }
John Reckcec24ae2013-11-05 13:27:50 -0800350 }
351
John Reck3b202512014-06-23 13:13:08 -0700352 static void invokeFunctor(long functor, boolean waitForCompletion) {
353 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700354 }
355
356 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800357 HardwareLayer createTextureLayer() {
358 long layer = nCreateTextureLayer(mNativeProxy);
359 return HardwareLayer.adoptTextureLayer(this, layer);
360 }
361
362 @Override
John Reck3e824952014-08-20 10:08:39 -0700363 void buildLayer(RenderNode node) {
364 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
365 }
366
367 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800368 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
369 return nCopyLayerInto(mNativeProxy,
370 layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap);
371 }
372
373 @Override
374 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700375 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800376 }
377
378 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800379 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700380 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800381 }
382
383 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800384 void setName(String name) {
John Reckcec24ae2013-11-05 13:27:50 -0800385 }
386
John Reck4f02bf42014-01-03 18:09:17 -0800387 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700388 void fence() {
389 nFence(mNativeProxy);
390 }
391
392 @Override
John Reckf47a5942014-06-30 16:20:04 -0700393 void stopDrawing() {
394 nStopDrawing(mNativeProxy);
395 }
396
397 @Override
John Recka5dda642014-05-22 15:43:54 -0700398 public void notifyFramePending() {
399 nNotifyFramePending(mNativeProxy);
400 }
401
402 @Override
John Reck119907c2014-08-14 09:02:01 -0700403 void registerAnimatingRenderNode(RenderNode animator) {
404 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
405 }
406
407 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800408 protected void finalize() throws Throwable {
409 try {
410 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700411 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800412 } finally {
413 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800414 }
415 }
416
John Reckf47a5942014-06-30 16:20:04 -0700417 static void trimMemory(int level) {
418 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700419 }
420
John Reck66f0be62014-05-13 13:39:31 -0700421 private static class AtlasInitializer {
422 static AtlasInitializer sInstance = new AtlasInitializer();
423
424 private boolean mInitialized = false;
425
426 private AtlasInitializer() {}
427
John Reck3b202512014-06-23 13:13:08 -0700428 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700429 if (mInitialized) return;
430 IBinder binder = ServiceManager.getService("assetatlas");
431 if (binder == null) return;
432
433 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
434 try {
435 if (atlas.isCompatible(android.os.Process.myPpid())) {
436 GraphicBuffer buffer = atlas.getBuffer();
437 if (buffer != null) {
438 long[] map = atlas.getMap();
439 if (map != null) {
John Reckb8802b12014-06-16 15:28:50 -0700440 // TODO Remove after fixing b/15425820
441 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700442 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700443 mInitialized = true;
444 }
445 // If IAssetAtlas is not the same class as the IBinder
446 // we are using a remote service and we can safely
447 // destroy the graphic buffer
448 if (atlas.getClass() != binder.getClass()) {
449 buffer.destroy();
450 }
451 }
452 }
453 } catch (RemoteException e) {
454 Log.w(LOG_TAG, "Could not acquire atlas", e);
455 }
456 }
John Reckb8802b12014-06-16 15:28:50 -0700457
458 private static void validateMap(Context context, long[] map) {
459 Log.d("Atlas", "Validating map...");
460 HashSet<Long> preloadedPointers = new HashSet<Long>();
461
462 // We only care about drawables that hold bitmaps
463 final Resources resources = context.getResources();
464 final LongSparseArray<Drawable.ConstantState> drawables = resources.getPreloadedDrawables();
465
466 final int count = drawables.size();
467 for (int i = 0; i < count; i++) {
468 final Bitmap bitmap = drawables.valueAt(i).getBitmap();
469 if (bitmap != null && bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
470 preloadedPointers.add(bitmap.mNativeBitmap);
471 }
472 }
473
474 for (int i = 0; i < map.length; i += 4) {
475 if (!preloadedPointers.contains(map[i])) {
476 Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i]));
477 map[i] = 0;
478 }
479 }
480 }
John Reck66f0be62014-05-13 13:39:31 -0700481 }
482
John Reck84a4c882014-05-30 14:34:03 -0700483 static native void setupShadersDiskCache(String cacheFile);
484
John Reck3b202512014-06-23 13:13:08 -0700485 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800486
John Recke45b1fd2014-04-15 09:50:16 -0700487 private static native long nCreateRootRenderNode();
488 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800489 private static native void nDeleteProxy(long nativeProxy);
490
John Reck18f16e62014-05-02 16:46:41 -0700491 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos);
John Recke4280ba2014-05-05 16:39:37 -0700492 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reck18f16e62014-05-02 16:46:41 -0700493
John Reck4f02bf42014-01-03 18:09:17 -0800494 private static native boolean nInitialize(long nativeProxy, Surface window);
495 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700496 private static native void nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700497 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700498 float lightX, float lightY, float lightZ, float lightRadius,
499 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700500 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckfe5e7b72014-05-23 17:42:28 -0700501 private static native int nSyncAndDrawFrame(long nativeProxy,
John Recke4267ea2014-06-03 15:53:15 -0700502 long frameTimeNanos, long recordDuration, float density);
John Reck17035b02014-09-03 07:39:53 -0700503 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700504 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800505
John Reck3b202512014-06-23 13:13:08 -0700506 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800507
John Reck19b6bcf2014-02-14 20:03:38 -0800508 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700509 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800510 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700511 private static native void nPushLayerUpdate(long nativeProxy, long layer);
512 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700513 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700514
John Reckf47a5942014-06-30 16:20:04 -0700515 private static native void nDestroyHardwareResources(long nativeProxy);
516 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700517
John Reck28ad7b52014-04-07 16:59:25 -0700518 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700519 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700520 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700521
522 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800523}