Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / croppedrects.cpp
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/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkSurface.h"
21 #include "tools/ToolUtils.h"
22
23 namespace skiagm {
24
25 constexpr SkRect kSrcImageClip{75, 75, 275, 275};
26
27 static sk_sp<SkImage> create_image(SkCanvas* destCanvas) {
28     sk_sp<SkSurface> srcSurface = SkSurface::MakeRasterN32Premul(500, 500);
29     SkCanvas* srcCanvas = srcSurface->getCanvas();
30
31     srcCanvas->clear(SK_ColorRED);
32
33     SkPaint paint;
34     paint.setColor(0xff00ff00);
35     srcCanvas->drawRect(kSrcImageClip, paint);
36
37     constexpr SkScalar kStrokeWidth = 10;
38     SkPaint stroke;
39     stroke.setStyle(SkPaint::kStroke_Style);
40     stroke.setStrokeWidth(kStrokeWidth);
41     stroke.setColor(0xff008800);
42     srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
43
44     return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot());
45 }
46
47 /*
48  * The purpose of this test is to exercise all three codepaths in skgpu::v1::SurfaceDrawContext
49  * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
50  * clip.
51  *
52  * The test creates an image of a green square surrounded by red background, then draws this image
53  * in various ways with the red clipped out. The test is successful if there is no visible red
54  * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
55  */
56 class CroppedRectsGM : public GM {
57 private:
58     SkString onShortName() final { return SkString("croppedrects"); }
59     SkISize onISize() override { return SkISize::Make(500, 500); }
60
61     void onDraw(SkCanvas* canvas) override {
62         if (!fSrcImage) {
63             fSrcImage = create_image(canvas);
64             fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
65         }
66
67         canvas->clear(SK_ColorWHITE);
68
69         {
70             // skgpu::v1::SurfaceDrawContext::drawFilledRect.
71             SkAutoCanvasRestore acr(canvas, true);
72             SkPaint paint;
73             paint.setShader(fSrcImageShader);
74             canvas->clipRect(kSrcImageClip);
75             canvas->drawPaint(paint);
76         }
77
78         {
79             // skgpu::v1::SurfaceDrawContext::fillRectToRect.
80             SkAutoCanvasRestore acr(canvas, true);
81             SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
82             canvas->clipRect(drawRect);
83             canvas->drawImageRect(fSrcImage.get(),
84                                   kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(),
85                                                            kSrcImageClip.height()),
86                                   drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
87                                   SkSamplingOptions(), nullptr,
88                                   SkCanvas::kStrict_SrcRectConstraint);
89         }
90
91         {
92             // skgpu::v1::SurfaceDrawContext::fillRectWithLocalMatrix.
93             SkAutoCanvasRestore acr(canvas, true);
94             SkPath path = SkPath::Line(
95                    {kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
96                    {kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()});
97             SkPaint paint;
98             paint.setStyle(SkPaint::kStroke_Style);
99             paint.setStrokeWidth(2 * kSrcImageClip.height());
100             paint.setShader(fSrcImageShader);
101             canvas->translate(23, 301);
102             canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
103             canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
104             canvas->clipRect(kSrcImageClip);
105             canvas->drawPath(path, paint);
106         }
107
108         // TODO: assert the draw target only has one op in the post-MDB world.
109     }
110
111     sk_sp<SkImage> fSrcImage;
112     sk_sp<SkShader> fSrcImageShader;
113
114     using INHERITED = GM;
115 };
116
117 DEF_GM( return new CroppedRectsGM(); )
118
119 }  // namespace skiagm