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 14 #include <memory> 15 #include <optional> 16 17 struct GrFPArgs; 18 class GrFragmentProcessor; 19 class SkColorInfo; 20 class SkRuntimeEffect; 21 struct SkStageRec; 22 23 namespace skgpu::graphite { 24 enum class DstColorType; 25 class KeyContext; 26 class PaintParamsKeyBuilder; 27 class PipelineDataGatherer; 28 } 29 30 #define SK_ALL_BLENDERS(M) \ 31 M(BlendMode) \ 32 M(Runtime) 33 34 /** 35 * Encapsulates a blend function, including non-public APIs. 36 * Blends combine a source color (the result of our paint) and destination color (from the canvas) 37 * into a final color. 38 */ 39 class SkBlenderBase : public SkBlender { 40 public: 41 /** 42 * Returns true if this SkBlender represents any SkBlendMode, and returns the blender's 43 * SkBlendMode in `mode`. Returns false for other types of blends. 44 */ asBlendMode()45 virtual std::optional<SkBlendMode> asBlendMode() const { return {}; } 46 47 bool affectsTransparentBlack() const; 48 appendStages(const SkStageRec & rec)49 [[nodiscard]] bool appendStages(const SkStageRec& rec) const { 50 return this->onAppendStages(rec); 51 } 52 53 [[nodiscard]] virtual bool onAppendStages(const SkStageRec& rec) const = 0; 54 asRuntimeEffect()55 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; } 56 GetFlattenableType()57 static SkFlattenable::Type GetFlattenableType() { return kSkBlender_Type; } getFlattenableType()58 SkFlattenable::Type getFlattenableType() const override { return GetFlattenableType(); } 59 60 enum class BlenderType { 61 #define M(type) k ## type, 62 SK_ALL_BLENDERS(M) 63 #undef M 64 }; 65 66 virtual BlenderType type() const = 0; 67 }; 68 as_BB(SkBlender * blend)69inline SkBlenderBase* as_BB(SkBlender* blend) { 70 return static_cast<SkBlenderBase*>(blend); 71 } 72 as_BB(const SkBlender * blend)73inline const SkBlenderBase* as_BB(const SkBlender* blend) { 74 return static_cast<const SkBlenderBase*>(blend); 75 } 76 as_BB(const sk_sp<SkBlender> & blend)77inline const SkBlenderBase* as_BB(const sk_sp<SkBlender>& blend) { 78 return static_cast<SkBlenderBase*>(blend.get()); 79 } 80 81 #endif // SkBlenderBase_DEFINED 82