blob: 2313b13befadad32f2f2f91a9acc20f892b2eeac [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 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
17package android.view;
18
Jorim Jaggi21c39a72017-10-20 15:47:51 +020019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Vishnu Nair04ab4392018-01-10 11:00:06 -080025import static android.view.Surface.ROTATION_270;
26import static android.view.Surface.ROTATION_90;
27import static android.view.SurfaceControlProto.HASH_CODE;
28import static android.view.SurfaceControlProto.NAME;
chaviwa69e0a72017-11-29 17:55:12 -080029
Robert Carr76907ee2019-01-11 13:38:19 -080030import android.annotation.FloatRange;
31import android.annotation.IntRange;
32import android.annotation.NonNull;
33import android.annotation.Nullable;
chaviw0dd03f52017-08-25 12:15:26 -070034import android.annotation.Size;
Mathew Inwooda570dee2018-08-17 14:56:00 +010035import android.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080036import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080037import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080038import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080040import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010041import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042import android.graphics.Rect;
43import android.graphics.Region;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070044import android.hardware.display.DisplayedContentSample;
45import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000046import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080047import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010048import android.os.Parcel;
49import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010050import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080051import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080052import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080053import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070054import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010055
56import com.android.internal.annotations.GuardedBy;
57
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070058import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080059
Robert Carre625fcf2017-09-01 12:36:28 -070060import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070061
Robert Carre13b58e2017-08-31 14:50:44 -070062import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080063import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080064import java.nio.ByteOrder;
Dan Gittik832b4972019-02-13 18:17:47 +000065import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070066
Mathias Agopian3866f0d2013-02-11 22:08:48 -080067/**
Robert Carr76907ee2019-01-11 13:38:19 -080068 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
69 * a combination of a buffer source, and metadata about how to display the buffers.
70 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
71 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
72 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
73 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
74 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
75 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080076 */
Robert Carr76907ee2019-01-11 13:38:19 -080077public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080079
Ashok Bhat36bef0b2014-01-20 20:08:01 +000080 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080081 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080082 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010083 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080084 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010085 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000086 private static native void nativeRelease(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080087 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080088
Peiyong Line3e5efd2019-03-21 20:59:47 +000089 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080090 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
91 boolean captureSecureLayers);
Peiyong Lin21e499a2019-04-03 16:37:46 -070092 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken,
93 IBinder layerHandleToken, Rect sourceCrop, float frameScale, IBinder[] excludeLayers);
Mathias Agopian29479eb2013-02-14 14:36:04 -080094
Robert Carre13b58e2017-08-31 14:50:44 -070095 private static native long nativeCreateTransaction();
96 private static native long nativeGetNativeTransactionFinalizer();
97 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -070098 private static native void nativeMergeTransaction(long transactionObj,
99 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700100 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100101 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800102
Robert Carre13b58e2017-08-31 14:50:44 -0700103 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
104 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
105 IBinder relativeTo, int zorder);
106 private static native void nativeSetPosition(long transactionObj, long nativeObject,
107 float x, float y);
108 private static native void nativeSetGeometryAppliesWithResize(long transactionObj,
109 long nativeObject);
110 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
111 private static native void nativeSetTransparentRegionHint(long transactionObj,
112 long nativeObject, Region region);
113 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
114 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
115 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700116 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700117 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
118 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800119 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
120 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800121 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
122 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700123 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
124 private static native void nativeSetFlags(long transactionObj, long nativeObject,
125 int flags, int mask);
126 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
127 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700128 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
129 float cornerRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700130 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
131 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800132
Svetoslav1376d602014-03-13 11:17:26 -0700133 private static native boolean nativeClearContentFrameStats(long nativeObject);
134 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
135 private static native boolean nativeClearAnimationFrameStats();
136 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
137
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800138 private static native long[] nativeGetPhysicalDisplayIds();
139 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800140 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700141 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700142 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000143 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700144 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800145 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700146 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800147 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700148 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800149 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700150 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
151 int width, int height);
Dan Stoza00101052014-05-02 15:23:40 -0700152 private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs(
153 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700154 private static native DisplayedContentSamplingAttributes
155 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
156 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
157 boolean enable, int componentMask, int maxFrames);
158 private static native DisplayedContentSample nativeGetDisplayedContentSample(
159 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700160 private static native int nativeGetActiveConfig(IBinder displayToken);
161 private static native boolean nativeSetActiveConfig(IBinder displayToken, int id);
Ady Abraham6070ce12019-01-24 18:48:58 -0800162 private static native boolean nativeSetAllowedDisplayConfigs(IBinder displayToken,
163 int[] allowedConfigs);
Ady Abraham42f9a2fb2019-02-26 14:13:39 -0800164 private static native int[] nativeGetAllowedDisplayConfigs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700165 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800166 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
167 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800168 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700169 private static native int nativeGetActiveColorMode(IBinder displayToken);
170 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
171 int colorMode);
Prashant Malanic55929a2014-05-25 01:59:21 -0700172 private static native void nativeSetDisplayPowerMode(
173 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700174 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000175 IBinder handle, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700176 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
177 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800178 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700179 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800180 IBinder handle);
Robert Carre13b58e2017-08-31 14:50:44 -0700181 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800182 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700183 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
184 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700185 int scalingMode);
Rob Carr64e516f2015-10-29 00:20:45 +0000186 private static native IBinder nativeGetHandle(long nativeObject);
Robert Carr6da3cc02016-06-16 15:17:07 -0700187 private static native boolean nativeGetTransformToDisplayInverse(long nativeObject);
188
Michael Wright9ff94c02016-03-30 18:05:40 -0700189 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800190
Robert Carr788f5742018-07-30 17:46:45 -0700191 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
192 InputWindowHandle handle);
chaviw59f532e2018-12-26 15:34:59 -0800193 private static native void nativeTransferTouchFocus(long transactionObj, IBinder fromToken,
194 IBinder toToken);
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800195 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700196 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
197 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800198 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000199 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
200 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
201 float brightness);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800202
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800203 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800204 private String mName;
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000205 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800206
Jorim Jaggia5e10572017-11-15 14:36:26 +0100207 // TODO: Move this to native.
208 private final Object mSizeLock = new Object();
209 @GuardedBy("mSizeLock")
210 private int mWidth;
211 @GuardedBy("mSizeLock")
212 private int mHeight;
213
Robert Carre13b58e2017-08-31 14:50:44 -0700214 static Transaction sGlobalTransaction;
215 static long sTransactionNestCount = 0;
216
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800217 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
218
219 /**
220 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800221 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800222 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100223 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800224 public static final int HIDDEN = 0x00000004;
225
226 /**
227 * Surface creation flag: The surface contains secure content, special
228 * measures will be taken to disallow the surface's content to be copied
229 * from another process. In particular, screenshots and VNC servers will
230 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700231 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800232 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800233 */
234 public static final int SECURE = 0x00000080;
235
236 /**
237 * Surface creation flag: Creates a surface where color components are interpreted
238 * as "non pre-multiplied" by their alpha channel. Of course this flag is
239 * meaningless for surfaces without an alpha channel. By default
240 * surfaces are pre-multiplied, which means that each color component is
241 * already multiplied by its alpha value. In this case the blending
242 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800243 * <p>
244 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
245 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800246 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800247 * <p>
248 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
249 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800250 * pre-multiplied surfaces must always be used if transparent pixels are
251 * composited on top of each-other into the surface. A pre-multiplied
252 * surface can never lower the value of the alpha component of a given
253 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800254 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800255 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800256 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800257 */
258 public static final int NON_PREMULTIPLIED = 0x00000100;
259
260 /**
261 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700262 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800263 * application needs full RGBA 8888 support for instance but will
264 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800265 * <p>
266 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
267 * Combined effects are (assuming a buffer format with an alpha channel):
268 * <ul>
269 * <li>OPAQUE + alpha(1.0) == opaque composition
270 * <li>OPAQUE + alpha(0.x) == blended composition
271 * <li>!OPAQUE + alpha(1.0) == blended composition
272 * <li>!OPAQUE + alpha(0.x) == blended composition
273 * </ul>
274 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
275 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800276 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800277 */
278 public static final int OPAQUE = 0x00000400;
279
280 /**
281 * Surface creation flag: Application requires a hardware-protected path to an
282 * external display sink. If a hardware-protected path is not available,
283 * then this surface will not be displayed on the external sink.
284 *
Robert Carra7827f72019-01-11 12:53:37 -0800285 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800286 */
287 public static final int PROTECTED_APP = 0x00000800;
288
289 // 0x1000 is reserved for an independent DRM protected flag in framework
290
291 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700292 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800293 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700294 */
295 public static final int CURSOR_WINDOW = 0x00002000;
296
297 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800298 * Surface creation flag: Creates a normal surface.
299 * This is the default.
300 *
Robert Carra7827f72019-01-11 12:53:37 -0800301 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800302 */
303 public static final int FX_SURFACE_NORMAL = 0x00000000;
304
305 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800306 * Surface creation flag: Creates a Dim surface.
307 * Everything behind this surface is dimmed by the amount specified
308 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
309 * doesn't have a backing store.
310 *
Robert Carra7827f72019-01-11 12:53:37 -0800311 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800312 */
313 public static final int FX_SURFACE_DIM = 0x00020000;
314
315 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700316 * Surface creation flag: Creates a container surface.
317 * This surface will have no buffers and will only be used
318 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800319 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700320 */
321 public static final int FX_SURFACE_CONTAINER = 0x00080000;
322
323 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800324 * Mask used for FX values above.
325 *
Robert Carra7827f72019-01-11 12:53:37 -0800326 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800327 */
328 public static final int FX_SURFACE_MASK = 0x000F0000;
329
330 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
331
332 /**
333 * Surface flag: Hide the surface.
334 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800335 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800336 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800337 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800338
Andy McFadden314405b2014-01-29 17:18:05 -0800339 /**
340 * Surface flag: composite without blending when possible.
341 * Updates the value set during Surface creation (see {@link #OPAQUE}).
342 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800343 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800344
Robert Carr76907ee2019-01-11 13:38:19 -0800345 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700346 /**
347 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700348 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800349 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700350 */
351 public static final int POWER_MODE_OFF = 0;
352
353 /**
354 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700355 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800356 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700357 */
358 public static final int POWER_MODE_DOZE = 1;
359
360 /**
361 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700362 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800363 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700364 */
365 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800366
Jeff Brown5dc21912014-07-17 18:50:18 -0700367 /**
368 * Display power mode doze: used while putting the screen into a suspended
369 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800370 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700371 */
372 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800373
374 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700375 * Display power mode on: used while putting the screen into a suspended
376 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800377 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700378 */
379 public static final int POWER_MODE_ON_SUSPEND = 4;
380
381 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700382 * A value for windowType used to indicate that the window should be omitted from screenshots
383 * and display mirroring. A temporary workaround until we express such things with
384 * the hierarchy.
385 * TODO: b/64227542
386 * @hide
387 */
388 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
389
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800390 /**
391 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
392 */
393 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
394 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
395 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
396
Robert Carreb344c72019-01-07 18:35:30 -0800397 private void assignNativeObject(long nativeObject) {
398 if (mNativeObject != 0) {
399 release();
400 }
401 mNativeObject = nativeObject;
402 }
403
Robert Carra7827f72019-01-11 12:53:37 -0800404 /**
405 * @hide
406 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800407 public void copyFrom(SurfaceControl other) {
408 mName = other.mName;
409 mWidth = other.mWidth;
410 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800411 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800412 }
413
Robert Carr132c9f52017-07-31 17:02:30 -0700414 /**
Evan Rosky485df202018-12-06 14:11:12 -0800415 * owner UID.
416 * @hide
417 */
418 public static final int METADATA_OWNER_UID = 1;
419
420 /**
421 * Window type as per {@link WindowManager.LayoutParams}.
422 * @hide
423 */
424 public static final int METADATA_WINDOW_TYPE = 2;
425
426 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800427 * Task id to allow association between surfaces and task.
428 * @hide
429 */
430 public static final int METADATA_TASK_ID = 3;
431
432 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000433 * A wrapper around GraphicBuffer that contains extra information about how to
434 * interpret the screenshot GraphicBuffer.
435 * @hide
436 */
437 public static class ScreenshotGraphicBuffer {
438 private final GraphicBuffer mGraphicBuffer;
439 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700440 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000441
Robert Carr66b5664f2019-04-02 14:18:56 -0700442 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
443 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000444 mGraphicBuffer = graphicBuffer;
445 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700446 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000447 }
448
449 /**
450 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
451 * @param width The width in pixels of the buffer
452 * @param height The height in pixels of the buffer
453 * @param format The format of each pixel as specified in {@link PixelFormat}
454 * @param usage Hint indicating how the buffer will be used
455 * @param unwrappedNativeObject The native object of GraphicBuffer
456 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700457 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
458 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000459 */
460 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700461 int usage, long unwrappedNativeObject, int namedColorSpace,
462 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000463 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
464 usage, unwrappedNativeObject);
465 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700466 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000467 }
468
469 public ColorSpace getColorSpace() {
470 return mColorSpace;
471 }
472
473 public GraphicBuffer getGraphicBuffer() {
474 return mGraphicBuffer;
475 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700476
477 public boolean containsSecureLayers() {
478 return mContainsSecureLayers;
479 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000480 }
481
482 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700483 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700484 *
485 * By default the surface will be hidden, and have "unset" bounds, meaning it can
486 * be as large as the bounds of its parent if a buffer or child so requires.
487 *
488 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700489 */
490 public static class Builder {
491 private SurfaceSession mSession;
492 private int mFlags = HIDDEN;
493 private int mWidth;
494 private int mHeight;
495 private int mFormat = PixelFormat.OPAQUE;
496 private String mName;
497 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800498 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700499
500 /**
501 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
502 *
503 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800504 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700505 */
506 public Builder(SurfaceSession session) {
507 mSession = session;
508 }
509
510 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800511 * Begin building a SurfaceControl.
512 */
513 public Builder() {
514 }
515
516 /**
517 * Construct a new {@link SurfaceControl} with the set parameters. The builder
518 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700519 */
Robert Carrda1d2422019-03-07 15:54:37 -0800520 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700521 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800522 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700523 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800524 "width and height must be positive or unset");
525 }
526 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700527 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800528 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700529 }
Evan Rosky485df202018-12-06 14:11:12 -0800530 return new SurfaceControl(
531 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700532 }
533
534 /**
535 * Set a debugging-name for the SurfaceControl.
536 *
537 * @param name A name to identify the Surface in debugging.
538 */
Robert Carrda1d2422019-03-07 15:54:37 -0800539 @NonNull
540 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700541 mName = name;
542 return this;
543 }
544
545 /**
546 * Set the initial size of the controlled surface's buffers in pixels.
547 *
548 * @param width The buffer width in pixels.
549 * @param height The buffer height in pixels.
550 */
Robert Carrda1d2422019-03-07 15:54:37 -0800551 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800552 public Builder setBufferSize(@IntRange(from = 0) int width,
553 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800554 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700555 throw new IllegalArgumentException(
556 "width and height must be positive");
557 }
558 mWidth = width;
559 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000560 // set this as a buffer layer since we are specifying a buffer size.
561 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
562 }
563
564 /**
565 * Set the initial size of the controlled surface's buffers in pixels.
566 */
567 private void unsetBufferSize() {
568 mWidth = 0;
569 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700570 }
571
572 /**
573 * Set the pixel format of the controlled surface's buffers, using constants from
574 * {@link android.graphics.PixelFormat}.
575 */
Robert Carr76907ee2019-01-11 13:38:19 -0800576 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700577 public Builder setFormat(@PixelFormat.Format int format) {
578 mFormat = format;
579 return this;
580 }
581
582 /**
583 * Specify if the app requires a hardware-protected path to
584 * an external display sync. If protected content is enabled, but
585 * such a path is not available, then the controlled Surface will
586 * not be displayed.
587 *
588 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800589 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700590 */
Robert Carr76907ee2019-01-11 13:38:19 -0800591 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700592 public Builder setProtected(boolean protectedContent) {
593 if (protectedContent) {
594 mFlags |= PROTECTED_APP;
595 } else {
596 mFlags &= ~PROTECTED_APP;
597 }
598 return this;
599 }
600
601 /**
602 * Specify whether the Surface contains secure content. If true, the system
603 * will prevent the surfaces content from being copied by another process. In
604 * particular screenshots and VNC servers will be disabled. This is however
605 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800606 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700607 */
Robert Carr76907ee2019-01-11 13:38:19 -0800608 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700609 public Builder setSecure(boolean secure) {
610 if (secure) {
611 mFlags |= SECURE;
612 } else {
613 mFlags &= ~SECURE;
614 }
615 return this;
616 }
617
618 /**
619 * Indicates whether the surface must be considered opaque,
620 * even if its pixel format is set to translucent. This can be useful if an
621 * application needs full RGBA 8888 support for instance but will
622 * still draw every pixel opaque.
623 * <p>
624 * This flag only determines whether opacity will be sampled from the alpha channel.
625 * Plane-alpha from calls to setAlpha() can still result in blended composition
626 * regardless of the opaque setting.
627 *
628 * Combined effects are (assuming a buffer format with an alpha channel):
629 * <ul>
630 * <li>OPAQUE + alpha(1.0) == opaque composition
631 * <li>OPAQUE + alpha(0.x) == blended composition
632 * <li>OPAQUE + alpha(0.0) == no composition
633 * <li>!OPAQUE + alpha(1.0) == blended composition
634 * <li>!OPAQUE + alpha(0.x) == blended composition
635 * <li>!OPAQUE + alpha(0.0) == no composition
636 * </ul>
637 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
638 * were set automatically.
639 * @param opaque Whether the Surface is OPAQUE.
640 */
Robert Carr76907ee2019-01-11 13:38:19 -0800641 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700642 public Builder setOpaque(boolean opaque) {
643 if (opaque) {
644 mFlags |= OPAQUE;
645 } else {
646 mFlags &= ~OPAQUE;
647 }
648 return this;
649 }
650
651 /**
652 * Set a parent surface for our new SurfaceControl.
653 *
654 * Child surfaces are constrained to the onscreen region of their parent.
655 * Furthermore they stack relatively in Z order, and inherit the transformation
656 * of the parent.
657 *
658 * @param parent The parent control.
659 */
Robert Carr76907ee2019-01-11 13:38:19 -0800660 @NonNull
661 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700662 mParent = parent;
663 return this;
664 }
665
666 /**
Evan Rosky485df202018-12-06 14:11:12 -0800667 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700668 *
Evan Rosky485df202018-12-06 14:11:12 -0800669 * @param key metadata key
670 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800671 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700672 */
Evan Rosky485df202018-12-06 14:11:12 -0800673 public Builder setMetadata(int key, int data) {
674 if (mMetadata == null) {
675 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700676 }
Evan Rosky485df202018-12-06 14:11:12 -0800677 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700678 return this;
679 }
680
681 /**
682 * Indicate whether a 'ColorLayer' is to be constructed.
683 *
684 * Color layers will not have an associated BufferQueue and will instead always render a
685 * solid color (that is, solid before plane alpha). Currently that color is black.
686 *
Robert Carra7827f72019-01-11 12:53:37 -0800687 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700688 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000689 public Builder setColorLayer() {
690 unsetBufferSize();
691 return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700692 }
693
Vishnu Naire86bd982018-11-28 13:23:17 -0800694 private boolean isColorLayerSet() {
695 return (mFlags & FX_SURFACE_DIM) == FX_SURFACE_DIM;
696 }
697
Robert Carre625fcf2017-09-01 12:36:28 -0700698 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700699 * Indicates whether a 'ContainerLayer' is to be constructed.
700 *
701 * Container layers will not be rendered in any fashion and instead are used
702 * as a parent of renderable layers.
703 *
Robert Carra7827f72019-01-11 12:53:37 -0800704 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700705 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000706 public Builder setContainerLayer() {
707 unsetBufferSize();
708 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700709 }
710
Vishnu Naire86bd982018-11-28 13:23:17 -0800711 private boolean isContainerLayerSet() {
712 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
713 }
714
Robert Carrb6cd6432018-08-13 13:01:47 -0700715 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000716 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700717 *
718 * TODO: Finish conversion to individual builder methods?
719 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800720 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700721 */
722 public Builder setFlags(int flags) {
723 mFlags = flags;
724 return this;
725 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000726
727 private Builder setFlags(int flags, int mask) {
728 mFlags = (mFlags & ~mask) | flags;
729 return this;
730 }
Robert Carre625fcf2017-09-01 12:36:28 -0700731 }
732
733 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800734 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800735 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800736 * The surface creation flags specify what kind of surface to create and
737 * certain options such as whether the surface can be assumed to be opaque
738 * and whether it should be initially hidden. Surfaces should always be
739 * created with the {@link #HIDDEN} flag set to ensure that they are not
740 * made visible prematurely before all of the surface's properties have been
741 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800742 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800743 * Good practice is to first create the surface with the {@link #HIDDEN} flag
744 * specified, open a transaction, set the surface layer, layer stack, alpha,
745 * and position, call {@link #show} if appropriate, and close the transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800746 * <p>
747 * Bounds of the surface is determined by its crop and its buffer size. If the
748 * surface has no buffer or crop, the surface is boundless and only constrained
749 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800750 *
751 * @param session The surface session, must not be null.
752 * @param name The surface name, must not be null.
753 * @param w The surface initial width.
754 * @param h The surface initial height.
755 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
756 * in the creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800757 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700758 *
759 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800760 */
Robert Carre625fcf2017-09-01 12:36:28 -0700761 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800762 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700763 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800764 if (name == null) {
765 throw new IllegalArgumentException("name must not be null");
766 }
767
768 if ((flags & SurfaceControl.HIDDEN) == 0) {
769 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
770 + "to ensure that they are not made visible prematurely before "
771 + "all of the surface's properties have been configured. "
772 + "Set the other properties and make the surface visible within "
773 + "a transaction. New surface name: " + name,
774 new Throwable());
775 }
776
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800777 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100778 mWidth = w;
779 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800780 Parcel metaParcel = Parcel.obtain();
781 try {
782 if (metadata != null && metadata.size() > 0) {
783 metaParcel.writeInt(metadata.size());
784 for (int i = 0; i < metadata.size(); ++i) {
785 metaParcel.writeInt(metadata.keyAt(i));
786 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800787 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
788 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800789 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800790 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800791 }
792 mNativeObject = nativeCreate(session, name, w, h, format, flags,
793 parent != null ? parent.mNativeObject : 0, metaParcel);
794 } finally {
795 metaParcel.recycle();
796 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800797 if (mNativeObject == 0) {
798 throw new OutOfResourcesException(
799 "Couldn't allocate SurfaceControl native object");
800 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700801
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800802 mCloseGuard.open("release");
803 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700804
Robert Carra7827f72019-01-11 12:53:37 -0800805 /** This is a transfer constructor, useful for transferring a live SurfaceControl native
806 * object to another Java wrapper which could have some different behavior, e.g.
807 * event logging.
808 * @hide
809 */
Robert Carr3b716242016-08-16 16:02:21 -0700810 public SurfaceControl(SurfaceControl other) {
811 mName = other.mName;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100812 mWidth = other.mWidth;
813 mHeight = other.mHeight;
Robert Carr3b716242016-08-16 16:02:21 -0700814 mNativeObject = other.mNativeObject;
815 other.mCloseGuard.close();
816 other.mNativeObject = 0;
817 mCloseGuard.open("release");
818 }
819
Jorim Jaggi06975df2017-12-01 14:52:13 +0100820 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800821 readFromParcel(in);
822 mCloseGuard.open("release");
823 }
824
Robert Carra7827f72019-01-11 12:53:37 -0800825 /**
826 * @hide
827 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800828 public SurfaceControl() {
829 mCloseGuard.open("release");
830 }
831
832 public void readFromParcel(Parcel in) {
833 if (in == null) {
834 throw new IllegalArgumentException("source must not be null");
835 }
836
Jorim Jaggi06975df2017-12-01 14:52:13 +0100837 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100838 mWidth = in.readInt();
839 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800840
Robert Carreb344c72019-01-07 18:35:30 -0800841 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800842 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800843 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100844 }
Robert Carreb344c72019-01-07 18:35:30 -0800845 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100846 }
847
848 @Override
849 public int describeContents() {
850 return 0;
851 }
852
853 @Override
854 public void writeToParcel(Parcel dest, int flags) {
855 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100856 dest.writeInt(mWidth);
857 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800858 if (mNativeObject == 0) {
859 dest.writeInt(0);
860 } else {
861 dest.writeInt(1);
862 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100863 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800864
865 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
866 release();
867 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100868 }
869
Vishnu Nair04ab4392018-01-10 11:00:06 -0800870 /**
871 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
872 * android.view.SurfaceControlProto}.
873 *
874 * @param proto Stream to write the SurfaceControl object to.
875 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
876 * @hide
877 */
878 public void writeToProto(ProtoOutputStream proto, long fieldId) {
879 final long token = proto.start(fieldId);
880 proto.write(HASH_CODE, System.identityHashCode(this));
881 proto.write(NAME, mName);
882 proto.end(token);
883 }
884
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700885 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100886 = new Creator<SurfaceControl>() {
887 public SurfaceControl createFromParcel(Parcel in) {
888 return new SurfaceControl(in);
889 }
890
891 public SurfaceControl[] newArray(int size) {
892 return new SurfaceControl[size];
893 }
894 };
895
Robert Carra7827f72019-01-11 12:53:37 -0800896 /**
897 * @hide
898 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800899 @Override
900 protected void finalize() throws Throwable {
901 try {
902 if (mCloseGuard != null) {
903 mCloseGuard.warnIfOpen();
904 }
905 if (mNativeObject != 0) {
906 nativeRelease(mNativeObject);
907 }
908 } finally {
909 super.finalize();
910 }
911 }
912
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800913 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800914 * Release the local reference to the server-side surface. The surface
915 * may continue to exist on-screen as long as its parent continues
916 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -0700917 * {@link Transaction#reparent} with a null-parent. After release,
918 * {@link #isValid} will return false and other methods will throw
919 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -0800920 *
921 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800922 */
923 public void release() {
924 if (mNativeObject != 0) {
925 nativeRelease(mNativeObject);
926 mNativeObject = 0;
927 }
928 mCloseGuard.close();
929 }
930
931 /**
Chong Zhang47e36a32016-02-29 16:44:33 -0800932 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800933 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -0800934 */
935 public void disconnect() {
936 if (mNativeObject != 0) {
937 nativeDisconnect(mNativeObject);
938 }
939 }
940
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800941 private void checkNotReleased() {
942 if (mNativeObject == 0) throw new NullPointerException(
943 "mNativeObject is null. Have you called release() already?");
944 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700945
Robert Carra7827f72019-01-11 12:53:37 -0800946 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800947 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -0700948 * example this may be false if construction failed, or the layer was released
949 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -0800950 *
951 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -0800952 */
Robert Carr5fea55b2018-12-10 13:05:52 -0800953 public boolean isValid() {
954 return mNativeObject != 0;
955 }
956
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800957 /*
958 * set surface parameters.
959 * needs to be inside open/closeTransaction block
960 */
961
Robert Carra7827f72019-01-11 12:53:37 -0800962 /** start a transaction
963 * @hide
964 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100965 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800966 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700967 synchronized (SurfaceControl.class) {
968 if (sGlobalTransaction == null) {
969 sGlobalTransaction = new Transaction();
970 }
971 synchronized(SurfaceControl.class) {
972 sTransactionNestCount++;
973 }
974 }
975 }
976
Robert Carrb1579c82017-09-05 14:54:47 -0700977 /**
978 * Merge the supplied transaction in to the deprecated "global" transaction.
979 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
980 * <p>
981 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -0800982 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -0700983 */
984 @Deprecated
985 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -0800986 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -0700987 sGlobalTransaction.merge(t);
988 }
989 }
990
chaviwdaaeab02019-03-20 12:25:37 -0700991 /** end a transaction
992 * @hide
Robert Carra7827f72019-01-11 12:53:37 -0800993 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100994 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800995 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -0700996 synchronized(SurfaceControl.class) {
997 if (sTransactionNestCount == 0) {
998 Log.e(TAG,
999 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1000 } else if (--sTransactionNestCount > 0) {
1001 return;
1002 }
1003 sGlobalTransaction.apply();
1004 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001005 }
1006
Robert Carra7827f72019-01-11 12:53:37 -08001007 /**
1008 * @hide
1009 */
Rob Carr64e516f2015-10-29 00:20:45 +00001010 public void deferTransactionUntil(IBinder handle, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001011 synchronized(SurfaceControl.class) {
1012 sGlobalTransaction.deferTransactionUntil(this, handle, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001013 }
1014 }
1015
Robert Carra7827f72019-01-11 12:53:37 -08001016 /**
1017 * @hide
1018 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001019 public void deferTransactionUntil(Surface barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001020 synchronized(SurfaceControl.class) {
1021 sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001022 }
1023 }
1024
Robert Carra7827f72019-01-11 12:53:37 -08001025 /**
1026 * @hide
1027 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001028 public void reparentChildren(IBinder newParentHandle) {
Robert Carre13b58e2017-08-31 14:50:44 -07001029 synchronized(SurfaceControl.class) {
1030 sGlobalTransaction.reparentChildren(this, newParentHandle);
1031 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001032 }
1033
Robert Carra7827f72019-01-11 12:53:37 -08001034 /**
1035 * @hide
1036 */
Robert Carr10584fa2019-01-14 15:55:19 -08001037 public void reparent(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001038 synchronized(SurfaceControl.class) {
Robert Carr10584fa2019-01-14 15:55:19 -08001039 sGlobalTransaction.reparent(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001040 }
chaviw63542382017-08-17 17:39:29 -07001041 }
1042
Robert Carra7827f72019-01-11 12:53:37 -08001043 /**
1044 * @hide
1045 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001046 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001047 synchronized(SurfaceControl.class) {
1048 sGlobalTransaction.detachChildren(this);
1049 }
Rob Carr64e516f2015-10-29 00:20:45 +00001050 }
1051
Robert Carra7827f72019-01-11 12:53:37 -08001052 /**
1053 * @hide
1054 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001055 public void setOverrideScalingMode(int scalingMode) {
1056 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001057 synchronized(SurfaceControl.class) {
1058 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1059 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001060 }
1061
Robert Carra7827f72019-01-11 12:53:37 -08001062 /**
1063 * @hide
1064 */
Rob Carr64e516f2015-10-29 00:20:45 +00001065 public IBinder getHandle() {
1066 return nativeGetHandle(mNativeObject);
1067 }
1068
Robert Carra7827f72019-01-11 12:53:37 -08001069 /**
1070 * @hide
1071 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001072 public static void setAnimationTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001073 synchronized (SurfaceControl.class) {
1074 sGlobalTransaction.setAnimationTransaction();
1075 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001076 }
1077
Robert Carra7827f72019-01-11 12:53:37 -08001078 /**
1079 * @hide
1080 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001081 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001082 public void setLayer(int zorder) {
1083 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001084 synchronized(SurfaceControl.class) {
1085 sGlobalTransaction.setLayer(this, zorder);
1086 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001087 }
1088
Robert Carra7827f72019-01-11 12:53:37 -08001089 /**
1090 * @hide
1091 */
Robert Carr77e34942017-10-18 19:13:56 -07001092 public void setRelativeLayer(SurfaceControl relativeTo, int zorder) {
Robert Carraf422a82017-04-10 18:34:33 -07001093 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001094 synchronized(SurfaceControl.class) {
1095 sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder);
1096 }
Robert Carraf422a82017-04-10 18:34:33 -07001097 }
1098
Robert Carra7827f72019-01-11 12:53:37 -08001099 /**
1100 * @hide
1101 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001102 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001103 public void setPosition(float x, float y) {
1104 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001105 synchronized(SurfaceControl.class) {
1106 sGlobalTransaction.setPosition(this, x, y);
1107 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001108 }
1109
Robert Carra7827f72019-01-11 12:53:37 -08001110 /**
1111 * @hide
1112 */
Robert Carr6da3cc02016-06-16 15:17:07 -07001113 public void setGeometryAppliesWithResize() {
Robert Carra9408d42016-06-03 13:28:48 -07001114 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001115 synchronized(SurfaceControl.class) {
1116 sGlobalTransaction.setGeometryAppliesWithResize(this);
1117 }
Robert Carra9408d42016-06-03 13:28:48 -07001118 }
1119
Robert Carra7827f72019-01-11 12:53:37 -08001120 /**
1121 * @hide
1122 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001123 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001124 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001125 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001126 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001127 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001128 }
1129
Robert Carra7827f72019-01-11 12:53:37 -08001130 /**
1131 * @hide
1132 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001133 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001134 public void hide() {
1135 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001136 synchronized(SurfaceControl.class) {
1137 sGlobalTransaction.hide(this);
1138 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001139 }
1140
Robert Carra7827f72019-01-11 12:53:37 -08001141 /**
1142 * @hide
1143 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001144 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001145 public void show() {
1146 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001147 synchronized(SurfaceControl.class) {
1148 sGlobalTransaction.show(this);
1149 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001150 }
1151
Robert Carra7827f72019-01-11 12:53:37 -08001152 /**
1153 * @hide
1154 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001155 public void setTransparentRegionHint(Region region) {
1156 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001157 synchronized(SurfaceControl.class) {
1158 sGlobalTransaction.setTransparentRegionHint(this, region);
1159 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001160 }
1161
Robert Carra7827f72019-01-11 12:53:37 -08001162 /**
1163 * @hide
1164 */
Svetoslav1376d602014-03-13 11:17:26 -07001165 public boolean clearContentFrameStats() {
1166 checkNotReleased();
1167 return nativeClearContentFrameStats(mNativeObject);
1168 }
1169
Robert Carra7827f72019-01-11 12:53:37 -08001170 /**
1171 * @hide
1172 */
Svetoslav1376d602014-03-13 11:17:26 -07001173 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1174 checkNotReleased();
1175 return nativeGetContentFrameStats(mNativeObject, outStats);
1176 }
1177
Robert Carra7827f72019-01-11 12:53:37 -08001178 /**
1179 * @hide
1180 */
Svetoslav1376d602014-03-13 11:17:26 -07001181 public static boolean clearAnimationFrameStats() {
1182 return nativeClearAnimationFrameStats();
1183 }
1184
Robert Carra7827f72019-01-11 12:53:37 -08001185 /**
1186 * @hide
1187 */
Svetoslav1376d602014-03-13 11:17:26 -07001188 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1189 return nativeGetAnimationFrameStats(outStats);
1190 }
1191
Robert Carra7827f72019-01-11 12:53:37 -08001192 /**
1193 * @hide
1194 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001195 public void setAlpha(float alpha) {
1196 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001197 synchronized(SurfaceControl.class) {
1198 sGlobalTransaction.setAlpha(this, alpha);
1199 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001200 }
1201
Robert Carra7827f72019-01-11 12:53:37 -08001202 /**
1203 * @hide
1204 */
chaviw0dd03f52017-08-25 12:15:26 -07001205 public void setColor(@Size(3) float[] color) {
1206 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001207 synchronized (SurfaceControl.class) {
1208 sGlobalTransaction.setColor(this, color);
1209 }
chaviw0dd03f52017-08-25 12:15:26 -07001210 }
1211
Robert Carra7827f72019-01-11 12:53:37 -08001212 /**
1213 * @hide
1214 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001215 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001216 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001217 synchronized(SurfaceControl.class) {
1218 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1219 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001220 }
1221
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001222 /**
1223 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix.
1224 *
1225 * @param matrix The matrix to apply.
1226 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08001227 * @hide
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001228 */
1229 public void setMatrix(Matrix matrix, float[] float9) {
1230 checkNotReleased();
1231 matrix.getValues(float9);
1232 synchronized (SurfaceControl.class) {
1233 sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y],
1234 float9[MSKEW_X], float9[MSCALE_Y]);
1235 sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]);
1236 }
1237 }
1238
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001239 /**
1240 * Sets the color transform for the Surface.
1241 * @param matrix A float array with 9 values represents a 3x3 transform matrix
1242 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08001243 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001244 */
1245 public void setColorTransform(@Size(9) float[] matrix, @Size(3) float[] translation) {
1246 checkNotReleased();
1247 synchronized (SurfaceControl.class) {
1248 sGlobalTransaction.setColorTransform(this, matrix, translation);
1249 }
1250 }
1251
Vishnu Naird454442d2018-11-13 13:51:01 -08001252 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001253 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1254 * the color can be interpreted in any color space.
1255 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1256 * @hide
1257 */
1258 public void setColorSpaceAgnostic(boolean agnostic) {
1259 checkNotReleased();
1260 synchronized (SurfaceControl.class) {
1261 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1262 }
1263 }
1264
1265 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001266 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1267 * ignored and only the crop and buffer size will be used to determine the bounds of the
1268 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1269 * constrained by the size of its parent bounds.
1270 *
1271 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001272 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001273 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001274 public void setWindowCrop(Rect crop) {
1275 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001276 synchronized (SurfaceControl.class) {
1277 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001278 }
1279 }
1280
Vishnu Naird454442d2018-11-13 13:51:01 -08001281 /**
1282 * Same as {@link SurfaceControl#setWindowCrop(Rect)} but sets the crop rect top left at 0, 0.
1283 *
1284 * @param width width of crop rect
1285 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08001286 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001287 */
1288 public void setWindowCrop(int width, int height) {
1289 checkNotReleased();
1290 synchronized (SurfaceControl.class) {
1291 sGlobalTransaction.setWindowCrop(this, width, height);
1292 }
1293 }
1294
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001295 /**
1296 * Sets the corner radius of a {@link SurfaceControl}.
1297 *
1298 * @param cornerRadius Corner radius in pixels.
Robert Carra7827f72019-01-11 12:53:37 -08001299 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001300 */
1301 public void setCornerRadius(float cornerRadius) {
1302 checkNotReleased();
1303 synchronized (SurfaceControl.class) {
1304 sGlobalTransaction.setCornerRadius(this, cornerRadius);
1305 }
1306 }
1307
Robert Carra7827f72019-01-11 12:53:37 -08001308 /**
1309 * @hide
1310 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001311 public void setLayerStack(int layerStack) {
1312 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001313 synchronized(SurfaceControl.class) {
1314 sGlobalTransaction.setLayerStack(this, layerStack);
1315 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001316 }
1317
Robert Carra7827f72019-01-11 12:53:37 -08001318 /**
1319 * @hide
1320 */
Andy McFadden314405b2014-01-29 17:18:05 -08001321 public void setOpaque(boolean isOpaque) {
1322 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001323
1324 synchronized (SurfaceControl.class) {
1325 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001326 }
1327 }
1328
Robert Carra7827f72019-01-11 12:53:37 -08001329 /**
1330 * @hide
1331 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001332 public void setSecure(boolean isSecure) {
1333 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001334
1335 synchronized (SurfaceControl.class) {
1336 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001337 }
1338 }
1339
Robert Carra7827f72019-01-11 12:53:37 -08001340 /**
1341 * @hide
1342 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001343 public int getWidth() {
1344 synchronized (mSizeLock) {
1345 return mWidth;
1346 }
1347 }
1348
Robert Carra7827f72019-01-11 12:53:37 -08001349 /**
1350 * @hide
1351 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001352 public int getHeight() {
1353 synchronized (mSizeLock) {
1354 return mHeight;
1355 }
1356 }
1357
Robert Carre13b58e2017-08-31 14:50:44 -07001358 @Override
1359 public String toString() {
1360 return "Surface(name=" + mName + ")/@0x" +
1361 Integer.toHexString(System.identityHashCode(this));
1362 }
1363
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001364 /*
1365 * set display parameters.
1366 * needs to be inside open/closeTransaction block
1367 */
1368
1369 /**
1370 * Describes the properties of a physical display known to surface flinger.
Robert Carr76907ee2019-01-11 13:38:19 -08001371 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001372 */
1373 public static final class PhysicalDisplayInfo {
Robert Carra7827f72019-01-11 12:53:37 -08001374 /**
1375 * @hide
1376 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001377 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001378 public int width;
Robert Carra7827f72019-01-11 12:53:37 -08001379
1380 /**
1381 * @hide
1382 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001383 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001384 public int height;
Robert Carra7827f72019-01-11 12:53:37 -08001385
1386 /**
1387 * @hide
1388 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001389 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001390 public float refreshRate;
Robert Carra7827f72019-01-11 12:53:37 -08001391
1392 /**
1393 * @hide
1394 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001395 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001396 public float density;
Robert Carra7827f72019-01-11 12:53:37 -08001397
1398 /**
1399 * @hide
1400 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001401 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001402 public float xDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001403
1404 /**
1405 * @hide
1406 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001407 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001408 public float yDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001409
1410 /**
1411 * @hide
1412 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001413 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001414 public boolean secure;
Robert Carra7827f72019-01-11 12:53:37 -08001415
1416 /**
1417 * @hide
1418 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001419 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001420 public long appVsyncOffsetNanos;
Robert Carra7827f72019-01-11 12:53:37 -08001421
1422 /**
1423 * @hide
1424 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001425 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001426 public long presentationDeadlineNanos;
Jesse Hall6a6bc212013-08-08 12:15:03 -07001427
Robert Carra7827f72019-01-11 12:53:37 -08001428 /**
1429 * @hide
1430 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001431 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001432 public PhysicalDisplayInfo() {
1433 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001434
Robert Carra7827f72019-01-11 12:53:37 -08001435 /**
1436 * @hide
1437 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001438 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
1439 copyFrom(other);
1440 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001441
Robert Carra7827f72019-01-11 12:53:37 -08001442 /**
1443 * @hide
1444 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001445 @Override
1446 public boolean equals(Object o) {
1447 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
1448 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001449
Robert Carra7827f72019-01-11 12:53:37 -08001450 /**
1451 * @hide
1452 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001453 public boolean equals(PhysicalDisplayInfo other) {
1454 return other != null
1455 && width == other.width
1456 && height == other.height
1457 && refreshRate == other.refreshRate
1458 && density == other.density
1459 && xDpi == other.xDpi
1460 && yDpi == other.yDpi
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001461 && secure == other.secure
1462 && appVsyncOffsetNanos == other.appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001463 && presentationDeadlineNanos == other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001464 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001465
Robert Carra7827f72019-01-11 12:53:37 -08001466 /**
1467 * @hide
1468 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001469 @Override
1470 public int hashCode() {
1471 return 0; // don't care
1472 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001473
Robert Carra7827f72019-01-11 12:53:37 -08001474 /**
1475 * @hide
1476 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001477 public void copyFrom(PhysicalDisplayInfo other) {
1478 width = other.width;
1479 height = other.height;
1480 refreshRate = other.refreshRate;
1481 density = other.density;
1482 xDpi = other.xDpi;
1483 yDpi = other.yDpi;
1484 secure = other.secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001485 appVsyncOffsetNanos = other.appVsyncOffsetNanos;
1486 presentationDeadlineNanos = other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001487 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001488
Robert Carra7827f72019-01-11 12:53:37 -08001489 /**
1490 * @hide
1491 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001492 @Override
1493 public String toString() {
1494 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
1495 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001496 + ", appVsyncOffset " + appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001497 + ", bufferDeadline " + presentationDeadlineNanos + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001498 }
1499 }
1500
Robert Carra7827f72019-01-11 12:53:37 -08001501 /**
1502 * @hide
1503 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001504 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001505 if (displayToken == null) {
1506 throw new IllegalArgumentException("displayToken must not be null");
1507 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001508 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001509 }
1510
Robert Carra7827f72019-01-11 12:53:37 -08001511 /**
1512 * @hide
1513 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001514 @UnsupportedAppUsage
Dan Stoza00101052014-05-02 15:23:40 -07001515 public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001516 if (displayToken == null) {
1517 throw new IllegalArgumentException("displayToken must not be null");
1518 }
Dan Stoza00101052014-05-02 15:23:40 -07001519 return nativeGetDisplayConfigs(displayToken);
1520 }
1521
Robert Carra7827f72019-01-11 12:53:37 -08001522 /**
1523 * @hide
1524 */
Dan Stoza00101052014-05-02 15:23:40 -07001525 public static int getActiveConfig(IBinder displayToken) {
1526 if (displayToken == null) {
1527 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001528 }
Dan Stoza00101052014-05-02 15:23:40 -07001529 return nativeGetActiveConfig(displayToken);
1530 }
1531
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001532 /**
1533 * @hide
1534 */
1535 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1536 IBinder displayToken) {
1537 if (displayToken == null) {
1538 throw new IllegalArgumentException("displayToken must not be null");
1539 }
1540 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1541 }
1542
1543 /**
1544 * @hide
1545 */
1546 public static boolean setDisplayedContentSamplingEnabled(
1547 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1548 if (displayToken == null) {
1549 throw new IllegalArgumentException("displayToken must not be null");
1550 }
1551 final int maxColorComponents = 4;
1552 if ((componentMask >> maxColorComponents) != 0) {
1553 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1554 }
1555 return nativeSetDisplayedContentSamplingEnabled(
1556 displayToken, enable, componentMask, maxFrames);
1557 }
1558
1559 /**
1560 * @hide
1561 */
1562 public static DisplayedContentSample getDisplayedContentSample(
1563 IBinder displayToken, long maxFrames, long timestamp) {
1564 if (displayToken == null) {
1565 throw new IllegalArgumentException("displayToken must not be null");
1566 }
1567 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1568 }
1569
1570
Robert Carra7827f72019-01-11 12:53:37 -08001571 /**
1572 * @hide
1573 */
Dan Stoza00101052014-05-02 15:23:40 -07001574 public static boolean setActiveConfig(IBinder displayToken, int id) {
1575 if (displayToken == null) {
1576 throw new IllegalArgumentException("displayToken must not be null");
1577 }
1578 return nativeSetActiveConfig(displayToken, id);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001579 }
1580
Robert Carra7827f72019-01-11 12:53:37 -08001581 /**
1582 * @hide
1583 */
Ady Abraham6070ce12019-01-24 18:48:58 -08001584 public static boolean setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs) {
1585 if (displayToken == null) {
1586 throw new IllegalArgumentException("displayToken must not be null");
1587 }
1588 if (allowedConfigs == null) {
1589 throw new IllegalArgumentException("allowedConfigs must not be null");
1590 }
1591
1592 return nativeSetAllowedDisplayConfigs(displayToken, allowedConfigs);
1593 }
1594
1595 /**
1596 * @hide
1597 */
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001598 public static int[] getAllowedDisplayConfigs(IBinder displayToken) {
1599 if (displayToken == null) {
1600 throw new IllegalArgumentException("displayToken must not be null");
1601 }
1602 return nativeGetAllowedDisplayConfigs(displayToken);
1603 }
1604
1605 /**
1606 * @hide
1607 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001608 public static int[] getDisplayColorModes(IBinder displayToken) {
1609 if (displayToken == null) {
1610 throw new IllegalArgumentException("displayToken must not be null");
1611 }
1612 return nativeGetDisplayColorModes(displayToken);
1613 }
1614
Robert Carra7827f72019-01-11 12:53:37 -08001615 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001616 * Color coordinates in CIE1931 XYZ color space
1617 *
1618 * @hide
1619 */
1620 public static final class CieXyz {
1621 /**
1622 * @hide
1623 */
1624 public float X;
1625
1626 /**
1627 * @hide
1628 */
1629 public float Y;
1630
1631 /**
1632 * @hide
1633 */
1634 public float Z;
1635 }
1636
1637 /**
1638 * Contains a display's color primaries
1639 *
1640 * @hide
1641 */
1642 public static final class DisplayPrimaries {
1643 /**
1644 * @hide
1645 */
1646 public CieXyz red;
1647
1648 /**
1649 * @hide
1650 */
1651 public CieXyz green;
1652
1653 /**
1654 * @hide
1655 */
1656 public CieXyz blue;
1657
1658 /**
1659 * @hide
1660 */
1661 public CieXyz white;
1662
1663 /**
1664 * @hide
1665 */
1666 public DisplayPrimaries() {
1667 }
1668 }
1669
1670 /**
1671 * @hide
1672 */
1673 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1674 IBinder displayToken) {
1675 if (displayToken == null) {
1676 throw new IllegalArgumentException("displayToken must not be null");
1677 }
1678
1679 return nativeGetDisplayNativePrimaries(displayToken);
1680 }
1681
1682 /**
Robert Carra7827f72019-01-11 12:53:37 -08001683 * @hide
1684 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001685 public static int getActiveColorMode(IBinder displayToken) {
1686 if (displayToken == null) {
1687 throw new IllegalArgumentException("displayToken must not be null");
1688 }
1689 return nativeGetActiveColorMode(displayToken);
1690 }
1691
Robert Carra7827f72019-01-11 12:53:37 -08001692 /**
1693 * @hide
1694 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001695 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1696 if (displayToken == null) {
1697 throw new IllegalArgumentException("displayToken must not be null");
1698 }
1699 return nativeSetActiveColorMode(displayToken, colorMode);
1700 }
1701
Robert Carra7827f72019-01-11 12:53:37 -08001702 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001703 * Returns an array of color spaces with 2 elements. The first color space is the
1704 * default color space and second one is wide color gamut color space.
1705 * @hide
1706 */
1707 public static ColorSpace[] getCompositionColorSpaces() {
1708 int[] dataspaces = nativeGetCompositionDataspaces();
1709 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1710 ColorSpace[] colorSpaces = { srgb, srgb };
1711 if (dataspaces.length == 2) {
1712 for (int i = 0; i < 2; ++i) {
1713 switch(dataspaces[i]) {
1714 case INTERNAL_DATASPACE_DISPLAY_P3:
1715 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1716 break;
1717 case INTERNAL_DATASPACE_SCRGB:
1718 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1719 break;
1720 case INTERNAL_DATASPACE_SRGB:
1721 // Other dataspace is not recognized, use SRGB color space instead,
1722 // the default value of the array is already SRGB, thus do nothing.
1723 default:
1724 break;
1725 }
1726 }
1727 }
1728 return colorSpaces;
1729 }
1730
1731 /**
Robert Carra7827f72019-01-11 12:53:37 -08001732 * @hide
1733 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001734 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001735 public static void setDisplayProjection(IBinder displayToken,
1736 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001737 synchronized (SurfaceControl.class) {
1738 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1739 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001740 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001741 }
1742
Robert Carra7827f72019-01-11 12:53:37 -08001743 /**
1744 * @hide
1745 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001746 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001747 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001748 synchronized (SurfaceControl.class) {
1749 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001750 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001751 }
1752
Robert Carra7827f72019-01-11 12:53:37 -08001753 /**
1754 * @hide
1755 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001756 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001757 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001758 synchronized (SurfaceControl.class) {
1759 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001760 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001761 }
1762
Robert Carra7827f72019-01-11 12:53:37 -08001763 /**
1764 * @hide
1765 */
Michael Wright01e840f2014-06-26 16:03:25 -07001766 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001767 synchronized (SurfaceControl.class) {
1768 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001769 }
Michael Wright01e840f2014-06-26 16:03:25 -07001770 }
1771
Robert Carra7827f72019-01-11 12:53:37 -08001772 /**
1773 * @hide
1774 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001775 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1776 if (displayToken == null) {
1777 throw new IllegalArgumentException("displayToken must not be null");
1778 }
1779 return nativeGetHdrCapabilities(displayToken);
1780 }
1781
Robert Carra7827f72019-01-11 12:53:37 -08001782 /**
1783 * @hide
1784 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001785 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001786 public static IBinder createDisplay(String name, boolean secure) {
1787 if (name == null) {
1788 throw new IllegalArgumentException("name must not be null");
1789 }
1790 return nativeCreateDisplay(name, secure);
1791 }
1792
Robert Carra7827f72019-01-11 12:53:37 -08001793 /**
1794 * @hide
1795 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001796 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001797 public static void destroyDisplay(IBinder displayToken) {
1798 if (displayToken == null) {
1799 throw new IllegalArgumentException("displayToken must not be null");
1800 }
1801 nativeDestroyDisplay(displayToken);
1802 }
1803
Robert Carra7827f72019-01-11 12:53:37 -08001804 /**
1805 * @hide
1806 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001807 public static long[] getPhysicalDisplayIds() {
1808 return nativeGetPhysicalDisplayIds();
1809 }
1810
1811 /**
1812 * @hide
1813 */
1814 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1815 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1816 }
1817
1818 /**
1819 * TODO(116025192): Remove this stopgap once framework is display-agnostic.
1820 *
1821 * @hide
1822 */
1823 public static IBinder getInternalDisplayToken() {
1824 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1825 if (physicalDisplayIds.length == 0) {
1826 return null;
1827 }
1828 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001829 }
1830
Mathias Agopian0449a402013-03-01 23:01:51 -08001831 /**
chaviw08520a02018-09-10 16:44:56 -07001832 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001833 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001834 */
1835 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001836 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1837 }
1838
1839 /**
1840 * Copy the current screen contents into the provided {@link Surface}
1841 *
1842 * @param consumer The {@link Surface} to take the screenshot into.
1843 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001844 * @hide
chaviw08520a02018-09-10 16:44:56 -07001845 */
1846 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1847 int height, boolean useIdentityTransform, int rotation) {
1848 if (consumer == null) {
1849 throw new IllegalArgumentException("consumer must not be null");
1850 }
1851
Peiyong Line3e5efd2019-03-21 20:59:47 +00001852 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1853 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001854 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001855 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1856 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001857 } catch (RuntimeException e) {
1858 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1859 }
1860 }
1861
1862 /**
1863 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001864 * @hide
chaviw08520a02018-09-10 16:44:56 -07001865 */
1866 @UnsupportedAppUsage
1867 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1868 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001869 }
1870
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001871 /**
chaviw1da9cd92017-12-06 10:48:11 -08001872 * Copy the current screen contents into a hardware bitmap and return it.
1873 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1874 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001875 *
chaviw08520a02018-09-10 16:44:56 -07001876 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1877 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1878 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1879 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001880 *
chaviw08520a02018-09-10 16:44:56 -07001881 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001882 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001883 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001884 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001885 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001886 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001887 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001888 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1889 if (displayToken == null) {
1890 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1891 return null;
1892 }
1893
chaviwa69e0a72017-11-29 17:55:12 -08001894 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1895 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1896 }
1897
1898 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00001899 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
1900 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001901
1902 if (buffer == null) {
1903 Log.w(TAG, "Failed to take screenshot");
1904 return null;
1905 }
Sunny Goyal62915b22019-04-10 12:28:47 -07001906 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001907 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001908
chaviw08520a02018-09-10 16:44:56 -07001909 /**
1910 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1911 *
1912 * @param display The display to take the screenshot of.
1913 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1914 * pass in 'new Rect()' if no cropping is desired.
1915 * @param width The desired width of the returned bitmap; the raw screen will be
1916 * scaled down to this size; caller may pass in 0 if no scaling is
1917 * desired.
1918 * @param height The desired height of the returned bitmap; the raw screen will
1919 * be scaled down to this size; caller may pass in 0 if no scaling
1920 * is desired.
1921 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1922 * the surface layers are currently using with the identity
1923 * transformation while taking the screenshot.
1924 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1925 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1926 * screenshots in its native portrait orientation by default, so
1927 * this is useful for returning screenshots that are independent of
1928 * device orientation.
1929 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001930 * @hide
chaviw08520a02018-09-10 16:44:56 -07001931 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001932 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
1933 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001934 if (display == null) {
1935 throw new IllegalArgumentException("displayToken must not be null");
1936 }
chaviw08520a02018-09-10 16:44:56 -07001937
Robert Carr5c52b132019-02-15 15:48:11 -08001938 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1939 false /* captureSecureLayers */);
1940 }
1941
1942 /**
1943 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1944 * for the capture of secure layers. This is used for the screen rotation
1945 * animation where the system server takes screenshots but does
1946 * not persist them or allow them to leave the server. However in other
1947 * cases in the system server, we mostly want to omit secure layers
1948 * like when we take a screenshot on behalf of the assistant.
1949 *
1950 * @hide
1951 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001952 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08001953 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1954 int rotation) {
1955 if (display == null) {
1956 throw new IllegalArgumentException("displayToken must not be null");
1957 }
1958
1959 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1960 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001961 }
Robert Carre13b58e2017-08-31 14:50:44 -07001962
chaviwa69e0a72017-11-29 17:55:12 -08001963 private static void rotateCropForSF(Rect crop, int rot) {
1964 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1965 int tmp = crop.top;
1966 crop.top = crop.left;
1967 crop.left = tmp;
1968 tmp = crop.right;
1969 crop.right = crop.bottom;
1970 crop.bottom = tmp;
1971 }
1972 }
1973
chaviw1cda84c2017-10-23 16:47:10 -07001974 /**
chaviw1da9cd92017-12-06 10:48:11 -08001975 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001976 *
1977 * @param layerHandleToken The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001978 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
1979 * Rect()' or null if no cropping is desired.
1980 * @param frameScale The desired scale of the returned buffer; the raw
1981 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001982 *
1983 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001984 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001985 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001986 public static ScreenshotGraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08001987 float frameScale) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07001988 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1989 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, null);
Robert Carrffcdc512019-04-02 11:51:11 -07001990 }
1991
1992 /**
1993 * Like {@link captureLayers} but with an array of layer handles to exclude.
1994 * @hide
1995 */
1996 public static ScreenshotGraphicBuffer captureLayersExcluding(IBinder layerHandleToken,
1997 Rect sourceCrop, float frameScale, IBinder[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07001998 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1999 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, exclude);
chaviw1cda84c2017-10-23 16:47:10 -07002000 }
2001
Robert Carra7827f72019-01-11 12:53:37 -08002002 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08002003 * Returns whether protected content is supported in GPU composition.
2004 * @hide
2005 */
2006 public static boolean getProtectedContentSupport() {
2007 return nativeGetProtectedContentSupport();
2008 }
2009
2010 /**
Dan Gittik832b4972019-02-13 18:17:47 +00002011 * Returns whether brightness operations are supported on a display.
2012 *
2013 * @param displayToken
2014 * The token for the display.
2015 *
2016 * @return Whether brightness operations are supported on the display.
2017 *
2018 * @hide
2019 */
2020 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
2021 return nativeGetDisplayBrightnessSupport(displayToken);
2022 }
2023
2024 /**
2025 * Sets the brightness of a display.
2026 *
2027 * @param displayToken
2028 * The token for the display whose brightness is set.
2029 * @param brightness
2030 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2031 * turn the backlight off.
2032 *
2033 * @return Whether the method succeeded or not.
2034 *
2035 * @throws IllegalArgumentException if:
2036 * - displayToken is null;
2037 * - brightness is NaN or greater than 1.0f.
2038 *
2039 * @hide
2040 */
2041 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2042 Objects.requireNonNull(displayToken);
2043 if (Float.isNaN(brightness) || brightness > 1.0f
2044 || (brightness < 0.0f && brightness != -1.0f)) {
2045 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2046 + " or -1 to turn the backlight off.");
2047 }
2048 return nativeSetDisplayBrightness(displayToken, brightness);
2049 }
2050
2051 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002052 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002053 */
Robert Carre13b58e2017-08-31 14:50:44 -07002054 public static class Transaction implements Closeable {
Robert Carr76907ee2019-01-11 13:38:19 -08002055 /**
2056 * @hide
2057 */
Robert Carre13b58e2017-08-31 14:50:44 -07002058 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2059 Transaction.class.getClassLoader(),
2060 nativeGetNativeTransactionFinalizer(), 512);
2061 private long mNativeObject;
2062
Jorim Jaggia5e10572017-11-15 14:36:26 +01002063 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07002064 Runnable mFreeNativeResources;
2065
Robert Carra7827f72019-01-11 12:53:37 -08002066 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002067 * Open a new transaction object. The transaction may be filed with commands to
2068 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2069 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2070 * is no longer required. Note however that re-using a transaction after a call to apply
2071 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002072 */
Robert Carre13b58e2017-08-31 14:50:44 -07002073 public Transaction() {
2074 mNativeObject = nativeCreateTransaction();
2075 mFreeNativeResources
2076 = sRegistry.registerNativeAllocation(this, mNativeObject);
2077 }
2078
2079 /**
2080 * Apply the transaction, clearing it's state, and making it usable
2081 * as a new transaction.
2082 */
2083 public void apply() {
2084 apply(false);
2085 }
2086
2087 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002088 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002089 */
2090 @Override
2091 public void close() {
2092 mFreeNativeResources.run();
2093 mNativeObject = 0;
2094 }
2095
2096 /**
2097 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002098 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002099 */
2100 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002101 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002102 nativeApplyTransaction(mNativeObject, sync);
2103 }
2104
Jorim Jaggia5e10572017-11-15 14:36:26 +01002105 private void applyResizedSurfaces() {
2106 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2107 final Point size = mResizedSurfaces.valueAt(i);
2108 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2109 synchronized (surfaceControl.mSizeLock) {
2110 surfaceControl.mWidth = size.x;
2111 surfaceControl.mHeight = size.y;
2112 }
2113 }
2114 mResizedSurfaces.clear();
2115 }
2116
Robert Carra7827f72019-01-11 12:53:37 -08002117 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002118 * Toggle the visibility of a given Layer and it's sub-tree.
2119 *
2120 * @param sc The SurfaceControl for which to set the visibility
2121 * @param visible The new visibility
2122 * @return This transaction object.
2123 */
2124 @NonNull
2125 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
2126 sc.checkNotReleased();
2127 if (visible) {
2128 return show(sc);
2129 } else {
2130 return hide(sc);
2131 }
2132 }
2133
2134 /**
2135 * Request that a given surface and it's sub-tree be shown.
2136 *
2137 * @param sc The surface to show.
2138 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002139 * @hide
2140 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002141 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002142 public Transaction show(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002143 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002144 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2145 return this;
2146 }
2147
Robert Carra7827f72019-01-11 12:53:37 -08002148 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002149 * Request that a given surface and it's sub-tree be hidden.
2150 *
2151 * @param sc The surface to hidden.
2152 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002153 * @hide
2154 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002155 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002156 public Transaction hide(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002157 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002158 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2159 return this;
2160 }
2161
Robert Carra7827f72019-01-11 12:53:37 -08002162 /**
2163 * @hide
2164 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002165 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002166 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002167 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002168 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2169 return this;
2170 }
2171
Robert Carra7827f72019-01-11 12:53:37 -08002172 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002173 * Set the default buffer size for the SurfaceControl, if there is a
2174 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002175 * this will be the default size for buffers dequeued from it.
2176 * @param sc The surface to set the buffer size for.
2177 * @param w The default width
2178 * @param h The default height
2179 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002180 */
Robert Carr76907ee2019-01-11 13:38:19 -08002181 @NonNull
2182 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2183 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002184 sc.checkNotReleased();
Jorim Jaggia5e10572017-11-15 14:36:26 +01002185 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002186 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002187 return this;
2188 }
2189
Robert Carra7827f72019-01-11 12:53:37 -08002190 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002191 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2192 * If two siblings share the same Z order the ordering is undefined. Surfaces
2193 * with a negative Z will be placed below the parent surface.
2194 *
2195 * @param sc The SurfaceControl to set the Z order on
2196 * @param z The Z-order
2197 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002198 */
Robert Carr76907ee2019-01-11 13:38:19 -08002199 @NonNull
2200 public Transaction setLayer(@NonNull SurfaceControl sc,
2201 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002202 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002203 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2204 return this;
2205 }
2206
Robert Carra7827f72019-01-11 12:53:37 -08002207 /**
2208 * @hide
2209 */
Robert Carr77e34942017-10-18 19:13:56 -07002210 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002211 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002212 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject,
Robert Carr77e34942017-10-18 19:13:56 -07002213 relativeTo.getHandle(), z);
Robert Carre13b58e2017-08-31 14:50:44 -07002214 return this;
2215 }
2216
Robert Carra7827f72019-01-11 12:53:37 -08002217 /**
2218 * @hide
2219 */
Robert Carre13b58e2017-08-31 14:50:44 -07002220 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002221 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002222 nativeSetTransparentRegionHint(mNativeObject,
2223 sc.mNativeObject, transparentRegion);
2224 return this;
2225 }
2226
Robert Carra7827f72019-01-11 12:53:37 -08002227 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002228 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2229 * will be blended with the Surfaces under it according to the specified ratio.
2230 *
2231 * @param sc The given SurfaceControl.
2232 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002233 */
Robert Carr76907ee2019-01-11 13:38:19 -08002234 @NonNull
2235 public Transaction setAlpha(@NonNull SurfaceControl sc,
2236 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002237 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002238 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2239 return this;
2240 }
2241
Robert Carra7827f72019-01-11 12:53:37 -08002242 /**
2243 * @hide
2244 */
Robert Carr788f5742018-07-30 17:46:45 -07002245 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
2246 sc.checkNotReleased();
2247 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2248 return this;
2249 }
2250
chaviw59f532e2018-12-26 15:34:59 -08002251 /**
2252 * Transfers touch focus from one window to another. It is possible for multiple windows to
2253 * have touch focus if they support split touch dispatch
2254 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
2255 * method only transfers touch focus of the specified window without affecting
2256 * other windows that may also have touch focus at the same time.
2257 * @param fromToken The token of a window that currently has touch focus.
2258 * @param toToken The token of the window that should receive touch focus in
2259 * place of the first.
Robert Carra7827f72019-01-11 12:53:37 -08002260 * @hide
chaviw59f532e2018-12-26 15:34:59 -08002261 */
2262 public Transaction transferTouchFocus(IBinder fromToken, IBinder toToken) {
2263 nativeTransferTouchFocus(mNativeObject, fromToken, toToken);
2264 return this;
2265 }
2266
Robert Carra7827f72019-01-11 12:53:37 -08002267 /**
chaviw319cd0782019-02-14 11:00:23 -08002268 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2269 * InputFlinger before returning.
2270 *
2271 * @hide
2272 */
2273 public Transaction syncInputWindows() {
2274 nativeSyncInputWindows(mNativeObject);
2275 return this;
2276 }
2277
2278 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002279 * Specify how the buffer assosciated with this Surface is mapped in to the
2280 * parent coordinate space. The source frame will be scaled to fit the destination
2281 * frame, after being rotated according to the orientation parameter.
2282 *
2283 * @param sc The SurfaceControl to specify the geometry of
2284 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2285 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2286 * @param orientation The buffer rotation
2287 * @return This transaction object.
2288 */
2289 @NonNull
2290 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2291 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
2292 sc.checkNotReleased();
2293 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2294 return this;
2295 }
2296
2297 /**
Robert Carra7827f72019-01-11 12:53:37 -08002298 * @hide
2299 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002300 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002301 public Transaction setMatrix(SurfaceControl sc,
2302 float dsdx, float dtdx, float dtdy, float dsdy) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002303 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002304 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2305 dsdx, dtdx, dtdy, dsdy);
2306 return this;
2307 }
2308
Robert Carra7827f72019-01-11 12:53:37 -08002309 /**
2310 * @hide
2311 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002312 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002313 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2314 matrix.getValues(float9);
2315 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2316 float9[MSKEW_X], float9[MSCALE_Y]);
2317 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2318 return this;
2319 }
2320
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002321 /**
2322 * Sets the color transform for the Surface.
2323 * @param matrix A float array with 9 values represents a 3x3 transform matrix
2324 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002325 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002326 */
2327 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2328 @Size(3) float[] translation) {
2329 sc.checkNotReleased();
2330 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2331 return this;
2332 }
2333
Robert Carra7827f72019-01-11 12:53:37 -08002334 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002335 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2336 * the color can be interpreted in any color space.
2337 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2338 * @hide
2339 */
2340 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
2341 sc.checkNotReleased();
2342 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2343 return this;
2344 }
2345
2346 /**
Robert Carra7827f72019-01-11 12:53:37 -08002347 * @hide
2348 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002349 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002350 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002351 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002352 if (crop != null) {
2353 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2354 crop.left, crop.top, crop.right, crop.bottom);
2355 } else {
2356 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2357 }
2358
2359 return this;
2360 }
2361
Robert Carra7827f72019-01-11 12:53:37 -08002362 /**
2363 * @hide
2364 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002365 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
2366 sc.checkNotReleased();
2367 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2368 return this;
2369 }
2370
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002371 /**
2372 * Sets the corner radius of a {@link SurfaceControl}.
2373 * @param sc SurfaceControl
2374 * @param cornerRadius Corner radius in pixels.
2375 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002376 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002377 */
2378 @UnsupportedAppUsage
2379 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
2380 sc.checkNotReleased();
2381 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2382
2383 return this;
2384 }
2385
Robert Carra7827f72019-01-11 12:53:37 -08002386 /**
2387 * @hide
2388 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002389 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002390 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002391 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002392 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2393 return this;
2394 }
2395
Robert Carra7827f72019-01-11 12:53:37 -08002396 /**
2397 * @hide
2398 */
Robert Carr76907ee2019-01-11 13:38:19 -08002399 @UnsupportedAppUsage
Jorim Jaggidfc27372017-10-27 17:47:49 +02002400 public Transaction deferTransactionUntil(SurfaceControl sc, IBinder handle,
2401 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002402 if (frameNumber < 0) {
2403 return this;
2404 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002405 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002406 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, handle, frameNumber);
2407 return this;
2408 }
2409
Robert Carra7827f72019-01-11 12:53:37 -08002410 /**
2411 * @hide
2412 */
Robert Carr76907ee2019-01-11 13:38:19 -08002413 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002414 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2415 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002416 if (frameNumber < 0) {
2417 return this;
2418 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002419 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002420 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2421 barrierSurface.mNativeObject, frameNumber);
2422 return this;
2423 }
2424
Robert Carra7827f72019-01-11 12:53:37 -08002425 /**
2426 * @hide
2427 */
Robert Carre13b58e2017-08-31 14:50:44 -07002428 public Transaction reparentChildren(SurfaceControl sc, IBinder newParentHandle) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002429 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002430 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParentHandle);
2431 return this;
2432 }
2433
Robert Carr10584fa2019-01-14 15:55:19 -08002434 /**
2435 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2436 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2437 * parent Surface.
2438 *
2439 * @param sc The SurfaceControl to reparent
2440 * @param newParent The new parent for the given control.
2441 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002442 */
Robert Carr76907ee2019-01-11 13:38:19 -08002443 @NonNull
2444 public Transaction reparent(@NonNull SurfaceControl sc,
2445 @Nullable SurfaceControl newParent) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002446 sc.checkNotReleased();
Robert Carr10584fa2019-01-14 15:55:19 -08002447 long otherObject = 0;
2448 if (newParent != null) {
2449 newParent.checkNotReleased();
2450 otherObject = newParent.mNativeObject;
2451 }
2452 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002453 return this;
2454 }
2455
Robert Carra7827f72019-01-11 12:53:37 -08002456 /**
2457 * @hide
2458 */
Robert Carre13b58e2017-08-31 14:50:44 -07002459 public Transaction detachChildren(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002460 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002461 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2462 return this;
2463 }
2464
Robert Carra7827f72019-01-11 12:53:37 -08002465 /**
2466 * @hide
2467 */
Robert Carre13b58e2017-08-31 14:50:44 -07002468 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002469 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002470 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2471 overrideScalingMode);
2472 return this;
2473 }
2474
2475 /**
2476 * Sets a color for the Surface.
2477 * @param color A float array with three values to represent r, g, b in range [0..1]
Robert Carra7827f72019-01-11 12:53:37 -08002478 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002479 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002480 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002481 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002482 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002483 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2484 return this;
2485 }
2486
2487 /**
2488 * If the buffer size changes in this transaction, position and crop updates specified
2489 * in this transaction will not complete until a buffer of the new size
2490 * arrives. As transform matrix and size are already frozen in this fashion,
2491 * this enables totally freezing the surface until the resize has completed
2492 * (at which point the geometry influencing aspects of this transaction will then occur)
Robert Carra7827f72019-01-11 12:53:37 -08002493 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002494 */
2495 public Transaction setGeometryAppliesWithResize(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002496 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002497 nativeSetGeometryAppliesWithResize(mNativeObject, sc.mNativeObject);
2498 return this;
2499 }
2500
2501 /**
2502 * Sets the security of the surface. Setting the flag is equivalent to creating the
2503 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002504 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002505 */
Robert Carrb1579c82017-09-05 14:54:47 -07002506 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002507 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002508 if (isSecure) {
2509 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2510 } else {
2511 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2512 }
2513 return this;
2514 }
2515
2516 /**
2517 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2518 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002519 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002520 */
2521 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002522 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002523 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002524 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002525 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002526 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002527 }
2528 return this;
2529 }
2530
Robert Carra7827f72019-01-11 12:53:37 -08002531 /**
2532 * @hide
2533 */
Robert Carre13b58e2017-08-31 14:50:44 -07002534 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2535 if (displayToken == null) {
2536 throw new IllegalArgumentException("displayToken must not be null");
2537 }
2538
2539 if (surface != null) {
2540 synchronized (surface.mLock) {
2541 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2542 }
2543 } else {
2544 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2545 }
2546 return this;
2547 }
2548
Robert Carra7827f72019-01-11 12:53:37 -08002549 /**
2550 * @hide
2551 */
Robert Carre13b58e2017-08-31 14:50:44 -07002552 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2553 if (displayToken == null) {
2554 throw new IllegalArgumentException("displayToken must not be null");
2555 }
2556 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2557 return this;
2558 }
2559
Robert Carra7827f72019-01-11 12:53:37 -08002560 /**
2561 * @hide
2562 */
Robert Carre13b58e2017-08-31 14:50:44 -07002563 public Transaction setDisplayProjection(IBinder displayToken,
2564 int orientation, Rect layerStackRect, Rect displayRect) {
2565 if (displayToken == null) {
2566 throw new IllegalArgumentException("displayToken must not be null");
2567 }
2568 if (layerStackRect == null) {
2569 throw new IllegalArgumentException("layerStackRect must not be null");
2570 }
2571 if (displayRect == null) {
2572 throw new IllegalArgumentException("displayRect must not be null");
2573 }
2574 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2575 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2576 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2577 return this;
2578 }
2579
Robert Carra7827f72019-01-11 12:53:37 -08002580 /**
2581 * @hide
2582 */
Robert Carre13b58e2017-08-31 14:50:44 -07002583 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2584 if (displayToken == null) {
2585 throw new IllegalArgumentException("displayToken must not be null");
2586 }
2587 if (width <= 0 || height <= 0) {
2588 throw new IllegalArgumentException("width and height must be positive");
2589 }
2590
2591 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2592 return this;
2593 }
2594
Robert Carra7827f72019-01-11 12:53:37 -08002595 /** flag the transaction as an animation
2596 * @hide
2597 */
Robert Carre13b58e2017-08-31 14:50:44 -07002598 public Transaction setAnimationTransaction() {
2599 nativeSetAnimationTransaction(mNativeObject);
2600 return this;
2601 }
Robert Carrb1579c82017-09-05 14:54:47 -07002602
2603 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002604 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2605 * transaction. This should be used when the caller thinks that the scene is complex enough
2606 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2607 * order not to miss frame deadlines.
2608 * <p>
2609 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002610 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002611 */
2612 public Transaction setEarlyWakeup() {
2613 nativeSetEarlyWakeup(mNativeObject);
2614 return this;
2615 }
2616
2617 /**
Evan Rosky485df202018-12-06 14:11:12 -08002618 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2619 * @hide
2620 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002621 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002622 Parcel parcel = Parcel.obtain();
2623 parcel.writeInt(data);
2624 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002625 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002626 } finally {
2627 parcel.recycle();
2628 }
2629 return this;
2630 }
2631
2632 /**
2633 * Sets an arbitrary piece of metadata on the surface.
2634 * @hide
2635 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002636 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
2637 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002638 return this;
2639 }
2640
2641 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002642 * Merge the other transaction into this transaction, clearing the
2643 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002644 *
2645 * @param other The transaction to merge in to this one.
2646 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002647 */
Robert Carr76907ee2019-01-11 13:38:19 -08002648 @NonNull
2649 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002650 if (this == other) {
2651 return this;
2652 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01002653 mResizedSurfaces.putAll(other.mResizedSurfaces);
2654 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002655 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2656 return this;
2657 }
Robert Carr71200f22019-02-05 09:44:53 -08002658
2659 /**
2660 * Equivalent to reparent with a null parent, in that it removes
2661 * the SurfaceControl from the scene, but it also releases
2662 * the local resources (by calling {@link SurfaceControl#release})
2663 * after this method returns, {@link SurfaceControl#isValid} will return
2664 * false for the argument.
2665 *
2666 * @param sc The surface to remove and release.
2667 * @return This transaction
2668 * @hide
2669 */
2670 @NonNull
2671 public Transaction remove(@NonNull SurfaceControl sc) {
2672 reparent(sc, null);
2673 sc.release();
2674 return this;
2675 }
Robert Carre13b58e2017-08-31 14:50:44 -07002676 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002677}