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 "include/core/SkColorSpace.h"
13 #include "src/core/SkArenaAlloc.h"
14 #include "src/core/SkVM.h"
15
16 #include <optional>
17
18 enum class SkBackend : uint8_t;
19 struct GrFPArgs;
20 class GrFragmentProcessor;
21 class SkPaintParamsKeyBuilder;
22 class SkRuntimeEffect;
23 class SkShaderCodeDictionary;
24 class SkUniformBlock;
25
26 /**
27 * Encapsulates a blend function, including non-public APIs.
28 * Blends combine a source color (the result of our paint) and destination color (from the canvas)
29 * into a final color.
30 */
31 class SkBlenderBase : public SkBlender {
32 public:
33 /**
34 * Returns true if this SkBlender represents any SkBlendMode, and returns the blender's
35 * SkBlendMode in `mode`. Returns false for other types of blends.
36 */
asBlendMode()37 virtual std::optional<SkBlendMode> asBlendMode() const { return {}; }
38
39 /** Creates the blend program in SkVM. */
40 SK_WARN_UNUSED_RESULT
program(skvm::Builder * p,skvm::Color src,skvm::Color dst,const SkColorInfo & colorInfo,skvm::Uniforms * uniforms,SkArenaAlloc * alloc)41 skvm::Color program(skvm::Builder* p, skvm::Color src, skvm::Color dst,
42 const SkColorInfo& colorInfo, skvm::Uniforms* uniforms,
43 SkArenaAlloc* alloc) const {
44 return this->onProgram(p, src, dst, colorInfo, uniforms, alloc);
45 }
46
47 #if SK_SUPPORT_GPU
48 /**
49 * Returns a GrFragmentProcessor that implements this blend for the GPU backend.
50 * The GrFragmentProcessor expects premultiplied inputs and returns a premultiplied output.
51 */
52 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
53 std::unique_ptr<GrFragmentProcessor> srcFP,
54 std::unique_ptr<GrFragmentProcessor> dstFP,
55 const GrFPArgs& fpArgs) const = 0;
56 #endif
57
asRuntimeEffect()58 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
59
60 // TODO: make pure virtual
61 virtual void addToKey(SkShaderCodeDictionary*,
62 SkBackend,
63 SkPaintParamsKeyBuilder*,
64 SkUniformBlock*) const;
65
GetFlattenableType()66 static SkFlattenable::Type GetFlattenableType() { return kSkBlender_Type; }
getFlattenableType()67 Type getFlattenableType() const override { return GetFlattenableType(); }
68
69 private:
70 virtual skvm::Color onProgram(skvm::Builder* p, skvm::Color src, skvm::Color dst,
71 const SkColorInfo& colorInfo, skvm::Uniforms* uniforms,
72 SkArenaAlloc* alloc) const = 0;
73
74 using INHERITED = SkFlattenable;
75 };
76
as_BB(SkBlender * blend)77 inline SkBlenderBase* as_BB(SkBlender* blend) {
78 return static_cast<SkBlenderBase*>(blend);
79 }
80
as_BB(const SkBlender * blend)81 inline const SkBlenderBase* as_BB(const SkBlender* blend) {
82 return static_cast<const SkBlenderBase*>(blend);
83 }
84
as_BB(const sk_sp<SkBlender> & blend)85 inline const SkBlenderBase* as_BB(const sk_sp<SkBlender>& blend) {
86 return static_cast<SkBlenderBase*>(blend.get());
87 }
88
89 #endif // SkBlenderBase_DEFINED
90