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; |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 22 | import android.graphics.GraphicBuffer; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 23 | import android.graphics.Matrix; |
| 24 | import android.graphics.Rect; |
Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 25 | import android.graphics.SurfaceTexture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Parcel; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 27 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 29 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 30 | import dalvik.system.CloseGuard; |
| 31 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 32 | import java.lang.annotation.Retention; |
| 33 | import java.lang.annotation.RetentionPolicy; |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | /** |
Glenn Kasten | 334031c | 2010-11-09 21:54:38 -0800 | [diff] [blame] | 36 | * Handle onto a raw buffer that is being managed by the screen compositor. |
Eino-Ville Talvala | c4faf0c | 2016-09-16 13:34:52 -0700 | [diff] [blame] | 37 | * |
| 38 | * <p>A Surface is generally created by or from a consumer of image buffers (such as a |
| 39 | * {@link android.graphics.SurfaceTexture}, {@link android.media.MediaRecorder}, or |
| 40 | * {@link android.renderscript.Allocation}), and is handed to some kind of producer (such as |
| 41 | * {@link android.opengl.EGL14#eglCreateWindowSurface(android.opengl.EGLDisplay,android.opengl.EGLConfig,java.lang.Object,int[],int) OpenGL}, |
| 42 | * {@link android.media.MediaPlayer#setSurface MediaPlayer}, or |
| 43 | * {@link android.hardware.camera2.CameraDevice#createCaptureSession CameraDevice}) to draw |
| 44 | * into.</p> |
| 45 | * |
| 46 | * <p><strong>Note:</strong> A Surface acts like a |
| 47 | * {@link java.lang.ref.WeakReference weak reference} to the consumer it is associated with. By |
| 48 | * itself it will not keep its parent consumer from being reclaimed.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | */ |
| 50 | public class Surface implements Parcelable { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 51 | private static final String TAG = "Surface"; |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 52 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 53 | private static native long nativeCreateFromSurfaceTexture(SurfaceTexture surfaceTexture) |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 54 | throws OutOfResourcesException; |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 55 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 56 | private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject); |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 57 | private static native long nativeGetFromSurfaceControl(long surfaceControlNativeObject); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 58 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 59 | private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty) |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 60 | throws OutOfResourcesException; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 61 | private static native void nativeUnlockCanvasAndPost(long nativeObject, Canvas canvas); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 62 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 63 | private static native void nativeRelease(long nativeObject); |
| 64 | private static native boolean nativeIsValid(long nativeObject); |
| 65 | private static native boolean nativeIsConsumerRunningBehind(long nativeObject); |
| 66 | private static native long nativeReadFromParcel(long nativeObject, Parcel source); |
| 67 | private static native void nativeWriteToParcel(long nativeObject, Parcel dest); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 68 | |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 69 | private static native void nativeAllocateBuffers(long nativeObject); |
| 70 | |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 71 | private static native int nativeGetWidth(long nativeObject); |
| 72 | private static native int nativeGetHeight(long nativeObject); |
| 73 | |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 74 | private static native long nativeGetNextFrameNumber(long nativeObject); |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 75 | private static native int nativeSetScalingMode(long nativeObject, int scalingMode); |
Robert Carr | 387838b | 2016-09-07 14:12:44 -0700 | [diff] [blame] | 76 | private static native int nativeForceScopedDisconnect(long nativeObject); |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 77 | private static native int nativeAttachAndQueueBuffer(long nativeObject, GraphicBuffer buffer); |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 78 | |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 79 | private static native int nativeSetSharedBufferModeEnabled(long nativeObject, boolean enabled); |
| 80 | private static native int nativeSetAutoRefreshEnabled(long nativeObject, boolean enabled); |
| 81 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 82 | public static final Parcelable.Creator<Surface> CREATOR = |
| 83 | new Parcelable.Creator<Surface>() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 84 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 85 | public Surface createFromParcel(Parcel source) { |
| 86 | try { |
| 87 | Surface s = new Surface(); |
| 88 | s.readFromParcel(source); |
| 89 | return s; |
| 90 | } catch (Exception e) { |
| 91 | Log.e(TAG, "Exception creating surface from parcel", e); |
| 92 | return null; |
| 93 | } |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 94 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 97 | public Surface[] newArray(int size) { |
| 98 | return new Surface[size]; |
| 99 | } |
| 100 | }; |
Kevin Hester | b85c933 | 2012-03-08 17:06:56 -0800 | [diff] [blame] | 101 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 102 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 103 | |
| 104 | // Guarded state. |
| 105 | final Object mLock = new Object(); // protects the native state |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 106 | private String mName; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 107 | long mNativeObject; // package scope only for SurfaceControl access |
| 108 | private long mLockedObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 109 | private int mGenerationId; // incremented each time mNativeObject changes |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 110 | private final Canvas mCanvas = new CompatibleCanvas(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 112 | // A matrix to scale the matrix set by application. This is set to null for |
| 113 | // non compatibility mode. |
| 114 | private Matrix mCompatibleMatrix; |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 115 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 116 | private HwuiContext mHwuiContext; |
| 117 | |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 118 | private boolean mIsSingleBuffered; |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 119 | private boolean mIsSharedBufferModeEnabled; |
| 120 | private boolean mIsAutoRefreshEnabled; |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 121 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 122 | /** @hide */ |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 123 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 124 | @IntDef(prefix = { "SCALING_MODE_" }, value = { |
| 125 | SCALING_MODE_FREEZE, |
| 126 | SCALING_MODE_SCALE_TO_WINDOW, |
| 127 | SCALING_MODE_SCALE_CROP, |
| 128 | SCALING_MODE_NO_SCALE_CROP |
| 129 | }) |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 130 | public @interface ScalingMode {} |
| 131 | // From system/window.h |
| 132 | /** @hide */ |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 133 | public static final int SCALING_MODE_FREEZE = 0; |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 134 | /** @hide */ |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 135 | public static final int SCALING_MODE_SCALE_TO_WINDOW = 1; |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 136 | /** @hide */ |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 137 | public static final int SCALING_MODE_SCALE_CROP = 2; |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 138 | /** @hide */ |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 139 | public static final int SCALING_MODE_NO_SCALE_CROP = 3; |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 140 | |
| 141 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 142 | @IntDef(prefix = { "ROTATION_" }, value = { |
| 143 | ROTATION_0, |
| 144 | ROTATION_90, |
| 145 | ROTATION_180, |
| 146 | ROTATION_270 |
| 147 | }) |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 148 | @Retention(RetentionPolicy.SOURCE) |
| 149 | public @interface Rotation {} |
| 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | /** |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 152 | * Rotation constant: 0 degree rotation (natural orientation) |
| 153 | */ |
| 154 | public static final int ROTATION_0 = 0; |
| 155 | |
| 156 | /** |
| 157 | * Rotation constant: 90 degree rotation. |
| 158 | */ |
| 159 | public static final int ROTATION_90 = 1; |
| 160 | |
| 161 | /** |
| 162 | * Rotation constant: 180 degree rotation. |
| 163 | */ |
| 164 | public static final int ROTATION_180 = 2; |
| 165 | |
| 166 | /** |
| 167 | * Rotation constant: 270 degree rotation. |
| 168 | */ |
| 169 | public static final int ROTATION_270 = 3; |
| 170 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 171 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 172 | * Create an empty surface, which will later be filled in by readFromParcel(). |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 173 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | */ |
| 175 | public Surface() { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Create Surface from a {@link SurfaceTexture}. |
| 180 | * |
| 181 | * Images drawn to the Surface will be made available to the {@link |
| 182 | * SurfaceTexture}, which can attach them to an OpenGL ES texture via {@link |
| 183 | * SurfaceTexture#updateTexImage}. |
| 184 | * |
| 185 | * @param surfaceTexture The {@link SurfaceTexture} that is updated by this |
| 186 | * Surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 187 | * @throws OutOfResourcesException if the surface could not be created. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 188 | */ |
| 189 | public Surface(SurfaceTexture surfaceTexture) { |
| 190 | if (surfaceTexture == null) { |
| 191 | throw new IllegalArgumentException("surfaceTexture must not be null"); |
| 192 | } |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 193 | mIsSingleBuffered = surfaceTexture.isSingleBuffered(); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 194 | synchronized (mLock) { |
| 195 | mName = surfaceTexture.toString(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 196 | setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 197 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 200 | /* called from android_view_Surface_createFromIGraphicBufferProducer() */ |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 201 | private Surface(long nativeObject) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 202 | synchronized (mLock) { |
| 203 | setNativeObjectLocked(nativeObject); |
| 204 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 207 | @Override |
| 208 | protected void finalize() throws Throwable { |
| 209 | try { |
| 210 | if (mCloseGuard != null) { |
| 211 | mCloseGuard.warnIfOpen(); |
| 212 | } |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 213 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 214 | } finally { |
| 215 | super.finalize(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Release the local reference to the server-side surface. |
| 221 | * Always call release() when you're done with a Surface. |
| 222 | * This will make the surface invalid. |
| 223 | */ |
| 224 | public void release() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 225 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 226 | if (mNativeObject != 0) { |
| 227 | nativeRelease(mNativeObject); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 228 | setNativeObjectLocked(0); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 229 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 230 | if (mHwuiContext != null) { |
| 231 | mHwuiContext.destroy(); |
| 232 | mHwuiContext = null; |
| 233 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 234 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Free all server-side state associated with this surface and |
| 239 | * release this object's reference. This method can only be |
| 240 | * called from the process that created the service. |
| 241 | * @hide |
| 242 | */ |
| 243 | public void destroy() { |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 244 | release(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns true if this object holds a valid surface. |
| 249 | * |
| 250 | * @return True if it holds a physical surface, so lockCanvas() will succeed. |
| 251 | * Otherwise returns false. |
| 252 | */ |
| 253 | public boolean isValid() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 254 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 255 | if (mNativeObject == 0) return false; |
| 256 | return nativeIsValid(mNativeObject); |
| 257 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Gets the generation number of this surface, incremented each time |
| 262 | * the native surface contained within this object changes. |
| 263 | * |
| 264 | * @return The current generation number. |
| 265 | * @hide |
| 266 | */ |
| 267 | public int getGenerationId() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 268 | synchronized (mLock) { |
| 269 | return mGenerationId; |
| 270 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 274 | * Returns the next frame number which will be dequeued for rendering. |
| 275 | * Intended for use with SurfaceFlinger's deferred transactions API. |
| 276 | * |
| 277 | * @hide |
| 278 | */ |
| 279 | public long getNextFrameNumber() { |
| 280 | synchronized (mLock) { |
| 281 | return nativeGetNextFrameNumber(mNativeObject); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 286 | * Returns true if the consumer of this Surface is running behind the producer. |
| 287 | * |
| 288 | * @return True if the consumer is more than one buffer ahead of the producer. |
| 289 | * @hide |
| 290 | */ |
| 291 | public boolean isConsumerRunningBehind() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 292 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 293 | checkNotReleasedLocked(); |
| 294 | return nativeIsConsumerRunningBehind(mNativeObject); |
| 295 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Gets a {@link Canvas} for drawing into this surface. |
| 300 | * |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 301 | * After drawing into the provided {@link Canvas}, the caller must |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 302 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 303 | * |
Mathias Agopian | 30d7ab9 | 2013-02-14 15:38:40 -0800 | [diff] [blame] | 304 | * @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] | 305 | * to redraw. This function may choose to expand the dirty rectangle if for example |
| 306 | * 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] | 307 | * not available. The caller must redraw the entire dirty region as represented |
| 308 | * by the contents of the inOutDirty rectangle upon return from this function. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 309 | * The caller may also pass <code>null</code> instead, in the case where the |
| 310 | * entire surface should be redrawn. |
| 311 | * @return A canvas for drawing into the surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 312 | * |
| 313 | * @throws IllegalArgumentException If the inOutDirty rectangle is not valid. |
| 314 | * @throws OutOfResourcesException If the canvas cannot be locked. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 315 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 316 | public Canvas lockCanvas(Rect inOutDirty) |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 317 | throws Surface.OutOfResourcesException, IllegalArgumentException { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 318 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 319 | checkNotReleasedLocked(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 320 | if (mLockedObject != 0) { |
| 321 | // Ideally, nativeLockCanvas() would throw in this situation and prevent the |
| 322 | // double-lock, but that won't happen if mNativeObject was updated. We can't |
| 323 | // abandon the old mLockedObject because it might still be in use, so instead |
| 324 | // we just refuse to re-lock the Surface. |
Jesse Hall | d7e5596 | 2014-10-13 11:01:15 -0700 | [diff] [blame] | 325 | throw new IllegalArgumentException("Surface was already locked"); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 326 | } |
| 327 | mLockedObject = nativeLockCanvas(mNativeObject, mCanvas, inOutDirty); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 328 | return mCanvas; |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 329 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Posts the new contents of the {@link Canvas} to the surface and |
| 334 | * releases the {@link Canvas}. |
| 335 | * |
| 336 | * @param canvas The canvas previously obtained from {@link #lockCanvas}. |
| 337 | */ |
| 338 | public void unlockCanvasAndPost(Canvas canvas) { |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 339 | synchronized (mLock) { |
| 340 | checkNotReleasedLocked(); |
| 341 | |
| 342 | if (mHwuiContext != null) { |
| 343 | mHwuiContext.unlockAndPost(canvas); |
| 344 | } else { |
| 345 | unlockSwCanvasAndPost(canvas); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private void unlockSwCanvasAndPost(Canvas canvas) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 351 | if (canvas != mCanvas) { |
| 352 | throw new IllegalArgumentException("canvas object must be the same instance that " |
| 353 | + "was previously returned by lockCanvas"); |
| 354 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 355 | if (mNativeObject != mLockedObject) { |
| 356 | Log.w(TAG, "WARNING: Surface's mNativeObject (0x" + |
| 357 | Long.toHexString(mNativeObject) + ") != mLockedObject (0x" + |
| 358 | Long.toHexString(mLockedObject) +")"); |
| 359 | } |
| 360 | if (mLockedObject == 0) { |
| 361 | throw new IllegalStateException("Surface was not locked"); |
| 362 | } |
| 363 | try { |
| 364 | nativeUnlockCanvasAndPost(mLockedObject, canvas); |
| 365 | } finally { |
| 366 | nativeRelease(mLockedObject); |
| 367 | mLockedObject = 0; |
| 368 | } |
| 369 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 370 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 371 | /** |
| 372 | * Gets a {@link Canvas} for drawing into this surface. |
| 373 | * |
| 374 | * After drawing into the provided {@link Canvas}, the caller must |
| 375 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 376 | * |
| 377 | * Unlike {@link #lockCanvas(Rect)} this will return a hardware-accelerated |
| 378 | * canvas. See the <a href="{@docRoot}guide/topics/graphics/hardware-accel.html#unsupported"> |
| 379 | * 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] | 380 | * supported in a hardware-accelerated canvas. It is also required to |
| 381 | * fully cover the surface every time {@link #lockHardwareCanvas()} is |
| 382 | * called as the buffer is not preserved between frames. Partial updates |
| 383 | * are not supported. |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 384 | * |
| 385 | * @return A canvas for drawing into the surface. |
| 386 | * |
| 387 | * @throws IllegalStateException If the canvas cannot be locked. |
| 388 | */ |
| 389 | public Canvas lockHardwareCanvas() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 390 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 391 | checkNotReleasedLocked(); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 392 | if (mHwuiContext == null) { |
| 393 | mHwuiContext = new HwuiContext(); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 394 | } |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 395 | return mHwuiContext.lockCanvas( |
| 396 | nativeGetWidth(mNativeObject), |
| 397 | nativeGetHeight(mNativeObject)); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 398 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 401 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 402 | * @deprecated This API has been removed and is not supported. Do not use. |
| 403 | */ |
| 404 | @Deprecated |
| 405 | public void unlockCanvas(Canvas canvas) { |
| 406 | throw new UnsupportedOperationException(); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Sets the translator used to scale canvas's width/height in compatibility |
| 411 | * mode. |
| 412 | */ |
| 413 | void setCompatibilityTranslator(Translator translator) { |
| 414 | if (translator != null) { |
| 415 | float appScale = translator.applicationScale; |
| 416 | mCompatibleMatrix = new Matrix(); |
| 417 | mCompatibleMatrix.setScale(appScale, appScale); |
| 418 | } |
| 419 | } |
| 420 | |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 421 | /** |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 422 | * Copy another surface to this one. This surface now holds a reference |
| 423 | * 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] | 424 | * This is for use by the window manager when returning a window surface |
| 425 | * back from a client, converting it from the representation being managed |
| 426 | * by the window manager to the representation the client uses to draw |
| 427 | * in to it. |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 428 | * |
| 429 | * @param other {@link SurfaceControl} to copy from. |
| 430 | * |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 431 | * @hide |
| 432 | */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 433 | public void copyFrom(SurfaceControl other) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 434 | if (other == null) { |
| 435 | throw new IllegalArgumentException("other must not be null"); |
| 436 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 437 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 438 | long surfaceControlPtr = other.mNativeObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 439 | if (surfaceControlPtr == 0) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 440 | throw new NullPointerException( |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 441 | "null SurfaceControl native object. Are you using a released SurfaceControl?"); |
| 442 | } |
| 443 | long newNativeObject = nativeGetFromSurfaceControl(surfaceControlPtr); |
| 444 | |
| 445 | synchronized (mLock) { |
| 446 | if (mNativeObject != 0) { |
| 447 | nativeRelease(mNativeObject); |
| 448 | } |
| 449 | setNativeObjectLocked(newNativeObject); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Gets a reference a surface created from this one. This surface now holds a reference |
| 455 | * to the same data as the original surface, and is -not- the owner. |
| 456 | * This is for use by the window manager when returning a window surface |
| 457 | * back from a client, converting it from the representation being managed |
| 458 | * by the window manager to the representation the client uses to draw |
| 459 | * in to it. |
| 460 | * |
| 461 | * @param other {@link SurfaceControl} to create surface from. |
| 462 | * |
| 463 | * @hide |
| 464 | */ |
| 465 | public void createFrom(SurfaceControl other) { |
| 466 | if (other == null) { |
| 467 | throw new IllegalArgumentException("other must not be null"); |
| 468 | } |
| 469 | |
| 470 | long surfaceControlPtr = other.mNativeObject; |
| 471 | if (surfaceControlPtr == 0) { |
| 472 | throw new NullPointerException( |
| 473 | "null SurfaceControl native object. Are you using a released SurfaceControl?"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 474 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 475 | long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 476 | |
| 477 | synchronized (mLock) { |
| 478 | if (mNativeObject != 0) { |
| 479 | nativeRelease(mNativeObject); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 480 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 481 | setNativeObjectLocked(newNativeObject); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 482 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 483 | } |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 484 | |
| 485 | /** |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 486 | * This is intended to be used by {@link SurfaceView#updateWindow} only. |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 487 | * @param other access is not thread safe |
| 488 | * @hide |
| 489 | * @deprecated |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 490 | */ |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 491 | @Deprecated |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 492 | public void transferFrom(Surface other) { |
| 493 | if (other == null) { |
| 494 | throw new IllegalArgumentException("other must not be null"); |
| 495 | } |
| 496 | if (other != this) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 497 | final long newPtr; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 498 | synchronized (other.mLock) { |
| 499 | newPtr = other.mNativeObject; |
| 500 | other.setNativeObjectLocked(0); |
| 501 | } |
| 502 | |
| 503 | synchronized (mLock) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 504 | if (mNativeObject != 0) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 505 | nativeRelease(mNativeObject); |
| 506 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 507 | setNativeObjectLocked(newPtr); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 508 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 509 | } |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 512 | @Override |
| 513 | public int describeContents() { |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | public void readFromParcel(Parcel source) { |
| 518 | if (source == null) { |
| 519 | throw new IllegalArgumentException("source must not be null"); |
| 520 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 521 | |
| 522 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 523 | // nativeReadFromParcel() will either return mNativeObject, or |
| 524 | // create a new native Surface and return it after reducing |
| 525 | // the reference count on mNativeObject. Either way, it is |
| 526 | // not necessary to call nativeRelease() here. |
Eino-Ville Talvala | f005f5e | 2016-08-29 17:51:23 -0700 | [diff] [blame] | 527 | // NOTE: This must be kept synchronized with the native parceling code |
| 528 | // in frameworks/native/libs/Surface.cpp |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 529 | mName = source.readString(); |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 530 | mIsSingleBuffered = source.readInt() != 0; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 531 | setNativeObjectLocked(nativeReadFromParcel(mNativeObject, source)); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 532 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void writeToParcel(Parcel dest, int flags) { |
| 537 | if (dest == null) { |
| 538 | throw new IllegalArgumentException("dest must not be null"); |
| 539 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 540 | synchronized (mLock) { |
Eino-Ville Talvala | f005f5e | 2016-08-29 17:51:23 -0700 | [diff] [blame] | 541 | // NOTE: This must be kept synchronized with the native parceling code |
| 542 | // in frameworks/native/libs/Surface.cpp |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 543 | dest.writeString(mName); |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 544 | dest.writeInt(mIsSingleBuffered ? 1 : 0); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 545 | nativeWriteToParcel(mNativeObject, dest); |
| 546 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 547 | if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { |
| 548 | release(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public String toString() { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 554 | synchronized (mLock) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 555 | return "Surface(name=" + mName + ")/@0x" + |
| 556 | Integer.toHexString(System.identityHashCode(this)); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 560 | private void setNativeObjectLocked(long ptr) { |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 561 | if (mNativeObject != ptr) { |
| 562 | if (mNativeObject == 0 && ptr != 0) { |
| 563 | mCloseGuard.open("release"); |
| 564 | } else if (mNativeObject != 0 && ptr == 0) { |
| 565 | mCloseGuard.close(); |
| 566 | } |
| 567 | mNativeObject = ptr; |
| 568 | mGenerationId += 1; |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 569 | if (mHwuiContext != null) { |
| 570 | mHwuiContext.updateSurface(); |
| 571 | } |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | private void checkNotReleasedLocked() { |
| 576 | if (mNativeObject == 0) { |
| 577 | throw new IllegalStateException("Surface has already been released."); |
| 578 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Mathias Agopian | c14bacf | 2012-04-23 18:23:08 -0700 | [diff] [blame] | 581 | /** |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 582 | * Allocate buffers ahead of time to avoid allocation delays during rendering |
| 583 | * @hide |
| 584 | */ |
| 585 | public void allocateBuffers() { |
| 586 | synchronized (mLock) { |
| 587 | checkNotReleasedLocked(); |
| 588 | nativeAllocateBuffers(mNativeObject); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 593 | * Set the scaling mode to be used for this surfaces buffers |
| 594 | * @hide |
| 595 | */ |
| 596 | void setScalingMode(@ScalingMode int scalingMode) { |
| 597 | synchronized (mLock) { |
| 598 | checkNotReleasedLocked(); |
| 599 | int err = nativeSetScalingMode(mNativeObject, scalingMode); |
| 600 | if (err != 0) { |
| 601 | throw new IllegalArgumentException("Invalid scaling mode: " + scalingMode); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Robert Carr | 387838b | 2016-09-07 14:12:44 -0700 | [diff] [blame] | 606 | void forceScopedDisconnect() { |
| 607 | synchronized (mLock) { |
| 608 | checkNotReleasedLocked(); |
| 609 | int err = nativeForceScopedDisconnect(mNativeObject); |
| 610 | if (err != 0) { |
| 611 | throw new RuntimeException("Failed to disconnect Surface instance (bad object?)"); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 616 | /** |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 617 | * Transfer ownership of buffer and present it on the Surface. |
| 618 | * @hide |
| 619 | */ |
| 620 | public void attachAndQueueBuffer(GraphicBuffer buffer) { |
| 621 | synchronized (mLock) { |
| 622 | checkNotReleasedLocked(); |
| 623 | int err = nativeAttachAndQueueBuffer(mNativeObject, buffer); |
| 624 | if (err != 0) { |
| 625 | throw new RuntimeException( |
| 626 | "Failed to attach and queue buffer to Surface (bad object?)"); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /** |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 632 | * Returns whether or not this Surface is backed by a single-buffered SurfaceTexture |
| 633 | * @hide |
| 634 | */ |
| 635 | public boolean isSingleBuffered() { |
| 636 | return mIsSingleBuffered; |
| 637 | } |
| 638 | |
| 639 | /** |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 640 | * <p>The shared buffer mode allows both the application and the surface compositor |
| 641 | * (SurfaceFlinger) to concurrently access this surface's buffer. While the |
| 642 | * application is still required to issue a present request |
| 643 | * (see {@link #unlockCanvasAndPost(Canvas)}) to the compositor when an update is required, |
| 644 | * the compositor may trigger an update at any time. Since the surface's buffer is shared |
| 645 | * between the application and the compositor, updates triggered by the compositor may |
| 646 | * cause visible tearing.</p> |
| 647 | * |
| 648 | * <p>The shared buffer mode can be used with |
| 649 | * {@link #setAutoRefreshEnabled(boolean) auto-refresh} to avoid the overhead of |
| 650 | * issuing present requests.</p> |
| 651 | * |
| 652 | * <p>If the application uses the shared buffer mode to reduce latency, it is |
| 653 | * recommended to use software rendering (see {@link #lockCanvas(Rect)} to ensure |
| 654 | * the graphics workloads are not affected by other applications and/or the system |
| 655 | * using the GPU. When using software rendering, the application should update the |
| 656 | * smallest possible region of the surface required.</p> |
| 657 | * |
| 658 | * <p class="note">The shared buffer mode might not be supported by the underlying |
| 659 | * hardware. Enabling shared buffer mode on hardware that does not support it will |
| 660 | * not yield an error but the application will not benefit from lower latency (and |
| 661 | * tearing will not be visible).</p> |
| 662 | * |
| 663 | * <p class="note">Depending on how many and what kind of surfaces are visible, the |
| 664 | * surface compositor may need to copy the shared buffer before it is displayed. When |
| 665 | * this happens, the latency benefits of shared buffer mode will be reduced.</p> |
| 666 | * |
| 667 | * @param enabled True to enable the shared buffer mode on this surface, false otherwise |
| 668 | * |
| 669 | * @see #isSharedBufferModeEnabled() |
| 670 | * @see #setAutoRefreshEnabled(boolean) |
Romain Guy | 010f082 | 2017-08-16 09:43:07 -0700 | [diff] [blame] | 671 | * |
| 672 | * @hide |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 673 | */ |
| 674 | public void setSharedBufferModeEnabled(boolean enabled) { |
| 675 | if (mIsSharedBufferModeEnabled != enabled) { |
| 676 | int error = nativeSetSharedBufferModeEnabled(mNativeObject, enabled); |
| 677 | if (error != 0) { |
| 678 | throw new RuntimeException( |
| 679 | "Failed to set shared buffer mode on Surface (bad object?)"); |
| 680 | } else { |
| 681 | mIsSharedBufferModeEnabled = enabled; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * @return True if shared buffer mode is enabled on this surface, false otherwise |
| 688 | * |
| 689 | * @see #setSharedBufferModeEnabled(boolean) |
Romain Guy | 010f082 | 2017-08-16 09:43:07 -0700 | [diff] [blame] | 690 | * |
| 691 | * @hide |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 692 | */ |
| 693 | public boolean isSharedBufferModeEnabled() { |
| 694 | return mIsSharedBufferModeEnabled; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * <p>When auto-refresh is enabled, the surface compositor (SurfaceFlinger) |
| 699 | * automatically updates the display on a regular refresh cycle. The application |
| 700 | * can continue to issue present requests but it is not required. Enabling |
| 701 | * auto-refresh may result in visible tearing.</p> |
| 702 | * |
| 703 | * <p>Auto-refresh has no effect if the {@link #setSharedBufferModeEnabled(boolean) |
| 704 | * shared buffer mode} is not enabled.</p> |
| 705 | * |
| 706 | * <p>Because auto-refresh will trigger continuous updates of the display, it is |
| 707 | * recommended to turn it on only when necessary. For example, in a drawing/painting |
| 708 | * application auto-refresh should be enabled on finger/pen down and disabled on |
| 709 | * finger/pen up.</p> |
| 710 | * |
| 711 | * @param enabled True to enable auto-refresh on this surface, false otherwise |
| 712 | * |
| 713 | * @see #isAutoRefreshEnabled() |
| 714 | * @see #setSharedBufferModeEnabled(boolean) |
Romain Guy | 010f082 | 2017-08-16 09:43:07 -0700 | [diff] [blame] | 715 | * |
| 716 | * @hide |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 717 | */ |
| 718 | public void setAutoRefreshEnabled(boolean enabled) { |
| 719 | if (mIsAutoRefreshEnabled != enabled) { |
| 720 | int error = nativeSetAutoRefreshEnabled(mNativeObject, enabled); |
| 721 | if (error != 0) { |
| 722 | throw new RuntimeException("Failed to set auto refresh on Surface (bad object?)"); |
| 723 | } else { |
| 724 | mIsAutoRefreshEnabled = enabled; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * @return True if auto-refresh is enabled on this surface, false otherwise |
Romain Guy | 010f082 | 2017-08-16 09:43:07 -0700 | [diff] [blame] | 731 | * |
| 732 | * @hide |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 733 | */ |
| 734 | public boolean isAutoRefreshEnabled() { |
| 735 | return mIsAutoRefreshEnabled; |
| 736 | } |
| 737 | |
| 738 | /** |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 739 | * Exception thrown when a Canvas couldn't be locked with {@link Surface#lockCanvas}, or |
| 740 | * when a SurfaceTexture could not successfully be allocated. |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 741 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 742 | @SuppressWarnings("serial") |
| 743 | public static class OutOfResourcesException extends RuntimeException { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 744 | public OutOfResourcesException() { |
| 745 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 746 | public OutOfResourcesException(String name) { |
| 747 | super(name); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /** |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 752 | * Returns a human readable representation of a rotation. |
| 753 | * |
| 754 | * @param rotation The rotation. |
| 755 | * @return The rotation symbolic name. |
Svetoslav Ganov | 545252f | 2012-12-10 18:29:24 -0800 | [diff] [blame] | 756 | * |
| 757 | * @hide |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 758 | */ |
| 759 | public static String rotationToString(int rotation) { |
| 760 | switch (rotation) { |
| 761 | case Surface.ROTATION_0: { |
| 762 | return "ROTATION_0"; |
| 763 | } |
| 764 | case Surface.ROTATION_90: { |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 765 | return "ROTATION_90"; |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 766 | } |
| 767 | case Surface.ROTATION_180: { |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 768 | return "ROTATION_180"; |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 769 | } |
| 770 | case Surface.ROTATION_270: { |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 771 | return "ROTATION_270"; |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 772 | } |
| 773 | default: { |
Michael Wright | 1985976 | 2017-09-18 20:57:58 +0100 | [diff] [blame] | 774 | return Integer.toString(rotation); |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /** |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 780 | * A Canvas class that can handle the compatibility mode. |
| 781 | * This does two things differently. |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 782 | * <ul> |
Mathias Agopian | b923066 | 2011-08-03 14:44:48 -0700 | [diff] [blame] | 783 | * <li>Returns the width and height of the target metrics, rather than |
| 784 | * native. For example, the canvas returns 320x480 even if an app is running |
| 785 | * in WVGA high density. |
| 786 | * <li>Scales the matrix in setMatrix by the application scale, except if |
| 787 | * the matrix looks like obtained from getMatrix. This is a hack to handle |
| 788 | * the case that an application uses getMatrix to keep the original matrix, |
| 789 | * set matrix of its own, then set the original matrix back. There is no |
| 790 | * perfect solution that works for all cases, and there are a lot of cases |
| 791 | * 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] | 792 | * </ul> |
| 793 | */ |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 794 | private final class CompatibleCanvas extends Canvas { |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 795 | // A temp matrix to remember what an application obtained via {@link getMatrix} |
| 796 | private Matrix mOrigMatrix = null; |
| 797 | |
| 798 | @Override |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 799 | public void setMatrix(Matrix matrix) { |
| 800 | if (mCompatibleMatrix == null || mOrigMatrix == null || mOrigMatrix.equals(matrix)) { |
| 801 | // don't scale the matrix if it's not compatibility mode, or |
| 802 | // the matrix was obtained from getMatrix. |
| 803 | super.setMatrix(matrix); |
| 804 | } else { |
| 805 | Matrix m = new Matrix(mCompatibleMatrix); |
| 806 | m.preConcat(matrix); |
| 807 | super.setMatrix(m); |
| 808 | } |
| 809 | } |
| 810 | |
Romain Guy | 8b5aa48 | 2013-02-28 18:13:54 -0800 | [diff] [blame] | 811 | @SuppressWarnings("deprecation") |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 812 | @Override |
| 813 | public void getMatrix(Matrix m) { |
| 814 | super.getMatrix(m); |
| 815 | if (mOrigMatrix == null) { |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 816 | mOrigMatrix = new Matrix(); |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 817 | } |
| 818 | mOrigMatrix.set(m); |
| 819 | } |
Romain Guy | d10cd57 | 2010-10-10 13:33:22 -0700 | [diff] [blame] | 820 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 821 | |
| 822 | private final class HwuiContext { |
| 823 | private final RenderNode mRenderNode; |
| 824 | private long mHwuiRenderer; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 825 | private DisplayListCanvas mCanvas; |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 826 | |
| 827 | HwuiContext() { |
| 828 | mRenderNode = RenderNode.create("HwuiCanvas", null); |
| 829 | mRenderNode.setClipToBounds(false); |
| 830 | mHwuiRenderer = nHwuiCreate(mRenderNode.mNativeRenderNode, mNativeObject); |
| 831 | } |
| 832 | |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 833 | Canvas lockCanvas(int width, int height) { |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 834 | if (mCanvas != null) { |
| 835 | throw new IllegalStateException("Surface was already locked!"); |
| 836 | } |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 837 | mCanvas = mRenderNode.start(width, height); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 838 | return mCanvas; |
| 839 | } |
| 840 | |
| 841 | void unlockAndPost(Canvas canvas) { |
| 842 | if (canvas != mCanvas) { |
| 843 | throw new IllegalArgumentException("canvas object must be the same instance that " |
| 844 | + "was previously returned by lockCanvas"); |
| 845 | } |
| 846 | mRenderNode.end(mCanvas); |
| 847 | mCanvas = null; |
| 848 | nHwuiDraw(mHwuiRenderer); |
| 849 | } |
| 850 | |
| 851 | void updateSurface() { |
| 852 | nHwuiSetSurface(mHwuiRenderer, mNativeObject); |
| 853 | } |
| 854 | |
| 855 | void destroy() { |
| 856 | if (mHwuiRenderer != 0) { |
| 857 | nHwuiDestroy(mHwuiRenderer); |
| 858 | mHwuiRenderer = 0; |
| 859 | } |
| 860 | } |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | private static native long nHwuiCreate(long rootNode, long surface); |
| 864 | private static native void nHwuiSetSurface(long renderer, long surface); |
| 865 | private static native void nHwuiDraw(long renderer); |
| 866 | private static native void nHwuiDestroy(long renderer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |