blob: f839213e90079a2aa82ece3c1aadf9895eb9cd3d [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;
Stan Iliev134372d2019-07-10 16:46:09 -040046 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();
Stan Iliev134372d2019-07-10 16:46:09 -040052 } 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();
Stan Iliev134372d2019-07-10 16:46:09 -040057 }
58 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
59 MathUtils::areEqual(dstH, srcRect.height()))) {
60 return true;
61 }
Stan Iliev019adb02019-09-26 14:29:48 -040062 // 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()));
John Reck0aff62d2018-11-26 16:41:34 -080068}
69
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040070bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
Stan Iliev1a025a72018-09-05 16:35:11 -040071 const SkRect* srcRect, const SkRect* dstRect,
72 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040073 if (context == nullptr) {
74 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
75 return false;
76 }
Stan Iliev021693b2016-10-17 16:26:15 -040077 // transform the matrix based on the layer
Stan Iliev564ca3e2018-09-04 22:00:00 +000078 SkMatrix layerTransform = layer->getTransform();
79 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040080 const int layerWidth = layer->getWidth();
81 const int layerHeight = layer->getHeight();
Greg Daniel45ec62b2017-01-04 14:27:00 -050082
Stan Iliev021693b2016-10-17 16:26:15 -040083 if (layerImage) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040084 SkMatrix textureMatrixInv;
Stan Iliev564ca3e2018-09-04 22:00:00 +000085 textureMatrixInv = layer->getTexTransform();
John Reck1bcacfd2017-11-03 10:12:19 -070086 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Iliev944dbf22017-09-27 11:05:29 -040087 // 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);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040090 textureMatrixInv.preConcat(flipV);
91 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +000092 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040093 SkMatrix textureMatrix;
94 if (!textureMatrixInv.invert(&textureMatrix)) {
95 textureMatrix = textureMatrixInv;
Stan Iliev944dbf22017-09-27 11:05:29 -040096 }
97
Stan Ilievaac878f2018-07-12 16:53:59 -040098 SkMatrix matrix;
Stan Iliev1a025a72018-09-05 16:35:11 -040099 if (useLayerTransform) {
Stan Ilievaac878f2018-07-12 16:53:59 -0400100 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
Stan Iliev1a025a72018-09-05 16:35:11 -0400101 } else {
102 matrix = textureMatrix;
Stan Ilievaac878f2018-07-12 16:53:59 -0400103 }
Stan Iliev944dbf22017-09-27 11:05:29 -0400104
Stan Iliev021693b2016-10-17 16:26:15 -0400105 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500106 paint.setAlpha(layer->getAlpha());
107 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400108 paint.setColorFilter(layer->getColorFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -0400109 const bool nonIdentityMatrix = !matrix.isIdentity();
110 if (nonIdentityMatrix) {
111 canvas->save();
112 canvas->concat(matrix);
113 }
Stan Ilievaa0a3312018-10-19 15:26:08 -0400114 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
Stan Iliev1a025a72018-09-05 16:35:11 -0400115 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400116 SkMatrix matrixInv;
117 if (!matrix.invert(&matrixInv)) {
118 matrixInv = matrix;
119 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400120 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 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400133 matrixInv.mapRect(&skiaDestRect);
Stan Iliev134372d2019-07-10 16:46:09 -0400134 // 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),
Stan Ilievaa0a3312018-10-19 15:26:08 -0400137 // then use nearest neighbor, otherwise use bilerp sampling.
Stan Ilievaa0a3312018-10-19 15:26:08 -0400138 // 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).
Stan Iliev134372d2019-07-10 16:46:09 -0400140 if (layer->getForceFilter() ||
141 shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400142 paint.setFilterQuality(kLow_SkFilterQuality);
143 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400144 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400145 SkCanvas::kFast_SrcRectConstraint);
146 } else {
Stan Iliev134372d2019-07-10 16:46:09 -0400147 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
148 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400149 paint.setFilterQuality(kLow_SkFilterQuality);
150 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400151 canvas->drawImage(layerImage.get(), 0, 0, &paint);
152 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400153 // restore the original matrix
154 if (nonIdentityMatrix) {
155 canvas->restore();
156 }
Stan Iliev021693b2016-10-17 16:26:15 -0400157 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500158
Ben Wagner6b62ac02018-05-29 14:16:02 -0400159 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400160}
161
Chris Blume7b8a8082018-11-30 15:51:58 -0800162} // namespace skiapipeline
163} // namespace uirenderer
164} // namespace android