1 | /*
|
---|
2 | * Copyright (c) 2019 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/bitmap.h>
|
---|
30 | #include <gfx/context.h>
|
---|
31 | #include <mem.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <stdbool.h>
|
---|
34 |
|
---|
35 | PCUT_INIT;
|
---|
36 |
|
---|
37 | PCUT_TEST_SUITE(bitmap);
|
---|
38 |
|
---|
39 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
40 | gfx_bitmap_alloc_t *, void **);
|
---|
41 | static errno_t testgc_bitmap_destroy(void *);
|
---|
42 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
43 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
44 |
|
---|
45 | static gfx_context_ops_t ops = {
|
---|
46 | .bitmap_create = testgc_bitmap_create,
|
---|
47 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
48 | .bitmap_render = testgc_bitmap_render,
|
---|
49 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
50 | };
|
---|
51 |
|
---|
52 | typedef struct {
|
---|
53 | bool bm_created;
|
---|
54 | bool bm_destroyed;
|
---|
55 | gfx_bitmap_params_t bm_params;
|
---|
56 | void *bm_pixels;
|
---|
57 | gfx_rect_t bm_srect;
|
---|
58 | gfx_coord2_t bm_offs;
|
---|
59 | bool bm_rendered;
|
---|
60 | bool bm_got_alloc;
|
---|
61 | } test_gc_t;
|
---|
62 |
|
---|
63 | typedef struct {
|
---|
64 | test_gc_t *tgc;
|
---|
65 | gfx_bitmap_alloc_t alloc;
|
---|
66 | bool myalloc;
|
---|
67 | } testgc_bitmap_t;
|
---|
68 |
|
---|
69 | enum {
|
---|
70 | alloc_pitch = 42,
|
---|
71 | alloc_off0 = 33
|
---|
72 | };
|
---|
73 |
|
---|
74 | PCUT_TEST(create_destroy)
|
---|
75 | {
|
---|
76 | errno_t rc;
|
---|
77 | gfx_context_t *gc = NULL;
|
---|
78 | test_gc_t tgc;
|
---|
79 | gfx_bitmap_params_t params;
|
---|
80 | gfx_bitmap_t *bitmap = NULL;
|
---|
81 |
|
---|
82 | memset(&tgc, 0, sizeof(tgc));
|
---|
83 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
84 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
85 |
|
---|
86 | params.rect.p0.x = 1;
|
---|
87 | params.rect.p0.y = 2;
|
---|
88 | params.rect.p1.x = 3;
|
---|
89 | params.rect.p1.y = 4;
|
---|
90 |
|
---|
91 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
93 | PCUT_ASSERT_NOT_NULL(bitmap);
|
---|
94 | PCUT_ASSERT_TRUE(tgc.bm_created);
|
---|
95 | PCUT_ASSERT_EQUALS(params.rect.p0.x, tgc.bm_params.rect.p0.x);
|
---|
96 | PCUT_ASSERT_EQUALS(params.rect.p0.y, tgc.bm_params.rect.p0.y);
|
---|
97 | PCUT_ASSERT_EQUALS(params.rect.p1.x, tgc.bm_params.rect.p1.x);
|
---|
98 | PCUT_ASSERT_EQUALS(params.rect.p1.y, tgc.bm_params.rect.p1.y);
|
---|
99 |
|
---|
100 | rc = gfx_bitmap_destroy(bitmap);
|
---|
101 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
102 | PCUT_ASSERT_TRUE(tgc.bm_destroyed);
|
---|
103 |
|
---|
104 | rc = gfx_context_delete(gc);
|
---|
105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
106 | }
|
---|
107 |
|
---|
108 | PCUT_TEST(render)
|
---|
109 | {
|
---|
110 | errno_t rc;
|
---|
111 | gfx_context_t *gc = NULL;
|
---|
112 | test_gc_t tgc;
|
---|
113 | gfx_bitmap_params_t params;
|
---|
114 | gfx_bitmap_t *bitmap = NULL;
|
---|
115 | gfx_rect_t srect;
|
---|
116 | gfx_coord2_t offs;
|
---|
117 |
|
---|
118 | memset(&tgc, 0, sizeof(tgc));
|
---|
119 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
120 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
121 |
|
---|
122 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
123 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
124 |
|
---|
125 | srect.p0.x = 1;
|
---|
126 | srect.p0.y = 2;
|
---|
127 | srect.p1.x = 3;
|
---|
128 | srect.p1.y = 4;
|
---|
129 | offs.x = 5;
|
---|
130 | offs.y = 6;
|
---|
131 |
|
---|
132 | rc = gfx_bitmap_render(bitmap, &srect, &offs);
|
---|
133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
134 | PCUT_ASSERT_TRUE(tgc.bm_rendered);
|
---|
135 | PCUT_ASSERT_EQUALS(srect.p0.x, tgc.bm_srect.p0.x);
|
---|
136 | PCUT_ASSERT_EQUALS(srect.p0.y, tgc.bm_srect.p0.y);
|
---|
137 | PCUT_ASSERT_EQUALS(srect.p1.x, tgc.bm_srect.p1.x);
|
---|
138 | PCUT_ASSERT_EQUALS(srect.p1.y, tgc.bm_srect.p1.y);
|
---|
139 | PCUT_ASSERT_EQUALS(offs.x, tgc.bm_offs.x);
|
---|
140 | PCUT_ASSERT_EQUALS(offs.y, tgc.bm_offs.y);
|
---|
141 |
|
---|
142 | rc = gfx_bitmap_destroy(bitmap);
|
---|
143 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
144 |
|
---|
145 | rc = gfx_context_delete(gc);
|
---|
146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
147 | }
|
---|
148 |
|
---|
149 | PCUT_TEST(get_alloc)
|
---|
150 | {
|
---|
151 | errno_t rc;
|
---|
152 | gfx_context_t *gc = NULL;
|
---|
153 | test_gc_t tgc;
|
---|
154 | gfx_bitmap_params_t params;
|
---|
155 | gfx_bitmap_t *bitmap = NULL;
|
---|
156 | gfx_bitmap_alloc_t alloc;
|
---|
157 |
|
---|
158 | memset(&tgc, 0, sizeof(tgc));
|
---|
159 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 |
|
---|
162 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
163 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
164 |
|
---|
165 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
167 | PCUT_ASSERT_TRUE(tgc.bm_got_alloc);
|
---|
168 | PCUT_ASSERT_EQUALS(alloc_pitch, alloc.pitch);
|
---|
169 | PCUT_ASSERT_EQUALS(alloc_off0, alloc.off0);
|
---|
170 | PCUT_ASSERT_EQUALS(tgc.bm_pixels, alloc.pixels);
|
---|
171 |
|
---|
172 | rc = gfx_bitmap_destroy(bitmap);
|
---|
173 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
174 |
|
---|
175 | rc = gfx_context_delete(gc);
|
---|
176 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
177 | }
|
---|
178 |
|
---|
179 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
180 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
181 | {
|
---|
182 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
183 | testgc_bitmap_t *tbm;
|
---|
184 |
|
---|
185 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
186 | if (tbm == NULL)
|
---|
187 | return ENOMEM;
|
---|
188 |
|
---|
189 | if (alloc == NULL) {
|
---|
190 | tbm->alloc.pitch = alloc_pitch;
|
---|
191 | tbm->alloc.off0 = alloc_off0;
|
---|
192 | tbm->alloc.pixels = calloc(1, 420);
|
---|
193 | tbm->myalloc = true;
|
---|
194 | if (tbm->alloc.pixels == NULL) {
|
---|
195 | free(tbm);
|
---|
196 | return ENOMEM;
|
---|
197 | }
|
---|
198 | } else {
|
---|
199 | tbm->alloc = *alloc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | tbm->tgc = tgc;
|
---|
203 | tgc->bm_created = true;
|
---|
204 | tgc->bm_params = *params;
|
---|
205 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
206 | *rbm = (void *)tbm;
|
---|
207 | return EOK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
211 | {
|
---|
212 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
213 | if (tbm->myalloc)
|
---|
214 | free(tbm->alloc.pixels);
|
---|
215 | tbm->tgc->bm_destroyed = true;
|
---|
216 | free(tbm);
|
---|
217 | return EOK;
|
---|
218 | }
|
---|
219 |
|
---|
220 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
221 | gfx_coord2_t *offs)
|
---|
222 | {
|
---|
223 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
224 | tbm->tgc->bm_rendered = true;
|
---|
225 | tbm->tgc->bm_srect = *srect;
|
---|
226 | tbm->tgc->bm_offs = *offs;
|
---|
227 | return EOK;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
231 | {
|
---|
232 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
233 | *alloc = tbm->alloc;
|
---|
234 | tbm->tgc->bm_got_alloc = true;
|
---|
235 | return EOK;
|
---|
236 | }
|
---|
237 |
|
---|
238 | PCUT_EXPORT(bitmap);
|
---|