1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "gm.h"
9 #include "SkPath.h"
10 #include "SkRegion.h"
11 #include "SkShader.h"
12 #include "SkUtils.h"
13 #include "SkColorFilter.h"
14 
15 // effects
16 #include "SkGradientShader.h"
17 #include "SkBlurDrawLooper.h"
18 
makebm(SkBitmap * bm,SkColorType ct,int w,int h)19 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
20     bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
21     bm->eraseColor(SK_ColorTRANSPARENT);
22 
23     SkCanvas    canvas(*bm);
24     SkPoint     pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
25     SkColor     colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
26     SkScalar    pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
27     SkPaint     paint;
28 
29     paint.setDither(true);
30     paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
31                 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
32     canvas.drawPaint(paint);
33 }
34 
setup(SkPaint * paint,const SkBitmap & bm,bool filter,SkShader::TileMode tmx,SkShader::TileMode tmy)35 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
36                   SkShader::TileMode tmx, SkShader::TileMode tmy) {
37     SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
38     paint->setShader(shader)->unref();
39     paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
40 }
41 
42 static const SkColorType gColorTypes[] = {
43     kN32_SkColorType,
44     kRGB_565_SkColorType,
45 };
46 
47 class TilingGM : public skiagm::GM {
48 public:
TilingGM(bool powerOfTwoSize)49     TilingGM(bool powerOfTwoSize)
50             : fPowerOfTwoSize(powerOfTwoSize) {
51     }
52 
53     SkBitmap    fTexture[SK_ARRAY_COUNT(gColorTypes)];
54 
55 protected:
56 
57     enum {
58         kPOTSize = 32,
59         kNPOTSize = 21,
60     };
61 
onShortName()62     SkString onShortName() override {
63         SkString name("tilemodes");
64         if (!fPowerOfTwoSize) {
65             name.append("_npot");
66         }
67         return name;
68     }
69 
onISize()70     SkISize onISize() override { return SkISize::Make(880, 560); }
71 
onOnceBeforeDraw()72     void onOnceBeforeDraw() override {
73         int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
74         for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
75             makebm(&fTexture[i], gColorTypes[i], size, size);
76         }
77     }
78 
onDraw(SkCanvas * canvas)79     void onDraw(SkCanvas* canvas) override {
80 
81         int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
82 
83         SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
84 
85         static const char* gConfigNames[] = { "8888", "565", "4444" };
86 
87         static const bool           gFilters[] = { false, true };
88         static const char*          gFilterNames[] = {     "point",                     "bilinear" };
89 
90         static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
91         static const char*          gModeNames[] = {    "C",                    "R",                   "M" };
92 
93         SkScalar y = SkIntToScalar(24);
94         SkScalar x = SkIntToScalar(10);
95 
96         for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
97             for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
98                 SkPaint p;
99                 SkString str;
100                 p.setAntiAlias(true);
101                 sk_tool_utils::set_portable_typeface(&p);
102                 p.setDither(true);
103                 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
104 
105                 p.setTextAlign(SkPaint::kCenter_Align);
106                 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
107 
108                 x += r.width() * 4 / 3;
109             }
110         }
111 
112         y += SkIntToScalar(16);
113 
114         for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
115             for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
116                 x = SkIntToScalar(10);
117                 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
118                     for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
119                         SkPaint paint;
120 #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
121       // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
122                         if (!fPowerOfTwoSize) {
123                             makebm(&fTexture[i], gColorTypes[i], size, size);
124                         }
125 #endif
126                         setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
127                         paint.setDither(true);
128 
129                         canvas->save();
130                         canvas->translate(x, y);
131                         canvas->drawRect(r, paint);
132                         canvas->restore();
133 
134                         x += r.width() * 4 / 3;
135                     }
136                 }
137                 {
138                     SkPaint p;
139                     SkString str;
140                     p.setAntiAlias(true);
141                     sk_tool_utils::set_portable_typeface(&p);
142                     str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
143                     canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
144                 }
145 
146                 y += r.height() * 4 / 3;
147             }
148         }
149     }
150 
151 private:
152     bool fPowerOfTwoSize;
153     typedef skiagm::GM INHERITED;
154 };
155 
156 static const int gWidth = 32;
157 static const int gHeight = 32;
158 
make_bm(SkShader::TileMode tx,SkShader::TileMode ty)159 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
160     SkBitmap bm;
161     makebm(&bm, kN32_SkColorType, gWidth, gHeight);
162     return SkShader::CreateBitmapShader(bm, tx, ty);
163 }
164 
make_grad(SkShader::TileMode tx,SkShader::TileMode ty)165 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
166     SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
167     SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
168     SkScalar rad = SkIntToScalar(gWidth)/2;
169     SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
170 
171     int index = (int)ty;
172     switch (index % 3) {
173         case 0:
174             return SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
175         case 1:
176             return SkGradientShader::CreateRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
177         case 2:
178             return SkGradientShader::CreateSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
179     }
180 
181     return nullptr;
182 }
183 
184 typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
185 
186 class Tiling2GM : public skiagm::GM {
187     ShaderProc fProc;
188     SkString   fName;
189 public:
Tiling2GM(ShaderProc proc,const char name[])190     Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
191         fName.printf("tilemode_%s", name);
192     }
193 
194 protected:
195 
onShortName()196     SkString onShortName() override {
197         return fName;
198     }
199 
onISize()200     SkISize onISize() override { return SkISize::Make(880, 560); }
201 
onDraw(SkCanvas * canvas)202     void onDraw(SkCanvas* canvas) override {
203         canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
204 
205         const SkScalar w = SkIntToScalar(gWidth);
206         const SkScalar h = SkIntToScalar(gHeight);
207         SkRect r = { -w, -h, w*2, h*2 };
208 
209         static const SkShader::TileMode gModes[] = {
210             SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
211         };
212         static const char* gModeNames[] = {
213             "Clamp", "Repeat", "Mirror"
214         };
215 
216         SkScalar y = SkIntToScalar(24);
217         SkScalar x = SkIntToScalar(66);
218 
219         SkPaint p;
220         p.setAntiAlias(true);
221         sk_tool_utils::set_portable_typeface(&p);
222         p.setTextAlign(SkPaint::kCenter_Align);
223 
224         for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
225             SkString str(gModeNames[kx]);
226             canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
227             x += r.width() * 4 / 3;
228         }
229 
230         y += SkIntToScalar(16) + h;
231         p.setTextAlign(SkPaint::kRight_Align);
232 
233         for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
234             x = SkIntToScalar(16) + w;
235 
236             SkString str(gModeNames[ky]);
237             canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
238 
239             x += SkIntToScalar(50);
240             for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
241                 SkPaint paint;
242                 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
243 
244                 canvas->save();
245                 canvas->translate(x, y);
246                 canvas->drawRect(r, paint);
247                 canvas->restore();
248 
249                 x += r.width() * 4 / 3;
250             }
251             y += r.height() * 4 / 3;
252         }
253     }
254 
255 private:
256     typedef skiagm::GM INHERITED;
257 };
258 
259 //////////////////////////////////////////////////////////////////////////////
260 
261 DEF_GM( return new TilingGM(true); )
262 DEF_GM( return new TilingGM(false); )
263 DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
264 DEF_GM( return new Tiling2GM(make_grad, "gradient"); )
265