robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkShader.h" |
robertphillips@google.com | 941ee93 | 2012-06-05 12:55:05 +0000 | [diff] [blame] | 11 | #include "SkStippleMaskFilter.h" |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 15 | /** |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 16 | * Stress test the samplers by rendering a textured glyph with a mask and |
| 17 | * an AA clip |
| 18 | */ |
| 19 | class SamplerStressGM : public GM { |
| 20 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 21 | SamplerStressGM() |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 22 | : fTextureCreated(false) |
| 23 | , fShader(NULL) |
| 24 | , fMaskFilter(NULL) { |
| 25 | } |
| 26 | |
| 27 | virtual ~SamplerStressGM() { |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | virtual SkString onShortName() { |
| 32 | return SkString("samplerstress"); |
| 33 | } |
| 34 | |
| 35 | virtual SkISize onISize() { |
| 36 | return make_isize(640, 480); |
| 37 | } |
| 38 | |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Create a red & green stripes on black texture |
| 41 | */ |
| 42 | void createTexture() { |
| 43 | if (fTextureCreated) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | static const int xSize = 16; |
| 48 | static const int ySize = 16; |
| 49 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 50 | fTexture.allocN32Pixels(xSize, ySize); |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 51 | SkPMColor* addr = fTexture.getAddr32(0, 0); |
| 52 | |
| 53 | for (int y = 0; y < ySize; ++y) { |
| 54 | for (int x = 0; x < xSize; ++x) { |
| 55 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK); |
| 56 | |
| 57 | if ((y % 5) == 0) { |
| 58 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED); |
| 59 | } |
| 60 | if ((x % 7) == 0) { |
| 61 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 66 | fTextureCreated = true; |
| 67 | } |
| 68 | |
| 69 | void createShader() { |
| 70 | if (NULL != fShader.get()) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | createTexture(); |
| 75 | |
| 76 | fShader.reset(SkShader::CreateBitmapShader(fTexture, |
| 77 | SkShader::kRepeat_TileMode, |
| 78 | SkShader::kRepeat_TileMode)); |
| 79 | } |
| 80 | |
| 81 | void createMaskFilter() { |
| 82 | if (NULL != fMaskFilter.get()) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | fMaskFilter.reset(SkNEW(SkStippleMaskFilter)); |
| 87 | } |
| 88 | |
| 89 | virtual void onDraw(SkCanvas* canvas) { |
| 90 | |
| 91 | createShader(); |
| 92 | createMaskFilter(); |
| 93 | |
| 94 | canvas->save(); |
| 95 | |
| 96 | // draw a letter "M" with a green & red striped texture and a |
| 97 | // stipple mask with a round rect soft clip |
| 98 | SkPaint paint; |
| 99 | paint.setAntiAlias(true); |
| 100 | paint.setTextSize(72); |
| 101 | paint.setShader(fShader.get()); |
| 102 | paint.setMaskFilter(fMaskFilter.get()); |
| 103 | |
| 104 | SkRect temp; |
| 105 | temp.set(SkIntToScalar(115), |
| 106 | SkIntToScalar(75), |
| 107 | SkIntToScalar(144), |
| 108 | SkIntToScalar(110)); |
| 109 | |
| 110 | SkPath path; |
| 111 | path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5)); |
| 112 | |
| 113 | canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on |
| 114 | |
| 115 | canvas->drawText("M", 1, |
| 116 | SkIntToScalar(100), SkIntToScalar(100), |
| 117 | paint); |
| 118 | |
| 119 | canvas->restore(); |
| 120 | |
| 121 | // Now draw stroked versions of the "M" and the round rect so we can |
| 122 | // see what is going on |
| 123 | SkPaint paint2; |
| 124 | paint2.setColor(SK_ColorBLACK); |
| 125 | paint2.setAntiAlias(true); |
| 126 | paint2.setTextSize(72); |
| 127 | paint2.setStyle(SkPaint::kStroke_Style); |
| 128 | paint2.setStrokeWidth(1); |
| 129 | canvas->drawText("M", 1, |
| 130 | SkIntToScalar(100), SkIntToScalar(100), |
| 131 | paint2); |
| 132 | |
| 133 | paint2.setColor(SK_ColorGRAY); |
| 134 | |
| 135 | canvas->drawPath(path, paint2); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | SkBitmap fTexture; |
| 140 | bool fTextureCreated; |
| 141 | SkAutoTUnref<SkShader> fShader; |
| 142 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
| 143 | |
| 144 | typedef GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | static GM* MyFactory(void*) { return new SamplerStressGM; } |
| 150 | static GMRegistry reg(MyFactory); |
| 151 | |
| 152 | } |