• 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.h"
9 #include "SkBlurImageFilter.h"
10 #include "SkRRect.h"
11 #include "SkSurface.h"
12 #include "SkClipOpPriv.h"
13 
14 #define WIDTH 512
15 #define HEIGHT 512
16 
17 namespace skiagm {
18 
19 class ComplexClipBlurTiledGM : public GM {
20 public:
ComplexClipBlurTiledGM()21     ComplexClipBlurTiledGM() {
22     }
23 
24 protected:
onShortName()25     SkString onShortName() override {
26         return SkString("complexclip_blur_tiled");
27     }
28 
onISize()29     SkISize onISize() override {
30         return SkISize::Make(WIDTH, HEIGHT);
31     }
32 
onDraw(SkCanvas * canvas)33     void onDraw(SkCanvas* canvas) override {
34         SkPaint blurPaint;
35         blurPaint.setImageFilter(SkBlurImageFilter::Make(5.0f, 5.0f, nullptr));
36         const SkScalar tileSize = SkIntToScalar(128);
37         SkRect bounds = canvas->getLocalClipBounds();
38         int ts = SkScalarCeilToInt(tileSize);
39         SkImageInfo info = SkImageInfo::MakeN32Premul(ts, ts);
40         auto tileSurface(canvas->makeSurface(info));
41         if (!tileSurface) {
42             tileSurface = SkSurface::MakeRaster(info);
43         }
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 
63 private:
64     typedef GM INHERITED;
65 };
66 
67 //////////////////////////////////////////////////////////////////////////////
68 
69 DEF_GM(return new ComplexClipBlurTiledGM;)
70 
71 }
72