blob: db147ab51263f672b2548011b497748a278079ec [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;
Chris Craik2507c342015-05-04 14:36:49 -070020import android.annotation.NonNull;
John Reckb8802b12014-06-16 15:28:50 -070021import android.content.Context;
Alan Viverette58c42c32014-07-12 20:33:45 -070022import android.content.res.TypedArray;
John Reck04fc5832014-02-05 16:38:25 -080023import android.graphics.Bitmap;
Alan Viverette50210d92015-05-14 18:05:36 -070024import android.graphics.Point;
Alan Viveretteccb11e12014-07-08 16:04:02 -070025import android.graphics.Rect;
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 Reckcec24ae2013-11-05 13:27:50 -080033import android.view.Surface.OutOfResourcesException;
34import android.view.View.AttachInfo;
35
John Reckba6adf62015-02-19 14:36:50 -080036import com.android.internal.R;
37
John Reckfe5e7b72014-05-23 17:42:28 -070038import java.io.FileDescriptor;
John Reckcec24ae2013-11-05 13:27:50 -080039import java.io.PrintWriter;
John Reckba6adf62015-02-19 14:36:50 -080040import java.lang.annotation.Retention;
41import java.lang.annotation.RetentionPolicy;
John Reckcec24ae2013-11-05 13:27:50 -080042
43/**
44 * Hardware renderer that proxies the rendering to a render thread. Most calls
John Reck4f02bf42014-01-03 18:09:17 -080045 * are currently synchronous.
John Reckcec24ae2013-11-05 13:27:50 -080046 *
47 * The UI thread can block on the RenderThread, but RenderThread must never
48 * block on the UI thread.
49 *
John Reck4f02bf42014-01-03 18:09:17 -080050 * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates
51 * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed
52 * by the lifecycle of the RenderProxy.
53 *
John Reckcec24ae2013-11-05 13:27:50 -080054 * Note that although currently the EGL context & surfaces are created & managed
55 * by the render thread, the goal is to move that into a shared structure that can
56 * be managed by both threads. EGLSurface creation & deletion should ideally be
57 * done on the UI thread and not the RenderThread to avoid stalling the
58 * RenderThread with surface buffer allocation.
59 *
60 * @hide
61 */
62public class ThreadedRenderer extends HardwareRenderer {
63 private static final String LOGTAG = "ThreadedRenderer";
64
John Reckf9be7792014-05-02 18:21:16 -070065 // Keep in sync with DrawFrameTask.h SYNC_* flags
66 // Nothing interesting to report
John Reckcd028f32014-06-24 08:44:29 -070067 private static final int SYNC_OK = 0;
John Reckf9be7792014-05-02 18:21:16 -070068 // Needs a ViewRoot invalidate
John Reckcd028f32014-06-24 08:44:29 -070069 private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0;
John Reckaa95a882014-11-07 11:02:07 -080070 // Spoiler: the reward is GPU-accelerated drawing, better find that Surface!
71 private static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 1 << 1;
John Reckf9be7792014-05-02 18:21:16 -070072
John Reckfe5e7b72014-05-23 17:42:28 -070073 private static final String[] VISUALIZERS = {
74 PROFILE_PROPERTY_VISUALIZE_BARS,
75 };
76
John Reckba6adf62015-02-19 14:36:50 -080077 private static final int FLAG_DUMP_FRAMESTATS = 1 << 0;
78 private static final int FLAG_DUMP_RESET = 1 << 1;
79
80 @IntDef(flag = true, value = {
81 FLAG_DUMP_FRAMESTATS, FLAG_DUMP_RESET })
82 @Retention(RetentionPolicy.SOURCE)
83 public @interface DumpFlags {}
84
Alan Viveretteccb11e12014-07-08 16:04:02 -070085 // Size of the rendered content.
John Reckcec24ae2013-11-05 13:27:50 -080086 private int mWidth, mHeight;
Alan Viveretteccb11e12014-07-08 16:04:02 -070087
88 // Actual size of the drawing surface.
89 private int mSurfaceWidth, mSurfaceHeight;
90
91 // Insets between the drawing surface and rendered content. These are
92 // applied as translation when updating the root render node.
93 private int mInsetTop, mInsetLeft;
94
Alan Viverette57774a82014-07-15 15:49:55 -070095 // Whether the surface has insets. Used to protect opacity.
96 private boolean mHasInsets;
97
Alan Viverette58c42c32014-07-12 20:33:45 -070098 // Light and shadow properties specified by the theme.
99 private final float mLightY;
100 private final float mLightZ;
101 private final float mLightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700102 private final int mAmbientShadowAlpha;
103 private final int mSpotShadowAlpha;
Alan Viverette58c42c32014-07-12 20:33:45 -0700104
John Reck4f02bf42014-01-03 18:09:17 -0800105 private long mNativeProxy;
John Reckf7d9c1d2014-04-09 10:01:03 -0700106 private boolean mInitialized = false;
John Reckbc0cc022014-04-11 16:08:14 -0700107 private RenderNode mRootNode;
John Reck18f16e62014-05-02 16:46:41 -0700108 private Choreographer mChoreographer;
John Reck0a973302014-07-16 13:29:45 -0700109 private boolean mRootNodeNeedsUpdate;
John Reckcec24ae2013-11-05 13:27:50 -0800110
John Reckb8802b12014-06-16 15:28:50 -0700111 ThreadedRenderer(Context context, boolean translucent) {
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700112 final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0);
Alan Viverette58c42c32014-07-12 20:33:45 -0700113 mLightY = a.getDimension(R.styleable.Lighting_lightY, 0);
114 mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0);
115 mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0);
Alan Viveretteed6f14a2014-08-26 14:53:28 -0700116 mAmbientShadowAlpha =
117 (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f);
118 mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f);
Alan Viverette58c42c32014-07-12 20:33:45 -0700119 a.recycle();
120
John Recke45b1fd2014-04-15 09:50:16 -0700121 long rootNodePtr = nCreateRootRenderNode();
122 mRootNode = RenderNode.adopt(rootNodePtr);
John Reckbc0cc022014-04-11 16:08:14 -0700123 mRootNode.setClipToBounds(false);
John Recke45b1fd2014-04-15 09:50:16 -0700124 mNativeProxy = nCreateProxy(translucent, rootNodePtr);
John Reck18f16e62014-05-02 16:46:41 -0700125
John Reckedc524c2015-03-18 15:24:33 -0700126 ProcessInitializer.sInstance.init(context, mNativeProxy);
John Reck3b202512014-06-23 13:13:08 -0700127
John Reckfe5e7b72014-05-23 17:42:28 -0700128 loadSystemProperties();
John Reckcec24ae2013-11-05 13:27:50 -0800129 }
130
131 @Override
John Reckf47a5942014-06-30 16:20:04 -0700132 void destroy() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700133 mInitialized = false;
134 updateEnabledState(null);
John Reck17035b02014-09-03 07:39:53 -0700135 nDestroy(mNativeProxy);
John Reckcec24ae2013-11-05 13:27:50 -0800136 }
137
John Reckf7d9c1d2014-04-09 10:01:03 -0700138 private void updateEnabledState(Surface surface) {
139 if (surface == null || !surface.isValid()) {
140 setEnabled(false);
141 } else {
142 setEnabled(mInitialized);
143 }
144 }
145
John Reckcec24ae2013-11-05 13:27:50 -0800146 @Override
147 boolean initialize(Surface surface) throws OutOfResourcesException {
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100148 boolean status = !mInitialized;
John Reckf7d9c1d2014-04-09 10:01:03 -0700149 mInitialized = true;
150 updateEnabledState(surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100151 nInitialize(mNativeProxy, surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700152 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800153 }
154
155 @Override
156 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700157 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800158 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800159 }
160
161 @Override
John Reck01a5ea32014-12-03 13:01:07 -0800162 boolean pauseSurface(Surface surface) {
163 return nPauseSurface(mNativeProxy, surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700164 }
165
166 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800167 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800168 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700169 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800170 }
171
172 private static void destroyResources(View view) {
173 view.destroyHardwareResources();
174
175 if (view instanceof ViewGroup) {
176 ViewGroup group = (ViewGroup) view;
177
178 int count = group.getChildCount();
179 for (int i = 0; i < count; i++) {
180 destroyResources(group.getChildAt(i));
181 }
182 }
John Reckcec24ae2013-11-05 13:27:50 -0800183 }
184
185 @Override
186 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800187 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800188 }
189
190 @Override
John Reck918ad522014-06-27 14:45:25 -0700191 void detachSurfaceTexture(long hardwareLayer) {
192 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800193 }
194
195 @Override
Alan Viverette50210d92015-05-14 18:05:36 -0700196 void setup(int width, int height, AttachInfo attachInfo, Rect surfaceInsets) {
John Reckcec24ae2013-11-05 13:27:50 -0800197 mWidth = width;
198 mHeight = height;
Alan Viverette50210d92015-05-14 18:05:36 -0700199
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700200 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
201 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700202 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700203 mInsetLeft = surfaceInsets.left;
204 mInsetTop = surfaceInsets.top;
205 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
206 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700207
208 // If the surface has insets, it can't be opaque.
209 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700210 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700211 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700212 mInsetLeft = 0;
213 mInsetTop = 0;
214 mSurfaceWidth = width;
215 mSurfaceHeight = height;
216 }
Alan Viverette50210d92015-05-14 18:05:36 -0700217
Alan Viveretteccb11e12014-07-08 16:04:02 -0700218 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Alan Viverette50210d92015-05-14 18:05:36 -0700219 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight, mLightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700220 mAmbientShadowAlpha, mSpotShadowAlpha);
Alan Viverette50210d92015-05-14 18:05:36 -0700221
222 setLightCenter(attachInfo);
223 }
224
225 @Override
226 void setLightCenter(AttachInfo attachInfo) {
227 // Adjust light position for window offsets.
228 final Point displaySize = attachInfo.mPoint;
229 attachInfo.mDisplay.getRealSize(displaySize);
230 final float lightX = displaySize.x / 2f - attachInfo.mWindowLeft;
231 final float lightY = mLightY - attachInfo.mWindowTop;
232
233 nSetLightCenter(mNativeProxy, lightX, lightY, mLightZ);
John Reckcec24ae2013-11-05 13:27:50 -0800234 }
235
236 @Override
John Reck63a06672014-05-07 13:45:54 -0700237 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700238 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700239 }
240
241 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800242 int getWidth() {
243 return mWidth;
244 }
245
246 @Override
247 int getHeight() {
248 return mHeight;
249 }
250
251 @Override
John Reckba6adf62015-02-19 14:36:50 -0800252 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
John Reckfe5e7b72014-05-23 17:42:28 -0700253 pw.flush();
John Reckba6adf62015-02-19 14:36:50 -0800254 int flags = 0;
255 for (int i = 0; i < args.length; i++) {
256 switch (args[i]) {
257 case "framestats":
258 flags |= FLAG_DUMP_FRAMESTATS;
259 break;
260 case "reset":
261 flags |= FLAG_DUMP_RESET;
262 break;
263 }
John Reckfe5e7b72014-05-23 17:42:28 -0700264 }
John Reckba6adf62015-02-19 14:36:50 -0800265 nDumpProfileInfo(mNativeProxy, fd, flags);
John Reckcec24ae2013-11-05 13:27:50 -0800266 }
267
268 @Override
269 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700270 boolean changed = nLoadSystemProperties(mNativeProxy);
John Reck23d307c2014-10-27 12:38:48 -0700271 if (changed) {
272 invalidateRoot();
273 }
John Reckfe5e7b72014-05-23 17:42:28 -0700274 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800275 }
276
John Reck0a973302014-07-16 13:29:45 -0700277 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800278 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800279 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
280 == View.PFLAG_INVALIDATED;
281 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -0700282 view.updateDisplayListIfDirty();
John Reckcec24ae2013-11-05 13:27:50 -0800283 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700284 }
285
John Reck61375a82014-09-18 19:27:48 +0000286 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
Chris Craik70850ea2014-11-18 10:49:23 -0800287 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
John Reck0a973302014-07-16 13:29:45 -0700288 updateViewTreeDisplayList(view);
289
290 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
Chris Craikf6829a02015-03-10 10:28:59 -0700291 DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
John Reck0a973302014-07-16 13:29:45 -0700292 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700293 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700294 canvas.translate(mInsetLeft, mInsetTop);
295 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700296
297 canvas.insertReorderBarrier();
Chris Craik31a2d062015-05-01 14:22:47 -0700298 canvas.drawRenderNode(view.updateDisplayListIfDirty());
Chris Craikabedca32014-08-28 15:03:55 -0700299 canvas.insertInorderBarrier();
300
John Reck0a973302014-07-16 13:29:45 -0700301 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700302 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700303 mRootNodeNeedsUpdate = false;
304 } finally {
305 mRootNode.end(canvas);
306 }
307 }
308 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
309 }
310
311 @Override
312 void invalidateRoot() {
313 mRootNodeNeedsUpdate = true;
314 }
315
John Reckbc0cc022014-04-11 16:08:14 -0700316 @Override
John Reck61375a82014-09-18 19:27:48 +0000317 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700318 attachInfo.mIgnoreDirtyState = true;
John Reckbc0cc022014-04-11 16:08:14 -0700319
John Reckba6adf62015-02-19 14:36:50 -0800320 final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer;
321 choreographer.mFrameInfo.markDrawStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700322
John Reck61375a82014-09-18 19:27:48 +0000323 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800324
John Reck6313b922014-04-16 18:59:21 -0700325 attachInfo.mIgnoreDirtyState = false;
326
John Reck119907c2014-08-14 09:02:01 -0700327 // register animating rendernodes which started animating prior to renderer
328 // creation, which is typical for animators started prior to first draw
329 if (attachInfo.mPendingAnimatingRenderNodes != null) {
330 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
331 for (int i = 0; i < count; i++) {
332 registerAnimatingRenderNode(
333 attachInfo.mPendingAnimatingRenderNodes.get(i));
334 }
335 attachInfo.mPendingAnimatingRenderNodes.clear();
336 // We don't need this anymore as subsequent calls to
337 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
338 attachInfo.mPendingAnimatingRenderNodes = null;
339 }
340
John Reckba6adf62015-02-19 14:36:50 -0800341 final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo;
342 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length);
John Reckaa95a882014-11-07 11:02:07 -0800343 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
344 setEnabled(false);
John Reckb13de072014-11-19 16:33:47 -0800345 attachInfo.mViewRootImpl.mSurface.release();
John Reckaa95a882014-11-07 11:02:07 -0800346 // Invalidate since we failed to draw. This should fetch a Surface
347 // if it is still needed or do nothing if we are no longer drawing
348 attachInfo.mViewRootImpl.invalidate();
349 }
John Reckf9be7792014-05-02 18:21:16 -0700350 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
351 attachInfo.mViewRootImpl.invalidate();
352 }
John Reckcec24ae2013-11-05 13:27:50 -0800353 }
354
John Reck3b202512014-06-23 13:13:08 -0700355 static void invokeFunctor(long functor, boolean waitForCompletion) {
356 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700357 }
358
359 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800360 HardwareLayer createTextureLayer() {
361 long layer = nCreateTextureLayer(mNativeProxy);
362 return HardwareLayer.adoptTextureLayer(this, layer);
363 }
364
365 @Override
John Reck3e824952014-08-20 10:08:39 -0700366 void buildLayer(RenderNode node) {
367 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
368 }
369
370 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800371 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
372 return nCopyLayerInto(mNativeProxy,
John Reck3731dc22015-04-13 15:20:29 -0700373 layer.getDeferredLayerUpdater(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800374 }
375
376 @Override
377 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700378 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800379 }
380
381 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800382 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700383 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800384 }
385
386 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800387 void setName(String name) {
John Reckb36016c2015-03-11 08:50:53 -0700388 nSetName(mNativeProxy, name);
John Reckcec24ae2013-11-05 13:27:50 -0800389 }
390
John Reck4f02bf42014-01-03 18:09:17 -0800391 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700392 void fence() {
393 nFence(mNativeProxy);
394 }
395
396 @Override
John Reckf47a5942014-06-30 16:20:04 -0700397 void stopDrawing() {
398 nStopDrawing(mNativeProxy);
399 }
400
401 @Override
John Recka5dda642014-05-22 15:43:54 -0700402 public void notifyFramePending() {
403 nNotifyFramePending(mNativeProxy);
404 }
405
406 @Override
John Reck119907c2014-08-14 09:02:01 -0700407 void registerAnimatingRenderNode(RenderNode animator) {
408 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
409 }
410
411 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800412 protected void finalize() throws Throwable {
413 try {
414 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700415 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800416 } finally {
417 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800418 }
419 }
420
John Reckf47a5942014-06-30 16:20:04 -0700421 static void trimMemory(int level) {
422 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700423 }
424
Chris Craik2507c342015-05-04 14:36:49 -0700425 public static void overrideProperty(@NonNull String name, @NonNull String value) {
426 if (name == null || value == null) {
427 throw new IllegalArgumentException("name and value must be non-null");
428 }
429 nOverrideProperty(name, value);
430 }
431
John Reckedc524c2015-03-18 15:24:33 -0700432 public static void dumpProfileData(byte[] data, FileDescriptor fd) {
433 nDumpProfileData(data, fd);
434 }
435
436 private static class ProcessInitializer {
437 static ProcessInitializer sInstance = new ProcessInitializer();
John Reckedc524c2015-03-18 15:24:33 -0700438 private static IBinder sProcToken;
John Reck66f0be62014-05-13 13:39:31 -0700439
440 private boolean mInitialized = false;
441
John Reckedc524c2015-03-18 15:24:33 -0700442 private ProcessInitializer() {}
John Reck66f0be62014-05-13 13:39:31 -0700443
John Reck3b202512014-06-23 13:13:08 -0700444 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700445 if (mInitialized) return;
John Reckedc524c2015-03-18 15:24:33 -0700446 mInitialized = true;
447 initGraphicsStats(context, renderProxy);
448 initAssetAtlas(context, renderProxy);
449 }
450
451 private static void initGraphicsStats(Context context, long renderProxy) {
John Reckedc524c2015-03-18 15:24:33 -0700452 try {
John Reck828698b2015-06-30 12:56:03 -0700453 IBinder binder = ServiceManager.getService("graphicsstats");
454 if (binder == null) return;
455 IGraphicsStats graphicsStatsService = IGraphicsStats.Stub
456 .asInterface(binder);
457 sProcToken = new Binder();
John Reckedc524c2015-03-18 15:24:33 -0700458 final String pkg = context.getApplicationInfo().packageName;
John Reck828698b2015-06-30 12:56:03 -0700459 ParcelFileDescriptor pfd = graphicsStatsService.
John Reckedc524c2015-03-18 15:24:33 -0700460 requestBufferForProcess(pkg, sProcToken);
461 nSetProcessStatsBuffer(renderProxy, pfd.getFd());
462 pfd.close();
John Reck828698b2015-06-30 12:56:03 -0700463 } catch (Throwable t) {
464 Log.w(LOG_TAG, "Could not acquire gfx stats buffer", t);
John Reckedc524c2015-03-18 15:24:33 -0700465 }
466 }
467
468 private static void initAssetAtlas(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700469 IBinder binder = ServiceManager.getService("assetatlas");
470 if (binder == null) return;
471
472 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
473 try {
474 if (atlas.isCompatible(android.os.Process.myPpid())) {
475 GraphicBuffer buffer = atlas.getBuffer();
476 if (buffer != null) {
477 long[] map = atlas.getMap();
478 if (map != null) {
John Reck3b202512014-06-23 13:13:08 -0700479 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700480 }
481 // If IAssetAtlas is not the same class as the IBinder
482 // we are using a remote service and we can safely
483 // destroy the graphic buffer
484 if (atlas.getClass() != binder.getClass()) {
485 buffer.destroy();
486 }
487 }
488 }
489 } catch (RemoteException e) {
490 Log.w(LOG_TAG, "Could not acquire atlas", e);
491 }
492 }
493 }
494
John Reck84a4c882014-05-30 14:34:03 -0700495 static native void setupShadersDiskCache(String cacheFile);
496
John Reck3b202512014-06-23 13:13:08 -0700497 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reckedc524c2015-03-18 15:24:33 -0700498 private static native void nSetProcessStatsBuffer(long nativeProxy, int fd);
John Reck4f02bf42014-01-03 18:09:17 -0800499
John Recke45b1fd2014-04-15 09:50:16 -0700500 private static native long nCreateRootRenderNode();
501 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800502 private static native void nDeleteProxy(long nativeProxy);
503
John Recke4280ba2014-05-05 16:39:37 -0700504 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reckb36016c2015-03-11 08:50:53 -0700505 private static native void nSetName(long nativeProxy, String name);
John Reck18f16e62014-05-02 16:46:41 -0700506
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100507 private static native void nInitialize(long nativeProxy, Surface window);
John Reck4f02bf42014-01-03 18:09:17 -0800508 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reck01a5ea32014-12-03 13:01:07 -0800509 private static native boolean nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700510 private static native void nSetup(long nativeProxy, int width, int height,
Alan Viverette50210d92015-05-14 18:05:36 -0700511 float lightRadius, int ambientShadowAlpha, int spotShadowAlpha);
512 private static native void nSetLightCenter(long nativeProxy,
513 float lightX, float lightY, float lightZ);
John Reck63a06672014-05-07 13:45:54 -0700514 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckba6adf62015-02-19 14:36:50 -0800515 private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
John Reck17035b02014-09-03 07:39:53 -0700516 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700517 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800518
John Reck3b202512014-06-23 13:13:08 -0700519 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800520
John Reck19b6bcf2014-02-14 20:03:38 -0800521 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700522 private static native void nBuildLayer(long nativeProxy, long node);
John Reck3731dc22015-04-13 15:20:29 -0700523 private static native boolean nCopyLayerInto(long nativeProxy, long layer, Bitmap bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700524 private static native void nPushLayerUpdate(long nativeProxy, long layer);
525 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700526 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700527
John Reckf47a5942014-06-30 16:20:04 -0700528 private static native void nDestroyHardwareResources(long nativeProxy);
529 private static native void nTrimMemory(int level);
Chris Craik2507c342015-05-04 14:36:49 -0700530 private static native void nOverrideProperty(String name, String value);
John Recke1628b72014-05-23 15:11:19 -0700531
John Reck28ad7b52014-04-07 16:59:25 -0700532 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700533 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700534 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700535
John Reckba6adf62015-02-19 14:36:50 -0800536 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
537 @DumpFlags int dumpFlags);
John Reckedc524c2015-03-18 15:24:33 -0700538 private static native void nDumpProfileData(byte[] data, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800539}