blob: 0563b60e0c09d1cd3da85e125a08d0ed22b29b5b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
13#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkMaskFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
23#include "include/core/SkTileMode.h"
24#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
reedd7127e72014-08-12 06:53:09 -070026
27namespace skiagm {
28
29/**
30 * Stress test the GPU samplers by rendering a textured glyph with a mask and
31 * an AA clip
32 */
33class SamplerStressGM : public GM {
34public:
35 SamplerStressGM()
36 : fTextureCreated(false)
halcanary96fcdcc2015-08-27 07:41:13 -070037 , fMaskFilter(nullptr) {
reedd7127e72014-08-12 06:53:09 -070038 }
39
reedd7127e72014-08-12 06:53:09 -070040protected:
reedd7127e72014-08-12 06:53:09 -070041
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
reedd7127e72014-08-12 06:53:09 -070043 return SkString("gpusamplerstress");
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 SkISize onISize() override {
reedd7127e72014-08-12 06:53:09 -070047 return SkISize::Make(640, 480);
48 }
49
50 /**
51 * Create a red & green stripes on black texture
52 */
53 void createTexture() {
54 if (fTextureCreated) {
55 return;
56 }
57
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr int xSize = 16;
59 constexpr int ySize = 16;
reedd7127e72014-08-12 06:53:09 -070060
61 fTexture.allocN32Pixels(xSize, ySize);
62 SkPMColor* addr = fTexture.getAddr32(0, 0);
63
64 for (int y = 0; y < ySize; ++y) {
65 for (int x = 0; x < xSize; ++x) {
66 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
67
68 if ((y % 5) == 0) {
69 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
70 }
71 if ((x % 7) == 0) {
72 addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
73 }
74 }
75 }
76
77 fTextureCreated = true;
78 }
79
80 void createShader() {
reed1a9b9642016-03-13 14:13:58 -070081 if (fShader) {
reedd7127e72014-08-12 06:53:09 -070082 return;
83 }
84
85 createTexture();
86
Mike Reed057fcbe2020-12-12 14:31:25 -050087 fShader = fTexture.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
88 SkSamplingOptions());
reedd7127e72014-08-12 06:53:09 -070089 }
90
91 void createMaskFilter() {
reedefdfd512016-04-04 10:02:58 -070092 if (fMaskFilter) {
reedd7127e72014-08-12 06:53:09 -070093 return;
94 }
95
96 const SkScalar sigma = 1;
Mike Reed1be1f8d2018-03-14 13:01:17 -040097 fMaskFilter = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma);
reedd7127e72014-08-12 06:53:09 -070098 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 void onDraw(SkCanvas* canvas) override {
reedd7127e72014-08-12 06:53:09 -0700101 createShader();
102 createMaskFilter();
103
104 canvas->save();
105
106 // draw a letter "M" with a green & red striped texture and a
107 // stipple mask with a round rect soft clip
108 SkPaint paint;
109 paint.setAntiAlias(true);
reedfe630452016-03-25 09:08:00 -0700110 paint.setShader(fShader);
reedefdfd512016-04-04 10:02:58 -0700111 paint.setMaskFilter(fMaskFilter);
Mike Kleinea3f0142019-03-20 11:12:10 -0500112 SkFont font(ToolUtils::create_portable_typeface(), 72);
reedd7127e72014-08-12 06:53:09 -0700113
114 SkRect temp;
Mike Reed92b33352019-08-24 19:39:13 -0400115 temp.setLTRB(115, 75, 144, 110);
reedd7127e72014-08-12 06:53:09 -0700116
117 SkPath path;
118 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
119
reed66998382016-09-21 11:15:07 -0700120 canvas->clipPath(path, true); // AA is on
reedd7127e72014-08-12 06:53:09 -0700121
Hal Canarydf2d27e2019-01-08 09:38:02 -0500122 canvas->drawString("M", 100.0f, 100.0f, font, paint);
reedd7127e72014-08-12 06:53:09 -0700123
124 canvas->restore();
125
126 // Now draw stroked versions of the "M" and the round rect so we can
127 // see what is going on
128 SkPaint paint2;
129 paint2.setColor(SK_ColorBLACK);
130 paint2.setAntiAlias(true);
reedd7127e72014-08-12 06:53:09 -0700131 paint2.setStyle(SkPaint::kStroke_Style);
132 paint2.setStrokeWidth(1);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500133 canvas->drawString("M", 100.0f, 100.0f, font, paint2);
reedd7127e72014-08-12 06:53:09 -0700134
Mike Kleind46dce32018-08-16 10:17:03 -0400135 paint2.setColor(SK_ColorGRAY);
reedd7127e72014-08-12 06:53:09 -0700136
137 canvas->drawPath(path, paint2);
138 }
139
140private:
reed1a9b9642016-03-13 14:13:58 -0700141 SkBitmap fTexture;
142 bool fTextureCreated;
143 sk_sp<SkShader> fShader;
reedefdfd512016-04-04 10:02:58 -0700144 sk_sp<SkMaskFilter> fMaskFilter;
reedd7127e72014-08-12 06:53:09 -0700145
John Stiles7571f9e2020-09-02 22:42:33 -0400146 using INHERITED = GM;
reedd7127e72014-08-12 06:53:09 -0700147};
148
149//////////////////////////////////////////////////////////////////////////////
150
Hal Canarye964c182019-01-23 10:22:01 -0500151DEF_GM( return new SamplerStressGM; )
reedd7127e72014-08-12 06:53:09 -0700152
John Stilesa6841be2020-08-06 14:11:56 -0400153} // namespace skiagm