3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
13 #include "SkColorPriv.h"
14 #include "SkColorFilter.h"
15 #include "SkTypeface.h"
18 #include "SkGradientShader.h"
19 #include "SkBlurDrawLooper.h"
21 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
22 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
23 bm->eraseColor(SK_ColorTRANSPARENT);
26 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
27 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
28 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
31 paint.setDither(true);
32 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
33 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
34 canvas.drawPaint(paint);
37 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
38 SkShader::TileMode tmx, SkShader::TileMode tmy) {
39 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
40 paint->setShader(shader)->unref();
41 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
44 static const SkColorType gColorTypes[] = {
49 class TilingGM : public skiagm::GM {
51 TilingGM(bool powerOfTwoSize)
52 : fPowerOfTwoSize(powerOfTwoSize) {
55 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
64 SkString onShortName() override {
65 SkString name("tilemodes");
66 if (!fPowerOfTwoSize) {
72 SkISize onISize() override { return SkISize::Make(880, 560); }
74 void onOnceBeforeDraw() override {
75 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
76 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
77 makebm(&fTexture[i], gColorTypes[i], size, size);
81 void onDraw(SkCanvas* canvas) override {
83 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
85 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
89 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
92 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
98 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
99 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
102 p.setAntiAlias(true);
103 sk_tool_utils::set_portable_typeface(&p);
105 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
107 p.setTextAlign(SkPaint::kCenter_Align);
108 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
110 x += r.width() * 4 / 3;
114 y += SkIntToScalar(16);
116 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
117 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
118 x = SkIntToScalar(10);
119 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
120 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
122 #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
123 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
124 if (!fPowerOfTwoSize) {
125 makebm(&fTexture[i], gColorTypes[i], size, size);
128 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
129 paint.setDither(true);
132 canvas->translate(x, y);
133 canvas->drawRect(r, paint);
136 x += r.width() * 4 / 3;
142 p.setAntiAlias(true);
143 sk_tool_utils::set_portable_typeface(&p);
144 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
145 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
148 y += r.height() * 4 / 3;
154 bool fPowerOfTwoSize;
155 typedef skiagm::GM INHERITED;
158 static const int gWidth = 32;
159 static const int gHeight = 32;
161 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
163 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
164 return SkShader::CreateBitmapShader(bm, tx, ty);
167 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
168 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
169 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
170 SkScalar rad = SkIntToScalar(gWidth)/2;
171 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
176 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
178 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
180 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
186 typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
188 class Tiling2GM : public skiagm::GM {
192 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
193 fName.printf("tilemode_%s", name);
198 SkString onShortName() override {
202 SkISize onISize() override { return SkISize::Make(880, 560); }
204 void onDraw(SkCanvas* canvas) override {
205 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
207 const SkScalar w = SkIntToScalar(gWidth);
208 const SkScalar h = SkIntToScalar(gHeight);
209 SkRect r = { -w, -h, w*2, h*2 };
211 static const SkShader::TileMode gModes[] = {
212 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
214 static const char* gModeNames[] = {
215 "Clamp", "Repeat", "Mirror"
218 SkScalar y = SkIntToScalar(24);
219 SkScalar x = SkIntToScalar(66);
222 p.setAntiAlias(true);
223 sk_tool_utils::set_portable_typeface(&p);
224 p.setTextAlign(SkPaint::kCenter_Align);
226 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
227 SkString str(gModeNames[kx]);
228 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
229 x += r.width() * 4 / 3;
232 y += SkIntToScalar(16) + h;
233 p.setTextAlign(SkPaint::kRight_Align);
235 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
236 x = SkIntToScalar(16) + w;
238 SkString str(gModeNames[ky]);
239 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
241 x += SkIntToScalar(50);
242 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
244 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
247 canvas->translate(x, y);
248 canvas->drawRect(r, paint);
251 x += r.width() * 4 / 3;
253 y += r.height() * 4 / 3;
258 typedef skiagm::GM INHERITED;
261 //////////////////////////////////////////////////////////////////////////////
263 DEF_GM( return new TilingGM(true); )
264 DEF_GM( return new TilingGM(false); )
265 DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
266 DEF_GM( return new Tiling2GM(make_grad, "gradient"); )