[de9992c] | 1 | /*
|
---|
[7470d97] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[de9992c] | 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>
|
---|
[d70dc1c4] | 35 | #include <ui/resource.h>
|
---|
[de9992c] | 36 |
|
---|
| 37 | PCUT_INIT;
|
---|
| 38 |
|
---|
| 39 | PCUT_TEST_SUITE(paint);
|
---|
| 40 |
|
---|
[7470d97] | 41 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
---|
[de9992c] | 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 = {
|
---|
[7470d97] | 51 | .set_clip_rect = testgc_set_clip_rect,
|
---|
[de9992c] | 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 |
|
---|
[d63623f] | 98 | rect.p0.x = 10;
|
---|
| 99 | rect.p0.y = 20;
|
---|
| 100 | rect.p1.x = 30;
|
---|
| 101 | rect.p1.y = 40;
|
---|
| 102 |
|
---|
[de9992c] | 103 | /* Paint bevel with NULL 'inside' output parameter */
|
---|
| 104 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
|
---|
| 105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 106 |
|
---|
| 107 | /* Paint bevel with valid 'inside' output parameter */
|
---|
| 108 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, &inside);
|
---|
| 109 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 110 |
|
---|
| 111 | gfx_color_delete(color2);
|
---|
| 112 | gfx_color_delete(color1);
|
---|
| 113 | rc = gfx_context_delete(gc);
|
---|
| 114 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[d63623f] | 117 | /** Get bevel inside */
|
---|
| 118 | PCUT_TEST(get_bevel_inside)
|
---|
| 119 | {
|
---|
| 120 | errno_t rc;
|
---|
| 121 | gfx_context_t *gc = NULL;
|
---|
| 122 | test_gc_t tgc;
|
---|
| 123 | gfx_rect_t rect;
|
---|
| 124 | gfx_rect_t inside;
|
---|
| 125 |
|
---|
| 126 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 127 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 128 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 129 |
|
---|
| 130 | rect.p0.x = 10;
|
---|
| 131 | rect.p0.y = 20;
|
---|
| 132 | rect.p1.x = 30;
|
---|
| 133 | rect.p1.y = 40;
|
---|
| 134 |
|
---|
| 135 | ui_paint_get_bevel_inside(gc, &rect, 2, &inside);
|
---|
| 136 | PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
|
---|
| 137 | PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
|
---|
| 138 | PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
|
---|
| 139 | PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
|
---|
| 140 |
|
---|
| 141 | rc = gfx_context_delete(gc);
|
---|
| 142 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[d70dc1c4] | 145 | /** Paint inset frame */
|
---|
| 146 | PCUT_TEST(inset_frame)
|
---|
| 147 | {
|
---|
| 148 | errno_t rc;
|
---|
| 149 | gfx_context_t *gc = NULL;
|
---|
| 150 | ui_resource_t *resource = NULL;
|
---|
| 151 | test_gc_t tgc;
|
---|
| 152 | gfx_rect_t rect;
|
---|
| 153 | gfx_rect_t inside;
|
---|
| 154 |
|
---|
| 155 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 156 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 158 |
|
---|
[9c7dc8e] | 159 | rc = ui_resource_create(gc, false, &resource);
|
---|
[d70dc1c4] | 160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 161 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 162 |
|
---|
[d63623f] | 163 | rect.p0.x = 10;
|
---|
| 164 | rect.p0.y = 20;
|
---|
| 165 | rect.p1.x = 30;
|
---|
| 166 | rect.p1.y = 40;
|
---|
| 167 |
|
---|
[d70dc1c4] | 168 | /* Paint inset frame with NULL 'inside' output parameter */
|
---|
| 169 | rc = ui_paint_inset_frame(resource, &rect, NULL);
|
---|
| 170 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 171 |
|
---|
| 172 | /* Paint inset frame with valid 'inside' output parameter */
|
---|
| 173 | rc = ui_paint_inset_frame(resource, &rect, &inside);
|
---|
| 174 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 175 |
|
---|
[f14a900] | 176 | ui_resource_destroy(resource);
|
---|
| 177 | rc = gfx_context_delete(gc);
|
---|
| 178 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[d63623f] | 181 | /** Get inset frame inside */
|
---|
| 182 | PCUT_TEST(get_inset_frame_inside)
|
---|
| 183 | {
|
---|
| 184 | errno_t rc;
|
---|
| 185 | gfx_context_t *gc = NULL;
|
---|
| 186 | ui_resource_t *resource = NULL;
|
---|
| 187 | test_gc_t tgc;
|
---|
| 188 | gfx_rect_t rect;
|
---|
| 189 | gfx_rect_t inside;
|
---|
| 190 |
|
---|
| 191 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 192 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 193 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 194 |
|
---|
| 195 | rc = ui_resource_create(gc, false, &resource);
|
---|
| 196 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 197 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 198 |
|
---|
| 199 | rect.p0.x = 10;
|
---|
| 200 | rect.p0.y = 20;
|
---|
| 201 | rect.p1.x = 30;
|
---|
| 202 | rect.p1.y = 40;
|
---|
| 203 |
|
---|
| 204 | ui_paint_get_inset_frame_inside(resource, &rect, &inside);
|
---|
| 205 | PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
|
---|
| 206 | PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
|
---|
| 207 | PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
|
---|
| 208 | PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
|
---|
| 209 |
|
---|
| 210 | ui_resource_destroy(resource);
|
---|
| 211 | rc = gfx_context_delete(gc);
|
---|
| 212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[f14a900] | 215 | /** Paint filled circle */
|
---|
| 216 | PCUT_TEST(filled_circle)
|
---|
| 217 | {
|
---|
| 218 | errno_t rc;
|
---|
| 219 | gfx_context_t *gc = NULL;
|
---|
| 220 | test_gc_t tgc;
|
---|
| 221 | gfx_coord2_t center;
|
---|
| 222 |
|
---|
| 223 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 224 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 225 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 226 |
|
---|
| 227 | /* Paint filled circle / upper-left half */
|
---|
| 228 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_upleft);
|
---|
| 229 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 230 |
|
---|
| 231 | /* Paint filled circle / lower-right half */
|
---|
| 232 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_lowright);
|
---|
| 233 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 234 |
|
---|
| 235 | /* Paint entire filled circle */
|
---|
| 236 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_entire);
|
---|
| 237 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 238 |
|
---|
[d70dc1c4] | 239 | rc = gfx_context_delete(gc);
|
---|
| 240 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[7470d97] | 243 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
| 244 | {
|
---|
| 245 | (void) arg;
|
---|
| 246 | (void) rect;
|
---|
| 247 | return EOK;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[de9992c] | 250 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
| 251 | {
|
---|
| 252 | (void) arg;
|
---|
| 253 | (void) color;
|
---|
| 254 | return EOK;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 258 | {
|
---|
| 259 | (void) arg;
|
---|
| 260 | (void) rect;
|
---|
| 261 | return EOK;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 265 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 266 | {
|
---|
| 267 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 268 | testgc_bitmap_t *tbm;
|
---|
| 269 |
|
---|
| 270 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 271 | if (tbm == NULL)
|
---|
| 272 | return ENOMEM;
|
---|
| 273 |
|
---|
| 274 | if (alloc == NULL) {
|
---|
| 275 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 276 | sizeof(uint32_t);
|
---|
| 277 | tbm->alloc.off0 = 0;
|
---|
| 278 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
| 279 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 280 | (params->rect.p1.y - params->rect.p0.y));
|
---|
| 281 | tbm->myalloc = true;
|
---|
| 282 | if (tbm->alloc.pixels == NULL) {
|
---|
| 283 | free(tbm);
|
---|
| 284 | return ENOMEM;
|
---|
| 285 | }
|
---|
| 286 | } else {
|
---|
| 287 | tbm->alloc = *alloc;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | tbm->tgc = tgc;
|
---|
| 291 | tgc->bm_created = true;
|
---|
| 292 | tgc->bm_params = *params;
|
---|
| 293 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 294 | *rbm = (void *)tbm;
|
---|
| 295 | return EOK;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 299 | {
|
---|
| 300 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 301 | if (tbm->myalloc)
|
---|
| 302 | free(tbm->alloc.pixels);
|
---|
| 303 | tbm->tgc->bm_destroyed = true;
|
---|
| 304 | free(tbm);
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 309 | gfx_coord2_t *offs)
|
---|
| 310 | {
|
---|
| 311 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 312 | tbm->tgc->bm_rendered = true;
|
---|
| 313 | tbm->tgc->bm_srect = *srect;
|
---|
| 314 | tbm->tgc->bm_offs = *offs;
|
---|
| 315 | return EOK;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 319 | {
|
---|
| 320 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 321 | *alloc = tbm->alloc;
|
---|
| 322 | tbm->tgc->bm_got_alloc = true;
|
---|
| 323 | return EOK;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | PCUT_EXPORT(paint);
|
---|