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