blob: faade96622c20fae909d4bc9549e328483714bd5 [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 Reckf9be7792014-05-02 18:21:16 -070069
John Reckfe5e7b72014-05-23 17:42:28 -070070 private static final String[] VISUALIZERS = {
71 PROFILE_PROPERTY_VISUALIZE_BARS,
72 };
73
Alan Viveretteccb11e12014-07-08 16:04:02 -070074 // Size of the rendered content.
John Reckcec24ae2013-11-05 13:27:50 -080075 private int mWidth, mHeight;
Alan Viveretteccb11e12014-07-08 16:04:02 -070076
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 Viverette57774a82014-07-15 15:49:55 -070084 // Whether the surface has insets. Used to protect opacity.
85 private boolean mHasInsets;
86
Alan Viverette58c42c32014-07-12 20:33:45 -070087 // Light and shadow properties specified by the theme.
88 private final float mLightY;
89 private final float mLightZ;
90 private final float mLightRadius;
Chris Craik058fc642014-07-23 18:19:28 -070091 private final int mAmbientShadowAlpha;
92 private final int mSpotShadowAlpha;
Alan Viverette58c42c32014-07-12 20:33:45 -070093
John Reck4f02bf42014-01-03 18:09:17 -080094 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -070095 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -070096 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -070097 private Choreographer mChoreographer;
John Reckfe5e7b72014-05-23 17:42:28 -070098 private boolean mProfilingEnabled;
John Reck0a973302014-07-16 13:29:45 -070099 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800100
John Reckb8802b12014-06-16 15:28:50 -0700101 ThreadedRenderer(Context context, boolean translucent) {
Alan Viverette58c42c32014-07-12 20:33:45 -0700102 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 Craik058fc642014-07-23 18:19:28 -0700107 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 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 Reckfae904d2014-04-14 11:01:57 -0700131 nDestroyCanvasAndSurface(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 Viverette57774a82014-07-15 15:49:55 -0700196 if (surfaceInsets != null && !surfaceInsets.isEmpty()) {
197 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700198 mInsetLeft = surfaceInsets.left;
199 mInsetTop = surfaceInsets.top;
200 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
201 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700202
203 // If the surface has insets, it can't be opaque.
204 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700205 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700206 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700207 mInsetLeft = 0;
208 mInsetTop = 0;
209 mSurfaceWidth = width;
210 mSurfaceHeight = height;
211 }
212 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700213 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
214 lightX, mLightY, mLightZ, mLightRadius,
215 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800216 }
217
218 @Override
John Reck63a06672014-05-07 13:45:54 -0700219 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700220 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700221 }
222
223 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800224 int getWidth() {
225 return mWidth;
226 }
227
228 @Override
229 int getHeight() {
230 return mHeight;
231 }
232
233 @Override
John Reckfe5e7b72014-05-23 17:42:28 -0700234 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) {
235 pw.flush();
236 nDumpProfileInfo(mNativeProxy, fd);
John Reckcec24ae2013-11-05 13:27:50 -0800237 }
238
John Reckfe5e7b72014-05-23 17:42:28 -0700239 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 Reckcec24ae2013-11-05 13:27:50 -0800250 }
251
252 @Override
253 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700254 boolean changed = nLoadSystemProperties(mNativeProxy);
255 boolean wantProfiling = checkIfProfilingRequested();
256 if (wantProfiling != mProfilingEnabled) {
257 mProfilingEnabled = wantProfiling;
258 changed = true;
259 }
260 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800261 }
262
John Reck0a973302014-07-16 13:29:45 -0700263 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800264 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800265 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
266 == View.PFLAG_INVALIDATED;
267 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
John Reck0a973302014-07-16 13:29:45 -0700268 view.getDisplayList();
John Reckcec24ae2013-11-05 13:27:50 -0800269 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700270 }
271
John Reck0a973302014-07-16 13:29:45 -0700272 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 Viverettedbed8932014-08-06 17:54:52 -0700279 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700280 canvas.translate(mInsetLeft, mInsetTop);
281 callbacks.onHardwarePreDraw(canvas);
282 canvas.drawRenderNode(view.getDisplayList());
283 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700284 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700285 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 Reckbc0cc022014-04-11 16:08:14 -0700298 @Override
John Recke4267ea2014-06-03 15:53:15 -0700299 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700300 attachInfo.mIgnoreDirtyState = true;
John Reck18f16e62014-05-02 16:46:41 -0700301 long frameTimeNanos = mChoreographer.getFrameTimeNanos();
John Reck315c3292014-05-09 19:21:04 -0700302 attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS;
John Reckbc0cc022014-04-11 16:08:14 -0700303
John Reckfe5e7b72014-05-23 17:42:28 -0700304 long recordDuration = 0;
305 if (mProfilingEnabled) {
306 recordDuration = System.nanoTime();
307 }
308
John Reckbc0cc022014-04-11 16:08:14 -0700309 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800310
John Reckfe5e7b72014-05-23 17:42:28 -0700311 if (mProfilingEnabled) {
312 recordDuration = System.nanoTime() - recordDuration;
313 }
314
John Reck6313b922014-04-16 18:59:21 -0700315 attachInfo.mIgnoreDirtyState = false;
316
John Reck119907c2014-08-14 09:02:01 -0700317 // 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 Reckf9be7792014-05-02 18:21:16 -0700331 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos,
John Recke4267ea2014-06-03 15:53:15 -0700332 recordDuration, view.getResources().getDisplayMetrics().density);
John Reckf9be7792014-05-02 18:21:16 -0700333 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
334 attachInfo.mViewRootImpl.invalidate();
335 }
John Reckcec24ae2013-11-05 13:27:50 -0800336 }
337
John Reck3b202512014-06-23 13:13:08 -0700338 static void invokeFunctor(long functor, boolean waitForCompletion) {
339 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700340 }
341
342 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800343 HardwareLayer createTextureLayer() {
344 long layer = nCreateTextureLayer(mNativeProxy);
345 return HardwareLayer.adoptTextureLayer(this, layer);
346 }
347
348 @Override
John Reck3e824952014-08-20 10:08:39 -0700349 void buildLayer(RenderNode node) {
350 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
351 }
352
353 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800354 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 Reckd72e0a32014-05-29 18:56:11 -0700361 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800362 }
363
364 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800365 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700366 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800367 }
368
369 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800370 void setName(String name) {
John Reckcec24ae2013-11-05 13:27:50 -0800371 }
372
John Reck4f02bf42014-01-03 18:09:17 -0800373 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700374 void fence() {
375 nFence(mNativeProxy);
376 }
377
378 @Override
John Reckf47a5942014-06-30 16:20:04 -0700379 void stopDrawing() {
380 nStopDrawing(mNativeProxy);
381 }
382
383 @Override
John Recka5dda642014-05-22 15:43:54 -0700384 public void notifyFramePending() {
385 nNotifyFramePending(mNativeProxy);
386 }
387
388 @Override
John Reck119907c2014-08-14 09:02:01 -0700389 void registerAnimatingRenderNode(RenderNode animator) {
390 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
391 }
392
393 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800394 protected void finalize() throws Throwable {
395 try {
396 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700397 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800398 } finally {
399 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800400 }
401 }
402
John Reckf47a5942014-06-30 16:20:04 -0700403 static void trimMemory(int level) {
404 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700405 }
406
John Reck66f0be62014-05-13 13:39:31 -0700407 private static class AtlasInitializer {
408 static AtlasInitializer sInstance = new AtlasInitializer();
409
410 private boolean mInitialized = false;
411
412 private AtlasInitializer() {}
413
John Reck3b202512014-06-23 13:13:08 -0700414 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700415 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 Reckb8802b12014-06-16 15:28:50 -0700426 // TODO Remove after fixing b/15425820
427 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700428 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700429 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 Reckb8802b12014-06-16 15:28:50 -0700443
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 Reck66f0be62014-05-13 13:39:31 -0700467 }
468
John Reck84a4c882014-05-30 14:34:03 -0700469 static native void setupShadersDiskCache(String cacheFile);
470
John Reck3b202512014-06-23 13:13:08 -0700471 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800472
John Recke45b1fd2014-04-15 09:50:16 -0700473 private static native long nCreateRootRenderNode();
474 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800475 private static native void nDeleteProxy(long nativeProxy);
476
John Reck18f16e62014-05-02 16:46:41 -0700477 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos);
John Recke4280ba2014-05-05 16:39:37 -0700478 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reck18f16e62014-05-02 16:46:41 -0700479
John Reck4f02bf42014-01-03 18:09:17 -0800480 private static native boolean nInitialize(long nativeProxy, Surface window);
481 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700482 private static native void nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700483 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700484 float lightX, float lightY, float lightZ, float lightRadius,
485 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700486 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckfe5e7b72014-05-23 17:42:28 -0700487 private static native int nSyncAndDrawFrame(long nativeProxy,
John Recke4267ea2014-06-03 15:53:15 -0700488 long frameTimeNanos, long recordDuration, float density);
John Reckfae904d2014-04-14 11:01:57 -0700489 private static native void nDestroyCanvasAndSurface(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700490 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800491
John Reck3b202512014-06-23 13:13:08 -0700492 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800493
494 private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height);
495 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700496 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800497 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700498 private static native void nPushLayerUpdate(long nativeProxy, long layer);
499 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700500 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700501
John Reckf47a5942014-06-30 16:20:04 -0700502 private static native void nDestroyHardwareResources(long nativeProxy);
503 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700504
John Reck28ad7b52014-04-07 16:59:25 -0700505 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700506 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700507 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700508
509 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800510}