Debian Codegraph @

src/gpu/graphite/precompile/PaintOption.cpp

raw
1 /*
2 * Copyright 2024 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/gpu/graphite/precompile/PaintOption.h"
9
10 #include "include/core/SkBlender.h"
11 #include "include/gpu/graphite/precompile/PrecompileBlender.h"
12 #include "include/gpu/graphite/precompile/PrecompileColorFilter.h"
13 #include "include/gpu/graphite/precompile/PrecompileShader.h"
14 #include "src/core/SkBlendModePriv.h"
15 #include "src/core/SkColorSpacePriv.h"
16 #include "src/gpu/graphite/ContextUtils.h"
17 #include "src/gpu/graphite/KeyContext.h"
18 #include "src/gpu/graphite/KeyHelpers.h"
19 #include "src/gpu/graphite/PaintParams.h"
20 #include "src/gpu/graphite/PaintParamsKey.h"
21 #include "src/gpu/graphite/PrecompileInternal.h"
22 #include "src/gpu/graphite/precompile/PrecompileBasePriv.h"
23 #include "src/gpu/graphite/precompile/PrecompileBlenderPriv.h"
24 #include "src/gpu/graphite/precompile/PrecompileShaderPriv.h"
25
26 namespace skgpu::graphite {
27
28 PaintOption::PaintOption(bool opaquePaintColor,
29 const std::pair<sk_sp<PrecompileBlender>, int>& finalBlender,
30 const std::pair<sk_sp<PrecompileShader>, int>& shader,
31 const std::pair<sk_sp<PrecompileColorFilter>, int>& colorFilter,
32 bool hasPrimitiveBlender,
33 SkBlendMode primitiveBlendMode,
34 bool skipColorXform,
35 const std::pair<sk_sp<PrecompileShader>, int>& clipShader,
36 Coverage coverage,
37 TextureFormat targetFormat,
38 bool dither,
39 bool analyticClip)
40 : fOpaquePaintColor(opaquePaintColor)
41 , fFinalBlender(finalBlender)
42 , fShader(shader)
43 , fColorFilter(colorFilter)
44 , fPrimitiveBlendMode(primitiveBlendMode)
45 , fHasPrimitiveBlender(hasPrimitiveBlender)
46 , fSkipColorXform(skipColorXform)
47 , fClipShader(clipShader)
48 , fRendererCoverage(coverage)
49 , fTargetFormat(targetFormat)
50 , fDither(dither)
51 , fAnalyticClip(analyticClip) {
52 if (!fHasPrimitiveBlender) {
53 if (fShader.first && fShader.first->priv().isConstant(fShader.second)) {
54 fShader = { nullptr, 0 };
55 }
56 if (!fShader.first && fColorFilter.first) {
57 fColorFilter = { nullptr, 0 };
58 }
59 }
60 }
61
62 void PaintOption::toKey(const KeyContext& keyContext) const {
63 // Root Node 0 is the source color, which is the output of all effects post dithering
64 this->handleDithering(keyContext);
65
66 // Root Node 1 is the final blender
67 std::optional<SkBlendMode> finalBlendMode =
68 this->finalBlender() ? this->finalBlender()->priv().asBlendMode()
69 : SkBlendMode::kSrcOver;
70
71 Coverage finalCoverage = fRendererCoverage;
72 if ((fClipShader.first || fAnalyticClip) && fRendererCoverage == Coverage::kNone) {
73 finalCoverage = Coverage::kSingleChannel;
74 }
75 bool dstReadReq = !finalBlendMode.has_value() ||
76 !CanUseHardwareBlending(keyContext.caps(),
77 fTargetFormat,
78 *finalBlendMode,
79 finalCoverage);
80
81 if (finalBlendMode) {
82 if (!dstReadReq) {
83 AddFixedBlendMode(keyContext, *finalBlendMode);
84 } else {
85 AddBlendMode(keyContext, *finalBlendMode);
86 }
87 } else {
88 SkASSERT(this->finalBlender());
89 fFinalBlender.first->priv().addToKey(keyContext, fFinalBlender.second);
90 }
91
92 // Optional Root Node 2 is the clip
93 this->handleClipping(keyContext);
94 }
95
96 void PaintOption::addPaintColorToKey(const KeyContext& keyContext) const {
97 if (fShader.first) {
98 fShader.first->priv().addToKey(keyContext, fShader.second);
99 } else {
100 RGBPaintColorBlock::AddBlock(keyContext);
101 }
102 }
103
104 void PaintOption::handlePrimitiveColor(const KeyContext& keyContext) const {
105 if (!fHasPrimitiveBlender) {
106 this->addPaintColorToKey(keyContext);
107 return;
108 }
109
110 if (fSkipColorXform && fPrimitiveBlendMode == SkBlendMode::kDst) {
111 AddPrimitiveColor(keyContext, fSkipColorXform);
112 return;
113 }
114
115 Blend(keyContext,
116 /* addBlendToKey= */ [&] () -> void {
117 /**
118 * TODO: Allow clients to provide precompile SkBlender options for primitive
119 * blending. For now we have a back door to internally specify an SkBlendMode.
120 */
121 AddToKey(keyContext, GetBlendModeSingleton(fPrimitiveBlendMode));
122 },
123 /* addSrcToKey= */ [&]() -> void {
124 this->addPaintColorToKey(keyContext);
125 },
126 /* addDstToKey= */ [&]() -> void {
127 AddPrimitiveColor(keyContext, fSkipColorXform);
128 });
129 }
130
131 void PaintOption::handlePaintAlpha(const KeyContext& keyContext) const {
132
133 if (!fShader.first && !fHasPrimitiveBlender) {
134 // If there is no shader and no primitive blending the input to the colorFilter stage
135 // is just the premultiplied paint color.
136 SolidColorShaderBlock::AddBlock(keyContext, SK_PMColor4fWHITE);
137 return;
138 }
139
140 if (!fOpaquePaintColor) {
141 Blend(keyContext,
142 /* addBlendToKey= */ [&] () -> void {
143 AddFixedBlendMode(keyContext, SkBlendMode::kSrcIn);
144 },
145 /* addSrcToKey= */ [&]() -> void {
146 this->handlePrimitiveColor(keyContext);
147 },
148 /* addDstToKey= */ [&]() -> void {
149 AlphaOnlyPaintColorBlock::AddBlock(keyContext);
150 });
151 } else {
152 this->handlePrimitiveColor(keyContext);
153 }
154 }
155
156 void PaintOption::handleColorFilter(const KeyContext& keyContext) const {
157 if (fColorFilter.first) {
158 Compose(keyContext,
159 /* addInnerToKey= */ [&]() -> void {
160 this->handlePaintAlpha(keyContext);
161 },
162 /* addOuterToKey= */ [&]() -> void {
163 fColorFilter.first->priv().addToKey(keyContext, fColorFilter.second);
164 });
165 } else {
166 this->handlePaintAlpha(keyContext);
167 }
168 }
169
170 // This should be kept in sync w/ SkPaintPriv::ShouldDither and PaintParams::should_dither
171 bool PaintOption::shouldDither(SkColorType dstCT) const {
172 // The paint dither flag can veto.
173 if (!fDither) {
174 return false;
175 }
176
177 if (dstCT == kUnknown_SkColorType) {
178 return false;
179 }
180
181 // We always dither 565 or 4444 when requested.
182 if (dstCT == kRGB_565_SkColorType || dstCT == kARGB_4444_SkColorType) {
183 return true;
184 }
185
186 // Otherwise, dither is only needed for non-const paints.
187 return fShader.first && !fShader.first->priv().isConstant(fShader.second);
188 }
189
190 void PaintOption::handleDithering(const KeyContext& keyContext) const {
191
192 #ifndef SK_IGNORE_GPU_DITHER
193 SkColorType ct = keyContext.dstColorInfo().colorType();
194 if (this->shouldDither(ct)) {
195 Compose(keyContext,
196 /* addInnerToKey= */ [&]() -> void {
197 this->handleColorFilter(keyContext);
198 },
199 /* addOuterToKey= */ [&]() -> void {
200 AddDitherBlock(keyContext, ct);
201 });
202 } else
203 #endif
204 {
205 this->handleColorFilter(keyContext);
206 }
207 }
208
209 void PaintOption::handleClipping(const KeyContext& keyContext) const {
210 if (fAnalyticClip) {
211 NonMSAAClipBlock::NonMSAAClipData data(
212 /* rect= */ {},
213 /* radiusPlusHalf= */ {},
214 /* edgeSelect= */ {},
215 /* texCoordOffset= */ {},
216 /* maskBounds= */ {},
217 // TODO: the kAnalyticAndAtlasClip vs. kAnalyticClip decision is based on this
218 // being a valid TextureProxy.
219 /* atlasTexture= */ nullptr);
220 if (fClipShader.first) {
221 // For both an analytic clip and clip shader, we need to compose them together into
222 // a single clipping root node.
223 Blend(keyContext,
224 /* addBlendToKey= */ [&]() -> void {
225 AddFixedBlendMode(keyContext, SkBlendMode::kModulate);
226 },
227 /* addSrcToKey= */ [&]() -> void {
228 NonMSAAClipBlock::AddBlock(keyContext, data);
229 },
230 /* addDstToKey= */ [&]() -> void {
231 fClipShader.first->priv().addToKey(keyContext, fClipShader.second);
232 });
233 } else {
234 // Without a clip shader, the analytic clip can be the clipping root node.
235 NonMSAAClipBlock::AddBlock(keyContext, data);
236 }
237 } else if (fClipShader.first) {
238 // Since there's no analytic clip, the clipping root node can be fClipShader directly.
239 fClipShader.first->priv().addToKey(keyContext, fClipShader.second);
240 }
241 }
242
243 } // namespace skgpu::graphite