reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
| 23 | #include "include/core/SkTileMode.h" |
| 24 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/ToolUtils.h" |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 26 | |
| 27 | namespace skiagm { |
| 28 | |
| 29 | /** |
| 30 | * Stress test the GPU samplers by rendering a textured glyph with a mask and |
| 31 | * an AA clip |
| 32 | */ |
| 33 | class SamplerStressGM : public GM { |
| 34 | public: |
| 35 | SamplerStressGM() |
| 36 | : fTextureCreated(false) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | , fMaskFilter(nullptr) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 38 | } |
| 39 | |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 40 | protected: |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkString onShortName() override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 43 | return SkString("gpusamplerstress"); |
| 44 | } |
| 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | SkISize onISize() override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 47 | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 58 | constexpr int xSize = 16; |
| 59 | constexpr int ySize = 16; |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 60 | |
| 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() { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 81 | if (fShader) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | createTexture(); |
| 86 | |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 87 | fShader = fTexture.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 88 | SkSamplingOptions()); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void createMaskFilter() { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 92 | if (fMaskFilter) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | const SkScalar sigma = 1; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 97 | fMaskFilter = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 98 | } |
| 99 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | void onDraw(SkCanvas* canvas) override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 101 | 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); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 110 | paint.setShader(fShader); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 111 | paint.setMaskFilter(fMaskFilter); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 112 | SkFont font(ToolUtils::create_portable_typeface(), 72); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 113 | |
| 114 | SkRect temp; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 115 | temp.setLTRB(115, 75, 144, 110); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 116 | |
| 117 | SkPath path; |
| 118 | path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5)); |
| 119 | |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 120 | canvas->clipPath(path, true); // AA is on |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 121 | |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 122 | canvas->drawString("M", 100.0f, 100.0f, font, paint); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 123 | |
| 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); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 131 | paint2.setStyle(SkPaint::kStroke_Style); |
| 132 | paint2.setStrokeWidth(1); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 133 | canvas->drawString("M", 100.0f, 100.0f, font, paint2); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 134 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 135 | paint2.setColor(SK_ColorGRAY); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 136 | |
| 137 | canvas->drawPath(path, paint2); |
| 138 | } |
| 139 | |
| 140 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 141 | SkBitmap fTexture; |
| 142 | bool fTextureCreated; |
| 143 | sk_sp<SkShader> fShader; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 144 | sk_sp<SkMaskFilter> fMaskFilter; |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 145 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 146 | using INHERITED = GM; |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////////////// |
| 150 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 151 | DEF_GM( return new SamplerStressGM; ) |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 152 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 153 | } // namespace skiagm |