diff options
29 files changed, 203 insertions, 241 deletions
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java index 556f84109ce6..40b0a5924e2f 100644 --- a/core/java/android/app/ActivityManager.java +++ b/core/java/android/app/ActivityManager.java @@ -51,6 +51,7 @@ import android.graphics.Matrix; import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Icon; +import android.hardware.HardwareBuffer; import android.os.BatteryStats; import android.os.Binder; import android.os.Build; @@ -2006,7 +2007,7 @@ public class ActivityManager { private final long mId; // Top activity in task when snapshot was taken private final ComponentName mTopActivityComponent; - private final GraphicBuffer mSnapshot; + private final HardwareBuffer mSnapshot; /** Indicates whether task was in landscape or portrait */ @Configuration.Orientation private final int mOrientation; @@ -2029,7 +2030,7 @@ public class ActivityManager { private final ColorSpace mColorSpace; public TaskSnapshot(long id, - @NonNull ComponentName topActivityComponent, GraphicBuffer snapshot, + @NonNull ComponentName topActivityComponent, HardwareBuffer snapshot, @NonNull ColorSpace colorSpace, int orientation, int rotation, Point taskSize, Rect contentInsets, boolean isLowResolution, boolean isRealSnapshot, int windowingMode, int systemUiVisibility, boolean isTranslucent) { @@ -2084,14 +2085,24 @@ public class ActivityManager { /** * @return The graphic buffer representing the screenshot. + * + * Note: Prefer {@link #getHardwareBuffer}, which returns the internal object. This version + * creates a new object. */ @UnsupportedAppUsage public GraphicBuffer getSnapshot() { + return GraphicBuffer.createFromHardwareBuffer(mSnapshot); + } + + /** + * @return The hardware buffer representing the screenshot. + */ + public HardwareBuffer getHardwareBuffer() { return mSnapshot; } /** - * @return The color space of graphic buffer representing the screenshot. + * @return The color space of hardware buffer representing the screenshot. */ public ColorSpace getColorSpace() { return mColorSpace; @@ -2224,7 +2235,7 @@ public class ActivityManager { public static final class Builder { private long mId; private ComponentName mTopActivity; - private GraphicBuffer mSnapshot; + private HardwareBuffer mSnapshot; private ColorSpace mColorSpace; private int mOrientation; private int mRotation; @@ -2246,7 +2257,7 @@ public class ActivityManager { return this; } - public Builder setSnapshot(GraphicBuffer buffer) { + public Builder setSnapshot(HardwareBuffer buffer) { mSnapshot = buffer; return this; } diff --git a/core/java/android/app/TaskStackListener.java b/core/java/android/app/TaskStackListener.java index 93772de0ba30..0ed2f3143745 100644 --- a/core/java/android/app/TaskStackListener.java +++ b/core/java/android/app/TaskStackListener.java @@ -155,9 +155,9 @@ public abstract class TaskStackListener extends ITaskStackListener.Stub { @UnsupportedAppUsage public void onTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) throws RemoteException { if (Binder.getCallingPid() != android.os.Process.myPid() - && snapshot != null && snapshot.getSnapshot() != null) { + && snapshot != null && snapshot.getHardwareBuffer() != null) { // Preemptively clear any reference to the buffer - snapshot.getSnapshot().destroy(); + snapshot.getHardwareBuffer().close(); } } diff --git a/core/java/android/hardware/display/DisplayManagerInternal.java b/core/java/android/hardware/display/DisplayManagerInternal.java index 1c42db9a3f8d..317e7ea34190 100644 --- a/core/java/android/hardware/display/DisplayManagerInternal.java +++ b/core/java/android/hardware/display/DisplayManagerInternal.java @@ -70,7 +70,7 @@ public abstract class DisplayManagerInternal { * @param displayId The display id to take the screenshot of. * @return The buffer or null if we have failed. */ - public abstract SurfaceControl.ScreenshotGraphicBuffer screenshot(int displayId); + public abstract SurfaceControl.ScreenshotHardwareBuffer screenshot(int displayId); /** * Returns information about the specified logical display. diff --git a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java index 306b4830932e..6411314f7542 100644 --- a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java +++ b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java @@ -31,7 +31,7 @@ import android.app.contentsuggestions.SelectionsRequest; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -62,7 +62,7 @@ public abstract class ContentSuggestionsService extends Service { private final IContentSuggestionsService mInterface = new IContentSuggestionsService.Stub() { @Override - public void provideContextImage(int taskId, GraphicBuffer contextImage, + public void provideContextImage(int taskId, HardwareBuffer contextImage, int colorSpaceId, Bundle imageContextRequestExtras) { if (imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP) && contextImage != null) { diff --git a/core/java/android/service/contentsuggestions/IContentSuggestionsService.aidl b/core/java/android/service/contentsuggestions/IContentSuggestionsService.aidl index 6240e00794bd..d8f23e78dd65 100644 --- a/core/java/android/service/contentsuggestions/IContentSuggestionsService.aidl +++ b/core/java/android/service/contentsuggestions/IContentSuggestionsService.aidl @@ -20,7 +20,7 @@ import android.app.contentsuggestions.IClassificationsCallback; import android.app.contentsuggestions.ISelectionsCallback; import android.app.contentsuggestions.ClassificationsRequest; import android.app.contentsuggestions.SelectionsRequest; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.os.Bundle; /** @@ -31,7 +31,7 @@ import android.os.Bundle; oneway interface IContentSuggestionsService { void provideContextImage( int taskId, - in GraphicBuffer contextImage, + in HardwareBuffer contextImage, int colorSpaceId, in Bundle imageContextRequestExtras); void suggestContentSelections( diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java index c87808b113f6..8ce8cc9d2d1a 100644 --- a/core/java/android/view/SurfaceControl.java +++ b/core/java/android/view/SurfaceControl.java @@ -35,12 +35,12 @@ import android.annotation.Size; import android.compat.annotation.UnsupportedAppUsage; import android.graphics.Bitmap; import android.graphics.ColorSpace; -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.hardware.HardwareBuffer; import android.hardware.display.DeviceProductInfo; import android.hardware.display.DisplayedContentSample; import android.hardware.display.DisplayedContentSamplingAttributes; @@ -87,10 +87,10 @@ public final class SurfaceControl implements Parcelable { private static native void nativeRelease(long nativeObject); private static native void nativeDisconnect(long nativeObject); - private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken, + private static native ScreenshotHardwareBuffer nativeScreenshot(IBinder displayToken, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation, boolean captureSecureLayers); - private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken, + private static native ScreenshotHardwareBuffer nativeCaptureLayers(IBinder displayToken, long layerObject, Rect sourceCrop, float frameScale, long[] excludeLayerObjects, int format); private static native long nativeMirrorSurface(long mirrorOfObject); @@ -467,48 +467,42 @@ public final class SurfaceControl implements Parcelable { public static final int METADATA_ACCESSIBILITY_ID = 5; /** - * A wrapper around GraphicBuffer that contains extra information about how to - * interpret the screenshot GraphicBuffer. + * A wrapper around HardwareBuffer that contains extra information about how to + * interpret the screenshot HardwareBuffer. + * * @hide */ - public static class ScreenshotGraphicBuffer { - private final GraphicBuffer mGraphicBuffer; + public static class ScreenshotHardwareBuffer { + private final HardwareBuffer mHardwareBuffer; private final ColorSpace mColorSpace; private final boolean mContainsSecureLayers; - public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace, + public ScreenshotHardwareBuffer(HardwareBuffer hardwareBuffer, ColorSpace colorSpace, boolean containsSecureLayers) { - mGraphicBuffer = graphicBuffer; + mHardwareBuffer = hardwareBuffer; mColorSpace = colorSpace; mContainsSecureLayers = containsSecureLayers; } /** - * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object. - * @param width The width in pixels of the buffer - * @param height The height in pixels of the buffer - * @param format The format of each pixel as specified in {@link PixelFormat} - * @param usage Hint indicating how the buffer will be used - * @param unwrappedNativeObject The native object of GraphicBuffer + * Create ScreenshotHardwareBuffer from an existing HardwareBuffer object. + * @param hardwareBuffer The existing HardwareBuffer object * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named} * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents * of secure layers, in which case the screenshot should not be persisted. */ - private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format, - int usage, long unwrappedNativeObject, int namedColorSpace, - boolean containsSecureLayers) { - GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format, - usage, unwrappedNativeObject); + private static ScreenshotHardwareBuffer createFromNative(HardwareBuffer hardwareBuffer, + int namedColorSpace, boolean containsSecureLayers) { ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]); - return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers); + return new ScreenshotHardwareBuffer(hardwareBuffer, colorSpace, containsSecureLayers); } public ColorSpace getColorSpace() { return mColorSpace; } - public GraphicBuffer getGraphicBuffer() { - return mGraphicBuffer; + public HardwareBuffer getHardwareBuffer() { + return mHardwareBuffer; } public boolean containsSecureLayers() { @@ -1792,10 +1786,10 @@ public final class SurfaceControl implements Parcelable { throw new IllegalArgumentException("consumer must not be null"); } - final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width, + final ScreenshotHardwareBuffer buffer = screenshotToBuffer(display, sourceCrop, width, height, useIdentityTransform, rotation); try { - consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(), + consumer.attachAndQueueBufferWithColorSpace(buffer.getHardwareBuffer(), buffer.getColorSpace()); } catch (RuntimeException e) { Log.w(TAG, "Failed to take screenshot - " + e.getMessage()); @@ -1818,7 +1812,7 @@ public final class SurfaceControl implements Parcelable { * * 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} such as - * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as + * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link HardwareBuffer} such as * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}. * * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)} @@ -1839,18 +1833,18 @@ public final class SurfaceControl implements Parcelable { } SurfaceControl.rotateCropForSF(sourceCrop, rotation); - final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width, + final ScreenshotHardwareBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width, height, useIdentityTransform, rotation); if (buffer == null) { Log.w(TAG, "Failed to take screenshot"); return null; } - return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace()); + return Bitmap.wrapHardwareBuffer(buffer.getHardwareBuffer(), buffer.getColorSpace()); } /** - * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content. + * Captures all the surfaces in a display and returns a {@link HardwareBuffer} with the content. * * @param display The display to take the screenshot of. * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may @@ -1869,10 +1863,10 @@ public final class SurfaceControl implements Parcelable { * screenshots in its native portrait orientation by default, so * this is useful for returning screenshots that are independent of * device orientation. - * @return Returns a GraphicBuffer that contains the captured content. + * @return Returns a HardwareBuffer that contains the captured content. * @hide */ - public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop, + public static ScreenshotHardwareBuffer screenshotToBuffer(IBinder display, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation) { if (display == null) { throw new IllegalArgumentException("displayToken must not be null"); @@ -1892,7 +1886,7 @@ public final class SurfaceControl implements Parcelable { * * @hide */ - public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display, + public static ScreenshotHardwareBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation) { if (display == null) { @@ -1915,7 +1909,7 @@ public final class SurfaceControl implements Parcelable { } /** - * Captures a layer and its children and returns a {@link GraphicBuffer} with the content. + * Captures a layer and its children and returns a {@link HardwareBuffer} with the content. * * @param layer The root layer to capture. * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new @@ -1925,16 +1919,16 @@ public final class SurfaceControl implements Parcelable { * @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. + * @return Returns a HardwareBuffer that contains the layer capture. * @hide */ - public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop, + public static ScreenshotHardwareBuffer captureLayers(SurfaceControl layer, Rect sourceCrop, float frameScale) { return captureLayers(layer, sourceCrop, frameScale, PixelFormat.RGBA_8888); } /** - * Captures a layer and its children and returns a {@link GraphicBuffer} with the content. + * Captures a layer and its children and returns a {@link HardwareBuffer} with the content. * * @param layer The root layer to capture. * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new @@ -1945,10 +1939,10 @@ public final class SurfaceControl implements Parcelable { * screen will be scaled up/down. * @param format The desired pixel format of the returned buffer. * - * @return Returns a GraphicBuffer that contains the layer capture. + * @return Returns a HardwareBuffer that contains the layer capture. * @hide */ - public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop, + public static ScreenshotHardwareBuffer captureLayers(SurfaceControl layer, Rect sourceCrop, float frameScale, int format) { final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale, null, @@ -1959,8 +1953,8 @@ public final class SurfaceControl implements Parcelable { * Like {@link captureLayers} but with an array of layer handles to exclude. * @hide */ - public static ScreenshotGraphicBuffer captureLayersExcluding(SurfaceControl layer, - Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) { + public static ScreenshotHardwareBuffer captureLayersExcluding(SurfaceControl layer, + Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) { final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); long[] nativeExcludeObjects = new long[exclude.length]; for (int i = 0; i < exclude.length; i++) { diff --git a/core/jni/android_graphics_GraphicBuffer.cpp b/core/jni/android_graphics_GraphicBuffer.cpp index 25a733234313..d5765f1907d5 100644 --- a/core/jni/android_graphics_GraphicBuffer.cpp +++ b/core/jni/android_graphics_GraphicBuffer.cpp @@ -104,15 +104,6 @@ private: // GraphicBuffer lifecycle // ---------------------------------------------------------------------------- -static jlong android_graphics_GraphicBuffer_wrap(JNIEnv* env, jobject clazz, - jlong unwrapped) { - sp<GraphicBuffer> b(reinterpret_cast<GraphicBuffer*>(unwrapped)); - LOG_ALWAYS_FATAL_IF(b == nullptr, - "*** android_graphics_GraphicBuffer_wrap() invalid state, b is null, unwrapped=%#" PRIx64, unwrapped); - GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(b); - return reinterpret_cast<jlong>(wrapper); -} - static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz, jint width, jint height, jint format, jint usage) { @@ -293,8 +284,6 @@ static const JNINativeMethod gMethods[] = { (void*) android_graphics_GraphicBuffer_lockCanvas }, { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z", (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost }, - { "nWrapGraphicBuffer", "(J)J", - (void*) android_graphics_GraphicBuffer_wrap }, { "nCreateFromHardwareBuffer", "(Landroid/hardware/HardwareBuffer;)Landroid/graphics/GraphicBuffer;", (void*) android_graphics_GraphicBuffer_createFromHardwareBuffer } diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp index b2ca0a7bcbe3..9e1d3df2e112 100644 --- a/core/jni/android_view_SurfaceControl.cpp +++ b/core/jni/android_view_SurfaceControl.cpp @@ -27,6 +27,7 @@ #include <android-base/chrono_utils.h> #include <android/graphics/region.h> #include <android_runtime/AndroidRuntime.h> +#include <android_runtime/android_hardware_HardwareBuffer.h> #include <android_runtime/android_view_Surface.h> #include <android_runtime/android_view_SurfaceSession.h> #include <gui/Surface.h> @@ -133,11 +134,6 @@ static struct { static struct { jclass clazz; - jmethodID builder; -} gGraphicBufferClassInfo; - -static struct { - jclass clazz; jmethodID ctor; } gDisplayedContentSampleClassInfo; @@ -166,7 +162,7 @@ static struct { static struct { jclass clazz; jmethodID builder; -} gScreenshotGraphicBufferClassInfo; +} gScreenshotHardwareBufferClassInfo; static struct { jclass clazz; @@ -298,16 +294,13 @@ static jobject nativeScreenshot(JNIEnv* env, jclass clazz, return NULL; } + jobject jhardwareBuffer = + android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, + buffer->toAHardwareBuffer()); const jint namedColorSpace = fromDataspaceToNamedColorSpaceValue(dataspace); - return env->CallStaticObjectMethod(gScreenshotGraphicBufferClassInfo.clazz, - gScreenshotGraphicBufferClassInfo.builder, - buffer->getWidth(), - buffer->getHeight(), - buffer->getPixelFormat(), - (jint)buffer->getUsage(), - (jlong)buffer.get(), - namedColorSpace, - capturedSecureLayers); + return env->CallStaticObjectMethod(gScreenshotHardwareBufferClassInfo.clazz, + gScreenshotHardwareBufferClassInfo.builder, jhardwareBuffer, + namedColorSpace, capturedSecureLayers); } static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject displayTokenObj, @@ -356,16 +349,13 @@ static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject displayTok return NULL; } + jobject jhardwareBuffer = + android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, + buffer->toAHardwareBuffer()); const jint namedColorSpace = fromDataspaceToNamedColorSpaceValue(dataspace); - return env->CallStaticObjectMethod(gScreenshotGraphicBufferClassInfo.clazz, - gScreenshotGraphicBufferClassInfo.builder, - buffer->getWidth(), - buffer->getHeight(), - buffer->getPixelFormat(), - (jint)buffer->getUsage(), - (jlong)buffer.get(), - namedColorSpace, - false /* capturedSecureLayers */); + return env->CallStaticObjectMethod(gScreenshotHardwareBufferClassInfo.clazz, + gScreenshotHardwareBufferClassInfo.builder, jhardwareBuffer, + namedColorSpace, false /* capturedSecureLayers */); } static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) { @@ -1548,12 +1538,12 @@ static const JNINativeMethod sSurfaceControlMethods[] = { (void*)nativeSetOverrideScalingMode }, {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZIZ)" - "Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;", + "Landroid/view/SurfaceControl$ScreenshotHardwareBuffer;", (void*)nativeScreenshot }, {"nativeCaptureLayers", "(Landroid/os/IBinder;JLandroid/graphics/Rect;" "F[JI)" - "Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;", + "Landroid/view/SurfaceControl$ScreenshotHardwareBuffer;", (void*)nativeCaptureLayers }, {"nativeSetInputWindowInfo", "(JJLandroid/view/InputWindowHandle;)V", (void*)nativeSetInputWindowInfo }, @@ -1663,18 +1653,14 @@ int register_android_view_SurfaceControl(JNIEnv* env) GetMethodIDOrDie(env, deviceProductInfoManufactureDateClazz, "<init>", "(Ljava/lang/Integer;Ljava/lang/Integer;)V"); - jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer"); - gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz); - gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz, - "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;"); - - jclass screenshotGraphicsBufferClazz = FindClassOrDie(env, - "android/view/SurfaceControl$ScreenshotGraphicBuffer"); - gScreenshotGraphicBufferClassInfo.clazz = + jclass screenshotGraphicsBufferClazz = + FindClassOrDie(env, "android/view/SurfaceControl$ScreenshotHardwareBuffer"); + gScreenshotHardwareBufferClassInfo.clazz = MakeGlobalRefOrDie(env, screenshotGraphicsBufferClazz); - gScreenshotGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, - screenshotGraphicsBufferClazz, - "createFromNative", "(IIIIJIZ)Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;"); + gScreenshotHardwareBufferClassInfo.builder = + GetStaticMethodIDOrDie(env, screenshotGraphicsBufferClazz, "createFromNative", + "(Landroid/hardware/HardwareBuffer;IZ)Landroid/view/" + "SurfaceControl$ScreenshotHardwareBuffer;"); jclass displayedContentSampleClazz = FindClassOrDie(env, "android/hardware/display/DisplayedContentSample"); diff --git a/graphics/java/android/graphics/GraphicBuffer.java b/graphics/java/android/graphics/GraphicBuffer.java index 0430847857b6..2c25f4546771 100644 --- a/graphics/java/android/graphics/GraphicBuffer.java +++ b/graphics/java/android/graphics/GraphicBuffer.java @@ -97,20 +97,6 @@ public class GraphicBuffer implements Parcelable { } /** - * For SurfaceControl JNI. - * @hide - */ - @UnsupportedAppUsage - public static GraphicBuffer createFromExisting(int width, int height, - int format, int usage, long unwrappedNativeObject) { - long nativeObject = nWrapGraphicBuffer(unwrappedNativeObject); - if (nativeObject != 0) { - return new GraphicBuffer(width, height, format, usage, nativeObject); - } - return null; - } - - /** * For Bitmap until all usages are updated to AHB * @hide */ @@ -313,6 +299,5 @@ public class GraphicBuffer implements Parcelable { private static native long nReadGraphicBufferFromParcel(Parcel in); private static native boolean nLockCanvas(long nativeObject, Canvas canvas, Rect dirty); private static native boolean nUnlockCanvasAndPost(long nativeObject, Canvas canvas); - private static native long nWrapGraphicBuffer(long nativeObject); private static native GraphicBuffer nCreateFromHardwareBuffer(HardwareBuffer buffer); } diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java index eca6ebf7f8e5..6bf3d11459df 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java +++ b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java @@ -57,7 +57,8 @@ public class ThumbnailData { } public ThumbnailData(TaskSnapshot snapshot) { - thumbnail = Bitmap.wrapHardwareBuffer(snapshot.getSnapshot(), snapshot.getColorSpace()); + thumbnail = Bitmap.wrapHardwareBuffer(snapshot.getHardwareBuffer(), + snapshot.getColorSpace()); insets = new Rect(snapshot.getContentInsets()); orientation = snapshot.getOrientation(); rotation = snapshot.getRotation(); diff --git a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java index c8b6d8dfd1b7..a050d8c1be7c 100644 --- a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java +++ b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java @@ -41,7 +41,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; -import android.graphics.GraphicBuffer; import android.graphics.ParcelableColorSpace; import android.graphics.Point; import android.graphics.Rect; @@ -67,7 +66,7 @@ import android.view.Display; import android.view.KeyEvent; import android.view.MagnificationSpec; import android.view.SurfaceControl; -import android.view.SurfaceControl.ScreenshotGraphicBuffer; +import android.view.SurfaceControl.ScreenshotHardwareBuffer; import android.view.View; import android.view.WindowInfo; import android.view.accessibility.AccessibilityCache; @@ -1021,7 +1020,7 @@ abstract class AbstractAccessibilityServiceConnection extends IAccessibilityServ sendScreenshotSuccess(display, callback); } - private ScreenshotGraphicBuffer takeScreenshotBuffer(Display display) { + private ScreenshotHardwareBuffer takeScreenshotBuffer(Display display) { final Point displaySize = new Point(); // TODO (b/145893483): calling new API with the display as a parameter // when surface control supported. @@ -1038,26 +1037,22 @@ abstract class AbstractAccessibilityServiceConnection extends IAccessibilityServ final long identity = Binder.clearCallingIdentity(); try { mMainHandler.post(PooledLambda.obtainRunnable((nonArg) -> { - final ScreenshotGraphicBuffer screenshotBuffer = takeScreenshotBuffer(display); - final GraphicBuffer graphicBuffer = screenshotBuffer.getGraphicBuffer(); - try (HardwareBuffer hardwareBuffer = - HardwareBuffer.createFromGraphicBuffer(graphicBuffer)) { - final ParcelableColorSpace colorSpace = - new ParcelableColorSpace(screenshotBuffer.getColorSpace()); - - final Bundle payload = new Bundle(); - payload.putInt(KEY_ACCESSIBILITY_SCREENSHOT_STATUS, - AccessibilityService.TAKE_SCREENSHOT_SUCCESS); - payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER, - hardwareBuffer); - payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE, colorSpace); - payload.putLong(KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP, - SystemClock.uptimeMillis()); - - // Send back the result. - callback.sendResult(payload); - hardwareBuffer.close(); - } + final ScreenshotHardwareBuffer screenshotBuffer = takeScreenshotBuffer(display); + final HardwareBuffer hardwareBuffer = screenshotBuffer.getHardwareBuffer(); + final ParcelableColorSpace colorSpace = + new ParcelableColorSpace(screenshotBuffer.getColorSpace()); + + final Bundle payload = new Bundle(); + payload.putInt(KEY_ACCESSIBILITY_SCREENSHOT_STATUS, + AccessibilityService.TAKE_SCREENSHOT_SUCCESS); + payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER, + hardwareBuffer); + payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE, colorSpace); + payload.putLong(KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP, + SystemClock.uptimeMillis()); + + // Send back the result. + callback.sendResult(payload); }, null).recycleOnUse()); } finally { Binder.restoreCallingIdentity(identity); diff --git a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsManagerService.java b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsManagerService.java index 0fdabd09f9d4..86ea1e44351a 100644 --- a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsManagerService.java +++ b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsManagerService.java @@ -31,7 +31,7 @@ import android.app.contentsuggestions.SelectionsRequest; import android.content.Context; import android.graphics.Bitmap; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.os.Binder; import android.os.Bundle; import android.os.RemoteException; @@ -156,7 +156,7 @@ public class ContentSuggestionsManagerService extends } enforceCaller(UserHandle.getCallingUserId(), "provideContextImage"); - GraphicBuffer snapshotBuffer = null; + HardwareBuffer snapshotBuffer = null; int colorSpaceId = 0; // Skip taking TaskSnapshot when bitmap is provided. @@ -165,7 +165,7 @@ public class ContentSuggestionsManagerService extends ActivityManager.TaskSnapshot snapshot = mActivityTaskManagerInternal.getTaskSnapshotBlocking(taskId, false); if (snapshot != null) { - snapshotBuffer = snapshot.getSnapshot(); + snapshotBuffer = snapshot.getHardwareBuffer(); ColorSpace colorSpace = snapshot.getColorSpace(); if (colorSpace != null) { colorSpaceId = colorSpace.getId(); diff --git a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java index cf53b169515e..c8588e2d98e6 100644 --- a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java +++ b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java @@ -27,7 +27,7 @@ import android.app.contentsuggestions.SelectionsRequest; import android.content.ComponentName; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.os.Bundle; import android.os.RemoteException; import android.util.Slog; @@ -98,7 +98,7 @@ public final class ContentSuggestionsPerUserService extends } @GuardedBy("mLock") - void provideContextImageLocked(int taskId, @Nullable GraphicBuffer snapshot, + void provideContextImageLocked(int taskId, @Nullable HardwareBuffer snapshot, int colorSpaceIdForSnapshot, @NonNull Bundle imageContextRequestExtras) { RemoteContentSuggestionsService service = ensureRemoteServiceLocked(); if (service != null) { diff --git a/services/contentsuggestions/java/com/android/server/contentsuggestions/RemoteContentSuggestionsService.java b/services/contentsuggestions/java/com/android/server/contentsuggestions/RemoteContentSuggestionsService.java index a8b7b814a9ba..6e0a0da76d05 100644 --- a/services/contentsuggestions/java/com/android/server/contentsuggestions/RemoteContentSuggestionsService.java +++ b/services/contentsuggestions/java/com/android/server/contentsuggestions/RemoteContentSuggestionsService.java @@ -24,7 +24,7 @@ import android.app.contentsuggestions.ISelectionsCallback; import android.app.contentsuggestions.SelectionsRequest; import android.content.ComponentName; import android.content.Context; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.os.Bundle; import android.os.IBinder; import android.service.contentsuggestions.ContentSuggestionsService; @@ -67,7 +67,7 @@ public class RemoteContentSuggestionsService extends return TIMEOUT_REMOTE_REQUEST_MILLIS; } - void provideContextImage(int taskId, @Nullable GraphicBuffer contextImage, + void provideContextImage(int taskId, @Nullable HardwareBuffer contextImage, int colorSpaceId, @NonNull Bundle imageContextRequestExtras) { scheduleAsyncRequest((s) -> s.provideContextImage(taskId, contextImage, colorSpaceId, imageContextRequestExtras)); diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java index 677555659186..b2049a8c54aa 100644 --- a/services/core/java/com/android/server/display/DisplayManagerService.java +++ b/services/core/java/com/android/server/display/DisplayManagerService.java @@ -1361,7 +1361,7 @@ public final class DisplayManagerService extends SystemService { return null; } - private SurfaceControl.ScreenshotGraphicBuffer screenshotInternal(int displayId) { + private SurfaceControl.ScreenshotHardwareBuffer screenshotInternal(int displayId) { synchronized (mSyncRoot) { final IBinder token = getDisplayToken(displayId); if (token == null) { @@ -2473,7 +2473,7 @@ public final class DisplayManagerService extends SystemService { } @Override - public SurfaceControl.ScreenshotGraphicBuffer screenshot(int displayId) { + public SurfaceControl.ScreenshotHardwareBuffer screenshot(int displayId) { return screenshotInternal(displayId); } diff --git a/services/core/java/com/android/server/wm/RecentsAnimationController.java b/services/core/java/com/android/server/wm/RecentsAnimationController.java index 3e5cb50d6101..41a6c6ac8ef7 100644 --- a/services/core/java/com/android/server/wm/RecentsAnimationController.java +++ b/services/core/java/com/android/server/wm/RecentsAnimationController.java @@ -610,7 +610,7 @@ public class RecentsAnimationController implements DeathRecipient { } final TaskScreenshotAnimatable animatable = new TaskScreenshotAnimatable(mService.mSurfaceControlFactory, task, - new SurfaceControl.ScreenshotGraphicBuffer(taskSnapshot.getSnapshot(), + new SurfaceControl.ScreenshotHardwareBuffer(taskSnapshot.getHardwareBuffer(), taskSnapshot.getColorSpace(), false /* containsSecureLayers */)); mRecentScreenshotAnimator = new SurfaceAnimator( animatable, diff --git a/services/core/java/com/android/server/wm/ScreenRotationAnimation.java b/services/core/java/com/android/server/wm/ScreenRotationAnimation.java index b92ead1a0531..9cbdb4b10785 100644 --- a/services/core/java/com/android/server/wm/ScreenRotationAnimation.java +++ b/services/core/java/com/android/server/wm/ScreenRotationAnimation.java @@ -205,24 +205,24 @@ class ScreenRotationAnimation { final int displayId = display.getDisplayId(); final Surface surface = mService.mSurfaceFactory.get(); surface.copyFrom(mScreenshotLayer); - SurfaceControl.ScreenshotGraphicBuffer gb = + SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer = mService.mDisplayManagerInternal.screenshot(displayId); - if (gb != null) { + if (screenshotBuffer != null) { Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "ScreenRotationAnimation#getMedianBorderLuma"); - mStartLuma = RotationAnimationUtils.getMedianBorderLuma(gb.getGraphicBuffer(), - gb.getColorSpace()); + mStartLuma = RotationAnimationUtils.getMedianBorderLuma( + screenshotBuffer.getHardwareBuffer(), screenshotBuffer.getColorSpace()); Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER); try { - surface.attachAndQueueBufferWithColorSpace(gb.getGraphicBuffer(), - gb.getColorSpace()); + surface.attachAndQueueBufferWithColorSpace(screenshotBuffer.getHardwareBuffer(), + screenshotBuffer.getColorSpace()); } catch (RuntimeException e) { Slog.w(TAG, "Failed to attach screenshot - " + e.getMessage()); } // If the screenshot contains secure layers, we have to make sure the // screenshot surface we display it in also has FLAG_SECURE so that // the user can not screenshot secure layers via the screenshot surface. - if (gb.containsSecureLayers()) { + if (screenshotBuffer.containsSecureLayers()) { t.setSecure(mScreenshotLayer, true); } t.setLayer(mScreenshotLayer, SCREEN_FREEZE_LAYER_BASE); diff --git a/services/core/java/com/android/server/wm/SurfaceFreezer.java b/services/core/java/com/android/server/wm/SurfaceFreezer.java index 8ab5043dc5d9..33970c73011d 100644 --- a/services/core/java/com/android/server/wm/SurfaceFreezer.java +++ b/services/core/java/com/android/server/wm/SurfaceFreezer.java @@ -22,9 +22,10 @@ import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_SCREEN_ROTATI import android.annotation.NonNull; import android.annotation.Nullable; -import android.graphics.GraphicBuffer; +import android.graphics.ColorSpace; import android.graphics.PixelFormat; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.view.Surface; import android.view.SurfaceControl; @@ -81,10 +82,15 @@ class SurfaceFreezer { SurfaceControl freezeTarget = mAnimatable.getFreezeSnapshotTarget(); if (freezeTarget != null) { - GraphicBuffer snapshot = createSnapshotBuffer(freezeTarget, startBounds); - if (snapshot != null) { - mSnapshot = new Snapshot(mWmService.mSurfaceFactory, t, snapshot, mLeash); + SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer = createSnapshotBuffer( + freezeTarget, startBounds); + final HardwareBuffer buffer = screenshotBuffer == null ? null + : screenshotBuffer.getHardwareBuffer(); + if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) { + return; } + mSnapshot = new Snapshot(mWmService.mSurfaceFactory, t, buffer, + screenshotBuffer.getColorSpace(), mLeash); } } @@ -122,22 +128,15 @@ class SurfaceFreezer { return mLeash != null; } - private static GraphicBuffer createSnapshotBuffer(@NonNull SurfaceControl target, - @Nullable Rect bounds) { + private static SurfaceControl.ScreenshotHardwareBuffer createSnapshotBuffer( + @NonNull SurfaceControl target, @Nullable Rect bounds) { Rect cropBounds = null; if (bounds != null) { cropBounds = new Rect(bounds); cropBounds.offsetTo(0, 0); } - final SurfaceControl.ScreenshotGraphicBuffer screenshotBuffer = - SurfaceControl.captureLayers( - target, cropBounds, 1.f /* frameScale */, PixelFormat.RGBA_8888); - final GraphicBuffer buffer = screenshotBuffer != null ? screenshotBuffer.getGraphicBuffer() - : null; - if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) { - return null; - } - return buffer; + return SurfaceControl.captureLayers(target, cropBounds, 1.f /* frameScale */, + PixelFormat.RGBA_8888); } class Snapshot { @@ -150,7 +149,7 @@ class SurfaceFreezer { * @param thumbnailHeader A thumbnail or placeholder for thumbnail to initialize with. */ Snapshot(Supplier<Surface> surfaceFactory, SurfaceControl.Transaction t, - GraphicBuffer thumbnailHeader, SurfaceControl parent) { + HardwareBuffer thumbnailHeader, ColorSpace colorSpace, SurfaceControl parent) { Surface drawSurface = surfaceFactory.get(); // We can't use a delegating constructor since we need to // reference this::onAnimationFinished @@ -168,7 +167,7 @@ class SurfaceFreezer { // Transfer the thumbnail to the surface drawSurface.copyFrom(mSurfaceControl); - drawSurface.attachAndQueueBuffer(thumbnailHeader); + drawSurface.attachAndQueueBufferWithColorSpace(thumbnailHeader, colorSpace); drawSurface.release(); t.show(mSurfaceControl); diff --git a/services/core/java/com/android/server/wm/TaskScreenshotAnimatable.java b/services/core/java/com/android/server/wm/TaskScreenshotAnimatable.java index adecc36671c9..1424c8d13252 100644 --- a/services/core/java/com/android/server/wm/TaskScreenshotAnimatable.java +++ b/services/core/java/com/android/server/wm/TaskScreenshotAnimatable.java @@ -17,7 +17,7 @@ package com.android.server.wm; import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_RECENTS_ANIMATIONS; -import android.graphics.GraphicBuffer; +import android.hardware.HardwareBuffer; import android.view.Surface; import android.view.SurfaceControl; import android.view.SurfaceSession; @@ -40,9 +40,9 @@ class TaskScreenshotAnimatable implements SurfaceAnimator.Animatable { private int mHeight; TaskScreenshotAnimatable(Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory, - Task task, SurfaceControl.ScreenshotGraphicBuffer screenshotBuffer) { - GraphicBuffer buffer = screenshotBuffer == null - ? null : screenshotBuffer.getGraphicBuffer(); + Task task, SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer) { + HardwareBuffer buffer = screenshotBuffer == null + ? null : screenshotBuffer.getHardwareBuffer(); mTask = task; mWidth = (buffer != null) ? buffer.getWidth() : 1; mHeight = (buffer != null) ? buffer.getHeight() : 1; diff --git a/services/core/java/com/android/server/wm/TaskSnapshotController.java b/services/core/java/com/android/server/wm/TaskSnapshotController.java index 0f5cafe9e4e6..63b3e40eff94 100644 --- a/services/core/java/com/android/server/wm/TaskSnapshotController.java +++ b/services/core/java/com/android/server/wm/TaskSnapshotController.java @@ -27,12 +27,12 @@ import android.annotation.Nullable; import android.app.ActivityManager.TaskSnapshot; import android.content.pm.PackageManager; import android.graphics.Bitmap; -import android.graphics.GraphicBuffer; import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.RecordingCanvas; import android.graphics.Rect; import android.graphics.RenderNode; +import android.hardware.HardwareBuffer; import android.os.Environment; import android.os.Handler; import android.util.ArraySet; @@ -196,9 +196,9 @@ class TaskSnapshotController { } } if (snapshot != null) { - final GraphicBuffer buffer = snapshot.getSnapshot(); + final HardwareBuffer buffer = snapshot.getHardwareBuffer(); if (buffer.getWidth() == 0 || buffer.getHeight() == 0) { - buffer.destroy(); + buffer.close(); Slog.e(TAG, "Invalid task snapshot dimensions " + buffer.getWidth() + "x" + buffer.getHeight()); } else { @@ -326,23 +326,23 @@ class TaskSnapshotController { } @Nullable - SurfaceControl.ScreenshotGraphicBuffer createTaskSnapshot(@NonNull Task task, + SurfaceControl.ScreenshotHardwareBuffer createTaskSnapshot(@NonNull Task task, TaskSnapshot.Builder builder) { Point taskSize = new Point(); - final SurfaceControl.ScreenshotGraphicBuffer taskSnapshot = createTaskSnapshot(task, + final SurfaceControl.ScreenshotHardwareBuffer taskSnapshot = createTaskSnapshot(task, mHighResTaskSnapshotScale, builder.getPixelFormat(), taskSize); builder.setTaskSize(taskSize); return taskSnapshot; } @Nullable - SurfaceControl.ScreenshotGraphicBuffer createTaskSnapshot(@NonNull Task task, + SurfaceControl.ScreenshotHardwareBuffer createTaskSnapshot(@NonNull Task task, float scaleFraction) { return createTaskSnapshot(task, scaleFraction, PixelFormat.RGBA_8888, null); } @Nullable - SurfaceControl.ScreenshotGraphicBuffer createTaskSnapshot(@NonNull Task task, + SurfaceControl.ScreenshotHardwareBuffer createTaskSnapshot(@NonNull Task task, float scaleFraction, int pixelFormat, Point outTaskSize) { if (task.getSurfaceControl() == null) { if (DEBUG_SCREENSHOT) { @@ -361,7 +361,7 @@ class TaskSnapshotController { } else { excludeLayers = new SurfaceControl[0]; } - final SurfaceControl.ScreenshotGraphicBuffer screenshotBuffer = + final SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer = SurfaceControl.captureLayersExcluding( task.getSurfaceControl(), mTmpRect, scaleFraction, pixelFormat, excludeLayers); @@ -369,8 +369,8 @@ class TaskSnapshotController { outTaskSize.x = mTmpRect.width(); outTaskSize.y = mTmpRect.height(); } - final GraphicBuffer buffer = screenshotBuffer != null ? screenshotBuffer.getGraphicBuffer() - : null; + final HardwareBuffer buffer = screenshotBuffer == null ? null + : screenshotBuffer.getHardwareBuffer(); if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) { return null; } @@ -391,14 +391,14 @@ class TaskSnapshotController { return null; } - final SurfaceControl.ScreenshotGraphicBuffer screenshotBuffer = + final SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer = createTaskSnapshot(task, builder); if (screenshotBuffer == null) { // Failed to acquire image. Has been logged. return null; } - builder.setSnapshot(screenshotBuffer.getGraphicBuffer()); + builder.setSnapshot(screenshotBuffer.getHardwareBuffer()); builder.setColorSpace(screenshotBuffer.getColorSpace()); return builder.build(); } @@ -493,7 +493,7 @@ class TaskSnapshotController { // color above return new TaskSnapshot( System.currentTimeMillis() /* id */, - topChild.mActivityComponent, hwBitmap.createGraphicBufferHandle(), + topChild.mActivityComponent, hwBitmap.getHardwareBuffer(), hwBitmap.getColorSpace(), mainWindow.getConfiguration().orientation, mainWindow.getWindowConfiguration().getRotation(), new Point(taskWidth, taskHeight), getInsets(mainWindow), false /* isLowResolution */, diff --git a/services/core/java/com/android/server/wm/TaskSnapshotLoader.java b/services/core/java/com/android/server/wm/TaskSnapshotLoader.java index c20ce5f40bc2..89ddc29aa642 100644 --- a/services/core/java/com/android/server/wm/TaskSnapshotLoader.java +++ b/services/core/java/com/android/server/wm/TaskSnapshotLoader.java @@ -26,9 +26,9 @@ import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; -import android.graphics.GraphicBuffer; import android.graphics.Point; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.util.Slog; import com.android.server.wm.nano.WindowManagerProtos.TaskSnapshotProto; @@ -174,7 +174,7 @@ class TaskSnapshotLoader { Slog.w(TAG, "Failed to create hardware bitmap: " + bitmapFile.getPath()); return null; } - final GraphicBuffer buffer = hwBitmap.createGraphicBufferHandle(); + final HardwareBuffer buffer = hwBitmap.getHardwareBuffer(); if (buffer == null) { Slog.w(TAG, "Failed to retrieve gralloc buffer for bitmap: " + bitmapFile.getPath()); diff --git a/services/core/java/com/android/server/wm/TaskSnapshotPersister.java b/services/core/java/com/android/server/wm/TaskSnapshotPersister.java index 45023acf4466..4b52cd714d8d 100644 --- a/services/core/java/com/android/server/wm/TaskSnapshotPersister.java +++ b/services/core/java/com/android/server/wm/TaskSnapshotPersister.java @@ -379,7 +379,7 @@ class TaskSnapshotPersister { boolean writeBuffer() { final Bitmap bitmap = Bitmap.wrapHardwareBuffer( - mSnapshot.getSnapshot(), mSnapshot.getColorSpace()); + mSnapshot.getHardwareBuffer(), mSnapshot.getColorSpace()); if (bitmap == null) { Slog.e(TAG, "Invalid task snapshot hw bitmap"); return false; diff --git a/services/core/java/com/android/server/wm/TaskSnapshotSurface.java b/services/core/java/com/android/server/wm/TaskSnapshotSurface.java index eb005e0f7eda..0c0cb91f5e72 100644 --- a/services/core/java/com/android/server/wm/TaskSnapshotSurface.java +++ b/services/core/java/com/android/server/wm/TaskSnapshotSurface.java @@ -53,12 +53,12 @@ import android.app.ActivityThread; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; -import android.graphics.GraphicBuffer; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; +import android.hardware.HardwareBuffer; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -202,7 +202,7 @@ class TaskSnapshotSurface implements StartingSurface { layoutParams.windowAnimations = mainWindow.getAttrs().windowAnimations; layoutParams.dimAmount = mainWindow.getAttrs().dimAmount; layoutParams.type = TYPE_APPLICATION_STARTING; - layoutParams.format = snapshot.getSnapshot().getFormat(); + layoutParams.format = snapshot.getHardwareBuffer().getFormat(); layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES) | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE; @@ -306,8 +306,9 @@ class TaskSnapshotSurface implements StartingSurface { mFrame.set(frame); mContentInsets.set(contentInsets); mStableInsets.set(stableInsets); - mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth() - || mFrame.height() != mSnapshot.getSnapshot().getHeight()); + final HardwareBuffer snapshot = mSnapshot.getHardwareBuffer(); + mSizeMismatch = (mFrame.width() != snapshot.getWidth() + || mFrame.height() != snapshot.getHeight()); mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets); } @@ -335,7 +336,7 @@ class TaskSnapshotSurface implements StartingSurface { } private void drawSizeMatchSnapshot() { - mSurface.attachAndQueueBufferWithColorSpace(mSnapshot.getSnapshot(), + mSurface.attachAndQueueBufferWithColorSpace(mSnapshot.getHardwareBuffer(), mSnapshot.getColorSpace()); mSurface.release(); } @@ -344,7 +345,7 @@ class TaskSnapshotSurface implements StartingSurface { if (!mSurface.isValid()) { throw new IllegalStateException("mSurface does not hold a valid surface."); } - final GraphicBuffer buffer = mSnapshot.getSnapshot(); + final HardwareBuffer buffer = mSnapshot.getHardwareBuffer(); final SurfaceSession session = new SurfaceSession(); // We consider nearly matched dimensions as there can be rounding errors and the user won't // notice very minute differences from scaling one dimension more than the other @@ -403,12 +404,12 @@ class TaskSnapshotSurface implements StartingSurface { @VisibleForTesting Rect calculateSnapshotCrop() { final Rect rect = new Rect(); - rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight()); + final HardwareBuffer snapshot = mSnapshot.getHardwareBuffer(); + rect.set(0, 0, snapshot.getWidth(), snapshot.getHeight()); final Rect insets = mSnapshot.getContentInsets(); - final float scaleX = (float) mSnapshot.getSnapshot().getWidth() / mSnapshot.getTaskSize().x; - final float scaleY = - (float) mSnapshot.getSnapshot().getHeight() / mSnapshot.getTaskSize().y; + final float scaleX = (float) snapshot.getWidth() / mSnapshot.getTaskSize().x; + final float scaleY = (float) snapshot.getHeight() / mSnapshot.getTaskSize().y; // Let's remove all system decorations except the status bar, but only if the task is at the // very top of the screen. @@ -427,9 +428,9 @@ class TaskSnapshotSurface implements StartingSurface { */ @VisibleForTesting Rect calculateSnapshotFrame(Rect crop) { - final float scaleX = (float) mSnapshot.getSnapshot().getWidth() / mSnapshot.getTaskSize().x; - final float scaleY = - (float) mSnapshot.getSnapshot().getHeight() / mSnapshot.getTaskSize().y; + final HardwareBuffer snapshot = mSnapshot.getHardwareBuffer(); + final float scaleX = (float) snapshot.getWidth() / mSnapshot.getTaskSize().x; + final float scaleY = (float) snapshot.getHeight() / mSnapshot.getTaskSize().y; // Rescale the frame from snapshot to window coordinate space final Rect frame = new Rect( diff --git a/services/core/java/com/android/server/wm/WallpaperController.java b/services/core/java/com/android/server/wm/WallpaperController.java index 57d0a335a688..732819410206 100644 --- a/services/core/java/com/android/server/wm/WallpaperController.java +++ b/services/core/java/com/android/server/wm/WallpaperController.java @@ -777,7 +777,7 @@ class WallpaperController { final Rect bounds = wallpaperWindowState.getBounds(); bounds.offsetTo(0, 0); - SurfaceControl.ScreenshotGraphicBuffer wallpaperBuffer = SurfaceControl.captureLayers( + SurfaceControl.ScreenshotHardwareBuffer wallpaperBuffer = SurfaceControl.captureLayers( wallpaperWindowState.getSurfaceControl(), bounds, 1 /* frameScale */); if (wallpaperBuffer == null) { @@ -785,7 +785,7 @@ class WallpaperController { return null; } return Bitmap.wrapHardwareBuffer( - wallpaperBuffer.getGraphicBuffer(), wallpaperBuffer.getColorSpace()); + wallpaperBuffer.getHardwareBuffer(), wallpaperBuffer.getColorSpace()); } private WindowState getTopVisibleWallpaper() { diff --git a/services/core/java/com/android/server/wm/utils/RotationAnimationUtils.java b/services/core/java/com/android/server/wm/utils/RotationAnimationUtils.java index 9dee7af59033..141561839ce3 100644 --- a/services/core/java/com/android/server/wm/utils/RotationAnimationUtils.java +++ b/services/core/java/com/android/server/wm/utils/RotationAnimationUtils.java @@ -16,14 +16,14 @@ package com.android.server.wm.utils; -import static android.graphics.PixelFormat.RGBA_8888; +import static android.hardware.HardwareBuffer.RGBA_8888; import android.graphics.Color; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.media.Image; import android.media.ImageReader; import android.view.Display; @@ -38,18 +38,18 @@ import java.util.Arrays; public class RotationAnimationUtils { /** - * Converts the provided {@link GraphicBuffer} and converts it to a bitmap to then sample the + * Converts the provided {@link HardwareBuffer} and converts it to a bitmap to then sample the * luminance at the borders of the bitmap * @return the average luminance of all the pixels at the borders of the bitmap */ - public static float getMedianBorderLuma(GraphicBuffer graphicBuffer, ColorSpace colorSpace) { - if (graphicBuffer == null || graphicBuffer.getFormat() != RGBA_8888) { + public static float getMedianBorderLuma(HardwareBuffer hardwareBuffer, ColorSpace colorSpace) { + if (hardwareBuffer == null || hardwareBuffer.getFormat() != RGBA_8888) { return 0; } - ImageReader ir = ImageReader.newInstance(graphicBuffer.getWidth(), - graphicBuffer.getHeight(), graphicBuffer.getFormat(), 1); - ir.getSurface().attachAndQueueBufferWithColorSpace(graphicBuffer, colorSpace); + ImageReader ir = ImageReader.newInstance(hardwareBuffer.getWidth(), + hardwareBuffer.getHeight(), hardwareBuffer.getFormat(), 1); + ir.getSurface().attachAndQueueBufferWithColorSpace(hardwareBuffer, colorSpace); Image image = ir.acquireLatestImage(); if (image == null || image.getPlanes().length == 0) { return 0; @@ -98,7 +98,7 @@ public class RotationAnimationUtils { /** * Gets the average border luma by taking a screenshot of the {@param surfaceControl}. - * @see #getMedianBorderLuma(GraphicBuffer, ColorSpace) + * @see #getMedianBorderLuma(HardwareBuffer, ColorSpace) */ public static float getLumaOfSurfaceControl(Display display, SurfaceControl surfaceControl) { if (surfaceControl == null) { @@ -108,13 +108,13 @@ public class RotationAnimationUtils { Point size = new Point(); display.getSize(size); Rect crop = new Rect(0, 0, size.x, size.y); - SurfaceControl.ScreenshotGraphicBuffer buffer = + SurfaceControl.ScreenshotHardwareBuffer buffer = SurfaceControl.captureLayers(surfaceControl, crop, 1); if (buffer == null) { return 0; } - return RotationAnimationUtils.getMedianBorderLuma(buffer.getGraphicBuffer(), + return RotationAnimationUtils.getMedianBorderLuma(buffer.getHardwareBuffer(), buffer.getColorSpace()); } diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotControllerTest.java index 20d9aff5f3bf..98f2f50346d9 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotControllerTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotControllerTest.java @@ -35,10 +35,10 @@ import android.app.WindowConfiguration; import android.content.ComponentName; import android.content.res.Configuration; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.platform.test.annotations.Presubmit; import android.util.ArraySet; import android.view.View; @@ -130,7 +130,7 @@ public class TaskSnapshotControllerTest extends WindowTestsBase { @Test public void testSnapshotBuilder() { - final GraphicBuffer buffer = Mockito.mock(GraphicBuffer.class); + final HardwareBuffer buffer = Mockito.mock(HardwareBuffer.class); final ColorSpace sRGB = ColorSpace.get(ColorSpace.Named.SRGB); final long id = 1234L; final ComponentName activityComponent = new ComponentName("package", ".Class"); @@ -173,12 +173,12 @@ public class TaskSnapshotControllerTest extends WindowTestsBase { assertEquals(orientation, snapshot.getOrientation()); assertEquals(contentInsets, snapshot.getContentInsets()); assertTrue(snapshot.isTranslucent()); - assertSame(buffer, snapshot.getSnapshot()); + assertSame(buffer, snapshot.getHardwareBuffer()); assertTrue(snapshot.isRealSnapshot()); assertEquals(taskSize, snapshot.getTaskSize()); } finally { if (buffer != null) { - buffer.destroy(); + buffer.close(); } } } diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java index 88de34dd36b0..87fa09a4724c 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java @@ -38,6 +38,7 @@ import android.graphics.GraphicBuffer; import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.os.UserManager; import android.view.Surface; @@ -177,7 +178,8 @@ class TaskSnapshotPersisterTestBase extends WindowTestsBase { Canvas c = buffer.lockCanvas(); c.drawColor(Color.RED); buffer.unlockCanvasAndPost(c); - return new TaskSnapshot(MOCK_SNAPSHOT_ID, new ComponentName("", ""), buffer, + return new TaskSnapshot(MOCK_SNAPSHOT_ID, new ComponentName("", ""), + HardwareBuffer.createFromGraphicBuffer(buffer), ColorSpace.get(ColorSpace.Named.SRGB), ORIENTATION_PORTRAIT, mRotation, taskSize, TEST_INSETS, // When building a TaskSnapshot with the Builder class, isLowResolution diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotSurfaceTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotSurfaceTest.java index 2164de9ea191..fb8fbe117f71 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotSurfaceTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotSurfaceTest.java @@ -36,10 +36,9 @@ import android.content.ComponentName; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; -import android.graphics.PixelFormat; import android.graphics.Point; import android.graphics.Rect; +import android.hardware.HardwareBuffer; import android.platform.test.annotations.Presubmit; import android.view.Surface; import android.view.SurfaceControl; @@ -66,8 +65,8 @@ public class TaskSnapshotSurfaceTest extends WindowTestsBase { private void setupSurface(int width, int height, Rect contentInsets, int sysuiVis, int windowFlags, Rect taskBounds) { - final GraphicBuffer buffer = GraphicBuffer.create(width, height, PixelFormat.RGBA_8888, - GraphicBuffer.USAGE_SW_READ_RARELY | GraphicBuffer.USAGE_SW_WRITE_NEVER); + final HardwareBuffer buffer = HardwareBuffer.create(width, height, HardwareBuffer.RGBA_8888, + 1, HardwareBuffer.USAGE_CPU_READ_RARELY); // Previously when constructing TaskSnapshots for this test, scale was 1.0f, so to mimic // this behavior set the taskSize to be the same as the taskBounds width and height. The diff --git a/services/tests/wmtests/src/com/android/server/wm/utils/RotationAnimationUtilsTest.java b/services/tests/wmtests/src/com/android/server/wm/utils/RotationAnimationUtilsTest.java index e5497a2313d1..34f8a0ada5e1 100644 --- a/services/tests/wmtests/src/com/android/server/wm/utils/RotationAnimationUtilsTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/utils/RotationAnimationUtilsTest.java @@ -23,9 +23,9 @@ import static org.junit.Assert.assertEquals; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.ColorSpace; -import android.graphics.GraphicBuffer; import android.graphics.Matrix; import android.graphics.PointF; +import android.hardware.HardwareBuffer; import android.view.Surface; import org.junit.Before; @@ -49,24 +49,24 @@ public class RotationAnimationUtilsTest { @Test public void blackLuma() { Bitmap swBitmap = createBitmap(0); - GraphicBuffer gb = swBitmapToGraphicsBuffer(swBitmap); - float borderLuma = RotationAnimationUtils.getMedianBorderLuma(gb, mColorSpace); + HardwareBuffer hb = swBitmapToHardwareBuffer(swBitmap); + float borderLuma = RotationAnimationUtils.getMedianBorderLuma(hb, mColorSpace); assertEquals(0, borderLuma, 0); } @Test public void whiteLuma() { Bitmap swBitmap = createBitmap(1); - GraphicBuffer gb = swBitmapToGraphicsBuffer(swBitmap); - float borderLuma = RotationAnimationUtils.getMedianBorderLuma(gb, mColorSpace); + HardwareBuffer hb = swBitmapToHardwareBuffer(swBitmap); + float borderLuma = RotationAnimationUtils.getMedianBorderLuma(hb, mColorSpace); assertEquals(1, borderLuma, 0); } @Test public void unevenBitmapDimens() { Bitmap swBitmap = createBitmap(1, BITMAP_WIDTH + 1, BITMAP_HEIGHT + 1); - GraphicBuffer gb = swBitmapToGraphicsBuffer(swBitmap); - float borderLuma = RotationAnimationUtils.getMedianBorderLuma(gb, mColorSpace); + HardwareBuffer hb = swBitmapToHardwareBuffer(swBitmap); + float borderLuma = RotationAnimationUtils.getMedianBorderLuma(hb, mColorSpace); assertEquals(1, borderLuma, 0); } @@ -74,8 +74,8 @@ public class RotationAnimationUtilsTest { public void whiteImageBlackBorderLuma() { Bitmap swBitmap = createBitmap(1); setBorderLuma(swBitmap, 0); - GraphicBuffer gb = swBitmapToGraphicsBuffer(swBitmap); - float borderLuma = RotationAnimationUtils.getMedianBorderLuma(gb, mColorSpace); + HardwareBuffer hb = swBitmapToHardwareBuffer(swBitmap); + float borderLuma = RotationAnimationUtils.getMedianBorderLuma(hb, mColorSpace); assertEquals(0, borderLuma, 0); } @@ -83,8 +83,8 @@ public class RotationAnimationUtilsTest { public void blackImageWhiteBorderLuma() { Bitmap swBitmap = createBitmap(0); setBorderLuma(swBitmap, 1); - GraphicBuffer gb = swBitmapToGraphicsBuffer(swBitmap); - float borderLuma = RotationAnimationUtils.getMedianBorderLuma(gb, mColorSpace); + HardwareBuffer hb = swBitmapToHardwareBuffer(swBitmap); + float borderLuma = RotationAnimationUtils.getMedianBorderLuma(hb, mColorSpace); assertEquals(1, borderLuma, 0); } @@ -144,9 +144,9 @@ public class RotationAnimationUtilsTest { return bitmap; } - private GraphicBuffer swBitmapToGraphicsBuffer(Bitmap swBitmap) { + private HardwareBuffer swBitmapToHardwareBuffer(Bitmap swBitmap) { Bitmap hwBitmap = swBitmap.copy(Bitmap.Config.HARDWARE, false); - return hwBitmap.createGraphicBufferHandle(); + return hwBitmap.getHardwareBuffer(); } private void setBorderLuma(Bitmap swBitmap, float luma) { |