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->recordingContext(), canvas, layer, nullptr, nullptr, true);
33 }
34 }
35
isIntegerAligned(SkScalar x)36 static inline SkScalar isIntegerAligned(SkScalar x) {
37 return MathUtils::isZero(roundf(x) - x);
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
70 // TODO: Context arg probably doesn't belong here – do debug check at callsite instead.
DrawLayer(GrRecordingContext * context,SkCanvas * canvas,Layer * layer,const SkRect * srcRect,const SkRect * dstRect,bool useLayerTransform)71 bool LayerDrawable::DrawLayer(GrRecordingContext* context,
72 SkCanvas* canvas,
73 Layer* layer,
74 const SkRect* srcRect,
75 const SkRect* dstRect,
76 bool useLayerTransform) {
77 if (context == nullptr) {
78 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
79 return false;
80 }
81 // transform the matrix based on the layer
82 SkMatrix layerTransform = layer->getTransform();
83 sk_sp<SkImage> layerImage = layer->getImage();
84 const int layerWidth = layer->getWidth();
85 const int layerHeight = layer->getHeight();
86
87 if (layerImage) {
88 SkMatrix textureMatrixInv;
89 textureMatrixInv = layer->getTexTransform();
90 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
91 // use bottom left origin and remove flipV and invert transformations.
92 SkMatrix flipV;
93 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
94 textureMatrixInv.preConcat(flipV);
95 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
96 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
97 SkMatrix textureMatrix;
98 if (!textureMatrixInv.invert(&textureMatrix)) {
99 textureMatrix = textureMatrixInv;
100 }
101
102 SkMatrix matrix;
103 if (useLayerTransform) {
104 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
105 } else {
106 matrix = textureMatrix;
107 }
108
109 SkPaint paint;
110 paint.setAlpha(layer->getAlpha());
111 paint.setBlendMode(layer->getMode());
112 paint.setColorFilter(layer->getColorFilter());
113 const bool nonIdentityMatrix = !matrix.isIdentity();
114 if (nonIdentityMatrix) {
115 canvas->save();
116 canvas->concat(matrix);
117 }
118 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
119 if (dstRect || srcRect) {
120 SkMatrix matrixInv;
121 if (!matrix.invert(&matrixInv)) {
122 matrixInv = matrix;
123 }
124 SkRect skiaSrcRect;
125 if (srcRect) {
126 skiaSrcRect = *srcRect;
127 } else {
128 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
129 }
130 matrixInv.mapRect(&skiaSrcRect);
131 SkRect skiaDestRect;
132 if (dstRect) {
133 skiaDestRect = *dstRect;
134 } else {
135 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
136 }
137 matrixInv.mapRect(&skiaDestRect);
138 // If (matrix is a rect-to-rect transform)
139 // and (src/dst buffers size match in screen coordinates)
140 // and (src/dst corners align fractionally),
141 // then use nearest neighbor, otherwise use bilerp sampling.
142 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
143 // only for SrcOver blending and without color filter (readback uses Src blending).
144 SkSamplingOptions sampling(SkFilterMode::kNearest);
145 if (layer->getForceFilter() ||
146 shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
147 sampling = SkSamplingOptions(SkFilterMode::kLinear);
148 }
149 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint,
150 SkCanvas::kFast_SrcRectConstraint);
151 } else {
152 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
153 SkSamplingOptions sampling(SkFilterMode::kNearest);
154 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
155 sampling = SkSamplingOptions(SkFilterMode::kLinear);
156 }
157 canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint);
158 }
159 // restore the original matrix
160 if (nonIdentityMatrix) {
161 canvas->restore();
162 }
163 }
164
165 return layerImage != nullptr;
166 }
167
168 } // namespace skiapipeline
169 } // namespace uirenderer
170 } // namespace android
171