xref: /aosp_15_r20/external/skia/gm/complexclip_blur_tiled.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "tools/ToolUtils.h"
22 
23 #define WIDTH 512
24 #define HEIGHT 512
25 
26 namespace skiagm {
27 
28 class ComplexClipBlurTiledGM : public GM {
29 public:
ComplexClipBlurTiledGM()30     ComplexClipBlurTiledGM() {
31     }
32 
33 protected:
getName() const34     SkString getName() const override { return SkString("complexclip_blur_tiled"); }
35 
getISize()36     SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
37 
onDraw(SkCanvas * canvas)38     void onDraw(SkCanvas* canvas) override {
39         SkPaint blurPaint;
40         blurPaint.setImageFilter(SkImageFilters::Blur(5.0f, 5.0f, nullptr));
41         const SkScalar tileSize = SkIntToScalar(128);
42         SkRect bounds = canvas->getLocalClipBounds();
43         int ts = SkScalarCeilToInt(tileSize);
44         SkImageInfo info = SkImageInfo::MakeN32Premul(ts, ts);
45         auto           tileSurface(ToolUtils::makeSurface(canvas, info));
46         SkCanvas* tileCanvas = tileSurface->getCanvas();
47         for (SkScalar y = bounds.top(); y < bounds.bottom(); y += tileSize) {
48             for (SkScalar x = bounds.left(); x < bounds.right(); x += tileSize) {
49                 tileCanvas->save();
50                 tileCanvas->clear(0);
51                 tileCanvas->translate(-x, -y);
52                 SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
53                 tileCanvas->saveLayer(&rect, &blurPaint);
54                 SkRRect rrect = SkRRect::MakeRectXY(rect.makeInset(20, 20), 25, 25);
55                 tileCanvas->clipRRect(rrect, SkClipOp::kDifference, true);
56                 SkPaint paint;
57                 tileCanvas->drawRect(rect, paint);
58                 tileCanvas->restore();
59                 tileCanvas->restore();
60                 canvas->drawImage(tileSurface->makeImageSnapshot().get(), x, y);
61             }
62         }
63     }
64 
65 private:
66     using INHERITED = GM;
67 };
68 
69 //////////////////////////////////////////////////////////////////////////////
70 
71 DEF_GM(return new ComplexClipBlurTiledGM;)
72 
73 }  // namespace skiagm
74