blob: ab75dc7306b8c059e9ff3726ac1e6730eadb082d [file] [log] [blame]
reed@google.com4bc0a9d2012-03-07 21:47:41 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
13/*
14 * Want to ensure that our bitmap sampler (in bitmap shader) keeps plenty of
15 * precision when scaling very large images (where the dx might get very small.
16 */
17
reed@google.com6005aeb2012-03-14 20:15:47 +000018#define W 257
19#define H 161
reed@google.com4bc0a9d2012-03-07 21:47:41 +000020
21class GiantBitmapGM : public skiagm::GM {
22 SkBitmap* fBM;
23 SkShader::TileMode fMode;
reed@google.comb0b462b2012-03-08 22:13:39 +000024 bool fDoFilter;
25 bool fDoRotate;
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@google.com4bc0a9d2012-03-07 21:47:41 +000027 const SkBitmap& getBitmap() {
28 if (NULL == fBM) {
29 fBM = new SkBitmap;
30 fBM->setConfig(SkBitmap::kARGB_8888_Config, W, H);
31 fBM->allocPixels();
32 fBM->eraseColor(SK_ColorWHITE);
33
34 const SkColor colors[] = {
35 SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
36 };
37
38 SkCanvas canvas(*fBM);
39 SkPaint paint;
40 paint.setAntiAlias(true);
reed@google.com6005aeb2012-03-14 20:15:47 +000041 paint.setStrokeWidth(SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@google.com6005aeb2012-03-14 20:15:47 +000043#if 0
44 for (int y = -H*2; y < H; y += 50) {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000045 SkScalar yy = SkIntToScalar(y);
reed@google.com6005aeb2012-03-14 20:15:47 +000046 paint.setColor(colors[y/50 & 0x3]);
reed@google.com4bc0a9d2012-03-07 21:47:41 +000047 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
48 paint);
49 }
reed@google.com6005aeb2012-03-14 20:15:47 +000050#else
51 for (int x = -W; x < W; x += 60) {
52 paint.setColor(colors[x/60 & 0x3]);
53
54 SkScalar xx = SkIntToScalar(x);
55 canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
56 paint);
57 }
58#endif
reed@google.com4bc0a9d2012-03-07 21:47:41 +000059 }
60 return *fBM;
61 }
62
63public:
reed@google.comb0b462b2012-03-08 22:13:39 +000064 GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(NULL) {
65 fMode = mode;
66 fDoFilter = doFilter;
67 fDoRotate = doRotate;
68 }
reed@google.com4bc0a9d2012-03-07 21:47:41 +000069
70 virtual ~GiantBitmapGM() {
71 SkDELETE(fBM);
72 }
73
74protected:
reed@google.com4bc0a9d2012-03-07 21:47:41 +000075 virtual SkString onShortName() {
76 SkString str("giantbitmap_");
77 switch (fMode) {
78 case SkShader::kClamp_TileMode:
79 str.append("clamp");
80 break;
81 case SkShader::kRepeat_TileMode:
82 str.append("repeat");
83 break;
84 case SkShader::kMirror_TileMode:
85 str.append("mirror");
86 break;
87 default:
88 break;
89 }
reed@google.comb0b462b2012-03-08 22:13:39 +000090 str.append(fDoFilter ? "_bilerp" : "_point");
91 str.append(fDoRotate ? "_rotate" : "_scale");
reed@google.com4bc0a9d2012-03-07 21:47:41 +000092 return str;
93 }
94
95 virtual SkISize onISize() { return SkISize::Make(640, 480); }
96
97 virtual void onDraw(SkCanvas* canvas) {
98 SkPaint paint;
99 SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode);
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000100
reed@google.comb0b462b2012-03-08 22:13:39 +0000101 SkMatrix m;
102 if (fDoRotate) {
103// m.setRotate(SkIntToScalar(30), 0, 0);
104 m.setSkew(SK_Scalar1, 0, 0, 0);
reed@google.com6005aeb2012-03-14 20:15:47 +0000105// m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
reed@google.comb0b462b2012-03-08 22:13:39 +0000106 } else {
reed@google.com6005aeb2012-03-14 20:15:47 +0000107 SkScalar scale = 11*SK_Scalar1/12;
108 m.setScale(scale, scale);
reed@google.comb0b462b2012-03-08 22:13:39 +0000109 }
110 s->setLocalMatrix(m);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed@google.comb0b462b2012-03-08 22:13:39 +0000112 paint.setShader(s)->unref();
reed@google.com44699382013-10-31 17:28:30 +0000113 paint.setFilterLevel(fDoFilter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.comb0b462b2012-03-08 22:13:39 +0000114
115 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
humper@google.com05af1af2013-01-07 16:47:43 +0000117// SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16);
reed@google.com6005aeb2012-03-14 20:15:47 +0000118// canvas->drawRect(r, paint); return;
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000119 canvas->drawPaint(paint);
120 }
121
122private:
123 typedef GM INHERITED;
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
reed@google.comb0b462b2012-03-08 22:13:39 +0000128static skiagm::GM* G000(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); }
129static skiagm::GM* G100(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); }
130static skiagm::GM* G200(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); }
131static skiagm::GM* G010(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); }
132static skiagm::GM* G110(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); }
133static skiagm::GM* G210(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); }
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000134
reed@google.comb0b462b2012-03-08 22:13:39 +0000135static skiagm::GM* G001(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); }
136static skiagm::GM* G101(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); }
137static skiagm::GM* G201(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); }
138static skiagm::GM* G011(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); }
139static skiagm::GM* G111(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); }
140static skiagm::GM* G211(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); }
141
142static skiagm::GMRegistry reg000(G000);
143static skiagm::GMRegistry reg100(G100);
144static skiagm::GMRegistry reg200(G200);
145static skiagm::GMRegistry reg010(G010);
146static skiagm::GMRegistry reg110(G110);
147static skiagm::GMRegistry reg210(G210);
148
149static skiagm::GMRegistry reg001(G001);
150static skiagm::GMRegistry reg101(G101);
151static skiagm::GMRegistry reg201(G201);
152static skiagm::GMRegistry reg011(G011);
153static skiagm::GMRegistry reg111(G111);
154static skiagm::GMRegistry reg211(G211);