Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 1 | /* |
| 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 Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 18 | |
| 19 | #include <shaders/shaders.h> |
| 20 | #include <utils/Color.h> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 21 | #include <utils/MathUtils.h> |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 22 | |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 23 | #include "DeviceInfo.h" |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 24 | #include "GrBackendSurface.h" |
Derek Sollenberger | f87da67 | 2016-11-02 11:34:27 -0400 | [diff] [blame] | 25 | #include "SkColorFilter.h" |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 26 | #include "SkRuntimeEffect.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 27 | #include "SkSurface.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 28 | #include "gl/GrGLTypes.h" |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 29 | #include "math/mat4.h" |
| 30 | #include "system/graphics-base-v1.0.h" |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 31 | #include "system/window.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace skiapipeline { |
| 36 | |
| 37 | void LayerDrawable::onDraw(SkCanvas* canvas) { |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 38 | Layer* layer = mLayerUpdater->backingLayer(); |
| 39 | if (layer) { |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 40 | SkRect srcRect = layer->getCurrentCropRect(); |
| 41 | DrawLayer(canvas->recordingContext(), canvas, layer, &srcRect, nullptr, true); |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 42 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Stan Iliev | 694f3e4 | 2019-07-29 17:00:49 -0400 | [diff] [blame] | 45 | static inline SkScalar isIntegerAligned(SkScalar x) { |
John Reck | 9a7c192 | 2021-02-08 19:32:21 -0500 | [diff] [blame] | 46 | return MathUtils::isZero(roundf(x) - x); |
Stan Iliev | 694f3e4 | 2019-07-29 17:00:49 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 49 | // 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). |
| 51 | static 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 Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 55 | 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 Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 61 | } 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 Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 66 | } |
| 67 | if (!(MathUtils::areEqual(dstW, srcRect.width()) && |
| 68 | MathUtils::areEqual(dstH, srcRect.height()))) { |
| 69 | return true; |
| 70 | } |
Stan Iliev | 019adb0 | 2019-09-26 14:29:48 -0400 | [diff] [blame] | 71 | // 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 Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 79 | static sk_sp<SkShader> createLinearEffectShader(sk_sp<SkShader> shader, |
| 80 | const shaders::LinearEffect& linearEffect, |
Alec Mouri | 43df223 | 2022-01-14 11:58:39 -0800 | [diff] [blame] | 81 | float maxDisplayLuminance, |
| 82 | float currentDisplayLuminanceNits, |
| 83 | float maxLuminance) { |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 84 | auto shaderString = SkString(shaders::buildLinearEffectSkSL(linearEffect)); |
| 85 | auto [runtimeEffect, error] = SkRuntimeEffect::MakeForShader(std::move(shaderString)); |
| 86 | if (!runtimeEffect) { |
| 87 | LOG_ALWAYS_FATAL("LinearColorFilter construction error: %s", error.c_str()); |
| 88 | } |
| 89 | |
| 90 | SkRuntimeShaderBuilder effectBuilder(std::move(runtimeEffect)); |
| 91 | |
| 92 | effectBuilder.child("child") = std::move(shader); |
| 93 | |
Alec Mouri | 43df223 | 2022-01-14 11:58:39 -0800 | [diff] [blame] | 94 | const auto uniforms = shaders::buildLinearEffectUniforms( |
| 95 | linearEffect, mat4(), maxDisplayLuminance, currentDisplayLuminanceNits, maxLuminance); |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 96 | |
| 97 | for (const auto& uniform : uniforms) { |
| 98 | effectBuilder.uniform(uniform.name.c_str()).set(uniform.value.data(), uniform.value.size()); |
| 99 | } |
| 100 | |
Brian Osman | bb7c43d | 2022-02-03 14:48:12 -0500 | [diff] [blame] | 101 | return effectBuilder.makeShader(); |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static bool isHdrDataspace(ui::Dataspace dataspace) { |
| 105 | const auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 106 | |
| 107 | return transfer == HAL_DATASPACE_TRANSFER_ST2084 || transfer == HAL_DATASPACE_TRANSFER_HLG; |
| 108 | } |
| 109 | |
John Reck | 8df7f3c | 2022-09-29 13:19:54 -0400 | [diff] [blame] | 110 | static void adjustCropForYUV(uint32_t format, int bufferWidth, int bufferHeight, SkRect* cropRect) { |
| 111 | // Chroma channels of YUV420 images are subsampled we may need to shrink the crop region by |
| 112 | // a whole texel on each side. Since skia still adds its own 0.5 inset, we apply an |
| 113 | // additional 0.5 inset. See GLConsumer::computeTransformMatrix for details. |
| 114 | float shrinkAmount = 0.0f; |
| 115 | switch (format) { |
| 116 | // Use HAL formats since some AHB formats are only available in vndk |
| 117 | case HAL_PIXEL_FORMAT_YCBCR_420_888: |
| 118 | case HAL_PIXEL_FORMAT_YV12: |
| 119 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 120 | shrinkAmount = 0.5f; |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | // Shrink the crop if it has more than 1-px and differs from the buffer size. |
| 127 | if (cropRect->width() > 1 && cropRect->width() < bufferWidth) { |
| 128 | cropRect->inset(shrinkAmount, 0); |
| 129 | } |
| 130 | |
| 131 | if (cropRect->height() > 1 && cropRect->height() < bufferHeight) { |
| 132 | cropRect->inset(0, shrinkAmount); |
| 133 | } |
| 134 | } |
| 135 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 136 | // TODO: Context arg probably doesn't belong here – do debug check at callsite instead. |
| 137 | bool LayerDrawable::DrawLayer(GrRecordingContext* context, |
| 138 | SkCanvas* canvas, |
| 139 | Layer* layer, |
| 140 | const SkRect* srcRect, |
| 141 | const SkRect* dstRect, |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 142 | bool useLayerTransform) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 143 | if (context == nullptr) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 144 | ALOGD("Attempting to draw LayerDrawable into an unsupported surface"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 145 | return false; |
| 146 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 147 | // transform the matrix based on the layer |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 148 | // SkMatrix layerTransform = layer->getTransform(); |
| 149 | const uint32_t windowTransform = layer->getWindowTransform(); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 150 | sk_sp<SkImage> layerImage = layer->getImage(); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 151 | const int layerWidth = layer->getWidth(); |
| 152 | const int layerHeight = layer->getHeight(); |
bsears | 9df09ccf | 2021-08-06 15:18:26 +0000 | [diff] [blame] | 153 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 154 | if (layerImage) { |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 155 | const int imageWidth = layerImage->width(); |
| 156 | const int imageHeight = layerImage->height(); |
bsears | 9df09ccf | 2021-08-06 15:18:26 +0000 | [diff] [blame] | 157 | |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 158 | if (useLayerTransform) { |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 159 | canvas->save(); |
| 160 | canvas->concat(layer->getTransform()); |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 161 | } |
bsears | 9df09ccf | 2021-08-06 15:18:26 +0000 | [diff] [blame] | 162 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 163 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 164 | paint.setAlpha(layer->getAlpha()); |
| 165 | paint.setBlendMode(layer->getMode()); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 166 | paint.setColorFilter(layer->getColorFilter()); |
bsears | 9df09ccf | 2021-08-06 15:18:26 +0000 | [diff] [blame] | 167 | const SkMatrix& totalMatrix = canvas->getTotalMatrix(); |
ramindani | 98ccd61 | 2021-08-25 15:45:03 +0000 | [diff] [blame] | 168 | SkRect skiaSrcRect; |
| 169 | if (srcRect && !srcRect->isEmpty()) { |
| 170 | skiaSrcRect = *srcRect; |
John Reck | 8df7f3c | 2022-09-29 13:19:54 -0400 | [diff] [blame] | 171 | adjustCropForYUV(layer->getBufferFormat(), imageWidth, imageHeight, &skiaSrcRect); |
ramindani | 98ccd61 | 2021-08-25 15:45:03 +0000 | [diff] [blame] | 172 | } else { |
| 173 | skiaSrcRect = SkRect::MakeIWH(imageWidth, imageHeight); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 174 | } |
ramindani | 98ccd61 | 2021-08-25 15:45:03 +0000 | [diff] [blame] | 175 | SkRect skiaDestRect; |
| 176 | if (dstRect && !dstRect->isEmpty()) { |
| 177 | skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 178 | ? SkRect::MakeIWH(dstRect->height(), dstRect->width()) |
| 179 | : SkRect::MakeIWH(dstRect->width(), dstRect->height()); |
| 180 | } else { |
| 181 | skiaDestRect = (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 182 | ? SkRect::MakeIWH(layerHeight, layerWidth) |
| 183 | : SkRect::MakeIWH(layerWidth, layerHeight); |
| 184 | } |
| 185 | |
| 186 | const float px = skiaDestRect.centerX(); |
| 187 | const float py = skiaDestRect.centerY(); |
| 188 | SkMatrix m; |
| 189 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 190 | m.postScale(-1.f, 1.f, px, py); |
| 191 | } |
| 192 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 193 | m.postScale(1.f, -1.f, px, py); |
| 194 | } |
| 195 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 196 | m.postRotate(90, 0, 0); |
| 197 | m.postTranslate(skiaDestRect.height(), 0); |
| 198 | } |
| 199 | auto constraint = SkCanvas::kFast_SrcRectConstraint; |
| 200 | if (srcRect && !srcRect->isEmpty()) { |
| 201 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 202 | } |
| 203 | |
| 204 | canvas->save(); |
| 205 | canvas->concat(m); |
| 206 | |
| 207 | // If (matrix is a rect-to-rect transform) |
| 208 | // and (src/dst buffers size match in screen coordinates) |
| 209 | // and (src/dst corners align fractionally), |
| 210 | // then use nearest neighbor, otherwise use bilerp sampling. |
| 211 | // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works |
| 212 | // only for SrcOver blending and without color filter (readback uses Src blending). |
| 213 | SkSamplingOptions sampling(SkFilterMode::kNearest); |
| 214 | if (layer->getForceFilter() || shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) { |
| 215 | sampling = SkSamplingOptions(SkFilterMode::kLinear); |
| 216 | } |
| 217 | |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 218 | const auto sourceDataspace = static_cast<ui::Dataspace>( |
| 219 | ColorSpaceToADataSpace(layerImage->colorSpace(), layerImage->colorType())); |
| 220 | const SkImageInfo& imageInfo = canvas->imageInfo(); |
| 221 | const auto destinationDataspace = static_cast<ui::Dataspace>( |
| 222 | ColorSpaceToADataSpace(imageInfo.colorSpace(), imageInfo.colorType())); |
| 223 | |
| 224 | if (isHdrDataspace(sourceDataspace) || isHdrDataspace(destinationDataspace)) { |
| 225 | const auto effect = shaders::LinearEffect{ |
| 226 | .inputDataspace = sourceDataspace, |
| 227 | .outputDataspace = destinationDataspace, |
| 228 | .undoPremultipliedAlpha = layerImage->alphaType() == kPremul_SkAlphaType, |
| 229 | .fakeInputDataspace = destinationDataspace}; |
| 230 | auto shader = layerImage->makeShader(sampling, |
| 231 | SkMatrix::RectToRect(skiaSrcRect, skiaDestRect)); |
| 232 | constexpr float kMaxDisplayBrightess = 1000.f; |
Alec Mouri | 43df223 | 2022-01-14 11:58:39 -0800 | [diff] [blame] | 233 | constexpr float kCurrentDisplayBrightness = 500.f; |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 234 | shader = createLinearEffectShader(std::move(shader), effect, kMaxDisplayBrightess, |
Alec Mouri | 43df223 | 2022-01-14 11:58:39 -0800 | [diff] [blame] | 235 | kCurrentDisplayBrightness, |
Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 236 | layer->getMaxLuminanceNits()); |
| 237 | paint.setShader(shader); |
| 238 | canvas->drawRect(skiaDestRect, paint); |
| 239 | } else { |
| 240 | canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint, |
| 241 | constraint); |
| 242 | } |
| 243 | |
ramindani | 98ccd61 | 2021-08-25 15:45:03 +0000 | [diff] [blame] | 244 | canvas->restore(); |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 245 | // restore the original matrix |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 246 | if (useLayerTransform) { |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 247 | canvas->restore(); |
| 248 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 249 | } |
bsears | 9df09ccf | 2021-08-06 15:18:26 +0000 | [diff] [blame] | 250 | |
Ben Wagner | 6b62ac0 | 2018-05-29 14:16:02 -0400 | [diff] [blame] | 251 | return layerImage != nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 254 | } // namespace skiapipeline |
| 255 | } // namespace uirenderer |
| 256 | } // namespace android |