localmatriximagefilter.cpp 3.1 KB

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