blob: 239466275c31b8f6f7189be0726391731c761e8f [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.comb0b462b2012-03-08 22:13:39 +000018#define W 256
19#define H 160
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;
reed@google.com4bc0a9d2012-03-07 21:47:41 +000026
27 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);
41 paint.setStrokeWidth(SkIntToScalar(30));
reed@google.comb0b462b2012-03-08 22:13:39 +000042 for (int y = -H*2; y < H; y += 80) {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000043 SkScalar yy = SkIntToScalar(y);
44 paint.setColor(colors[y/80 & 0x3]);
45 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
46 paint);
47 }
48 }
49 return *fBM;
50 }
51
52public:
reed@google.comb0b462b2012-03-08 22:13:39 +000053 GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(NULL) {
54 fMode = mode;
55 fDoFilter = doFilter;
56 fDoRotate = doRotate;
57 }
reed@google.com4bc0a9d2012-03-07 21:47:41 +000058
59 virtual ~GiantBitmapGM() {
60 SkDELETE(fBM);
61 }
62
63protected:
reed@google.com584493e2012-03-08 14:40:07 +000064 // work-around for bug http://code.google.com/p/skia/issues/detail?id=520
65 //
66 virtual uint32_t onGetFlags() const { return kSkipPDF_Flag; }
67
reed@google.com4bc0a9d2012-03-07 21:47:41 +000068 virtual SkString onShortName() {
69 SkString str("giantbitmap_");
70 switch (fMode) {
71 case SkShader::kClamp_TileMode:
72 str.append("clamp");
73 break;
74 case SkShader::kRepeat_TileMode:
75 str.append("repeat");
76 break;
77 case SkShader::kMirror_TileMode:
78 str.append("mirror");
79 break;
80 default:
81 break;
82 }
reed@google.comb0b462b2012-03-08 22:13:39 +000083 str.append(fDoFilter ? "_bilerp" : "_point");
84 str.append(fDoRotate ? "_rotate" : "_scale");
reed@google.com4bc0a9d2012-03-07 21:47:41 +000085 return str;
86 }
87
88 virtual SkISize onISize() { return SkISize::Make(640, 480); }
89
90 virtual void onDraw(SkCanvas* canvas) {
91 SkPaint paint;
92 SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode);
reed@google.com4bc0a9d2012-03-07 21:47:41 +000093
reed@google.comb0b462b2012-03-08 22:13:39 +000094 SkMatrix m;
95 if (fDoRotate) {
96// m.setRotate(SkIntToScalar(30), 0, 0);
97 m.setSkew(SK_Scalar1, 0, 0, 0);
98 m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
99 } else {
100 m.setScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
101 }
102 s->setLocalMatrix(m);
103
104 paint.setShader(s)->unref();
105 paint.setFilterBitmap(fDoFilter);
106
107 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000108 canvas->drawPaint(paint);
109 }
110
111private:
112 typedef GM INHERITED;
113};
114
115///////////////////////////////////////////////////////////////////////////////
116
reed@google.comb0b462b2012-03-08 22:13:39 +0000117static skiagm::GM* G000(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); }
118static skiagm::GM* G100(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); }
119static skiagm::GM* G200(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); }
120static skiagm::GM* G010(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); }
121static skiagm::GM* G110(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); }
122static skiagm::GM* G210(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); }
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000123
reed@google.comb0b462b2012-03-08 22:13:39 +0000124static skiagm::GM* G001(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); }
125static skiagm::GM* G101(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); }
126static skiagm::GM* G201(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); }
127static skiagm::GM* G011(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); }
128static skiagm::GM* G111(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); }
129static skiagm::GM* G211(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); }
130
131static skiagm::GMRegistry reg000(G000);
132static skiagm::GMRegistry reg100(G100);
133static skiagm::GMRegistry reg200(G200);
134static skiagm::GMRegistry reg010(G010);
135static skiagm::GMRegistry reg110(G110);
136static skiagm::GMRegistry reg210(G210);
137
138static skiagm::GMRegistry reg001(G001);
139static skiagm::GMRegistry reg101(G101);
140static skiagm::GMRegistry reg201(G201);
141static skiagm::GMRegistry reg011(G011);
142static skiagm::GMRegistry reg111(G111);
143static skiagm::GMRegistry reg211(G211);
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000144