Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / dropshadowimagefilter.cpp
1 /*
2  * Copyright 2013 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/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "include/utils/SkTextUtils.h"
24 #include "tools/ToolUtils.h"
25
26 #include <utility>
27
28 ///////////////////////////////////////////////////////////////////////////////
29
30 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
31     SkPaint paint;
32     paint.setImageFilter(std::move(imf));
33     paint.setColor(SK_ColorBLACK);
34     canvas->save();
35     canvas->clipRect(r);
36     canvas->drawPaint(paint);
37     canvas->restore();
38 }
39
40 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
41     SkPaint paint;
42     paint.setColor(SK_ColorGREEN);
43     paint.setImageFilter(std::move(imf));
44     paint.setAntiAlias(true);
45     canvas->save();
46     canvas->clipRect(r);
47     canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
48     canvas->restore();
49 }
50
51 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
52     SkPaint paint;
53     paint.setImageFilter(std::move(imf));
54     paint.setColor(SK_ColorGREEN);
55     paint.setAntiAlias(true);
56
57     SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
58     canvas->save();
59     canvas->clipRect(r);
60     SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
61     canvas->restore();
62 }
63
64 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
65     SkPaint paint;
66
67     SkIRect bounds;
68     r.roundOut(&bounds);
69
70     auto surf = SkSurface::MakeRasterN32Premul(bounds.width(), bounds.height());
71     draw_path(surf->getCanvas(), r, nullptr);
72
73     paint.setImageFilter(std::move(imf));
74     canvas->save();
75     canvas->clipRect(r);
76     surf->draw(canvas, 0, 0, SkSamplingOptions(), &paint);
77     canvas->restore();
78 }
79
80 ///////////////////////////////////////////////////////////////////////////////
81
82 DEF_SIMPLE_GM(dropshadowimagefilter, canvas, 400, 656) {
83     void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
84         draw_bitmap, draw_path, draw_paint, draw_text
85     };
86
87     sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorMAGENTA, SkBlendMode::kSrcIn));
88     sk_sp<SkImageFilter> cfif(SkImageFilters::ColorFilter(std::move(cf), nullptr));
89     SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
90     SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
91
92     sk_sp<SkImageFilter> filters[] = {
93         nullptr,
94         SkImageFilters::DropShadow(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, nullptr),
95         SkImageFilters::DropShadow(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, nullptr),
96         SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
97         SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, std::move(cfif)),
98         SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &cropRect),
99         SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &bogusRect),
100         SkImageFilters::DropShadowOnly(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
101     };
102
103     SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
104     SkScalar MARGIN = SkIntToScalar(16);
105     SkScalar DX = r.width() + MARGIN;
106     SkScalar DY = r.height() + MARGIN;
107
108     canvas->translate(MARGIN, MARGIN);
109     for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
110         canvas->save();
111         for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
112             drawProc[j](canvas, r, filters[i]);
113             canvas->translate(0, DY);
114         }
115         canvas->restore();
116         canvas->translate(DX, 0);
117     }
118 }