1 /*
2 * Copyright 2021 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 #ifndef SkBlenderBase_DEFINED
9 #define SkBlenderBase_DEFINED
10
11 #include "include/core/SkBlender.h"
12 #include "src/base/SkArenaAlloc.h"
13 #include "src/core/SkVM.h"
14
15 #include <optional>
16
17 struct GrFPArgs;
18 class GrFragmentProcessor;
19 class SkColorInfo;
20 class SkRuntimeEffect;
21
22 namespace skgpu::graphite {
23 class KeyContext;
24 class PaintParamsKeyBuilder;
25 class PipelineDataGatherer;
26 }
27
28 /**
29 * Encapsulates a blend function, including non-public APIs.
30 * Blends combine a source color (the result of our paint) and destination color (from the canvas)
31 * into a final color.
32 */
33 class SkBlenderBase : public SkBlender {
34 public:
35 /**
36 * Returns true if this SkBlender represents any SkBlendMode, and returns the blender's
37 * SkBlendMode in `mode`. Returns false for other types of blends.
38 */
asBlendMode()39 virtual std::optional<SkBlendMode> asBlendMode() const { return {}; }
40
41 /** Creates the blend program in SkVM. */
42 SK_WARN_UNUSED_RESULT
program(skvm::Builder * p,skvm::Color src,skvm::Color dst,const SkColorInfo & colorInfo,skvm::Uniforms * uniforms,SkArenaAlloc * alloc)43 skvm::Color program(skvm::Builder* p, skvm::Color src, skvm::Color dst,
44 const SkColorInfo& colorInfo, skvm::Uniforms* uniforms,
45 SkArenaAlloc* alloc) const {
46 return this->onProgram(p, src, dst, colorInfo, uniforms, alloc);
47 }
48
49 #if defined(SK_GANESH)
50 /**
51 * Returns a GrFragmentProcessor that implements this blend for the GPU backend.
52 * The GrFragmentProcessor expects premultiplied inputs and returns a premultiplied output.
53 */
54 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
55 std::unique_ptr<GrFragmentProcessor> srcFP,
56 std::unique_ptr<GrFragmentProcessor> dstFP,
57 const GrFPArgs& fpArgs) const = 0;
58 #endif
59
asRuntimeEffect()60 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
61
62 #if defined(SK_GRAPHITE)
63 /**
64 * TODO: Make pure virtual.
65 * primitiveColorBlender = true when blending the result of the paint evaluation with a
66 * primitive color (which is supplied by certain geometries). primitiveColorBlender = false when
67 * blending the result of the paint evaluation with the back buffer.
68 */
69 virtual void addToKey(const skgpu::graphite::KeyContext&,
70 skgpu::graphite::PaintParamsKeyBuilder*,
71 skgpu::graphite::PipelineDataGatherer*,
72 bool primitiveColorBlender) const;
73 #endif
74
GetFlattenableType()75 static SkFlattenable::Type GetFlattenableType() { return kSkBlender_Type; }
getFlattenableType()76 Type getFlattenableType() const override { return GetFlattenableType(); }
77
78 private:
79 virtual skvm::Color onProgram(skvm::Builder* p, skvm::Color src, skvm::Color dst,
80 const SkColorInfo& colorInfo, skvm::Uniforms* uniforms,
81 SkArenaAlloc* alloc) const = 0;
82
83 using INHERITED = SkFlattenable;
84 };
85
as_BB(SkBlender * blend)86 inline SkBlenderBase* as_BB(SkBlender* blend) {
87 return static_cast<SkBlenderBase*>(blend);
88 }
89
as_BB(const SkBlender * blend)90 inline const SkBlenderBase* as_BB(const SkBlender* blend) {
91 return static_cast<const SkBlenderBase*>(blend);
92 }
93
as_BB(const sk_sp<SkBlender> & blend)94 inline const SkBlenderBase* as_BB(const sk_sp<SkBlender>& blend) {
95 return static_cast<SkBlenderBase*>(blend.get());
96 }
97
98 #endif // SkBlenderBase_DEFINED
99