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