blob: f6119e2898f0026dba043b89b3a7e4cef68d4fb7 [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 {
John Reckf7d9c1d2014-04-09 10:01:03 -0700148 mInitialized = true;
149 updateEnabledState(surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700150 boolean status = nInitialize(mNativeProxy, surface);
Dan Stoza5795d642014-06-20 13:01:36 -0700151 return status;
John Reckcec24ae2013-11-05 13:27:50 -0800152 }
153
154 @Override
155 void updateSurface(Surface surface) throws OutOfResourcesException {
John Reckf7d9c1d2014-04-09 10:01:03 -0700156 updateEnabledState(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800157 nUpdateSurface(mNativeProxy, surface);
John Reckcec24ae2013-11-05 13:27:50 -0800158 }
159
160 @Override
John Reck01a5ea32014-12-03 13:01:07 -0800161 boolean pauseSurface(Surface surface) {
162 return nPauseSurface(mNativeProxy, surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700163 }
164
165 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800166 void destroyHardwareResources(View view) {
John Reck4f02bf42014-01-03 18:09:17 -0800167 destroyResources(view);
John Reckf47a5942014-06-30 16:20:04 -0700168 nDestroyHardwareResources(mNativeProxy);
John Reck4f02bf42014-01-03 18:09:17 -0800169 }
170
171 private static void destroyResources(View view) {
172 view.destroyHardwareResources();
173
174 if (view instanceof ViewGroup) {
175 ViewGroup group = (ViewGroup) view;
176
177 int count = group.getChildCount();
178 for (int i = 0; i < count; i++) {
179 destroyResources(group.getChildAt(i));
180 }
181 }
John Reckcec24ae2013-11-05 13:27:50 -0800182 }
183
184 @Override
185 void invalidate(Surface surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800186 updateSurface(surface);
John Reckcec24ae2013-11-05 13:27:50 -0800187 }
188
189 @Override
John Reck918ad522014-06-27 14:45:25 -0700190 void detachSurfaceTexture(long hardwareLayer) {
191 nDetachSurfaceTexture(mNativeProxy, hardwareLayer);
John Reckcec24ae2013-11-05 13:27:50 -0800192 }
193
194 @Override
Alan Viverette50210d92015-05-14 18:05:36 -0700195 void setup(int width, int height, AttachInfo attachInfo, Rect surfaceInsets) {
John Reckcec24ae2013-11-05 13:27:50 -0800196 mWidth = width;
197 mHeight = height;
Alan Viverette50210d92015-05-14 18:05:36 -0700198
Alan Viverette3aa1ffb2014-10-30 12:22:08 -0700199 if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0
200 || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) {
Alan Viverette57774a82014-07-15 15:49:55 -0700201 mHasInsets = true;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700202 mInsetLeft = surfaceInsets.left;
203 mInsetTop = surfaceInsets.top;
204 mSurfaceWidth = width + mInsetLeft + surfaceInsets.right;
205 mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom;
Alan Viverette57774a82014-07-15 15:49:55 -0700206
207 // If the surface has insets, it can't be opaque.
208 setOpaque(false);
Alan Viveretteccb11e12014-07-08 16:04:02 -0700209 } else {
Alan Viverette57774a82014-07-15 15:49:55 -0700210 mHasInsets = false;
Alan Viveretteccb11e12014-07-08 16:04:02 -0700211 mInsetLeft = 0;
212 mInsetTop = 0;
213 mSurfaceWidth = width;
214 mSurfaceHeight = height;
215 }
Alan Viverette50210d92015-05-14 18:05:36 -0700216
Alan Viveretteccb11e12014-07-08 16:04:02 -0700217 mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight);
Alan Viverette50210d92015-05-14 18:05:36 -0700218 nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight, mLightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700219 mAmbientShadowAlpha, mSpotShadowAlpha);
Alan Viverette50210d92015-05-14 18:05:36 -0700220
221 setLightCenter(attachInfo);
222 }
223
224 @Override
225 void setLightCenter(AttachInfo attachInfo) {
226 // Adjust light position for window offsets.
227 final Point displaySize = attachInfo.mPoint;
228 attachInfo.mDisplay.getRealSize(displaySize);
229 final float lightX = displaySize.x / 2f - attachInfo.mWindowLeft;
230 final float lightY = mLightY - attachInfo.mWindowTop;
231
232 nSetLightCenter(mNativeProxy, lightX, lightY, mLightZ);
John Reckcec24ae2013-11-05 13:27:50 -0800233 }
234
235 @Override
John Reck63a06672014-05-07 13:45:54 -0700236 void setOpaque(boolean opaque) {
Alan Viverette57774a82014-07-15 15:49:55 -0700237 nSetOpaque(mNativeProxy, opaque && !mHasInsets);
John Reck63a06672014-05-07 13:45:54 -0700238 }
239
240 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800241 int getWidth() {
242 return mWidth;
243 }
244
245 @Override
246 int getHeight() {
247 return mHeight;
248 }
249
250 @Override
John Reckba6adf62015-02-19 14:36:50 -0800251 void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
John Reckfe5e7b72014-05-23 17:42:28 -0700252 pw.flush();
John Reckba6adf62015-02-19 14:36:50 -0800253 int flags = 0;
254 for (int i = 0; i < args.length; i++) {
255 switch (args[i]) {
256 case "framestats":
257 flags |= FLAG_DUMP_FRAMESTATS;
258 break;
259 case "reset":
260 flags |= FLAG_DUMP_RESET;
261 break;
262 }
John Reckfe5e7b72014-05-23 17:42:28 -0700263 }
John Reckba6adf62015-02-19 14:36:50 -0800264 nDumpProfileInfo(mNativeProxy, fd, flags);
John Reckcec24ae2013-11-05 13:27:50 -0800265 }
266
267 @Override
268 boolean loadSystemProperties() {
John Reckfe5e7b72014-05-23 17:42:28 -0700269 boolean changed = nLoadSystemProperties(mNativeProxy);
John Reck23d307c2014-10-27 12:38:48 -0700270 if (changed) {
271 invalidateRoot();
272 }
John Reckfe5e7b72014-05-23 17:42:28 -0700273 return changed;
John Reckcec24ae2013-11-05 13:27:50 -0800274 }
275
John Reck0a973302014-07-16 13:29:45 -0700276 private void updateViewTreeDisplayList(View view) {
John Reckcec24ae2013-11-05 13:27:50 -0800277 view.mPrivateFlags |= View.PFLAG_DRAWN;
John Reckcec24ae2013-11-05 13:27:50 -0800278 view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
279 == View.PFLAG_INVALIDATED;
280 view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -0700281 view.updateDisplayListIfDirty();
John Reckcec24ae2013-11-05 13:27:50 -0800282 view.mRecreateDisplayList = false;
John Reckbc0cc022014-04-11 16:08:14 -0700283 }
284
John Reck61375a82014-09-18 19:27:48 +0000285 private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {
Chris Craik70850ea2014-11-18 10:49:23 -0800286 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
John Reck0a973302014-07-16 13:29:45 -0700287 updateViewTreeDisplayList(view);
288
289 if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
Chris Craikf6829a02015-03-10 10:28:59 -0700290 DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
John Reck0a973302014-07-16 13:29:45 -0700291 try {
Alan Viverettedbed8932014-08-06 17:54:52 -0700292 final int saveCount = canvas.save();
John Reck0a973302014-07-16 13:29:45 -0700293 canvas.translate(mInsetLeft, mInsetTop);
294 callbacks.onHardwarePreDraw(canvas);
Chris Craikabedca32014-08-28 15:03:55 -0700295
296 canvas.insertReorderBarrier();
Chris Craik31a2d062015-05-01 14:22:47 -0700297 canvas.drawRenderNode(view.updateDisplayListIfDirty());
Chris Craikabedca32014-08-28 15:03:55 -0700298 canvas.insertInorderBarrier();
299
John Reck0a973302014-07-16 13:29:45 -0700300 callbacks.onHardwarePostDraw(canvas);
Alan Viverettedbed8932014-08-06 17:54:52 -0700301 canvas.restoreToCount(saveCount);
John Reck0a973302014-07-16 13:29:45 -0700302 mRootNodeNeedsUpdate = false;
303 } finally {
304 mRootNode.end(canvas);
305 }
306 }
307 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
308 }
309
310 @Override
311 void invalidateRoot() {
312 mRootNodeNeedsUpdate = true;
313 }
314
John Reckbc0cc022014-04-11 16:08:14 -0700315 @Override
John Reck61375a82014-09-18 19:27:48 +0000316 void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {
John Reckbc0cc022014-04-11 16:08:14 -0700317 attachInfo.mIgnoreDirtyState = true;
John Reckbc0cc022014-04-11 16:08:14 -0700318
John Reckba6adf62015-02-19 14:36:50 -0800319 final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer;
320 choreographer.mFrameInfo.markDrawStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700321
John Reck61375a82014-09-18 19:27:48 +0000322 updateRootDisplayList(view, callbacks);
John Reckcec24ae2013-11-05 13:27:50 -0800323
John Reck6313b922014-04-16 18:59:21 -0700324 attachInfo.mIgnoreDirtyState = false;
325
John Reck119907c2014-08-14 09:02:01 -0700326 // register animating rendernodes which started animating prior to renderer
327 // creation, which is typical for animators started prior to first draw
328 if (attachInfo.mPendingAnimatingRenderNodes != null) {
329 final int count = attachInfo.mPendingAnimatingRenderNodes.size();
330 for (int i = 0; i < count; i++) {
331 registerAnimatingRenderNode(
332 attachInfo.mPendingAnimatingRenderNodes.get(i));
333 }
334 attachInfo.mPendingAnimatingRenderNodes.clear();
335 // We don't need this anymore as subsequent calls to
336 // ViewRootImpl#attachRenderNodeAnimator will go directly to us.
337 attachInfo.mPendingAnimatingRenderNodes = null;
338 }
339
John Reckba6adf62015-02-19 14:36:50 -0800340 final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo;
341 int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length);
John Reckaa95a882014-11-07 11:02:07 -0800342 if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) {
343 setEnabled(false);
John Reckb13de072014-11-19 16:33:47 -0800344 attachInfo.mViewRootImpl.mSurface.release();
John Reckaa95a882014-11-07 11:02:07 -0800345 // Invalidate since we failed to draw. This should fetch a Surface
346 // if it is still needed or do nothing if we are no longer drawing
347 attachInfo.mViewRootImpl.invalidate();
348 }
John Reckf9be7792014-05-02 18:21:16 -0700349 if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) {
350 attachInfo.mViewRootImpl.invalidate();
351 }
John Reckcec24ae2013-11-05 13:27:50 -0800352 }
353
John Reck3b202512014-06-23 13:13:08 -0700354 static void invokeFunctor(long functor, boolean waitForCompletion) {
355 nInvokeFunctor(functor, waitForCompletion);
John Reck0d1f6342014-03-28 20:30:27 -0700356 }
357
358 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800359 HardwareLayer createTextureLayer() {
360 long layer = nCreateTextureLayer(mNativeProxy);
361 return HardwareLayer.adoptTextureLayer(this, layer);
362 }
363
364 @Override
John Reck3e824952014-08-20 10:08:39 -0700365 void buildLayer(RenderNode node) {
366 nBuildLayer(mNativeProxy, node.getNativeDisplayList());
367 }
368
369 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800370 boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) {
371 return nCopyLayerInto(mNativeProxy,
John Reck3731dc22015-04-13 15:20:29 -0700372 layer.getDeferredLayerUpdater(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800373 }
374
375 @Override
376 void pushLayerUpdate(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700377 nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800378 }
379
380 @Override
John Reck19b6bcf2014-02-14 20:03:38 -0800381 void onLayerDestroyed(HardwareLayer layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700382 nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater());
John Reck19b6bcf2014-02-14 20:03:38 -0800383 }
384
385 @Override
John Reckcec24ae2013-11-05 13:27:50 -0800386 void setName(String name) {
John Reckb36016c2015-03-11 08:50:53 -0700387 nSetName(mNativeProxy, name);
John Reckcec24ae2013-11-05 13:27:50 -0800388 }
389
John Reck4f02bf42014-01-03 18:09:17 -0800390 @Override
John Reck28ad7b52014-04-07 16:59:25 -0700391 void fence() {
392 nFence(mNativeProxy);
393 }
394
395 @Override
John Reckf47a5942014-06-30 16:20:04 -0700396 void stopDrawing() {
397 nStopDrawing(mNativeProxy);
398 }
399
400 @Override
John Recka5dda642014-05-22 15:43:54 -0700401 public void notifyFramePending() {
402 nNotifyFramePending(mNativeProxy);
403 }
404
405 @Override
John Reck119907c2014-08-14 09:02:01 -0700406 void registerAnimatingRenderNode(RenderNode animator) {
407 nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
408 }
409
410 @Override
John Reck4f02bf42014-01-03 18:09:17 -0800411 protected void finalize() throws Throwable {
412 try {
413 nDeleteProxy(mNativeProxy);
John Reck0ed751d2014-04-08 14:10:17 -0700414 mNativeProxy = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800415 } finally {
416 super.finalize();
John Reckcec24ae2013-11-05 13:27:50 -0800417 }
418 }
419
John Reckf47a5942014-06-30 16:20:04 -0700420 static void trimMemory(int level) {
421 nTrimMemory(level);
John Reck84a4c882014-05-30 14:34:03 -0700422 }
423
Chris Craik2507c342015-05-04 14:36:49 -0700424 public static void overrideProperty(@NonNull String name, @NonNull String value) {
425 if (name == null || value == null) {
426 throw new IllegalArgumentException("name and value must be non-null");
427 }
428 nOverrideProperty(name, value);
429 }
430
John Reckedc524c2015-03-18 15:24:33 -0700431 public static void dumpProfileData(byte[] data, FileDescriptor fd) {
432 nDumpProfileData(data, fd);
433 }
434
435 private static class ProcessInitializer {
436 static ProcessInitializer sInstance = new ProcessInitializer();
John Reckedc524c2015-03-18 15:24:33 -0700437 private static IBinder sProcToken;
John Reck66f0be62014-05-13 13:39:31 -0700438
439 private boolean mInitialized = false;
440
John Reckedc524c2015-03-18 15:24:33 -0700441 private ProcessInitializer() {}
John Reck66f0be62014-05-13 13:39:31 -0700442
John Reck3b202512014-06-23 13:13:08 -0700443 synchronized void init(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700444 if (mInitialized) return;
John Reckedc524c2015-03-18 15:24:33 -0700445 mInitialized = true;
446 initGraphicsStats(context, renderProxy);
447 initAssetAtlas(context, renderProxy);
448 }
449
450 private static void initGraphicsStats(Context context, long renderProxy) {
John Reckedc524c2015-03-18 15:24:33 -0700451 try {
John Reck828698b2015-06-30 12:56:03 -0700452 IBinder binder = ServiceManager.getService("graphicsstats");
453 if (binder == null) return;
454 IGraphicsStats graphicsStatsService = IGraphicsStats.Stub
455 .asInterface(binder);
456 sProcToken = new Binder();
John Reckedc524c2015-03-18 15:24:33 -0700457 final String pkg = context.getApplicationInfo().packageName;
John Reck828698b2015-06-30 12:56:03 -0700458 ParcelFileDescriptor pfd = graphicsStatsService.
John Reckedc524c2015-03-18 15:24:33 -0700459 requestBufferForProcess(pkg, sProcToken);
460 nSetProcessStatsBuffer(renderProxy, pfd.getFd());
461 pfd.close();
John Reck828698b2015-06-30 12:56:03 -0700462 } catch (Throwable t) {
463 Log.w(LOG_TAG, "Could not acquire gfx stats buffer", t);
John Reckedc524c2015-03-18 15:24:33 -0700464 }
465 }
466
467 private static void initAssetAtlas(Context context, long renderProxy) {
John Reck66f0be62014-05-13 13:39:31 -0700468 IBinder binder = ServiceManager.getService("assetatlas");
469 if (binder == null) return;
470
471 IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder);
472 try {
473 if (atlas.isCompatible(android.os.Process.myPpid())) {
474 GraphicBuffer buffer = atlas.getBuffer();
475 if (buffer != null) {
476 long[] map = atlas.getMap();
477 if (map != null) {
John Reck3b202512014-06-23 13:13:08 -0700478 nSetAtlas(renderProxy, buffer, map);
John Reck66f0be62014-05-13 13:39:31 -0700479 }
480 // If IAssetAtlas is not the same class as the IBinder
481 // we are using a remote service and we can safely
482 // destroy the graphic buffer
483 if (atlas.getClass() != binder.getClass()) {
484 buffer.destroy();
485 }
486 }
487 }
488 } catch (RemoteException e) {
489 Log.w(LOG_TAG, "Could not acquire atlas", e);
490 }
491 }
492 }
493
John Reck84a4c882014-05-30 14:34:03 -0700494 static native void setupShadersDiskCache(String cacheFile);
495
John Reck3b202512014-06-23 13:13:08 -0700496 private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
John Reckedc524c2015-03-18 15:24:33 -0700497 private static native void nSetProcessStatsBuffer(long nativeProxy, int fd);
John Reck4f02bf42014-01-03 18:09:17 -0800498
John Recke45b1fd2014-04-15 09:50:16 -0700499 private static native long nCreateRootRenderNode();
500 private static native long nCreateProxy(boolean translucent, long rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800501 private static native void nDeleteProxy(long nativeProxy);
502
John Recke4280ba2014-05-05 16:39:37 -0700503 private static native boolean nLoadSystemProperties(long nativeProxy);
John Reckb36016c2015-03-11 08:50:53 -0700504 private static native void nSetName(long nativeProxy, String name);
John Reck18f16e62014-05-02 16:46:41 -0700505
John Reck4f02bf42014-01-03 18:09:17 -0800506 private static native boolean nInitialize(long nativeProxy, Surface window);
507 private static native void nUpdateSurface(long nativeProxy, Surface window);
John Reck01a5ea32014-12-03 13:01:07 -0800508 private static native boolean nPauseSurface(long nativeProxy, Surface window);
Chris Craik797b95b2014-05-20 18:10:25 -0700509 private static native void nSetup(long nativeProxy, int width, int height,
Alan Viverette50210d92015-05-14 18:05:36 -0700510 float lightRadius, int ambientShadowAlpha, int spotShadowAlpha);
511 private static native void nSetLightCenter(long nativeProxy,
512 float lightX, float lightY, float lightZ);
John Reck63a06672014-05-07 13:45:54 -0700513 private static native void nSetOpaque(long nativeProxy, boolean opaque);
John Reckba6adf62015-02-19 14:36:50 -0800514 private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
John Reck17035b02014-09-03 07:39:53 -0700515 private static native void nDestroy(long nativeProxy);
John Reck119907c2014-08-14 09:02:01 -0700516 private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
John Reck4f02bf42014-01-03 18:09:17 -0800517
John Reck3b202512014-06-23 13:13:08 -0700518 private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
John Reck19b6bcf2014-02-14 20:03:38 -0800519
John Reck19b6bcf2014-02-14 20:03:38 -0800520 private static native long nCreateTextureLayer(long nativeProxy);
John Reck3e824952014-08-20 10:08:39 -0700521 private static native void nBuildLayer(long nativeProxy, long node);
John Reck3731dc22015-04-13 15:20:29 -0700522 private static native boolean nCopyLayerInto(long nativeProxy, long layer, Bitmap bitmap);
John Reckd72e0a32014-05-29 18:56:11 -0700523 private static native void nPushLayerUpdate(long nativeProxy, long layer);
524 private static native void nCancelLayerUpdate(long nativeProxy, long layer);
John Reck918ad522014-06-27 14:45:25 -0700525 private static native void nDetachSurfaceTexture(long nativeProxy, long layer);
John Reck28ad7b52014-04-07 16:59:25 -0700526
John Reckf47a5942014-06-30 16:20:04 -0700527 private static native void nDestroyHardwareResources(long nativeProxy);
528 private static native void nTrimMemory(int level);
Chris Craik2507c342015-05-04 14:36:49 -0700529 private static native void nOverrideProperty(String name, String value);
John Recke1628b72014-05-23 15:11:19 -0700530
John Reck28ad7b52014-04-07 16:59:25 -0700531 private static native void nFence(long nativeProxy);
John Reckf47a5942014-06-30 16:20:04 -0700532 private static native void nStopDrawing(long nativeProxy);
John Recka5dda642014-05-22 15:43:54 -0700533 private static native void nNotifyFramePending(long nativeProxy);
John Reckfe5e7b72014-05-23 17:42:28 -0700534
John Reckba6adf62015-02-19 14:36:50 -0800535 private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
536 @DumpFlags int dumpFlags);
John Reckedc524c2015-03-18 15:24:33 -0700537 private static native void nDumpProfileData(byte[] data, FileDescriptor fd);
John Reckcec24ae2013-11-05 13:27:50 -0800538}