1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <gfx/context.h>
|
---|
30 | #include <gfx/coord.h>
|
---|
31 | #include <mem.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <stdbool.h>
|
---|
34 | #include <ui/paint.h>
|
---|
35 | #include <ui/resource.h>
|
---|
36 |
|
---|
37 | PCUT_INIT;
|
---|
38 |
|
---|
39 | PCUT_TEST_SUITE(paint);
|
---|
40 |
|
---|
41 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
---|
42 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
43 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
44 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
45 | gfx_bitmap_alloc_t *, void **);
|
---|
46 | static errno_t testgc_bitmap_destroy(void *);
|
---|
47 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
48 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
49 |
|
---|
50 | static gfx_context_ops_t ops = {
|
---|
51 | .set_clip_rect = testgc_set_clip_rect,
|
---|
52 | .set_color = testgc_set_color,
|
---|
53 | .fill_rect = testgc_fill_rect,
|
---|
54 | .bitmap_create = testgc_bitmap_create,
|
---|
55 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
56 | .bitmap_render = testgc_bitmap_render,
|
---|
57 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
58 | };
|
---|
59 |
|
---|
60 | typedef struct {
|
---|
61 | bool bm_created;
|
---|
62 | bool bm_destroyed;
|
---|
63 | gfx_bitmap_params_t bm_params;
|
---|
64 | void *bm_pixels;
|
---|
65 | gfx_rect_t bm_srect;
|
---|
66 | gfx_coord2_t bm_offs;
|
---|
67 | bool bm_rendered;
|
---|
68 | bool bm_got_alloc;
|
---|
69 | } test_gc_t;
|
---|
70 |
|
---|
71 | typedef struct {
|
---|
72 | test_gc_t *tgc;
|
---|
73 | gfx_bitmap_alloc_t alloc;
|
---|
74 | bool myalloc;
|
---|
75 | } testgc_bitmap_t;
|
---|
76 |
|
---|
77 | /** Paint bevel */
|
---|
78 | PCUT_TEST(bevel)
|
---|
79 | {
|
---|
80 | errno_t rc;
|
---|
81 | gfx_context_t *gc = NULL;
|
---|
82 | test_gc_t tgc;
|
---|
83 | gfx_rect_t rect;
|
---|
84 | gfx_color_t *color1;
|
---|
85 | gfx_color_t *color2;
|
---|
86 | gfx_rect_t inside;
|
---|
87 |
|
---|
88 | memset(&tgc, 0, sizeof(tgc));
|
---|
89 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
90 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
91 |
|
---|
92 | rc = gfx_color_new_rgb_i16(1, 2, 3, &color1);
|
---|
93 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
94 |
|
---|
95 | rc = gfx_color_new_rgb_i16(4, 5, 6, &color2);
|
---|
96 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
97 |
|
---|
98 | /* Paint bevel with NULL 'inside' output parameter */
|
---|
99 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
|
---|
100 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
101 |
|
---|
102 | /* Paint bevel with valid 'inside' output parameter */
|
---|
103 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, &inside);
|
---|
104 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
105 |
|
---|
106 | gfx_color_delete(color2);
|
---|
107 | gfx_color_delete(color1);
|
---|
108 | rc = gfx_context_delete(gc);
|
---|
109 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Paint inset frame */
|
---|
113 | PCUT_TEST(inset_frame)
|
---|
114 | {
|
---|
115 | errno_t rc;
|
---|
116 | gfx_context_t *gc = NULL;
|
---|
117 | ui_resource_t *resource = NULL;
|
---|
118 | test_gc_t tgc;
|
---|
119 | gfx_rect_t rect;
|
---|
120 | gfx_rect_t inside;
|
---|
121 |
|
---|
122 | memset(&tgc, 0, sizeof(tgc));
|
---|
123 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
124 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
125 |
|
---|
126 | rc = ui_resource_create(gc, false, &resource);
|
---|
127 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
128 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
129 |
|
---|
130 | /* Paint inset frame with NULL 'inside' output parameter */
|
---|
131 | rc = ui_paint_inset_frame(resource, &rect, NULL);
|
---|
132 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
133 |
|
---|
134 | /* Paint inset frame with valid 'inside' output parameter */
|
---|
135 | rc = ui_paint_inset_frame(resource, &rect, &inside);
|
---|
136 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
137 |
|
---|
138 | ui_resource_destroy(resource);
|
---|
139 | rc = gfx_context_delete(gc);
|
---|
140 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Paint filled circle */
|
---|
144 | PCUT_TEST(filled_circle)
|
---|
145 | {
|
---|
146 | errno_t rc;
|
---|
147 | gfx_context_t *gc = NULL;
|
---|
148 | test_gc_t tgc;
|
---|
149 | gfx_coord2_t center;
|
---|
150 |
|
---|
151 | memset(&tgc, 0, sizeof(tgc));
|
---|
152 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
153 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
154 |
|
---|
155 | /* Paint filled circle / upper-left half */
|
---|
156 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_upleft);
|
---|
157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
158 |
|
---|
159 | /* Paint filled circle / lower-right half */
|
---|
160 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_lowright);
|
---|
161 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
162 |
|
---|
163 | /* Paint entire filled circle */
|
---|
164 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_entire);
|
---|
165 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
166 |
|
---|
167 | rc = gfx_context_delete(gc);
|
---|
168 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
169 | }
|
---|
170 |
|
---|
171 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
172 | {
|
---|
173 | (void) arg;
|
---|
174 | (void) rect;
|
---|
175 | return EOK;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
179 | {
|
---|
180 | (void) arg;
|
---|
181 | (void) color;
|
---|
182 | return EOK;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
186 | {
|
---|
187 | (void) arg;
|
---|
188 | (void) rect;
|
---|
189 | return EOK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
193 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
194 | {
|
---|
195 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
196 | testgc_bitmap_t *tbm;
|
---|
197 |
|
---|
198 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
199 | if (tbm == NULL)
|
---|
200 | return ENOMEM;
|
---|
201 |
|
---|
202 | if (alloc == NULL) {
|
---|
203 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
204 | sizeof(uint32_t);
|
---|
205 | tbm->alloc.off0 = 0;
|
---|
206 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
207 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
208 | (params->rect.p1.y - params->rect.p0.y));
|
---|
209 | tbm->myalloc = true;
|
---|
210 | if (tbm->alloc.pixels == NULL) {
|
---|
211 | free(tbm);
|
---|
212 | return ENOMEM;
|
---|
213 | }
|
---|
214 | } else {
|
---|
215 | tbm->alloc = *alloc;
|
---|
216 | }
|
---|
217 |
|
---|
218 | tbm->tgc = tgc;
|
---|
219 | tgc->bm_created = true;
|
---|
220 | tgc->bm_params = *params;
|
---|
221 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
222 | *rbm = (void *)tbm;
|
---|
223 | return EOK;
|
---|
224 | }
|
---|
225 |
|
---|
226 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
227 | {
|
---|
228 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
229 | if (tbm->myalloc)
|
---|
230 | free(tbm->alloc.pixels);
|
---|
231 | tbm->tgc->bm_destroyed = true;
|
---|
232 | free(tbm);
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
237 | gfx_coord2_t *offs)
|
---|
238 | {
|
---|
239 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
240 | tbm->tgc->bm_rendered = true;
|
---|
241 | tbm->tgc->bm_srect = *srect;
|
---|
242 | tbm->tgc->bm_offs = *offs;
|
---|
243 | return EOK;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
247 | {
|
---|
248 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
249 | *alloc = tbm->alloc;
|
---|
250 | tbm->tgc->bm_got_alloc = true;
|
---|
251 | return EOK;
|
---|
252 | }
|
---|
253 |
|
---|
254 | PCUT_EXPORT(paint);
|
---|