blob: 553b08febdcc3ff3cde391fb75a53da3a46eae6c [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
19#include <shaders/shaders.h>
20#include <utils/Color.h>
John Reck283bb462018-12-13 16:40:14 -080021#include <utils/MathUtils.h>
Greg Daniel45ec62b2017-01-04 14:27:00 -050022
Alec Mourid0001fe2021-11-22 10:09:22 -080023#include "DeviceInfo.h"
Greg Danielac2d2322017-07-12 11:30:15 -040024#include "GrBackendSurface.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040025#include "SkColorFilter.h"
Alec Mourid0001fe2021-11-22 10:09:22 -080026#include "SkRuntimeEffect.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050027#include "SkSurface.h"
Stan Iliev021693b2016-10-17 16:26:15 -040028#include "gl/GrGLTypes.h"
Alec Mourid0001fe2021-11-22 10:09:22 -080029#include "math/mat4.h"
30#include "system/graphics-base-v1.0.h"
ramindani3952ed62021-08-12 15:55:12 +000031#include "system/window.h"
Stan Iliev021693b2016-10-17 16:26:15 -040032
33namespace android {
34namespace uirenderer {
35namespace skiapipeline {
36
37void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040038 Layer* layer = mLayerUpdater->backingLayer();
39 if (layer) {
ramindani3952ed62021-08-12 15:55:12 +000040 SkRect srcRect = layer->getCurrentCropRect();
41 DrawLayer(canvas->recordingContext(), canvas, layer, &srcRect, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040042 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050043}
44
Stan Iliev694f3e42019-07-29 17:00:49 -040045static inline SkScalar isIntegerAligned(SkScalar x) {
John Reck9a7c1922021-02-08 19:32:21 -050046 return MathUtils::isZero(roundf(x) - x);
Stan Iliev694f3e42019-07-29 17:00:49 -040047}
48
Stan Iliev134372d2019-07-10 16:46:09 -040049// Disable filtering when there is no scaling in screen coordinates and the corners have the same
50// fraction (for translate) or zero fraction (for any other rect-to-rect transform).
51static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) {
52 if (!matrix.rectStaysRect()) return true;
53 SkRect dstDevRect = matrix.mapRect(dstRect);
54 float dstW, dstH;
Stan Iliev134372d2019-07-10 16:46:09 -040055 if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) {
56 // Has a 90 or 270 degree rotation, although total matrix may also have scale factors
57 // in m10 and m01. Those scalings are automatically handled by mapRect so comparing
58 // dimensions is sufficient, but swap width and height comparison.
59 dstW = dstDevRect.height();
60 dstH = dstDevRect.width();
Stan Iliev134372d2019-07-10 16:46:09 -040061 } else {
62 // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but
63 // dimensions are still safe to compare directly.
64 dstW = dstDevRect.width();
65 dstH = dstDevRect.height();
Stan Iliev134372d2019-07-10 16:46:09 -040066 }
67 if (!(MathUtils::areEqual(dstW, srcRect.width()) &&
68 MathUtils::areEqual(dstH, srcRect.height()))) {
69 return true;
70 }
Stan Iliev019adb02019-09-26 14:29:48 -040071 // Device rect and source rect should be integer aligned to ensure there's no difference
72 // in how nearest-neighbor sampling is resolved.
73 return !(isIntegerAligned(srcRect.x()) &&
74 isIntegerAligned(srcRect.y()) &&
75 isIntegerAligned(dstDevRect.x()) &&
76 isIntegerAligned(dstDevRect.y()));
John Reck0aff62d2018-11-26 16:41:34 -080077}
78
Alec Mourid0001fe2021-11-22 10:09:22 -080079static sk_sp<SkShader> createLinearEffectShader(sk_sp<SkShader> shader,
80 const shaders::LinearEffect& linearEffect,
81 float maxDisplayLuminance, float maxLuminance) {
82 auto shaderString = SkString(shaders::buildLinearEffectSkSL(linearEffect));
83 auto [runtimeEffect, error] = SkRuntimeEffect::MakeForShader(std::move(shaderString));
84 if (!runtimeEffect) {
85 LOG_ALWAYS_FATAL("LinearColorFilter construction error: %s", error.c_str());
86 }
87
88 SkRuntimeShaderBuilder effectBuilder(std::move(runtimeEffect));
89
90 effectBuilder.child("child") = std::move(shader);
91
92 const auto uniforms = shaders::buildLinearEffectUniforms(linearEffect, mat4(),
93 maxDisplayLuminance, maxLuminance);
94
95 for (const auto& uniform : uniforms) {
96 effectBuilder.uniform(uniform.name.c_str()).set(uniform.value.data(), uniform.value.size());
97 }
98
99 return effectBuilder.makeShader(nullptr, false);
100}
101
102static bool isHdrDataspace(ui::Dataspace dataspace) {
103 const auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
104
105 return transfer == HAL_DATASPACE_TRANSFER_ST2084 || transfer == HAL_DATASPACE_TRANSFER_HLG;
106}
107
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400108// TODO: Context arg probably doesn't belong here – do debug check at callsite instead.
109bool LayerDrawable::DrawLayer(GrRecordingContext* context,
110 SkCanvas* canvas,
111 Layer* layer,
112 const SkRect* srcRect,
113 const SkRect* dstRect,
Stan Iliev1a025a72018-09-05 16:35:11 -0400114 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400115 if (context == nullptr) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000116 ALOGD("Attempting to draw LayerDrawable into an unsupported surface");
Stan Ilieve9d00122017-09-19 12:07:10 -0400117 return false;
118 }
Stan Iliev021693b2016-10-17 16:26:15 -0400119 // transform the matrix based on the layer
ramindani3952ed62021-08-12 15:55:12 +0000120 // SkMatrix layerTransform = layer->getTransform();
121 const uint32_t windowTransform = layer->getWindowTransform();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000122 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400123 const int layerWidth = layer->getWidth();
124 const int layerHeight = layer->getHeight();
bsears9df09ccf2021-08-06 15:18:26 +0000125
Stan Iliev021693b2016-10-17 16:26:15 -0400126 if (layerImage) {
ramindani3952ed62021-08-12 15:55:12 +0000127 const int imageWidth = layerImage->width();
128 const int imageHeight = layerImage->height();
bsears9df09ccf2021-08-06 15:18:26 +0000129
Stan Iliev1a025a72018-09-05 16:35:11 -0400130 if (useLayerTransform) {
ramindani3952ed62021-08-12 15:55:12 +0000131 canvas->save();
132 canvas->concat(layer->getTransform());
Stan Ilievaac878f2018-07-12 16:53:59 -0400133 }
bsears9df09ccf2021-08-06 15:18:26 +0000134
Stan Iliev021693b2016-10-17 16:26:15 -0400135 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500136 paint.setAlpha(layer->getAlpha());
137 paint.setBlendMode(layer->getMode());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400138 paint.setColorFilter(layer->getColorFilter());
bsears9df09ccf2021-08-06 15:18:26 +0000139 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
ramindani98ccd612021-08-25 15:45:03 +0000140 SkRect skiaSrcRect;
141 if (srcRect && !srcRect->isEmpty()) {
142 skiaSrcRect = *srcRect;
143 } else {
144 skiaSrcRect = SkRect::MakeIWH(imageWidth, imageHeight);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400145 }
ramindani98ccd612021-08-25 15:45:03 +0000146 SkRect skiaDestRect;
147 if (dstRect && !dstRect->isEmpty()) {
148 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
149 ? SkRect::MakeIWH(dstRect->height(), dstRect->width())
150 : SkRect::MakeIWH(dstRect->width(), dstRect->height());
151 } else {
152 skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90)
153 ? SkRect::MakeIWH(layerHeight, layerWidth)
154 : SkRect::MakeIWH(layerWidth, layerHeight);
155 }
156
157 const float px = skiaDestRect.centerX();
158 const float py = skiaDestRect.centerY();
159 SkMatrix m;
160 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
161 m.postScale(-1.f, 1.f, px, py);
162 }
163 if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
164 m.postScale(1.f, -1.f, px, py);
165 }
166 if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
167 m.postRotate(90, 0, 0);
168 m.postTranslate(skiaDestRect.height(), 0);
169 }
170 auto constraint = SkCanvas::kFast_SrcRectConstraint;
171 if (srcRect && !srcRect->isEmpty()) {
172 constraint = SkCanvas::kStrict_SrcRectConstraint;
173 }
174
175 canvas->save();
176 canvas->concat(m);
177
178 // If (matrix is a rect-to-rect transform)
179 // and (src/dst buffers size match in screen coordinates)
180 // and (src/dst corners align fractionally),
181 // then use nearest neighbor, otherwise use bilerp sampling.
182 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
183 // only for SrcOver blending and without color filter (readback uses Src blending).
184 SkSamplingOptions sampling(SkFilterMode::kNearest);
185 if (layer->getForceFilter() || shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) {
186 sampling = SkSamplingOptions(SkFilterMode::kLinear);
187 }
188
Alec Mourid0001fe2021-11-22 10:09:22 -0800189 const auto sourceDataspace = static_cast<ui::Dataspace>(
190 ColorSpaceToADataSpace(layerImage->colorSpace(), layerImage->colorType()));
191 const SkImageInfo& imageInfo = canvas->imageInfo();
192 const auto destinationDataspace = static_cast<ui::Dataspace>(
193 ColorSpaceToADataSpace(imageInfo.colorSpace(), imageInfo.colorType()));
194
195 if (isHdrDataspace(sourceDataspace) || isHdrDataspace(destinationDataspace)) {
196 const auto effect = shaders::LinearEffect{
197 .inputDataspace = sourceDataspace,
198 .outputDataspace = destinationDataspace,
199 .undoPremultipliedAlpha = layerImage->alphaType() == kPremul_SkAlphaType,
200 .fakeInputDataspace = destinationDataspace};
201 auto shader = layerImage->makeShader(sampling,
202 SkMatrix::RectToRect(skiaSrcRect, skiaDestRect));
203 constexpr float kMaxDisplayBrightess = 1000.f;
204 shader = createLinearEffectShader(std::move(shader), effect, kMaxDisplayBrightess,
205 layer->getMaxLuminanceNits());
206 paint.setShader(shader);
207 canvas->drawRect(skiaDestRect, paint);
208 } else {
209 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint,
210 constraint);
211 }
212
ramindani98ccd612021-08-25 15:45:03 +0000213 canvas->restore();
Stan Ilieva73b0be2017-10-06 10:16:58 -0400214 // restore the original matrix
ramindani3952ed62021-08-12 15:55:12 +0000215 if (useLayerTransform) {
Stan Ilieva73b0be2017-10-06 10:16:58 -0400216 canvas->restore();
217 }
Stan Iliev021693b2016-10-17 16:26:15 -0400218 }
bsears9df09ccf2021-08-06 15:18:26 +0000219
Ben Wagner6b62ac02018-05-29 14:16:02 -0400220 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400221}
222
Chris Blume7b8a8082018-11-30 15:51:58 -0800223} // namespace skiapipeline
224} // namespace uirenderer
225} // namespace android