Fix compiler warnings on windows
[platform/core/graphics/tizenvg.git] / test / testSwCanvasBase.cpp
1 /*
2  * Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <thorvg.h>
24 #include "catch.hpp"
25
26 using namespace tvg;
27
28
29 TEST_CASE("Memory Reservation", "[tvgSwCanvasBase]")
30 {
31     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
32
33     auto canvas = SwCanvas::gen();
34     REQUIRE(canvas);
35
36     //Check Growth / Reduction
37     REQUIRE(canvas->reserve(10) == Result::Success);
38     REQUIRE(canvas->reserve(1000) == Result::Success);
39     REQUIRE(canvas->reserve(100) == Result::Success);
40     REQUIRE(canvas->reserve(0) == Result::Success);
41
42     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
43 }
44
45 TEST_CASE("Pushing Paints", "[tvgSwCanvasBase]")
46 {
47     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
48
49     auto canvas = SwCanvas::gen();
50     REQUIRE(canvas);
51
52     uint32_t buffer[100*100];
53     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ARGB8888) == Result::Success);
54
55     //Try all types of paints.
56     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
57     REQUIRE(canvas->push(Picture::gen()) == Result::Success);
58     REQUIRE(canvas->push(Scene::gen()) == Result::Success);
59
60     //Cases by contexts.
61     REQUIRE(canvas->update() == Result::Success);
62
63     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
64     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
65
66     REQUIRE(canvas->clear() == Result::Success);
67
68     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
69
70     //Negative case 1
71     REQUIRE(canvas->push(nullptr) == Result::MemoryCorruption);
72
73     //Negative case 2
74     std::unique_ptr<Shape> shape6 = nullptr;
75     REQUIRE(canvas->push(move(shape6)) == Result::MemoryCorruption);
76
77     //Negative case 3
78     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
79     REQUIRE(canvas->draw() == Result::Success);
80     REQUIRE(canvas->push(Shape::gen()) == Result::InsufficientCondition);
81
82     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
83 }
84
85 TEST_CASE("Clear", "[tvgSwCanvasBase]")
86 {
87     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
88
89     auto canvas = SwCanvas::gen();
90     REQUIRE(canvas);
91     auto canvas2 = SwCanvas::gen();
92     REQUIRE(canvas2);
93
94     //Try 0: Clear
95     REQUIRE(canvas->clear() == Result::Success);
96     REQUIRE(canvas->clear(false) == Result::Success);
97     REQUIRE(canvas->clear() == Result::Success);
98
99     REQUIRE(canvas2->clear(false) == Result::Success);
100     REQUIRE(canvas2->clear() == Result::Success);
101     REQUIRE(canvas2->clear(false) == Result::Success);
102
103     Shape* ptrs[5];
104
105     //Try 1: Push -> Clear
106     for (int i = 0; i < 5; ++i) {
107         REQUIRE(canvas->push(Shape::gen()) == Result::Success);
108
109         auto shape2 = Shape::gen();
110         REQUIRE(shape2);
111         ptrs[i] = shape2.get();
112
113         REQUIRE(canvas2->push(move(shape2)) == Result::Success);
114     }
115
116     REQUIRE(canvas->clear() == Result::Success);
117     REQUIRE(canvas->clear(false) == Result::Success);
118
119     REQUIRE(canvas2->clear(false) == Result::Success);
120     REQUIRE(canvas2->clear() == Result::Success);
121
122     for (int i = 0; i < 5; ++i) delete(ptrs[i]);
123
124     //Try 2: Push -> Update -> Clear
125     for (int i = 0; i < 5; ++i) {
126         REQUIRE(canvas->push(Shape::gen()) == Result::Success);
127
128         auto shape2 = Shape::gen();
129         REQUIRE(shape2);
130         ptrs[i] = shape2.get();
131
132         REQUIRE(canvas2->push(move(shape2)) == Result::Success);
133     }
134
135     REQUIRE(canvas->update() == Result::Success);
136     REQUIRE(canvas->clear() == Result::Success);
137     REQUIRE(canvas->clear(false) == Result::Success);
138
139     REQUIRE(canvas2->update() == Result::Success);
140     REQUIRE(canvas2->clear(false) == Result::Success);
141     REQUIRE(canvas2->clear() == Result::Success);
142
143     for (int i = 0; i < 5; ++i) delete(ptrs[i]);
144
145     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
146 }
147
148 TEST_CASE("Update", "[tvgSwCanvasBase]")
149 {
150     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
151
152     auto canvas = SwCanvas::gen();
153     REQUIRE(canvas);
154
155     uint32_t buffer[100*100];
156     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ARGB8888) == Result::Success);
157
158     REQUIRE(canvas->update() == Result::InsufficientCondition);
159
160     REQUIRE(canvas->push(Shape::gen()) == Result::Success);
161
162     //No pushed shape
163     auto shape = Shape::gen();
164     REQUIRE(canvas->update(shape.get()) == Result::InvalidArguments);
165
166     //Normal case
167     auto ptr = shape.get();
168     REQUIRE(canvas->push(move(shape)) == Result::Success);
169     REQUIRE(canvas->update(ptr) == Result::Success);
170     REQUIRE(canvas->update() == Result::Success);
171     REQUIRE(canvas->draw() == Result::Success);
172     REQUIRE(canvas->update() == Result::InsufficientCondition);
173
174     REQUIRE(canvas->clear() == Result::Success);
175
176     //Invalid shape
177     REQUIRE(canvas->update(ptr) == Result::InsufficientCondition);
178
179     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
180 }
181
182 TEST_CASE("Synchronized Drawing", "[tvgSwCanvasBase]")
183 {
184     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
185
186     auto canvas = SwCanvas::gen();
187     REQUIRE(canvas);
188
189     REQUIRE(canvas->sync() == Result::InsufficientCondition);
190     REQUIRE(canvas->draw() == Result::InsufficientCondition);
191
192     uint32_t buffer[100*100];
193     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ARGB8888) == Result::Success);
194
195     REQUIRE(canvas->draw() == Result::InsufficientCondition);
196     REQUIRE(canvas->sync() == Result::InsufficientCondition);
197
198     //Invalid Shape
199     auto shape = Shape::gen();
200     REQUIRE(shape);
201     REQUIRE(canvas->push(move(shape)) == Result::Success);
202
203     REQUIRE(canvas->draw() == Result::Success);
204     REQUIRE(canvas->sync() == Result::Success);
205     REQUIRE(canvas->clear() == Result::Success);
206
207     auto shape2 = Shape::gen();
208     REQUIRE(shape2);
209     REQUIRE(shape2->appendRect(0, 0, 100, 100, 0, 0) == Result::Success);
210     REQUIRE(shape2->fill(255, 255, 255, 255) == Result::Success);
211
212     REQUIRE(canvas->push(move(shape2)) == Result::Success);
213     REQUIRE(canvas->draw() == Result::Success);
214     REQUIRE(canvas->sync() == Result::Success);
215
216     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
217 }
218
219 TEST_CASE("Asynchronized Drawing", "[tvgSwCanvasBase]")
220 {
221     //Use multi-threading
222     REQUIRE(Initializer::init(CanvasEngine::Sw, 2) == Result::Success);
223
224     auto canvas = SwCanvas::gen();
225     REQUIRE(canvas);
226
227     uint32_t buffer[100*100];
228     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ARGB8888) == Result::Success);
229
230     for (int i = 0; i < 3; ++i) {
231         auto shape = Shape::gen();
232         REQUIRE(shape);
233         REQUIRE(shape->appendRect(0, 0, 100, 100, 0, 0) == Result::Success);
234         REQUIRE(shape->fill(255, 255, 255, 255) == Result::Success);
235
236         REQUIRE(canvas->push(move(shape)) == Result::Success);
237     }
238
239     REQUIRE(canvas->draw() == Result::Success);
240     REQUIRE(canvas->sync() == Result::Success);
241
242     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
243 }