blob: b0173846e582c6ff3ac7a18e087afa84e86da77a [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -04001/*
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"
John Reck283bb462018-12-13 16:40:14 -080018#include <utils/MathUtils.h>
Greg Daniel45ec62b2017-01-04 14:27:00 -050019
Greg Danielac2d2322017-07-12 11:30:15 -040020#include "GrBackendSurface.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040021#include "SkColorFilter.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050022#include "SkSurface.h"
Stan Iliev021693b2016-10-17 16:26:15 -040023#include "gl/GrGLTypes.h"
24
25namespace android {
26namespace uirenderer {
27namespace skiapipeline {
28
29void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040030 Layer* layer = mLayerUpdater->backingLayer();
31 if (layer) {
Stan Iliev1a025a72018-09-05 16:35:11 -040032 DrawLayer(canvas->getGrContext(), canvas, layer, nullptr, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040033 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050034}
35
Stan Iliev694f3e42019-07-29 17:00:49 -040036static inline SkScalar isIntegerAligned(SkScalar x) {
37 return fabsf(roundf(x) - x) <= NON_ZERO_EPSILON;
38}
39
Stan Iliev134372d2019-07-10 16:46:09 -040040// 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).
42static 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 bool requiresIntegerTranslate = false;
47 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
48 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
49 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
50 // dimensions is sufficient, but swap width and height comparison.
51 dstW = dstDevRect.height();
52 dstH = dstDevRect.width();
53 requiresIntegerTranslate = true;
54 } else {
55 // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
56 // dimensions are still safe to compare directly.
57 dstW = dstDevRect.width();
58 dstH = dstDevRect.height();
59 requiresIntegerTranslate =
60 matrix.getScaleX() < -NON_ZERO_EPSILON || matrix.getScaleY() < -NON_ZERO_EPSILON;
61 }
62 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
63 MathUtils::areEqual(dstH, srcRect.height()))) {
64 return true;
65 }
66 if (requiresIntegerTranslate) {
67 // Device rect and source rect should be integer aligned to ensure there's no difference
68 // in how nearest-neighbor sampling is resolved.
Stan Iliev694f3e42019-07-29 17:00:49 -040069 return !(isIntegerAligned(srcRect.x()) &&
70 isIntegerAligned(srcRect.y()) &&
71 isIntegerAligned(dstDevRect.x()) &&
72 isIntegerAligned(dstDevRect.y()));
Stan Iliev134372d2019-07-10 16:46:09 -040073 } else {
74 // As long as src and device rects are translated by the same fractional amount,
75 // filtering won't be needed
76 return !(MathUtils::areEqual(SkScalarFraction(srcRect.x()),
77 SkScalarFraction(dstDevRect.x())) &&
78 MathUtils::areEqual(SkScalarFraction(srcRect.y()),
79 SkScalarFraction(dstDevRect.y())));
80 }
John Reck0aff62d2018-11-26 16:41:34 -080081}
82
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040083bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
Stan Iliev1a025a72018-09-05 16:35:11 -040084 const SkRect* srcRect, const SkRect* dstRect,
85 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040086 if (context == nullptr) {
87 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
88 return false;
89 }
Stan Iliev021693b2016-10-17 16:26:15 -040090 // transform the matrix based on the layer
Stan Iliev564ca3e2018-09-04 22:00:00 +000091 SkMatrix layerTransform = layer->getTransform();
92 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040093 const int layerWidth = layer->getWidth();
94 const int layerHeight = layer->getHeight();
Greg Daniel45ec62b2017-01-04 14:27:00 -050095
Stan Iliev021693b2016-10-17 16:26:15 -040096 if (layerImage) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040097 SkMatrix textureMatrixInv;
Stan Iliev564ca3e2018-09-04 22:00:00 +000098 textureMatrixInv = layer->getTexTransform();
John Reck1bcacfd2017-11-03 10:12:19 -070099 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Iliev944dbf22017-09-27 11:05:29 -0400100 // use bottom left origin and remove flipV and invert transformations.
101 SkMatrix flipV;
102 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400103 textureMatrixInv.preConcat(flipV);
104 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000105 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400106 SkMatrix textureMatrix;
107 if (!textureMatrixInv.invert(&textureMatrix)) {
108 textureMatrix = textureMatrixInv;
Stan Iliev944dbf22017-09-27 11:05:29 -0400109 }
110
Stan Ilievaac878f2018-07-12 16:53:59 -0400111 SkMatrix matrix;
Stan Iliev1a025a72018-09-05 16:35:11 -0400112 if (useLayerTransform) {
Stan Ilievaac878f2018-07-12 16:53:59 -0400113 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
Stan Iliev1a025a72018-09-05 16:35:11 -0400114 } else {
115 matrix = textureMatrix;
Stan Ilievaac878f2018-07-12 16:53:59 -0400116 }
Stan Iliev944dbf22017-09-27 11:05:29 -0400117
Stan Iliev021693b2016-10-17 16:26:15 -0400118 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500119 paint.setAlpha(layer->getAlpha());
120 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400121 paint.setColorFilter(layer->getColorFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -0400122 const bool nonIdentityMatrix = !matrix.isIdentity();
123 if (nonIdentityMatrix) {
124 canvas->save();
125 canvas->concat(matrix);
126 }
Stan Ilievaa0a3312018-10-19 15:26:08 -0400127 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
Stan Iliev1a025a72018-09-05 16:35:11 -0400128 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400129 SkMatrix matrixInv;
130 if (!matrix.invert(&matrixInv)) {
131 matrixInv = matrix;
132 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400133 SkRect skiaSrcRect;
134 if (srcRect) {
135 skiaSrcRect = *srcRect;
136 } else {
137 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
138 }
139 matrixInv.mapRect(&skiaSrcRect);
140 SkRect skiaDestRect;
141 if (dstRect) {
142 skiaDestRect = *dstRect;
143 } else {
144 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
145 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400146 matrixInv.mapRect(&skiaDestRect);
Stan Iliev134372d2019-07-10 16:46:09 -0400147 // If (matrix is a rect-to-rect transform)
148 // and (src/dst buffers size match in screen coordinates)
149 // and (src/dst corners align fractionally),
Stan Ilievaa0a3312018-10-19 15:26:08 -0400150 // then use nearest neighbor, otherwise use bilerp sampling.
Stan Ilievaa0a3312018-10-19 15:26:08 -0400151 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
152 // only for SrcOver blending and without color filter (readback uses Src blending).
Stan Iliev134372d2019-07-10 16:46:09 -0400153 if (layer->getForceFilter() ||
154 shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400155 paint.setFilterQuality(kLow_SkFilterQuality);
156 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400157 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400158 SkCanvas::kFast_SrcRectConstraint);
159 } else {
Stan Iliev134372d2019-07-10 16:46:09 -0400160 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
161 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400162 paint.setFilterQuality(kLow_SkFilterQuality);
163 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400164 canvas->drawImage(layerImage.get(), 0, 0, &paint);
165 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400166 // restore the original matrix
167 if (nonIdentityMatrix) {
168 canvas->restore();
169 }
Stan Iliev021693b2016-10-17 16:26:15 -0400170 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500171
Ben Wagner6b62ac02018-05-29 14:16:02 -0400172 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400173}
174
Chris Blume7b8a8082018-11-30 15:51:58 -0800175} // namespace skiapipeline
176} // namespace uirenderer
177} // namespace android