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 | |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 15 | /** |
| 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: |
| 21 | SamplerStressGM() |
| 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 | |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 39 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 40 | // Skip Pipe playback since we use a custom MaskFilter that will not be |
| 41 | // flattened correctly |
| 42 | return this->INHERITED::onGetFlags() | GM::kSkipPipe_Flag; |
| 43 | } |
| 44 | |
robertphillips@google.com | d9d5385 | 2012-05-02 13:55:06 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Create a red & green stripes on black texture |
| 47 | */ |
| 48 | void createTexture() { |
| 49 | if (fTextureCreated) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | static const int xSize = 16; |
| 54 | static const int ySize = 16; |
| 55 | |
| 56 | fTexture.setConfig(SkBitmap::kARGB_8888_Config, |
| 57 | xSize, |
| 58 | ySize, |
| 59 | xSize*sizeof(SkColor)); |
| 60 | |
| 61 | fTexture.allocPixels(); |
| 62 | fTexture.lockPixels(); |
| 63 | SkPMColor* addr = fTexture.getAddr32(0, 0); |
| 64 | |
| 65 | for (int y = 0; y < ySize; ++y) { |
| 66 | for (int x = 0; x < xSize; ++x) { |
| 67 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK); |
| 68 | |
| 69 | if ((y % 5) == 0) { |
| 70 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED); |
| 71 | } |
| 72 | if ((x % 7) == 0) { |
| 73 | addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fTexture.unlockPixels(); |
| 79 | |
| 80 | fTextureCreated = true; |
| 81 | } |
| 82 | |
| 83 | void createShader() { |
| 84 | if (NULL != fShader.get()) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | createTexture(); |
| 89 | |
| 90 | fShader.reset(SkShader::CreateBitmapShader(fTexture, |
| 91 | SkShader::kRepeat_TileMode, |
| 92 | SkShader::kRepeat_TileMode)); |
| 93 | } |
| 94 | |
| 95 | void createMaskFilter() { |
| 96 | if (NULL != fMaskFilter.get()) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | fMaskFilter.reset(SkNEW(SkStippleMaskFilter)); |
| 101 | } |
| 102 | |
| 103 | virtual void onDraw(SkCanvas* canvas) { |
| 104 | |
| 105 | createShader(); |
| 106 | createMaskFilter(); |
| 107 | |
| 108 | canvas->save(); |
| 109 | |
| 110 | // draw a letter "M" with a green & red striped texture and a |
| 111 | // stipple mask with a round rect soft clip |
| 112 | SkPaint paint; |
| 113 | paint.setAntiAlias(true); |
| 114 | paint.setTextSize(72); |
| 115 | paint.setShader(fShader.get()); |
| 116 | paint.setMaskFilter(fMaskFilter.get()); |
| 117 | |
| 118 | SkRect temp; |
| 119 | temp.set(SkIntToScalar(115), |
| 120 | SkIntToScalar(75), |
| 121 | SkIntToScalar(144), |
| 122 | SkIntToScalar(110)); |
| 123 | |
| 124 | SkPath path; |
| 125 | path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5)); |
| 126 | |
| 127 | canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on |
| 128 | |
| 129 | canvas->drawText("M", 1, |
| 130 | SkIntToScalar(100), SkIntToScalar(100), |
| 131 | paint); |
| 132 | |
| 133 | canvas->restore(); |
| 134 | |
| 135 | // Now draw stroked versions of the "M" and the round rect so we can |
| 136 | // see what is going on |
| 137 | SkPaint paint2; |
| 138 | paint2.setColor(SK_ColorBLACK); |
| 139 | paint2.setAntiAlias(true); |
| 140 | paint2.setTextSize(72); |
| 141 | paint2.setStyle(SkPaint::kStroke_Style); |
| 142 | paint2.setStrokeWidth(1); |
| 143 | canvas->drawText("M", 1, |
| 144 | SkIntToScalar(100), SkIntToScalar(100), |
| 145 | paint2); |
| 146 | |
| 147 | paint2.setColor(SK_ColorGRAY); |
| 148 | |
| 149 | canvas->drawPath(path, paint2); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | SkBitmap fTexture; |
| 154 | bool fTextureCreated; |
| 155 | SkAutoTUnref<SkShader> fShader; |
| 156 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
| 157 | |
| 158 | typedef GM INHERITED; |
| 159 | }; |
| 160 | |
| 161 | ////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | static GM* MyFactory(void*) { return new SamplerStressGM; } |
| 164 | static GMRegistry reg(MyFactory); |
| 165 | |
| 166 | } |