diff options
-rw-r--r-- | libs/hwui/pipeline/skia/LayerDrawable.cpp | 40 | ||||
-rw-r--r-- | libs/hwui/pipeline/skia/LayerDrawable.h | 3 | ||||
-rw-r--r-- | libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp | 4 |
3 files changed, 32 insertions, 15 deletions
diff --git a/libs/hwui/pipeline/skia/LayerDrawable.cpp b/libs/hwui/pipeline/skia/LayerDrawable.cpp index d9584db2df9d..293e45f2273f 100644 --- a/libs/hwui/pipeline/skia/LayerDrawable.cpp +++ b/libs/hwui/pipeline/skia/LayerDrawable.cpp @@ -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(); diff --git a/libs/hwui/pipeline/skia/LayerDrawable.h b/libs/hwui/pipeline/skia/LayerDrawable.h index 345038769306..18d118405a39 100644 --- a/libs/hwui/pipeline/skia/LayerDrawable.h +++ b/libs/hwui/pipeline/skia/LayerDrawable.h @@ -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 { diff --git a/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp b/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp index 365d7403e046..74cfb2854e62 100644 --- a/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp +++ b/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp @@ -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(); |