blob: 00a88846a56a0c5ed73843a80588e84fa1713b70 [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 Viveretteed6f14a2014-08-26 14:53:28 -0700102 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700103 mLightY = a.getDimension(R.styleable.Lighting_lightY, 0);
104 mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0);
105 mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0);
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700106 mAmbientShadowAlpha =
107 (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f);
108 mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f);
Alan Viverette58c42c32014-07-12 20:33:45 -0700109 a.recycle();
110
John Recke45b1fd2014-04-15 09:50:16 -0700111 long rootNodePtr = nCreateRootRenderNode();
112 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700113 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700114 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700115
John Reck3b202512014-06-23 13:13:08 -0700116 AtlasInitializer.sInstance.init(context, mNativeProxy);
117
John Reck18f16e62014-05-02 16:46:41 -0700118 // Setup timing
119 mChoreographer = Choreographer.getInstance();
120 nSetFrameInterval(mNativeProxy, mChoreographer.getFrameIntervalNanos());
John Reckfe5e7b72014-05-23 17:42:28 -0700121
122 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800123 }
124
125 @Override
John Reckf47a5942014-06-30 16:20:04 -0700126 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700127 mInitialized = false;
128 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700129 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800130 }
131
John Reckf7d9c1d2014-04-09 10:01:03 -0700132 private void updateEnabledState(Surface surface) {
133 if (surface == null || !surface.isValid()) {
134 setEnabled(false);
135 } else {
136 setEnabled(mInitialized);
137 }
138 }
139
John Reckcec24ae2013-11-05 13:27:50 -0800140 @Override
141 boolean initialize(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700142 mInitialized = true;
143 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700144 boolean status = nInitialize(mNativeProxy, surface);
145 surface.allocateBuffers();
146 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800147 }
148
149 @Override
150 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700151 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800152 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800153 }
154
155 @Override
John Reckf7d9c1d2014-04-09 10:01:03 -0700156 void pauseSurface(Surface surface) {
157 nPauseSurface(mNativeProxy, surface);
158 }
159
160 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800161 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800162 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700163 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800164 }
165
166 private static void destroyResources(View view) {
167 view.destroyHardwareResources();
168
169 if (view instanceof ViewGroup) {
170 ViewGroup group = (ViewGroup) view;
171
172 int count = group.getChildCount();
173 for (int i = 0; i < count; i++) {
174 destroyResources(group.getChildAt(i));
175 }
176 }
John Reckcec24ae2013-11-05 13:27:50 -0800177 }
178
179 @Override
180 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800181 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800182 }
183
184 @Override
John Reck918ad522014-06-27 14:45:25 -0700185 void detachSurfaceTexture(long hardwareLayer) {
186 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800187 }
188
189 @Override
Alan Viverette58c42c32014-07-12 20:33:45 -0700190 void setup(int width, int height, Rect surfaceInsets) {
191 final float lightX = width / 2.0f;
John Reckcec24ae2013-11-05 13:27:50 -0800192 mWidth = width;
193 mHeight = height;
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700194 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
195 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700196 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700197 mInsetLeft = surfaceInsets.left;
198 mInsetTop = surfaceInsets.top;
199 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
200 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700201
202 // If the surface has insets, it can't be opaque.
203 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700204 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700205 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700206 mInsetLeft = 0;
207 mInsetTop = 0;
208 mSurfaceWidth = width;
209 mSurfaceHeight = height;
210 }
211 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700212 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
213 lightX, mLightY, mLightZ, mLightRadius,
214 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800215 }
216
217 @Override
John Reck63a06672014-05-07 13:45:54 -0700218 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700219 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700220 }
221
222 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800223 int getWidth() {
224 return mWidth;
225 }
226
227 @Override
228 int getHeight() {
229 return mHeight;
230 }
231
232 @Override
John Reckfe5e7b72014-05-23 17:42:28 -0700233 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) {
234 pw.flush();
235 nDumpProfileInfo(mNativeProxy, fd);
John Reckcec24ae2013-11-05 13:27:50 -0800236 }
237
John Reckfe5e7b72014-05-23 17:42:28 -0700238 private static int search(String[] values, String value) {
239 for (int i = 0; i < values.length; i++) {
240 if (values[i].equals(value)) return i;
241 }
242 return -1;
243 }
244
245 private static boolean checkIfProfilingRequested() {
246 String profiling = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY);
247 int graphType = search(VISUALIZERS, profiling);
248 return (graphType >= 0) || Boolean.parseBoolean(profiling);
John Reckcec24ae2013-11-05 13:27:50 -0800249 }
250
251 @Override
252 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700253 boolean changed = nLoadSystemProperties(mNativeProxy);
254 boolean wantProfiling = checkIfProfilingRequested();
255 if (wantProfiling != mProfilingEnabled) {
256 mProfilingEnabled = wantProfiling;
257 changed = true;
258 }
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) {
John Reck0a973302014-07-16 13:29:45 -0700275 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList");
276 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 Reck18f16e62014-05-02 16:46:41 -0700307 long frameTimeNanos = mChoreographer.getFrameTimeNanos();
John Reck315c3292014-05-09 19:21:04 -0700308 attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS;
John Reckbc0cc022014-04-11 16:08:14 -0700309
John Reckfe5e7b72014-05-23 17:42:28 -0700310 long recordDuration = 0;
311 if (mProfilingEnabled) {
312 recordDuration = System.nanoTime();
313 }
314
John Reck61375a82014-09-18 19:27:48 +0000315 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800316
John Reckfe5e7b72014-05-23 17:42:28 -0700317 if (mProfilingEnabled) {
318 recordDuration = System.nanoTime() - recordDuration;
319 }
320
John Reck6313b922014-04-16 18:59:21 -0700321 attachInfo.mIgnoreDirtyState = false;
322
John Reck119907c2014-08-14 09:02:01 -0700323 // register animating rendernodes which started animating prior to renderer
324 // creation, which is typical for animators started prior to first draw
325 if (attachInfo.mPendingAnimatingRenderNodes != null) {
326 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
327 for (int i = 0; i < count; i++) {
328 registerAnimatingRenderNode(
329 attachInfo.mPendingAnimatingRenderNodes.get(i));
330 }
331 attachInfo.mPendingAnimatingRenderNodes.clear();
332 // We don't need this anymore as subsequent calls to
333 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
334 attachInfo.mPendingAnimatingRenderNodes = null;
335 }
336
John Reckf9be7792014-05-02 18:21:16 -0700337 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos,
John Recke4267ea2014-06-03 15:53:15 -0700338 recordDuration, view.getResources().getDisplayMetrics().density);
John Reckf9be7792014-05-02 18:21:16 -0700339 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
340 attachInfo.mViewRootImpl.invalidate();
341 }
John Reckcec24ae2013-11-05 13:27:50 -0800342 }
343
John Reck3b202512014-06-23 13:13:08 -0700344 static void invokeFunctor(long functor, boolean waitForCompletion) {
345 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700346 }
347
348 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800349 HardwareLayer createTextureLayer() {
350 long layer = nCreateTextureLayer(mNativeProxy);
351 return HardwareLayer.adoptTextureLayer(this, layer);
352 }
353
354 @Override
John Reck3e824952014-08-20 10:08:39 -0700355 void buildLayer(RenderNode node) {
356 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
357 }
358
359 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800360 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
361 return nCopyLayerInto(mNativeProxy,
362 layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap);
363 }
364
365 @Override
366 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700367 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800368 }
369
370 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800371 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700372 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800373 }
374
375 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800376 void setName(String 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();
459 for (int i = 0; i < count; i++) {
460 final Bitmap bitmap = drawables.valueAt(i).getBitmap();
461 if (bitmap != null && bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
462 preloadedPointers.add(bitmap.mNativeBitmap);
463 }
464 }
465
466 for (int i = 0; i < map.length; i += 4) {
467 if (!preloadedPointers.contains(map[i])) {
468 Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i]));
469 map[i] = 0;
470 }
471 }
472 }
John Reck66f0be62014-05-13 13:39:31 -0700473 }
474
John Reck84a4c882014-05-30 14:34:03 -0700475 static native void setupShadersDiskCache(String cacheFile);
476
John Reck3b202512014-06-23 13:13:08 -0700477 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800478
John Recke45b1fd2014-04-15 09:50:16 -0700479 private static native long nCreateRootRenderNode();
480 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800481 private static native void nDeleteProxy(long nativeProxy);
482
John Reck18f16e62014-05-02 16:46:41 -0700483 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos);
John Recke4280ba2014-05-05 16:39:37 -0700484 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reck18f16e62014-05-02 16:46:41 -0700485
John Reck4f02bf42014-01-03 18:09:17 -0800486 private static native boolean nInitialize(long nativeProxy, Surface window);
487 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700488 private static native void nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700489 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700490 float lightX, float lightY, float lightZ, float lightRadius,
491 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700492 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckfe5e7b72014-05-23 17:42:28 -0700493 private static native int nSyncAndDrawFrame(long nativeProxy,
John Recke4267ea2014-06-03 15:53:15 -0700494 long frameTimeNanos, long recordDuration, float density);
John Reck17035b02014-09-03 07:39:53 -0700495 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700496 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800497
John Reck3b202512014-06-23 13:13:08 -0700498 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800499
John Reck19b6bcf2014-02-14 20:03:38 -0800500 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700501 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800502 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700503 private static native void nPushLayerUpdate(long nativeProxy, long layer);
504 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700505 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700506
John Reckf47a5942014-06-30 16:20:04 -0700507 private static native void nDestroyHardwareResources(long nativeProxy);
508 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700509
John Reck28ad7b52014-04-07 16:59:25 -0700510 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700511 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700512 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700513
514 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800515}