blob: 87d5d9a4b1aa863d0ef5f2661928c8e0651ae41e [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
John Reckba6adf62015-02-19 14:36:50 -080019import android.annotation.IntDef;
John Reckb8802b12014-06-16 15:28:50 -070020import android.content.Context;
21import android.content.res.Resources;
Alan Viverette58c42c32014-07-12 20:33:45 -070022import android.content.res.TypedArray;
John Reck04fc5832014-02-05 16:38:25 -080023import android.graphics.Bitmap;
Alan Viveretteccb11e12014-07-08 16:04:02 -070024import android.graphics.Rect;
John Reckb8802b12014-06-16 15:28:50 -070025import android.graphics.drawable.Drawable;
John Reckedc524c2015-03-18 15:24:33 -070026import android.os.Binder;
John Reck66f0be62014-05-13 13:39:31 -070027import android.os.IBinder;
John Reckedc524c2015-03-18 15:24:33 -070028import android.os.ParcelFileDescriptor;
John Reck66f0be62014-05-13 13:39:31 -070029import android.os.RemoteException;
30import android.os.ServiceManager;
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 Reckcec24ae2013-11-05 13:27:50 -080034import android.view.Surface.OutOfResourcesException;
35import android.view.View.AttachInfo;
36
John Reckba6adf62015-02-19 14:36:50 -080037import com.android.internal.R;
38
John Reckfe5e7b72014-05-23 17:42:28 -070039import java.io.FileDescriptor;
John Reckcec24ae2013-11-05 13:27:50 -080040import java.io.PrintWriter;
John Reckba6adf62015-02-19 14:36:50 -080041import java.lang.annotation.Retention;
42import java.lang.annotation.RetentionPolicy;
John Reckdad7d84c2014-12-09 12:33:26 -080043import java.util.ArrayList;
John Reckb8802b12014-06-16 15:28:50 -070044import java.util.HashSet;
John Reckcec24ae2013-11-05 13:27:50 -080045
46/**
47 * Hardware renderer that proxies the rendering to a render thread. Most calls
John Reck4f02bf42014-01-03 18:09:17 -080048 * are currently synchronous.
John Reckcec24ae2013-11-05 13:27:50 -080049 *
50 * The UI thread can block on the RenderThread, but RenderThread must never
51 * block on the UI thread.
52 *
John Reck4f02bf42014-01-03 18:09:17 -080053 * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates
54 * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed
55 * by the lifecycle of the RenderProxy.
56 *
John Reckcec24ae2013-11-05 13:27:50 -080057 * Note that although currently the EGL context & surfaces are created & managed
58 * by the render thread, the goal is to move that into a shared structure that can
59 * be managed by both threads. EGLSurface creation & deletion should ideally be
60 * done on the UI thread and not the RenderThread to avoid stalling the
61 * RenderThread with surface buffer allocation.
62 *
63 * @hide
64 */
65public class ThreadedRenderer extends HardwareRenderer {
66 private static final String LOGTAG = "ThreadedRenderer";
67
John Reckf9be7792014-05-02 18:21:16 -070068 // Keep in sync with DrawFrameTask.h SYNC_* flags
69 // Nothing interesting to report
John Reckcd028f32014-06-24 08:44:29 -070070 private static final int SYNC_OK = 0;
John Reckf9be7792014-05-02 18:21:16 -070071 // Needs a ViewRoot invalidate
John Reckcd028f32014-06-24 08:44:29 -070072 private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0;
John Reckaa95a882014-11-07 11:02:07 -080073 // Spoiler: the reward is GPU-accelerated drawing, better find that Surface!
74 private static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 1 << 1;
John Reckf9be7792014-05-02 18:21:16 -070075
John Reckfe5e7b72014-05-23 17:42:28 -070076 private static final String[] VISUALIZERS = {
77 PROFILE_PROPERTY_VISUALIZE_BARS,
78 };
79
John Reckba6adf62015-02-19 14:36:50 -080080 private static final int FLAG_DUMP_FRAMESTATS = 1 << 0;
81 private static final int FLAG_DUMP_RESET = 1 << 1;
82
83 @IntDef(flag = true, value = {
84 FLAG_DUMP_FRAMESTATS, FLAG_DUMP_RESET })
85 @Retention(RetentionPolicy.SOURCE)
86 public @interface DumpFlags {}
87
Alan Viveretteccb11e12014-07-08 16:04:02 -070088 // Size of the rendered content.
John Reckcec24ae2013-11-05 13:27:50 -080089 private int mWidth, mHeight;
Alan Viveretteccb11e12014-07-08 16:04:02 -070090
91 // Actual size of the drawing surface.
92 private int mSurfaceWidth, mSurfaceHeight;
93
94 // Insets between the drawing surface and rendered content. These are
95 // applied as translation when updating the root render node.
96 private int mInsetTop, mInsetLeft;
97
Alan Viverette57774a82014-07-15 15:49:55 -070098 // Whether the surface has insets. Used to protect opacity.
99 private boolean mHasInsets;
100
Alan Viverette58c42c32014-07-12 20:33:45 -0700101 // Light and shadow properties specified by the theme.
102 private final float mLightY;
103 private final float mLightZ;
104 private final float mLightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700105 private final int mAmbientShadowAlpha;
106 private final int mSpotShadowAlpha;
Alan Viverette58c42c32014-07-12 20:33:45 -0700107
John Reck4f02bf42014-01-03 18:09:17 -0800108 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -0700109 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -0700110 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -0700111 private Choreographer mChoreographer;
John Reck0a973302014-07-16 13:29:45 -0700112 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800113
John Reckb8802b12014-06-16 15:28:50 -0700114 ThreadedRenderer(Context context, boolean translucent) {
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700115 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700116 mLightY = a.getDimension(R.styleable.Lighting_lightY, 0);
117 mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0);
118 mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0);
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700119 mAmbientShadowAlpha =
120 (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f);
121 mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f);
Alan Viverette58c42c32014-07-12 20:33:45 -0700122 a.recycle();
123
John Recke45b1fd2014-04-15 09:50:16 -0700124 long rootNodePtr = nCreateRootRenderNode();
125 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700126 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700127 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700128
John Reckedc524c2015-03-18 15:24:33 -0700129 ProcessInitializer.sInstance.init(context, mNativeProxy);
John Reck3b202512014-06-23 13:13:08 -0700130
John Reckfe5e7b72014-05-23 17:42:28 -0700131 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800132 }
133
134 @Override
John Reckf47a5942014-06-30 16:20:04 -0700135 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700136 mInitialized = false;
137 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700138 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800139 }
140
John Reckf7d9c1d2014-04-09 10:01:03 -0700141 private void updateEnabledState(Surface surface) {
142 if (surface == null || !surface.isValid()) {
143 setEnabled(false);
144 } else {
145 setEnabled(mInitialized);
146 }
147 }
148
John Reckcec24ae2013-11-05 13:27:50 -0800149 @Override
150 boolean initialize(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700151 mInitialized = true;
152 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700153 boolean status = nInitialize(mNativeProxy, surface);
154 surface.allocateBuffers();
155 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800156 }
157
158 @Override
159 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700160 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800161 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800162 }
163
164 @Override
John Reck01a5ea32014-12-03 13:01:07 -0800165 boolean pauseSurface(Surface surface) {
166 return nPauseSurface(mNativeProxy, surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700167 }
168
169 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800170 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800171 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700172 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800173 }
174
175 private static void destroyResources(View view) {
176 view.destroyHardwareResources();
177
178 if (view instanceof ViewGroup) {
179 ViewGroup group = (ViewGroup) view;
180
181 int count = group.getChildCount();
182 for (int i = 0; i < count; i++) {
183 destroyResources(group.getChildAt(i));
184 }
185 }
John Reckcec24ae2013-11-05 13:27:50 -0800186 }
187
188 @Override
189 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800190 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800191 }
192
193 @Override
John Reck918ad522014-06-27 14:45:25 -0700194 void detachSurfaceTexture(long hardwareLayer) {
195 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800196 }
197
198 @Override
Alan Viverette58c42c32014-07-12 20:33:45 -0700199 void setup(int width, int height, Rect surfaceInsets) {
200 final float lightX = width / 2.0f;
John Reckcec24ae2013-11-05 13:27:50 -0800201 mWidth = width;
202 mHeight = height;
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700203 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
204 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700205 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700206 mInsetLeft = surfaceInsets.left;
207 mInsetTop = surfaceInsets.top;
208 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
209 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700210
211 // If the surface has insets, it can't be opaque.
212 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700213 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700214 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700215 mInsetLeft = 0;
216 mInsetTop = 0;
217 mSurfaceWidth = width;
218 mSurfaceHeight = height;
219 }
220 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Chris Craik058fc642014-07-23 18:19:28 -0700221 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight,
222 lightX, mLightY, mLightZ, mLightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700223 mAmbientShadowAlpha, mSpotShadowAlpha);
John Reckcec24ae2013-11-05 13:27:50 -0800224 }
225
226 @Override
John Reck63a06672014-05-07 13:45:54 -0700227 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700228 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700229 }
230
231 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800232 int getWidth() {
233 return mWidth;
234 }
235
236 @Override
237 int getHeight() {
238 return mHeight;
239 }
240
241 @Override
John Reckba6adf62015-02-19 14:36:50 -0800242 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
John Reckfe5e7b72014-05-23 17:42:28 -0700243 pw.flush();
John Reckba6adf62015-02-19 14:36:50 -0800244 int flags = 0;
245 for (int i = 0; i < args.length; i++) {
246 switch (args[i]) {
247 case "framestats":
248 flags |= FLAG_DUMP_FRAMESTATS;
249 break;
250 case "reset":
251 flags |= FLAG_DUMP_RESET;
252 break;
253 }
John Reckfe5e7b72014-05-23 17:42:28 -0700254 }
John Reckba6adf62015-02-19 14:36:50 -0800255 nDumpProfileInfo(mNativeProxy, fd, flags);
John Reckcec24ae2013-11-05 13:27:50 -0800256 }
257
258 @Override
259 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700260 boolean changed = nLoadSystemProperties(mNativeProxy);
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) {
Chris Craik70850ea2014-11-18 10:49:23 -0800277 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
John Reck0a973302014-07-16 13:29:45 -0700278 updateViewTreeDisplayList(view);
279
280 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
Chris Craikf6829a02015-03-10 10:28:59 -0700281 DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
John Reck0a973302014-07-16 13:29:45 -0700282 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 Reckbc0cc022014-04-11 16:08:14 -0700309
John Reckba6adf62015-02-19 14:36:50 -0800310 final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer;
311 choreographer.mFrameInfo.markDrawStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700312
John Reck61375a82014-09-18 19:27:48 +0000313 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800314
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 Reckba6adf62015-02-19 14:36:50 -0800331 final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo;
332 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length);
John Reckaa95a882014-11-07 11:02:07 -0800333 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
334 setEnabled(false);
John Reckb13de072014-11-19 16:33:47 -0800335 attachInfo.mViewRootImpl.mSurface.release();
John Reckaa95a882014-11-07 11:02:07 -0800336 // Invalidate since we failed to draw. This should fetch a Surface
337 // if it is still needed or do nothing if we are no longer drawing
338 attachInfo.mViewRootImpl.invalidate();
339 }
John Reckf9be7792014-05-02 18:21:16 -0700340 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
341 attachInfo.mViewRootImpl.invalidate();
342 }
John Reckcec24ae2013-11-05 13:27:50 -0800343 }
344
John Reck3b202512014-06-23 13:13:08 -0700345 static void invokeFunctor(long functor, boolean waitForCompletion) {
346 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700347 }
348
349 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800350 HardwareLayer createTextureLayer() {
351 long layer = nCreateTextureLayer(mNativeProxy);
352 return HardwareLayer.adoptTextureLayer(this, layer);
353 }
354
355 @Override
John Reck3e824952014-08-20 10:08:39 -0700356 void buildLayer(RenderNode node) {
357 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
358 }
359
360 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800361 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
362 return nCopyLayerInto(mNativeProxy,
John Reck9d4efdf2015-04-17 20:45:40 +0000363 layer.getDeferredLayerUpdater(), bitmap.getSkBitmap());
John Reck19b6bcf2014-02-14 20:03:38 -0800364 }
365
366 @Override
367 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700368 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800369 }
370
371 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800372 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700373 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800374 }
375
376 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800377 void setName(String name) {
John Reckb36016c2015-03-11 08:50:53 -0700378 nSetName(mNativeProxy, name);
John Reckcec24ae2013-11-05 13:27:50 -0800379 }
380
John Reck4f02bf42014-01-03 18:09:17 -0800381 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700382 void fence() {
383 nFence(mNativeProxy);
384 }
385
386 @Override
John Reckf47a5942014-06-30 16:20:04 -0700387 void stopDrawing() {
388 nStopDrawing(mNativeProxy);
389 }
390
391 @Override
John Recka5dda642014-05-22 15:43:54 -0700392 public void notifyFramePending() {
393 nNotifyFramePending(mNativeProxy);
394 }
395
396 @Override
John Reck119907c2014-08-14 09:02:01 -0700397 void registerAnimatingRenderNode(RenderNode animator) {
398 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
399 }
400
401 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800402 protected void finalize() throws Throwable {
403 try {
404 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700405 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800406 } finally {
407 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800408 }
409 }
410
John Reckf47a5942014-06-30 16:20:04 -0700411 static void trimMemory(int level) {
412 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700413 }
414
John Reckedc524c2015-03-18 15:24:33 -0700415 public static void dumpProfileData(byte[] data, FileDescriptor fd) {
416 nDumpProfileData(data, fd);
417 }
418
419 private static class ProcessInitializer {
420 static ProcessInitializer sInstance = new ProcessInitializer();
421 static IGraphicsStats sGraphicsStatsService;
422 private static IBinder sProcToken;
John Reck66f0be62014-05-13 13:39:31 -0700423
424 private boolean mInitialized = false;
425
John Reckedc524c2015-03-18 15:24:33 -0700426 private ProcessInitializer() {}
John Reck66f0be62014-05-13 13:39:31 -0700427
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;
John Reckedc524c2015-03-18 15:24:33 -0700430 mInitialized = true;
431 initGraphicsStats(context, renderProxy);
432 initAssetAtlas(context, renderProxy);
433 }
434
435 private static void initGraphicsStats(Context context, long renderProxy) {
436 IBinder binder = ServiceManager.getService("graphicsstats");
437 if (binder == null) return;
438
439 sGraphicsStatsService = IGraphicsStats.Stub.asInterface(binder);
440 sProcToken = new Binder();
441 try {
442 final String pkg = context.getApplicationInfo().packageName;
443 ParcelFileDescriptor pfd = sGraphicsStatsService.
444 requestBufferForProcess(pkg, sProcToken);
445 nSetProcessStatsBuffer(renderProxy, pfd.getFd());
446 pfd.close();
447 } catch (Exception e) {
448 Log.w(LOG_TAG, "Could not acquire gfx stats buffer", e);
449 }
450 }
451
452 private static void initAssetAtlas(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700453 IBinder binder = ServiceManager.getService("assetatlas");
454 if (binder == null) return;
455
456 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
457 try {
458 if (atlas.isCompatible(android.os.Process.myPpid())) {
459 GraphicBuffer buffer = atlas.getBuffer();
460 if (buffer != null) {
461 long[] map = atlas.getMap();
462 if (map != null) {
John Reck7809f832015-04-17 20:45:19 +0000463 // TODO Remove after fixing b/15425820
464 validateMap(context, map);
John Reck3b202512014-06-23 13:13:08 -0700465 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700466 }
467 // If IAssetAtlas is not the same class as the IBinder
468 // we are using a remote service and we can safely
469 // destroy the graphic buffer
470 if (atlas.getClass() != binder.getClass()) {
471 buffer.destroy();
472 }
473 }
474 }
475 } catch (RemoteException e) {
476 Log.w(LOG_TAG, "Could not acquire atlas", e);
477 }
478 }
John Reck7809f832015-04-17 20:45:19 +0000479
480 private static void validateMap(Context context, long[] map) {
481 Log.d("Atlas", "Validating map...");
482 HashSet<Long> preloadedPointers = new HashSet<Long>();
483
484 // We only care about drawables that hold bitmaps
485 final Resources resources = context.getResources();
486 final LongSparseArray<Drawable.ConstantState> drawables = resources.getPreloadedDrawables();
487
488 final int count = drawables.size();
489 ArrayList<Bitmap> tmpList = new ArrayList<Bitmap>();
490 for (int i = 0; i < count; i++) {
491 drawables.valueAt(i).addAtlasableBitmaps(tmpList);
492 for (int j = 0; j < tmpList.size(); j++) {
493 preloadedPointers.add(tmpList.get(j).getSkBitmap());
494 }
495 tmpList.clear();
496 }
497
498 for (int i = 0; i < map.length; i += 4) {
499 if (!preloadedPointers.contains(map[i])) {
500 Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i]));
501 map[i] = 0;
502 }
503 }
504 }
John Reck66f0be62014-05-13 13:39:31 -0700505 }
506
John Reck84a4c882014-05-30 14:34:03 -0700507 static native void setupShadersDiskCache(String cacheFile);
508
John Reck3b202512014-06-23 13:13:08 -0700509 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reckedc524c2015-03-18 15:24:33 -0700510 private static native void nSetProcessStatsBuffer(long nativeProxy, int fd);
John Reck4f02bf42014-01-03 18:09:17 -0800511
John Recke45b1fd2014-04-15 09:50:16 -0700512 private static native long nCreateRootRenderNode();
513 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800514 private static native void nDeleteProxy(long nativeProxy);
515
John Recke4280ba2014-05-05 16:39:37 -0700516 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reckb36016c2015-03-11 08:50:53 -0700517 private static native void nSetName(long nativeProxy, String name);
John Reck18f16e62014-05-02 16:46:41 -0700518
John Reck4f02bf42014-01-03 18:09:17 -0800519 private static native boolean nInitialize(long nativeProxy, Surface window);
520 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reck01a5ea32014-12-03 13:01:07 -0800521 private static native boolean nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700522 private static native void nSetup(long nativeProxy, int width, int height,
Chris Craik058fc642014-07-23 18:19:28 -0700523 float lightX, float lightY, float lightZ, float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700524 int ambientShadowAlpha, int spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -0700525 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckba6adf62015-02-19 14:36:50 -0800526 private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
John Reck17035b02014-09-03 07:39:53 -0700527 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700528 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800529
John Reck3b202512014-06-23 13:13:08 -0700530 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800531
John Reck19b6bcf2014-02-14 20:03:38 -0800532 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700533 private static native void nBuildLayer(long nativeProxy, long node);
John Reck9d4efdf2015-04-17 20:45:40 +0000534 private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700535 private static native void nPushLayerUpdate(long nativeProxy, long layer);
536 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700537 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700538
John Reckf47a5942014-06-30 16:20:04 -0700539 private static native void nDestroyHardwareResources(long nativeProxy);
540 private static native void nTrimMemory(int level);
John Recke1628b72014-05-23 15:11:19 -0700541
John Reck28ad7b52014-04-07 16:59:25 -0700542 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700543 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700544 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700545
John Reckba6adf62015-02-19 14:36:50 -0800546 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
547 @DumpFlags int dumpFlags);
John Reckedc524c2015-03-18 15:24:33 -0700548 private static native void nDumpProfileData(byte[] data, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800549}