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 "sk_tool_utils.h" 10 #include "SkCanvas.h" 11 #include "SkMaskFilter.h" 12 #include "SkPath.h" 13 #include "SkShader.h" 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: SamplerStressGM()23 SamplerStressGM() 24 : fTextureCreated(false) 25 , fMaskFilter(nullptr) { 26 } 27 28 protected: 29 onShortName()30 SkString onShortName() override { 31 return SkString("gpusamplerstress"); 32 } 33 onISize()34 SkISize onISize() override { 35 return SkISize::Make(640, 480); 36 } 37 38 /** 39 * Create a red & green stripes on black texture 40 */ createTexture()41 void createTexture() { 42 if (fTextureCreated) { 43 return; 44 } 45 46 constexpr int xSize = 16; 47 constexpr int ySize = 16; 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 createShader()68 void createShader() { 69 if (fShader) { 70 return; 71 } 72 73 createTexture(); 74 75 fShader = SkShader::MakeBitmapShader(fTexture, SkShader::kRepeat_TileMode, 76 SkShader::kRepeat_TileMode); 77 } 78 createMaskFilter()79 void createMaskFilter() { 80 if (fMaskFilter) { 81 return; 82 } 83 84 const SkScalar sigma = 1; 85 fMaskFilter = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma); 86 } 87 onDraw(SkCanvas * canvas)88 void onDraw(SkCanvas* canvas) override { 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.setShader(fShader); 99 paint.setMaskFilter(fMaskFilter); 100 SkFont font(sk_tool_utils::create_portable_typeface(), 72); 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 111 canvas->clipPath(path, true); // AA is on 112 113 canvas->drawString("M", 100.0f, 100.0f, font, paint); 114 115 canvas->restore(); 116 117 // Now draw stroked versions of the "M" and the round rect so we can 118 // see what is going on 119 SkPaint paint2; 120 paint2.setColor(SK_ColorBLACK); 121 paint2.setAntiAlias(true); 122 paint2.setStyle(SkPaint::kStroke_Style); 123 paint2.setStrokeWidth(1); 124 canvas->drawString("M", 100.0f, 100.0f, font, paint2); 125 126 paint2.setColor(SK_ColorGRAY); 127 128 canvas->drawPath(path, paint2); 129 } 130 131 private: 132 SkBitmap fTexture; 133 bool fTextureCreated; 134 sk_sp<SkShader> fShader; 135 sk_sp<SkMaskFilter> fMaskFilter; 136 137 typedef GM INHERITED; 138 }; 139 140 ////////////////////////////////////////////////////////////////////////////// 141 142 DEF_GM( return new SamplerStressGM; ) 143 144 } 145