blob: 4005359acae3d09f0b4f83b4b86c90a7d5212453 [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() {
halcanary96fcdcc2015-08-27 07:41:13 -070028 if (nullptr == fBM) {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000029 fBM = new SkBitmap;
reed@google.comeb9a46c2014-01-25 16:46:20 +000030 fBM->allocN32Pixels(W, H);
reed@google.com4bc0a9d2012-03-07 21:47:41 +000031 fBM->eraseColor(SK_ColorWHITE);
32
33 const SkColor colors[] = {
34 SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
35 };
36
37 SkCanvas canvas(*fBM);
38 SkPaint paint;
39 paint.setAntiAlias(true);
reed@google.com6005aeb2012-03-14 20:15:47 +000040 paint.setStrokeWidth(SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
reed@google.com6005aeb2012-03-14 20:15:47 +000042#if 0
43 for (int y = -H*2; y < H; y += 50) {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000044 SkScalar yy = SkIntToScalar(y);
reed@google.com6005aeb2012-03-14 20:15:47 +000045 paint.setColor(colors[y/50 & 0x3]);
reed@google.com4bc0a9d2012-03-07 21:47:41 +000046 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
47 paint);
48 }
reed@google.com6005aeb2012-03-14 20:15:47 +000049#else
50 for (int x = -W; x < W; x += 60) {
51 paint.setColor(colors[x/60 & 0x3]);
52
53 SkScalar xx = SkIntToScalar(x);
54 canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
55 paint);
56 }
57#endif
reed@google.com4bc0a9d2012-03-07 21:47:41 +000058 }
59 return *fBM;
60 }
61
62public:
halcanary96fcdcc2015-08-27 07:41:13 -070063 GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(nullptr) {
reed@google.comb0b462b2012-03-08 22:13:39 +000064 fMode = mode;
65 fDoFilter = doFilter;
66 fDoRotate = doRotate;
67 }
reed@google.com4bc0a9d2012-03-07 21:47:41 +000068
halcanary385fe4d2015-08-26 13:07:48 -070069 virtual ~GiantBitmapGM() { delete fBM; }
reed@google.com4bc0a9d2012-03-07 21:47:41 +000070
71protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000072
mtklein36352bf2015-03-25 18:17:31 -070073 SkString onShortName() override {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000074 SkString str("giantbitmap_");
75 switch (fMode) {
76 case SkShader::kClamp_TileMode:
77 str.append("clamp");
78 break;
79 case SkShader::kRepeat_TileMode:
80 str.append("repeat");
81 break;
82 case SkShader::kMirror_TileMode:
83 str.append("mirror");
84 break;
85 default:
86 break;
87 }
reed@google.comb0b462b2012-03-08 22:13:39 +000088 str.append(fDoFilter ? "_bilerp" : "_point");
89 str.append(fDoRotate ? "_rotate" : "_scale");
reed@google.com4bc0a9d2012-03-07 21:47:41 +000090 return str;
91 }
92
mtklein36352bf2015-03-25 18:17:31 -070093 SkISize onISize() override { return SkISize::Make(640, 480); }
reed@google.com4bc0a9d2012-03-07 21:47:41 +000094
mtklein36352bf2015-03-25 18:17:31 -070095 void onDraw(SkCanvas* canvas) override {
reed@google.com4bc0a9d2012-03-07 21:47:41 +000096 SkPaint paint;
reed@google.com4bc0a9d2012-03-07 21:47:41 +000097
reed@google.comb0b462b2012-03-08 22:13:39 +000098 SkMatrix m;
99 if (fDoRotate) {
100// m.setRotate(SkIntToScalar(30), 0, 0);
101 m.setSkew(SK_Scalar1, 0, 0, 0);
reed@google.com6005aeb2012-03-14 20:15:47 +0000102// m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
reed@google.comb0b462b2012-03-08 22:13:39 +0000103 } else {
reed@google.com6005aeb2012-03-14 20:15:47 +0000104 SkScalar scale = 11*SK_Scalar1/12;
105 m.setScale(scale, scale);
reed@google.comb0b462b2012-03-08 22:13:39 +0000106 }
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000107 SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode, &m);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@google.comb0b462b2012-03-08 22:13:39 +0000109 paint.setShader(s)->unref();
reed93a12152015-03-16 10:08:34 -0700110 paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@google.comb0b462b2012-03-08 22:13:39 +0000111
112 canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
humper@google.com05af1af2013-01-07 16:47:43 +0000114// SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16);
reed@google.com6005aeb2012-03-14 20:15:47 +0000115// canvas->drawRect(r, paint); return;
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000116 canvas->drawPaint(paint);
117 }
118
119private:
120 typedef GM INHERITED;
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
scroggo96f16e82015-12-10 13:31:59 -0800125DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); )
126DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); )
127DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); )
128DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); )
129DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); )
130DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); )
reed@google.com4bc0a9d2012-03-07 21:47:41 +0000131
scroggo96f16e82015-12-10 13:31:59 -0800132DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); )
133DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); )
134DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); )
135DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); )
136DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); )
137DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); )