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