blob: 5d2822d895aec65df997900558599878c2cb2888 [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 Viverette57774a82014-07-15 15:49:55 -0700194 if (surfaceInsets != null && !surfaceInsets.isEmpty()) {
195 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700196 mInsetLeft = surfaceInsets.left;
197 mInsetTop = surfaceInsets.top;
198 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
199 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700200
201 // If the surface has insets, it can't be opaque.
202 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700203 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700204 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700205 mInsetLeft = 0;
206 mInsetTop = 0;
207 mSurfaceWidth = width;
208 mSurfaceHeight = height;
209 }
210 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700211 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
212 lightX, mLightY, mLightZ, mLightRadius,
213 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800214 }
215
216 @Override
John Reck63a06672014-05-07 13:45:54 -0700217 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700218 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700219 }
220
221 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800222 int getWidth() {
223 return mWidth;
224 }
225
226 @Override
227 int getHeight() {
228 return mHeight;
229 }
230
231 @Override
John Reckfe5e7b72014-05-23 17:42:28 -0700232 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) {
233 pw.flush();
234 nDumpProfileInfo(mNativeProxy, fd);
John Reckcec24ae2013-11-05 13:27:50 -0800235 }
236
John Reckfe5e7b72014-05-23 17:42:28 -0700237 private static int search(String[] values, String value) {
238 for (int i = 0; i < values.length; i++) {
239 if (values[i].equals(value)) return i;
240 }
241 return -1;
242 }
243
244 private static boolean checkIfProfilingRequested() {
245 String profiling = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY);
246 int graphType = search(VISUALIZERS, profiling);
247 return (graphType >= 0) || Boolean.parseBoolean(profiling);
John Reckcec24ae2013-11-05 13:27:50 -0800248 }
249
250 @Override
251 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700252 boolean changed = nLoadSystemProperties(mNativeProxy);
253 boolean wantProfiling = checkIfProfilingRequested();
254 if (wantProfiling != mProfilingEnabled) {
255 mProfilingEnabled = wantProfiling;
256 changed = true;
257 }
258 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800259 }
260
John Reck0a973302014-07-16 13:29:45 -0700261 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800262 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800263 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
264 == View.PFLAG_INVALIDATED;
265 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
John Reck0a973302014-07-16 13:29:45 -0700266 view.getDisplayList();
John Reckcec24ae2013-11-05 13:27:50 -0800267 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700268 }
269
John Reck61375a82014-09-18 19:27:48 +0000270 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
John Reck0a973302014-07-16 13:29:45 -0700271 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList");
272 updateViewTreeDisplayList(view);
273
274 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
275 HardwareCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
276 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700277 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700278 canvas.translate(mInsetLeft, mInsetTop);
279 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700280
281 canvas.insertReorderBarrier();
John Reck0a973302014-07-16 13:29:45 -0700282 canvas.drawRenderNode(view.getDisplayList());
Chris Craikabedca32014-08-28 15:03:55 -0700283 canvas.insertInorderBarrier();
284
John Reck0a973302014-07-16 13:29:45 -0700285 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700286 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700287 mRootNodeNeedsUpdate = false;
288 } finally {
289 mRootNode.end(canvas);
290 }
291 }
292 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
293 }
294
295 @Override
296 void invalidateRoot() {
297 mRootNodeNeedsUpdate = true;
298 }
299
John Reckbc0cc022014-04-11 16:08:14 -0700300 @Override
John Reck61375a82014-09-18 19:27:48 +0000301 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700302 attachInfo.mIgnoreDirtyState = true;
John Reck18f16e62014-05-02 16:46:41 -0700303 long frameTimeNanos = mChoreographer.getFrameTimeNanos();
John Reck315c3292014-05-09 19:21:04 -0700304 attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS;
John Reckbc0cc022014-04-11 16:08:14 -0700305
John Reckfe5e7b72014-05-23 17:42:28 -0700306 long recordDuration = 0;
307 if (mProfilingEnabled) {
308 recordDuration = System.nanoTime();
309 }
310
John Reck61375a82014-09-18 19:27:48 +0000311 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800312
John Reckfe5e7b72014-05-23 17:42:28 -0700313 if (mProfilingEnabled) {
314 recordDuration = System.nanoTime() - recordDuration;
315 }
316
John Reck6313b922014-04-16 18:59:21 -0700317 attachInfo.mIgnoreDirtyState = false;
318
John Reck119907c2014-08-14 09:02:01 -0700319 // register animating rendernodes which started animating prior to renderer
320 // creation, which is typical for animators started prior to first draw
321 if (attachInfo.mPendingAnimatingRenderNodes != null) {
322 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
323 for (int i = 0; i < count; i++) {
324 registerAnimatingRenderNode(
325 attachInfo.mPendingAnimatingRenderNodes.get(i));
326 }
327 attachInfo.mPendingAnimatingRenderNodes.clear();
328 // We don't need this anymore as subsequent calls to
329 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
330 attachInfo.mPendingAnimatingRenderNodes = null;
331 }
332
John Reckf9be7792014-05-02 18:21:16 -0700333 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos,
John Recke4267ea2014-06-03 15:53:15 -0700334 recordDuration, view.getResources().getDisplayMetrics().density);
John Reckf9be7792014-05-02 18:21:16 -0700335 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
336 attachInfo.mViewRootImpl.invalidate();
337 }
John Reckcec24ae2013-11-05 13:27:50 -0800338 }
339
John Reck3b202512014-06-23 13:13:08 -0700340 static void invokeFunctor(long functor, boolean waitForCompletion) {
341 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700342 }
343
344 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800345 HardwareLayer createTextureLayer() {
346 long layer = nCreateTextureLayer(mNativeProxy);
347 return HardwareLayer.adoptTextureLayer(this, layer);
348 }
349
350 @Override
John Reck3e824952014-08-20 10:08:39 -0700351 void buildLayer(RenderNode node) {
352 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
353 }
354
355 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800356 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
357 return nCopyLayerInto(mNativeProxy,
358 layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap);
359 }
360
361 @Override
362 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700363 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800364 }
365
366 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800367 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700368 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800369 }
370
371 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800372 void setName(String name) {
John Reckcec24ae2013-11-05 13:27:50 -0800373 }
374
John Reck4f02bf42014-01-03 18:09:17 -0800375 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700376 void fence() {
377 nFence(mNativeProxy);
378 }
379
380 @Override
John Reckf47a5942014-06-30 16:20:04 -0700381 void stopDrawing() {
382 nStopDrawing(mNativeProxy);
383 }
384
385 @Override
John Recka5dda642014-05-22 15:43:54 -0700386 public void notifyFramePending() {
387 nNotifyFramePending(mNativeProxy);
388 }
389
390 @Override
John Reck119907c2014-08-14 09:02:01 -0700391 void registerAnimatingRenderNode(RenderNode animator) {
392 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
393 }
394
395 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800396 protected void finalize() throws Throwable {
397 try {
398 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700399 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800400 } finally {
401 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800402 }
403 }
404
John Reckf47a5942014-06-30 16:20:04 -0700405 static void trimMemory(int level) {
406 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700407 }
408
John Reck66f0be62014-05-13 13:39:31 -0700409 private static class AtlasInitializer {
410 static AtlasInitializer sInstance = new AtlasInitializer();
411
412 private boolean mInitialized = false;
413
414 private AtlasInitializer() {}
415
John Reck3b202512014-06-23 13:13:08 -0700416 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700417 if (mInitialized) return;
418 IBinder binder = ServiceManager.getService("assetatlas");
419 if (binder == null) return;
420
421 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
422 try {
423 if (atlas.isCompatible(android.os.Process.myPpid())) {
424 GraphicBuffer buffer = atlas.getBuffer();
425 if (buffer != null) {
426 long[] map = atlas.getMap();
427 if (map != null) {
John Reckb8802b12014-06-16 15:28:50 -0700428 // TODO Remove after fixing b/15425820
429 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700430 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700431 mInitialized = true;
432 }
433 // If IAssetAtlas is not the same class as the IBinder
434 // we are using a remote service and we can safely
435 // destroy the graphic buffer
436 if (atlas.getClass() != binder.getClass()) {
437 buffer.destroy();
438 }
439 }
440 }
441 } catch (RemoteException e) {
442 Log.w(LOG_TAG, "Could not acquire atlas", e);
443 }
444 }
John Reckb8802b12014-06-16 15:28:50 -0700445
446 private static void validateMap(Context context, long[] map) {
447 Log.d("Atlas", "Validating map...");
448 HashSet<Long> preloadedPointers = new HashSet<Long>();
449
450 // We only care about drawables that hold bitmaps
451 final Resources resources = context.getResources();
452 final LongSparseArray<Drawable.ConstantState> drawables = resources.getPreloadedDrawables();
453
454 final int count = drawables.size();
455 for (int i = 0; i < count; i++) {
456 final Bitmap bitmap = drawables.valueAt(i).getBitmap();
457 if (bitmap != null && bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
458 preloadedPointers.add(bitmap.mNativeBitmap);
459 }
460 }
461
462 for (int i = 0; i < map.length; i += 4) {
463 if (!preloadedPointers.contains(map[i])) {
464 Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i]));
465 map[i] = 0;
466 }
467 }
468 }
John Reck66f0be62014-05-13 13:39:31 -0700469 }
470
John Reck84a4c882014-05-30 14:34:03 -0700471 static native void setupShadersDiskCache(String cacheFile);
472
John Reck3b202512014-06-23 13:13:08 -0700473 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reck4f02bf42014-01-03 18:09:17 -0800474
John Recke45b1fd2014-04-15 09:50:16 -0700475 private static native long nCreateRootRenderNode();
476 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800477 private static native void nDeleteProxy(long nativeProxy);
478
John Reck18f16e62014-05-02 16:46:41 -0700479 private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos);
John Recke4280ba2014-05-05 16:39:37 -0700480 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reck18f16e62014-05-02 16:46:41 -0700481
John Reck4f02bf42014-01-03 18:09:17 -0800482 private static native boolean nInitialize(long nativeProxy, Surface window);
483 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700484 private static native void nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700485 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700486 float lightX, float lightY, float lightZ, float lightRadius,
487 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700488 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckfe5e7b72014-05-23 17:42:28 -0700489 private static native int nSyncAndDrawFrame(long nativeProxy,
John Recke4267ea2014-06-03 15:53:15 -0700490 long frameTimeNanos, long recordDuration, float density);
John Reck17035b02014-09-03 07:39:53 -0700491 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700492 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800493
John Reck3b202512014-06-23 13:13:08 -0700494 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800495
496 private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height);
497 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700498 private static native void nBuildLayer(long nativeProxy, long node);
John Reck19b6bcf2014-02-14 20:03:38 -0800499 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700500 private static native void nPushLayerUpdate(long nativeProxy, long layer);
501 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700502 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700503
John Reckf47a5942014-06-30 16:20:04 -0700504 private static native void nDestroyHardwareResources(long nativeProxy);
505 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700506
John Reck28ad7b52014-04-07 16:59:25 -0700507 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700508 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700509 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700510
511 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800512}