[de9992c] | 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>
|
---|
[d70dc1c4] | 35 | #include <ui/resource.h>
|
---|
[de9992c] | 36 |
|
---|
| 37 | PCUT_INIT;
|
---|
| 38 |
|
---|
| 39 | PCUT_TEST_SUITE(paint);
|
---|
| 40 |
|
---|
| 41 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
| 42 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
| 43 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 44 | gfx_bitmap_alloc_t *, void **);
|
---|
| 45 | static errno_t testgc_bitmap_destroy(void *);
|
---|
| 46 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 47 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 48 |
|
---|
| 49 | static gfx_context_ops_t ops = {
|
---|
| 50 | .set_color = testgc_set_color,
|
---|
| 51 | .fill_rect = testgc_fill_rect,
|
---|
| 52 | .bitmap_create = testgc_bitmap_create,
|
---|
| 53 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
| 54 | .bitmap_render = testgc_bitmap_render,
|
---|
| 55 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | typedef struct {
|
---|
| 59 | bool bm_created;
|
---|
| 60 | bool bm_destroyed;
|
---|
| 61 | gfx_bitmap_params_t bm_params;
|
---|
| 62 | void *bm_pixels;
|
---|
| 63 | gfx_rect_t bm_srect;
|
---|
| 64 | gfx_coord2_t bm_offs;
|
---|
| 65 | bool bm_rendered;
|
---|
| 66 | bool bm_got_alloc;
|
---|
| 67 | } test_gc_t;
|
---|
| 68 |
|
---|
| 69 | typedef struct {
|
---|
| 70 | test_gc_t *tgc;
|
---|
| 71 | gfx_bitmap_alloc_t alloc;
|
---|
| 72 | bool myalloc;
|
---|
| 73 | } testgc_bitmap_t;
|
---|
| 74 |
|
---|
| 75 | /** Paint bevel */
|
---|
| 76 | PCUT_TEST(bevel)
|
---|
| 77 | {
|
---|
| 78 | errno_t rc;
|
---|
| 79 | gfx_context_t *gc = NULL;
|
---|
| 80 | test_gc_t tgc;
|
---|
| 81 | gfx_rect_t rect;
|
---|
| 82 | gfx_color_t *color1;
|
---|
| 83 | gfx_color_t *color2;
|
---|
| 84 | gfx_rect_t inside;
|
---|
| 85 |
|
---|
| 86 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 87 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 88 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 89 |
|
---|
| 90 | rc = gfx_color_new_rgb_i16(1, 2, 3, &color1);
|
---|
| 91 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 92 |
|
---|
| 93 | rc = gfx_color_new_rgb_i16(4, 5, 6, &color2);
|
---|
| 94 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 95 |
|
---|
| 96 | /* Paint bevel with NULL 'inside' output parameter */
|
---|
| 97 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
|
---|
| 98 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 99 |
|
---|
| 100 | /* Paint bevel with valid 'inside' output parameter */
|
---|
| 101 | rc = ui_paint_bevel(gc, &rect, color1, color2, 2, &inside);
|
---|
| 102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 103 |
|
---|
| 104 | gfx_color_delete(color2);
|
---|
| 105 | gfx_color_delete(color1);
|
---|
| 106 | rc = gfx_context_delete(gc);
|
---|
| 107 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[d70dc1c4] | 110 | /** Paint inset frame */
|
---|
| 111 | PCUT_TEST(inset_frame)
|
---|
| 112 | {
|
---|
| 113 | errno_t rc;
|
---|
| 114 | gfx_context_t *gc = NULL;
|
---|
| 115 | ui_resource_t *resource = NULL;
|
---|
| 116 | test_gc_t tgc;
|
---|
| 117 | gfx_rect_t rect;
|
---|
| 118 | gfx_rect_t inside;
|
---|
| 119 |
|
---|
| 120 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 121 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 122 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 123 |
|
---|
[9c7dc8e] | 124 | rc = ui_resource_create(gc, false, &resource);
|
---|
[d70dc1c4] | 125 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 126 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 127 |
|
---|
| 128 | /* Paint inset frame with NULL 'inside' output parameter */
|
---|
| 129 | rc = ui_paint_inset_frame(resource, &rect, NULL);
|
---|
| 130 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 131 |
|
---|
| 132 | /* Paint inset frame with valid 'inside' output parameter */
|
---|
| 133 | rc = ui_paint_inset_frame(resource, &rect, &inside);
|
---|
| 134 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 135 |
|
---|
[f14a900] | 136 | ui_resource_destroy(resource);
|
---|
| 137 | rc = gfx_context_delete(gc);
|
---|
| 138 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Paint filled circle */
|
---|
| 142 | PCUT_TEST(filled_circle)
|
---|
| 143 | {
|
---|
| 144 | errno_t rc;
|
---|
| 145 | gfx_context_t *gc = NULL;
|
---|
| 146 | test_gc_t tgc;
|
---|
| 147 | gfx_coord2_t center;
|
---|
| 148 |
|
---|
| 149 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 150 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 152 |
|
---|
| 153 | /* Paint filled circle / upper-left half */
|
---|
| 154 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_upleft);
|
---|
| 155 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 156 |
|
---|
| 157 | /* Paint filled circle / lower-right half */
|
---|
| 158 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_lowright);
|
---|
| 159 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 160 |
|
---|
| 161 | /* Paint entire filled circle */
|
---|
| 162 | rc = ui_paint_filled_circle(gc, ¢er, 10, ui_fcircle_entire);
|
---|
| 163 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 164 |
|
---|
[d70dc1c4] | 165 | rc = gfx_context_delete(gc);
|
---|
| 166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[de9992c] | 169 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
| 170 | {
|
---|
| 171 | (void) arg;
|
---|
| 172 | (void) color;
|
---|
| 173 | return EOK;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 177 | {
|
---|
| 178 | (void) arg;
|
---|
| 179 | (void) rect;
|
---|
| 180 | return EOK;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 184 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 185 | {
|
---|
| 186 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 187 | testgc_bitmap_t *tbm;
|
---|
| 188 |
|
---|
| 189 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 190 | if (tbm == NULL)
|
---|
| 191 | return ENOMEM;
|
---|
| 192 |
|
---|
| 193 | if (alloc == NULL) {
|
---|
| 194 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 195 | sizeof(uint32_t);
|
---|
| 196 | tbm->alloc.off0 = 0;
|
---|
| 197 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
| 198 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 199 | (params->rect.p1.y - params->rect.p0.y));
|
---|
| 200 | tbm->myalloc = true;
|
---|
| 201 | if (tbm->alloc.pixels == NULL) {
|
---|
| 202 | free(tbm);
|
---|
| 203 | return ENOMEM;
|
---|
| 204 | }
|
---|
| 205 | } else {
|
---|
| 206 | tbm->alloc = *alloc;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | tbm->tgc = tgc;
|
---|
| 210 | tgc->bm_created = true;
|
---|
| 211 | tgc->bm_params = *params;
|
---|
| 212 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 213 | *rbm = (void *)tbm;
|
---|
| 214 | return EOK;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 218 | {
|
---|
| 219 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 220 | if (tbm->myalloc)
|
---|
| 221 | free(tbm->alloc.pixels);
|
---|
| 222 | tbm->tgc->bm_destroyed = true;
|
---|
| 223 | free(tbm);
|
---|
| 224 | return EOK;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 228 | gfx_coord2_t *offs)
|
---|
| 229 | {
|
---|
| 230 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 231 | tbm->tgc->bm_rendered = true;
|
---|
| 232 | tbm->tgc->bm_srect = *srect;
|
---|
| 233 | tbm->tgc->bm_offs = *offs;
|
---|
| 234 | return EOK;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 238 | {
|
---|
| 239 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 240 | *alloc = tbm->alloc;
|
---|
| 241 | tbm->tgc->bm_got_alloc = true;
|
---|
| 242 | return EOK;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | PCUT_EXPORT(paint);
|
---|