[dcc4cb31] | 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 |
|
---|
[a8eed5f] | 86 | gfx_bitmap_params_init(¶ms);
|
---|
[dcc4cb31] | 87 | params.rect.p0.x = 1;
|
---|
| 88 | params.rect.p0.y = 2;
|
---|
| 89 | params.rect.p1.x = 3;
|
---|
| 90 | params.rect.p1.y = 4;
|
---|
| 91 |
|
---|
| 92 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 93 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 94 | PCUT_ASSERT_NOT_NULL(bitmap);
|
---|
| 95 | PCUT_ASSERT_TRUE(tgc.bm_created);
|
---|
| 96 | PCUT_ASSERT_EQUALS(params.rect.p0.x, tgc.bm_params.rect.p0.x);
|
---|
| 97 | PCUT_ASSERT_EQUALS(params.rect.p0.y, tgc.bm_params.rect.p0.y);
|
---|
| 98 | PCUT_ASSERT_EQUALS(params.rect.p1.x, tgc.bm_params.rect.p1.x);
|
---|
| 99 | PCUT_ASSERT_EQUALS(params.rect.p1.y, tgc.bm_params.rect.p1.y);
|
---|
| 100 |
|
---|
| 101 | rc = gfx_bitmap_destroy(bitmap);
|
---|
| 102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 103 | PCUT_ASSERT_TRUE(tgc.bm_destroyed);
|
---|
| 104 |
|
---|
| 105 | rc = gfx_context_delete(gc);
|
---|
| 106 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | PCUT_TEST(render)
|
---|
| 110 | {
|
---|
| 111 | errno_t rc;
|
---|
| 112 | gfx_context_t *gc = NULL;
|
---|
| 113 | test_gc_t tgc;
|
---|
| 114 | gfx_bitmap_params_t params;
|
---|
| 115 | gfx_bitmap_t *bitmap = NULL;
|
---|
| 116 | gfx_rect_t srect;
|
---|
| 117 | gfx_coord2_t offs;
|
---|
| 118 |
|
---|
| 119 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 120 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 122 |
|
---|
[a8eed5f] | 123 | gfx_bitmap_params_init(¶ms);
|
---|
| 124 |
|
---|
[dcc4cb31] | 125 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 126 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 127 |
|
---|
| 128 | srect.p0.x = 1;
|
---|
| 129 | srect.p0.y = 2;
|
---|
| 130 | srect.p1.x = 3;
|
---|
| 131 | srect.p1.y = 4;
|
---|
| 132 | offs.x = 5;
|
---|
| 133 | offs.y = 6;
|
---|
| 134 |
|
---|
| 135 | rc = gfx_bitmap_render(bitmap, &srect, &offs);
|
---|
| 136 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 137 | PCUT_ASSERT_TRUE(tgc.bm_rendered);
|
---|
| 138 | PCUT_ASSERT_EQUALS(srect.p0.x, tgc.bm_srect.p0.x);
|
---|
| 139 | PCUT_ASSERT_EQUALS(srect.p0.y, tgc.bm_srect.p0.y);
|
---|
| 140 | PCUT_ASSERT_EQUALS(srect.p1.x, tgc.bm_srect.p1.x);
|
---|
| 141 | PCUT_ASSERT_EQUALS(srect.p1.y, tgc.bm_srect.p1.y);
|
---|
| 142 | PCUT_ASSERT_EQUALS(offs.x, tgc.bm_offs.x);
|
---|
| 143 | PCUT_ASSERT_EQUALS(offs.y, tgc.bm_offs.y);
|
---|
| 144 |
|
---|
| 145 | rc = gfx_bitmap_destroy(bitmap);
|
---|
| 146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 147 |
|
---|
| 148 | rc = gfx_context_delete(gc);
|
---|
| 149 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | PCUT_TEST(get_alloc)
|
---|
| 153 | {
|
---|
| 154 | errno_t rc;
|
---|
| 155 | gfx_context_t *gc = NULL;
|
---|
| 156 | test_gc_t tgc;
|
---|
| 157 | gfx_bitmap_params_t params;
|
---|
| 158 | gfx_bitmap_t *bitmap = NULL;
|
---|
| 159 | gfx_bitmap_alloc_t alloc;
|
---|
| 160 |
|
---|
| 161 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 162 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 163 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 164 |
|
---|
[a8eed5f] | 165 | gfx_bitmap_params_init(¶ms);
|
---|
| 166 |
|
---|
[dcc4cb31] | 167 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 168 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 169 |
|
---|
| 170 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 171 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 172 | PCUT_ASSERT_TRUE(tgc.bm_got_alloc);
|
---|
| 173 | PCUT_ASSERT_EQUALS(alloc_pitch, alloc.pitch);
|
---|
| 174 | PCUT_ASSERT_EQUALS(alloc_off0, alloc.off0);
|
---|
| 175 | PCUT_ASSERT_EQUALS(tgc.bm_pixels, alloc.pixels);
|
---|
| 176 |
|
---|
| 177 | rc = gfx_bitmap_destroy(bitmap);
|
---|
| 178 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 179 |
|
---|
| 180 | rc = gfx_context_delete(gc);
|
---|
| 181 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 185 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 186 | {
|
---|
| 187 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 188 | testgc_bitmap_t *tbm;
|
---|
| 189 |
|
---|
| 190 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 191 | if (tbm == NULL)
|
---|
| 192 | return ENOMEM;
|
---|
| 193 |
|
---|
| 194 | if (alloc == NULL) {
|
---|
| 195 | tbm->alloc.pitch = alloc_pitch;
|
---|
| 196 | tbm->alloc.off0 = alloc_off0;
|
---|
| 197 | tbm->alloc.pixels = calloc(1, 420);
|
---|
| 198 | tbm->myalloc = true;
|
---|
| 199 | if (tbm->alloc.pixels == NULL) {
|
---|
| 200 | free(tbm);
|
---|
| 201 | return ENOMEM;
|
---|
| 202 | }
|
---|
| 203 | } else {
|
---|
| 204 | tbm->alloc = *alloc;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | tbm->tgc = tgc;
|
---|
| 208 | tgc->bm_created = true;
|
---|
| 209 | tgc->bm_params = *params;
|
---|
| 210 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 211 | *rbm = (void *)tbm;
|
---|
| 212 | return EOK;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 216 | {
|
---|
| 217 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 218 | if (tbm->myalloc)
|
---|
| 219 | free(tbm->alloc.pixels);
|
---|
| 220 | tbm->tgc->bm_destroyed = true;
|
---|
| 221 | free(tbm);
|
---|
| 222 | return EOK;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 226 | gfx_coord2_t *offs)
|
---|
| 227 | {
|
---|
| 228 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 229 | tbm->tgc->bm_rendered = true;
|
---|
| 230 | tbm->tgc->bm_srect = *srect;
|
---|
| 231 | tbm->tgc->bm_offs = *offs;
|
---|
| 232 | return EOK;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 236 | {
|
---|
| 237 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 238 | *alloc = tbm->alloc;
|
---|
| 239 | tbm->tgc->bm_got_alloc = true;
|
---|
| 240 | return EOK;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | PCUT_EXPORT(bitmap);
|
---|