blob: 14396569ccdd501d6a8d11000ee6e3572bad089d [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"
ramindani3952ed62021-08-12 15:55:12 +000024#include "system/window.h"
Stan Iliev021693b2016-10-17 16:26:15 -040025
26namespace android {
27namespace uirenderer {
28namespace skiapipeline {
29
30void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040031 Layer* layer = mLayerUpdater->backingLayer();
32 if (layer) {
ramindani3952ed62021-08-12 15:55:12 +000033 SkRect srcRect = layer->getCurrentCropRect();
34 DrawLayer(canvas->recordingContext(), canvas, layer, &srcRect, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040035 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050036}
37
Stan Iliev694f3e42019-07-29 17:00:49 -040038static inline SkScalar isIntegerAligned(SkScalar x) {
John Reck9a7c1922021-02-08 19:32:21 -050039 return MathUtils::isZero(roundf(x) - x);
Stan Iliev694f3e42019-07-29 17:00:49 -040040}
41
Stan Iliev134372d2019-07-10 16:46:09 -040042// Disable filtering when there is no scaling in screen coordinates and the corners have the same
43// fraction (for translate) or zero fraction (for any other rect-to-rect transform).
44static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
45 if (!matrix.rectStaysRect()) return true;
46 SkRect dstDevRect = matrix.mapRect(dstRect);
47 float dstW, dstH;
Stan Iliev134372d2019-07-10 16:46:09 -040048 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
49 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
50 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
51 // dimensions is sufficient, but swap width and height comparison.
52 dstW = dstDevRect.height();
53 dstH = dstDevRect.width();
Stan Iliev134372d2019-07-10 16:46:09 -040054 } 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();
Stan Iliev134372d2019-07-10 16:46:09 -040059 }
60 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
61 MathUtils::areEqual(dstH, srcRect.height()))) {
62 return true;
63 }
Stan Iliev019adb02019-09-26 14:29:48 -040064 // Device rect and source rect should be integer aligned to ensure there's no difference
65 // in how nearest-neighbor sampling is resolved.
66 return !(isIntegerAligned(srcRect.x()) &&
67 isIntegerAligned(srcRect.y()) &&
68 isIntegerAligned(dstDevRect.x()) &&
69 isIntegerAligned(dstDevRect.y()));
John Reck0aff62d2018-11-26 16:41:34 -080070}
71
Adlai Hollerf8c434e2020-07-27 11:42:45 -040072// TODO: Context arg probably doesn't belong here – do debug check at callsite instead.
73bool LayerDrawable::DrawLayer(GrRecordingContext* context,
74 SkCanvas* canvas,
75 Layer* layer,
76 const SkRect* srcRect,
77 const SkRect* dstRect,
Stan Iliev1a025a72018-09-05 16:35:11 -040078 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040079 if (context == nullptr) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000080 ALOGD("Attempting to draw LayerDrawable into an unsupported surface");
Stan Ilieve9d00122017-09-19 12:07:10 -040081 return false;
82 }
Stan Iliev021693b2016-10-17 16:26:15 -040083 // transform the matrix based on the layer
ramindani3952ed62021-08-12 15:55:12 +000084 // SkMatrix layerTransform = layer->getTransform();
85 const uint32_t windowTransform = layer->getWindowTransform();
Stan Iliev564ca3e2018-09-04 22:00:00 +000086 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040087 const int layerWidth = layer->getWidth();
88 const int layerHeight = layer->getHeight();
bsears9df09ccf2021-08-06 15:18:26 +000089
Stan Iliev021693b2016-10-17 16:26:15 -040090 if (layerImage) {
ramindani3952ed62021-08-12 15:55:12 +000091 const int imageWidth = layerImage->width();
92 const int imageHeight = layerImage->height();
bsears9df09ccf2021-08-06 15:18:26 +000093
Stan Iliev1a025a72018-09-05 16:35:11 -040094 if (useLayerTransform) {
ramindani3952ed62021-08-12 15:55:12 +000095 canvas->save();
96 canvas->concat(layer->getTransform());
Stan Ilievaac878f2018-07-12 16:53:59 -040097 }
bsears9df09ccf2021-08-06 15:18:26 +000098
Stan Iliev021693b2016-10-17 16:26:15 -040099 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500100 paint.setAlpha(layer->getAlpha());
101 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400102 paint.setColorFilter(layer->getColorFilter());
bsears9df09ccf2021-08-06 15:18:26 +0000103 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
ramindani98ccd612021-08-25 15:45:03 +0000104 SkRect skiaSrcRect;
105 if (srcRect && !srcRect->isEmpty()) {
106 skiaSrcRect = *srcRect;
107 } else {
108 skiaSrcRect = SkRect::MakeIWH(imageWidth, imageHeight);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400109 }
ramindani98ccd612021-08-25 15:45:03 +0000110 SkRect skiaDestRect;
111 if (dstRect && !dstRect->isEmpty()) {
112 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
113 ? SkRect::MakeIWH(dstRect->height(), dstRect->width())
114 : SkRect::MakeIWH(dstRect->width(), dstRect->height());
115 } else {
116 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
117 ? SkRect::MakeIWH(layerHeight, layerWidth)
118 : SkRect::MakeIWH(layerWidth, layerHeight);
119 }
120
121 const float px = skiaDestRect.centerX();
122 const float py = skiaDestRect.centerY();
123 SkMatrix m;
124 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
125 m.postScale(-1.f, 1.f, px, py);
126 }
127 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
128 m.postScale(1.f, -1.f, px, py);
129 }
130 if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
131 m.postRotate(90, 0, 0);
132 m.postTranslate(skiaDestRect.height(), 0);
133 }
134 auto constraint = SkCanvas::kFast_SrcRectConstraint;
135 if (srcRect && !srcRect->isEmpty()) {
136 constraint = SkCanvas::kStrict_SrcRectConstraint;
137 }
138
139 canvas->save();
140 canvas->concat(m);
141
142 // If (matrix is a rect-to-rect transform)
143 // and (src/dst buffers size match in screen coordinates)
144 // and (src/dst corners align fractionally),
145 // then use nearest neighbor, otherwise use bilerp sampling.
146 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
147 // only for SrcOver blending and without color filter (readback uses Src blending).
148 SkSamplingOptions sampling(SkFilterMode::kNearest);
149 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
150 sampling = SkSamplingOptions(SkFilterMode::kLinear);
151 }
152
153 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint,
154 constraint);
155 canvas->restore();
Stan Ilieva73b0be2017-10-06 10:16:58 -0400156 // restore the original matrix
ramindani3952ed62021-08-12 15:55:12 +0000157 if (useLayerTransform) {
Stan Ilieva73b0be2017-10-06 10:16:58 -0400158 canvas->restore();
159 }
Stan Iliev021693b2016-10-17 16:26:15 -0400160 }
bsears9df09ccf2021-08-06 15:18:26 +0000161
Ben Wagner6b62ac02018-05-29 14:16:02 -0400162 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400163}
164
Chris Blume7b8a8082018-11-30 15:51:58 -0800165} // namespace skiapipeline
166} // namespace uirenderer
167} // namespace android