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