blob: 4303118d076c19dc57219ace48021e772f71af58 [file] [log] [blame]
robertphillips@google.comd9d53852012-05-02 13:55:06 +00001/*
2 * Copyright 2012 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 "SkShader.h"
robertphillips@google.com941ee932012-06-05 12:55:05 +000011#include "SkStippleMaskFilter.h"
robertphillips@google.comd9d53852012-05-02 13:55:06 +000012
13namespace skiagm {
14
rmistry@google.comae933ce2012-08-23 18:19:56 +000015/**
robertphillips@google.comd9d53852012-05-02 13:55:06 +000016 * Stress test the samplers by rendering a textured glyph with a mask and
17 * an AA clip
18 */
19class SamplerStressGM : public GM {
20public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000021 SamplerStressGM()
robertphillips@google.comd9d53852012-05-02 13:55:06 +000022 : fTextureCreated(false)
23 , fShader(NULL)
24 , fMaskFilter(NULL) {
25 }
26
27 virtual ~SamplerStressGM() {
28 }
29
30protected:
31 virtual SkString onShortName() {
32 return SkString("samplerstress");
33 }
34
35 virtual SkISize onISize() {
36 return make_isize(640, 480);
37 }
38
robertphillips@google.comd9d53852012-05-02 13:55:06 +000039 /**
40 * Create a red & green stripes on black texture
41 */
42 void createTexture() {
43 if (fTextureCreated) {
44 return;
45 }
46
47 static const int xSize = 16;
48 static const int ySize = 16;
49
reed@google.comeb9a46c2014-01-25 16:46:20 +000050 fTexture.allocN32Pixels(xSize, ySize);
robertphillips@google.comd9d53852012-05-02 13:55:06 +000051 SkPMColor* addr = fTexture.getAddr32(0, 0);
52
53 for (int y = 0; y < ySize; ++y) {
54 for (int x = 0; x < xSize; ++x) {
55 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
56
57 if ((y % 5) == 0) {
58 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
59 }
60 if ((x % 7) == 0) {
61 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
62 }
63 }
64 }
65
robertphillips@google.comd9d53852012-05-02 13:55:06 +000066 fTextureCreated = true;
67 }
68
69 void createShader() {
70 if (NULL != fShader.get()) {
71 return;
72 }
73
74 createTexture();
75
76 fShader.reset(SkShader::CreateBitmapShader(fTexture,
77 SkShader::kRepeat_TileMode,
78 SkShader::kRepeat_TileMode));
79 }
80
81 void createMaskFilter() {
82 if (NULL != fMaskFilter.get()) {
83 return;
84 }
85
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +000086 fMaskFilter.reset(SkStippleMaskFilter::Create());
robertphillips@google.comd9d53852012-05-02 13:55:06 +000087 }
88
89 virtual void onDraw(SkCanvas* canvas) {
90
91 createShader();
92 createMaskFilter();
93
94 canvas->save();
95
96 // draw a letter "M" with a green & red striped texture and a
97 // stipple mask with a round rect soft clip
98 SkPaint paint;
99 paint.setAntiAlias(true);
100 paint.setTextSize(72);
101 paint.setShader(fShader.get());
102 paint.setMaskFilter(fMaskFilter.get());
103
104 SkRect temp;
105 temp.set(SkIntToScalar(115),
106 SkIntToScalar(75),
107 SkIntToScalar(144),
108 SkIntToScalar(110));
109
110 SkPath path;
111 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
112
113 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on
114
115 canvas->drawText("M", 1,
116 SkIntToScalar(100), SkIntToScalar(100),
117 paint);
118
119 canvas->restore();
120
121 // Now draw stroked versions of the "M" and the round rect so we can
122 // see what is going on
123 SkPaint paint2;
124 paint2.setColor(SK_ColorBLACK);
125 paint2.setAntiAlias(true);
126 paint2.setTextSize(72);
127 paint2.setStyle(SkPaint::kStroke_Style);
128 paint2.setStrokeWidth(1);
129 canvas->drawText("M", 1,
130 SkIntToScalar(100), SkIntToScalar(100),
131 paint2);
132
133 paint2.setColor(SK_ColorGRAY);
134
135 canvas->drawPath(path, paint2);
136 }
137
138private:
139 SkBitmap fTexture;
140 bool fTextureCreated;
141 SkAutoTUnref<SkShader> fShader;
142 SkAutoTUnref<SkMaskFilter> fMaskFilter;
143
144 typedef GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149static GM* MyFactory(void*) { return new SamplerStressGM; }
150static GMRegistry reg(MyFactory);
151
152}