Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
composecolorfilter.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkImage.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkLumaColorFilter.h"
#include "include/effects/SkPerlinNoiseShader.h"
#include "include/effects/SkRuntimeEffect.h"
#include "src/core/SkRuntimeEffectPriv.h"
#include "tools/Resources.h"
#include <math.h>

Go to the source code of this file.

Functions

static sk_sp< SkColorFilterMakeTintColorFilter (SkColor lo, SkColor hi, bool useSkSL)
 
 DEF_SIMPLE_GM (composeCF, canvas, 200, 200)
 
 DEF_SIMPLE_GM (composeCFIF, canvas, 604, 200)
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/2]

DEF_SIMPLE_GM ( composeCF  ,
canvas  ,
200  ,
200   
)

Definition at line 81 of file composecolorfilter.cpp.

81 {
82 // This GM draws a simple color-filter network, using the existing "makeComposed" API, and also
83 // using a runtime color filter that does the same thing.
85 const SkColor gradient_colors[] = {SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED};
87 50, 50, gradient_colors, nullptr, std::size(gradient_colors)));
88
89 canvas->save();
90 for (bool useSkSL : {false, true}) {
91 auto cf0 = MakeTintColorFilter(0xff300000, 0xffa00000, useSkSL); // red tint
92 auto cf1 = MakeTintColorFilter(0xff003000, 0xff00a000, useSkSL); // green tint
93
94 paint.setColorFilter(cf0);
95 canvas->drawRect({0, 0, 100, 100}, paint);
96 canvas->translate(100, 0);
97
98 paint.setColorFilter(cf1);
99 canvas->drawRect({0, 0, 100, 100}, paint);
100
101 canvas->restore();
102 canvas->translate(0, 100);
103 }
104}
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
const Paint & paint
static sk_sp< SkColorFilter > MakeTintColorFilter(SkColor lo, SkColor hi, bool useSkSL)

◆ DEF_SIMPLE_GM() [2/2]

DEF_SIMPLE_GM ( composeCFIF  ,
canvas  ,
604  ,
200   
)

Definition at line 106 of file composecolorfilter.cpp.

106 {
107 // This GM draws a ::Shader image filter composed with a ::ColorFilter image filter in two
108 // ways (direct and via ::Compose). This ensures the use (or non-use in this case) of the source
109 // image is the same across both means of composition.
110 auto cf = MakeTintColorFilter(0xff300000, 0xffa00000, /*useSkSL=*/false);
111 auto shader = SkShaders::MakeTurbulence(0.01f, 0.01f, 2, 0.f);
112
114 auto directCompose = SkImageFilters::ColorFilter(cf, shaderIF);
115 auto indirectCompose = SkImageFilters::Compose(
116 /*outer=*/SkImageFilters::ColorFilter(cf, nullptr),
117 /*inner=*/shaderIF);
118
119 { // Directly draw the shader composed with the color filter
120 canvas->save();
121 canvas->clipRect({0, 0, 200, 200});
122 SkPaint p;
123 p.setShader(shader);
124 p.setColorFilter(cf);
125 canvas->drawPaint(p);
126 canvas->restore();
127 }
128 canvas->translate(202, 0);
129 { // Draw with the directly composed image filter
130 canvas->save();
131 canvas->clipRect({0, 0, 200, 200});
132 SkPaint p;
133 p.setImageFilter(directCompose);
134 canvas->drawPaint(p);
135 canvas->restore();
136 }
137 canvas->translate(202, 0);
138 {
139 // Draw with the indirectly composed image filter
140 canvas->save();
141 canvas->clipRect({0, 0, 200, 200});
142 SkPaint p;
143 p.setImageFilter(indirectCompose);
144 canvas->drawPaint(p);
145 canvas->restore();
146 }
147}
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Compose(sk_sp< SkImageFilter > outer, sk_sp< SkImageFilter > inner)
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
SK_API sk_sp< SkShader > MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, int numOctaves, SkScalar seed, const SkISize *tileSize=nullptr)

◆ MakeTintColorFilter()

static sk_sp< SkColorFilter > MakeTintColorFilter ( SkColor  lo,
SkColor  hi,
bool  useSkSL 
)
static

Definition at line 37 of file composecolorfilter.cpp.

37 {
38 const auto r_lo = SkColorGetR(lo),
39 g_lo = SkColorGetG(lo),
40 b_lo = SkColorGetB(lo),
41 a_lo = SkColorGetA(lo),
42 r_hi = SkColorGetR(hi),
43 g_hi = SkColorGetG(hi),
44 b_hi = SkColorGetB(hi),
45 a_hi = SkColorGetA(hi);
46
47 // We map component-wise:
48 //
49 // r' = lo.r + (hi.r - lo.r) * luma
50 // g' = lo.g + (hi.g - lo.g) * luma
51 // b' = lo.b + (hi.b - lo.b) * luma
52 // a' = lo.a + (hi.a - lo.a) * luma
53 //
54 // The input luminance is stored in the alpha channel
55 // (and RGB are cleared -- see SkLumaColorFilter). Thus:
56 const float tint_matrix[] = {
57 0, 0, 0, (r_hi - r_lo) / 255.0f, SkIntToScalar(r_lo) / 255.0f,
58 0, 0, 0, (g_hi - g_lo) / 255.0f, SkIntToScalar(g_lo) / 255.0f,
59 0, 0, 0, (b_hi - b_lo) / 255.0f, SkIntToScalar(b_lo) / 255.0f,
60 0, 0, 0, (a_hi - a_lo) / 255.0f, SkIntToScalar(a_lo) / 255.0f,
61 };
62
64 outer = SkColorFilters::Matrix(tint_matrix);
65
66 // Prove that we can implement compose-color-filter using runtime effects
67 if (useSkSL) {
69 uniform colorFilter inner;
70 uniform colorFilter outer;
71 half4 main(half4 c) { return outer.eval(inner.eval(c)); }
72 )"));
73 SkASSERT(effect);
74 sk_sp<SkColorFilter> children[] = { inner, outer };
75 return effect->makeColorFilter(nullptr, children, std::size(children));
76 } else {
77 return outer->makeComposed(inner);
78 }
79}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkColorGetR(color)
Definition SkColor.h:65
#define SkColorGetG(color)
Definition SkColor.h:69
#define SkColorGetA(color)
Definition SkColor.h:61
#define SkColorGetB(color)
Definition SkColor.h:73
#define SkIntToScalar(x)
Definition SkScalar.h:57
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static Result MakeForColorFilter(SkString sksl, const Options &)
const uint8_t uint32_t uint32_t GError ** error
static sk_sp< SkColorFilter > Make()