1 /* 2 * Copyright 2017 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 "SkSurface.h" 11 #include "SkBlurImageFilter.h" 12 13 static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) { 14 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200); 15 auto surface = sk_tool_utils::makeSurface(canvas, info); 16 SkCanvas* c = surface->getCanvas(); 17 SkPaint paint; 18 paint.setAntiAlias(true); 19 20 const SkColor colors[] = { 21 SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK 22 }; 23 24 int width = 25; 25 bool xDirection = (direction & 0x1) == 1; 26 bool yDirection = (direction & 0x2) == 2; 27 if (xDirection) { 28 for (int x = 0; x < info.width(); x += width) { 29 paint.setColor(colors[x/width % 5]); 30 if (yDirection) { 31 paint.setAlpha(127); 32 } 33 c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint); 34 } 35 } 36 37 if (yDirection) { 38 for (int y = 0; y < info.height(); y += width) { 39 paint.setColor(colors[y/width % 5]); 40 if (xDirection) { 41 paint.setAlpha(127); 42 } 43 c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint); 44 } 45 } 46 return surface->makeImageSnapshot(); 47 } 48 49 static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) { 50 SkAutoCanvasRestore acr(canvas, true); 51 SkPaint paint; 52 paint.setImageFilter(std::move(filter)); 53 54 canvas->translate(SkIntToScalar(30), 0); 55 canvas->clipRect(SkRect::MakeIWH(image->width(),image->height())); 56 canvas->drawImage(image, 0, 0, &paint); 57 } 58 59 namespace skiagm { 60 61 // This GM draws a colorful grids with different blur settings. 62 class ImageBlurRepeatModeGM : public GM { 63 public: 64 ImageBlurRepeatModeGM() { 65 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); 66 } 67 68 protected: 69 70 SkString onShortName() override { 71 return SkString("imageblurrepeatmode"); 72 } 73 74 SkISize onISize() override { 75 return SkISize::Make(850, 920); 76 } 77 78 bool runAsBench() const override { return true; } 79 80 void onDraw(SkCanvas* canvas) override { 81 sk_sp<SkImage> image[] = 82 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) }; 83 84 canvas->translate(0, 30); 85 // Test different kernel size, including the one to launch 2d Gaussian 86 // blur. 87 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) { 88 canvas->save(); 89 sk_sp<SkImageFilter> filter( 90 SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr, 91 SkBlurImageFilter::kRepeat_TileMode)); 92 draw_image(canvas, image[0], std::move(filter)); 93 canvas->translate(image[0]->width() + 20, 0); 94 95 filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr, 96 SkBlurImageFilter::kRepeat_TileMode); 97 draw_image(canvas, image[1], std::move(filter)); 98 canvas->translate(image[1]->width() + 20, 0); 99 100 filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr, 101 SkBlurImageFilter::kRepeat_TileMode); 102 draw_image(canvas, image[2], std::move(filter)); 103 canvas->translate(image[2]->width() + 20, 0); 104 105 canvas->restore(); 106 canvas->translate(0, image[0]->height() + 20); 107 } 108 } 109 110 private: 111 typedef GM INHERITED; 112 }; 113 114 ////////////////////////////////////////////////////////////////////////////// 115 116 DEF_GM(return new ImageBlurRepeatModeGM;) 117 } 118