blob: 38b94bf03a81c665d0a67c097284e2d365f4c4a3 [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());
caryclarkf597c422015-07-28 10:37:53 -0700104 sk_tool_utils::set_portable_typeface(&paint);
reedd7127e72014-08-12 06:53:09 -0700105
106 SkRect temp;
107 temp.set(SkIntToScalar(115),
108 SkIntToScalar(75),
109 SkIntToScalar(144),
110 SkIntToScalar(110));
111
112 SkPath path;
113 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
114
115 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on
116
117 canvas->drawText("M", 1,
118 SkIntToScalar(100), SkIntToScalar(100),
119 paint);
120
121 canvas->restore();
122
123 // Now draw stroked versions of the "M" and the round rect so we can
124 // see what is going on
125 SkPaint paint2;
126 paint2.setColor(SK_ColorBLACK);
127 paint2.setAntiAlias(true);
128 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
caryclarkf597c422015-07-28 10:37:53 -0700135 paint2.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
reedd7127e72014-08-12 06:53:09 -0700136
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}