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