Flutter Engine
The Flutter Engine
bitmapfilters.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
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#include "gm/gm.h"
12#include "include/core/SkFont.h"
16#include "include/core/SkSize.h"
20#include "tools/ToolUtils.h"
22
23static void make_bm(SkBitmap* bm) {
24 const SkColor colors[4] = {
27 };
28 SkPMColor colorsPM[4];
29 for (size_t i = 0; i < std::size(colors); ++i) {
30 colorsPM[i] = SkPreMultiplyColor(colors[i]);
31 }
32 bm->allocN32Pixels(2, 2, true);
33
34 *bm->getAddr32(0, 0) = colorsPM[0];
35 *bm->getAddr32(1, 0) = colorsPM[1];
36 *bm->getAddr32(0, 1) = colorsPM[2];
37 *bm->getAddr32(1, 1) = colorsPM[3];
38}
39
42 canvas->drawImage(img, x, y, sampling, paint);
43 return SkIntToScalar(img->width()) * 5/4;
44}
45
47 x += draw_bm(c, img, x, 0, SkSamplingOptions(), p);
49 p->setDither(true);
50 return x + draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p);
51}
52
54 SkAutoCanvasRestore acr(canvas, true);
55
57 paint.setAntiAlias(true);
58
59 SkScalar x = 0;
60 const int scale = 32;
61
63 const char* name = ToolUtils::colortype_name(img->colorType());
64 canvas->drawString(name, x, SkIntToScalar(img->height())*scale*5/8, font, paint);
65 canvas->translate(SkIntToScalar(48), 0);
66
68
69 x += draw_set(canvas, img, 0, &paint);
70 paint.reset();
71 paint.setAlphaf(0.5f);
72 draw_set(canvas, img, x, &paint);
73 return x * scale / 3;
74}
75
76class FilterGM : public skiagm::GM {
77 void onOnceBeforeDraw() override {
78 SkBitmap bm32, bm4444, bm565;
79 make_bm(&bm32);
82
83 fImg32 = bm32.asImage();
84 fImg4444 = bm4444.asImage();
85 fImg565 = bm565.asImage();
86 }
87
88public:
90
92 this->setBGColor(0xFFDDDDDD);
93 }
94
95protected:
96 SkString getName() const override { return SkString("bitmapfilters"); }
97
98 SkISize getISize() override { return SkISize::Make(540, 250); }
99
100 void onDraw(SkCanvas* canvas) override {
103
104 canvas->translate(x, y);
105 y = draw_row(canvas, fImg4444);
106 canvas->translate(0, y);
107 y = draw_row(canvas, fImg565);
108 canvas->translate(0, y);
109 draw_row(canvas, fImg32);
110 }
111
112private:
113 using INHERITED = skiagm::GM;
114};
115DEF_GM( return new FilterGM; )
116
117//////////////////////////////////////////////////////////////////////////////
118
120 void onOnceBeforeDraw() override {
121 // Make a bitmap with per-pixels alpha (stroked circle)
122 fBitmap.allocN32Pixels(100, 100);
123 SkCanvas canvas(fBitmap);
124 canvas.clear(0);
125
127 paint.setAntiAlias(true);
128 paint.setColor(SK_ColorBLUE);
130 paint.setStrokeWidth(20);
131
132 canvas.drawCircle(50, 50, 39, paint);
133
134 fBitmap.extractAlpha(&fAlpha);
135 }
136
137public:
138 SkBitmap fBitmap, fAlpha;
139
140protected:
141 SkString getName() const override { return SkString("extractalpha"); }
142
143 SkISize getISize() override { return SkISize::Make(540, 330); }
144
145 void onDraw(SkCanvas* canvas) override {
147 paint.setAntiAlias(true);
148 paint.setColor(SK_ColorRED);
149
151
152 // should stay blue (ignore paint's color)
153 canvas->drawImage(fBitmap.asImage(), 10, 10, sampling, &paint);
154 // should draw red
155 canvas->drawImage(fAlpha.asImage(), 120, 10, sampling, &paint);
156 }
157
158private:
159 using INHERITED = skiagm::GM;
160};
161DEF_GM( return new TestExtractAlphaGM; )
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Definition: SkColor.cpp:21
uint32_t SkColor
Definition: SkColor.h:37
uint32_t SkPMColor
Definition: SkColor.h:205
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static SkScalar draw_bm(SkCanvas *canvas, sk_sp< SkImage > img, SkScalar x, SkScalar y, const SkSamplingOptions &sampling, SkPaint *paint)
static SkScalar draw_set(SkCanvas *c, sk_sp< SkImage > img, SkScalar x, SkPaint *p)
static void make_bm(SkBitmap *bm)
static SkScalar draw_row(SkCanvas *canvas, sk_sp< SkImage > img)
SkString getName() const override
SkISize getISize() override
sk_sp< SkImage > fImg32
sk_sp< SkImage > fImg565
void onDraw(SkCanvas *canvas) override
sk_sp< SkImage > fImg4444
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
Definition: SkFont.h:35
int width() const
Definition: SkImage.h:285
SkColorType colorType() const
Definition: SkImage.cpp:152
int height() const
Definition: SkImage.h:291
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
SkString getName() const override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
Definition: gm.h:110
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
SkFont DefaultPortableFont()
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
Definition: ToolUtils.cpp:394
const char * colortype_name(SkColorType ct)
Definition: ToolUtils.cpp:65
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
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
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
const Scalar scale
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20