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