| 1 |
/* |
| 2 |
* Copyright 2023 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/private/SkGainmapShader.h" |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkShader.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/effects/SkRuntimeEffect.h" |
| 17 |
#include "include/private/SkGainmapInfo.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "src/core/SkImageInfoPriv.h" |
| 20 |
|
| 21 |
#include <cmath> |
| 22 |
#include <cstdint> |
| 23 |
|
| 24 |
static constexpr char gGainmapSKSL[] = |
| 25 |
"uniform shader base;" |
| 26 |
"uniform shader gainmap;" |
| 27 |
"uniform half4 logRatioMin;" |
| 28 |
"uniform half4 logRatioMax;" |
| 29 |
"uniform half4 gainmapGamma;" |
| 30 |
"uniform half4 epsilonBase;" |
| 31 |
"uniform half4 epsilonOther;" |
| 32 |
"uniform half W;" |
| 33 |
"uniform int gainmapIsAlpha;" |
| 34 |
"uniform int gainmapIsRed;" |
| 35 |
"uniform int singleChannel;" |
| 36 |
"uniform int noGamma;" |
| 37 |
"uniform int isApple;" |
| 38 |
"uniform half appleG;" |
| 39 |
"uniform half appleH;" |
| 40 |
"" |
| 41 |
"half4 main(float2 coord) {" |
| 42 |
"half4 S = base.eval(coord);" |
| 43 |
"half4 G = gainmap.eval(coord);" |
| 44 |
"if (gainmapIsAlpha == 1) {" |
| 45 |
"G = half4(G.a, G.a, G.a, 1.0);" |
| 46 |
"}" |
| 47 |
"if (gainmapIsRed == 1) {" |
| 48 |
"G = half4(G.r, G.r, G.r, 1.0);" |
| 49 |
"}" |
| 50 |
"if (singleChannel == 1) {" |
| 51 |
"half L;" |
| 52 |
"if (isApple == 1) {" |
| 53 |
"L = pow(G.r, appleG);" |
| 54 |
"L = log(1.0 + (appleH - 1.0) * pow(G.r, appleG));" |
| 55 |
"} else if (noGamma == 1) {" |
| 56 |
"L = mix(logRatioMin.r, logRatioMax.r, G.r);" |
| 57 |
"} else {" |
| 58 |
"L = mix(logRatioMin.r, logRatioMax.r, pow(G.r, gainmapGamma.r));" |
| 59 |
"}" |
| 60 |
"half3 H = (S.rgb + epsilonBase.rgb) * exp(L * W) - epsilonOther.rgb;" |
| 61 |
"return half4(H.r, H.g, H.b, S.a);" |
| 62 |
"} else {" |
| 63 |
"half3 L;" |
| 64 |
"if (isApple == 1) {" |
| 65 |
"L = pow(G.rgb, half3(appleG));" |
| 66 |
"L = log(half3(1.0) + (appleH - 1.0) * L);" |
| 67 |
"} else if (noGamma == 1) {" |
| 68 |
"L = mix(logRatioMin.rgb, logRatioMax.rgb, G.rgb);" |
| 69 |
"} else {" |
| 70 |
"L = mix(logRatioMin.rgb, logRatioMax.rgb, pow(G.rgb, gainmapGamma.rgb));" |
| 71 |
"}" |
| 72 |
"half3 H = (S.rgb + epsilonBase.rgb) * exp(L * W) - epsilonOther.rgb;" |
| 73 |
"return half4(H.r, H.g, H.b, S.a);" |
| 74 |
"}" |
| 75 |
"}"; |
| 76 |
|
| 77 |
static sk_sp<SkRuntimeEffect> gainmap_apply_effect() { |
| 78 |
static const SkRuntimeEffect* effect = |
| 79 |
SkRuntimeEffect::MakeForShader(SkString(gGainmapSKSL), {}).effect.release(); |
| 80 |
SkASSERT(effect); |
| 81 |
return sk_ref_sp(effect); |
| 82 |
} |
| 83 |
|
| 84 |
static bool all_channels_equal(const SkColor4f& c) { |
| 85 |
return c.fR == c.fG && c.fR == c.fB; |
| 86 |
} |
| 87 |
|
| 88 |
sk_sp<SkShader> SkGainmapShader::Make(const sk_sp<const SkImage>& baseImage, |
| 89 |
const SkRect& baseRect, |
| 90 |
const SkSamplingOptions& baseSamplingOptions, |
| 91 |
const sk_sp<const SkImage>& gainmapImage, |
| 92 |
const SkRect& gainmapRect, |
| 93 |
const SkSamplingOptions& gainmapSamplingOptions, |
| 94 |
const SkGainmapInfo& gainmapInfo, |
| 95 |
const SkRect& dstRect, |
| 96 |
float dstHdrRatio, |
| 97 |
sk_sp<SkColorSpace> dstColorSpace) { |
| 98 |
return Make(baseImage, baseRect, baseSamplingOptions, gainmapImage, gainmapRect, |
| 99 |
gainmapSamplingOptions, gainmapInfo, dstRect, dstHdrRatio); |
| 100 |
} |
| 101 |
|
| 102 |
sk_sp<SkShader> SkGainmapShader::Make(const sk_sp<const SkImage>& baseImage, |
| 103 |
const SkRect& baseRect, |
| 104 |
const SkSamplingOptions& baseSamplingOptions, |
| 105 |
const sk_sp<const SkImage>& gainmapImage, |
| 106 |
const SkRect& gainmapRect, |
| 107 |
const SkSamplingOptions& gainmapSamplingOptions, |
| 108 |
const SkGainmapInfo& gainmapInfo, |
| 109 |
const SkRect& dstRect, |
| 110 |
float dstHdrRatio) { |
| 111 |
sk_sp<SkColorSpace> baseColorSpace = |
| 112 |
baseImage->colorSpace() ? baseImage->refColorSpace() : SkColorSpace::MakeSRGB(); |
| 113 |
|
| 114 |
// Determine the color space in which the gainmap math is to be applied. |
| 115 |
sk_sp<SkColorSpace> gainmapMathColorSpace = |
| 116 |
gainmapInfo.fGainmapMathColorSpace |
| 117 |
? gainmapInfo.fGainmapMathColorSpace->makeLinearGamma() |
| 118 |
: baseColorSpace->makeLinearGamma(); |
| 119 |
|
| 120 |
// Compute the sampling transformation matrices. |
| 121 |
const SkMatrix baseRectToDstRect = SkMatrix::RectToRectOrIdentity(baseRect, dstRect); |
| 122 |
const SkMatrix gainmapRectToDstRect = SkMatrix::RectToRectOrIdentity(gainmapRect, dstRect); |
| 123 |
|
| 124 |
// Compute the weight parameter that will be used to blend between the images. |
| 125 |
float W = 0.f; |
| 126 |
if (dstHdrRatio > gainmapInfo.fDisplayRatioSdr) { |
| 127 |
if (dstHdrRatio < gainmapInfo.fDisplayRatioHdr) { |
| 128 |
W = (std::log(dstHdrRatio) - std::log(gainmapInfo.fDisplayRatioSdr)) / |
| 129 |
(std::log(gainmapInfo.fDisplayRatioHdr) - |
| 130 |
std::log(gainmapInfo.fDisplayRatioSdr)); |
| 131 |
} else { |
| 132 |
W = 1.f; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
const bool baseImageIsHdr = (gainmapInfo.fBaseImageType == SkGainmapInfo::BaseImageType::kHDR); |
| 137 |
if (baseImageIsHdr) { |
| 138 |
W -= 1.f; |
| 139 |
} |
| 140 |
|
| 141 |
// Return the base image directly if the gainmap will not be applied at all. |
| 142 |
if (W == 0.f) { |
| 143 |
return baseImage->makeShader(baseSamplingOptions, &baseRectToDstRect); |
| 144 |
} |
| 145 |
|
| 146 |
// The base image will have color space conversion performed. |
| 147 |
auto baseImageShader = baseImage->makeShader(baseSamplingOptions, &baseRectToDstRect); |
| 148 |
|
| 149 |
// The gainmap image shader will ignore any color space that the gainmap has. |
| 150 |
auto gainmapImageShader = |
| 151 |
gainmapImage->makeRawShader(gainmapSamplingOptions, &gainmapRectToDstRect); |
| 152 |
|
| 153 |
// Create the shader to apply the gainmap in the gain application color space. |
| 154 |
sk_sp<SkShader> gainmapMathShader; |
| 155 |
{ |
| 156 |
SkRuntimeShaderBuilder builder(gainmap_apply_effect()); |
| 157 |
const SkColor4f logRatioMin({std::log(gainmapInfo.fGainmapRatioMin.fR), |
| 158 |
std::log(gainmapInfo.fGainmapRatioMin.fG), |
| 159 |
std::log(gainmapInfo.fGainmapRatioMin.fB), |
| 160 |
1.f}); |
| 161 |
const SkColor4f logRatioMax({std::log(gainmapInfo.fGainmapRatioMax.fR), |
| 162 |
std::log(gainmapInfo.fGainmapRatioMax.fG), |
| 163 |
std::log(gainmapInfo.fGainmapRatioMax.fB), |
| 164 |
1.f}); |
| 165 |
const int noGamma = |
| 166 |
gainmapInfo.fGainmapGamma.fR == 1.f && |
| 167 |
gainmapInfo.fGainmapGamma.fG == 1.f && |
| 168 |
gainmapInfo.fGainmapGamma.fB == 1.f; |
| 169 |
const uint32_t colorTypeFlags = SkColorTypeChannelFlags(gainmapImage->colorType()); |
| 170 |
const int gainmapIsAlpha = colorTypeFlags == kAlpha_SkColorChannelFlag; |
| 171 |
const int gainmapIsRed = colorTypeFlags == kRed_SkColorChannelFlag; |
| 172 |
const int singleChannel = all_channels_equal(gainmapInfo.fGainmapGamma) && |
| 173 |
all_channels_equal(gainmapInfo.fGainmapRatioMin) && |
| 174 |
all_channels_equal(gainmapInfo.fGainmapRatioMax) && |
| 175 |
(colorTypeFlags == kGray_SkColorChannelFlag || |
| 176 |
colorTypeFlags == kAlpha_SkColorChannelFlag || |
| 177 |
colorTypeFlags == kRed_SkColorChannelFlag); |
| 178 |
const SkColor4f& epsilonBase = |
| 179 |
baseImageIsHdr ? gainmapInfo.fEpsilonHdr : gainmapInfo.fEpsilonSdr; |
| 180 |
const SkColor4f& epsilonOther = |
| 181 |
baseImageIsHdr ? gainmapInfo.fEpsilonSdr : gainmapInfo.fEpsilonHdr; |
| 182 |
|
| 183 |
const int isApple = gainmapInfo.fType == SkGainmapInfo::Type::kApple; |
| 184 |
const float appleG = 1.961f; |
| 185 |
const float appleH = gainmapInfo.fDisplayRatioHdr; |
| 186 |
|
| 187 |
builder.child("base") = baseImageShader; |
| 188 |
builder.child("gainmap") = gainmapImageShader; |
| 189 |
builder.uniform("logRatioMin") = logRatioMin; |
| 190 |
builder.uniform("logRatioMax") = logRatioMax; |
| 191 |
builder.uniform("gainmapGamma") = gainmapInfo.fGainmapGamma; |
| 192 |
builder.uniform("epsilonBase") = epsilonBase; |
| 193 |
builder.uniform("epsilonOther") = epsilonOther; |
| 194 |
builder.uniform("noGamma") = noGamma; |
| 195 |
builder.uniform("singleChannel") = singleChannel; |
| 196 |
builder.uniform("gainmapIsAlpha") = gainmapIsAlpha; |
| 197 |
builder.uniform("gainmapIsRed") = gainmapIsRed; |
| 198 |
builder.uniform("W") = W; |
| 199 |
|
| 200 |
builder.uniform("isApple") = isApple; |
| 201 |
builder.uniform("appleG") = appleG; |
| 202 |
builder.uniform("appleH") = appleH; |
| 203 |
|
| 204 |
gainmapMathShader = builder.makeShader(); |
| 205 |
SkASSERT(gainmapMathShader); |
| 206 |
} |
| 207 |
|
| 208 |
return gainmapMathShader->makeWithWorkingColorSpace(gainmapMathColorSpace); |
| 209 |
} |