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