1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "LayerDrawable.h"
18 #include <utils/MathUtils.h>
19 
20 #include "GrBackendSurface.h"
21 #include "SkColorFilter.h"
22 #include "SkSurface.h"
23 #include "gl/GrGLTypes.h"
24 
25 namespace android {
26 namespace uirenderer {
27 namespace skiapipeline {
28 
onDraw(SkCanvas * canvas)29 void LayerDrawable::onDraw(SkCanvas* canvas) {
30     Layer* layer = mLayerUpdater->backingLayer();
31     if (layer) {
32         DrawLayer(canvas->getGrContext(), canvas, layer, nullptr, nullptr, true);
33     }
34 }
35 
36 // This is a less-strict matrix.isTranslate() that will still report being translate-only
37 // on imperceptibly small scaleX & scaleY values.
isBasicallyTranslate(const SkMatrix & matrix)38 static bool isBasicallyTranslate(const SkMatrix& matrix) {
39     if (!matrix.isScaleTranslate()) return false;
40     return MathUtils::isOne(matrix.getScaleX()) && MathUtils::isOne(matrix.getScaleY());
41 }
42 
shouldFilter(const SkMatrix & matrix)43 static bool shouldFilter(const SkMatrix& matrix) {
44     if (!matrix.isScaleTranslate()) return true;
45 
46     // We only care about meaningful scale here
47     bool noScale = MathUtils::isOne(matrix.getScaleX()) && MathUtils::isOne(matrix.getScaleY());
48     bool pixelAligned =
49             SkScalarIsInt(matrix.getTranslateX()) && SkScalarIsInt(matrix.getTranslateY());
50     return !(noScale && pixelAligned);
51 }
52 
DrawLayer(GrContext * context,SkCanvas * canvas,Layer * layer,const SkRect * srcRect,const SkRect * dstRect,bool useLayerTransform)53 bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
54                               const SkRect* srcRect, const SkRect* dstRect,
55                               bool useLayerTransform) {
56     if (context == nullptr) {
57         SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
58         return false;
59     }
60     // transform the matrix based on the layer
61     SkMatrix layerTransform = layer->getTransform();
62     sk_sp<SkImage> layerImage = layer->getImage();
63     const int layerWidth = layer->getWidth();
64     const int layerHeight = layer->getHeight();
65 
66     if (layerImage) {
67         SkMatrix textureMatrixInv;
68         textureMatrixInv = layer->getTexTransform();
69         // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
70         // use bottom left origin and remove flipV and invert transformations.
71         SkMatrix flipV;
72         flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
73         textureMatrixInv.preConcat(flipV);
74         textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
75         textureMatrixInv.postScale(layerImage->width(), layerImage->height());
76         SkMatrix textureMatrix;
77         if (!textureMatrixInv.invert(&textureMatrix)) {
78             textureMatrix = textureMatrixInv;
79         }
80 
81         SkMatrix matrix;
82         if (useLayerTransform) {
83             matrix = SkMatrix::Concat(layerTransform, textureMatrix);
84         } else {
85             matrix = textureMatrix;
86         }
87 
88         SkPaint paint;
89         paint.setAlpha(layer->getAlpha());
90         paint.setBlendMode(layer->getMode());
91         paint.setColorFilter(layer->getColorFilter());
92         const bool nonIdentityMatrix = !matrix.isIdentity();
93         if (nonIdentityMatrix) {
94             canvas->save();
95             canvas->concat(matrix);
96         }
97         const SkMatrix& totalMatrix = canvas->getTotalMatrix();
98         if (dstRect || srcRect) {
99             SkMatrix matrixInv;
100             if (!matrix.invert(&matrixInv)) {
101                 matrixInv = matrix;
102             }
103             SkRect skiaSrcRect;
104             if (srcRect) {
105                 skiaSrcRect = *srcRect;
106             } else {
107                 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
108             }
109             matrixInv.mapRect(&skiaSrcRect);
110             SkRect skiaDestRect;
111             if (dstRect) {
112                 skiaDestRect = *dstRect;
113             } else {
114                 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
115             }
116             matrixInv.mapRect(&skiaDestRect);
117             // If (matrix is identity or an integer translation) and (src/dst buffers size match),
118             // then use nearest neighbor, otherwise use bilerp sampling.
119             // Integer translation is defined as when src rect and dst rect align fractionally.
120             // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
121             // only for SrcOver blending and without color filter (readback uses Src blending).
122             bool isIntegerTranslate =
123                     isBasicallyTranslate(totalMatrix) &&
124                     SkScalarFraction(skiaDestRect.fLeft + totalMatrix[SkMatrix::kMTransX]) ==
125                             SkScalarFraction(skiaSrcRect.fLeft) &&
126                     SkScalarFraction(skiaDestRect.fTop + totalMatrix[SkMatrix::kMTransY]) ==
127                             SkScalarFraction(skiaSrcRect.fTop);
128             if (layer->getForceFilter() || !isIntegerTranslate) {
129                 paint.setFilterQuality(kLow_SkFilterQuality);
130             }
131             canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
132                                   SkCanvas::kFast_SrcRectConstraint);
133         } else {
134             if (layer->getForceFilter() || shouldFilter(totalMatrix)) {
135                 paint.setFilterQuality(kLow_SkFilterQuality);
136             }
137             canvas->drawImage(layerImage.get(), 0, 0, &paint);
138         }
139         // restore the original matrix
140         if (nonIdentityMatrix) {
141             canvas->restore();
142         }
143     }
144 
145     return layerImage != nullptr;
146 }
147 
148 }  // namespace skiapipeline
149 }  // namespace uirenderer
150 }  // namespace android
151