Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
tilemodes.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"
17#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
28#include "tools/DecodeUtils.h"
29#include "tools/GpuToolUtils.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
33
34#include <functional>
35
36static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
39
40 SkCanvas canvas(*bm);
41 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
43 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
45
46 paint.setDither(true);
47 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, std::size(colors),
49 canvas.drawPaint(paint);
50}
51
52static void setup(SkCanvas* canvas, SkPaint* paint, const SkBitmap& bm, SkFilterMode fm,
55 img = ToolUtils::MakeTextureImage(canvas, std::move(img));
56 if (img) {
57 // img can be null if the GPU context has been abandoned.
58 paint->setShader(img->makeShader(tmx, tmy, SkSamplingOptions(fm)));
59 }
60}
61
62constexpr SkColorType gColorTypes[] = {
63 kN32_SkColorType,
65};
66
67class TilingGM : public skiagm::GM {
68public:
69 TilingGM(bool powerOfTwoSize)
70 : fPowerOfTwoSize(powerOfTwoSize) {
71 }
72
74
75protected:
76
77 enum {
80 };
81
82 SkString getName() const override {
83 SkString name("tilemodes");
84 if (!fPowerOfTwoSize) {
85 name.append("_npot");
86 }
87 return name;
88 }
89
90 SkISize getISize() override { return SkISize::Make(880, 560); }
91
92 void onOnceBeforeDraw() override {
93 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
94 for (size_t i = 0; i < std::size(gColorTypes); i++) {
95 makebm(&fTexture[i], gColorTypes[i], size, size);
96 }
97 }
98
99 void onDraw(SkCanvas* canvas) override {
100 SkPaint textPaint;
102
103 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
104
105 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
106
107 const char* gConfigNames[] = { "8888", "565" };
108
110 static const char* gFilterNames[] = { "point", "bilinear" };
111
112 constexpr SkTileMode gModes[] = {
114 static const char* gModeNames[] = { "C", "R", "M" };
115
118
119 for (size_t kx = 0; kx < std::size(gModes); kx++) {
120 for (size_t ky = 0; ky < std::size(gModes); ky++) {
121 SkPaint p;
122 p.setDither(true);
123 SkString str;
124 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
125
126 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p,
128
129 x += r.width() * 4 / 3;
130 }
131 }
132
133 y += SkIntToScalar(16);
134
135 for (size_t i = 0; i < std::size(gColorTypes); i++) {
136 for (size_t j = 0; j < std::size(gFilters); j++) {
137 x = SkIntToScalar(10);
138 for (size_t kx = 0; kx < std::size(gModes); kx++) {
139 for (size_t ky = 0; ky < std::size(gModes); ky++) {
141#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
142 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
143 if (!fPowerOfTwoSize) {
144 makebm(&fTexture[i], gColorTypes[i], size, size);
145 }
146#endif
147 setup(canvas, &paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
148 paint.setDither(true);
149
150 canvas->save();
151 canvas->translate(x, y);
152 canvas->drawRect(r, paint);
153 canvas->restore();
154
155 x += r.width() * 4 / 3;
156 }
157 }
158 canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
159 x, y + r.height() * 2 / 3, font, textPaint);
160
161 y += r.height() * 4 / 3;
162 }
163 }
164 }
165
166private:
167 bool fPowerOfTwoSize;
168 using INHERITED = skiagm::GM;
169};
170DEF_GM( return new TilingGM(true); )
171DEF_GM( return new TilingGM(false); )
172
173constexpr int gWidth = 32;
174constexpr int gHeight = 32;
175
177 SkBitmap bm;
178 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
179 return bm.makeShader(tx, ty, SkSamplingOptions());
180}
181
183 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
186 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
187
188 int index = (int)ty;
189 switch (index % 3) {
190 case 0:
191 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors), tx);
192 case 1:
193 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, std::size(colors), tx);
194 case 2:
195 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
196 std::size(colors), tx, 135, 225, 0, nullptr);
197 }
198 return nullptr;
199}
200
202
203class Tiling2GM : public skiagm::GM {
204 ShaderProc fProc;
205 const char* fName;
206
207public:
208 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
209
210private:
211 SkString getName() const override { return SkString(fName); }
212
213 SkISize getISize() override { return SkISize::Make(650, 610); }
214
215 void onDraw(SkCanvas* canvas) override {
216 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
217
220 SkRect r = { -w, -h, w*2, h*2 };
221
222 constexpr SkTileMode gModes[] = {
224 };
225 const char* gModeNames[] = {
226 "Clamp", "Repeat", "Mirror"
227 };
228
231
233
234 for (size_t kx = 0; kx < std::size(gModes); kx++) {
235 SkString str(gModeNames[kx]);
236 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
238 x += r.width() * 4 / 3;
239 }
240
241 y += SkIntToScalar(16) + h;
242
243 for (size_t ky = 0; ky < std::size(gModes); ky++) {
244 x = SkIntToScalar(16) + w;
245
246 SkString str(gModeNames[ky]);
247 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(),
249
250 x += SkIntToScalar(50);
251 for (size_t kx = 0; kx < std::size(gModes); kx++) {
253 paint.setShader(fProc(gModes[kx], gModes[ky]));
254
255 canvas->save();
256 canvas->translate(x, y);
257 canvas->drawRect(r, paint);
258 canvas->restore();
259
260 x += r.width() * 4 / 3;
261 }
262 y += r.height() * 4 / 3;
263 }
264 }
265};
266
267DEF_GM( return new Tiling2GM(make_bm, "tilemode_bitmap"); )
268DEF_GM( return new Tiling2GM(make_grad, "tilemode_gradient"); )
269
270////////////////////
271
272DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) {
273 auto img = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
274 SkPaint bgpaint;
275 bgpaint.setColor(SK_ColorYELLOW);
276
277 SkRect r = { -20, -20, img->width() + 20.0f, img->height() + 20.0f };
278 canvas->translate(45, 45);
279
280 std::function<void(SkPaint*, SkTileMode, SkTileMode)> shader_procs[] = {
281 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
282 // Test no filtering with decal mode
283 paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kNearest)));
284 },
285 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
286 // Test bilerp approximation for decal mode (or clamp to border HW)
287 paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kLinear)));
288 },
289 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
290 // Test bicubic filter with decal mode
291 paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkCubicResampler::Mitchell())));
292 },
293 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
294 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
295 const SkPoint pts[] = {{ 0, 0 }, {img->width()*1.0f, img->height()*1.0f }};
296 const SkScalar* pos = nullptr;
297 const int count = std::size(colors);
298 paint->setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, tx));
299 },
300 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
301 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
302 const SkScalar* pos = nullptr;
303 const int count = std::size(colors);
304 paint->setShader(SkGradientShader::MakeRadial({ img->width()*0.5f, img->width()*0.5f },
305 img->width()*0.5f, colors, pos, count, tx));
306 },
307 };
308
309 const struct XY {
310 SkTileMode fX;
311 SkTileMode fY;
312 } pairs[] = {
317 };
318 for (const auto& p : pairs) {
320 canvas->save();
321 for (const auto& proc : shader_procs) {
322 canvas->save();
323 // Apply a slight rotation to highlight the differences between filtered and unfiltered
324 // decal edges
325 canvas->rotate(4);
326 canvas->drawRect(r, bgpaint);
327 proc(&paint, p.fX, p.fY);
328 canvas->drawRect(r, paint);
329 canvas->restore();
330 canvas->translate(0, r.height() + 20);
331 }
332 canvas->restore();
333 canvas->translate(r.width() + 10, 0);
334 }
335}
const char * fName
int count
SkPoint pos
IsFiniteProc fProc
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
SkColorType
Definition SkColorType.h:19
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
SkFilterMode
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
SkTileMode
Definition SkTileMode.h:13
static SkScalar center(float pos0, float pos1)
constexpr SkBlendMode gModes[]
Type::kYUV Type::kRGBA() int(0.7 *637)
static sk_sp< SkImage > make_bm()
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawPaint(const SkPaint &paint)
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
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)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
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)
void setColor(SkColor color)
Definition SkPaint.cpp:119
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
const char * c_str() const
Definition SkString.h:133
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition SkTextUtils.h:34
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
Tiling2GM(ShaderProc proc, const char name[])
void onDraw(SkCanvas *canvas) override
Definition tilemodes.cpp:99
SkString getName() const override
Definition tilemodes.cpp:82
SkBitmap fTexture[std::size(gColorTypes)]
Definition tilemodes.cpp:73
void onOnceBeforeDraw() override
Definition tilemodes.cpp:92
SkISize getISize() override
Definition tilemodes.cpp:90
TilingGM(bool powerOfTwoSize)
Definition tilemodes.cpp:69
const Paint & paint
float SkScalar
Definition extension.cpp:12
const char * name
Definition fuchsia.cc:50
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double y
double x
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)
SkFont DefaultPortableFont()
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
SkColor color_to_565(SkColor color)
Definition setup.py:1
SkTileMode tmy
SkTileMode tmx
SkScalar w
SkScalar h
static constexpr SkCubicResampler Mitchell()
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static sk_sp< SkShader > make_grad(SkTileMode tx, SkTileMode ty)
static void makebm(SkBitmap *bm, SkColorType ct, int w, int h)
Definition tilemodes.cpp:36
constexpr int gHeight
constexpr SkColorType gColorTypes[]
Definition tilemodes.cpp:62
constexpr int gWidth
sk_sp< SkShader >(* ShaderProc)(SkTileMode, SkTileMode)