blob: 08bcb79f652902a0a0cd6778d112b6f9ffc31f60 [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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com1a2fec52009-06-22 02:17:34 +000029 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
35 SkUnitMapper* um = NULL;
reed@android.com1a2fec52009-06-22 02:17:34 +000036
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:
rmistry@google.comae933ce2012-08-23 18:19:56 +000066 TilingGM()
reed@android.com1a2fec52009-06-22 02:17:34 +000067 : 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)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com1a2fec52009-06-22 02:17:34 +000076protected:
77 SkString onShortName() {
78 return SkString("tilemodes");
79 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
81 SkISize onISize() { return make_isize(880, 560); }
82
reed@android.com1a2fec52009-06-22 02:17:34 +000083 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com1a2fec52009-06-22 02:17:34 +000085 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
86
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com1a2fec52009-06-22 02:17:34 +000089 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com1a2fec52009-06-22 02:17:34 +000092 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
94
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
97
98 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
99 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
100 SkPaint p;
101 SkString str;
102 p.setAntiAlias(true);
103 p.setDither(true);
104 p.setLooper(&fLooper);
105 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
106
107 p.setTextAlign(SkPaint::kCenter_Align);
108 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@android.com1a2fec52009-06-22 02:17:34 +0000110 x += r.width() * 4 / 3;
111 }
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com1a2fec52009-06-22 02:17:34 +0000114 y += SkIntToScalar(16);
115
116 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); 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++) {
121 SkPaint paint;
122 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
123 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com1a2fec52009-06-22 02:17:34 +0000125 canvas->save();
126 canvas->translate(x, y);
127 canvas->drawRect(r, paint);
128 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com1a2fec52009-06-22 02:17:34 +0000130 x += r.width() * 4 / 3;
131 }
132 }
133 {
134 SkPaint p;
135 SkString str;
136 p.setAntiAlias(true);
137 p.setLooper(&fLooper);
138 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
139 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
140 }
141
142 y += r.height() * 4 / 3;
143 }
144 }
145 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com1a2fec52009-06-22 02:17:34 +0000147private:
148 typedef GM INHERITED;
149};
150
151//////////////////////////////////////////////////////////////////////////////
152
153static GM* MyFactory(void*) { return new TilingGM; }
154static GMRegistry reg(MyFactory);
155
156}
157