OSDN Git Service

Copy hardware bitmap to a new hardware bitmap for new properties.
authorchaviw <chaviw@google.com>
Tue, 12 Dec 2017 22:46:40 +0000 (14:46 -0800)
committerchaviw <chaviw@google.com>
Tue, 12 Dec 2017 22:46:40 +0000 (14:46 -0800)
The previous code copied the hardware bitmap to a software bitmap so
properties like size and matrix could be updated. Instead, copy the
hardware bitmap to another hardware bitmap when adjusting the
properties.

Test: Screenshots notification from status bar and lock screen contain
the new bitmap with the correct size.
Fixes: 69929720

Change-Id: Ia0bd20a0085a15cfebbe0dfffddcd834363a6964

packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java

index 8316331..6db46b5 100644 (file)
@@ -38,7 +38,6 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
-import android.graphics.Canvas;
 import android.graphics.ColorMatrix;
 import android.graphics.ColorMatrixColorFilter;
 import android.graphics.Matrix;
@@ -58,10 +57,13 @@ import android.provider.MediaStore;
 import android.util.DisplayMetrics;
 import android.util.Slog;
 import android.view.Display;
+import android.view.DisplayListCanvas;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
+import android.view.RenderNode;
 import android.view.Surface;
 import android.view.SurfaceControl;
+import android.view.ThreadedRenderer;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
@@ -153,7 +155,6 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
         int previewWidth = data.previewWidth;
         int previewHeight = data.previewheight;
 
-        Canvas c = new Canvas();
         Paint paint = new Paint();
         ColorMatrix desat = new ColorMatrix();
         desat.setSaturation(0.25f);
@@ -161,27 +162,17 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
         Matrix matrix = new Matrix();
         int overlayColor = 0x40FFFFFF;
 
-        // Bitmaps created for screenshots are hardware bitmaps. Copy to a software bitmap in order
-        // to update size for previews.
-        Bitmap swBitmap = data.image.copy(Bitmap.Config.ARGB_8888, true);
-
-        Bitmap picture = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
         matrix.setTranslate((previewWidth - mImageWidth) / 2, (previewHeight - mImageHeight) / 2);
-        c.setBitmap(picture);
-        c.drawBitmap(swBitmap, matrix, paint);
-        c.drawColor(overlayColor);
-        c.setBitmap(null);
+        Bitmap picture = generateAdjustedHwBitmap(data.image, previewWidth, previewHeight, matrix,
+                paint, overlayColor);
 
         // Note, we can't use the preview for the small icon, since it is non-square
         float scale = (float) iconSize / Math.min(mImageWidth, mImageHeight);
-        Bitmap icon = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
         matrix.setScale(scale, scale);
         matrix.postTranslate((iconSize - (scale * mImageWidth)) / 2,
                 (iconSize - (scale * mImageHeight)) / 2);
-        c.setBitmap(icon);
-        c.drawBitmap(swBitmap, matrix, paint);
-        c.drawColor(overlayColor);
-        c.setBitmap(null);
+        Bitmap icon = generateAdjustedHwBitmap(data.image, iconSize, iconSize, matrix, paint,
+                overlayColor);
 
         // Show the intermediate notification
         mTickerAddSpace = !mTickerAddSpace;
@@ -235,6 +226,22 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
         mNotificationStyle.bigLargeIcon((Bitmap) null);
     }
 
+    /**
+     * Generates a new hardware bitmap with specified values, copying the content from the passed
+     * in bitmap.
+     */
+    private Bitmap generateAdjustedHwBitmap(Bitmap bitmap, int width, int height, Matrix matrix,
+            Paint paint, int color) {
+        RenderNode node = RenderNode.create("ScreenshotCanvas", null);
+        node.setLeftTopRightBottom(0, 0, width, height);
+        node.setClipToBounds(false);
+        DisplayListCanvas canvas = node.start(width, height);
+        canvas.drawColor(color);
+        canvas.drawBitmap(bitmap, matrix, paint);
+        node.end(canvas);
+        return ThreadedRenderer.createHardwareBitmap(node, width, height);
+    }
+
     @Override
     protected Void doInBackground(Void... params) {
         if (isCancelled()) {