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
isIntegerAligned(SkScalar x)36 static inline SkScalar isIntegerAligned(SkScalar x) {
37 return fabsf(roundf(x) - x) <= NON_ZERO_EPSILON;
38 }
39
40 // Disable filtering when there is no scaling in screen coordinates and the corners have the same
41 // fraction (for translate) or zero fraction (for any other rect-to-rect transform).
shouldFilterRect(const SkMatrix & matrix,const SkRect & srcRect,const SkRect & dstRect)42 static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
43 if (!matrix.rectStaysRect()) return true;
44 SkRect dstDevRect = matrix.mapRect(dstRect);
45 float dstW, dstH;
46 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
47 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
48 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
49 // dimensions is sufficient, but swap width and height comparison.
50 dstW = dstDevRect.height();
51 dstH = dstDevRect.width();
52 } else {
53 // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
54 // dimensions are still safe to compare directly.
55 dstW = dstDevRect.width();
56 dstH = dstDevRect.height();
57 }
58 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
59 MathUtils::areEqual(dstH, srcRect.height()))) {
60 return true;
61 }
62 // Device rect and source rect should be integer aligned to ensure there's no difference
63 // in how nearest-neighbor sampling is resolved.
64 return !(isIntegerAligned(srcRect.x()) &&
65 isIntegerAligned(srcRect.y()) &&
66 isIntegerAligned(dstDevRect.x()) &&
67 isIntegerAligned(dstDevRect.y()));
68 }
69
DrawLayer(GrContext * context,SkCanvas * canvas,Layer * layer,const SkRect * srcRect,const SkRect * dstRect,bool useLayerTransform)70 bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
71 const SkRect* srcRect, const SkRect* dstRect,
72 bool useLayerTransform) {
73 if (context == nullptr) {
74 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
75 return false;
76 }
77 // transform the matrix based on the layer
78 SkMatrix layerTransform = layer->getTransform();
79 sk_sp<SkImage> layerImage = layer->getImage();
80 const int layerWidth = layer->getWidth();
81 const int layerHeight = layer->getHeight();
82
83 if (layerImage) {
84 SkMatrix textureMatrixInv;
85 textureMatrixInv = layer->getTexTransform();
86 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
87 // use bottom left origin and remove flipV and invert transformations.
88 SkMatrix flipV;
89 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
90 textureMatrixInv.preConcat(flipV);
91 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
92 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
93 SkMatrix textureMatrix;
94 if (!textureMatrixInv.invert(&textureMatrix)) {
95 textureMatrix = textureMatrixInv;
96 }
97
98 SkMatrix matrix;
99 if (useLayerTransform) {
100 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
101 } else {
102 matrix = textureMatrix;
103 }
104
105 SkPaint paint;
106 paint.setAlpha(layer->getAlpha());
107 paint.setBlendMode(layer->getMode());
108 paint.setColorFilter(layer->getColorFilter());
109 const bool nonIdentityMatrix = !matrix.isIdentity();
110 if (nonIdentityMatrix) {
111 canvas->save();
112 canvas->concat(matrix);
113 }
114 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
115 if (dstRect || srcRect) {
116 SkMatrix matrixInv;
117 if (!matrix.invert(&matrixInv)) {
118 matrixInv = matrix;
119 }
120 SkRect skiaSrcRect;
121 if (srcRect) {
122 skiaSrcRect = *srcRect;
123 } else {
124 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
125 }
126 matrixInv.mapRect(&skiaSrcRect);
127 SkRect skiaDestRect;
128 if (dstRect) {
129 skiaDestRect = *dstRect;
130 } else {
131 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
132 }
133 matrixInv.mapRect(&skiaDestRect);
134 // If (matrix is a rect-to-rect transform)
135 // and (src/dst buffers size match in screen coordinates)
136 // and (src/dst corners align fractionally),
137 // then use nearest neighbor, otherwise use bilerp sampling.
138 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
139 // only for SrcOver blending and without color filter (readback uses Src blending).
140 if (layer->getForceFilter() ||
141 shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
142 paint.setFilterQuality(kLow_SkFilterQuality);
143 }
144 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
145 SkCanvas::kFast_SrcRectConstraint);
146 } else {
147 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
148 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
149 paint.setFilterQuality(kLow_SkFilterQuality);
150 }
151 canvas->drawImage(layerImage.get(), 0, 0, &paint);
152 }
153 // restore the original matrix
154 if (nonIdentityMatrix) {
155 canvas->restore();
156 }
157 }
158
159 return layerImage != nullptr;
160 }
161
162 } // namespace skiapipeline
163 } // namespace uirenderer
164 } // namespace android
165