/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.view; import static android.graphics.Matrix.MSCALE_X; import static android.graphics.Matrix.MSCALE_Y; import static android.graphics.Matrix.MSKEW_X; import static android.graphics.Matrix.MSKEW_Y; import static android.graphics.Matrix.MTRANS_X; import static android.graphics.Matrix.MTRANS_Y; import static android.view.Surface.ROTATION_270; import static android.view.Surface.ROTATION_90; import static android.view.SurfaceControlProto.HASH_CODE; import static android.view.SurfaceControlProto.NAME; import android.annotation.Size; import android.graphics.Bitmap; import android.graphics.GraphicBuffer; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.Rect; import android.graphics.Region; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; import android.os.Process; import android.os.UserHandle; import android.util.ArrayMap; import android.util.Log; import android.util.proto.ProtoOutputStream; import android.view.Surface.OutOfResourcesException; import com.android.internal.annotations.GuardedBy; import dalvik.system.CloseGuard; import libcore.util.NativeAllocationRegistry; import java.io.Closeable; /** * SurfaceControl * @hide */ public class SurfaceControl implements Parcelable { private static final String TAG = "SurfaceControl"; private static native long nativeCreate(SurfaceSession session, String name, int w, int h, int format, int flags, long parentObject, int windowType, int ownerUid) throws OutOfResourcesException; private static native long nativeReadFromParcel(Parcel in); private static native void nativeWriteToParcel(long nativeObject, Parcel out); private static native void nativeRelease(long nativeObject); private static native void nativeDestroy(long nativeObject); private static native void nativeDisconnect(long nativeObject); private static native Bitmap nativeScreenshot(IBinder displayToken, Rect sourceCrop, int width, int height, int minLayer, int maxLayer, boolean allLayers, boolean useIdentityTransform, int rotation); private static native GraphicBuffer nativeScreenshotToBuffer(IBinder displayToken, Rect sourceCrop, int width, int height, int minLayer, int maxLayer, boolean allLayers, boolean useIdentityTransform, int rotation); private static native void nativeScreenshot(IBinder displayToken, Surface consumer, Rect sourceCrop, int width, int height, int minLayer, int maxLayer, boolean allLayers, boolean useIdentityTransform); private static native GraphicBuffer nativeCaptureLayers(IBinder layerHandleToken, Rect sourceCrop, float frameScale); private static native long nativeCreateTransaction(); private static native long nativeGetNativeTransactionFinalizer(); private static native void nativeApplyTransaction(long transactionObj, boolean sync); private static native void nativeMergeTransaction(long transactionObj, long otherTransactionObj); private static native void nativeSetAnimationTransaction(long transactionObj); private static native void nativeSetEarlyWakeup(long transactionObj); private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder); private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject, IBinder relativeTo, int zorder); private static native void nativeSetPosition(long transactionObj, long nativeObject, float x, float y); private static native void nativeSetGeometryAppliesWithResize(long transactionObj, long nativeObject); private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h); private static native void nativeSetTransparentRegionHint(long transactionObj, long nativeObject, Region region); private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha); private static native void nativeSetMatrix(long transactionObj, long nativeObject, float dsdx, float dtdx, float dtdy, float dsdy); private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color); private static native void nativeSetFlags(long transactionObj, long nativeObject, int flags, int mask); private static native void nativeSetWindowCrop(long transactionObj, long nativeObject, int l, int t, int r, int b); private static native void nativeSetFinalCrop(long transactionObj, long nativeObject, int l, int t, int r, int b); private static native void nativeSetLayerStack(long transactionObj, long nativeObject, int layerStack); private static native boolean nativeClearContentFrameStats(long nativeObject); private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats); private static native boolean nativeClearAnimationFrameStats(); private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats); private static native IBinder nativeGetBuiltInDisplay(int physicalDisplayId); private static native IBinder nativeCreateDisplay(String name, boolean secure); private static native void nativeDestroyDisplay(IBinder displayToken); private static native void nativeSetDisplaySurface(long transactionObj, IBinder displayToken, long nativeSurfaceObject); private static native void nativeSetDisplayLayerStack(long transactionObj, IBinder displayToken, int layerStack); private static native void nativeSetDisplayProjection(long transactionObj, IBinder displayToken, int orientation, int l, int t, int r, int b, int L, int T, int R, int B); private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken, int width, int height); private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs( IBinder displayToken); private static native int nativeGetActiveConfig(IBinder displayToken); private static native boolean nativeSetActiveConfig(IBinder displayToken, int id); private static native int[] nativeGetDisplayColorModes(IBinder displayToken); private static native int nativeGetActiveColorMode(IBinder displayToken); private static native boolean nativeSetActiveColorMode(IBinder displayToken, int colorMode); private static native void nativeSetDisplayPowerMode( IBinder displayToken, int mode); private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject, IBinder handle, long frame); private static native void nativeDeferTransactionUntilSurface(long transactionObj, long nativeObject, long surfaceObject, long frame); private static native void nativeReparentChildren(long transactionObj, long nativeObject, IBinder handle); private static native void nativeReparent(long transactionObj, long nativeObject, IBinder parentHandle); private static native void nativeSeverChildren(long transactionObj, long nativeObject); private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject, int scalingMode); private static native void nativeDestroy(long transactionObj, long nativeObject); private static native IBinder nativeGetHandle(long nativeObject); private static native boolean nativeGetTransformToDisplayInverse(long nativeObject); private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken); private final CloseGuard mCloseGuard = CloseGuard.get(); private final String mName; long mNativeObject; // package visibility only for Surface.java access // TODO: Move this to native. private final Object mSizeLock = new Object(); @GuardedBy("mSizeLock") private int mWidth; @GuardedBy("mSizeLock") private int mHeight; static Transaction sGlobalTransaction; static long sTransactionNestCount = 0; /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */ /** * Surface creation flag: Surface is created hidden */ public static final int HIDDEN = 0x00000004; /** * Surface creation flag: The surface contains secure content, special * measures will be taken to disallow the surface's content to be copied * from another process. In particular, screenshots and VNC servers will * be disabled, but other measures can take place, for instance the * surface might not be hardware accelerated. * */ public static final int SECURE = 0x00000080; /** * Surface creation flag: Creates a surface where color components are interpreted * as "non pre-multiplied" by their alpha channel. Of course this flag is * meaningless for surfaces without an alpha channel. By default * surfaces are pre-multiplied, which means that each color component is * already multiplied by its alpha value. In this case the blending * equation used is: *
* DEST = SRC + DEST * (1-SRC_ALPHA)
*
* By contrast, non pre-multiplied surfaces use the following equation: *
* DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)
*
* pre-multiplied surfaces must always be used if transparent pixels are * composited on top of each-other into the surface. A pre-multiplied * surface can never lower the value of the alpha component of a given * pixel. *
* In some rare situations, a non pre-multiplied surface is preferable. * */ public static final int NON_PREMULTIPLIED = 0x00000100; /** * Surface creation flag: Indicates that the surface must be considered opaque, * even if its pixel format contains an alpha channel. This can be useful if an * application needs full RGBA 8888 support for instance but will * still draw every pixel opaque. *
* This flag is ignored if setAlpha() is used to make the surface non-opaque. * Combined effects are (assuming a buffer format with an alpha channel): *
* This flag only determines whether opacity will be sampled from the alpha channel. * Plane-alpha from calls to setAlpha() can still result in blended composition * regardless of the opaque setting. * * Combined effects are (assuming a buffer format with an alpha channel): *
* The surface creation flags specify what kind of surface to create and * certain options such as whether the surface can be assumed to be opaque * and whether it should be initially hidden. Surfaces should always be * created with the {@link #HIDDEN} flag set to ensure that they are not * made visible prematurely before all of the surface's properties have been * configured. *
* Good practice is to first create the surface with the {@link #HIDDEN} flag
* specified, open a transaction, set the surface layer, layer stack, alpha,
* and position, call {@link #show} if appropriate, and close the transaction.
*
* @param session The surface session, must not be null.
* @param name The surface name, must not be null.
* @param w The surface initial width.
* @param h The surface initial height.
* @param flags The surface creation flags. Should always include {@link #HIDDEN}
* in the creation flags.
* @param windowType The type of the window as specified in WindowManager.java.
* @param ownerUid A unique per-app ID.
*
* @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
*/
private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
SurfaceControl parent, int windowType, int ownerUid)
throws OutOfResourcesException, IllegalArgumentException {
if (session == null) {
throw new IllegalArgumentException("session must not be null");
}
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
if ((flags & SurfaceControl.HIDDEN) == 0) {
Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
+ "to ensure that they are not made visible prematurely before "
+ "all of the surface's properties have been configured. "
+ "Set the other properties and make the surface visible within "
+ "a transaction. New surface name: " + name,
new Throwable());
}
mName = name;
mWidth = w;
mHeight = h;
mNativeObject = nativeCreate(session, name, w, h, format, flags,
parent != null ? parent.mNativeObject : 0, windowType, ownerUid);
if (mNativeObject == 0) {
throw new OutOfResourcesException(
"Couldn't allocate SurfaceControl native object");
}
mCloseGuard.open("release");
}
// This is a transfer constructor, useful for transferring a live SurfaceControl native
// object to another Java wrapper which could have some different behavior, e.g.
// event logging.
public SurfaceControl(SurfaceControl other) {
mName = other.mName;
mWidth = other.mWidth;
mHeight = other.mHeight;
mNativeObject = other.mNativeObject;
other.mCloseGuard.close();
other.mNativeObject = 0;
mCloseGuard.open("release");
}
private SurfaceControl(Parcel in) {
mName = in.readString();
mWidth = in.readInt();
mHeight = in.readInt();
mNativeObject = nativeReadFromParcel(in);
if (mNativeObject == 0) {
throw new IllegalArgumentException("Couldn't read SurfaceControl from parcel=" + in);
}
mCloseGuard.open("release");
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeInt(mWidth);
dest.writeInt(mHeight);
nativeWriteToParcel(mNativeObject, dest);
}
/**
* Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
* android.view.SurfaceControlProto}.
*
* @param proto Stream to write the SurfaceControl object to.
* @param fieldId Field Id of the SurfaceControl as defined in the parent message.
* @hide
*/
public void writeToProto(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
proto.write(HASH_CODE, System.identityHashCode(this));
proto.write(NAME, mName);
proto.end(token);
}
public static final Creator
* This is a utility for interop with legacy-code and will go away with the Global Transaction.
*/
@Deprecated
public static void mergeToGlobalTransaction(Transaction t) {
synchronized(SurfaceControl.class) {
sGlobalTransaction.merge(t);
}
}
/** end a transaction */
public static void closeTransaction() {
closeTransaction(false);
}
public static void closeTransactionSync() {
closeTransaction(true);
}
public void deferTransactionUntil(IBinder handle, long frame) {
synchronized(SurfaceControl.class) {
sGlobalTransaction.deferTransactionUntil(this, handle, frame);
}
}
public void deferTransactionUntil(Surface barrier, long frame) {
synchronized(SurfaceControl.class) {
sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame);
}
}
public void reparentChildren(IBinder newParentHandle) {
synchronized(SurfaceControl.class) {
sGlobalTransaction.reparentChildren(this, newParentHandle);
}
}
public void reparent(IBinder newParentHandle) {
synchronized(SurfaceControl.class) {
sGlobalTransaction.reparent(this, newParentHandle);
}
}
public void detachChildren() {
synchronized(SurfaceControl.class) {
sGlobalTransaction.detachChildren(this);
}
}
public void setOverrideScalingMode(int scalingMode) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
}
}
public IBinder getHandle() {
return nativeGetHandle(mNativeObject);
}
public static void setAnimationTransaction() {
synchronized (SurfaceControl.class) {
sGlobalTransaction.setAnimationTransaction();
}
}
public void setLayer(int zorder) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setLayer(this, zorder);
}
}
public void setRelativeLayer(SurfaceControl relativeTo, int zorder) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder);
}
}
public void setPosition(float x, float y) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setPosition(this, x, y);
}
}
public void setGeometryAppliesWithResize() {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setGeometryAppliesWithResize(this);
}
}
public void setSize(int w, int h) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setSize(this, w, h);
}
}
public void hide() {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.hide(this);
}
}
public void show() {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.show(this);
}
}
public void setTransparentRegionHint(Region region) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setTransparentRegionHint(this, region);
}
}
public boolean clearContentFrameStats() {
checkNotReleased();
return nativeClearContentFrameStats(mNativeObject);
}
public boolean getContentFrameStats(WindowContentFrameStats outStats) {
checkNotReleased();
return nativeGetContentFrameStats(mNativeObject, outStats);
}
public static boolean clearAnimationFrameStats() {
return nativeClearAnimationFrameStats();
}
public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
return nativeGetAnimationFrameStats(outStats);
}
public void setAlpha(float alpha) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setAlpha(this, alpha);
}
}
public void setColor(@Size(3) float[] color) {
checkNotReleased();
synchronized (SurfaceControl.class) {
sGlobalTransaction.setColor(this, color);
}
}
public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
}
}
/**
* Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix.
*
* @param matrix The matrix to apply.
* @param float9 An array of 9 floats to be used to extract the values from the matrix.
*/
public void setMatrix(Matrix matrix, float[] float9) {
checkNotReleased();
matrix.getValues(float9);
synchronized (SurfaceControl.class) {
sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y],
float9[MSKEW_X], float9[MSCALE_Y]);
sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]);
}
}
public void setWindowCrop(Rect crop) {
checkNotReleased();
synchronized (SurfaceControl.class) {
sGlobalTransaction.setWindowCrop(this, crop);
}
}
public void setFinalCrop(Rect crop) {
checkNotReleased();
synchronized (SurfaceControl.class) {
sGlobalTransaction.setFinalCrop(this, crop);
}
}
public void setLayerStack(int layerStack) {
checkNotReleased();
synchronized(SurfaceControl.class) {
sGlobalTransaction.setLayerStack(this, layerStack);
}
}
public void setOpaque(boolean isOpaque) {
checkNotReleased();
synchronized (SurfaceControl.class) {
sGlobalTransaction.setOpaque(this, isOpaque);
}
}
public void setSecure(boolean isSecure) {
checkNotReleased();
synchronized (SurfaceControl.class) {
sGlobalTransaction.setSecure(this, isSecure);
}
}
public int getWidth() {
synchronized (mSizeLock) {
return mWidth;
}
}
public int getHeight() {
synchronized (mSizeLock) {
return mHeight;
}
}
@Override
public String toString() {
return "Surface(name=" + mName + ")/@0x" +
Integer.toHexString(System.identityHashCode(this));
}
/*
* set display parameters.
* needs to be inside open/closeTransaction block
*/
/**
* Describes the properties of a physical display known to surface flinger.
*/
public static final class PhysicalDisplayInfo {
public int width;
public int height;
public float refreshRate;
public float density;
public float xDpi;
public float yDpi;
public boolean secure;
public long appVsyncOffsetNanos;
public long presentationDeadlineNanos;
public PhysicalDisplayInfo() {
}
public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
copyFrom(other);
}
@Override
public boolean equals(Object o) {
return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
}
public boolean equals(PhysicalDisplayInfo other) {
return other != null
&& width == other.width
&& height == other.height
&& refreshRate == other.refreshRate
&& density == other.density
&& xDpi == other.xDpi
&& yDpi == other.yDpi
&& secure == other.secure
&& appVsyncOffsetNanos == other.appVsyncOffsetNanos
&& presentationDeadlineNanos == other.presentationDeadlineNanos;
}
@Override
public int hashCode() {
return 0; // don't care
}
public void copyFrom(PhysicalDisplayInfo other) {
width = other.width;
height = other.height;
refreshRate = other.refreshRate;
density = other.density;
xDpi = other.xDpi;
yDpi = other.yDpi;
secure = other.secure;
appVsyncOffsetNanos = other.appVsyncOffsetNanos;
presentationDeadlineNanos = other.presentationDeadlineNanos;
}
// For debugging purposes
@Override
public String toString() {
return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
+ "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
+ ", appVsyncOffset " + appVsyncOffsetNanos
+ ", bufferDeadline " + presentationDeadlineNanos + "}";
}
}
public static void setDisplayPowerMode(IBinder displayToken, int mode) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
nativeSetDisplayPowerMode(displayToken, mode);
}
public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeGetDisplayConfigs(displayToken);
}
public static int getActiveConfig(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeGetActiveConfig(displayToken);
}
public static boolean setActiveConfig(IBinder displayToken, int id) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeSetActiveConfig(displayToken, id);
}
public static int[] getDisplayColorModes(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeGetDisplayColorModes(displayToken);
}
public static int getActiveColorMode(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeGetActiveColorMode(displayToken);
}
public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeSetActiveColorMode(displayToken, colorMode);
}
public static void setDisplayProjection(IBinder displayToken,
int orientation, Rect layerStackRect, Rect displayRect) {
synchronized (SurfaceControl.class) {
sGlobalTransaction.setDisplayProjection(displayToken, orientation,
layerStackRect, displayRect);
}
}
public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
synchronized (SurfaceControl.class) {
sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
}
}
public static void setDisplaySurface(IBinder displayToken, Surface surface) {
synchronized (SurfaceControl.class) {
sGlobalTransaction.setDisplaySurface(displayToken, surface);
}
}
public static void setDisplaySize(IBinder displayToken, int width, int height) {
synchronized (SurfaceControl.class) {
sGlobalTransaction.setDisplaySize(displayToken, width, height);
}
}
public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
return nativeGetHdrCapabilities(displayToken);
}
public static IBinder createDisplay(String name, boolean secure) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
return nativeCreateDisplay(name, secure);
}
public static void destroyDisplay(IBinder displayToken) {
if (displayToken == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
nativeDestroyDisplay(displayToken);
}
public static IBinder getBuiltInDisplay(int builtInDisplayId) {
return nativeGetBuiltInDisplay(builtInDisplayId);
}
/**
* Copy the current screen contents into the provided {@link Surface}
*
* @param display The display to take the screenshot of.
* @param consumer The {@link Surface} to take the screenshot into.
* @param width The desired width of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param height The desired height of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param minLayer The lowest (bottom-most Z order) surface layer to
* include in the screenshot.
* @param maxLayer The highest (top-most Z order) surface layer to
* include in the screenshot.
* @param useIdentityTransform Replace whatever transformation (rotation,
* scaling, translation) the surface layers are currently using with the
* identity transformation while taking the screenshot.
*/
public static void screenshot(IBinder display, Surface consumer,
int width, int height, int minLayer, int maxLayer,
boolean useIdentityTransform) {
screenshot(display, consumer, new Rect(), width, height, minLayer, maxLayer,
false, useIdentityTransform);
}
/**
* Copy the current screen contents into the provided {@link Surface}
*
* @param display The display to take the screenshot of.
* @param consumer The {@link Surface} to take the screenshot into.
* @param width The desired width of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param height The desired height of the returned bitmap; the raw
* screen will be scaled down to this size.
*/
public static void screenshot(IBinder display, Surface consumer,
int width, int height) {
screenshot(display, consumer, new Rect(), width, height, 0, 0, true, false);
}
/**
* Copy the current screen contents into the provided {@link Surface}
*
* @param display The display to take the screenshot of.
* @param consumer The {@link Surface} to take the screenshot into.
*/
public static void screenshot(IBinder display, Surface consumer) {
screenshot(display, consumer, new Rect(), 0, 0, 0, 0, true, false);
}
/**
* Copy the current screen contents into a hardware bitmap and return it.
* Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
* a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
*
* CAVEAT: Versions of screenshot that return a {@link Bitmap} can
* be extremely slow; avoid use unless absolutely necessary; prefer
* the versions that use a {@link Surface} instead, such as
* {@link SurfaceControl#screenshot(IBinder, Surface)}.
*
* @param sourceCrop The portion of the screen to capture into the Bitmap;
* caller may pass in 'new Rect()' if no cropping is desired.
* @param width The desired width of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param height The desired height of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param minLayer The lowest (bottom-most Z order) surface layer to
* include in the screenshot.
* @param maxLayer The highest (top-most Z order) surface layer to
* include in the screenshot.
* @param useIdentityTransform Replace whatever transformation (rotation,
* scaling, translation) the surface layers are currently using with the
* identity transformation while taking the screenshot.
* @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
* Surface.ROTATION_0,90,180,270. Surfaceflinger will always take
* screenshots in its native portrait orientation by default, so this is
* useful for returning screenshots that are independent of device
* orientation.
* @return Returns a hardware Bitmap containing the screen contents, or null
* if an error occurs. Make sure to call Bitmap.recycle() as soon as
* possible, once its content is not needed anymore.
*/
public static Bitmap screenshot(Rect sourceCrop, int width, int height,
int minLayer, int maxLayer, boolean useIdentityTransform,
int rotation) {
// TODO: should take the display as a parameter
IBinder displayToken = SurfaceControl.getBuiltInDisplay(
SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
return nativeScreenshot(displayToken, sourceCrop, width, height,
minLayer, maxLayer, false, useIdentityTransform, rotation);
}
/**
* Like {@link SurfaceControl#screenshot(Rect, int, int, int, int, boolean, int)}
* but returns a GraphicBuffer.
*/
public static GraphicBuffer screenshotToBuffer(Rect sourceCrop, int width, int height,
int minLayer, int maxLayer, boolean useIdentityTransform,
int rotation) {
IBinder displayToken = SurfaceControl.getBuiltInDisplay(
SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
return nativeScreenshotToBuffer(displayToken, sourceCrop, width, height,
minLayer, maxLayer, false, useIdentityTransform, rotation);
}
/**
* Like {@link SurfaceControl#screenshot(Rect, int, int, int, int, boolean, int)} but
* includes all Surfaces in the screenshot. This will also update the orientation so it
* sends the correct coordinates to SF based on the rotation value.
*
* @param sourceCrop The portion of the screen to capture into the Bitmap;
* caller may pass in 'new Rect()' if no cropping is desired.
* @param width The desired width of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param height The desired height of the returned bitmap; the raw
* screen will be scaled down to this size.
* @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
* Surface.ROTATION_0,90,180,270. Surfaceflinger will always take
* screenshots in its native portrait orientation by default, so this is
* useful for returning screenshots that are independent of device
* orientation.
* @return Returns a Bitmap containing the screen contents, or null
* if an error occurs. Make sure to call Bitmap.recycle() as soon as
* possible, once its content is not needed anymore.
*/
public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
// TODO: should take the display as a parameter
IBinder displayToken = SurfaceControl.getBuiltInDisplay(
SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
if (rotation == ROTATION_90 || rotation == ROTATION_270) {
rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
}
SurfaceControl.rotateCropForSF(sourceCrop, rotation);
return nativeScreenshot(displayToken, sourceCrop, width, height, 0, 0, true,
false, rotation);
}
private static void screenshot(IBinder display, Surface consumer, Rect sourceCrop,
int width, int height, int minLayer, int maxLayer, boolean allLayers,
boolean useIdentityTransform) {
if (display == null) {
throw new IllegalArgumentException("displayToken must not be null");
}
if (consumer == null) {
throw new IllegalArgumentException("consumer must not be null");
}
nativeScreenshot(display, consumer, sourceCrop, width, height,
minLayer, maxLayer, allLayers, useIdentityTransform);
}
private static void rotateCropForSF(Rect crop, int rot) {
if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
int tmp = crop.top;
crop.top = crop.left;
crop.left = tmp;
tmp = crop.right;
crop.right = crop.bottom;
crop.bottom = tmp;
}
}
/**
* Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
*
* @param layerHandleToken The root layer to capture.
* @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
* Rect()' or null if no cropping is desired.
* @param frameScale The desired scale of the returned buffer; the raw
* screen will be scaled up/down.
*
* @return Returns a GraphicBuffer that contains the layer capture.
*/
public static GraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,
float frameScale) {
return nativeCaptureLayers(layerHandleToken, sourceCrop, frameScale);
}
public static class Transaction implements Closeable {
public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
Transaction.class.getClassLoader(),
nativeGetNativeTransactionFinalizer(), 512);
private long mNativeObject;
private final ArrayMap
* Corresponds to setting ISurfaceComposer::eEarlyWakeup
*/
public Transaction setEarlyWakeup() {
nativeSetEarlyWakeup(mNativeObject);
return this;
}
/**
* Merge the other transaction into this transaction, clearing the
* other transaction as if it had been applied.
*/
public Transaction merge(Transaction other) {
mResizedSurfaces.putAll(other.mResizedSurfaces);
other.mResizedSurfaces.clear();
nativeMergeTransaction(mNativeObject, other.mNativeObject);
return this;
}
}
}