blob: 9053c1240957bd1e4d947a68e15b2e012faecee7 [file] [log] [blame]
Chet Haased15ebf22012-09-05 11:40:29 -07001/*
2 * Copyright (C) 2012 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
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "Layer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070018
Chris Craik65fe5ee2015-01-26 18:06:29 -080019#include "renderstate/RenderState.h"
Stan Iliev564ca3e2018-09-04 22:00:00 +000020#include "utils/Color.h"
Nader Jawad55f19762020-11-25 15:30:20 -080021#include "utils/MathUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080022
Sally Qi7e3f93b2021-07-15 00:00:54 +000023#include <log/log.h>
24
Chet Haased15ebf22012-09-05 11:40:29 -070025namespace android {
26namespace uirenderer {
27
Stan Iliev564ca3e2018-09-04 22:00:00 +000028Layer::Layer(RenderState& renderState, sk_sp<SkColorFilter> colorFilter, int alpha,
29 SkBlendMode mode)
Stan Iliev1a025a72018-09-05 16:35:11 -040030 : mRenderState(renderState)
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040031 , mColorFilter(colorFilter)
sergeyv3e9999b2017-01-19 15:37:02 -080032 , alpha(alpha)
33 , mode(mode) {
John Reck0e89e2b2014-10-31 14:49:06 -070034 // TODO: This is a violation of Android's typical ref counting, but it
35 // preserves the old inc/dec ref locations. This should be changed...
Chris Craikd41c4d82015-01-05 15:51:13 -080036 incStrong(nullptr);
John Reck0e89e2b2014-10-31 14:49:06 -070037 renderState.registerLayer(this);
Stan Iliev564ca3e2018-09-04 22:00:00 +000038 transform.setIdentity();
Chet Haase603f6de2012-09-14 15:31:25 -070039}
40
Chet Haased15ebf22012-09-05 11:40:29 -070041Layer::~Layer() {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050042 mRenderState.unregisterLayer(this);
John Reck57998012015-01-29 10:17:57 -080043}
44
John Reck0e89e2b2014-10-31 14:49:06 -070045void Layer::postDecStrong() {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050046 mRenderState.postDecStrong(this);
John Reck0e89e2b2014-10-31 14:49:06 -070047}
48
Stan Iliev8fc3d8e2018-09-28 18:44:26 -040049SkBlendMode Layer::getMode() const {
50 if (mBlend || mode != SkBlendMode::kSrcOver) {
51 return mode;
52 } else {
53 return SkBlendMode::kSrc;
54 }
55}
56
Nader Jawad55f19762020-11-25 15:30:20 -080057static inline SkScalar isIntegerAligned(SkScalar x) {
John Reck9a7c1922021-02-08 19:32:21 -050058 return MathUtils::isZero(roundf(x) - x);
Nader Jawad55f19762020-11-25 15:30:20 -080059}
60
61// Disable filtering when there is no scaling in screen coordinates and the corners have the same
62// fraction (for translate) or zero fraction (for any other rect-to-rect transform).
63static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
64 if (!matrix.rectStaysRect()) return true;
65 SkRect dstDevRect = matrix.mapRect(dstRect);
66 float dstW, dstH;
67 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
68 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
69 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
70 // dimensions is sufficient, but swap width and height comparison.
71 dstW = dstDevRect.height();
72 dstH = dstDevRect.width();
73 } else {
74 // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
75 // dimensions are still safe to compare directly.
76 dstW = dstDevRect.width();
77 dstH = dstDevRect.height();
78 }
79 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
80 MathUtils::areEqual(dstH, srcRect.height()))) {
81 return true;
82 }
83 // Device rect and source rect should be integer aligned to ensure there's no difference
84 // in how nearest-neighbor sampling is resolved.
85 return !(isIntegerAligned(srcRect.x()) &&
86 isIntegerAligned(srcRect.y()) &&
87 isIntegerAligned(dstDevRect.x()) &&
88 isIntegerAligned(dstDevRect.y()));
89}
90
91void Layer::draw(SkCanvas* canvas) {
92 GrRecordingContext* context = canvas->recordingContext();
93 if (context == nullptr) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000094 ALOGD("Attempting to draw LayerDrawable into an unsupported surface");
Nader Jawad55f19762020-11-25 15:30:20 -080095 return;
96 }
97 SkMatrix layerTransform = getTransform();
98 //sk_sp<SkImage> layerImage = getImage();
99 const int layerWidth = getWidth();
100 const int layerHeight = getHeight();
101 if (layerImage) {
102 SkMatrix textureMatrixInv;
Nader Jawad55f19762020-11-25 15:30:20 -0800103 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
104 // use bottom left origin and remove flipV and invert transformations.
105 SkMatrix flipV;
106 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
107 textureMatrixInv.preConcat(flipV);
108 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
109 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
110 SkMatrix textureMatrix;
111 if (!textureMatrixInv.invert(&textureMatrix)) {
112 textureMatrix = textureMatrixInv;
113 }
114
115 SkMatrix matrix;
116 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
117
118 SkPaint paint;
119 paint.setAlpha(getAlpha());
120 paint.setBlendMode(getMode());
121 paint.setColorFilter(getColorFilter());
122 const bool nonIdentityMatrix = !matrix.isIdentity();
123 if (nonIdentityMatrix) {
124 canvas->save();
125 canvas->concat(matrix);
126 }
127 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
128
129 SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height());
Mike Reed7994a312021-01-28 18:06:26 -0500130 SkSamplingOptions sampling;
Nader Jawad55f19762020-11-25 15:30:20 -0800131 if (getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) {
Mike Reed7994a312021-01-28 18:06:26 -0500132 sampling = SkSamplingOptions(SkFilterMode::kLinear);
Nader Jawad55f19762020-11-25 15:30:20 -0800133 }
Mike Reed7994a312021-01-28 18:06:26 -0500134 canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint);
Nader Jawad55f19762020-11-25 15:30:20 -0800135 // restore the original matrix
136 if (nonIdentityMatrix) {
137 canvas->restore();
138 }
139 }
140}
141
Chris Blume7b8a8082018-11-30 15:51:58 -0800142} // namespace uirenderer
143} // namespace android