Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions | Variables
runtimecolorfilter.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRSXform.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkVertices.h"
#include "include/effects/SkRuntimeEffect.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include <stddef.h>
#include <utility>

Go to the source code of this file.

Classes

class  RuntimeColorFilterGM
 

Functions

 DEF_SIMPLE_GM (runtimecolorfilter_vertices_atlas_and_patch, canvas, 404, 404)
 

Variables

const char * gNoop
 
const char * gLumaSrc
 
const char * gTernary
 
const char * gIfs
 
const char * gEarlyReturn
 

Function Documentation

◆ DEF_SIMPLE_GM()

DEF_SIMPLE_GM ( runtimecolorfilter_vertices_atlas_and_patch  ,
canvas  ,
404  ,
404   
)

Definition at line 128 of file runtimecolorfilter.cpp.

128 {
129 const SkRect r = SkRect::MakeWH(128, 128);
130
131 // Make a vertices that draws the same as SkRect 'r'.
132 SkPoint pos[4];
133 r.toQuad(pos);
136
137 // Make an image from the vertices to do equivalent drawAtlas, drawPatch using an image shader.
138 auto info = SkImageInfo::Make({128, 128},
141 canvas->imageInfo().refColorSpace());
142 auto surf = SkSurfaces::Raster(info);
143 surf->getCanvas()->drawVertices(verts, SkBlendMode::kDst, SkPaint());
144 auto atlas = surf->makeImageSnapshot();
145 auto xform = SkRSXform::Make(1, 0, 0, 0);
146
147 // Make a patch that draws the same as the SkRect 'r'
148 SkVector vx = pos[1] - pos[0];
149 SkVector vy = pos[3] - pos[0];
150 vx.setLength(vx.length()/3.f);
151 vy.setLength(vy.length()/3.f);
152 const SkPoint cubics[12] = {
153 pos[0], pos[0] + vx, pos[1] - vx,
154 pos[1], pos[1] + vy, pos[2] - vy,
155 pos[2], pos[2] - vx, pos[3] + vx,
156 pos[3], pos[3] - vy, pos[0] + vy
157 };
158
160 if (!effect) {
161 SkDebugf("%s\n%s\n", gLumaSrc, err.c_str());
162 }
163 SkASSERT(effect);
164 sk_sp<SkColorFilter> colorfilter = effect->makeColorFilter(nullptr);
165
166 auto makePaint = [&](bool useCF, bool useShader) {
168 paint.setColorFilter(useCF ? colorfilter : nullptr);
169 paint.setShader(useShader ? atlas->makeShader(SkFilterMode::kNearest) : nullptr);
170 return paint;
171 };
172
173 auto drawVertices = [&](float x, bool useCF, bool useShader) {
174 SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
175 canvas->translate(x, 0);
176 // Use just the shader or just the vertex colors.
177 auto mode = useShader ? SkBlendMode::kSrc : SkBlendMode::kDst;
178 canvas->drawVertices(verts, mode, makePaint(useCF, useShader));
179 };
180
181 auto drawAtlas = [&](float x, bool useCF) {
182 SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
183 canvas->translate(x, 0);
184 SkPaint paint = makePaint(useCF, /*useShader=*/false);
185 constexpr SkColor kColor = SK_ColorWHITE;
186 canvas->drawAtlas(atlas.get(),
187 &xform,
188 &r,
189 &kColor,
190 1,
193 nullptr,
194 &paint);
195 };
196
197 auto drawPatch = [&](float x, bool useCF) {
198 SkAutoCanvasRestore acr(canvas, true);
199 canvas->translate(x, 0);
200 SkPaint paint = makePaint(useCF, /*useShader=*/true);
201 canvas->drawPatch(cubics, nullptr, pos, SkBlendMode::kModulate, paint);
202 };
203
204 drawVertices( 0, /*useCF=*/false, /*useShader=*/false);
205 drawVertices( r.width() + 10, /*useCF=*/ true, /*useShader=*/false);
206 drawVertices(2*(r.width() + 10), /*useCF=*/ true, /*useShader=*/ true);
207
208 canvas->translate(0, r.height() + 10);
209 drawAtlas( 0, /*useCF=*/false);
210 drawAtlas(r.width() + 10, /*useCF=*/ true);
211
212 canvas->translate(0, r.height() + 10);
213 drawPatch( 0, /*useCF=*/false);
214 drawPatch(r.width() + 10, /*useCF=*/ true);
215}
static constexpr SkColor kColor
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkPoint pos
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kModulate
r = s*d
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
static Result MakeForColorFilter(SkString sksl, const Options &)
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
@ kTriangleFan_VertexMode
Definition SkVertices.h:33
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
double x
sk_sp< const SkImage > atlas
Definition SkRecords.h:331
drawAtlas(r.atlas.get(), r.xforms, r.texs, r.colors, r.count, r.mode, r.sampling, r.cull, r.paint)) DRAW(DrawAnnotation
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
const DlColor kColors[]
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition switches.h:228
const char * gLumaSrc
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
bool setLength(float length)
Definition SkPoint.cpp:30
float length() const
static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty)
Definition SkRSXform.h:24
void toQuad(SkPoint quad[4]) const
Definition SkRect.cpp:50
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609

Variable Documentation

◆ gEarlyReturn

const char* gEarlyReturn
Initial value:
= R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = 0;
if (luma < 0.33333) {
return half4(color.rgb * 0.5, color.a);
} else if (luma < 0.66666) {
scale = 0.166666 + 2.0 * (luma - 0.33333);
} else {
scale = 0.833333 + 0.5 * (luma - 0.66666);
}
return half4(color.rgb * (scale/luma), color.a);
}
)"

Definition at line 73 of file runtimecolorfilter.cpp.

◆ gIfs

const char* gIfs
Initial value:
= R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = 0;
if (luma < 0.33333) {
scale = 0.5;
} else if (luma < 0.66666) {
scale = (0.166666 + 2.0 * (luma - 0.33333)) / luma;
} else {
scale = (0.833333 + 0.5 * (luma - 0.66666)) / luma;
}
return half4(color.rgb * scale, color.a);
}
)"

Definition at line 56 of file runtimecolorfilter.cpp.

◆ gLumaSrc

const char* gLumaSrc
Initial value:
= R"(
half4 main(half4 color) {
return dot(color.rgb, half3(0.3, 0.6, 0.1)).000r;
}
)"

Definition at line 34 of file runtimecolorfilter.cpp.

◆ gNoop

const char* gNoop
Initial value:
= R"(
half4 main(half4 color) {
return color;
}
)"

Definition at line 28 of file runtimecolorfilter.cpp.

◆ gTernary

const char* gTernary
Initial value:
= R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = luma < 0.33333 ? 0.5
: luma < 0.66666 ? (0.166666 + 2.0 * (luma - 0.33333)) / luma
: /* else */ (0.833333 + 0.5 * (luma - 0.66666)) / luma;
return half4(color.rgb * scale, color.a);
}
)"

Definition at line 44 of file runtimecolorfilter.cpp.