Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / verylargebitmap.cpp
1 /*
2  * Copyright 2012 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/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPicture.h"
16 #include "include/core/SkPictureRecorder.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "tools/ToolUtils.h"
28
29 static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
30     const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
31     const SkScalar radius = 40;
32     SkPaint paint;
33     paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
34                                                  SkTileMode::kMirror));
35     paint.setBlendMode(SkBlendMode::kSrc);
36     canvas->drawPaint(paint);
37 }
38
39 static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
40     auto surface(SkSurface::MakeRasterN32Premul(width, height));
41     draw(surface->getCanvas(), width, height, colors);
42     return surface->makeImageSnapshot();
43 }
44
45 static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
46     SkPictureRecorder recorder;
47     draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
48     return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
49                                     {width, height}, nullptr, nullptr,
50                                     SkImage::BitDepth::kU8,
51                                     SkColorSpace::MakeSRGB());
52 }
53
54 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
55
56 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
57                        ImageMakerProc proc) {
58     sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, proc(width, height, colors));
59     if (!image) {
60         return;
61     }
62
63     SkPaint borderPaint;
64
65     borderPaint.setStyle(SkPaint::kStroke_Style);
66
67     SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
68
69     canvas->save();
70     canvas->clipRect(dstRect);
71     canvas->drawImage(image, 0, 0);
72     canvas->restore();
73     canvas->drawRect(dstRect, borderPaint);
74
75     dstRect.offset(SkIntToScalar(150), 0);
76     int hw = width / 2;
77     int hh = height / 2;
78     SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
79     canvas->drawImageRect(image, subset, dstRect, SkSamplingOptions(), nullptr,
80                           SkCanvas::kStrict_SrcRectConstraint);
81     canvas->drawRect(dstRect, borderPaint);
82
83     dstRect.offset(SkIntToScalar(150), 0);
84     canvas->drawImageRect(image, dstRect, SkSamplingOptions(), nullptr);
85     canvas->drawRect(dstRect, borderPaint);
86 }
87
88 class VeryLargeBitmapGM : public skiagm::GM {
89     ImageMakerProc  fProc;
90     const char*     fName;
91
92 public:
93     VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
94
95 private:
96     SkString onShortName() override { return SkString(fName); }
97
98     SkISize onISize() override { return {500, 600}; }
99
100     void onDraw(SkCanvas* canvas) override {
101         int veryBig = 65*1024; // 64K < size
102         int big = 33*1024;     // 32K < size < 64K
103         // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
104         int medium = 5*1024;
105         int small = 150;
106
107         SkColor colors[2];
108
109         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
110         colors[0] = SK_ColorRED;
111         colors[1] = SK_ColorGREEN;
112         show_image(canvas, small, small, colors, fProc);
113         canvas->translate(0, SkIntToScalar(150));
114
115         colors[0] = SK_ColorBLUE;
116         colors[1] = SK_ColorMAGENTA;
117         show_image(canvas, big, small, colors, fProc);
118         canvas->translate(0, SkIntToScalar(150));
119
120         colors[0] = SK_ColorMAGENTA;
121         colors[1] = SK_ColorYELLOW;
122         show_image(canvas, medium, medium, colors, fProc);
123         canvas->translate(0, SkIntToScalar(150));
124
125         colors[0] = SK_ColorGREEN;
126         colors[1] = SK_ColorYELLOW;
127         // This used to be big enough that we didn't draw on CPU, but now we do.
128         show_image(canvas, veryBig, small, colors, fProc);
129     }
130 };
131
132 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
133 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )