Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / flippity.cpp
1 /*
2  * Copyright 2017 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/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkSurface.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/gpu/GrDirectContext.h"
27 #include "include/gpu/GrRecordingContext.h"
28 #include "include/gpu/GrTypes.h"
29 #include "include/private/SkTArray.h"
30 #include "src/gpu/ganesh/GrDirectContextPriv.h"
31 #include "src/gpu/ganesh/GrPixmap.h"
32 #include "src/image/SkImage_Base.h"
33 #include "src/image/SkImage_Gpu.h"
34 #include "tools/ToolUtils.h"
35 #include "tools/gpu/ProxyUtils.h"
36
37 #include <string.h>
38 #include <utility>
39
40 static const int kNumMatrices = 6;
41 static const int kImageSize = 128;
42 static const int kLabelSize = 32;
43 static const int kNumLabels = 4;
44 static const int kInset = 16;
45
46 static const int kCellSize = kImageSize+2*kLabelSize;
47 static const int kGMWidth  = kNumMatrices*kCellSize;
48 static const int kGMHeight = 4*kCellSize;
49
50 static const SkPoint kPoints[kNumLabels] = {
51     {          0, kImageSize },     // LL
52     { kImageSize, kImageSize },     // LR
53     {          0,          0 },     // UL
54     { kImageSize,          0 },     // UR
55 };
56
57 static const SkMatrix kUVMatrices[kNumMatrices] = {
58     SkMatrix::MakeAll( 0, -1, 1,
59                       -1,  0, 1,
60                        0,  0, 1),
61     SkMatrix::MakeAll( 1,  0, 0,
62                        0, -1, 1,
63                        0,  0, 1),
64     // flip x
65     SkMatrix::MakeAll(-1,  0, 1,
66                        0,  1, 0,
67                        0,  0, 1),
68     SkMatrix::MakeAll( 0,  1, 0,
69                       -1,  0, 1,
70                        0,  0, 1),
71     // flip both x & y == rotate 180
72     SkMatrix::MakeAll(-1,  0, 1,
73                        0, -1, 1,
74                        0,  0, 1),
75     // identity
76     SkMatrix::MakeAll(1,  0, 0,
77                       0,  1, 0,
78                       0,  0, 1)
79 };
80
81
82 // Create a fixed size text label like "LL" or "LR".
83 static sk_sp<SkImage> make_text_image(GrDirectContext* dContext, const char* text, SkColor color) {
84     SkPaint paint;
85     paint.setAntiAlias(true);
86     paint.setColor(color);
87
88     SkFont font;
89     font.setEdging(SkFont::Edging::kAntiAlias);
90     font.setTypeface(ToolUtils::create_portable_typeface());
91     font.setSize(32);
92
93     SkRect bounds;
94     font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
95     const SkMatrix mat = SkMatrix::RectToRect(bounds, SkRect::MakeWH(kLabelSize, kLabelSize));
96
97     const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize);
98     sk_sp<SkSurface> surf = SkSurface::MakeRaster(ii);
99
100     SkCanvas* canvas = surf->getCanvas();
101
102     canvas->clear(SK_ColorWHITE);
103     canvas->concat(mat);
104     canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint);
105
106     sk_sp<SkImage> image = surf->makeImageSnapshot();
107
108     return image->makeTextureImage(dContext);
109 }
110
111 // Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left
112 // or top-left.
113 static sk_sp<SkImage> make_reference_image(GrDirectContext* dContext,
114                                            const SkTArray<sk_sp<SkImage>>& labels,
115                                            bool bottomLeftOrigin) {
116     SkASSERT(kNumLabels == labels.count());
117
118     SkImageInfo ii =
119             SkImageInfo::Make(kImageSize, kImageSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
120     SkBitmap bm;
121     bm.allocPixels(ii);
122     SkCanvas canvas(bm);
123
124     canvas.clear(SK_ColorWHITE);
125     for (int i = 0; i < kNumLabels; ++i) {
126         canvas.drawImage(labels[i],
127                          0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset,
128                          0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset);
129     }
130
131     auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
132
133     auto view = sk_gpu_test::MakeTextureProxyViewFromData(dContext, GrRenderable::kNo, origin,
134                                                           bm.pixmap());
135     if (!view) {
136         return nullptr;
137     }
138
139     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(dContext),
140                                    kNeedNewImageUniqueID,
141                                    std::move(view),
142                                    ii.colorInfo());
143 }
144
145 // Here we're converting from a matrix that is intended for UVs to a matrix that is intended
146 // for rect geometry used for a drawImage call. They are, in some sense, inverses of each
147 // other but we also need a scale to map from the [0..1] uv range to the actual size of
148 // image.
149 static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) {
150
151     const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
152
153     SkMatrix tmp = uvMat;
154     tmp.preConcat(yFlip);
155     tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize);
156
157     tmp.postConcat(yFlip);
158     tmp.postScale(kImageSize, kImageSize);
159
160     return tmp.invert(geomMat);
161 }
162
163 // This GM exercises drawImage with a set of matrices that use an unusual amount of flips and
164 // rotates.
165 class FlippityGM : public skiagm::GM {
166 public:
167     FlippityGM() {
168         this->setBGColor(0xFFCCCCCC);
169     }
170
171 private:
172     SkString onShortName() override {
173         return SkString("flippity");
174     }
175
176     SkISize onISize() override {
177         return SkISize::Make(kGMWidth, kGMHeight);
178     }
179
180     // Draw the reference image and the four corner labels in the matrix's coordinate space
181     void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex,
182                                       bool drawSubset, bool drawScaled) {
183         static const SkRect kSubsets[kNumMatrices] = {
184             SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize),
185             SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset),
186             SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize),
187             SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset),
188             SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset),
189             SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset),
190         };
191
192         SkMatrix imageGeomMat;
193         SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex]));
194
195         canvas->save();
196
197             // draw the reference image
198             canvas->concat(imageGeomMat);
199             if (drawSubset) {
200                 canvas->drawImageRect(image, kSubsets[matIndex],
201                                       drawScaled ? SkRect::MakeWH(kImageSize, kImageSize)
202                                                  : kSubsets[matIndex],
203                                       SkSamplingOptions(), nullptr,
204                                       SkCanvas::kFast_SrcRectConstraint);
205             } else {
206                 canvas->drawImage(image, 0, 0);
207             }
208
209             // draw the labels
210             for (int i = 0; i < kNumLabels; ++i) {
211                 canvas->drawImage(fLabels[i],
212                                     0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX,
213                                     0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY);
214             }
215         canvas->restore();
216     }
217
218     void drawRow(SkCanvas* canvas, bool bottomLeftImage, bool drawSubset, bool drawScaled) {
219
220         canvas->save();
221             canvas->translate(kLabelSize, kLabelSize);
222
223             for (int i = 0; i < kNumMatrices; ++i) {
224                 this->drawImageWithMatrixAndLabels(canvas, fReferenceImages[bottomLeftImage].get(),
225                                                    i, drawSubset, drawScaled);
226                 canvas->translate(kCellSize, 0);
227             }
228         canvas->restore();
229     }
230
231     void makeLabels(GrDirectContext* dContext) {
232         if (fLabels.count()) {
233             return;
234         }
235
236         static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" };
237
238         static const SkColor kLabelColors[kNumLabels] = {
239             SK_ColorRED,
240             SK_ColorGREEN,
241             SK_ColorBLUE,
242             SK_ColorCYAN
243         };
244
245         for (int i = 0; i < kNumLabels; ++i) {
246             fLabels.push_back(make_text_image(dContext, kLabelText[i], kLabelColors[i]));
247         }
248         SkASSERT(kNumLabels == fLabels.count());
249     }
250
251     DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
252         if (!dContext || dContext->abandoned()) {
253             *errorMsg = "DirectContext required to create reference images";
254             return DrawResult::kSkip;
255         }
256
257         this->makeLabels(dContext);
258         fReferenceImages[0] = make_reference_image(dContext, fLabels, false);
259         fReferenceImages[1] = make_reference_image(dContext, fLabels, true);
260         if (!fReferenceImages[0] || !fReferenceImages[1]) {
261             *errorMsg = "Failed to create reference images.";
262             return DrawResult::kFail;
263         }
264
265         return DrawResult::kOk;
266     }
267
268     void onGpuTeardown() override {
269         fLabels.reset();
270         fReferenceImages[0] = fReferenceImages[1] = nullptr;
271     }
272
273     void onDraw(SkCanvas* canvas) override {
274         SkASSERT(fReferenceImages[0] && fReferenceImages[1]);
275
276         canvas->save();
277
278         // Top row gets TL image
279         this->drawRow(canvas, false, false, false);
280
281         canvas->translate(0, kCellSize);
282
283         // Bottom row gets BL image
284         this->drawRow(canvas, true, false, false);
285
286         canvas->translate(0, kCellSize);
287
288         // Third row gets subsets of BL images
289         this->drawRow(canvas, true, true, false);
290
291         canvas->translate(0, kCellSize);
292
293         // Fourth row gets scaled subsets of BL images
294         this->drawRow(canvas, true, true, true);
295
296         canvas->restore();
297
298         // separator grid
299         for (int i = 0; i < 4; ++i) {
300             canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint());
301         }
302         for (int i = 0; i < kNumMatrices; ++i) {
303             canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint());
304         }
305     }
306
307 private:
308     SkTArray<sk_sp<SkImage>> fLabels;
309     sk_sp<SkImage> fReferenceImages[2];
310
311     using INHERITED = GM;
312 };
313
314 DEF_GM(return new FlippityGM;)