Flutter Engine
The Flutter Engine
Classes | Functions | Variables
composeshader.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorPriv.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.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/private/base/SkTDArray.h"
#include "src/base/SkTLazy.h"
#include "tools/GpuToolUtils.h"
#include "tools/ToolUtils.h"
#include <utility>

Go to the source code of this file.

Classes

class  ComposeShaderGM
 
class  ComposeShaderAlphaGM
 
class  ComposeShaderBitmapGM
 

Functions

static sk_sp< SkShadermake_shader (SkBlendMode mode)
 
static void draw_color_bm (SkBitmap *bm, int length)
 
static void draw_alpha8_bm (SkBitmap *bm, int length)
 
static sk_sp< SkShadermake_linear_gradient_shader (int length)
 
 DEF_SIMPLE_GM (composeshader_bitmap2, canvas, 200, 200)
 
static sk_sp< SkShadermake_src_shader (SkScalar size)
 
static sk_sp< SkShadermake_dst_shader (SkScalar size)
 
static void draw_cell (SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode, SkAlpha alpha)
 
static void draw_composed (SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode, SkAlpha alpha)
 
static void draw_pair (SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode)
 
 DEF_SIMPLE_GM (composeshader_grid, canvas, 882, 882)
 

Variables

const SkScalar gCellSize = 100
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/2]

DEF_SIMPLE_GM ( composeshader_bitmap2  ,
canvas  ,
200  ,
200   
)

Definition at line 253 of file composeshader.cpp.

253 {
254 int width = 255;
255 int height = 255;
256 SkTDArray<uint8_t> dst8Storage;
257 dst8Storage.resize(width * height);
258 SkTDArray<uint32_t> dst32Storage;
259 dst32Storage.resize(width * height * sizeof(int32_t));
260 for (int y = 0; y < height; ++y) {
261 for (int x = 0; x < width; ++x) {
262 dst8Storage[y * width + x] = (y + x) / 2;
263 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
264 }
265 }
267 paint.setAntiAlias(true);
268 paint.setColor(SK_ColorBLUE);
270 canvas->drawRect(r, paint);
271 SkBitmap skBitmap, skMask;
273 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
274 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
275 nullptr, nullptr);
276 imageInfo = SkImageInfo::Make(width, height,
278 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
279 sk_sp<SkImage> skSrc = skBitmap.asImage();
280 sk_sp<SkImage> skMaskImage = skMask.asImage();
281 paint.setShader(
283 skMaskImage->makeShader(SkSamplingOptions()),
284 skSrc->makeShader(SkSamplingOptions())));
285 canvas->drawRect(r, paint);
286}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kSrcIn
r = s * da
static SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorPriv.h:106
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
#define SkIntToScalar(x)
Definition: SkScalar.h:57
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition: SkBitmap.cpp:323
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
T * begin()
Definition: SkTDArray.h:150
void resize(int count)
Definition: SkTDArray.h:183
const Paint & paint
Definition: color_source.cc:38
double y
double x
SK_API sk_sp< SkShader > Blend(SkBlendMode mode, sk_sp< SkShader > dst, sk_sp< SkShader > src)
int32_t height
int32_t width
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ DEF_SIMPLE_GM() [2/2]

DEF_SIMPLE_GM ( composeshader_grid  ,
canvas  ,
882  ,
882   
)

Definition at line 355 of file composeshader.cpp.

355 {
358
359 const SkScalar margin = 15;
360 const SkScalar dx = 2*gCellSize + margin;
361 const SkScalar dy = 2*gCellSize + margin;
362
363 canvas->translate(margin, margin);
364 canvas->save();
365 for (int m = 0; m < 16; ++m) {
366 SkBlendMode mode = static_cast<SkBlendMode>(m);
367 draw_pair(canvas, src, dst, mode);
368 if ((m % 4) == 3) {
369 canvas->restore();
370 canvas->translate(0, dy);
371 canvas->save();
372 } else {
373 canvas->translate(dx, 0);
374 }
375 }
376 canvas->restore();
377}
SkBlendMode
Definition: SkBlendMode.h:38
static sk_sp< SkShader > make_dst_shader(SkScalar size)
static sk_sp< SkShader > make_src_shader(SkScalar size)
static void draw_pair(SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode)
const SkScalar gCellSize
float SkScalar
Definition: extension.cpp:12
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
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
dst
Definition: cp.py:12

◆ draw_alpha8_bm()

static void draw_alpha8_bm ( SkBitmap bm,
int  length 
)
static

Definition at line 137 of file composeshader.cpp.

137 {
138 SkPaint circlePaint;
139 circlePaint.setColor(SK_ColorBLACK);
140
143
144 SkCanvas canvas(*bm);
145 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
146 circlePaint);
147}
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void setColor(SkColor color)
Definition: SkPaint.cpp:119
size_t length
static SkImageInfo MakeA8(int width, int height)

◆ draw_cell()

static void draw_cell ( SkCanvas canvas,
sk_sp< SkShader src,
sk_sp< SkShader dst,
SkBlendMode  mode,
SkAlpha  alpha 
)
static

Definition at line 304 of file composeshader.cpp.

305 {
307 SkPaint p;
308 p.setAlpha(alpha);
309
310 SkAutoCanvasRestore acr(canvas, false);
311 canvas->saveLayer(&r, &p);
312 p.setAlpha(0xFF);
313
314 p.setShader(dst);
315 p.setBlendMode(SkBlendMode::kSrc);
316 canvas->drawRect(r, p);
317
318 p.setShader(src);
319 p.setBlendMode(mode);
320 canvas->drawRect(r, p);
321}
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ draw_color_bm()

static void draw_color_bm ( SkBitmap bm,
int  length 
)
static

Definition at line 124 of file composeshader.cpp.

124 {
126 paint.setColor(SK_ColorGREEN);
127
130
131 SkCanvas canvas(*bm);
132 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
133 paint);
134}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232

◆ draw_composed()

static void draw_composed ( SkCanvas canvas,
sk_sp< SkShader src,
sk_sp< SkShader dst,
SkBlendMode  mode,
SkAlpha  alpha 
)
static

Definition at line 323 of file composeshader.cpp.

324 {
325 SkPaint p;
326 p.setAlpha(alpha);
327 p.setShader(SkShaders::Blend(mode, dst, src));
329}

◆ draw_pair()

static void draw_pair ( SkCanvas canvas,
sk_sp< SkShader src,
sk_sp< SkShader dst,
SkBlendMode  mode 
)
static

Definition at line 331 of file composeshader.cpp.

332 {
333 SkAutoCanvasRestore acr(canvas, true);
334
335 const SkScalar gap = 4;
336 SkRect r = SkRect::MakeWH(2 * gCellSize + gap, 2 * gCellSize + gap);
337 r.outset(gap + 1.5f, gap + 1.5f);
338 SkPaint p;
339 p.setStyle(SkPaint::kStroke_Style);
340 canvas->drawRect(r, p); // border
341
342 SkAlpha alpha = 0xFF;
343 for (int y = 0; y < 2; ++y) {
344 draw_cell(canvas, src, dst, mode, alpha);
345 canvas->save();
346 canvas->translate(gCellSize + gap, 0);
347 draw_composed(canvas, src, dst, mode, alpha);
348 canvas->restore();
349
350 canvas->translate(0, gCellSize + gap);
351 alpha = 0x80;
352 }
353}
uint8_t SkAlpha
Definition: SkColor.h:26
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
static void draw_cell(SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode, SkAlpha alpha)
static void draw_composed(SkCanvas *canvas, sk_sp< SkShader > src, sk_sp< SkShader > dst, SkBlendMode mode, SkAlpha alpha)
void outset(float dx, float dy)
Definition: SkRect.h:1077

◆ make_dst_shader()

static sk_sp< SkShader > make_dst_shader ( SkScalar  size)
static

Definition at line 296 of file composeshader.cpp.

296 {
297 const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
298 const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
300}
uint32_t SkColor
Definition: SkColor.h:37
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
PODArray< SkColor > colors
Definition: SkRecords.h:276
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 keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ make_linear_gradient_shader()

static sk_sp< SkShader > make_linear_gradient_shader ( int  length)
static

Definition at line 150 of file composeshader.cpp.

150 {
151 SkPoint pts[2];
152 SkColor colors[2];
153 pts[0].set(0, 0);
154 pts[1].set(SkIntToScalar(length), 0);
155 colors[0] = SK_ColorBLUE;
156 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
158}
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
void set(float x, float y)
Definition: SkPoint_impl.h:200

◆ make_shader()

static sk_sp< SkShader > make_shader ( SkBlendMode  mode)
static

Definition at line 35 of file composeshader.cpp.

35 {
36 SkPoint pts[2];
37 SkColor colors[2];
38
39 pts[0].set(0, 0);
40 pts[1].set(SkIntToScalar(100), 0);
43 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
44
45 pts[0].set(0, 0);
46 pts[1].set(0, SkIntToScalar(100));
48 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
49 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
50
51 return SkShaders::Blend(mode, std::move(shaderA), std::move(shaderB));
52}

◆ make_src_shader()

static sk_sp< SkShader > make_src_shader ( SkScalar  size)
static

Definition at line 290 of file composeshader.cpp.

290 {
291 const SkPoint pts[] = { { 0, 0 }, { 0, size } };
292 const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
294}

Variable Documentation

◆ gCellSize

const SkScalar gCellSize = 100

Definition at line 302 of file composeshader.cpp.