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); |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame^] | 116 | boolean status = nInitialize(mNativeProxy, surface); |
| 117 | surface.allocateBuffers(); |
| 118 | return status; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 123 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 124 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 128 | void pauseSurface(Surface surface) { |
| 129 | nPauseSurface(mNativeProxy, surface); |
| 130 | } |
| 131 | |
| 132 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 133 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 134 | destroyResources(view); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 135 | nFlushCaches(mNativeProxy, GLES20Canvas.FLUSH_CACHES_LAYERS); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private static void destroyResources(View view) { |
| 139 | view.destroyHardwareResources(); |
| 140 | |
| 141 | if (view instanceof ViewGroup) { |
| 142 | ViewGroup group = (ViewGroup) view; |
| 143 | |
| 144 | int count = group.getChildCount(); |
| 145 | for (int i = 0; i < count; i++) { |
| 146 | destroyResources(group.getChildAt(i)); |
| 147 | } |
| 148 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | void invalidate(Surface surface) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 153 | updateSurface(surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 157 | boolean safelyRun(Runnable action) { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 158 | nRunWithGlContext(mNativeProxy, action); |
| 159 | return true; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 163 | 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] | 164 | mWidth = width; |
| 165 | mHeight = height; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 166 | mRootNode.setLeftTopRightBottom(0, 0, mWidth, mHeight); |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 167 | nSetup(mNativeProxy, width, height, lightX, lightY, lightZ, lightRadius); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 171 | void setOpaque(boolean opaque) { |
| 172 | nSetOpaque(mNativeProxy, opaque); |
| 173 | } |
| 174 | |
| 175 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 176 | int getWidth() { |
| 177 | return mWidth; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | int getHeight() { |
| 182 | return mHeight; |
| 183 | } |
| 184 | |
| 185 | @Override |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 186 | void dumpGfxInfo(PrintWriter pw, FileDescriptor fd) { |
| 187 | pw.flush(); |
| 188 | nDumpProfileInfo(mNativeProxy, fd); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 189 | } |
| 190 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 191 | private static int search(String[] values, String value) { |
| 192 | for (int i = 0; i < values.length; i++) { |
| 193 | if (values[i].equals(value)) return i; |
| 194 | } |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | private static boolean checkIfProfilingRequested() { |
| 199 | String profiling = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY); |
| 200 | int graphType = search(VISUALIZERS, profiling); |
| 201 | return (graphType >= 0) || Boolean.parseBoolean(profiling); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | boolean loadSystemProperties() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 206 | boolean changed = nLoadSystemProperties(mNativeProxy); |
| 207 | boolean wantProfiling = checkIfProfilingRequested(); |
| 208 | if (wantProfiling != mProfilingEnabled) { |
| 209 | mProfilingEnabled = wantProfiling; |
| 210 | changed = true; |
| 211 | } |
| 212 | return changed; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 213 | } |
| 214 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 215 | private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 216 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
| 217 | |
| 218 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 219 | == View.PFLAG_INVALIDATED; |
| 220 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 221 | |
| 222 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 223 | HardwareCanvas canvas = mRootNode.start(mWidth, mHeight); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 224 | try { |
John Reck | 86faf9e | 2014-05-19 13:19:07 -0700 | [diff] [blame] | 225 | canvas.save(); |
Alan Viverette | d5b2ec4 | 2014-05-17 16:34:09 -0700 | [diff] [blame] | 226 | callbacks.onHardwarePreDraw(canvas); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 227 | canvas.drawDisplayList(view.getDisplayList()); |
| 228 | callbacks.onHardwarePostDraw(canvas); |
John Reck | 86faf9e | 2014-05-19 13:19:07 -0700 | [diff] [blame] | 229 | canvas.restore(); |
John Reck | 05e8584 | 2014-04-23 14:48:28 -0700 | [diff] [blame] | 230 | } finally { |
| 231 | mRootNode.end(canvas); |
| 232 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 233 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 234 | |
| 235 | view.mRecreateDisplayList = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 239 | void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) { |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 240 | attachInfo.mIgnoreDirtyState = true; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 241 | long frameTimeNanos = mChoreographer.getFrameTimeNanos(); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 242 | attachInfo.mDrawingTime = frameTimeNanos / TimeUtils.NANOS_PER_MS; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 243 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 244 | long recordDuration = 0; |
| 245 | if (mProfilingEnabled) { |
| 246 | recordDuration = System.nanoTime(); |
| 247 | } |
| 248 | |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 249 | updateRootDisplayList(view, callbacks); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 250 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 251 | if (mProfilingEnabled) { |
| 252 | recordDuration = System.nanoTime() - recordDuration; |
| 253 | } |
| 254 | |
John Reck | 6313b92 | 2014-04-16 18:59:21 -0700 | [diff] [blame] | 255 | attachInfo.mIgnoreDirtyState = false; |
| 256 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 257 | int syncResult = nSyncAndDrawFrame(mNativeProxy, frameTimeNanos, |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 258 | recordDuration, view.getResources().getDisplayMetrics().density); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 259 | if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) { |
| 260 | attachInfo.mViewRootImpl.invalidate(); |
| 261 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
Bo Liu | ae738a7 | 2014-04-27 16:22:04 -0700 | [diff] [blame] | 265 | void invokeFunctor(long functor, boolean waitForCompletion) { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 266 | nInvokeFunctor(mNativeProxy, functor, waitForCompletion); |
| 267 | } |
| 268 | |
| 269 | @Override |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 270 | HardwareLayer createTextureLayer() { |
| 271 | long layer = nCreateTextureLayer(mNativeProxy); |
| 272 | return HardwareLayer.adoptTextureLayer(this, layer); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | SurfaceTexture createSurfaceTexture(final HardwareLayer layer) { |
| 277 | final SurfaceTexture[] ret = new SurfaceTexture[1]; |
| 278 | nRunWithGlContext(mNativeProxy, new Runnable() { |
| 279 | @Override |
| 280 | public void run() { |
| 281 | ret[0] = layer.createSurfaceTexture(); |
| 282 | } |
| 283 | }); |
| 284 | return ret[0]; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { |
| 289 | return nCopyLayerInto(mNativeProxy, |
| 290 | layer.getDeferredLayerUpdater(), bitmap.mNativeBitmap); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | void pushLayerUpdate(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 295 | nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | void flushLayerUpdates() { |
| 300 | // TODO: Figure out what this should do or remove it |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | void onLayerDestroyed(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 305 | nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 309 | void setName(String name) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 310 | } |
| 311 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 312 | @Override |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 313 | void fence() { |
| 314 | nFence(mNativeProxy); |
| 315 | } |
| 316 | |
| 317 | @Override |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 318 | public void notifyFramePending() { |
| 319 | nNotifyFramePending(mNativeProxy); |
| 320 | } |
| 321 | |
| 322 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 323 | protected void finalize() throws Throwable { |
| 324 | try { |
| 325 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame] | 326 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 327 | } finally { |
| 328 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 332 | static void startTrimMemory(int level) { |
| 333 | // TODO |
| 334 | } |
| 335 | |
| 336 | static void endTrimMemory() { |
| 337 | // TODO |
| 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 | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 347 | synchronized void init(Context context) { |
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 | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 361 | nSetAtlas(buffer, map); |
| 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 | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 404 | private static native void nSetAtlas(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 | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 421 | private static native void nRunWithGlContext(long nativeProxy, Runnable runnable); |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 422 | private static native void nDestroyCanvasAndSurface(long nativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 423 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 424 | private static native void nInvokeFunctor(long nativeProxy, long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 425 | |
| 426 | private static native long nCreateDisplayListLayer(long nativeProxy, int width, int height); |
| 427 | private static native long nCreateTextureLayer(long nativeProxy); |
| 428 | private static native boolean nCopyLayerInto(long nativeProxy, long layer, long bitmap); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 429 | private static native void nPushLayerUpdate(long nativeProxy, long layer); |
| 430 | private static native void nCancelLayerUpdate(long nativeProxy, long layer); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 431 | |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 432 | private static native void nFlushCaches(long nativeProxy, int flushMode); |
| 433 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 434 | private static native void nFence(long nativeProxy); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 435 | private static native void nNotifyFramePending(long nativeProxy); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 436 | |
| 437 | private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 438 | } |