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 class GrColorInfo;
16 class GrFragmentProcessor;
17 class GrRecordingContext;
18 class SkArenaAlloc;
19 class SkBitmap;
20 class SkColorSpace;
21 struct SkStageRec;
22 using GrFPResult = std::tuple<bool, std::unique_ptr<GrFragmentProcessor>>;
23
24 class SkColorFilterBase : public SkColorFilter {
25 public:
26 SK_WARN_UNUSED_RESULT
27 bool appendStages(const SkStageRec& rec, bool shaderIsOpaque) const;
28
29 SK_WARN_UNUSED_RESULT
30 skvm::Color program(skvm::Builder*, skvm::Color,
31 const SkColorInfo& dst, skvm::Uniforms*, SkArenaAlloc*) const;
32
33 /** Returns the flags for this filter. Override in subclasses to return custom flags.
34 */
onIsAlphaUnchanged()35 virtual bool onIsAlphaUnchanged() const { return false; }
36
37 #if SK_SUPPORT_GPU
38 /**
39 * A subclass may implement this factory function to work with the GPU backend. It returns
40 * a GrFragmentProcessor that implements the color filter in GPU shader code.
41 *
42 * The fragment processor receives a input FP that generates a premultiplied input color, and
43 * produces a premultiplied output color.
44 *
45 * A GrFPFailure indicates that the color filter isn't implemented for the GPU backend.
46 */
47 virtual GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
48 GrRecordingContext* context,
49 const GrColorInfo& dstColorInfo) const;
50 #endif
51
affectsTransparentBlack()52 bool affectsTransparentBlack() const {
53 return this->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT;
54 }
55
56 static void RegisterFlattenables();
57
GetFlattenableType()58 static SkFlattenable::Type GetFlattenableType() {
59 return kSkColorFilter_Type;
60 }
61
getFlattenableType()62 SkFlattenable::Type getFlattenableType() const override {
63 return kSkColorFilter_Type;
64 }
65
66 static sk_sp<SkColorFilter> Deserialize(const void* data, size_t size,
67 const SkDeserialProcs* procs = nullptr) {
68 return sk_sp<SkColorFilter>(static_cast<SkColorFilter*>(
69 SkFlattenable::Deserialize(
70 kSkColorFilter_Type, data, size, procs).release()));
71 }
72
73 virtual SkPMColor4f onFilterColor4f(const SkPMColor4f& color, SkColorSpace* dstCS) const;
74
75 protected:
SkColorFilterBase()76 SkColorFilterBase() {}
77
78 virtual bool onAsAColorMatrix(float[20]) const;
79 virtual bool onAsAColorMode(SkColor* color, SkBlendMode* bmode) const;
80
81 private:
82 virtual bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const = 0;
83
84 virtual skvm::Color onProgram(skvm::Builder*, skvm::Color,
85 const SkColorInfo& dst, skvm::Uniforms*, SkArenaAlloc*) const = 0;
86
87 friend class SkColorFilter;
88
89 using INHERITED = SkFlattenable;
90 };
91
as_CFB(SkColorFilter * filter)92 static inline SkColorFilterBase* as_CFB(SkColorFilter* filter) {
93 return static_cast<SkColorFilterBase*>(filter);
94 }
95
as_CFB(const SkColorFilter * filter)96 static inline const SkColorFilterBase* as_CFB(const SkColorFilter* filter) {
97 return static_cast<const SkColorFilterBase*>(filter);
98 }
99
as_CFB(const sk_sp<SkColorFilter> & filter)100 static inline const SkColorFilterBase* as_CFB(const sk_sp<SkColorFilter>& filter) {
101 return static_cast<SkColorFilterBase*>(filter.get());
102 }
103
as_CFB_sp(sk_sp<SkColorFilter> filter)104 static inline sk_sp<SkColorFilterBase> as_CFB_sp(sk_sp<SkColorFilter> filter) {
105 return sk_sp<SkColorFilterBase>(static_cast<SkColorFilterBase*>(filter.release()));
106 }
107
108 #endif
109