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