OSDN Git Service

Fix window copy for rotation = 90/270
authorJohn Reck <jreck@google.com>
Thu, 8 Dec 2016 00:36:15 +0000 (16:36 -0800)
committerJohn Reck <jreck@google.com>
Thu, 8 Dec 2016 19:22:31 +0000 (11:22 -0800)
Bug: 33421965
Test: Manual via PixelCopyWindow test in HwAccelerationTest
Change-Id: I2a59fd6a26499635a22444e124cd1ec6f82f6e31

libs/hwui/OpenGLReadback.cpp
tests/HwAccelerationTest/AndroidManifest.xml
tests/HwAccelerationTest/src/com/android/test/hwui/PixelCopyWindow.java [new file with mode: 0644]

index 408159b..74a8395 100644 (file)
@@ -82,8 +82,15 @@ CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer,
         return CopyResult::UnknownError;
     }
 
-    CopyResult copyResult = copyImageInto(sourceImage, texTransform, graphicBuffer->getWidth(),
-            graphicBuffer->getHeight(), srcRect, bitmap);
+    uint32_t width = graphicBuffer->getWidth();
+    uint32_t height = graphicBuffer->getHeight();
+    // If this is a 90 or 270 degree rotation we need to swap width/height
+    // This is a fuzzy way of checking that.
+    if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
+        std::swap(width, height);
+    }
+    CopyResult copyResult = copyImageInto(sourceImage, texTransform, width, height,
+            srcRect, bitmap);
 
     // All we're flushing & finishing is the deletion of the texture since
     // copyImageInto already did a major flush & finish as an implicit
index 3785cdc..b4f3d69 100644 (file)
             </intent-filter>
         </activity>
 
+        <activity
+                android:name="PixelCopyWindow"
+                android:label="Readback/Window"
+                android:screenOrientation="fullSensor">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.hwui.TEST" />
+            </intent-filter>
+        </activity>
+
     </application>
 </manifest>
diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/PixelCopyWindow.java b/tests/HwAccelerationTest/src/com/android/test/hwui/PixelCopyWindow.java
new file mode 100644 (file)
index 0000000..a039fba
--- /dev/null
@@ -0,0 +1,98 @@
+package com.android.test.hwui;
+
+import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
+import android.graphics.Paint.Style;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.ColorFilter;
+import android.graphics.Paint;
+import android.graphics.PixelFormat;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.os.Handler;
+import android.util.Log;
+import android.view.PixelCopy;
+import android.view.View;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+public class PixelCopyWindow extends Activity {
+
+    private Handler mHandler;
+    private ImageView mImage;
+    private TextView mText;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mHandler = new Handler();
+
+        LinearLayout layout = new LinearLayout(this);
+        TextView text = new TextView(this);
+        text.setText("Hello, World!");
+        Button btn = new Button(this);
+        btn.setText("Screenshot!");
+        btn.setOnClickListener((v) -> takeScreenshot());
+        mImage = new ImageView(this);
+        mText = new TextView(this);
+
+        layout.setOrientation(LinearLayout.VERTICAL);
+        layout.addView(text);
+        layout.addView(btn);
+        layout.addView(mImage);
+        layout.addView(mText);
+        final float density = getResources().getDisplayMetrics().density;
+        layout.setBackground(new Drawable() {
+            Paint mPaint = new Paint();
+
+            @Override
+            public void draw(Canvas canvas) {
+                mPaint.setStyle(Style.STROKE);
+                mPaint.setStrokeWidth(4 * density);
+                mPaint.setColor(Color.BLUE);
+                final Rect bounds = getBounds();
+                canvas.drawRect(bounds, mPaint);
+                mPaint.setColor(Color.RED);
+                canvas.drawLine(bounds.centerX(), 0, bounds.centerX(), bounds.height(), mPaint);
+                mPaint.setColor(Color.GREEN);
+                canvas.drawLine(0, bounds.centerY(), bounds.width(), bounds.centerY(), mPaint);
+            }
+
+            @Override
+            public void setAlpha(int alpha) {
+            }
+
+            @Override
+            public void setColorFilter(ColorFilter colorFilter) {
+            }
+
+            @Override
+            public int getOpacity() {
+                return PixelFormat.TRANSLUCENT;
+            }
+        });
+        setContentView(layout);
+    }
+
+    private void takeScreenshot() {
+        View decor = getWindow().getDecorView();
+        Rect srcRect = new Rect();
+        decor.getGlobalVisibleRect(srcRect);
+        final Bitmap bitmap = Bitmap.createBitmap(
+                (int) (srcRect.width() * .25), (int) (srcRect.height() * .25), Config.ARGB_8888);
+        PixelCopy.request(getWindow(), srcRect, bitmap, (result) -> {
+            if (result != PixelCopy.SUCCESS) {
+                mText.setText("Copy failed, result: " + result);
+                mImage.setImageBitmap(null);
+            } else {
+                mText.setText("");
+                mImage.setImageBitmap(bitmap);
+            }
+        }, mHandler);
+    }
+}