John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 22 | import android.graphics.Rect; |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 27 | import android.os.SystemProperties; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 28 | import android.os.Trace; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 29 | import android.util.Log; |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 30 | import android.util.LongSparseArray; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 31 | import android.util.TimeUtils; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 32 | import android.view.Surface.OutOfResourcesException; |
| 33 | import android.view.View.AttachInfo; |
| 34 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 35 | import java.io.FileDescriptor; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 36 | import java.io.PrintWriter; |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 37 | import java.util.HashSet; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Hardware renderer that proxies the rendering to a render thread. Most calls |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 41 | * are currently synchronous. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 42 | * |
| 43 | * The UI thread can block on the RenderThread, but RenderThread must never |
| 44 | * block on the UI thread. |
| 45 | * |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 46 | * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates |
| 47 | * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed |
| 48 | * by the lifecycle of the RenderProxy. |
| 49 | * |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 50 | * Note that although currently the EGL context & surfaces are created & managed |
| 51 | * by the render thread, the goal is to move that into a shared structure that can |
| 52 | * be managed by both threads. EGLSurface creation & deletion should ideally be |
| 53 | * done on the UI thread and not the RenderThread to avoid stalling the |
| 54 | * RenderThread with surface buffer allocation. |
| 55 | * |
| 56 | * @hide |
| 57 | */ |
| 58 | public class ThreadedRenderer extends HardwareRenderer { |
| 59 | private static final String LOGTAG = "ThreadedRenderer"; |
| 60 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 61 | // Keep in sync with DrawFrameTask.h SYNC_* flags |
| 62 | // Nothing interesting to report |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 63 | private static final int SYNC_OK = 0; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 64 | // Needs a ViewRoot invalidate |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 65 | private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 66 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 67 | private static final String[] VISUALIZERS = { |
| 68 | PROFILE_PROPERTY_VISUALIZE_BARS, |
| 69 | }; |
| 70 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 71 | // Size of the rendered content. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 72 | private int mWidth, mHeight; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 73 | |
| 74 | // Actual size of the drawing surface. |
| 75 | private int mSurfaceWidth, mSurfaceHeight; |
| 76 | |
| 77 | // Insets between the drawing surface and rendered content. These are |
| 78 | // applied as translation when updating the root render node. |
| 79 | private int mInsetTop, mInsetLeft; |
| 80 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 81 | private long mNativeProxy; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 82 | private boolean mInitialized = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 83 | private RenderNode mRootNode; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 84 | private Choreographer mChoreographer; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 85 | private boolean mProfilingEnabled; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 86 | |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 87 | ThreadedRenderer(Context context, boolean translucent) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 88 | long rootNodePtr = nCreateRootRenderNode(); |
| 89 | mRootNode = RenderNode.adopt(rootNodePtr); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 90 | mRootNode.setClipToBounds(false); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 91 | mNativeProxy = nCreateProxy(translucent, rootNodePtr); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 92 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 93 | AtlasInitializer.sInstance.init(context, mNativeProxy); |
| 94 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 95 | // Setup timing |
| 96 | mChoreographer = Choreographer.getInstance(); |
| 97 | nSetFrameInterval(mNativeProxy, mChoreographer.getFrameIntervalNanos()); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 98 | |
| 99 | loadSystemProperties(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 103 | void destroy() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 104 | mInitialized = false; |
| 105 | updateEnabledState(null); |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 106 | nDestroyCanvasAndSurface(mNativeProxy); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 107 | } |
| 108 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 109 | private void updateEnabledState(Surface surface) { |
| 110 | if (surface == null || !surface.isValid()) { |
| 111 | setEnabled(false); |
| 112 | } else { |
| 113 | setEnabled(mInitialized); |
| 114 | } |
| 115 | } |
| 116 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 117 | @Override |
| 118 | boolean initialize(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 119 | mInitialized = true; |
| 120 | updateEnabledState(surface); |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 121 | boolean status = nInitialize(mNativeProxy, surface); |
| 122 | surface.allocateBuffers(); |
| 123 | return status; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 128 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 129 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 133 | void pauseSurface(Surface surface) { |
| 134 | nPauseSurface(mNativeProxy, surface); |
| 135 | } |
| 136 | |
| 137 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 138 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 139 | destroyResources(view); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 140 | nDestroyHardwareResources(mNativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private static void destroyResources(View view) { |
| 144 | view.destroyHardwareResources(); |
| 145 | |
| 146 | if (view instanceof ViewGroup) { |
| 147 | ViewGroup group = (ViewGroup) view; |
| 148 | |
| 149 | int count = group.getChildCount(); |
| 150 | for (int i = 0; i < count; i++) { |
| 151 | destroyResources(group.getChildAt(i)); |
| 152 | } |
| 153 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 158 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 162 | void detachSurfaceTexture(long hardwareLayer) { |
| 163 | nDetachSurfaceTexture(mNativeProxy, hardwareLayer); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 167 | void setup(int width, int height, Rect surfaceInsets, float lightX, float lightY, float lightZ, |
| 168 | float lightRadius) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 169 | mWidth = width; |
| 170 | mHeight = height; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 171 | if (surfaceInsets != null) { |
| 172 | mInsetLeft = surfaceInsets.left; |
| 173 | mInsetTop = surfaceInsets.top; |
| 174 | mSurfaceWidth = width + mInsetLeft + surfaceInsets.right; |
| 175 | mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom; |
| 176 | } else { |
| 177 | mInsetLeft = 0; |
| 178 | mInsetTop = 0; |
| 179 | mSurfaceWidth = width; |
| 180 | mSurfaceHeight = height; |
| 181 | } |
| 182 | mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight); |
| 183 | nSetup(mNativeProxy, mSurfaceWidth, mSurfaceHeight, lightX, lightY, lightZ, lightRadius); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 187 | void setOpaque(boolean opaque) { |
| 188 | nSetOpaque(mNativeProxy, opaque); |
| 189 | } |
| 190 | |
| 191 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 192 | int getWidth() { |
| 193 | return mWidth; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | int getHeight() { |
| 198 | return mHeight; |
| 199 | } |
| 200 | |
| 201 | @Override |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 202 | void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) { |
| 203 | pw.flush(); |
| 204 | nDumpProfileInfo(mNativeProxy, fd); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 205 | } |
| 206 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 207 | private static int search(String[] values, String value) { |
| 208 | for (int i = 0; i < values.length; i++) { |
| 209 | if (values[i].equals(value)) return i; |
| 210 | } |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | private static boolean checkIfProfilingRequested() { |
| 215 | String profiling = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY); |
| 216 | int graphType = search(VISUALIZERS, profiling); |
| 217 | return (graphType >= 0) || Boolean.parseBoolean(profiling); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | boolean loadSystemProperties() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 222 | boolean changed = nLoadSystemProperties(mNativeProxy); |
| 223 | boolean wantProfiling = checkIfProfilingRequested(); |
| 224 | if (wantProfiling != mProfilingEnabled) { |
| 225 | mProfilingEnabled = wantProfiling; |
| 226 | changed = true; |
| 227 | } |
| 228 | return changed; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 229 | } |
| 230 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 231 | private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 232 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
| 233 | |
| 234 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 235 | == View.PFLAG_INVALIDATED; |
| 236 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 237 | |
| 238 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 239 | HardwareCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 240 | try { |
John Reck | 86faf9e | 2014-05-19 13:19:07 -0700 | [diff] [blame] | 241 | canvas.save(); |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame^] | 242 | canvas.translate(mInsetLeft, mInsetTop); |
Alan Viverette | d5b2ec4 | 2014-05-17 16:34:09 -0700 | [diff] [blame] | 243 | callbacks.onHardwarePreDraw(canvas); |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 244 | canvas.drawRenderNode(view.getDisplayList()); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 245 | callbacks.onHardwarePostDraw(canvas); |
John Reck | 86faf9e | 2014-05-19 13:19:07 -0700 | [diff] [blame] | 246 | canvas.restore(); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 247 | } finally { |
| 248 | mRootNode.end(canvas); |
| 249 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 250 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 251 | |
| 252 | view.mRecreateDisplayList = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 256 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) { |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 257 | attachInfo.mIgnoreDirtyState = true; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 258 | long frameTimeNanos = mChoreographer.getFrameTimeNanos(); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 259 | attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 260 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 261 | long recordDuration = 0; |
| 262 | if (mProfilingEnabled) { |
| 263 | recordDuration = System.nanoTime(); |
| 264 | } |
| 265 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 266 | updateRootDisplayList(view, callbacks); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 267 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 268 | if (mProfilingEnabled) { |
| 269 | recordDuration = System.nanoTime() - recordDuration; |
| 270 | } |
| 271 | |
John Reck | 6313b92 | 2014-04-16 18:59:21 -0700 | [diff] [blame] | 272 | attachInfo.mIgnoreDirtyState = false; |
| 273 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 274 | int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos, |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 275 | recordDuration, view.getResources().getDisplayMetrics().density); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 276 | if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) { |
| 277 | attachInfo.mViewRootImpl.invalidate(); |
| 278 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 279 | } |
| 280 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 281 | static void invokeFunctor(long functor, boolean waitForCompletion) { |
| 282 | nInvokeFunctor(functor, waitForCompletion); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 286 | HardwareLayer createTextureLayer() { |
| 287 | long layer = nCreateTextureLayer(mNativeProxy); |
| 288 | return HardwareLayer.adoptTextureLayer(this, layer); |
| 289 | } |
| 290 | |
| 291 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 292 | boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { |
| 293 | return nCopyLayerInto(mNativeProxy, |
| 294 | layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | void pushLayerUpdate(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 299 | nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 303 | void onLayerDestroyed(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 304 | nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 308 | void setName(String name) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 309 | } |
| 310 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 311 | @Override |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 312 | void fence() { |
| 313 | nFence(mNativeProxy); |
| 314 | } |
| 315 | |
| 316 | @Override |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 317 | void stopDrawing() { |
| 318 | nStopDrawing(mNativeProxy); |
| 319 | } |
| 320 | |
| 321 | @Override |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 322 | public void notifyFramePending() { |
| 323 | nNotifyFramePending(mNativeProxy); |
| 324 | } |
| 325 | |
| 326 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 327 | protected void finalize() throws Throwable { |
| 328 | try { |
| 329 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame] | 330 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 331 | } finally { |
| 332 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 336 | static void trimMemory(int level) { |
| 337 | nTrimMemory(level); |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 340 | private static class AtlasInitializer { |
| 341 | static AtlasInitializer sInstance = new AtlasInitializer(); |
| 342 | |
| 343 | private boolean mInitialized = false; |
| 344 | |
| 345 | private AtlasInitializer() {} |
| 346 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 347 | synchronized void init(Context context, long renderProxy) { |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 348 | if (mInitialized) return; |
| 349 | IBinder binder = ServiceManager.getService("assetatlas"); |
| 350 | if (binder == null) return; |
| 351 | |
| 352 | IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder); |
| 353 | try { |
| 354 | if (atlas.isCompatible(android.os.Process.myPpid())) { |
| 355 | GraphicBuffer buffer = atlas.getBuffer(); |
| 356 | if (buffer != null) { |
| 357 | long[] map = atlas.getMap(); |
| 358 | if (map != null) { |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 359 | // TODO Remove after fixing b/15425820 |
| 360 | validateMap(context, map); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 361 | nSetAtlas(renderProxy, buffer, map); |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 362 | mInitialized = true; |
| 363 | } |
| 364 | // If IAssetAtlas is not the same class as the IBinder |
| 365 | // we are using a remote service and we can safely |
| 366 | // destroy the graphic buffer |
| 367 | if (atlas.getClass() != binder.getClass()) { |
| 368 | buffer.destroy(); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } catch (RemoteException e) { |
| 373 | Log.w(LOG_TAG, "Could not acquire atlas", e); |
| 374 | } |
| 375 | } |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 376 | |
| 377 | private static void validateMap(Context context, long[] map) { |
| 378 | Log.d("Atlas", "Validating map..."); |
| 379 | HashSet<Long> preloadedPointers = new HashSet<Long>(); |
| 380 | |
| 381 | // We only care about drawables that hold bitmaps |
| 382 | final Resources resources = context.getResources(); |
| 383 | final LongSparseArray<Drawable.ConstantState> drawables = resources.getPreloadedDrawables(); |
| 384 | |
| 385 | final int count = drawables.size(); |
| 386 | for (int i = 0; i < count; i++) { |
| 387 | final Bitmap bitmap = drawables.valueAt(i).getBitmap(); |
| 388 | if (bitmap != null && bitmap.getConfig() == Bitmap.Config.ARGB_8888) { |
| 389 | preloadedPointers.add(bitmap.mNativeBitmap); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | for (int i = 0; i < map.length; i += 4) { |
| 394 | if (!preloadedPointers.contains(map[i])) { |
| 395 | Log.w("Atlas", String.format("Pointer 0x%X, not in getPreloadedDrawables?", map[i])); |
| 396 | map[i] = 0; |
| 397 | } |
| 398 | } |
| 399 | } |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 400 | } |
| 401 | |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 402 | static native void setupShadersDiskCache(String cacheFile); |
| 403 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 404 | private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 405 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 406 | private static native long nCreateRootRenderNode(); |
| 407 | private static native long nCreateProxy(boolean translucent, long rootRenderNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 408 | private static native void nDeleteProxy(long nativeProxy); |
| 409 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 410 | private static native void nSetFrameInterval(long nativeProxy, long frameIntervalNanos); |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 411 | private static native boolean nLoadSystemProperties(long nativeProxy); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 412 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 413 | private static native boolean nInitialize(long nativeProxy, Surface window); |
| 414 | private static native void nUpdateSurface(long nativeProxy, Surface window); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 415 | private static native void nPauseSurface(long nativeProxy, Surface window); |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 416 | private static native void nSetup(long nativeProxy, int width, int height, |
| 417 | float lightX, float lightY, float lightZ, float lightRadius); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 418 | private static native void nSetOpaque(long nativeProxy, boolean opaque); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 419 | private static native int nSyncAndDrawFrame(long nativeProxy, |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 420 | long frameTimeNanos, long recordDuration, float density); |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 421 | private static native void nDestroyCanvasAndSurface(long nativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 422 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 423 | private static native void nInvokeFunctor(long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 424 | |
| 425 | private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height); |
| 426 | private static native long nCreateTextureLayer(long nativeProxy); |
| 427 | private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 428 | private static native void nPushLayerUpdate(long nativeProxy, long layer); |
| 429 | private static native void nCancelLayerUpdate(long nativeProxy, long layer); |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 430 | private static native void nDetachSurfaceTexture(long nativeProxy, long layer); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 431 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 432 | private static native void nDestroyHardwareResources(long nativeProxy); |
| 433 | private static native void nTrimMemory(int level); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 434 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 435 | private static native void nFence(long nativeProxy); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 436 | private static native void nStopDrawing(long nativeProxy); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 437 | private static native void nNotifyFramePending(long nativeProxy); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 438 | |
| 439 | private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 440 | } |