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