blob: 4329f12470bb588faa40ec1fa538238e322b9b3d [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
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
50 fTexture.setConfig(SkBitmap::kARGB_8888_Config,
51 xSize,
52 ySize,
53 xSize*sizeof(SkColor));
54
55 fTexture.allocPixels();
56 fTexture.lockPixels();
57 SkPMColor* addr = fTexture.getAddr32(0, 0);
58
59 for (int y = 0; y < ySize; ++y) {
60 for (int x = 0; x < xSize; ++x) {
61 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
62
63 if ((y % 5) == 0) {
64 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
65 }
66 if ((x % 7) == 0) {
67 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
68 }
69 }
70 }
71
72 fTexture.unlockPixels();
73
74 fTextureCreated = true;
75 }
76
77 void createShader() {
78 if (NULL != fShader.get()) {
79 return;
80 }
81
82 createTexture();
83
84 fShader.reset(SkShader::CreateBitmapShader(fTexture,
85 SkShader::kRepeat_TileMode,
86 SkShader::kRepeat_TileMode));
87 }
88
89 void createMaskFilter() {
90 if (NULL != fMaskFilter.get()) {
91 return;
92 }
93
94 fMaskFilter.reset(SkNEW(SkStippleMaskFilter));
95 }
96
97 virtual void onDraw(SkCanvas* canvas) {
98
99 createShader();
100 createMaskFilter();
101
102 canvas->save();
103
104 // draw a letter "M" with a green & red striped texture and a
105 // stipple mask with a round rect soft clip
106 SkPaint paint;
107 paint.setAntiAlias(true);
108 paint.setTextSize(72);
109 paint.setShader(fShader.get());
110 paint.setMaskFilter(fMaskFilter.get());
111
112 SkRect temp;
113 temp.set(SkIntToScalar(115),
114 SkIntToScalar(75),
115 SkIntToScalar(144),
116 SkIntToScalar(110));
117
118 SkPath path;
119 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
120
121 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on
122
123 canvas->drawText("M", 1,
124 SkIntToScalar(100), SkIntToScalar(100),
125 paint);
126
127 canvas->restore();
128
129 // Now draw stroked versions of the "M" and the round rect so we can
130 // see what is going on
131 SkPaint paint2;
132 paint2.setColor(SK_ColorBLACK);
133 paint2.setAntiAlias(true);
134 paint2.setTextSize(72);
135 paint2.setStyle(SkPaint::kStroke_Style);
136 paint2.setStrokeWidth(1);
137 canvas->drawText("M", 1,
138 SkIntToScalar(100), SkIntToScalar(100),
139 paint2);
140
141 paint2.setColor(SK_ColorGRAY);
142
143 canvas->drawPath(path, paint2);
144 }
145
146private:
147 SkBitmap fTexture;
148 bool fTextureCreated;
149 SkAutoTUnref<SkShader> fShader;
150 SkAutoTUnref<SkMaskFilter> fMaskFilter;
151
152 typedef GM INHERITED;
153};
154
155//////////////////////////////////////////////////////////////////////////////
156
157static GM* MyFactory(void*) { return new SamplerStressGM; }
158static GMRegistry reg(MyFactory);
159
160}