The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 20 | import android.content.res.CompatibilityInfo.Translator; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Matrix; |
| 23 | import android.graphics.Rect; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 24 | import android.graphics.SurfaceTexture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 26 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 28 | |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 32 | import dalvik.system.CloseGuard; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
Glenn Kasten | 334031c | 2010-11-09 21:54:38 -0800 | [diff] [blame] | 35 | * Handle onto a raw buffer that is being managed by the screen compositor. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class Surface implements Parcelable { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 38 | private static final String TAG = "Surface"; |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 39 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 40 | private static native long nativeCreateFromSurfaceTexture(SurfaceTexture surfaceTexture) |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 41 | throws OutOfResourcesException; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 42 | private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 43 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 44 | private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty) |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 45 | throws OutOfResourcesException; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 46 | private static native void nativeUnlockCanvasAndPost(long nativeObject, Canvas canvas); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 47 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 48 | private static native void nativeRelease(long nativeObject); |
| 49 | private static native boolean nativeIsValid(long nativeObject); |
| 50 | private static native boolean nativeIsConsumerRunningBehind(long nativeObject); |
| 51 | private static native long nativeReadFromParcel(long nativeObject, Parcel source); |
| 52 | private static native void nativeWriteToParcel(long nativeObject, Parcel dest); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 53 | |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 54 | private static native void nativeAllocateBuffers(long nativeObject); |
| 55 | |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 56 | private static native int nativeGetWidth(long nativeObject); |
| 57 | private static native int nativeGetHeight(long nativeObject); |
| 58 | |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 59 | private static native long nativeGetNextFrameNumber(long nativeObject); |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 60 | private static native int nativeSetScalingMode(long nativeObject, int scalingMode); |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 61 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 62 | public static final Parcelable.Creator<Surface> CREATOR = |
| 63 | new Parcelable.Creator<Surface>() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 64 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 65 | public Surface createFromParcel(Parcel source) { |
| 66 | try { |
| 67 | Surface s = new Surface(); |
| 68 | s.readFromParcel(source); |
| 69 | return s; |
| 70 | } catch (Exception e) { |
| 71 | Log.e(TAG, "Exception creating surface from parcel", e); |
| 72 | return null; |
| 73 | } |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 74 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 77 | public Surface[] newArray(int size) { |
| 78 | return new Surface[size]; |
| 79 | } |
| 80 | }; |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 81 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 82 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 83 | |
| 84 | // Guarded state. |
| 85 | final Object mLock = new Object(); // protects the native state |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 86 | private String mName; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 87 | long mNativeObject; // package scope only for SurfaceControl access |
| 88 | private long mLockedObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 89 | private int mGenerationId; // incremented each time mNativeObject changes |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 90 | private final Canvas mCanvas = new CompatibleCanvas(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 92 | // A matrix to scale the matrix set by application. This is set to null for |
| 93 | // non compatibility mode. |
| 94 | private Matrix mCompatibleMatrix; |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 95 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 96 | private HwuiContext mHwuiContext; |
| 97 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 98 | /** @hide */ |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 99 | @Retention(RetentionPolicy.SOURCE) |
| 100 | @IntDef({SCALING_MODE_FREEZE, SCALING_MODE_SCALE_TO_WINDOW, |
| 101 | SCALING_MODE_SCALE_CROP, SCALING_MODE_NO_SCALE_CROP}) |
| 102 | public @interface ScalingMode {} |
| 103 | // From system/window.h |
| 104 | /** @hide */ |
| 105 | static final int SCALING_MODE_FREEZE = 0; |
| 106 | /** @hide */ |
| 107 | static final int SCALING_MODE_SCALE_TO_WINDOW = 1; |
| 108 | /** @hide */ |
| 109 | static final int SCALING_MODE_SCALE_CROP = 2; |
| 110 | /** @hide */ |
| 111 | static final int SCALING_MODE_NO_SCALE_CROP = 3; |
| 112 | |
| 113 | /** @hide */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 114 | @IntDef({ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270}) |
| 115 | @Retention(RetentionPolicy.SOURCE) |
| 116 | public @interface Rotation {} |
| 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | /** |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 119 | * Rotation constant: 0 degree rotation (natural orientation) |
| 120 | */ |
| 121 | public static final int ROTATION_0 = 0; |
| 122 | |
| 123 | /** |
| 124 | * Rotation constant: 90 degree rotation. |
| 125 | */ |
| 126 | public static final int ROTATION_90 = 1; |
| 127 | |
| 128 | /** |
| 129 | * Rotation constant: 180 degree rotation. |
| 130 | */ |
| 131 | public static final int ROTATION_180 = 2; |
| 132 | |
| 133 | /** |
| 134 | * Rotation constant: 270 degree rotation. |
| 135 | */ |
| 136 | public static final int ROTATION_270 = 3; |
| 137 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 138 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 139 | * Create an empty surface, which will later be filled in by readFromParcel(). |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 140 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | */ |
| 142 | public Surface() { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Create Surface from a {@link SurfaceTexture}. |
| 147 | * |
| 148 | * Images drawn to the Surface will be made available to the {@link |
| 149 | * SurfaceTexture}, which can attach them to an OpenGL ES texture via {@link |
| 150 | * SurfaceTexture#updateTexImage}. |
| 151 | * |
| 152 | * @param surfaceTexture The {@link SurfaceTexture} that is updated by this |
| 153 | * Surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 154 | * @throws OutOfResourcesException if the surface could not be created. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 155 | */ |
| 156 | public Surface(SurfaceTexture surfaceTexture) { |
| 157 | if (surfaceTexture == null) { |
| 158 | throw new IllegalArgumentException("surfaceTexture must not be null"); |
| 159 | } |
| 160 | |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 161 | synchronized (mLock) { |
| 162 | mName = surfaceTexture.toString(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 163 | setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 164 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 167 | /* called from android_view_Surface_createFromIGraphicBufferProducer() */ |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 168 | private Surface(long nativeObject) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 169 | synchronized (mLock) { |
| 170 | setNativeObjectLocked(nativeObject); |
| 171 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 174 | @Override |
| 175 | protected void finalize() throws Throwable { |
| 176 | try { |
| 177 | if (mCloseGuard != null) { |
| 178 | mCloseGuard.warnIfOpen(); |
| 179 | } |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 180 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 181 | } finally { |
| 182 | super.finalize(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Release the local reference to the server-side surface. |
| 188 | * Always call release() when you're done with a Surface. |
| 189 | * This will make the surface invalid. |
| 190 | */ |
| 191 | public void release() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 192 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 193 | if (mNativeObject != 0) { |
| 194 | nativeRelease(mNativeObject); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 195 | setNativeObjectLocked(0); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 196 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 197 | if (mHwuiContext != null) { |
| 198 | mHwuiContext.destroy(); |
| 199 | mHwuiContext = null; |
| 200 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 201 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Free all server-side state associated with this surface and |
| 206 | * release this object's reference. This method can only be |
| 207 | * called from the process that created the service. |
| 208 | * @hide |
| 209 | */ |
| 210 | public void destroy() { |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 211 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns true if this object holds a valid surface. |
| 216 | * |
| 217 | * @return True if it holds a physical surface, so lockCanvas() will succeed. |
| 218 | * Otherwise returns false. |
| 219 | */ |
| 220 | public boolean isValid() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 221 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 222 | if (mNativeObject == 0) return false; |
| 223 | return nativeIsValid(mNativeObject); |
| 224 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Gets the generation number of this surface, incremented each time |
| 229 | * the native surface contained within this object changes. |
| 230 | * |
| 231 | * @return The current generation number. |
| 232 | * @hide |
| 233 | */ |
| 234 | public int getGenerationId() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 235 | synchronized (mLock) { |
| 236 | return mGenerationId; |
| 237 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 241 | * Returns the next frame number which will be dequeued for rendering. |
| 242 | * Intended for use with SurfaceFlinger's deferred transactions API. |
| 243 | * |
| 244 | * @hide |
| 245 | */ |
| 246 | public long getNextFrameNumber() { |
| 247 | synchronized (mLock) { |
| 248 | return nativeGetNextFrameNumber(mNativeObject); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 253 | * Returns true if the consumer of this Surface is running behind the producer. |
| 254 | * |
| 255 | * @return True if the consumer is more than one buffer ahead of the producer. |
| 256 | * @hide |
| 257 | */ |
| 258 | public boolean isConsumerRunningBehind() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 259 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 260 | checkNotReleasedLocked(); |
| 261 | return nativeIsConsumerRunningBehind(mNativeObject); |
| 262 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Gets a {@link Canvas} for drawing into this surface. |
| 267 | * |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 268 | * After drawing into the provided {@link Canvas}, the caller must |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 269 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 270 | * |
Mathias Agopian | 30d7ab9 | 2013-02-14 15:38:40 -0800 | [diff] [blame] | 271 | * @param inOutDirty A rectangle that represents the dirty region that the caller wants |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 272 | * to redraw. This function may choose to expand the dirty rectangle if for example |
| 273 | * the surface has been resized or if the previous contents of the surface were |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 274 | * not available. The caller must redraw the entire dirty region as represented |
| 275 | * by the contents of the inOutDirty rectangle upon return from this function. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 276 | * The caller may also pass <code>null</code> instead, in the case where the |
| 277 | * entire surface should be redrawn. |
| 278 | * @return A canvas for drawing into the surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 279 | * |
| 280 | * @throws IllegalArgumentException If the inOutDirty rectangle is not valid. |
| 281 | * @throws OutOfResourcesException If the canvas cannot be locked. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 282 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 283 | public Canvas lockCanvas(Rect inOutDirty) |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 284 | throws Surface.OutOfResourcesException, IllegalArgumentException { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 285 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 286 | checkNotReleasedLocked(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 287 | if (mLockedObject != 0) { |
| 288 | // Ideally, nativeLockCanvas() would throw in this situation and prevent the |
| 289 | // double-lock, but that won't happen if mNativeObject was updated. We can't |
| 290 | // abandon the old mLockedObject because it might still be in use, so instead |
| 291 | // we just refuse to re-lock the Surface. |
Jesse Hall | d7e5596 | 2014-10-13 11:01:15 -0700 | [diff] [blame] | 292 | throw new IllegalArgumentException("Surface was already locked"); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 293 | } |
| 294 | mLockedObject = nativeLockCanvas(mNativeObject, mCanvas, inOutDirty); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 295 | return mCanvas; |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 296 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Posts the new contents of the {@link Canvas} to the surface and |
| 301 | * releases the {@link Canvas}. |
| 302 | * |
| 303 | * @param canvas The canvas previously obtained from {@link #lockCanvas}. |
| 304 | */ |
| 305 | public void unlockCanvasAndPost(Canvas canvas) { |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 306 | synchronized (mLock) { |
| 307 | checkNotReleasedLocked(); |
| 308 | |
| 309 | if (mHwuiContext != null) { |
| 310 | mHwuiContext.unlockAndPost(canvas); |
| 311 | } else { |
| 312 | unlockSwCanvasAndPost(canvas); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private void unlockSwCanvasAndPost(Canvas canvas) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 318 | if (canvas != mCanvas) { |
| 319 | throw new IllegalArgumentException("canvas object must be the same instance that " |
| 320 | + "was previously returned by lockCanvas"); |
| 321 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 322 | if (mNativeObject != mLockedObject) { |
| 323 | Log.w(TAG, "WARNING: Surface's mNativeObject (0x" + |
| 324 | Long.toHexString(mNativeObject) + ") != mLockedObject (0x" + |
| 325 | Long.toHexString(mLockedObject) +")"); |
| 326 | } |
| 327 | if (mLockedObject == 0) { |
| 328 | throw new IllegalStateException("Surface was not locked"); |
| 329 | } |
| 330 | try { |
| 331 | nativeUnlockCanvasAndPost(mLockedObject, canvas); |
| 332 | } finally { |
| 333 | nativeRelease(mLockedObject); |
| 334 | mLockedObject = 0; |
| 335 | } |
| 336 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 337 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 338 | /** |
| 339 | * Gets a {@link Canvas} for drawing into this surface. |
| 340 | * |
| 341 | * After drawing into the provided {@link Canvas}, the caller must |
| 342 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 343 | * |
| 344 | * Unlike {@link #lockCanvas(Rect)} this will return a hardware-accelerated |
| 345 | * canvas. See the <a href="{@docRoot}guide/topics/graphics/hardware-accel.html#unsupported"> |
| 346 | * unsupported drawing operations</a> for a list of what is and isn't |
John Reck | 43a5328 | 2014-10-23 13:56:03 -0700 | [diff] [blame] | 347 | * supported in a hardware-accelerated canvas. It is also required to |
| 348 | * fully cover the surface every time {@link #lockHardwareCanvas()} is |
| 349 | * called as the buffer is not preserved between frames. Partial updates |
| 350 | * are not supported. |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 351 | * |
| 352 | * @return A canvas for drawing into the surface. |
| 353 | * |
| 354 | * @throws IllegalStateException If the canvas cannot be locked. |
| 355 | */ |
| 356 | public Canvas lockHardwareCanvas() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 357 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 358 | checkNotReleasedLocked(); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 359 | if (mHwuiContext == null) { |
| 360 | mHwuiContext = new HwuiContext(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 361 | } |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 362 | return mHwuiContext.lockCanvas( |
| 363 | nativeGetWidth(mNativeObject), |
| 364 | nativeGetHeight(mNativeObject)); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 365 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 368 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 369 | * @deprecated This API has been removed and is not supported. Do not use. |
| 370 | */ |
| 371 | @Deprecated |
| 372 | public void unlockCanvas(Canvas canvas) { |
| 373 | throw new UnsupportedOperationException(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Sets the translator used to scale canvas's width/height in compatibility |
| 378 | * mode. |
| 379 | */ |
| 380 | void setCompatibilityTranslator(Translator translator) { |
| 381 | if (translator != null) { |
| 382 | float appScale = translator.applicationScale; |
| 383 | mCompatibleMatrix = new Matrix(); |
| 384 | mCompatibleMatrix.setScale(appScale, appScale); |
| 385 | } |
| 386 | } |
| 387 | |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 388 | /** |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 389 | * Copy another surface to this one. This surface now holds a reference |
| 390 | * to the same data as the original surface, and is -not- the owner. |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 391 | * This is for use by the window manager when returning a window surface |
| 392 | * back from a client, converting it from the representation being managed |
| 393 | * by the window manager to the representation the client uses to draw |
| 394 | * in to it. |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 395 | * @hide |
| 396 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 397 | public void copyFrom(SurfaceControl other) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 398 | if (other == null) { |
| 399 | throw new IllegalArgumentException("other must not be null"); |
| 400 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 401 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 402 | long surfaceControlPtr = other.mNativeObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 403 | if (surfaceControlPtr == 0) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 404 | throw new NullPointerException( |
| 405 | "SurfaceControl native object is null. Are you using a released SurfaceControl?"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 406 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 407 | long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 408 | |
| 409 | synchronized (mLock) { |
| 410 | if (mNativeObject != 0) { |
| 411 | nativeRelease(mNativeObject); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 412 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 413 | setNativeObjectLocked(newNativeObject); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 414 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 415 | } |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 416 | |
| 417 | /** |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 418 | * This is intended to be used by {@link SurfaceView#updateWindow} only. |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 419 | * @param other access is not thread safe |
| 420 | * @hide |
| 421 | * @deprecated |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 422 | */ |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 423 | @Deprecated |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 424 | public void transferFrom(Surface other) { |
| 425 | if (other == null) { |
| 426 | throw new IllegalArgumentException("other must not be null"); |
| 427 | } |
| 428 | if (other != this) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 429 | final long newPtr; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 430 | synchronized (other.mLock) { |
| 431 | newPtr = other.mNativeObject; |
| 432 | other.setNativeObjectLocked(0); |
| 433 | } |
| 434 | |
| 435 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 436 | if (mNativeObject != 0) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 437 | nativeRelease(mNativeObject); |
| 438 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 439 | setNativeObjectLocked(newPtr); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 440 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 441 | } |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 444 | @Override |
| 445 | public int describeContents() { |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | public void readFromParcel(Parcel source) { |
| 450 | if (source == null) { |
| 451 | throw new IllegalArgumentException("source must not be null"); |
| 452 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 453 | |
| 454 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 455 | // nativeReadFromParcel() will either return mNativeObject, or |
| 456 | // create a new native Surface and return it after reducing |
| 457 | // the reference count on mNativeObject. Either way, it is |
| 458 | // not necessary to call nativeRelease() here. |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 459 | mName = source.readString(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 460 | setNativeObjectLocked(nativeReadFromParcel(mNativeObject, source)); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 461 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public void writeToParcel(Parcel dest, int flags) { |
| 466 | if (dest == null) { |
| 467 | throw new IllegalArgumentException("dest must not be null"); |
| 468 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 469 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 470 | dest.writeString(mName); |
| 471 | nativeWriteToParcel(mNativeObject, dest); |
| 472 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 473 | if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { |
| 474 | release(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public String toString() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 480 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 481 | return "Surface(name=" + mName + ")/@0x" + |
| 482 | Integer.toHexString(System.identityHashCode(this)); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 486 | private void setNativeObjectLocked(long ptr) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 487 | if (mNativeObject != ptr) { |
| 488 | if (mNativeObject == 0 && ptr != 0) { |
| 489 | mCloseGuard.open("release"); |
| 490 | } else if (mNativeObject != 0 && ptr == 0) { |
| 491 | mCloseGuard.close(); |
| 492 | } |
| 493 | mNativeObject = ptr; |
| 494 | mGenerationId += 1; |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 495 | if (mHwuiContext != null) { |
| 496 | mHwuiContext.updateSurface(); |
| 497 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | private void checkNotReleasedLocked() { |
| 502 | if (mNativeObject == 0) { |
| 503 | throw new IllegalStateException("Surface has already been released."); |
| 504 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Mathias Agopian | c14bacf | 2012-04-23 18:23:08 -0700 | [diff] [blame] | 507 | /** |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 508 | * Allocate buffers ahead of time to avoid allocation delays during rendering |
| 509 | * @hide |
| 510 | */ |
| 511 | public void allocateBuffers() { |
| 512 | synchronized (mLock) { |
| 513 | checkNotReleasedLocked(); |
| 514 | nativeAllocateBuffers(mNativeObject); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 519 | * Set the scaling mode to be used for this surfaces buffers |
| 520 | * @hide |
| 521 | */ |
| 522 | void setScalingMode(@ScalingMode int scalingMode) { |
| 523 | synchronized (mLock) { |
| 524 | checkNotReleasedLocked(); |
| 525 | int err = nativeSetScalingMode(mNativeObject, scalingMode); |
| 526 | if (err != 0) { |
| 527 | throw new IllegalArgumentException("Invalid scaling mode: " + scalingMode); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 533 | * Exception thrown when a Canvas couldn't be locked with {@link Surface#lockCanvas}, or |
| 534 | * when a SurfaceTexture could not successfully be allocated. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 535 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 536 | @SuppressWarnings("serial") |
| 537 | public static class OutOfResourcesException extends RuntimeException { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 538 | public OutOfResourcesException() { |
| 539 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 540 | public OutOfResourcesException(String name) { |
| 541 | super(name); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 546 | * Returns a human readable representation of a rotation. |
| 547 | * |
| 548 | * @param rotation The rotation. |
| 549 | * @return The rotation symbolic name. |
Svetoslav Ganov | 545252f | 2012-12-10 18:29:24 -0800 | [diff] [blame] | 550 | * |
| 551 | * @hide |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 552 | */ |
| 553 | public static String rotationToString(int rotation) { |
| 554 | switch (rotation) { |
| 555 | case Surface.ROTATION_0: { |
| 556 | return "ROTATION_0"; |
| 557 | } |
| 558 | case Surface.ROTATION_90: { |
| 559 | return "ROATATION_90"; |
| 560 | } |
| 561 | case Surface.ROTATION_180: { |
| 562 | return "ROATATION_180"; |
| 563 | } |
| 564 | case Surface.ROTATION_270: { |
| 565 | return "ROATATION_270"; |
| 566 | } |
| 567 | default: { |
| 568 | throw new IllegalArgumentException("Invalid rotation: " + rotation); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 574 | * A Canvas class that can handle the compatibility mode. |
| 575 | * This does two things differently. |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 576 | * <ul> |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 577 | * <li>Returns the width and height of the target metrics, rather than |
| 578 | * native. For example, the canvas returns 320x480 even if an app is running |
| 579 | * in WVGA high density. |
| 580 | * <li>Scales the matrix in setMatrix by the application scale, except if |
| 581 | * the matrix looks like obtained from getMatrix. This is a hack to handle |
| 582 | * the case that an application uses getMatrix to keep the original matrix, |
| 583 | * set matrix of its own, then set the original matrix back. There is no |
| 584 | * perfect solution that works for all cases, and there are a lot of cases |
| 585 | * that this model does not work, but we hope this works for many apps. |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 586 | * </ul> |
| 587 | */ |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 588 | private final class CompatibleCanvas extends Canvas { |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 589 | // A temp matrix to remember what an application obtained via {@link getMatrix} |
| 590 | private Matrix mOrigMatrix = null; |
| 591 | |
| 592 | @Override |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 593 | public void setMatrix(Matrix matrix) { |
| 594 | if (mCompatibleMatrix == null || mOrigMatrix == null || mOrigMatrix.equals(matrix)) { |
| 595 | // don't scale the matrix if it's not compatibility mode, or |
| 596 | // the matrix was obtained from getMatrix. |
| 597 | super.setMatrix(matrix); |
| 598 | } else { |
| 599 | Matrix m = new Matrix(mCompatibleMatrix); |
| 600 | m.preConcat(matrix); |
| 601 | super.setMatrix(m); |
| 602 | } |
| 603 | } |
| 604 | |
Romain Guy | 8b5aa48 | 2013-02-28 18:13:54 -0800 | [diff] [blame] | 605 | @SuppressWarnings("deprecation") |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 606 | @Override |
| 607 | public void getMatrix(Matrix m) { |
| 608 | super.getMatrix(m); |
| 609 | if (mOrigMatrix == null) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 610 | mOrigMatrix = new Matrix(); |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 611 | } |
| 612 | mOrigMatrix.set(m); |
| 613 | } |
Romain Guy | d10cd57 | 2010-10-10 13:33:22 -0700 | [diff] [blame] | 614 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 615 | |
| 616 | private final class HwuiContext { |
| 617 | private final RenderNode mRenderNode; |
| 618 | private long mHwuiRenderer; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 619 | private DisplayListCanvas mCanvas; |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 620 | |
| 621 | HwuiContext() { |
| 622 | mRenderNode = RenderNode.create("HwuiCanvas", null); |
| 623 | mRenderNode.setClipToBounds(false); |
| 624 | mHwuiRenderer = nHwuiCreate(mRenderNode.mNativeRenderNode, mNativeObject); |
| 625 | } |
| 626 | |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 627 | Canvas lockCanvas(int width, int height) { |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 628 | if (mCanvas != null) { |
| 629 | throw new IllegalStateException("Surface was already locked!"); |
| 630 | } |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 631 | mCanvas = mRenderNode.start(width, height); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 632 | return mCanvas; |
| 633 | } |
| 634 | |
| 635 | void unlockAndPost(Canvas canvas) { |
| 636 | if (canvas != mCanvas) { |
| 637 | throw new IllegalArgumentException("canvas object must be the same instance that " |
| 638 | + "was previously returned by lockCanvas"); |
| 639 | } |
| 640 | mRenderNode.end(mCanvas); |
| 641 | mCanvas = null; |
| 642 | nHwuiDraw(mHwuiRenderer); |
| 643 | } |
| 644 | |
| 645 | void updateSurface() { |
| 646 | nHwuiSetSurface(mHwuiRenderer, mNativeObject); |
| 647 | } |
| 648 | |
| 649 | void destroy() { |
| 650 | if (mHwuiRenderer != 0) { |
| 651 | nHwuiDestroy(mHwuiRenderer); |
| 652 | mHwuiRenderer = 0; |
| 653 | } |
| 654 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | private static native long nHwuiCreate(long rootNode, long surface); |
| 658 | private static native void nHwuiSetSurface(long renderer, long surface); |
| 659 | private static native void nHwuiDraw(long renderer); |
| 660 | private static native void nHwuiDestroy(long renderer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | } |