blob: 12b1b09d06d8718163e769eb9ed637507d9995d4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com1a2fec52009-06-22 02:17:34 +00008#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
17// effects
18#include "SkGradientShader.h"
19#include "SkUnitMappers.h"
20#include "SkBlurDrawLooper.h"
21
22namespace skiagm {
23
24static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
25 bm->setConfig(config, w, h);
26 bm->allocPixels();
27 bm->eraseColor(0);
28
29 SkCanvas canvas(*bm);
30 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
31 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
32 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
33 SkPaint paint;
34
35 SkUnitMapper* um = NULL;
36
37 um = new SkCosineMapper;
38// um = new SkDiscreteMapper(12);
39
40 SkAutoUnref au(um);
41
42 paint.setDither(true);
43 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
44 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
45 canvas.drawPaint(paint);
46}
47
48static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
49 SkShader::TileMode tmx, SkShader::TileMode tmy) {
50 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
51 paint->setShader(shader)->unref();
52 paint->setFilterBitmap(filter);
53}
54
55static const SkBitmap::Config gConfigs[] = {
56 SkBitmap::kARGB_8888_Config,
57 SkBitmap::kRGB_565_Config,
58 SkBitmap::kARGB_4444_Config
59};
60static const int gWidth = 32;
61static const int gHeight = 32;
62
63class TilingGM : public GM {
64 SkBlurDrawLooper fLooper;
65public:
66 TilingGM()
67 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
68 0x88000000) {
69 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
70 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
71 }
72 }
73
74 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
75
76protected:
77 SkString onShortName() {
78 return SkString("tilemodes");
79 }
80
81 SkISize onISize() { return make_isize(880, 560); }
82
83 void drawBG(SkCanvas* canvas) {
84 canvas->drawColor(SK_ColorWHITE);
85 }
86
87 virtual void onDraw(SkCanvas* canvas) {
88 this->drawBG(canvas);
89
90 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
91
92 static const char* gConfigNames[] = { "8888", "565", "4444" };
93
94 static const bool gFilters[] = { false, true };
95 static const char* gFilterNames[] = { "point", "bilinear" };
96
97 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
98 static const char* gModeNames[] = { "C", "R", "M" };
99
100 SkScalar y = SkIntToScalar(24);
101 SkScalar x = SkIntToScalar(10);
102
103 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
104 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
105 SkPaint p;
106 SkString str;
107 p.setAntiAlias(true);
108 p.setDither(true);
109 p.setLooper(&fLooper);
110 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
111
112 p.setTextAlign(SkPaint::kCenter_Align);
113 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
114
115 x += r.width() * 4 / 3;
116 }
117 }
118
119 y += SkIntToScalar(16);
120
121 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
122 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
123 x = SkIntToScalar(10);
124 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
125 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
126 SkPaint paint;
127 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
128 paint.setDither(true);
129
130 canvas->save();
131 canvas->translate(x, y);
132 canvas->drawRect(r, paint);
133 canvas->restore();
134
135 x += r.width() * 4 / 3;
136 }
137 }
138 {
139 SkPaint p;
140 SkString str;
141 p.setAntiAlias(true);
142 p.setLooper(&fLooper);
143 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
144 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
145 }
146
147 y += r.height() * 4 / 3;
148 }
149 }
150 }
151
152private:
153 typedef GM INHERITED;
154};
155
156//////////////////////////////////////////////////////////////////////////////
157
158static GM* MyFactory(void*) { return new TilingGM; }
159static GMRegistry reg(MyFactory);
160
161}
162