1 | /*
|
---|
2 | * Copyright (c) 2020 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 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(paint);
|
---|
39 |
|
---|
40 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
41 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
42 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
43 | gfx_bitmap_alloc_t *, void **);
|
---|
44 | static errno_t testgc_bitmap_destroy(void *);
|
---|
45 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
46 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
47 |
|
---|
48 | static gfx_context_ops_t ops = {
|
---|
49 | .set_color = testgc_set_color,
|
---|
50 | .fill_rect = testgc_fill_rect,
|
---|
51 | .bitmap_create = testgc_bitmap_create,
|
---|
52 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
53 | .bitmap_render = testgc_bitmap_render,
|
---|
54 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
55 | };
|
---|
56 |
|
---|
57 | typedef struct {
|
---|
58 | bool bm_created;
|
---|
59 | bool bm_destroyed;
|
---|
60 | gfx_bitmap_params_t bm_params;
|
---|
61 | void *bm_pixels;
|
---|
62 | gfx_rect_t bm_srect;
|
---|
63 | gfx_coord2_t bm_offs;
|
---|
64 | bool bm_rendered;
|
---|
65 | bool bm_got_alloc;
|
---|
66 | } test_gc_t;
|
---|
67 |
|
---|
68 | typedef struct {
|
---|
69 | test_gc_t *tgc;
|
---|
70 | gfx_bitmap_alloc_t alloc;
|
---|
71 | bool myalloc;
|
---|
72 | } testgc_bitmap_t;
|
---|
73 |
|
---|
74 | /** Paint bevel */
|
---|
75 | PCUT_TEST(bevel)
|
---|
76 | {
|
---|
77 | errno_t rc;
|
---|
78 | gfx_context_t *gc = NULL;
|
---|
79 | test_gc_t tgc;
|
---|
80 | gfx_rect_t rect;
|
---|
81 | gfx_color_t *color1;
|
---|
82 | gfx_color_t *color2;
|
---|
83 | gfx_rect_t inside;
|
---|
84 |
|
---|
85 | memset(&tgc, 0, sizeof(tgc));
|
---|
86 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
87 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
88 |
|
---|
89 | rc = gfx_color_new_rgb_i16(1, 2, 3, &color1);
|
---|
90 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
91 |
|
---|
92 | rc = gfx_color_new_rgb_i16(4, 5, 6, &color2);
|
---|
93 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
94 |
|
---|
95 | /* Paint bevel with NULL 'inside' output parameter */
|
---|
96 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
|
---|
97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
98 |
|
---|
99 | /* Paint bevel with valid 'inside' output parameter */
|
---|
100 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, &inside);
|
---|
101 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
102 |
|
---|
103 | gfx_color_delete(color2);
|
---|
104 | gfx_color_delete(color1);
|
---|
105 | rc = gfx_context_delete(gc);
|
---|
106 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
107 | }
|
---|
108 |
|
---|
109 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
110 | {
|
---|
111 | (void) arg;
|
---|
112 | (void) color;
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
117 | {
|
---|
118 | (void) arg;
|
---|
119 | (void) rect;
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
124 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
125 | {
|
---|
126 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
127 | testgc_bitmap_t *tbm;
|
---|
128 |
|
---|
129 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
130 | if (tbm == NULL)
|
---|
131 | return ENOMEM;
|
---|
132 |
|
---|
133 | if (alloc == NULL) {
|
---|
134 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
135 | sizeof(uint32_t);
|
---|
136 | tbm->alloc.off0 = 0;
|
---|
137 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
138 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
139 | (params->rect.p1.y - params->rect.p0.y));
|
---|
140 | tbm->myalloc = true;
|
---|
141 | if (tbm->alloc.pixels == NULL) {
|
---|
142 | free(tbm);
|
---|
143 | return ENOMEM;
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | tbm->alloc = *alloc;
|
---|
147 | }
|
---|
148 |
|
---|
149 | tbm->tgc = tgc;
|
---|
150 | tgc->bm_created = true;
|
---|
151 | tgc->bm_params = *params;
|
---|
152 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
153 | *rbm = (void *)tbm;
|
---|
154 | return EOK;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
158 | {
|
---|
159 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
160 | if (tbm->myalloc)
|
---|
161 | free(tbm->alloc.pixels);
|
---|
162 | tbm->tgc->bm_destroyed = true;
|
---|
163 | free(tbm);
|
---|
164 | return EOK;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
168 | gfx_coord2_t *offs)
|
---|
169 | {
|
---|
170 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
171 | tbm->tgc->bm_rendered = true;
|
---|
172 | tbm->tgc->bm_srect = *srect;
|
---|
173 | tbm->tgc->bm_offs = *offs;
|
---|
174 | return EOK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
178 | {
|
---|
179 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
180 | *alloc = tbm->alloc;
|
---|
181 | tbm->tgc->bm_got_alloc = true;
|
---|
182 | return EOK;
|
---|
183 | }
|
---|
184 |
|
---|
185 | PCUT_EXPORT(paint);
|
---|