blob: 8f3366c2f91280de68383d1790ec8304e2425169 [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"
Alec Mourid0001fe2021-11-22 10:09:22 -080018
Nolan Scobie572d8012024-08-30 13:43:34 -040019#include <include/gpu/ganesh/GrBackendSurface.h>
20#include <include/gpu/ganesh/gl/GrGLTypes.h>
Alec Mourid0001fe2021-11-22 10:09:22 -080021#include <shaders/shaders.h>
22#include <utils/Color.h>
John Reck283bb462018-12-13 16:40:14 -080023#include <utils/MathUtils.h>
Greg Daniel45ec62b2017-01-04 14:27:00 -050024
Alec Mourid0001fe2021-11-22 10:09:22 -080025#include "DeviceInfo.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040026#include "SkColorFilter.h"
Alec Mourid0001fe2021-11-22 10:09:22 -080027#include "SkRuntimeEffect.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050028#include "SkSurface.h"
Alec Mouri97326742022-11-04 01:32:24 +000029#include "Tonemapper.h"
Alec Mourid0001fe2021-11-22 10:09:22 -080030#include "math/mat4.h"
31#include "system/graphics-base-v1.0.h"
ramindani3952ed62021-08-12 15:55:12 +000032#include "system/window.h"
Stan Iliev021693b2016-10-17 16:26:15 -040033
34namespace android {
35namespace uirenderer {
36namespace skiapipeline {
37
38void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040039 Layer* layer = mLayerUpdater->backingLayer();
40 if (layer) {
ramindani3952ed62021-08-12 15:55:12 +000041 SkRect srcRect = layer->getCurrentCropRect();
42 DrawLayer(canvas->recordingContext(), canvas, layer, &srcRect, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040043 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050044}
45
Stan Iliev694f3e42019-07-29 17:00:49 -040046static inline SkScalar isIntegerAligned(SkScalar x) {
John Reck9a7c1922021-02-08 19:32:21 -050047 return MathUtils::isZero(roundf(x) - x);
Stan Iliev694f3e42019-07-29 17:00:49 -040048}
49
Stan Iliev134372d2019-07-10 16:46:09 -040050// Disable filtering when there is no scaling in screen coordinates and the corners have the same
51// fraction (for translate) or zero fraction (for any other rect-to-rect transform).
52static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
53 if (!matrix.rectStaysRect()) return true;
54 SkRect dstDevRect = matrix.mapRect(dstRect);
55 float dstW, dstH;
Stan Iliev134372d2019-07-10 16:46:09 -040056 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
57 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
58 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
59 // dimensions is sufficient, but swap width and height comparison.
60 dstW = dstDevRect.height();
61 dstH = dstDevRect.width();
Stan Iliev134372d2019-07-10 16:46:09 -040062 } else {
63 // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
64 // dimensions are still safe to compare directly.
65 dstW = dstDevRect.width();
66 dstH = dstDevRect.height();
Stan Iliev134372d2019-07-10 16:46:09 -040067 }
68 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
69 MathUtils::areEqual(dstH, srcRect.height()))) {
70 return true;
71 }
Stan Iliev019adb02019-09-26 14:29:48 -040072 // Device rect and source rect should be integer aligned to ensure there's no difference
73 // in how nearest-neighbor sampling is resolved.
74 return !(isIntegerAligned(srcRect.x()) &&
75 isIntegerAligned(srcRect.y()) &&
76 isIntegerAligned(dstDevRect.x()) &&
77 isIntegerAligned(dstDevRect.y()));
John Reck0aff62d2018-11-26 16:41:34 -080078}
79
John Reck8df7f3c2022-09-29 13:19:54 -040080static void adjustCropForYUV(uint32_t format, int bufferWidth, int bufferHeight, SkRect* cropRect) {
81 // Chroma channels of YUV420 images are subsampled we may need to shrink the crop region by
82 // a whole texel on each side. Since skia still adds its own 0.5 inset, we apply an
83 // additional 0.5 inset. See GLConsumer::computeTransformMatrix for details.
84 float shrinkAmount = 0.0f;
85 switch (format) {
86 // Use HAL formats since some AHB formats are only available in vndk
87 case HAL_PIXEL_FORMAT_YCBCR_420_888:
88 case HAL_PIXEL_FORMAT_YV12:
89 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
90 shrinkAmount = 0.5f;
91 break;
92 default:
93 break;
94 }
95
96 // Shrink the crop if it has more than 1-px and differs from the buffer size.
97 if (cropRect->width() > 1 && cropRect->width() < bufferWidth) {
98 cropRect->inset(shrinkAmount, 0);
99 }
100
101 if (cropRect->height() > 1 && cropRect->height() < bufferHeight) {
102 cropRect->inset(0, shrinkAmount);
103 }
104}
105
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400106// TODO: Context arg probably doesn't belong here – do debug check at callsite instead.
107bool LayerDrawable::DrawLayer(GrRecordingContext* context,
108 SkCanvas* canvas,
109 Layer* layer,
110 const SkRect* srcRect,
111 const SkRect* dstRect,
Stan Iliev1a025a72018-09-05 16:35:11 -0400112 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400113 if (context == nullptr) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000114 ALOGD("Attempting to draw LayerDrawable into an unsupported surface");
Stan Ilieve9d00122017-09-19 12:07:10 -0400115 return false;
116 }
Stan Iliev021693b2016-10-17 16:26:15 -0400117 // transform the matrix based on the layer
ramindani3952ed62021-08-12 15:55:12 +0000118 // SkMatrix layerTransform = layer->getTransform();
119 const uint32_t windowTransform = layer->getWindowTransform();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000120 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400121 const int layerWidth = layer->getWidth();
122 const int layerHeight = layer->getHeight();
bsears9df09ccf2021-08-06 15:18:26 +0000123
Stan Iliev021693b2016-10-17 16:26:15 -0400124 if (layerImage) {
ramindani3952ed62021-08-12 15:55:12 +0000125 const int imageWidth = layerImage->width();
126 const int imageHeight = layerImage->height();
bsears9df09ccf2021-08-06 15:18:26 +0000127
Stan Iliev1a025a72018-09-05 16:35:11 -0400128 if (useLayerTransform) {
ramindani3952ed62021-08-12 15:55:12 +0000129 canvas->save();
130 canvas->concat(layer->getTransform());
Stan Ilievaac878f2018-07-12 16:53:59 -0400131 }
bsears9df09ccf2021-08-06 15:18:26 +0000132
Stan Iliev021693b2016-10-17 16:26:15 -0400133 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500134 paint.setAlpha(layer->getAlpha());
135 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400136 paint.setColorFilter(layer->getColorFilter());
bsears9df09ccf2021-08-06 15:18:26 +0000137 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
ramindani98ccd612021-08-25 15:45:03 +0000138 SkRect skiaSrcRect;
139 if (srcRect && !srcRect->isEmpty()) {
140 skiaSrcRect = *srcRect;
John Reck8df7f3c2022-09-29 13:19:54 -0400141 adjustCropForYUV(layer->getBufferFormat(), imageWidth, imageHeight, &skiaSrcRect);
ramindani98ccd612021-08-25 15:45:03 +0000142 } else {
143 skiaSrcRect = SkRect::MakeIWH(imageWidth, imageHeight);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400144 }
ramindani98ccd612021-08-25 15:45:03 +0000145 SkRect skiaDestRect;
146 if (dstRect && !dstRect->isEmpty()) {
147 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
148 ? SkRect::MakeIWH(dstRect->height(), dstRect->width())
149 : SkRect::MakeIWH(dstRect->width(), dstRect->height());
150 } else {
151 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
152 ? SkRect::MakeIWH(layerHeight, layerWidth)
153 : SkRect::MakeIWH(layerWidth, layerHeight);
154 }
155
156 const float px = skiaDestRect.centerX();
157 const float py = skiaDestRect.centerY();
158 SkMatrix m;
159 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
160 m.postScale(-1.f, 1.f, px, py);
161 }
162 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
163 m.postScale(1.f, -1.f, px, py);
164 }
165 if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
166 m.postRotate(90, 0, 0);
167 m.postTranslate(skiaDestRect.height(), 0);
168 }
169 auto constraint = SkCanvas::kFast_SrcRectConstraint;
170 if (srcRect && !srcRect->isEmpty()) {
171 constraint = SkCanvas::kStrict_SrcRectConstraint;
172 }
173
174 canvas->save();
175 canvas->concat(m);
176
177 // If (matrix is a rect-to-rect transform)
178 // and (src/dst buffers size match in screen coordinates)
179 // and (src/dst corners align fractionally),
180 // then use nearest neighbor, otherwise use bilerp sampling.
181 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
182 // only for SrcOver blending and without color filter (readback uses Src blending).
183 SkSamplingOptions sampling(SkFilterMode::kNearest);
184 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
185 sampling = SkSamplingOptions(SkFilterMode::kLinear);
186 }
187
Alec Mouri97326742022-11-04 01:32:24 +0000188 tonemapPaint(layerImage->imageInfo(), canvas->imageInfo(), layer->getMaxLuminanceNits(),
189 paint);
190 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint,
191 constraint);
Alec Mourid0001fe2021-11-22 10:09:22 -0800192
ramindani98ccd612021-08-25 15:45:03 +0000193 canvas->restore();
Stan Ilieva73b0be2017-10-06 10:16:58 -0400194 // restore the original matrix
ramindani3952ed62021-08-12 15:55:12 +0000195 if (useLayerTransform) {
Stan Ilieva73b0be2017-10-06 10:16:58 -0400196 canvas->restore();
197 }
Stan Iliev021693b2016-10-17 16:26:15 -0400198 }
bsears9df09ccf2021-08-06 15:18:26 +0000199
Ben Wagner6b62ac02018-05-29 14:16:02 -0400200 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400201}
202
Chris Blume7b8a8082018-11-30 15:51:58 -0800203} // namespace skiapipeline
204} // namespace uirenderer
205} // namespace android