complexclip_blur_tiled.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2016 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/SkImageFilter.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkRRect.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkSurface.h"
  19. #include "include/effects/SkBlurImageFilter.h"
  20. #include "src/core/SkClipOpPriv.h"
  21. #include "tools/ToolUtils.h"
  22. #define WIDTH 512
  23. #define HEIGHT 512
  24. namespace skiagm {
  25. class ComplexClipBlurTiledGM : public GM {
  26. public:
  27. ComplexClipBlurTiledGM() {
  28. }
  29. protected:
  30. SkString onShortName() override {
  31. return SkString("complexclip_blur_tiled");
  32. }
  33. SkISize onISize() override {
  34. return SkISize::Make(WIDTH, HEIGHT);
  35. }
  36. void onDraw(SkCanvas* canvas) override {
  37. SkPaint blurPaint;
  38. blurPaint.setImageFilter(SkBlurImageFilter::Make(5.0f, 5.0f, nullptr));
  39. const SkScalar tileSize = SkIntToScalar(128);
  40. SkRect bounds = canvas->getLocalClipBounds();
  41. int ts = SkScalarCeilToInt(tileSize);
  42. SkImageInfo info = SkImageInfo::MakeN32Premul(ts, ts);
  43. auto tileSurface(ToolUtils::makeSurface(canvas, info));
  44. SkCanvas* tileCanvas = tileSurface->getCanvas();
  45. for (SkScalar y = bounds.top(); y < bounds.bottom(); y += tileSize) {
  46. for (SkScalar x = bounds.left(); x < bounds.right(); x += tileSize) {
  47. tileCanvas->save();
  48. tileCanvas->clear(0);
  49. tileCanvas->translate(-x, -y);
  50. SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
  51. tileCanvas->saveLayer(&rect, &blurPaint);
  52. SkRRect rrect = SkRRect::MakeRectXY(rect.makeInset(20, 20), 25, 25);
  53. tileCanvas->clipRRect(rrect, kDifference_SkClipOp, true);
  54. SkPaint paint;
  55. tileCanvas->drawRect(rect, paint);
  56. tileCanvas->restore();
  57. tileCanvas->restore();
  58. canvas->drawImage(tileSurface->makeImageSnapshot().get(), x, y);
  59. }
  60. }
  61. }
  62. private:
  63. typedef GM INHERITED;
  64. };
  65. //////////////////////////////////////////////////////////////////////////////
  66. DEF_GM(return new ComplexClipBlurTiledGM;)
  67. }