Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce44045d authored by John Reck's avatar John Reck Committed by Android (Google) Code Review
Browse files

Merge "Fix window copy for rotation = 90/270"

parents c1b9569d 912bebeb
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -82,8 +82,15 @@ CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer,
        return CopyResult::UnknownError;
        return CopyResult::UnknownError;
    }
    }


    CopyResult copyResult = copyImageInto(sourceImage, texTransform, graphicBuffer->getWidth(),
    uint32_t width = graphicBuffer->getWidth();
            graphicBuffer->getHeight(), srcRect, bitmap);
    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
    // All we're flushing & finishing is the deletion of the texture since
    // copyImageInto already did a major flush & finish as an implicit
    // copyImageInto already did a major flush & finish as an implicit
+10 −0
Original line number Original line Diff line number Diff line
@@ -989,5 +989,15 @@
            </intent-filter>
            </intent-filter>
        </activity>
        </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>
    </application>
</manifest>
</manifest>
+98 −0
Original line number Original line Diff line number Diff line
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);
    }
}