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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkShader.h" |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /** |
| 18 | * Stress test the GPU samplers by rendering a textured glyph with a mask and |
| 19 | * an AA clip |
| 20 | */ |
| 21 | class SamplerStressGM : public GM { |
| 22 | public: |
| 23 | SamplerStressGM() |
| 24 | : fTextureCreated(false) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | , fMaskFilter(nullptr) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 26 | } |
| 27 | |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 28 | protected: |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 31 | return SkString("gpusamplerstress"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 35 | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | constexpr int xSize = 16; |
| 47 | constexpr int ySize = 16; |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 48 | |
| 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() { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | if (fShader) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | createTexture(); |
| 74 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 75 | fShader = SkShader::MakeBitmapShader(fTexture, SkShader::kRepeat_TileMode, |
| 76 | SkShader::kRepeat_TileMode); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void createMaskFilter() { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 80 | if (fMaskFilter) { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | const SkScalar sigma = 1; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 85 | fMaskFilter = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 86 | } |
| 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void onDraw(SkCanvas* canvas) override { |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 89 | 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); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 99 | paint.setShader(fShader); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 100 | paint.setMaskFilter(fMaskFilter); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 101 | sk_tool_utils::set_portable_typeface(&paint); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 102 | |
| 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 | |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 112 | canvas->clipPath(path, true); // AA is on |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 113 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 114 | canvas->drawString("M", |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 115 | 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); |
caryclark | 66fb63f | 2015-07-28 11:22:48 -0700 | [diff] [blame] | 128 | sk_tool_utils::set_portable_typeface(&paint2); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 129 | canvas->drawString("M", |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 130 | SkIntToScalar(100), SkIntToScalar(100), |
| 131 | paint2); |
| 132 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 133 | paint2.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 134 | |
| 135 | canvas->drawPath(path, paint2); |
| 136 | } |
| 137 | |
| 138 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 139 | SkBitmap fTexture; |
| 140 | bool fTextureCreated; |
| 141 | sk_sp<SkShader> fShader; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 142 | sk_sp<SkMaskFilter> fMaskFilter; |
reed | d7127e7 | 2014-08-12 06:53:09 -0700 | [diff] [blame] | 143 | |
| 144 | typedef GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | static GM* MyFactory(void*) { return new SamplerStressGM; } |
| 150 | static GMRegistry reg(MyFactory); |
| 151 | |
| 152 | } |