| OLD | NEW |
| (Empty) |
| 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" | |
| 11 #include "SkStippleMaskFilter.h" | |
| 12 | |
| 13 namespace skiagm { | |
| 14 | |
| 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 SkISize::Make(640, 480); | |
| 37 } | |
| 38 | |
| 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 | |
| 50 fTexture.allocN32Pixels(xSize, ySize); | |
| 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 | |
| 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(SkStippleMaskFilter::Create()); | |
| 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 sk_tool_utils::set_portable_typeface(&paint); | |
| 101 paint.setTextSize(72); | |
| 102 paint.setShader(fShader.get()); | |
| 103 paint.setMaskFilter(fMaskFilter.get()); | |
| 104 | |
| 105 SkRect temp; | |
| 106 temp.set(SkIntToScalar(115), | |
| 107 SkIntToScalar(75), | |
| 108 SkIntToScalar(144), | |
| 109 SkIntToScalar(110)); | |
| 110 | |
| 111 SkPath path; | |
| 112 path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5)); | |
| 113 | |
| 114 canvas->clipPath(path, SkRegion::kReplace_Op, true); // AA is on | |
| 115 | |
| 116 canvas->drawText("M", 1, | |
| 117 SkIntToScalar(100), SkIntToScalar(100), | |
| 118 paint); | |
| 119 | |
| 120 canvas->restore(); | |
| 121 | |
| 122 // Now draw stroked versions of the "M" and the round rect so we can | |
| 123 // see what is going on | |
| 124 SkPaint paint2; | |
| 125 paint2.setColor(SK_ColorBLACK); | |
| 126 paint2.setAntiAlias(true); | |
| 127 sk_tool_utils::set_portable_typeface(&paint2); | |
| 128 paint2.setTextSize(72); | |
| 129 paint2.setStyle(SkPaint::kStroke_Style); | |
| 130 paint2.setStrokeWidth(1); | |
| 131 canvas->drawText("M", 1, | |
| 132 SkIntToScalar(100), SkIntToScalar(100), | |
| 133 paint2); | |
| 134 | |
| 135 paint2.setColor(SK_ColorGRAY); | |
| 136 | |
| 137 canvas->drawPath(path, paint2); | |
| 138 } | |
| 139 | |
| 140 private: | |
| 141 SkBitmap fTexture; | |
| 142 bool fTextureCreated; | |
| 143 SkAutoTUnref<SkShader> fShader; | |
| 144 SkAutoTUnref<SkMaskFilter> fMaskFilter; | |
| 145 | |
| 146 typedef GM INHERITED; | |
| 147 }; | |
| 148 | |
| 149 ////////////////////////////////////////////////////////////////////////////// | |
| 150 | |
| 151 static GM* MyFactory(void*) { return new SamplerStressGM; } | |
| 152 static GMRegistry reg(MyFactory); | |
| 153 | |
| 154 } | |
| OLD | NEW |