1 /*
2 * Copyright 2006 The Android Open Source Project
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 SkColorFilterBase_DEFINED
9 #define SkColorFilterBase_DEFINED
10
11 #include "include/core/SkColorFilter.h"
12 #include "include/private/SkColorData.h"
13 #include "src/core/SkVM_fwd.h"
14
15 #include <memory>
16
17 class GrColorInfo;
18 class GrFragmentProcessor;
19 class GrRecordingContext;
20 class SkArenaAlloc;
21 class SkBitmap;
22 class SkColorInfo;
23 class SkColorSpace;
24 class SkRuntimeEffect;
25 class SkSurfaceProps;
26 struct SkStageRec;
27 using GrFPResult = std::tuple<bool, std::unique_ptr<GrFragmentProcessor>>;
28
29 namespace skgpu::graphite {
30 class KeyContext;
31 class PaintParamsKeyBuilder;
32 class PipelineDataGatherer;
33 }
34
35 class SkColorFilterBase : public SkColorFilter {
36 public:
37 SK_WARN_UNUSED_RESULT
38 virtual bool appendStages(const SkStageRec& rec, bool shaderIsOpaque) const = 0;
39
40 SK_WARN_UNUSED_RESULT
41 skvm::Color program(skvm::Builder*, skvm::Color,
42 const SkColorInfo& dst, skvm::Uniforms*, SkArenaAlloc*) const;
43
44 /** Returns the flags for this filter. Override in subclasses to return custom flags.
45 */
onIsAlphaUnchanged()46 virtual bool onIsAlphaUnchanged() const { return false; }
47
48 #if defined(SK_GANESH)
49 /**
50 * A subclass may implement this factory function to work with the GPU backend. It returns
51 * a GrFragmentProcessor that implements the color filter in GPU shader code.
52 *
53 * The fragment processor receives a input FP that generates a premultiplied input color, and
54 * produces a premultiplied output color.
55 *
56 * A GrFPFailure indicates that the color filter isn't implemented for the GPU backend.
57 */
58 virtual GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
59 GrRecordingContext* context,
60 const GrColorInfo& dstColorInfo,
61 const SkSurfaceProps& props) const;
62 #endif
63
affectsTransparentBlack()64 bool affectsTransparentBlack() const {
65 return this->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT;
66 }
67
asRuntimeEffect()68 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
69
GetFlattenableType()70 static SkFlattenable::Type GetFlattenableType() {
71 return kSkColorFilter_Type;
72 }
73
getFlattenableType()74 SkFlattenable::Type getFlattenableType() const override {
75 return kSkColorFilter_Type;
76 }
77
78 static sk_sp<SkColorFilter> Deserialize(const void* data, size_t size,
79 const SkDeserialProcs* procs = nullptr) {
80 return sk_sp<SkColorFilter>(static_cast<SkColorFilter*>(
81 SkFlattenable::Deserialize(
82 kSkColorFilter_Type, data, size, procs).release()));
83 }
84
85 virtual SkPMColor4f onFilterColor4f(const SkPMColor4f& color, SkColorSpace* dstCS) const;
86
87 #if defined(SK_GRAPHITE)
88 /**
89 Add implementation details, for the specified backend, of this SkColorFilter to the
90 provided key.
91
92 @param keyContext backend context for key creation
93 @param builder builder for creating the key for this SkShader
94 @param gatherer if non-null, storage for this colorFilter's data
95 */
96 virtual void addToKey(const skgpu::graphite::KeyContext& keyContext,
97 skgpu::graphite::PaintParamsKeyBuilder* builder,
98 skgpu::graphite::PipelineDataGatherer* gatherer) const;
99 #endif
100
101 protected:
SkColorFilterBase()102 SkColorFilterBase() {}
103
104 virtual bool onAsAColorMatrix(float[20]) const;
105 virtual bool onAsAColorMode(SkColor* color, SkBlendMode* bmode) const;
106
107 private:
108 virtual skvm::Color onProgram(skvm::Builder*, skvm::Color,
109 const SkColorInfo& dst, skvm::Uniforms*, SkArenaAlloc*) const = 0;
110
111 friend class SkColorFilter;
112
113 using INHERITED = SkFlattenable;
114 };
115
as_CFB(SkColorFilter * filter)116 static inline SkColorFilterBase* as_CFB(SkColorFilter* filter) {
117 return static_cast<SkColorFilterBase*>(filter);
118 }
119
as_CFB(const SkColorFilter * filter)120 static inline const SkColorFilterBase* as_CFB(const SkColorFilter* filter) {
121 return static_cast<const SkColorFilterBase*>(filter);
122 }
123
as_CFB(const sk_sp<SkColorFilter> & filter)124 static inline const SkColorFilterBase* as_CFB(const sk_sp<SkColorFilter>& filter) {
125 return static_cast<SkColorFilterBase*>(filter.get());
126 }
127
as_CFB_sp(sk_sp<SkColorFilter> filter)128 static inline sk_sp<SkColorFilterBase> as_CFB_sp(sk_sp<SkColorFilter> filter) {
129 return sk_sp<SkColorFilterBase>(static_cast<SkColorFilterBase*>(filter.release()));
130 }
131
132
133 void SkRegisterComposeColorFilterFlattenable();
134 void SkRegisterMatrixColorFilterFlattenable();
135 void SkRegisterModeColorFilterFlattenable();
136 void SkRegisterColorSpaceXformColorFilterFlattenable();
137 void SkRegisterTableColorFilterFlattenable();
138 void SkRegisterWorkingFormatColorFilterFlattenable();
139
140 #endif
141