blob: 9e0843e0717bd0021a9e8d860714ebeea80405b7 [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() {
tfarinaf5393182014-06-09 23:59:03 -070036 return SkISize::Make(640, 480);
robertphillips@google.comd9d53852012-05-02 13:55:06 +000037 }
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);
Cary Clark992c7b02014-07-31 08:58:44 -0400100 sk_tool_utils::set_portable_typeface(&paint);
robertphillips@google.comd9d53852012-05-02 13:55:06 +0000101 paint.setTextSize(72);
102 paint.setShader(fShader.get());
103 paint.setMaskFilter(fMaskFilter.get());
104
105 SkRect temp;
106 temp.set(SkIntToScalar(115),
107 SkIntToScalar(75),
108 SkIntToScalar(144),
109 SkIntToScalar(110));
110
111 SkPath path;
112 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
113
114 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on
115
116 canvas->drawText("M", 1,
117 SkIntToScalar(100), SkIntToScalar(100),
118 paint);
119
120 canvas->restore();
121
122 // Now draw stroked versions of the "M" and the round rect so we can
123 // see what is going on
124 SkPaint paint2;
125 paint2.setColor(SK_ColorBLACK);
126 paint2.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400127 sk_tool_utils::set_portable_typeface(&paint2);
robertphillips@google.comd9d53852012-05-02 13:55:06 +0000128 paint2.setTextSize(72);
129 paint2.setStyle(SkPaint::kStroke_Style);
130 paint2.setStrokeWidth(1);
131 canvas->drawText("M", 1,
132 SkIntToScalar(100), SkIntToScalar(100),
133 paint2);
134
135 paint2.setColor(SK_ColorGRAY);
136
137 canvas->drawPath(path, paint2);
138 }
139
140private:
141 SkBitmap fTexture;
142 bool fTextureCreated;
143 SkAutoTUnref<SkShader> fShader;
144 SkAutoTUnref<SkMaskFilter> fMaskFilter;
145
146 typedef GM INHERITED;
147};
148
149//////////////////////////////////////////////////////////////////////////////
150
151static GM* MyFactory(void*) { return new SamplerStressGM; }
152static GMRegistry reg(MyFactory);
153
154}