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"
16 #include "SkBlurMask.h"
19 #include "SkGradientShader.h"
20 #include "SkBlurDrawLooper.h"
22 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
23 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
24 bm->eraseColor(SK_ColorTRANSPARENT);
27 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
28 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
29 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
32 paint.setDither(true);
33 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
34 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
35 canvas.drawPaint(paint);
38 static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
39 SkShader::TileMode tmx, SkShader::TileMode tmy) {
40 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
41 paint->setShader(shader)->unref();
42 paint->setFilterQuality(filter_level);
45 static const SkColorType gColorTypes[] = {
50 class ScaledTilingGM : public skiagm::GM {
51 SkAutoTUnref<SkBlurDrawLooper> fLooper;
53 ScaledTilingGM(bool powerOfTwoSize)
54 : fLooper(SkBlurDrawLooper::Create(0x88000000,
55 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
56 SkIntToScalar(2), SkIntToScalar(2)))
57 , fPowerOfTwoSize(powerOfTwoSize) {
60 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
68 SkString onShortName() override {
69 SkString name("scaled_tilemodes");
70 if (!fPowerOfTwoSize) {
76 SkISize onISize() override { return SkISize::Make(880, 760); }
78 void onOnceBeforeDraw() override {
79 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
80 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
81 makebm(&fTexture[i], gColorTypes[i], size, size);
85 void onDraw(SkCanvas* canvas) override {
86 float scale = 32.f/kPOTSize;
88 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
90 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
92 static const char* gColorTypeNames[] = { "8888" , "565", "4444" };
94 static const SkFilterQuality gFilterQualitys[] =
95 { kNone_SkFilterQuality,
97 kMedium_SkFilterQuality,
98 kHigh_SkFilterQuality };
99 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
101 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
102 static const char* gModeNames[] = { "C", "R", "M" };
104 SkScalar y = SkIntToScalar(24);
105 SkScalar x = SkIntToScalar(10)/scale;
107 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
108 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
111 p.setAntiAlias(true);
112 sk_tool_utils::set_portable_typeface(&p);
114 p.setLooper(fLooper);
115 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
117 p.setTextAlign(SkPaint::kCenter_Align);
118 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p);
120 x += r.width() * 4 / 3;
124 y = SkIntToScalar(40) / scale;
126 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
127 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
128 x = SkIntToScalar(10)/scale;
129 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
130 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
132 #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
133 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
134 if (!fPowerOfTwoSize) {
135 makebm(&fTexture[i], gColorTypes[i], size, size);
138 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
139 paint.setDither(true);
142 canvas->scale(scale,scale);
143 canvas->translate(x, y);
144 canvas->drawRect(r, paint);
147 x += r.width() * 4 / 3;
153 p.setAntiAlias(true);
154 sk_tool_utils::set_portable_typeface(&p);
155 p.setLooper(fLooper);
156 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
157 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p);
160 y += r.height() * 4 / 3;
166 bool fPowerOfTwoSize;
167 typedef skiagm::GM INHERITED;
170 static const int gWidth = 32;
171 static const int gHeight = 32;
173 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
175 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
176 return SkShader::CreateBitmapShader(bm, tx, ty);
179 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
180 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
181 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
182 SkScalar rad = SkIntToScalar(gWidth)/2;
183 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
188 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
190 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
192 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
198 typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
200 class ScaledTiling2GM : public skiagm::GM {
204 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
205 fName.printf("scaled_tilemode_%s", name);
210 SkString onShortName() override {
214 SkISize onISize() override { return SkISize::Make(880, 560); }
216 void onDraw(SkCanvas* canvas) override {
217 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
219 const SkScalar w = SkIntToScalar(gWidth);
220 const SkScalar h = SkIntToScalar(gHeight);
221 SkRect r = { -w, -h, w*2, h*2 };
223 static const SkShader::TileMode gModes[] = {
224 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
226 static const char* gModeNames[] = {
227 "Clamp", "Repeat", "Mirror"
230 SkScalar y = SkIntToScalar(24);
231 SkScalar x = SkIntToScalar(66);
234 p.setAntiAlias(true);
235 sk_tool_utils::set_portable_typeface(&p);
236 p.setTextAlign(SkPaint::kCenter_Align);
238 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
239 SkString str(gModeNames[kx]);
240 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
241 x += r.width() * 4 / 3;
244 y += SkIntToScalar(16) + h;
245 p.setTextAlign(SkPaint::kRight_Align);
247 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
248 x = SkIntToScalar(16) + w;
250 SkString str(gModeNames[ky]);
251 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
253 x += SkIntToScalar(50);
254 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
256 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
259 canvas->translate(x, y);
260 canvas->drawRect(r, paint);
263 x += r.width() * 4 / 3;
265 y += r.height() * 4 / 3;
270 typedef skiagm::GM INHERITED;
273 //////////////////////////////////////////////////////////////////////////////
275 DEF_GM( return new ScaledTilingGM(true); )
276 DEF_GM( return new ScaledTilingGM(false); )
277 DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
278 DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )