1 /* 2 * Copyright 2015 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 "SkBlurImageFilter.h" 11 #include "SkColorFilterImageFilter.h" 12 #include "SkModeColorFilter.h" 13 #include "SkMorphologyImageFilter.h" 14 #include "SkOffsetImageFilter.h" 15 #include "SkSurface.h" 16 #include "sk_tool_utils.h" 17 18 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) { 19 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); 20 auto surface(sk_tool_utils::makeSurface(rootCanvas, info)); 21 22 SkPaint paint; 23 paint.setAntiAlias(true); 24 paint.setColor(SK_ColorRED); 25 surface->getCanvas()->drawCircle(50, 50, 50, paint); 26 return surface->makeImageSnapshot(); 27 } 28 29 static void show_image(SkCanvas* canvas, SkImage* image, sk_sp<SkImageFilter> filter) { 30 SkPaint paint; 31 paint.setStyle(SkPaint::kStroke_Style); 32 SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf, 33 SK_ScalarHalf); 34 canvas->drawRect(r, paint); 35 36 paint.setStyle(SkPaint::kFill_Style); 37 paint.setImageFilter(filter); 38 canvas->drawImage(image, 0, 0, &paint); 39 } 40 41 typedef sk_sp<SkImageFilter> (*ImageFilterFactory)(); 42 43 // +[]{...} did not work on windows (VS) 44 // (ImageFilterFactory)[]{...} did not work on linux (gcc) 45 // hence this cast function 46 template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; } 47 48 // Show the effect of localmatriximagefilter with various matrices, on various filters 49 DEF_SIMPLE_GM(localmatriximagefilter, canvas, 640, 640) { 50 sk_sp<SkImage> image0(make_image(canvas)); 51 52 const ImageFilterFactory factories[] = { 53 IFCCast([]{ return SkBlurImageFilter::Make(8, 8, nullptr); }), 54 IFCCast([]{ return SkDilateImageFilter::Make(8, 8, nullptr); }), 55 IFCCast([]{ return SkErodeImageFilter::Make(8, 8, nullptr); }), 56 IFCCast([]{ return SkOffsetImageFilter::Make(8, 8, nullptr); }), 57 }; 58 59 const SkMatrix matrices[] = { 60 SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf), 61 SkMatrix::MakeScale(2, 2), 62 SkMatrix::MakeTrans(10, 10) 63 }; 64 65 const SkScalar spacer = image0->width() * 3.0f / 2; 66 67 canvas->translate(40, 40); 68 for (auto&& factory : factories) { 69 sk_sp<SkImageFilter> filter(factory()); 70 71 canvas->save(); 72 show_image(canvas, image0.get(), filter); 73 for (const auto& matrix : matrices) { 74 sk_sp<SkImageFilter> localFilter(filter->makeWithLocalMatrix(matrix)); 75 canvas->translate(spacer, 0); 76 show_image(canvas, image0.get(), std::move(localFilter)); 77 } 78 canvas->restore(); 79 canvas->translate(0, spacer); 80 } 81 } 82