1 /*
2  * Copyright (C) 2012 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 "Layer.h"
18 
19 #include "renderstate/RenderState.h"
20 #include "utils/Color.h"
21 #include "utils/MathUtils.h"
22 
23 namespace android {
24 namespace uirenderer {
25 
Layer(RenderState & renderState,sk_sp<SkColorFilter> colorFilter,int alpha,SkBlendMode mode)26 Layer::Layer(RenderState& renderState, sk_sp<SkColorFilter> colorFilter, int alpha,
27         SkBlendMode mode)
28         : mRenderState(renderState)
29         , mColorFilter(colorFilter)
30         , alpha(alpha)
31         , mode(mode) {
32     // TODO: This is a violation of Android's typical ref counting, but it
33     // preserves the old inc/dec ref locations. This should be changed...
34     incStrong(nullptr);
35     renderState.registerLayer(this);
36     texTransform.setIdentity();
37     transform.setIdentity();
38 }
39 
~Layer()40 Layer::~Layer() {
41     mRenderState.unregisterLayer(this);
42 }
43 
postDecStrong()44 void Layer::postDecStrong() {
45     mRenderState.postDecStrong(this);
46 }
47 
getMode() const48 SkBlendMode Layer::getMode() const {
49     if (mBlend || mode != SkBlendMode::kSrcOver) {
50         return mode;
51     } else {
52         return SkBlendMode::kSrc;
53     }
54 }
55 
isIntegerAligned(SkScalar x)56 static inline SkScalar isIntegerAligned(SkScalar x) {
57     return MathUtils::isZero(roundf(x) - x);
58 }
59 
60 // Disable filtering when there is no scaling in screen coordinates and the corners have the same
61 // fraction (for translate) or zero fraction (for any other rect-to-rect transform).
shouldFilterRect(const SkMatrix & matrix,const SkRect & srcRect,const SkRect & dstRect)62 static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
63     if (!matrix.rectStaysRect()) return true;
64     SkRect dstDevRect = matrix.mapRect(dstRect);
65     float dstW, dstH;
66     if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
67         // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
68         // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
69         // dimensions is sufficient, but swap width and height comparison.
70         dstW = dstDevRect.height();
71         dstH = dstDevRect.width();
72     } else {
73         // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
74         // dimensions are still safe to compare directly.
75         dstW = dstDevRect.width();
76         dstH = dstDevRect.height();
77     }
78     if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
79           MathUtils::areEqual(dstH, srcRect.height()))) {
80         return true;
81     }
82     // Device rect and source rect should be integer aligned to ensure there's no difference
83     // in how nearest-neighbor sampling is resolved.
84     return !(isIntegerAligned(srcRect.x()) &&
85              isIntegerAligned(srcRect.y()) &&
86              isIntegerAligned(dstDevRect.x()) &&
87              isIntegerAligned(dstDevRect.y()));
88 }
89 
draw(SkCanvas * canvas)90 void Layer::draw(SkCanvas* canvas) {
91     GrRecordingContext* context = canvas->recordingContext();
92     if (context == nullptr) {
93         SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
94         return;
95     }
96     SkMatrix layerTransform = getTransform();
97     //sk_sp<SkImage> layerImage = getImage();
98     const int layerWidth = getWidth();
99     const int layerHeight = getHeight();
100     if (layerImage) {
101         SkMatrix textureMatrixInv;
102         textureMatrixInv = getTexTransform();
103         // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
104         // use bottom left origin and remove flipV and invert transformations.
105         SkMatrix flipV;
106         flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
107         textureMatrixInv.preConcat(flipV);
108         textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
109         textureMatrixInv.postScale(layerImage->width(), layerImage->height());
110         SkMatrix textureMatrix;
111         if (!textureMatrixInv.invert(&textureMatrix)) {
112             textureMatrix = textureMatrixInv;
113         }
114 
115         SkMatrix matrix;
116         matrix = SkMatrix::Concat(layerTransform, textureMatrix);
117 
118         SkPaint paint;
119         paint.setAlpha(getAlpha());
120         paint.setBlendMode(getMode());
121         paint.setColorFilter(getColorFilter());
122         const bool nonIdentityMatrix = !matrix.isIdentity();
123         if (nonIdentityMatrix) {
124             canvas->save();
125             canvas->concat(matrix);
126         }
127         const SkMatrix& totalMatrix = canvas->getTotalMatrix();
128 
129         SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
130         SkSamplingOptions sampling;
131         if (getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
132             sampling = SkSamplingOptions(SkFilterMode::kLinear);
133         }
134         canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint);
135         // restore the original matrix
136         if (nonIdentityMatrix) {
137             canvas->restore();
138         }
139     }
140 }
141 
142 }  // namespace uirenderer
143 }  // namespace android
144