blob: e32788c9ec515188a44b89e4cce7c0a1243b4e64 [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) {
bsears9df09ccf2021-08-06 15:18:26 +000032 DrawLayer(canvas->recordingContext(), 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) {
John Reck9a7c1922021-02-08 19:32:21 -050037 return MathUtils::isZero(roundf(x) - x);
Stan Iliev694f3e42019-07-29 17:00:49 -040038}
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
Adlai Hollerf8c434e2020-07-27 11:42:45 -040070// TODO: Context arg probably doesn't belong here – do debug check at callsite instead.
71bool LayerDrawable::DrawLayer(GrRecordingContext* context,
72 SkCanvas* canvas,
73 Layer* layer,
74 const SkRect* srcRect,
75 const SkRect* dstRect,
Stan Iliev1a025a72018-09-05 16:35:11 -040076 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040077 if (context == nullptr) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000078 ALOGD("Attempting to draw LayerDrawable into an unsupported surface");
Stan Ilieve9d00122017-09-19 12:07:10 -040079 return false;
80 }
Stan Iliev021693b2016-10-17 16:26:15 -040081 // transform the matrix based on the layer
bsears9df09ccf2021-08-06 15:18:26 +000082 SkMatrix layerTransform = layer->getTransform();
Stan Iliev564ca3e2018-09-04 22:00:00 +000083 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040084 const int layerWidth = layer->getWidth();
85 const int layerHeight = layer->getHeight();
bsears9df09ccf2021-08-06 15:18:26 +000086
Stan Iliev021693b2016-10-17 16:26:15 -040087 if (layerImage) {
bsears9df09ccf2021-08-06 15:18:26 +000088 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
Stan Ilievaac878f2018-07-12 16:53:59 -0400102 SkMatrix matrix;
Stan Iliev1a025a72018-09-05 16:35:11 -0400103 if (useLayerTransform) {
bsears9df09ccf2021-08-06 15:18:26 +0000104 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
105 } else {
106 matrix = textureMatrix;
Stan Ilievaac878f2018-07-12 16:53:59 -0400107 }
bsears9df09ccf2021-08-06 15:18:26 +0000108
Stan Iliev021693b2016-10-17 16:26:15 -0400109 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500110 paint.setAlpha(layer->getAlpha());
111 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400112 paint.setColorFilter(layer->getColorFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -0400113 const bool nonIdentityMatrix = !matrix.isIdentity();
114 if (nonIdentityMatrix) {
115 canvas->save();
116 canvas->concat(matrix);
117 }
bsears9df09ccf2021-08-06 15:18:26 +0000118 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
Stan Iliev1a025a72018-09-05 16:35:11 -0400119 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400120 SkMatrix matrixInv;
121 if (!matrix.invert(&matrixInv)) {
122 matrixInv = matrix;
123 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400124 SkRect skiaSrcRect;
bsears9df09ccf2021-08-06 15:18:26 +0000125 if (srcRect) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400126 skiaSrcRect = *srcRect;
127 } else {
bsears9df09ccf2021-08-06 15:18:26 +0000128 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
Stan Iliev1a025a72018-09-05 16:35:11 -0400129 }
130 matrixInv.mapRect(&skiaSrcRect);
131 SkRect skiaDestRect;
bsears9df09ccf2021-08-06 15:18:26 +0000132 if (dstRect) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400133 skiaDestRect = *dstRect;
134 } else {
135 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
136 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400137 matrixInv.mapRect(&skiaDestRect);
bsears9df09ccf2021-08-06 15:18:26 +0000138 // 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).
Mike Reed7994a312021-01-28 18:06:26 -0500144 SkSamplingOptions sampling(SkFilterMode::kNearest);
Stan Iliev134372d2019-07-10 16:46:09 -0400145 if (layer->getForceFilter() ||
146 shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
Mike Reed7994a312021-01-28 18:06:26 -0500147 sampling = SkSamplingOptions(SkFilterMode::kLinear);
Stan Ilievaa0a3312018-10-19 15:26:08 -0400148 }
Mike Reed7994a312021-01-28 18:06:26 -0500149 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint,
bsears9df09ccf2021-08-06 15:18:26 +0000150 SkCanvas::kFast_SrcRectConstraint);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400151 } else {
Stan Iliev134372d2019-07-10 16:46:09 -0400152 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
Mike Reed7994a312021-01-28 18:06:26 -0500153 SkSamplingOptions sampling(SkFilterMode::kNearest);
Stan Iliev134372d2019-07-10 16:46:09 -0400154 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
Mike Reed7994a312021-01-28 18:06:26 -0500155 sampling = SkSamplingOptions(SkFilterMode::kLinear);
Stan Ilievaa0a3312018-10-19 15:26:08 -0400156 }
Mike Reed7994a312021-01-28 18:06:26 -0500157 canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400158 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400159 // restore the original matrix
160 if (nonIdentityMatrix) {
161 canvas->restore();
162 }
Stan Iliev021693b2016-10-17 16:26:15 -0400163 }
bsears9df09ccf2021-08-06 15:18:26 +0000164
Ben Wagner6b62ac02018-05-29 14:16:02 -0400165 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400166}
167
Chris Blume7b8a8082018-11-30 15:51:58 -0800168} // namespace skiapipeline
169} // namespace uirenderer
170} // namespace android