Merge "Fix Skia's impl for TextureView.getBitmap" into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
5d27097739
@@ -34,7 +34,8 @@ void LayerDrawable::onDraw(SkCanvas* canvas) {
|
||||
}
|
||||
}
|
||||
|
||||
bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer) {
|
||||
bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
|
||||
const SkRect* dstRect) {
|
||||
if (context == nullptr) {
|
||||
SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
|
||||
return false;
|
||||
@@ -43,8 +44,8 @@ bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer
|
||||
SkMatrix layerTransform;
|
||||
layer->getTransform().copyTo(layerTransform);
|
||||
sk_sp<SkImage> layerImage;
|
||||
int layerWidth = layer->getWidth();
|
||||
int layerHeight = layer->getHeight();
|
||||
const int layerWidth = layer->getWidth();
|
||||
const int layerHeight = layer->getHeight();
|
||||
if (layer->getApi() == Layer::Api::OpenGL) {
|
||||
GlLayer* glLayer = static_cast<GlLayer*>(layer);
|
||||
GrGLTextureInfo externalTexture;
|
||||
@@ -62,21 +63,21 @@ bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer
|
||||
}
|
||||
|
||||
if (layerImage) {
|
||||
SkMatrix textureMatrix;
|
||||
layer->getTexTransform().copyTo(textureMatrix);
|
||||
SkMatrix textureMatrixInv;
|
||||
layer->getTexTransform().copyTo(textureMatrixInv);
|
||||
// TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
|
||||
// use bottom left origin and remove flipV and invert transformations.
|
||||
SkMatrix flipV;
|
||||
flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
|
||||
textureMatrix.preConcat(flipV);
|
||||
textureMatrix.preScale(1.0f / layerWidth, 1.0f / layerHeight);
|
||||
textureMatrix.postScale(layerWidth, layerHeight);
|
||||
SkMatrix textureMatrixInv;
|
||||
if (!textureMatrix.invert(&textureMatrixInv)) {
|
||||
textureMatrixInv = textureMatrix;
|
||||
textureMatrixInv.preConcat(flipV);
|
||||
textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
|
||||
textureMatrixInv.postScale(layerWidth, layerHeight);
|
||||
SkMatrix textureMatrix;
|
||||
if (!textureMatrixInv.invert(&textureMatrix)) {
|
||||
textureMatrix = textureMatrixInv;
|
||||
}
|
||||
|
||||
SkMatrix matrix = SkMatrix::Concat(layerTransform, textureMatrixInv);
|
||||
SkMatrix matrix = SkMatrix::Concat(layerTransform, textureMatrix);
|
||||
|
||||
SkPaint paint;
|
||||
paint.setAlpha(layer->getAlpha());
|
||||
@@ -88,7 +89,20 @@ bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer
|
||||
canvas->save();
|
||||
canvas->concat(matrix);
|
||||
}
|
||||
canvas->drawImage(layerImage.get(), 0, 0, &paint);
|
||||
if (dstRect) {
|
||||
SkMatrix matrixInv;
|
||||
if (!matrix.invert(&matrixInv)) {
|
||||
matrixInv = matrix;
|
||||
}
|
||||
SkRect srcRect = SkRect::MakeIWH(layerWidth, layerHeight);
|
||||
matrixInv.mapRect(&srcRect);
|
||||
SkRect skiaDestRect = *dstRect;
|
||||
matrixInv.mapRect(&skiaDestRect);
|
||||
canvas->drawImageRect(layerImage.get(), srcRect, skiaDestRect, &paint,
|
||||
SkCanvas::kFast_SrcRectConstraint);
|
||||
} else {
|
||||
canvas->drawImage(layerImage.get(), 0, 0, &paint);
|
||||
}
|
||||
// restore the original matrix
|
||||
if (nonIdentityMatrix) {
|
||||
canvas->restore();
|
||||
|
||||
@@ -32,7 +32,8 @@ class LayerDrawable : public SkDrawable {
|
||||
public:
|
||||
explicit LayerDrawable(DeferredLayerUpdater* layerUpdater) : mLayerUpdater(layerUpdater) {}
|
||||
|
||||
static bool DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer);
|
||||
static bool DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
|
||||
const SkRect* dstRect = nullptr);
|
||||
|
||||
protected:
|
||||
virtual SkRect onGetBounds() override {
|
||||
|
||||
@@ -138,7 +138,9 @@ bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBi
|
||||
SkBudgeted::kYes, bitmap->info());
|
||||
|
||||
Layer* layer = deferredLayer->backingLayer();
|
||||
if (LayerDrawable::DrawLayer(mRenderThread.getGrContext(), tmpSurface->getCanvas(), layer)) {
|
||||
const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height());
|
||||
if (LayerDrawable::DrawLayer(mRenderThread.getGrContext(), tmpSurface->getCanvas(), layer,
|
||||
&dstRect)) {
|
||||
sk_sp<SkImage> tmpImage = tmpSurface->makeImageSnapshot();
|
||||
if (tmpImage->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
|
||||
bitmap->notifyPixelsChanged();
|
||||
|
||||
Reference in New Issue
Block a user