• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "sk_tool_utils.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkCanvas.h"
12 #include "SkPath.h"
13 #include "SkShader.h"
14 
15 namespace skiagm {
16 
17 /**
18  * Stress test the GPU samplers by rendering a textured glyph with a mask and
19  * an AA clip
20  */
21 class SamplerStressGM : public GM {
22 public:
SamplerStressGM()23     SamplerStressGM()
24     : fTextureCreated(false)
25     , fMaskFilter(nullptr) {
26     }
27 
28 protected:
29 
onShortName()30     SkString onShortName() override {
31         return SkString("gpusamplerstress");
32     }
33 
onISize()34     SkISize onISize() override {
35         return SkISize::Make(640, 480);
36     }
37 
38     /**
39      * Create a red & green stripes on black texture
40      */
createTexture()41     void createTexture() {
42         if (fTextureCreated) {
43             return;
44         }
45 
46         constexpr int xSize = 16;
47         constexpr int ySize = 16;
48 
49         fTexture.allocN32Pixels(xSize, ySize);
50         SkPMColor* addr = fTexture.getAddr32(0, 0);
51 
52         for (int y = 0; y < ySize; ++y) {
53             for (int x = 0; x < xSize; ++x) {
54                 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
55 
56                 if ((y % 5) == 0) {
57                     addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
58                 }
59                 if ((x % 7) == 0) {
60                     addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
61                 }
62             }
63         }
64 
65         fTextureCreated = true;
66     }
67 
createShader()68     void createShader() {
69         if (fShader) {
70             return;
71         }
72 
73         createTexture();
74 
75         fShader = SkShader::MakeBitmapShader(fTexture, SkShader::kRepeat_TileMode,
76                                              SkShader::kRepeat_TileMode);
77     }
78 
createMaskFilter()79     void createMaskFilter() {
80         if (fMaskFilter) {
81             return;
82         }
83 
84         const SkScalar sigma = 1;
85         fMaskFilter = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma);
86     }
87 
onDraw(SkCanvas * canvas)88     void onDraw(SkCanvas* canvas) override {
89         createShader();
90         createMaskFilter();
91 
92         canvas->save();
93 
94         // draw a letter "M" with a green & red striped texture and a
95         // stipple mask with a round rect soft clip
96         SkPaint paint;
97         paint.setAntiAlias(true);
98         paint.setTextSize(72);
99         paint.setShader(fShader);
100         paint.setMaskFilter(fMaskFilter);
101         sk_tool_utils::set_portable_typeface(&paint);
102 
103         SkRect temp;
104         temp.set(SkIntToScalar(115),
105                  SkIntToScalar(75),
106                  SkIntToScalar(144),
107                  SkIntToScalar(110));
108 
109         SkPath path;
110         path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
111 
112         canvas->clipPath(path, true); // AA is on
113 
114         canvas->drawString("M",
115                          SkIntToScalar(100), SkIntToScalar(100),
116                          paint);
117 
118         canvas->restore();
119 
120         // Now draw stroked versions of the "M" and the round rect so we can
121         // see what is going on
122         SkPaint paint2;
123         paint2.setColor(SK_ColorBLACK);
124         paint2.setAntiAlias(true);
125         paint2.setTextSize(72);
126         paint2.setStyle(SkPaint::kStroke_Style);
127         paint2.setStrokeWidth(1);
128         sk_tool_utils::set_portable_typeface(&paint2);
129         canvas->drawString("M",
130                          SkIntToScalar(100), SkIntToScalar(100),
131                          paint2);
132 
133         paint2.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
134 
135         canvas->drawPath(path, paint2);
136     }
137 
138 private:
139     SkBitmap        fTexture;
140     bool            fTextureCreated;
141     sk_sp<SkShader> fShader;
142     sk_sp<SkMaskFilter> fMaskFilter;
143 
144     typedef GM INHERITED;
145 };
146 
147 //////////////////////////////////////////////////////////////////////////////
148 
MyFactory(void *)149 static GM* MyFactory(void*) { return new SamplerStressGM; }
150 static GMRegistry reg(MyFactory);
151 
152 }
153