Flutter Engine
The Flutter Engine
PaintParams.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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
9
16#include "src/gpu/Blend.h"
17#include "src/gpu/DitherUtils.h"
26
27namespace skgpu::graphite {
28
29namespace {
30
31// This should be kept in sync w/ SkPaintPriv::ShouldDither and PaintOption::shouldDither
32bool should_dither(const PaintParams& p, SkColorType dstCT) {
33 // The paint dither flag can veto.
34 if (!p.dither()) {
35 return false;
36 }
37
38 if (dstCT == kUnknown_SkColorType) {
39 return false;
40 }
41
42 // We always dither 565 or 4444 when requested.
43 if (dstCT == kRGB_565_SkColorType || dstCT == kARGB_4444_SkColorType) {
44 return true;
45 }
46
47 // Otherwise, dither is only needed for non-const paints.
48 return p.shader() && !as_SB(p.shader())->isConstant();
49}
50
51} // anonymous namespace
52
54 sk_sp<SkBlender> primitiveBlender,
55 sk_sp<SkShader> clipShader,
56 DstReadRequirement dstReadReq,
57 bool skipColorXform)
58 : fColor(paint.getColor4f())
59 , fFinalBlender(paint.refBlender())
60 , fShader(paint.refShader())
61 , fColorFilter(paint.refColorFilter())
62 , fPrimitiveBlender(std::move(primitiveBlender))
63 , fClipShader(std::move(clipShader))
64 , fDstReadReq(dstReadReq)
65 , fSkipColorXform(skipColorXform)
66 , fDither(paint.isDither()) {}
67
68PaintParams::PaintParams(const PaintParams& other) = default;
70PaintParams& PaintParams::operator=(const PaintParams& other) = default;
71
72std::optional<SkBlendMode> PaintParams::asFinalBlendMode() const {
73 return fFinalBlender ? as_BB(fFinalBlender)->asBlendMode()
75}
76
77sk_sp<SkBlender> PaintParams::refFinalBlender() const { return fFinalBlender; }
78
79sk_sp<SkShader> PaintParams::refShader() const { return fShader; }
80
81sk_sp<SkColorFilter> PaintParams::refColorFilter() const { return fColorFilter; }
82
83sk_sp<SkBlender> PaintParams::refPrimitiveBlender() const { return fPrimitiveBlender; }
84
86 // xform from sRGB to the destination colorspace
88 dstColorInfo.colorSpace(), kUnpremul_SkAlphaType);
89
90 SkColor4f result = srcColor;
91 steps.apply(result.vec());
92 return result;
93}
94
95void Blend(const KeyContext& keyContext,
96 PaintParamsKeyBuilder* keyBuilder,
97 PipelineDataGatherer* gatherer,
98 AddToKeyFn addBlendToKey,
99 AddToKeyFn addSrcToKey,
100 AddToKeyFn addDstToKey) {
101 BlendShaderBlock::BeginBlock(keyContext, keyBuilder, gatherer);
102
103 addSrcToKey();
104
105 addDstToKey();
106
107 addBlendToKey();
108
109 keyBuilder->endBlock(); // BlendShaderBlock
110}
111
112void Compose(const KeyContext& keyContext,
113 PaintParamsKeyBuilder* keyBuilder,
114 PipelineDataGatherer* gatherer,
115 AddToKeyFn addInnerToKey,
116 AddToKeyFn addOuterToKey) {
117 ComposeBlock::BeginBlock(keyContext, keyBuilder, gatherer);
118
119 addInnerToKey();
120
121 addOuterToKey();
122
123 keyBuilder->endBlock(); // ComposeBlock
124}
125
126void AddKnownModeBlend(const KeyContext& keyContext,
128 PipelineDataGatherer* gatherer,
129 SkBlendMode bm) {
132 static_cast<int>(bm));
133 builder->addBlock(id);
134}
135
136void AddModeBlend(const KeyContext& keyContext,
138 PipelineDataGatherer* gatherer,
139 SkBlendMode bm) {
141 if (!coeffs.empty()) {
142 CoeffBlenderBlock::AddBlock(keyContext, builder, gatherer, coeffs);
143 } else {
144 BlendModeBlenderBlock::AddBlock(keyContext, builder, gatherer, bm);
145 }
146}
147
148void AddDstReadBlock(const KeyContext& keyContext,
150 PipelineDataGatherer* gatherer,
151 DstReadRequirement dstReadReq) {
152 switch(dstReadReq) {
154 SkASSERT(false); // This should never be reached
155 return;
157 [[fallthrough]];
159 DstReadSampleBlock::AddBlock(keyContext, builder, gatherer, keyContext.dstTexture(),
160 keyContext.dstOffset());
161 break;
164 break;
165 }
166}
167
168void AddDitherBlock(const KeyContext& keyContext,
170 PipelineDataGatherer* gatherer,
171 SkColorType ct) {
172 static const SkBitmap gLUT = skgpu::MakeDitherLUT();
173
175 "DitherLUT");
176 if (keyContext.recorder() && !proxy) {
177 SKGPU_LOG_W("Couldn't create dither shader's LUT");
179 return;
180 }
181
183
184 DitherShaderBlock::AddBlock(keyContext, builder, gatherer, data);
185}
186
187void PaintParams::addPaintColorToKey(const KeyContext& keyContext,
188 PaintParamsKeyBuilder* keyBuilder,
189 PipelineDataGatherer* gatherer) const {
190 if (fShader) {
191 AddToKey(keyContext, keyBuilder, gatherer, fShader.get());
192 } else {
193 RGBPaintColorBlock::AddBlock(keyContext, keyBuilder, gatherer);
194 }
195}
196
197/**
198 * Primitive blend blocks are used to blend either the paint color or the output of another shader
199 * with a primitive color emitted by certain draw geometry calls (drawVertices, drawAtlas, etc.).
200 * Dst: primitiveColor Src: Paint color/shader output
201 */
202void PaintParams::handlePrimitiveColor(const KeyContext& keyContext,
203 PaintParamsKeyBuilder* keyBuilder,
204 PipelineDataGatherer* gatherer) const {
205 if (fPrimitiveBlender) {
206 Blend(keyContext, keyBuilder, gatherer,
207 /* addBlendToKey= */ [&] () -> void {
208 AddToKey(keyContext, keyBuilder, gatherer, fPrimitiveBlender.get());
209 },
210 /* addSrcToKey= */ [&]() -> void {
211 this->addPaintColorToKey(keyContext, keyBuilder, gatherer);
212 },
213 /* addDstToKey= */ [&]() -> void {
214 keyBuilder->addBlock(BuiltInCodeSnippetID::kPrimitiveColor);
215 });
216 } else {
217 this->addPaintColorToKey(keyContext, keyBuilder, gatherer);
218 }
219}
220
221// Apply the paint's alpha value.
222void PaintParams::handlePaintAlpha(const KeyContext& keyContext,
223 PaintParamsKeyBuilder* keyBuilder,
224 PipelineDataGatherer* gatherer) const {
225
226 if (!fShader && !fPrimitiveBlender) {
227 // If there is no shader and no primitive blending the input to the colorFilter stage
228 // is just the premultiplied paint color.
230 keyContext.dstColorInfo()).premul();
231 SolidColorShaderBlock::AddBlock(keyContext, keyBuilder, gatherer, paintColor);
232 return;
233 }
234
235 if (fColor.fA != 1.0f) {
236 Blend(keyContext, keyBuilder, gatherer,
237 /* addBlendToKey= */ [&] () -> void {
238 AddKnownModeBlend(keyContext, keyBuilder, gatherer, SkBlendMode::kSrcIn);
239 },
240 /* addSrcToKey= */ [&]() -> void {
241 this->handlePrimitiveColor(keyContext, keyBuilder, gatherer);
242 },
243 /* addDstToKey= */ [&]() -> void {
244 AlphaOnlyPaintColorBlock::AddBlock(keyContext, keyBuilder, gatherer);
245 });
246 } else {
247 this->handlePrimitiveColor(keyContext, keyBuilder, gatherer);
248 }
249}
250
251void PaintParams::handleColorFilter(const KeyContext& keyContext,
252 PaintParamsKeyBuilder* builder,
253 PipelineDataGatherer* gatherer) const {
254 if (fColorFilter) {
255 Compose(keyContext, builder, gatherer,
256 /* addInnerToKey= */ [&]() -> void {
257 this->handlePaintAlpha(keyContext, builder, gatherer);
258 },
259 /* addOuterToKey= */ [&]() -> void {
260 AddToKey(keyContext, builder, gatherer, fColorFilter.get());
261 });
262 } else {
263 this->handlePaintAlpha(keyContext, builder, gatherer);
264 }
265}
266
267void PaintParams::handleDithering(const KeyContext& keyContext,
268 PaintParamsKeyBuilder* builder,
269 PipelineDataGatherer* gatherer) const {
270
271#ifndef SK_IGNORE_GPU_DITHER
272 SkColorType ct = keyContext.dstColorInfo().colorType();
273 if (should_dither(*this, ct)) {
274 Compose(keyContext, builder, gatherer,
275 /* addInnerToKey= */ [&]() -> void {
276 this->handleColorFilter(keyContext, builder, gatherer);
277 },
278 /* addOuterToKey= */ [&]() -> void {
279 AddDitherBlock(keyContext, builder, gatherer, ct);
280 });
281 } else
282#endif
283 {
284 this->handleColorFilter(keyContext, builder, gatherer);
285 }
286}
287
288void PaintParams::handleDstRead(const KeyContext& keyContext,
289 PaintParamsKeyBuilder* builder,
290 PipelineDataGatherer* gatherer) const {
291 if (fDstReadReq != DstReadRequirement::kNone) {
292 Blend(keyContext, builder, gatherer,
293 /* addBlendToKey= */ [&] () -> void {
294 if (fFinalBlender) {
295 AddToKey(keyContext, builder, gatherer, fFinalBlender.get());
296 } else {
297 AddKnownModeBlend(keyContext, builder, gatherer, SkBlendMode::kSrcOver);
298 }
299 },
300 /* addSrcToKey= */ [&]() -> void {
301 this->handleDithering(keyContext, builder, gatherer);
302 },
303 /* addDstToKey= */ [&]() -> void {
304 AddDstReadBlock(keyContext, builder, gatherer, fDstReadReq);
305 });
306 } else {
307 this->handleDithering(keyContext, builder, gatherer);
308 }
309}
310
311void PaintParams::toKey(const KeyContext& keyContext,
313 PipelineDataGatherer* gatherer) const {
314 this->handleDstRead(keyContext, builder, gatherer);
315
316 std::optional<SkBlendMode> finalBlendMode = this->asFinalBlendMode();
317 if (fDstReadReq != DstReadRequirement::kNone) {
318 // In this case the blend will have been handled by shader-based blending with the dstRead.
319 finalBlendMode = SkBlendMode::kSrc;
320 }
321
322 if (fClipShader) {
323 ClipShaderBlock::BeginBlock(keyContext, builder, gatherer);
324
325 AddToKey(keyContext, builder, gatherer, fClipShader.get());
326
327 builder->endBlock();
328 }
329
330 // Set the hardware blend mode.
331 SkASSERT(finalBlendMode);
332 BuiltInCodeSnippetID fixedFuncBlendModeID = static_cast<BuiltInCodeSnippetID>(
333 kFixedFunctionBlendModeIDOffset + static_cast<int>(*finalBlendMode));
334
335 builder->addBlock(fixedFuncBlendModeID);
336}
337
338// TODO(b/330864257): Can be deleted once keys are determined by the Device draw.
340 DrawContext* drawContext) const {
341 if (fShader) {
342 NotifyImagesInUse(recorder, drawContext, fShader.get());
343 }
344 if (fPrimitiveBlender) {
345 NotifyImagesInUse(recorder, drawContext, fPrimitiveBlender.get());
346 }
347 if (fColorFilter) {
348 NotifyImagesInUse(recorder, drawContext, fColorFilter.get());
349 }
350 if (fFinalBlender) {
351 NotifyImagesInUse(recorder, drawContext, fFinalBlender.get());
352 }
353 if (fClipShader) {
354 NotifyImagesInUse(recorder, drawContext, fClipShader.get());
355 }
356}
357
358} // namespace skgpu::graphite
#define SKGPU_LOG_W(fmt,...)
Definition: Log.h:40
kUnpremul_SkAlphaType
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkBlendMode
Definition: SkBlendMode.h:38
@ kLastCoeffMode
last porter duff blend mode
@ kSrcOver
r = s + (1-sa)*d
@ kSrcIn
r = s * da
SkBlenderBase * as_BB(SkBlender *blend)
Definition: SkBlenderBase.h:69
SkColorSpace * sk_srgb_singleton()
SkColorType
Definition: SkColorType.h:19
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
SkShaderBase * as_SB(SkShader *shader)
Definition: SkShaderBase.h:412
virtual std::optional< SkBlendMode > asBlendMode() const
Definition: SkBlenderBase.h:45
SkColorSpace * colorSpace() const
Definition: SkImageInfo.cpp:66
virtual bool isConstant() const
Definition: SkShaderBase.h:197
T * get() const
Definition: SkRefCnt.h:303
SkIPoint dstOffset() const
Definition: KeyContext.h:72
Recorder * recorder() const
Definition: KeyContext.h:57
sk_sp< TextureProxy > dstTexture() const
Definition: KeyContext.h:70
PaintParams & operator=(const PaintParams &)
sk_sp< SkBlender > refFinalBlender() const
Definition: PaintParams.cpp:77
sk_sp< SkBlender > refPrimitiveBlender() const
Definition: PaintParams.cpp:83
void toKey(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *) const
PaintParams(const SkPaint &, sk_sp< SkBlender > primitiveBlender, sk_sp< SkShader > clipShader, DstReadRequirement dstReadReq, bool skipColorXform)
Definition: PaintParams.cpp:53
std::optional< SkBlendMode > asFinalBlendMode() const
Definition: PaintParams.cpp:72
static SkColor4f Color4fPrepForDst(SkColor4f srgb, const SkColorInfo &dstColorInfo)
Definition: PaintParams.cpp:85
sk_sp< SkColorFilter > refColorFilter() const
Definition: PaintParams.cpp:81
void notifyImagesInUse(Recorder *, DrawContext *) const
sk_sp< SkShader > refShader() const
Definition: PaintParams.cpp:79
static sk_sp< TextureProxy > CreateCachedProxy(Recorder *, const SkBitmap &, std::string_view label)
Definition: Recorder.cpp:544
const Paint & paint
Definition: color_source.cc:38
GAsyncResult * result
static constexpr int kFixedFunctionBlendModeIDOffset
void AddToKey(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, const SkBlender *blender)
std::function< void()> AddToKeyFn
Definition: PaintParams.h:97
void AddKnownModeBlend(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, SkBlendMode bm)
void Compose(const KeyContext &keyContext, PaintParamsKeyBuilder *keyBuilder, PipelineDataGatherer *gatherer, AddToKeyFn addInnerToKey, AddToKeyFn addOuterToKey)
void AddDitherBlock(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, SkColorType ct)
void AddDstReadBlock(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, DstReadRequirement dstReadReq)
void Blend(const KeyContext &keyContext, PaintParamsKeyBuilder *keyBuilder, PipelineDataGatherer *gatherer, AddToKeyFn addBlendToKey, AddToKeyFn addSrcToKey, AddToKeyFn addDstToKey)
Definition: PaintParams.cpp:95
DstReadRequirement
Definition: Caps.h:64
void AddModeBlend(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, SkBlendMode bm)
void NotifyImagesInUse(Recorder *recorder, DrawContext *drawContext, const SkBlender *blender)
float DitherRangeForConfig(SkColorType dstColorType)
Definition: DitherUtils.cpp:20
SkBitmap MakeDitherLUT()
Definition: DitherUtils.cpp:74
SkSpan< const float > GetPorterDuffBlendConstants(SkBlendMode mode)
Definition: Blend.cpp:53
Definition: ref_ptr.h:256
void apply(float rgba[4]) const
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:139
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, SkBlendMode)
Definition: KeyHelpers.cpp:936
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:926
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:961
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, SkSpan< const float > coeffs)
Definition: KeyHelpers.cpp:948
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:971
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const DitherData &)
Definition: KeyHelpers.cpp:876
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, sk_sp< TextureProxy > dst, SkIPoint dstOffset)
Definition: KeyHelpers.cpp:169
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:131
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const SkPMColor4f &)
Definition: KeyHelpers.cpp:102
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63