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