blob: 30f3af1e9e2eb34f5ae9c14f82fbf8a8af2272bb [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
18#define W 2560
19#define H 1600
20
21class GiantBitmapGM : public skiagm::GM {
22 SkBitmap* fBM;
23 SkShader::TileMode fMode;
24
25 const SkBitmap& getBitmap() {
26 if (NULL == fBM) {
27 fBM = new SkBitmap;
28 fBM->setConfig(SkBitmap::kARGB_8888_Config, W, H);
29 fBM->allocPixels();
30 fBM->eraseColor(SK_ColorWHITE);
31
32 const SkColor colors[] = {
33 SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
34 };
35
36 SkCanvas canvas(*fBM);
37 SkPaint paint;
38 paint.setAntiAlias(true);
39 paint.setStrokeWidth(SkIntToScalar(30));
40 for (int y = -H; y < H; y += 80) {
41 SkScalar yy = SkIntToScalar(y);
42 paint.setColor(colors[y/80 & 0x3]);
43 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
44 paint);
45 }
46 }
47 return *fBM;
48 }
49
50public:
51 GiantBitmapGM(SkShader::TileMode mode) : fBM(NULL), fMode(mode) {}
52
53 virtual ~GiantBitmapGM() {
54 SkDELETE(fBM);
55 }
56
57protected:
58
59 virtual SkString onShortName() {
60 SkString str("giantbitmap_");
61 switch (fMode) {
62 case SkShader::kClamp_TileMode:
63 str.append("clamp");
64 break;
65 case SkShader::kRepeat_TileMode:
66 str.append("repeat");
67 break;
68 case SkShader::kMirror_TileMode:
69 str.append("mirror");
70 break;
71 default:
72 break;
73 }
74 return str;
75 }
76
77 virtual SkISize onISize() { return SkISize::Make(640, 480); }
78
79 virtual void onDraw(SkCanvas* canvas) {
80 SkPaint paint;
81 SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode);
82 SkMatrix m;
83 m.setScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
84 s->setLocalMatrix(m);
85 paint.setShader(s)->unref();
86
87 canvas->drawPaint(paint);
88 }
89
90private:
91 typedef GM INHERITED;
92};
93
94///////////////////////////////////////////////////////////////////////////////
95
96static skiagm::GM* G0(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode); }
97static skiagm::GM* G1(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode); }
98static skiagm::GM* G2(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode); }
99
100static skiagm::GMRegistry reg0(G0);
101static skiagm::GMRegistry reg1(G1);
102static skiagm::GMRegistry reg2(G2);
103