Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / surface.cpp
1 /*
2  * Copyright 2014 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/SkFont.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.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/SkSurfaceProps.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypeface.h"
28 #include "include/core/SkTypes.h"
29 #include "include/effects/SkGradientShader.h"
30 #include "include/gpu/GrDirectContext.h"
31 #include "include/gpu/GrRecordingContext.h"
32 #include "include/utils/SkTextUtils.h"
33 #include "tools/ToolUtils.h"
34 #include "tools/gpu/BackendSurfaceFactory.h"
35
36 #define W 200
37 #define H 100
38
39 static sk_sp<SkShader> make_shader() {
40     int a = 0x99;
41     int b = 0xBB;
42     SkPoint pts[] = { { 0, 0 }, { W, H } };
43     SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
44     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
45 }
46
47 static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
48                                      const SkImageInfo& info,
49                                      SkPixelGeometry geo) {
50     SkSurfaceProps props(0, geo);
51     if (ctx) {
52         return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
53     } else {
54         return SkSurface::MakeRaster(info, &props);
55     }
56 }
57
58 static void test_draw(SkCanvas* canvas, const char label[]) {
59     SkPaint paint;
60
61     paint.setAntiAlias(true);
62     paint.setDither(true);
63
64     paint.setShader(make_shader());
65     canvas->drawRect(SkRect::MakeWH(W, H), paint);
66     paint.setShader(nullptr);
67
68     paint.setColor(SK_ColorWHITE);
69     SkFont font(ToolUtils::create_portable_typeface(), 32);
70     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
71     SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
72                             SkTextUtils::kCenter_Align);
73 }
74
75 class SurfacePropsGM : public skiagm::GM {
76 public:
77     SurfacePropsGM() {}
78
79 protected:
80     SkString onShortName() override {
81         return SkString("surfaceprops");
82     }
83
84     SkISize onISize() override {
85         return SkISize::Make(W, H * 5);
86     }
87
88     void onDraw(SkCanvas* canvas) override {
89         auto ctx = canvas->recordingContext();
90
91         // must be opaque to have a hope of testing LCD text
92         const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
93
94         const struct {
95             SkPixelGeometry fGeo;
96             const char*     fLabel;
97         } recs[] = {
98             { kUnknown_SkPixelGeometry, "Unknown" },
99             { kRGB_H_SkPixelGeometry,   "RGB_H" },
100             { kBGR_H_SkPixelGeometry,   "BGR_H" },
101             { kRGB_V_SkPixelGeometry,   "RGB_V" },
102             { kBGR_V_SkPixelGeometry,   "BGR_V" },
103         };
104
105         SkScalar x = 0;
106         SkScalar y = 0;
107         for (const auto& rec : recs) {
108             auto surface(make_surface(ctx, info, rec.fGeo));
109             if (!surface) {
110                 SkDebugf("failed to create surface! label: %s", rec.fLabel);
111                 continue;
112             }
113             test_draw(surface->getCanvas(), rec.fLabel);
114             surface->draw(canvas, x, y);
115             y += H;
116         }
117     }
118
119 private:
120     using INHERITED = GM;
121 };
122 DEF_GM( return new SurfacePropsGM )
123
124 #ifdef SK_DEBUG
125 static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
126     return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
127 }
128 #endif
129
130 class NewSurfaceGM : public skiagm::GM {
131 public:
132     NewSurfaceGM() {}
133
134 protected:
135     SkString onShortName() override {
136         return SkString("surfacenew");
137     }
138
139     SkISize onISize() override {
140         return SkISize::Make(300, 140);
141     }
142
143     static void drawInto(SkCanvas* canvas) {
144         canvas->drawColor(SK_ColorRED);
145     }
146
147     void onDraw(SkCanvas* canvas) override {
148         SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
149
150         auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
151         drawInto(surf->getCanvas());
152
153         sk_sp<SkImage> image(surf->makeImageSnapshot());
154         canvas->drawImage(image, 10, 10);
155
156         auto surf2(surf->makeSurface(info));
157         drawInto(surf2->getCanvas());
158
159         // Assert that the props were communicated transitively through the first image
160         SkASSERT(equal(surf->props(), surf2->props()));
161
162         sk_sp<SkImage> image2(surf2->makeImageSnapshot());
163         canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
164     }
165
166 private:
167     using INHERITED = GM;
168 };
169 DEF_GM( return new NewSurfaceGM )
170
171 ///////////////////////////////////////////////////////////////////////////////////////////////////
172
173 // The GPU backend may behave differently when images are snapped from wrapped textures and
174 // render targets compared.
175 namespace {
176 enum SurfaceType {
177     kManaged,
178     kBackendTexture,
179     kBackendRenderTarget
180 };
181 }
182
183 static sk_sp<SkSurface> make_surface(const SkImageInfo& ii, SkCanvas* canvas, SurfaceType type) {
184     GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());
185     switch (type) {
186         case kManaged:
187             return ToolUtils::makeSurface(canvas, ii);
188         case kBackendTexture:
189             if (!direct) {
190                 return nullptr;
191             }
192             return sk_gpu_test::MakeBackendTextureSurface(direct, ii, kTopLeft_GrSurfaceOrigin, 1);
193         case kBackendRenderTarget:
194             return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
195                                                                ii,
196                                                                kTopLeft_GrSurfaceOrigin,
197                                                                1);
198     }
199     return nullptr;
200 }
201
202 using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
203
204 #define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H)            \
205     DEF_SIMPLE_GM(name, canvas, W, H) {                             \
206         auto make = [canvas](const SkImageInfo& ii) {               \
207             return make_surface(ii, canvas, SurfaceType::kManaged); \
208         };                                                          \
209         main(canvas, MakeSurfaceFn(make));                          \
210     }
211
212 #define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H)                                \
213     DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) {                                       \
214         GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());                \
215         if (!direct || direct->abandoned()) {                                                   \
216             *err_msg = "Requires non-abandoned GrDirectContext";                                \
217             return skiagm::DrawResult::kSkip;                                                   \
218         }                                                                                       \
219         auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
220         main(canvas, MakeSurfaceFn(make));                                                      \
221         return skiagm::DrawResult::kOk;                                                         \
222     }
223
224 #define DEF_BET_SURFACE_TEST(name, canvas, main, W, H)                  \
225     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
226                              SurfaceType::kBackendTexture, W, H)
227
228 #define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H)                  \
229     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
230                              SurfaceType::kBackendRenderTarget, W, H)
231
232 // This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
233 // render target.
234 #define DEF_SURFACE_TESTS(name, canvas, W, H)                                  \
235     static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
236     DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
237     DEF_BET_SURFACE_TEST  (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
238     DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
239     static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
240
241 DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
242     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
243     sk_sp<SkSurface> surf = make(info);
244
245     surf->getCanvas()->clear(SK_ColorRED);
246     // its important that image survives longer than the next draw, so the surface will see
247     // an outstanding image, and have to decide if it should retain or discard those pixels
248     sk_sp<SkImage> image = surf->makeImageSnapshot();
249
250     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
251     // it should not (we need the previous red pixels).
252     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
253     surf->getCanvas()->clear(SK_ColorBLUE);
254
255     // expect to see two rects: blue | red
256     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
257 }
258
259 // Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
260 DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
261     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
262     sk_sp<SkSurface> surf = make(info);
263
264     surf->getCanvas()->clear(SK_ColorBLUE);
265     // its important that image survives longer than the next draw, so the surface will see
266     // an outstanding image, and have to decide if it should retain or discard those pixels
267     sk_sp<SkImage> image = surf->makeImageSnapshot();
268
269     surf->getCanvas()->clear(SK_ColorRED);
270     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
271     // it should not (we need the previous red pixels).
272     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
273     surf->getCanvas()->drawImage(image, 0, 0);
274
275     // expect to see two rects: blue | red
276     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
277 }
278
279 DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
280     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
281     sk_sp<SkSurface> surf = make(info);
282
283     surf->getCanvas()->clear(SK_ColorRED);
284     sk_sp<SkImage> image = surf->makeImageSnapshot();
285     // expect to see just red
286     canvas->drawImage(std::move(image), 0, 0);
287 }
288
289 // Like simple_snap_image but the surface dies before the image.
290 DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
291     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
292     sk_sp<SkSurface> surf = make(info);
293
294     surf->getCanvas()->clear(SK_ColorRED);
295     sk_sp<SkImage> image = surf->makeImageSnapshot();
296     surf.reset();
297     // expect to see just red
298     canvas->drawImage(std::move(image), 0, 0);
299 }
300
301 DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
302     auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
303                       ? kRGBA_8888_SkColorType
304                       : canvas->imageInfo().colorType();
305     auto ii = SkImageInfo::Make({32, 32},
306                                 ct,
307                                 kPremul_SkAlphaType,
308                                 canvas->imageInfo().refColorSpace());
309     auto surface = SkSurface::MakeRaster(ii);
310
311     auto nextImage = [&](SkColor color) {
312         surface->getCanvas()->clear(color);
313         SkPaint paint;
314         paint.setColor(~color | 0xFF000000);
315         surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
316                                                         surface->height()*2/5.f,
317                                                         surface->width() *3/5.f,
318                                                         surface->height()*3/5.f),
319                                     paint);
320         return surface->makeImageSnapshot()->withDefaultMipmaps();
321     };
322
323     static constexpr int kPad = 8;
324     static const SkSamplingOptions kSampling{SkFilterMode::kLinear, SkMipmapMode::kLinear};
325
326     canvas->save();
327     for (int y = 0; y < 3; ++y) {
328         canvas->save();
329         SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
330         for (int x = 0; x < 2; ++x) {
331             auto image = nextImage(kColors[x]);
332             canvas->drawImage(image, 0, 0, kSampling);
333             canvas->translate(ii.width() + kPad, 0);
334         }
335         canvas->restore();
336         canvas->translate(0, ii.width() + kPad);
337         canvas->scale(.4f, .4f);
338     }
339     canvas->restore();
340 }
341
342 DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
343     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
344     sk_sp<SkSurface> surf = make(info);
345     surf->getCanvas()->clear(SK_ColorRED);
346     // its important that image survives longer than the next draw, so the surface will see
347     // an outstanding image, and have to decide if it should retain or discard those pixels
348     sk_sp<SkImage> image = surf->makeImageSnapshot();
349
350     // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
351     // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
352     // with a non-opaque paint.
353     SkPaint paint;
354     paint.setAlphaf(0.25f);
355     surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
356     surf->getCanvas()->clear(SK_ColorBLUE);
357     surf->getCanvas()->restore();
358
359     // expect to see two rects: blue blended on red
360     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
361 }
362
363 DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
364     SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
365     auto surf = make(info);
366
367     const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
368
369     // noisy background
370     {
371         SkPoint pts[] = {{0, 0}, {40, 50}};
372         SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
373         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
374         SkPaint paint;
375         paint.setShader(sh);
376         surf->getCanvas()->drawPaint(paint);
377     }
378
379     // save away the right-hand strip, then clear it
380     sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
381     {
382         SkPaint paint;
383         paint.setBlendMode(SkBlendMode::kClear);
384         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
385     }
386
387     // draw the "foreground"
388     {
389         SkPaint paint;
390         paint.setColor(SK_ColorGREEN);
391         SkRect r = { 0, 10, 256, 35 };
392         while (r.fBottom < 256) {
393             surf->getCanvas()->drawRect(r, paint);
394             r.offset(0, r.height() * 2);
395         }
396     }
397
398     // apply the "fade"
399     {
400         SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
401         SkColor colors[] = {0xFF000000, 0};
402         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
403         SkPaint paint;
404         paint.setShader(sh);
405         paint.setBlendMode(SkBlendMode::kDstIn);
406         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
407     }
408
409     // restore the original strip, drawing it "under" the current foreground
410     {
411         SkPaint paint;
412         paint.setBlendMode(SkBlendMode::kDstOver);
413         surf->getCanvas()->drawImage(saveImg,
414                                      SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
415                                      SkSamplingOptions(), &paint);
416     }
417
418     // show it on screen
419    surf->draw(canvas, 0, 0);
420 }