Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 "Tonemapper.h" |
| 18 | |
| 19 | #include <SkRuntimeEffect.h> |
| 20 | #include <log/log.h> |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 21 | // libshaders only exists on Android devices |
| 22 | #ifdef __ANDROID__ |
Alec Mouri | 5ba9783 | 2023-07-31 20:19:38 +0000 | [diff] [blame] | 23 | #include <renderthread/CanvasContext.h> |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 24 | #include <shaders/shaders.h> |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 25 | #endif |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 26 | |
| 27 | #include "utils/Color.h" |
| 28 | |
| 29 | namespace android::uirenderer { |
| 30 | |
| 31 | namespace { |
| 32 | |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 33 | // custom tonemapping only exists on Android devices |
| 34 | #ifdef __ANDROID__ |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 35 | class ColorFilterRuntimeEffectBuilder : public SkRuntimeEffectBuilder { |
| 36 | public: |
| 37 | explicit ColorFilterRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect) |
| 38 | : SkRuntimeEffectBuilder(std::move(effect)) {} |
| 39 | |
| 40 | sk_sp<SkColorFilter> makeColorFilter() { |
| 41 | return this->effect()->makeColorFilter(this->uniforms()); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | static sk_sp<SkColorFilter> createLinearEffectColorFilter(const shaders::LinearEffect& linearEffect, |
| 46 | float maxDisplayLuminance, |
| 47 | float currentDisplayLuminanceNits, |
| 48 | float maxLuminance) { |
| 49 | auto shaderString = SkString(shaders::buildLinearEffectSkSL(linearEffect)); |
| 50 | auto [runtimeEffect, error] = SkRuntimeEffect::MakeForColorFilter(std::move(shaderString)); |
| 51 | if (!runtimeEffect) { |
| 52 | LOG_ALWAYS_FATAL("LinearColorFilter construction error: %s", error.c_str()); |
| 53 | } |
| 54 | |
| 55 | ColorFilterRuntimeEffectBuilder effectBuilder(std::move(runtimeEffect)); |
| 56 | |
Alec Mouri | 5ba9783 | 2023-07-31 20:19:38 +0000 | [diff] [blame] | 57 | auto colorTransform = android::mat4(); |
| 58 | const auto* context = renderthread::CanvasContext::getActiveContext(); |
| 59 | if (context) { |
| 60 | const auto ratio = context->targetSdrHdrRatio(); |
| 61 | if (ratio > 1.0f) { |
| 62 | colorTransform = android::mat4::scale(vec4(ratio, ratio, ratio, 1.f)); |
| 63 | } |
| 64 | } |
| 65 | |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 66 | const auto uniforms = |
Alec Mouri | 5ba9783 | 2023-07-31 20:19:38 +0000 | [diff] [blame] | 67 | shaders::buildLinearEffectUniforms(linearEffect, colorTransform, maxDisplayLuminance, |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 68 | currentDisplayLuminanceNits, maxLuminance); |
| 69 | |
| 70 | for (const auto& uniform : uniforms) { |
| 71 | effectBuilder.uniform(uniform.name.c_str()).set(uniform.value.data(), uniform.value.size()); |
| 72 | } |
| 73 | |
| 74 | return effectBuilder.makeColorFilter(); |
| 75 | } |
| 76 | |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 77 | static ui::Dataspace extractTransfer(ui::Dataspace dataspace) { |
| 78 | return static_cast<ui::Dataspace>(dataspace & HAL_DATASPACE_TRANSFER_MASK); |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static bool isHdrDataspace(ui::Dataspace dataspace) { |
| 82 | const auto transfer = extractTransfer(dataspace); |
| 83 | |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 84 | return transfer == ui::Dataspace::TRANSFER_ST2084 || transfer == ui::Dataspace::TRANSFER_HLG; |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static ui::Dataspace getDataspace(const SkImageInfo& image) { |
| 88 | return static_cast<ui::Dataspace>( |
| 89 | ColorSpaceToADataSpace(image.colorSpace(), image.colorType())); |
| 90 | } |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 91 | #endif |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | // Given a source and destination image info, and the max content luminance, generate a tonemaping |
| 96 | // shader and tag it on the supplied paint. |
| 97 | void tonemapPaint(const SkImageInfo& source, const SkImageInfo& destination, float maxLuminanceNits, |
| 98 | SkPaint& paint) { |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 99 | // custom tonemapping only exists on Android devices |
| 100 | #ifdef __ANDROID__ |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 101 | const auto sourceDataspace = getDataspace(source); |
| 102 | const auto destinationDataspace = getDataspace(destination); |
| 103 | |
| 104 | if (extractTransfer(sourceDataspace) != extractTransfer(destinationDataspace) && |
| 105 | (isHdrDataspace(sourceDataspace) || isHdrDataspace(destinationDataspace))) { |
| 106 | const auto effect = shaders::LinearEffect{ |
| 107 | .inputDataspace = sourceDataspace, |
| 108 | .outputDataspace = destinationDataspace, |
| 109 | .undoPremultipliedAlpha = source.alphaType() == kPremul_SkAlphaType, |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 110 | .type = shaders::LinearEffect::SkSLType::ColorFilter}; |
| 111 | constexpr float kMaxDisplayBrightnessNits = 1000.f; |
| 112 | constexpr float kCurrentDisplayBrightnessNits = 500.f; |
| 113 | sk_sp<SkColorFilter> colorFilter = createLinearEffectColorFilter( |
| 114 | effect, kMaxDisplayBrightnessNits, kCurrentDisplayBrightnessNits, maxLuminanceNits); |
| 115 | |
| 116 | if (paint.getColorFilter()) { |
| 117 | paint.setColorFilter(SkColorFilters::Compose(paint.refColorFilter(), colorFilter)); |
| 118 | } else { |
| 119 | paint.setColorFilter(colorFilter); |
| 120 | } |
| 121 | } |
Alec Mouri | dded51f | 2023-02-01 06:04:55 +0000 | [diff] [blame] | 122 | #else |
| 123 | return; |
| 124 | #endif |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace android::uirenderer |