Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / tablecolorfilter.cpp
1 /*
2  * Copyright 2011 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/SkImageFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkGradientShader.h"
26 #include "include/effects/SkImageFilters.h"
27 #include "include/effects/SkTableColorFilter.h"
28
29 #include <math.h>
30 #include <utility>
31
32 static sk_sp<SkShader> make_shader0(int w, int h) {
33     SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
34     SkColor colors[] = {
35         SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
36         SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
37     };
38     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
39                                         SkTileMode::kClamp);
40 }
41 static void make_bm0(SkBitmap* bm) {
42     int W = 120;
43     int H = 120;
44     bm->allocN32Pixels(W, H);
45     bm->eraseColor(SK_ColorTRANSPARENT);
46
47     SkCanvas canvas(*bm);
48     SkPaint paint;
49     paint.setShader(make_shader0(W, H));
50     canvas.drawPaint(paint);
51 }
52 static sk_sp<SkShader> make_shader1(int w, int h) {
53     SkScalar cx = SkIntToScalar(w)/2;
54     SkScalar cy = SkIntToScalar(h)/2;
55     SkColor colors[] = {
56         SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
57     };
58     return SkGradientShader::MakeRadial(SkPoint::Make(cx, cy), cx, colors, nullptr,
59                                         SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
60 }
61 static void make_bm1(SkBitmap* bm) {
62     int W = 120;
63     int H = 120;
64     SkScalar cx = SkIntToScalar(W)/2;
65     SkScalar cy = SkIntToScalar(H)/2;
66     bm->allocN32Pixels(W, H);
67     bm->eraseColor(SK_ColorTRANSPARENT);
68
69     SkCanvas canvas(*bm);
70     SkPaint paint;
71     paint.setShader(make_shader1(W, H));
72     paint.setAntiAlias(true);
73     canvas.drawCircle(cx, cy, cx, paint);
74 }
75
76 static void make_table0(uint8_t table[]) {
77     for (int i = 0; i < 256; ++i) {
78         int n = i >> 5;
79         table[i] = (n << 5) | (n << 2) | (n >> 1);
80     }
81 }
82 static void make_table1(uint8_t table[]) {
83     for (int i = 0; i < 256; ++i) {
84         table[i] = i * i / 255;
85     }
86 }
87 static void make_table2(uint8_t table[]) {
88     for (int i = 0; i < 256; ++i) {
89         float fi = i / 255.0f;
90         table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
91     }
92 }
93
94 static sk_sp<SkColorFilter> make_null_cf() {
95     return nullptr;
96 }
97
98 static sk_sp<SkColorFilter> make_cf0() {
99     uint8_t table[256]; make_table0(table);
100     return SkTableColorFilter::Make(table);
101 }
102 static sk_sp<SkColorFilter> make_cf1() {
103     uint8_t table[256]; make_table1(table);
104     return SkTableColorFilter::Make(table);
105 }
106 static sk_sp<SkColorFilter> make_cf2() {
107     uint8_t table[256]; make_table2(table);
108     return SkTableColorFilter::Make(table);
109 }
110 static sk_sp<SkColorFilter> make_cf3() {
111     uint8_t table0[256]; make_table0(table0);
112     uint8_t table1[256]; make_table1(table1);
113     uint8_t table2[256]; make_table2(table2);
114     return SkTableColorFilter::MakeARGB(nullptr, table0, table1, table2);
115 }
116
117 class TableColorFilterGM : public skiagm::GM {
118 public:
119     TableColorFilterGM() {}
120
121 protected:
122     SkString onShortName() override {
123         return SkString("tablecolorfilter");
124     }
125
126     SkISize onISize() override {
127         return {700, 1650};
128     }
129
130     void onDraw(SkCanvas* canvas) override {
131         canvas->drawColor(0xFFDDDDDD);
132         canvas->translate(20, 20);
133
134         static sk_sp<SkColorFilter> (*gColorFilterMakers[])() = {
135             make_null_cf, make_cf0, make_cf1, make_cf2, make_cf3
136         };
137         static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
138
139         // This test will be done once for each bitmap with the results stacked vertically.
140         // For a single bitmap the resulting image will be the following:
141         //  - A first line with the original bitmap, followed by the image drawn once
142         //  with each of the N color filters
143         //  - N lines of the bitmap drawn N times, this will cover all N*N combinations of
144         //  pair of color filters in order to test the collapsing of consecutive table
145         //  color filters.
146         //
147         //  Here is a graphical representation of the result for 2 bitmaps and 2 filters
148         //  with the number corresponding to the number of filters the bitmap goes through:
149         //
150         //  --bitmap1
151         //  011
152         //  22
153         //  22
154         //  --bitmap2
155         //  011
156         //  22
157         //  22
158
159         SkScalar x = 0, y = 0;
160         for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
161             SkBitmap bm;
162             gBitmapMakers[bitmapMaker](&bm);
163
164             SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
165             SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
166
167             // Draw the first element of the first line
168             x = 0;
169             SkPaint paint;
170             SkSamplingOptions sampling;
171
172             canvas->drawImage(bm.asImage(), x, y);
173
174             // Draws the rest of the first line for this bitmap
175             // each draw being at xOffset of the previous one
176             for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
177                 x += xOffset;
178                 paint.setColorFilter(gColorFilterMakers[i]());
179                 canvas->drawImage(bm.asImage(), x, y, sampling, &paint);
180             }
181
182             paint.setColorFilter(nullptr);
183
184             for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
185                 sk_sp<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
186                 sk_sp<SkImageFilter> imageFilter1(SkImageFilters::ColorFilter(
187                         std::move(colorFilter1), nullptr));
188
189                 // Move down to the next line and draw it
190                 // each draw being at xOffset of the previous one
191                 y += yOffset;
192                 x = 0;
193                 for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
194                     sk_sp<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
195                     sk_sp<SkImageFilter> imageFilter2(SkImageFilters::ColorFilter(
196                             std::move(colorFilter2), imageFilter1, nullptr));
197                     paint.setImageFilter(std::move(imageFilter2));
198                     canvas->drawImage(bm.asImage(), x, y, sampling, &paint);
199                     x += xOffset;
200                 }
201             }
202
203             // Move down one line to the beginning of the block for next bitmap
204             y += yOffset;
205         }
206     }
207
208 private:
209     using INHERITED = GM;
210 };
211 DEF_GM( return new TableColorFilterGM; )
212
213 //////////////////////////////////////////////////////////////////////////////
214
215 class ComposeColorFilterGM : public skiagm::GM {
216     enum {
217         COLOR_COUNT = 3,
218         MODE_COUNT = 4,
219     };
220     const SkColor*      fColors;
221     const SkBlendMode*  fModes;
222     const char*         fName;
223
224 public:
225     ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[], const char* name)
226         : fColors(colors), fModes(modes), fName(name) {}
227
228 private:
229     SkString onShortName() override { return SkString(fName); }
230
231     SkISize onISize() override { return {790, 790}; }
232
233     void onDraw(SkCanvas* canvas) override {
234         SkBitmap bm;
235         make_bm1(&bm);
236
237         canvas->drawColor(0xFFDDDDDD);
238
239         const int MODES = MODE_COUNT * COLOR_COUNT;
240         sk_sp<SkColorFilter> filters[MODES];
241         int index = 0;
242         for (int i = 0; i < MODE_COUNT; ++i) {
243             for (int j = 0; j < COLOR_COUNT; ++j) {
244                 filters[index++] = SkColorFilters::Blend(fColors[j], fModes[i]);
245             }
246         }
247
248         SkPaint paint;
249         paint.setShader(make_shader1(50, 50));
250         SkRect r = SkRect::MakeWH(50, 50);
251         const SkScalar spacer = 10;
252
253         canvas->translate(spacer, spacer);
254
255         canvas->drawRect(r, paint); // orig
256
257         for (int i = 0; i < MODES; ++i) {
258             paint.setColorFilter(filters[i]);
259
260             canvas->save();
261             canvas->translate((i + 1) * (r.width() + spacer), 0);
262             canvas->drawRect(r, paint);
263             canvas->restore();
264
265             canvas->save();
266             canvas->translate(0, (i + 1) * (r.width() + spacer));
267             canvas->drawRect(r, paint);
268             canvas->restore();
269         }
270
271         canvas->translate(r.width() + spacer, r.width() + spacer);
272
273         for (int y = 0; y < MODES; ++y) {
274             canvas->save();
275             for (int x = 0; x < MODES; ++x) {
276                 paint.setColorFilter(filters[y]->makeComposed(filters[x]));
277                 canvas->drawRect(r, paint);
278                 canvas->translate(r.width() + spacer, 0);
279             }
280             canvas->restore();
281             canvas->translate(0, r.height() + spacer);
282         }
283     }
284 };
285
286 const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
287 const SkBlendMode gModes0[] = {
288     SkBlendMode::kOverlay,
289     SkBlendMode::kDarken,
290     SkBlendMode::kColorBurn,
291     SkBlendMode::kExclusion,
292 };
293 DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "colorcomposefilter_wacky"); )
294
295 const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
296 const SkBlendMode gModes1[] = {
297     SkBlendMode::kSrcOver,
298     SkBlendMode::kXor,
299     SkBlendMode::kDstOut,
300     SkBlendMode::kSrcATop,
301 };
302 DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "colorcomposefilter_alpha"); )