blob: ee87f091f9b86e8145c0798299410449853f2db7 [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"
reedfb8c1fc2015-08-04 18:44:56 -07009#include "SkBlurMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkShader.h"
reedd7127e72014-08-12 06:53:09 -070013
14namespace skiagm {
15
16/**
17 * Stress test the GPU samplers by rendering a textured glyph with a mask and
18 * an AA clip
19 */
20class SamplerStressGM : public GM {
21public:
22 SamplerStressGM()
23 : fTextureCreated(false)
halcanary96fcdcc2015-08-27 07:41:13 -070024 , fMaskFilter(nullptr) {
reedd7127e72014-08-12 06:53:09 -070025 }
26
reedd7127e72014-08-12 06:53:09 -070027protected:
reedd7127e72014-08-12 06:53:09 -070028
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
reedd7127e72014-08-12 06:53:09 -070030 return SkString("gpusamplerstress");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
reedd7127e72014-08-12 06:53:09 -070034 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() {
reed1a9b9642016-03-13 14:13:58 -070068 if (fShader) {
reedd7127e72014-08-12 06:53:09 -070069 return;
70 }
71
72 createTexture();
73
reed1a9b9642016-03-13 14:13:58 -070074 fShader = SkShader::MakeBitmapShader(fTexture, SkShader::kRepeat_TileMode,
75 SkShader::kRepeat_TileMode);
reedd7127e72014-08-12 06:53:09 -070076 }
77
78 void createMaskFilter() {
reedefdfd512016-04-04 10:02:58 -070079 if (fMaskFilter) {
reedd7127e72014-08-12 06:53:09 -070080 return;
81 }
82
83 const SkScalar sigma = 1;
reedefdfd512016-04-04 10:02:58 -070084 fMaskFilter = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma);
reedd7127e72014-08-12 06:53:09 -070085 }
86
mtklein36352bf2015-03-25 18:17:31 -070087 void onDraw(SkCanvas* canvas) override {
reedd7127e72014-08-12 06:53:09 -070088 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);
reedfe630452016-03-25 09:08:00 -070098 paint.setShader(fShader);
reedefdfd512016-04-04 10:02:58 -070099 paint.setMaskFilter(fMaskFilter);
caryclarkf597c422015-07-28 10:37:53 -0700100 sk_tool_utils::set_portable_typeface(&paint);
reedd7127e72014-08-12 06:53:09 -0700101
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);
caryclark66fb63f2015-07-28 11:22:48 -0700127 sk_tool_utils::set_portable_typeface(&paint2);
reedd7127e72014-08-12 06:53:09 -0700128 canvas->drawText("M", 1,
129 SkIntToScalar(100), SkIntToScalar(100),
130 paint2);
131
caryclarkf597c422015-07-28 10:37:53 -0700132 paint2.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
reedd7127e72014-08-12 06:53:09 -0700133
134 canvas->drawPath(path, paint2);
135 }
136
137private:
reed1a9b9642016-03-13 14:13:58 -0700138 SkBitmap fTexture;
139 bool fTextureCreated;
140 sk_sp<SkShader> fShader;
reedefdfd512016-04-04 10:02:58 -0700141 sk_sp<SkMaskFilter> fMaskFilter;
reedd7127e72014-08-12 06:53:09 -0700142
143 typedef GM INHERITED;
144};
145
146//////////////////////////////////////////////////////////////////////////////
147
148static GM* MyFactory(void*) { return new SamplerStressGM; }
149static GMRegistry reg(MyFactory);
150
151}