Debian Codegraph @

gm/samplerstress.cpp

raw
1 /*
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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlurTypes.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkMaskFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkRRect.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/core/SkTypeface.h"
26 #include "tools/ToolUtils.h"
27 #include "tools/fonts/FontToolUtils.h"
28
29 namespace skiagm {
30
31 /**
32 * Stress test the GPU samplers by rendering a textured glyph with a mask and
33 * an AA clip
34 */
35 class SamplerStressGM : public GM {
36 public:
37 SamplerStressGM()
38 : fTextureCreated(false)
39 , fMaskFilter(nullptr) {
40 }
41
42 protected:
43 SkString getName() const override { return SkString("gpusamplerstress"); }
44
45 SkISize getISize() override { return SkISize::Make(640, 480); }
46
47 /**
48 * Create a red & green stripes on black texture
49 */
50 void createTexture() {
51 if (fTextureCreated) {
52 return;
53 }
54
55 constexpr int xSize = 16;
56 constexpr int ySize = 16;
57
58 fTexture.allocN32Pixels(xSize, ySize);
59 SkPMColor* addr = fTexture.getAddr32(0, 0);
60
61 for (int y = 0; y < ySize; ++y) {
62 for (int x = 0; x < xSize; ++x) {
63 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
64
65 if ((y % 5) == 0) {
66 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
67 }
68 if ((x % 7) == 0) {
69 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
70 }
71 }
72 }
73
74 fTextureCreated = true;
75 }
76
77 void createShader() {
78 if (fShader) {
79 return;
80 }
81
82 createTexture();
83
84 fShader = fTexture.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
85 SkSamplingOptions());
86 }
87
88 void createMaskFilter() {
89 if (fMaskFilter) {
90 return;
91 }
92
93 const SkScalar sigma = 1;
94 fMaskFilter = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma);
95 }
96
97 void onDraw(SkCanvas* canvas) override {
98 createShader();
99 createMaskFilter();
100
101 canvas->save();
102
103 // draw a letter "M" with a green & red striped texture and a
104 // stipple mask with a round rect soft clip
105 SkPaint paint;
106 paint.setAntiAlias(true);
107 paint.setShader(fShader);
108 paint.setMaskFilter(fMaskFilter);
109 SkFont font(ToolUtils::DefaultPortableTypeface(), 72);
110
111 SkRect temp;
112 temp.setLTRB(115, 75, 144, 110);
113
114 SkPath path = SkPath::RRect(SkRRect::MakeRectXY(temp, 5, 5));
115
116 canvas->clipPath(path, true); // AA is on
117
118 canvas->drawString("M", 100.0f, 100.0f, font, 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);
127 paint2.setStyle(SkPaint::kStroke_Style);
128 paint2.setStrokeWidth(1);
129 canvas->drawString("M", 100.0f, 100.0f, font, paint2);
130
131 paint2.setColor(SK_ColorGRAY);
132
133 canvas->drawPath(path, paint2);
134 }
135
136 private:
137 SkBitmap fTexture;
138 bool fTextureCreated;
139 sk_sp<SkShader> fShader;
140 sk_sp<SkMaskFilter> fMaskFilter;
141
142 using INHERITED = GM;
143 };
144
145 //////////////////////////////////////////////////////////////////////////////
146
147 DEF_GM( return new SamplerStressGM; )
148
149 } // namespace skiagm