blob: a3d60abc21752963011ad0cfd5501029ebc976a9 [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
robertphillips@google.comd9d53852012-05-02 13:55:06 +000015/**
16 * Stress test the samplers by rendering a textured glyph with a mask and
17 * an AA clip
18 */
19class SamplerStressGM : public GM {
20public:
21 SamplerStressGM()
22 : 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
scroggo@google.com5af9b202012-06-04 17:17:36 +000039 virtual uint32_t onGetFlags() const SK_OVERRIDE {
40 // Skip Pipe playback since we use a custom MaskFilter that will not be
41 // flattened correctly
42 return this->INHERITED::onGetFlags() | GM::kSkipPipe_Flag;
43 }
44
robertphillips@google.comd9d53852012-05-02 13:55:06 +000045 /**
46 * Create a red & green stripes on black texture
47 */
48 void createTexture() {
49 if (fTextureCreated) {
50 return;
51 }
52
53 static const int xSize = 16;
54 static const int ySize = 16;
55
56 fTexture.setConfig(SkBitmap::kARGB_8888_Config,
57 xSize,
58 ySize,
59 xSize*sizeof(SkColor));
60
61 fTexture.allocPixels();
62 fTexture.lockPixels();
63 SkPMColor* addr = fTexture.getAddr32(0, 0);
64
65 for (int y = 0; y < ySize; ++y) {
66 for (int x = 0; x < xSize; ++x) {
67 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
68
69 if ((y % 5) == 0) {
70 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
71 }
72 if ((x % 7) == 0) {
73 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
74 }
75 }
76 }
77
78 fTexture.unlockPixels();
79
80 fTextureCreated = true;
81 }
82
83 void createShader() {
84 if (NULL != fShader.get()) {
85 return;
86 }
87
88 createTexture();
89
90 fShader.reset(SkShader::CreateBitmapShader(fTexture,
91 SkShader::kRepeat_TileMode,
92 SkShader::kRepeat_TileMode));
93 }
94
95 void createMaskFilter() {
96 if (NULL != fMaskFilter.get()) {
97 return;
98 }
99
100 fMaskFilter.reset(SkNEW(SkStippleMaskFilter));
101 }
102
103 virtual void onDraw(SkCanvas* canvas) {
104
105 createShader();
106 createMaskFilter();
107
108 canvas->save();
109
110 // draw a letter "M" with a green & red striped texture and a
111 // stipple mask with a round rect soft clip
112 SkPaint paint;
113 paint.setAntiAlias(true);
114 paint.setTextSize(72);
115 paint.setShader(fShader.get());
116 paint.setMaskFilter(fMaskFilter.get());
117
118 SkRect temp;
119 temp.set(SkIntToScalar(115),
120 SkIntToScalar(75),
121 SkIntToScalar(144),
122 SkIntToScalar(110));
123
124 SkPath path;
125 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
126
127 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on
128
129 canvas->drawText("M", 1,
130 SkIntToScalar(100), SkIntToScalar(100),
131 paint);
132
133 canvas->restore();
134
135 // Now draw stroked versions of the "M" and the round rect so we can
136 // see what is going on
137 SkPaint paint2;
138 paint2.setColor(SK_ColorBLACK);
139 paint2.setAntiAlias(true);
140 paint2.setTextSize(72);
141 paint2.setStyle(SkPaint::kStroke_Style);
142 paint2.setStrokeWidth(1);
143 canvas->drawText("M", 1,
144 SkIntToScalar(100), SkIntToScalar(100),
145 paint2);
146
147 paint2.setColor(SK_ColorGRAY);
148
149 canvas->drawPath(path, paint2);
150 }
151
152private:
153 SkBitmap fTexture;
154 bool fTextureCreated;
155 SkAutoTUnref<SkShader> fShader;
156 SkAutoTUnref<SkMaskFilter> fMaskFilter;
157
158 typedef GM INHERITED;
159};
160
161//////////////////////////////////////////////////////////////////////////////
162
163static GM* MyFactory(void*) { return new SamplerStressGM; }
164static GMRegistry reg(MyFactory);
165
166}