• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "gm.h"
2 #include "SkPath.h"
3 #include "SkRegion.h"
4 #include "SkShader.h"
5 #include "SkUtils.h"
6 #include "SkColorPriv.h"
7 #include "SkColorFilter.h"
8 #include "SkTypeface.h"
9 
10 // effects
11 #include "SkGradientShader.h"
12 #include "SkUnitMappers.h"
13 #include "SkBlurDrawLooper.h"
14 
15 namespace skiagm {
16 
makebm(SkBitmap * bm,SkBitmap::Config config,int w,int h)17 static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
18     bm->setConfig(config, w, h);
19     bm->allocPixels();
20     bm->eraseColor(0);
21 
22     SkCanvas    canvas(*bm);
23     SkPoint     pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
24     SkColor     colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25     SkScalar    pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
26     SkPaint     paint;
27 
28     SkUnitMapper*   um = NULL;
29 
30     um = new SkCosineMapper;
31 //    um = new SkDiscreteMapper(12);
32 
33     SkAutoUnref au(um);
34 
35     paint.setDither(true);
36     paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
37                 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
38     canvas.drawPaint(paint);
39 }
40 
setup(SkPaint * paint,const SkBitmap & bm,bool filter,SkShader::TileMode tmx,SkShader::TileMode tmy)41 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
42                   SkShader::TileMode tmx, SkShader::TileMode tmy) {
43     SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
44     paint->setShader(shader)->unref();
45     paint->setFilterBitmap(filter);
46 }
47 
48 static const SkBitmap::Config gConfigs[] = {
49     SkBitmap::kARGB_8888_Config,
50     SkBitmap::kRGB_565_Config,
51     SkBitmap::kARGB_4444_Config
52 };
53 static const int gWidth = 32;
54 static const int gHeight = 32;
55 
56 class TilingGM : public GM {
57     SkBlurDrawLooper    fLooper;
58 public:
TilingGM()59 	TilingGM()
60             : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
61                       0x88000000) {
62         for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
63             makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
64         }
65     }
66 
67     SkBitmap    fTexture[SK_ARRAY_COUNT(gConfigs)];
68 
69 protected:
onShortName()70     SkString onShortName() {
71         return SkString("tilemodes");
72     }
73 
onISize()74 	SkISize onISize() { return make_isize(880, 560); }
75 
drawBG(SkCanvas * canvas)76     void drawBG(SkCanvas* canvas) {
77         canvas->drawColor(SK_ColorWHITE);
78     }
79 
onDraw(SkCanvas * canvas)80     virtual void onDraw(SkCanvas* canvas) {
81         this->drawBG(canvas);
82 
83         SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*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                 p.setDither(true);
102                 p.setLooper(&fLooper);
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(gConfigs); 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                         setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
121                         paint.setDither(true);
122 
123                         canvas->save();
124                         canvas->translate(x, y);
125                         canvas->drawRect(r, paint);
126                         canvas->restore();
127 
128                         x += r.width() * 4 / 3;
129                     }
130                 }
131                 {
132                     SkPaint p;
133                     SkString str;
134                     p.setAntiAlias(true);
135                     p.setLooper(&fLooper);
136                     str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
137                     canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
138                 }
139 
140                 y += r.height() * 4 / 3;
141             }
142         }
143     }
144 
145 private:
146     typedef GM INHERITED;
147 };
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
MyFactory(void *)151 static GM* MyFactory(void*) { return new TilingGM; }
152 static GMRegistry reg(MyFactory);
153 
154 }
155 
156