Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
tilemodes_scaled.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/SkRect.h"
20#include "include/core/SkSize.h"
27#include "tools/ToolUtils.h"
29
37
38static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
41
42 SkCanvas canvas(*bm);
43 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
45 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
47
48 paint.setDither(true);
49 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
50 std::size(colors), SkTileMode::kClamp));
51 canvas.drawPaint(paint);
52}
53
54static void setup(SkPaint* paint, const SkBitmap& bm, const SkSamplingOptions& sampling,
56 paint->setShader(bm.makeShader(tmx, tmy, sampling));
57}
58
59constexpr SkColorType gColorTypes[] = {
60 kN32_SkColorType,
62};
63
64class ScaledTilingGM : public skiagm::GM {
65public:
66 ScaledTilingGM(bool powerOfTwoSize)
67 : fPowerOfTwoSize(powerOfTwoSize) {
68 }
69
71
72protected:
73 enum {
76 };
77
78 SkString getName() const override {
79 SkString name("scaled_tilemodes");
80 if (!fPowerOfTwoSize) {
81 name.append("_npot");
82 }
83 return name;
84 }
85
86 SkISize getISize() override { return SkISize::Make(880, 880); }
87
88 void onOnceBeforeDraw() override {
89 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
90 for (size_t i = 0; i < std::size(gColorTypes); i++) {
91 makebm(&fTexture[i], gColorTypes[i], size, size);
92 }
93 }
94
95 void onDraw(SkCanvas* canvas) override {
96 SkPaint textPaint;
98
99 float scale = 32.f/kPOTSize;
100
101 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
102
103 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
104
105 const char* gColorTypeNames[] = { "8888", "565" };
106
107 const char* gFilterNames[] = { "Nearest", "Linear", "Trilinear", "Mitchell", "Aniso" };
108
109 constexpr SkTileMode gModes[] = {
111 const char* gModeNames[] = { "C", "R", "M" };
112
115
116 for (size_t kx = 0; kx < std::size(gModes); kx++) {
117 for (size_t ky = 0; ky < std::size(gModes); ky++) {
118 SkString str;
119 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
120
121 SkTextUtils::DrawString(canvas, str.c_str(), scale*(x + r.width()/2), y, font, SkPaint(),
123
124 x += r.width() * 4 / 3;
125 }
126 }
127
128 y = SkIntToScalar(40) / scale;
129
130 for (size_t i = 0; i < std::size(gColorTypes); i++) {
131 for (size_t j = 0; j < std::size(gSamplings); j++) {
132 x = SkIntToScalar(10)/scale;
133 for (size_t kx = 0; kx < std::size(gModes); kx++) {
134 for (size_t ky = 0; ky < std::size(gModes); ky++) {
136#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
137 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
138 if (!fPowerOfTwoSize) {
139 makebm(&fTexture[i], gColorTypes[i], size, size);
140 }
141#endif
142 setup(&paint, fTexture[i], gSamplings[j], gModes[kx], gModes[ky]);
143 paint.setDither(true);
144
145 canvas->save();
146 canvas->scale(scale,scale);
147 canvas->translate(x, y);
148 canvas->drawRect(r, paint);
149 canvas->restore();
150
151 x += r.width() * 4 / 3;
152 }
153 }
154 canvas->drawString(SkStringPrintf("%s, %s", gColorTypeNames[i], gFilterNames[j]),
155 scale * x, scale * (y + r.height() * 2 / 3), font, textPaint);
156
157 y += r.height() * 4 / 3;
158 }
159 }
160 }
161
162private:
163 bool fPowerOfTwoSize;
164 using INHERITED = skiagm::GM;
165};
166
167constexpr int gWidth = 32;
168constexpr int gHeight = 32;
169
171 SkBitmap bm;
172 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
173 return bm.makeShader(tx, ty, SkSamplingOptions());
174}
175
177 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
180 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
181
182 int index = (int)ty;
183 switch (index % 3) {
184 case 0:
185 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors), tx);
186 case 1:
187 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, std::size(colors), tx);
188 case 2:
189 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, std::size(colors));
190 }
191
192 return nullptr;
193}
194
196
198 ShaderProc fProc;
199 const char* fName;
200public:
201 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
202
203private:
204 SkString getName() const override { return SkString(fName); }
205
206 SkISize getISize() override { return SkISize::Make(650, 610); }
207
208 void onDraw(SkCanvas* canvas) override {
209 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
210
213 SkRect r = { -w, -h, w*2, h*2 };
214
215 constexpr SkTileMode gModes[] = {
217 };
218 const char* gModeNames[] = {
219 "Clamp", "Repeat", "Mirror"
220 };
221
224
226
227 for (size_t kx = 0; kx < std::size(gModes); kx++) {
228 SkString str(gModeNames[kx]);
229 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
231 x += r.width() * 4 / 3;
232 }
233
234 y += SkIntToScalar(16) + h;
235
236 for (size_t ky = 0; ky < std::size(gModes); ky++) {
237 x = SkIntToScalar(16) + w;
238
239 SkString str(gModeNames[ky]);
240 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), SkTextUtils::kRight_Align);
241
242 x += SkIntToScalar(50);
243 for (size_t kx = 0; kx < std::size(gModes); kx++) {
245 paint.setShader(fProc(gModes[kx], gModes[ky]));
246
247 canvas->save();
248 canvas->translate(x, y);
249 canvas->drawRect(r, paint);
250 canvas->restore();
251
252 x += r.width() * 4 / 3;
253 }
254 y += r.height() * 4 / 3;
255 }
256 }
257};
258
259//////////////////////////////////////////////////////////////////////////////
260
261DEF_GM( return new ScaledTilingGM(true); )
262DEF_GM( return new ScaledTilingGM(false); )
263DEF_GM( return new ScaledTiling2GM(make_bm, "scaled_tilemode_bitmap"); )
264DEF_GM( return new ScaledTiling2GM(make_grad, "scaled_tilemode_gradient"); )
const char * fName
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
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
#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()
SkString getName() const override
SkISize getISize() override
ScaledTiling2GM(ShaderProc proc, const char name[])
void onDraw(SkCanvas *canvas) override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
ScaledTilingGM(bool powerOfTwoSize)
void onOnceBeforeDraw() override
SkBitmap fTexture[std::size(gColorTypes)]
SkISize getISize() override
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 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
const Paint & paint
float SkScalar
Definition extension.cpp:12
const char * name
Definition fuchsia.cc:50
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
sk_sp< SkTypeface > DefaultPortableTypeface()
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
Definition setup.py:1
SkTileMode tmy
SkTileMode tmx
SkScalar w
SkScalar h
const Scalar scale
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 constexpr SkSamplingOptions Aniso(int maxAniso)
static sk_sp< SkShader > make_grad(SkTileMode tx, SkTileMode ty)
static void makebm(SkBitmap *bm, SkColorType ct, int w, int h)
constexpr int gHeight
constexpr SkColorType gColorTypes[]
constexpr int gWidth
const SkSamplingOptions gSamplings[]
sk_sp< SkShader >(* ShaderProc)(SkTileMode, SkTileMode)