blurimagevmask.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "gm/gm.h"
  8. #include "include/core/SkBlurTypes.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkMaskFilter.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "include/effects/SkBlurImageFilter.h"
  21. #include "tools/Resources.h"
  22. #include "tools/ToolUtils.h"
  23. #include <stdio.h>
  24. DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) {
  25. SkPaint paint;
  26. paint.setAntiAlias(true);
  27. paint.setColor(SK_ColorBLACK);
  28. SkFont font(ToolUtils::create_portable_typeface(), 25);
  29. const double sigmas[] = {3.0, 8.0, 16.0, 24.0, 32.0};
  30. canvas->drawString("mask blur", 285, 50, font, paint);
  31. canvas->drawString("image blur", 285 + 250, 50, font, paint);
  32. SkRect r = {35, 100, 135, 200};
  33. for (auto sigma:sigmas) {
  34. canvas->drawRect(r, paint);
  35. char out[100];
  36. sprintf(out, "Sigma: %g", sigma);
  37. canvas->drawString(out, r.left(), r.bottom() + 35, font, paint);
  38. r.offset(250, 0);
  39. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
  40. canvas->drawRect(r, paint);
  41. paint.setMaskFilter(nullptr);
  42. SkPaint imageBlurPaint;
  43. r.offset(250, 0);
  44. imageBlurPaint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
  45. canvas->saveLayer(nullptr, &imageBlurPaint);
  46. canvas->drawRect(r, paint);
  47. canvas->restore();
  48. r.offset(-500, 200);
  49. }
  50. }
  51. DEF_SIMPLE_GM_CAN_FAIL(blur_image, canvas, errorMsg, 500, 500) {
  52. auto image = GetResourceAsImage("images/mandrill_128.png");
  53. if (!image) {
  54. *errorMsg = "Could not load mandrill_128.png. Did you forget to set the resourcePath?";
  55. return skiagm::DrawResult::kFail;
  56. }
  57. SkPaint paint;
  58. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4));
  59. // both of these should draw with the blur, but (formerally) we had a bug where the unscaled
  60. // version (taking the spriteblitter code path) ignore the maskfilter.
  61. canvas->drawImage(image, 10, 10, &paint);
  62. canvas->scale(1.01f, 1.01f);
  63. canvas->drawImage(image, 10 + image->width() + 10.f, 10, &paint);
  64. return skiagm::DrawResult::kOk;
  65. }