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 | |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 19 | import android.content.res.CompatibilityInfo.Translator; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Matrix; |
| 22 | import android.graphics.Rect; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 23 | import android.graphics.SurfaceTexture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.os.Parcel; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 25 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.util.Log; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 27 | import dalvik.system.CloseGuard; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
Glenn Kasten | 334031c | 2010-11-09 21:54:38 -0800 | [diff] [blame] | 30 | * 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] | 31 | */ |
| 32 | public class Surface implements Parcelable { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 33 | private static final String TAG = "Surface"; |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 34 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 35 | private static native long nativeCreateFromSurfaceTexture(SurfaceTexture surfaceTexture) |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 36 | throws OutOfResourcesException; |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 37 | private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 38 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 39 | private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty) |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 40 | throws OutOfResourcesException; |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 41 | private static native void nativeUnlockCanvasAndPost(long nativeObject, Canvas canvas); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 42 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 43 | private static native void nativeRelease(long nativeObject); |
| 44 | private static native boolean nativeIsValid(long nativeObject); |
| 45 | private static native boolean nativeIsConsumerRunningBehind(long nativeObject); |
| 46 | private static native long nativeReadFromParcel(long nativeObject, Parcel source); |
| 47 | private static native void nativeWriteToParcel(long nativeObject, Parcel dest); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 48 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 49 | public static final Parcelable.Creator<Surface> CREATOR = |
| 50 | new Parcelable.Creator<Surface>() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 51 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 52 | public Surface createFromParcel(Parcel source) { |
| 53 | try { |
| 54 | Surface s = new Surface(); |
| 55 | s.readFromParcel(source); |
| 56 | return s; |
| 57 | } catch (Exception e) { |
| 58 | Log.e(TAG, "Exception creating surface from parcel", e); |
| 59 | return null; |
| 60 | } |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 61 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 62 | |
| 63 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 64 | public Surface[] newArray(int size) { |
| 65 | return new Surface[size]; |
| 66 | } |
| 67 | }; |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 68 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 69 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 70 | |
| 71 | // Guarded state. |
| 72 | final Object mLock = new Object(); // protects the native state |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 73 | private String mName; |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 74 | long mNativeObject; // package scope only for SurfaceControl access |
| 75 | private long mLockedObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 76 | private int mGenerationId; // incremented each time mNativeObject changes |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 77 | private final Canvas mCanvas = new CompatibleCanvas(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 79 | // A matrix to scale the matrix set by application. This is set to null for |
| 80 | // non compatibility mode. |
| 81 | private Matrix mCompatibleMatrix; |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 84 | * Rotation constant: 0 degree rotation (natural orientation) |
| 85 | */ |
| 86 | public static final int ROTATION_0 = 0; |
| 87 | |
| 88 | /** |
| 89 | * Rotation constant: 90 degree rotation. |
| 90 | */ |
| 91 | public static final int ROTATION_90 = 1; |
| 92 | |
| 93 | /** |
| 94 | * Rotation constant: 180 degree rotation. |
| 95 | */ |
| 96 | public static final int ROTATION_180 = 2; |
| 97 | |
| 98 | /** |
| 99 | * Rotation constant: 270 degree rotation. |
| 100 | */ |
| 101 | public static final int ROTATION_270 = 3; |
| 102 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 103 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 104 | * Create an empty surface, which will later be filled in by readFromParcel(). |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 105 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | */ |
| 107 | public Surface() { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Create Surface from a {@link SurfaceTexture}. |
| 112 | * |
| 113 | * Images drawn to the Surface will be made available to the {@link |
| 114 | * SurfaceTexture}, which can attach them to an OpenGL ES texture via {@link |
| 115 | * SurfaceTexture#updateTexImage}. |
| 116 | * |
| 117 | * @param surfaceTexture The {@link SurfaceTexture} that is updated by this |
| 118 | * Surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 119 | * @throws OutOfResourcesException if the surface could not be created. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 120 | */ |
| 121 | public Surface(SurfaceTexture surfaceTexture) { |
| 122 | if (surfaceTexture == null) { |
| 123 | throw new IllegalArgumentException("surfaceTexture must not be null"); |
| 124 | } |
| 125 | |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 126 | synchronized (mLock) { |
| 127 | mName = surfaceTexture.toString(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 128 | setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 129 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 132 | /* called from android_view_Surface_createFromIGraphicBufferProducer() */ |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 133 | private Surface(long nativeObject) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 134 | synchronized (mLock) { |
| 135 | setNativeObjectLocked(nativeObject); |
| 136 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 139 | @Override |
| 140 | protected void finalize() throws Throwable { |
| 141 | try { |
| 142 | if (mCloseGuard != null) { |
| 143 | mCloseGuard.warnIfOpen(); |
| 144 | } |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 145 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 146 | } finally { |
| 147 | super.finalize(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Release the local reference to the server-side surface. |
| 153 | * Always call release() when you're done with a Surface. |
| 154 | * This will make the surface invalid. |
| 155 | */ |
| 156 | public void release() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 157 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 158 | if (mNativeObject != 0) { |
| 159 | nativeRelease(mNativeObject); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 160 | setNativeObjectLocked(0); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 161 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 162 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Free all server-side state associated with this surface and |
| 167 | * release this object's reference. This method can only be |
| 168 | * called from the process that created the service. |
| 169 | * @hide |
| 170 | */ |
| 171 | public void destroy() { |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 172 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns true if this object holds a valid surface. |
| 177 | * |
| 178 | * @return True if it holds a physical surface, so lockCanvas() will succeed. |
| 179 | * Otherwise returns false. |
| 180 | */ |
| 181 | public boolean isValid() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 182 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 183 | if (mNativeObject == 0) return false; |
| 184 | return nativeIsValid(mNativeObject); |
| 185 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Gets the generation number of this surface, incremented each time |
| 190 | * the native surface contained within this object changes. |
| 191 | * |
| 192 | * @return The current generation number. |
| 193 | * @hide |
| 194 | */ |
| 195 | public int getGenerationId() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 196 | synchronized (mLock) { |
| 197 | return mGenerationId; |
| 198 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns true if the consumer of this Surface is running behind the producer. |
| 203 | * |
| 204 | * @return True if the consumer is more than one buffer ahead of the producer. |
| 205 | * @hide |
| 206 | */ |
| 207 | public boolean isConsumerRunningBehind() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 208 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 209 | checkNotReleasedLocked(); |
| 210 | return nativeIsConsumerRunningBehind(mNativeObject); |
| 211 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Gets a {@link Canvas} for drawing into this surface. |
| 216 | * |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 217 | * After drawing into the provided {@link Canvas}, the caller must |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 218 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 219 | * |
Mathias Agopian | 30d7ab9 | 2013-02-14 15:38:40 -0800 | [diff] [blame] | 220 | * @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] | 221 | * to redraw. This function may choose to expand the dirty rectangle if for example |
| 222 | * 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] | 223 | * not available. The caller must redraw the entire dirty region as represented |
| 224 | * by the contents of the inOutDirty rectangle upon return from this function. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 225 | * The caller may also pass <code>null</code> instead, in the case where the |
| 226 | * entire surface should be redrawn. |
| 227 | * @return A canvas for drawing into the surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 228 | * |
| 229 | * @throws IllegalArgumentException If the inOutDirty rectangle is not valid. |
| 230 | * @throws OutOfResourcesException If the canvas cannot be locked. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 231 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 232 | public Canvas lockCanvas(Rect inOutDirty) |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 233 | throws Surface.OutOfResourcesException, IllegalArgumentException { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 234 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 235 | checkNotReleasedLocked(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 236 | if (mLockedObject != 0) { |
| 237 | // Ideally, nativeLockCanvas() would throw in this situation and prevent the |
| 238 | // double-lock, but that won't happen if mNativeObject was updated. We can't |
| 239 | // abandon the old mLockedObject because it might still be in use, so instead |
| 240 | // we just refuse to re-lock the Surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 241 | throw new IllegalStateException("Surface was already locked"); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 242 | } |
| 243 | mLockedObject = nativeLockCanvas(mNativeObject, mCanvas, inOutDirty); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 244 | return mCanvas; |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 245 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Posts the new contents of the {@link Canvas} to the surface and |
| 250 | * releases the {@link Canvas}. |
| 251 | * |
| 252 | * @param canvas The canvas previously obtained from {@link #lockCanvas}. |
| 253 | */ |
| 254 | public void unlockCanvasAndPost(Canvas canvas) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 255 | if (canvas != mCanvas) { |
| 256 | throw new IllegalArgumentException("canvas object must be the same instance that " |
| 257 | + "was previously returned by lockCanvas"); |
| 258 | } |
| 259 | |
| 260 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 261 | checkNotReleasedLocked(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 262 | if (mNativeObject != mLockedObject) { |
| 263 | Log.w(TAG, "WARNING: Surface's mNativeObject (0x" + |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 264 | Long.toHexString(mNativeObject) + ") != mLockedObject (0x" + |
| 265 | Long.toHexString(mLockedObject) +")"); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 266 | } |
| 267 | if (mLockedObject == 0) { |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 268 | throw new IllegalStateException("Surface was not locked"); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 269 | } |
| 270 | nativeUnlockCanvasAndPost(mLockedObject, canvas); |
| 271 | nativeRelease(mLockedObject); |
| 272 | mLockedObject = 0; |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 273 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 276 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 277 | * @deprecated This API has been removed and is not supported. Do not use. |
| 278 | */ |
| 279 | @Deprecated |
| 280 | public void unlockCanvas(Canvas canvas) { |
| 281 | throw new UnsupportedOperationException(); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Sets the translator used to scale canvas's width/height in compatibility |
| 286 | * mode. |
| 287 | */ |
| 288 | void setCompatibilityTranslator(Translator translator) { |
| 289 | if (translator != null) { |
| 290 | float appScale = translator.applicationScale; |
| 291 | mCompatibleMatrix = new Matrix(); |
| 292 | mCompatibleMatrix.setScale(appScale, appScale); |
| 293 | } |
| 294 | } |
| 295 | |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 296 | /** |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 297 | * Copy another surface to this one. This surface now holds a reference |
| 298 | * 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] | 299 | * This is for use by the window manager when returning a window surface |
| 300 | * back from a client, converting it from the representation being managed |
| 301 | * by the window manager to the representation the client uses to draw |
| 302 | * in to it. |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 303 | * @hide |
| 304 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 305 | public void copyFrom(SurfaceControl other) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 306 | if (other == null) { |
| 307 | throw new IllegalArgumentException("other must not be null"); |
| 308 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 309 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 310 | long surfaceControlPtr = other.mNativeObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 311 | if (surfaceControlPtr == 0) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 312 | throw new NullPointerException( |
| 313 | "SurfaceControl native object is null. Are you using a released SurfaceControl?"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 314 | } |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 315 | long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 316 | |
| 317 | synchronized (mLock) { |
| 318 | if (mNativeObject != 0) { |
| 319 | nativeRelease(mNativeObject); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 320 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 321 | setNativeObjectLocked(newNativeObject); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 322 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 323 | } |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 326 | * This is intended to be used by {@link SurfaceView#updateWindow} only. |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 327 | * @param other access is not thread safe |
| 328 | * @hide |
| 329 | * @deprecated |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 330 | */ |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 331 | @Deprecated |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 332 | public void transferFrom(Surface other) { |
| 333 | if (other == null) { |
| 334 | throw new IllegalArgumentException("other must not be null"); |
| 335 | } |
| 336 | if (other != this) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 337 | final long newPtr; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 338 | synchronized (other.mLock) { |
| 339 | newPtr = other.mNativeObject; |
| 340 | other.setNativeObjectLocked(0); |
| 341 | } |
| 342 | |
| 343 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 344 | if (mNativeObject != 0) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 345 | nativeRelease(mNativeObject); |
| 346 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 347 | setNativeObjectLocked(newPtr); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 348 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 349 | } |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 352 | @Override |
| 353 | public int describeContents() { |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | public void readFromParcel(Parcel source) { |
| 358 | if (source == null) { |
| 359 | throw new IllegalArgumentException("source must not be null"); |
| 360 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 361 | |
| 362 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 363 | // nativeReadFromParcel() will either return mNativeObject, or |
| 364 | // create a new native Surface and return it after reducing |
| 365 | // the reference count on mNativeObject. Either way, it is |
| 366 | // not necessary to call nativeRelease() here. |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 367 | mName = source.readString(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 368 | setNativeObjectLocked(nativeReadFromParcel(mNativeObject, source)); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 369 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void writeToParcel(Parcel dest, int flags) { |
| 374 | if (dest == null) { |
| 375 | throw new IllegalArgumentException("dest must not be null"); |
| 376 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 377 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 378 | dest.writeString(mName); |
| 379 | nativeWriteToParcel(mNativeObject, dest); |
| 380 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 381 | if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { |
| 382 | release(); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public String toString() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 388 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 389 | return "Surface(name=" + mName + ")/@0x" + |
| 390 | Integer.toHexString(System.identityHashCode(this)); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 394 | private void setNativeObjectLocked(long ptr) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 395 | if (mNativeObject != ptr) { |
| 396 | if (mNativeObject == 0 && ptr != 0) { |
| 397 | mCloseGuard.open("release"); |
| 398 | } else if (mNativeObject != 0 && ptr == 0) { |
| 399 | mCloseGuard.close(); |
| 400 | } |
| 401 | mNativeObject = ptr; |
| 402 | mGenerationId += 1; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | private void checkNotReleasedLocked() { |
| 407 | if (mNativeObject == 0) { |
| 408 | throw new IllegalStateException("Surface has already been released."); |
| 409 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Mathias Agopian | c14bacf | 2012-04-23 18:23:08 -0700 | [diff] [blame] | 412 | /** |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 413 | * Exception thrown when a Canvas couldn't be locked with {@link Surface#lockCanvas}, or |
| 414 | * when a SurfaceTexture could not successfully be allocated. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 415 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 416 | @SuppressWarnings("serial") |
| 417 | public static class OutOfResourcesException extends RuntimeException { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 418 | public OutOfResourcesException() { |
| 419 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 420 | public OutOfResourcesException(String name) { |
| 421 | super(name); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 426 | * Returns a human readable representation of a rotation. |
| 427 | * |
| 428 | * @param rotation The rotation. |
| 429 | * @return The rotation symbolic name. |
Svetoslav Ganov | 545252f | 2012-12-10 18:29:24 -0800 | [diff] [blame] | 430 | * |
| 431 | * @hide |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 432 | */ |
| 433 | public static String rotationToString(int rotation) { |
| 434 | switch (rotation) { |
| 435 | case Surface.ROTATION_0: { |
| 436 | return "ROTATION_0"; |
| 437 | } |
| 438 | case Surface.ROTATION_90: { |
| 439 | return "ROATATION_90"; |
| 440 | } |
| 441 | case Surface.ROTATION_180: { |
| 442 | return "ROATATION_180"; |
| 443 | } |
| 444 | case Surface.ROTATION_270: { |
| 445 | return "ROATATION_270"; |
| 446 | } |
| 447 | default: { |
| 448 | throw new IllegalArgumentException("Invalid rotation: " + rotation); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 454 | * A Canvas class that can handle the compatibility mode. |
| 455 | * This does two things differently. |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 456 | * <ul> |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 457 | * <li>Returns the width and height of the target metrics, rather than |
| 458 | * native. For example, the canvas returns 320x480 even if an app is running |
| 459 | * in WVGA high density. |
| 460 | * <li>Scales the matrix in setMatrix by the application scale, except if |
| 461 | * the matrix looks like obtained from getMatrix. This is a hack to handle |
| 462 | * the case that an application uses getMatrix to keep the original matrix, |
| 463 | * set matrix of its own, then set the original matrix back. There is no |
| 464 | * perfect solution that works for all cases, and there are a lot of cases |
| 465 | * 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] | 466 | * </ul> |
| 467 | */ |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 468 | private final class CompatibleCanvas extends Canvas { |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 469 | // A temp matrix to remember what an application obtained via {@link getMatrix} |
| 470 | private Matrix mOrigMatrix = null; |
| 471 | |
| 472 | @Override |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 473 | public void setMatrix(Matrix matrix) { |
| 474 | if (mCompatibleMatrix == null || mOrigMatrix == null || mOrigMatrix.equals(matrix)) { |
| 475 | // don't scale the matrix if it's not compatibility mode, or |
| 476 | // the matrix was obtained from getMatrix. |
| 477 | super.setMatrix(matrix); |
| 478 | } else { |
| 479 | Matrix m = new Matrix(mCompatibleMatrix); |
| 480 | m.preConcat(matrix); |
| 481 | super.setMatrix(m); |
| 482 | } |
| 483 | } |
| 484 | |
Romain Guy | 8b5aa48 | 2013-02-28 18:13:54 -0800 | [diff] [blame] | 485 | @SuppressWarnings("deprecation") |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 486 | @Override |
| 487 | public void getMatrix(Matrix m) { |
| 488 | super.getMatrix(m); |
| 489 | if (mOrigMatrix == null) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 490 | mOrigMatrix = new Matrix(); |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 491 | } |
| 492 | mOrigMatrix.set(m); |
| 493 | } |
Romain Guy | d10cd57 | 2010-10-10 13:33:22 -0700 | [diff] [blame] | 494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |