blob: ec072708cbcf2695a05758b5018e18b4adaae994 [file] [log] [blame]
reedd7127e72014-08-12 06:53:09 -07001/*
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"
11#include "SkBlurMaskFilter.h"
12
13namespace skiagm {
14
15/**
16 * Stress test the GPU 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:
reedd7127e72014-08-12 06:53:09 -070031
mtklein36352bf2015-03-25 18:17:31 -070032 SkString onShortName() override {
reedd7127e72014-08-12 06:53:09 -070033 return SkString("gpusamplerstress");
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 SkISize onISize() override {
reedd7127e72014-08-12 06:53:09 -070037 return SkISize::Make(640, 480);
38 }
39
40 /**
41 * Create a red & green stripes on black texture
42 */
43 void createTexture() {
44 if (fTextureCreated) {
45 return;
46 }
47
48 static const int xSize = 16;
49 static const int ySize = 16;
50
51 fTexture.allocN32Pixels(xSize, ySize);
52 SkPMColor* addr = fTexture.getAddr32(0, 0);
53
54 for (int y = 0; y < ySize; ++y) {
55 for (int x = 0; x < xSize; ++x) {
56 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
57
58 if ((y % 5) == 0) {
59 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
60 }
61 if ((x % 7) == 0) {
62 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
63 }
64 }
65 }
66
67 fTextureCreated = true;
68 }
69
70 void createShader() {
bsalomon49f085d2014-09-05 13:34:00 -070071 if (fShader.get()) {
reedd7127e72014-08-12 06:53:09 -070072 return;
73 }
74
75 createTexture();
76
77 fShader.reset(SkShader::CreateBitmapShader(fTexture,
78 SkShader::kRepeat_TileMode,
79 SkShader::kRepeat_TileMode));
80 }
81
82 void createMaskFilter() {
bsalomon49f085d2014-09-05 13:34:00 -070083 if (fMaskFilter.get()) {
reedd7127e72014-08-12 06:53:09 -070084 return;
85 }
86
87 const SkScalar sigma = 1;
88 fMaskFilter.reset(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, sigma));
89 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 void onDraw(SkCanvas* canvas) override {
reedd7127e72014-08-12 06:53:09 -070092 createShader();
93 createMaskFilter();
94
95 canvas->save();
96
97 // draw a letter "M" with a green & red striped texture and a
98 // stipple mask with a round rect soft clip
99 SkPaint paint;
100 paint.setAntiAlias(true);
101 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);
127 paint2.setTextSize(72);
128 paint2.setStyle(SkPaint::kStroke_Style);
129 paint2.setStrokeWidth(1);
130 canvas->drawText("M", 1,
131 SkIntToScalar(100), SkIntToScalar(100),
132 paint2);
133
134 paint2.setColor(SK_ColorGRAY);
135
136 canvas->drawPath(path, paint2);
137 }
138
139private:
140 SkBitmap fTexture;
141 bool fTextureCreated;
142 SkAutoTUnref<SkShader> fShader;
143 SkAutoTUnref<SkMaskFilter> fMaskFilter;
144
145 typedef GM INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
150static GM* MyFactory(void*) { return new SamplerStressGM; }
151static GMRegistry reg(MyFactory);
152
153}