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> |
| 21 | #include <shaders/shaders.h> |
| 22 | |
| 23 | #include "utils/Color.h" |
| 24 | |
| 25 | namespace android::uirenderer { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class ColorFilterRuntimeEffectBuilder : public SkRuntimeEffectBuilder { |
| 30 | public: |
| 31 | explicit ColorFilterRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect) |
| 32 | : SkRuntimeEffectBuilder(std::move(effect)) {} |
| 33 | |
| 34 | sk_sp<SkColorFilter> makeColorFilter() { |
| 35 | return this->effect()->makeColorFilter(this->uniforms()); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | static sk_sp<SkColorFilter> createLinearEffectColorFilter(const shaders::LinearEffect& linearEffect, |
| 40 | float maxDisplayLuminance, |
| 41 | float currentDisplayLuminanceNits, |
| 42 | float maxLuminance) { |
| 43 | auto shaderString = SkString(shaders::buildLinearEffectSkSL(linearEffect)); |
| 44 | auto [runtimeEffect, error] = SkRuntimeEffect::MakeForColorFilter(std::move(shaderString)); |
| 45 | if (!runtimeEffect) { |
| 46 | LOG_ALWAYS_FATAL("LinearColorFilter construction error: %s", error.c_str()); |
| 47 | } |
| 48 | |
| 49 | ColorFilterRuntimeEffectBuilder effectBuilder(std::move(runtimeEffect)); |
| 50 | |
| 51 | const auto uniforms = |
| 52 | shaders::buildLinearEffectUniforms(linearEffect, android::mat4(), maxDisplayLuminance, |
| 53 | currentDisplayLuminanceNits, maxLuminance); |
| 54 | |
| 55 | for (const auto& uniform : uniforms) { |
| 56 | effectBuilder.uniform(uniform.name.c_str()).set(uniform.value.data(), uniform.value.size()); |
| 57 | } |
| 58 | |
| 59 | return effectBuilder.makeColorFilter(); |
| 60 | } |
| 61 | |
| 62 | static bool extractTransfer(ui::Dataspace dataspace) { |
| 63 | return dataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 64 | } |
| 65 | |
| 66 | static bool isHdrDataspace(ui::Dataspace dataspace) { |
| 67 | const auto transfer = extractTransfer(dataspace); |
| 68 | |
| 69 | return transfer == HAL_DATASPACE_TRANSFER_ST2084 || transfer == HAL_DATASPACE_TRANSFER_HLG; |
| 70 | } |
| 71 | |
| 72 | static ui::Dataspace getDataspace(const SkImageInfo& image) { |
| 73 | return static_cast<ui::Dataspace>( |
| 74 | ColorSpaceToADataSpace(image.colorSpace(), image.colorType())); |
| 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | |
| 79 | // Given a source and destination image info, and the max content luminance, generate a tonemaping |
| 80 | // shader and tag it on the supplied paint. |
| 81 | void tonemapPaint(const SkImageInfo& source, const SkImageInfo& destination, float maxLuminanceNits, |
| 82 | SkPaint& paint) { |
| 83 | const auto sourceDataspace = getDataspace(source); |
| 84 | const auto destinationDataspace = getDataspace(destination); |
| 85 | |
| 86 | if (extractTransfer(sourceDataspace) != extractTransfer(destinationDataspace) && |
| 87 | (isHdrDataspace(sourceDataspace) || isHdrDataspace(destinationDataspace))) { |
| 88 | const auto effect = shaders::LinearEffect{ |
| 89 | .inputDataspace = sourceDataspace, |
| 90 | .outputDataspace = destinationDataspace, |
| 91 | .undoPremultipliedAlpha = source.alphaType() == kPremul_SkAlphaType, |
| 92 | .fakeInputDataspace = destinationDataspace, |
| 93 | .type = shaders::LinearEffect::SkSLType::ColorFilter}; |
| 94 | constexpr float kMaxDisplayBrightnessNits = 1000.f; |
| 95 | constexpr float kCurrentDisplayBrightnessNits = 500.f; |
| 96 | sk_sp<SkColorFilter> colorFilter = createLinearEffectColorFilter( |
| 97 | effect, kMaxDisplayBrightnessNits, kCurrentDisplayBrightnessNits, maxLuminanceNits); |
| 98 | |
| 99 | if (paint.getColorFilter()) { |
| 100 | paint.setColorFilter(SkColorFilters::Compose(paint.refColorFilter(), colorFilter)); |
| 101 | } else { |
| 102 | paint.setColorFilter(colorFilter); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } // namespace android::uirenderer |