Debian Codegraph @

src/shaders/SkWorkingColorSpaceShader.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
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 "src/shaders/SkWorkingColorSpaceShader.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImageInfo.h"
15 #include "src/base/SkArenaAlloc.h"
16 #include "src/core/SkColorSpaceXformSteps.h"
17 #include "src/core/SkEffectPriv.h"
18 #include "src/core/SkReadBuffer.h"
19 #include "src/core/SkWriteBuffer.h"
20 #include "src/shaders/SkShaderBase.h"
21
22 #include <utility>
23
24 sk_sp<SkShader> SkWorkingColorSpaceShader::Make(sk_sp<SkShader> shader,
25 sk_sp<SkColorSpace> inputCS,
26 sk_sp<SkColorSpace> outputCS,
27 bool workInUnpremul) {
28 if (!shader) {
29 return nullptr;
30 }
31
32 if (!inputCS && !outputCS && !workInUnpremul) {
33 // A null input is the final dst CS, and a null output is the input CS, so if both are null
34 // then there's no additional conversion for children and no additional conversion
35 // applied to the shader's output.
36 return shader;
37 } else {
38 // Otherwise there's some conversion that has to happen on the input side or the output side
39 // that makes this not a no-op.
40 return sk_sp(new SkWorkingColorSpaceShader(std::move(shader),
41 std::move(inputCS),
42 std::move(outputCS),
43 workInUnpremul));
44 }
45 }
46
47 bool SkWorkingColorSpaceShader::appendStages(const SkStageRec& rec,
48 const SkShaders::MatrixRec& mRec) const {
49 sk_sp<SkColorSpace> dstCS = sk_ref_sp(rec.fDstCS);
50 if (!dstCS) {
51 dstCS = SkColorSpace::MakeSRGB();
52 }
53
54 // TODO(b/431253455): Should get the dstAT from `rec`
55 const SkAlphaType dstAT = kPremul_SkAlphaType;
56 auto [inputCS, outputCS, workingAT] = this->workingSpace(dstCS, dstAT);
57
58 SkColorInfo dst = {rec.fDstColorType, dstAT, dstCS},
59 input = {rec.fDstColorType, workingAT, inputCS},
60 output = {rec.fDstColorType, workingAT, outputCS};
61
62 const auto* dstToInput = rec.fAlloc->make<SkColorSpaceXformSteps>(dst, input);
63 const auto* outputToDst = rec.fAlloc->make<SkColorSpaceXformSteps>(output, dst);
64 // NOTE: There is no inputToOutput steps to apply because it is assumed that the child shader
65 // is responsible for such conversion (or input == output and it's a no-op).
66
67 // Alpha-only image shaders reference the paint color, which is already in the destination
68 // color space. We need to transform it to the working space for consistency.
69 SkColor4f paintColorInWorkingSpace = rec.fPaintColor.makeOpaque();
70 dstToInput->apply(paintColorInWorkingSpace.vec());
71
72 // TODO(b/431253455): The working rec should have its alpha type set to `workingAT`
73 SkStageRec workingRec = {rec.fPipeline,
74 rec.fAlloc,
75 rec.fDstColorType,
76 fInputSpace.get(),
77 paintColorInWorkingSpace,
78 rec.fSurfaceProps,
79 rec.fDstBounds};
80
81 if (!as_SB(fShader)->appendStages(workingRec, mRec)) {
82 return false;
83 }
84
85 outputToDst->apply(rec.fPipeline);
86 return true;
87 }
88
89 void SkWorkingColorSpaceShader::flatten(SkWriteBuffer& buffer) const {
90 buffer.writeFlattenable(fShader.get());
91 buffer.writeBool(fWorkInUnpremul);
92
93 buffer.writeBool(SkToBool(fInputSpace));
94 if (fInputSpace) {
95 buffer.writeDataAsByteArray(fInputSpace->serialize().get());
96 }
97
98 buffer.writeBool(SkToBool(fOutputSpace));
99 if (fOutputSpace) {
100 buffer.writeDataAsByteArray(fOutputSpace->serialize().get());
101 }
102 }
103
104 sk_sp<SkFlattenable> SkWorkingColorSpaceShader::CreateProc(SkReadBuffer& buffer) {
105 sk_sp<SkShader> shader(buffer.readShader());
106
107 // If true, will not work in unpremul and assume inputSpace will be non-null and the outputSpace
108 // will be null.
109 const bool legacyWorkingCS = buffer.isVersionLT(SkPicturePriv::kWorkingColorSpaceOutput);
110
111 bool workInUnpremul = !legacyWorkingCS && buffer.readBool();
112
113 // The input/output spaces are allowed to be null, but if we think we have a non-null CS, then
114 // it better be deserializable.
115 sk_sp<SkColorSpace> inputSpace;
116 if (legacyWorkingCS || buffer.readBool()) {
117 auto data = buffer.readByteArrayAsData();
118 if (!buffer.validate(data != nullptr)) {
119 return nullptr;
120 }
121 inputSpace = SkColorSpace::Deserialize(data->data(), data->size());
122 if (!buffer.validate(inputSpace != nullptr)) {
123 return nullptr;
124 }
125 }
126
127 sk_sp<SkColorSpace> outputSpace;
128 if (!legacyWorkingCS && buffer.readBool()) {
129 auto data = buffer.readByteArrayAsData();
130 if (!buffer.validate(data != nullptr)) {
131 return nullptr;
132 }
133 outputSpace = SkColorSpace::Deserialize(data->data(), data->size());
134 if (!buffer.validate(outputSpace != nullptr)) {
135 return nullptr;
136 }
137 }
138
139 return Make(std::move(shader), std::move(inputSpace), std::move(outputSpace), workInUnpremul);
140 }
141
142 void SkRegisterWorkingColorSpaceShaderFlattenable() {
143 SK_REGISTER_FLATTENABLE(SkWorkingColorSpaceShader);
144 }