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 "SkColorPriv.h" 14#include "SkColorFilter.h" 15#include "SkTypeface.h" 16#include "SkBlurMask.h" 17 18// effects 19#include "SkGradientShader.h" 20#include "SkBlurDrawLooper.h" 21 22static 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); 25 26 SkCanvas canvas(*bm); 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 }; 30 SkPaint paint; 31 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); 36} 37 38static 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); 43} 44 45static const SkColorType gColorTypes[] = { 46 kN32_SkColorType, 47 kRGB_565_SkColorType, 48}; 49 50class ScaledTilingGM : public skiagm::GM { 51 SkAutoTUnref<SkBlurDrawLooper> fLooper; 52public: 53 ScaledTilingGM(bool powerOfTwoSize) 54 : fLooper(SkBlurDrawLooper::Create(0x88000000, 55 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), 56 SkIntToScalar(2), SkIntToScalar(2))) 57 , fPowerOfTwoSize(powerOfTwoSize) { 58 } 59 60 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; 61 62protected: 63 enum { 64 kPOTSize = 4, 65 kNPOTSize = 3, 66 }; 67 68 SkString onShortName() override { 69 SkString name("scaled_tilemodes"); 70 if (!fPowerOfTwoSize) { 71 name.append("_npot"); 72 } 73 return name; 74 } 75 76 SkISize onISize() override { return SkISize::Make(880, 760); } 77 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); 82 } 83 } 84 85 void onDraw(SkCanvas* canvas) override { 86 float scale = 32.f/kPOTSize; 87 88 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; 89 90 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; 91 92 static const char* gColorTypeNames[] = { "8888" , "565", "4444" }; 93 94 static const SkFilterQuality gFilterQualitys[] = 95 { kNone_SkFilterQuality, 96 kLow_SkFilterQuality, 97 kMedium_SkFilterQuality, 98 kHigh_SkFilterQuality }; 99 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" }; 100 101 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; 102 static const char* gModeNames[] = { "C", "R", "M" }; 103 104 SkScalar y = SkIntToScalar(24); 105 SkScalar x = SkIntToScalar(10)/scale; 106 107 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 108 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 109 SkPaint p; 110 SkString str; 111 p.setAntiAlias(true); 112 sk_tool_utils::set_portable_typeface(&p); 113 p.setDither(true); 114 p.setLooper(fLooper); 115 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); 116 117 p.setTextAlign(SkPaint::kCenter_Align); 118 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p); 119 120 x += r.width() * 4 / 3; 121 } 122 } 123 124 y = SkIntToScalar(40) / scale; 125 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++) { 131 SkPaint paint; 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); 136 } 137#endif 138 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]); 139 paint.setDither(true); 140 141 canvas->save(); 142 canvas->scale(scale,scale); 143 canvas->translate(x, y); 144 canvas->drawRect(r, paint); 145 canvas->restore(); 146 147 x += r.width() * 4 / 3; 148 } 149 } 150 { 151 SkPaint p; 152 SkString str; 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); 158 } 159 160 y += r.height() * 4 / 3; 161 } 162 } 163 } 164 165private: 166 bool fPowerOfTwoSize; 167 typedef skiagm::GM INHERITED; 168}; 169 170static const int gWidth = 32; 171static const int gHeight = 32; 172 173static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { 174 SkBitmap bm; 175 makebm(&bm, kN32_SkColorType, gWidth, gHeight); 176 return SkShader::CreateBitmapShader(bm, tx, ty); 177} 178 179static 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 }; 184 185 int index = (int)ty; 186 switch (index % 3) { 187 case 0: 188 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx); 189 case 1: 190 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx); 191 case 2: 192 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors)); 193 } 194 195 return NULL; 196} 197 198typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); 199 200class ScaledTiling2GM : public skiagm::GM { 201 ShaderProc fProc; 202 SkString fName; 203public: 204 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { 205 fName.printf("scaled_tilemode_%s", name); 206 } 207 208protected: 209 210 SkString onShortName() override { 211 return fName; 212 } 213 214 SkISize onISize() override { return SkISize::Make(880, 560); } 215 216 void onDraw(SkCanvas* canvas) override { 217 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); 218 219 const SkScalar w = SkIntToScalar(gWidth); 220 const SkScalar h = SkIntToScalar(gHeight); 221 SkRect r = { -w, -h, w*2, h*2 }; 222 223 static const SkShader::TileMode gModes[] = { 224 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode 225 }; 226 static const char* gModeNames[] = { 227 "Clamp", "Repeat", "Mirror" 228 }; 229 230 SkScalar y = SkIntToScalar(24); 231 SkScalar x = SkIntToScalar(66); 232 233 SkPaint p; 234 p.setAntiAlias(true); 235 sk_tool_utils::set_portable_typeface(&p); 236 p.setTextAlign(SkPaint::kCenter_Align); 237 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; 242 } 243 244 y += SkIntToScalar(16) + h; 245 p.setTextAlign(SkPaint::kRight_Align); 246 247 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 248 x = SkIntToScalar(16) + w; 249 250 SkString str(gModeNames[ky]); 251 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); 252 253 x += SkIntToScalar(50); 254 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 255 SkPaint paint; 256 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); 257 258 canvas->save(); 259 canvas->translate(x, y); 260 canvas->drawRect(r, paint); 261 canvas->restore(); 262 263 x += r.width() * 4 / 3; 264 } 265 y += r.height() * 4 / 3; 266 } 267 } 268 269private: 270 typedef skiagm::GM INHERITED; 271}; 272 273////////////////////////////////////////////////////////////////////////////// 274 275DEF_GM( return new ScaledTilingGM(true); ) 276DEF_GM( return new ScaledTilingGM(false); ) 277DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) 278DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) 279