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