blob: f3287437eeba779a7a31d50954e993d50bdc665e [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reedfb8c1fc2015-08-04 18:44:56 -070010#include "SkBlurMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkCanvas.h"
12#include "SkPath.h"
13#include "SkShader.h"
reedd7127e72014-08-12 06:53:09 -070014
15namespace skiagm {
16
17/**
18 * Stress test the GPU samplers by rendering a textured glyph with a mask and
19 * an AA clip
20 */
21class SamplerStressGM : public GM {
22public:
23 SamplerStressGM()
24 : fTextureCreated(false)
halcanary96fcdcc2015-08-27 07:41:13 -070025 , fMaskFilter(nullptr) {
reedd7127e72014-08-12 06:53:09 -070026 }
27
reedd7127e72014-08-12 06:53:09 -070028protected:
reedd7127e72014-08-12 06:53:09 -070029
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
reedd7127e72014-08-12 06:53:09 -070031 return SkString("gpusamplerstress");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
reedd7127e72014-08-12 06:53:09 -070035 return SkISize::Make(640, 480);
36 }
37
38 /**
39 * Create a red & green stripes on black texture
40 */
41 void createTexture() {
42 if (fTextureCreated) {
43 return;
44 }
45
mtkleindbfd7ab2016-09-01 11:24:54 -070046 constexpr int xSize = 16;
47 constexpr int ySize = 16;
reedd7127e72014-08-12 06:53:09 -070048
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
68 void createShader() {
reed1a9b9642016-03-13 14:13:58 -070069 if (fShader) {
reedd7127e72014-08-12 06:53:09 -070070 return;
71 }
72
73 createTexture();
74
reed1a9b9642016-03-13 14:13:58 -070075 fShader = SkShader::MakeBitmapShader(fTexture, SkShader::kRepeat_TileMode,
76 SkShader::kRepeat_TileMode);
reedd7127e72014-08-12 06:53:09 -070077 }
78
79 void createMaskFilter() {
reedefdfd512016-04-04 10:02:58 -070080 if (fMaskFilter) {
reedd7127e72014-08-12 06:53:09 -070081 return;
82 }
83
84 const SkScalar sigma = 1;
reedefdfd512016-04-04 10:02:58 -070085 fMaskFilter = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma);
reedd7127e72014-08-12 06:53:09 -070086 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(SkCanvas* canvas) override {
reedd7127e72014-08-12 06:53:09 -070089 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);
reedfe630452016-03-25 09:08:00 -070099 paint.setShader(fShader);
reedefdfd512016-04-04 10:02:58 -0700100 paint.setMaskFilter(fMaskFilter);
caryclarkf597c422015-07-28 10:37:53 -0700101 sk_tool_utils::set_portable_typeface(&paint);
reedd7127e72014-08-12 06:53:09 -0700102
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
reed66998382016-09-21 11:15:07 -0700112 canvas->clipPath(path, true); // AA is on
reedd7127e72014-08-12 06:53:09 -0700113
Cary Clark2a475ea2017-04-28 15:35:12 -0400114 canvas->drawString("M",
reedd7127e72014-08-12 06:53:09 -0700115 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);
caryclark66fb63f2015-07-28 11:22:48 -0700128 sk_tool_utils::set_portable_typeface(&paint2);
Cary Clark2a475ea2017-04-28 15:35:12 -0400129 canvas->drawString("M",
reedd7127e72014-08-12 06:53:09 -0700130 SkIntToScalar(100), SkIntToScalar(100),
131 paint2);
132
caryclarkf597c422015-07-28 10:37:53 -0700133 paint2.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
reedd7127e72014-08-12 06:53:09 -0700134
135 canvas->drawPath(path, paint2);
136 }
137
138private:
reed1a9b9642016-03-13 14:13:58 -0700139 SkBitmap fTexture;
140 bool fTextureCreated;
141 sk_sp<SkShader> fShader;
reedefdfd512016-04-04 10:02:58 -0700142 sk_sp<SkMaskFilter> fMaskFilter;
reedd7127e72014-08-12 06:53:09 -0700143
144 typedef GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149static GM* MyFactory(void*) { return new SamplerStressGM; }
150static GMRegistry reg(MyFactory);
151
152}