[f7a90df] | 1 | /*
|
---|
[2ab8ab3] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[f7a90df] | 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 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Dummy graphic context
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <gfx/context.h>
|
---|
| 37 | #include <gfx/coord.h>
|
---|
| 38 | #include <mem.h>
|
---|
| 39 | #include <stdbool.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include "../private/dummygc.h"
|
---|
| 42 |
|
---|
[7470d97] | 43 | static errno_t dummygc_set_clip_rect(void *, gfx_rect_t *);
|
---|
[f7a90df] | 44 | static errno_t dummygc_set_color(void *, gfx_color_t *);
|
---|
| 45 | static errno_t dummygc_fill_rect(void *, gfx_rect_t *);
|
---|
[f0ccb2ab] | 46 | static errno_t dummygc_update(void *);
|
---|
[f7a90df] | 47 | static errno_t dummygc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 48 | gfx_bitmap_alloc_t *, void **);
|
---|
| 49 | static errno_t dummygc_bitmap_destroy(void *);
|
---|
| 50 | static errno_t dummygc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 51 | static errno_t dummygc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 52 |
|
---|
| 53 | /** Dummy GC operations */
|
---|
| 54 | gfx_context_ops_t dummygc_ops = {
|
---|
[7470d97] | 55 | .set_clip_rect = dummygc_set_clip_rect,
|
---|
[f7a90df] | 56 | .set_color = dummygc_set_color,
|
---|
| 57 | .fill_rect = dummygc_fill_rect,
|
---|
[f0ccb2ab] | 58 | .update = dummygc_update,
|
---|
[f7a90df] | 59 | .bitmap_create = dummygc_bitmap_create,
|
---|
| 60 | .bitmap_destroy = dummygc_bitmap_destroy,
|
---|
| 61 | .bitmap_render = dummygc_bitmap_render,
|
---|
| 62 | .bitmap_get_alloc = dummygc_bitmap_get_alloc
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | /** Create dummy GC.
|
---|
| 66 | *
|
---|
| 67 | * @param rdgc Place to store pointer to new dummy GC
|
---|
| 68 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 69 | */
|
---|
| 70 | errno_t dummygc_create(dummy_gc_t **rdgc)
|
---|
| 71 | {
|
---|
| 72 | dummy_gc_t *dgc;
|
---|
| 73 | gfx_context_t *gc;
|
---|
| 74 | errno_t rc;
|
---|
| 75 |
|
---|
| 76 | dgc = calloc(1, sizeof(dummy_gc_t));
|
---|
| 77 | if (dgc == NULL)
|
---|
| 78 | return ENOMEM;
|
---|
| 79 |
|
---|
| 80 | rc = gfx_context_new(&dummygc_ops, dgc, &gc);
|
---|
| 81 | if (rc != EOK) {
|
---|
| 82 | free(dgc);
|
---|
| 83 | return rc;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | dgc->gc = gc;
|
---|
| 87 | *rdgc = dgc;
|
---|
| 88 | return EOK;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Destroy dummy GC.
|
---|
| 92 | *
|
---|
| 93 | * @param dgc Dummy GC
|
---|
| 94 | */
|
---|
| 95 | void dummygc_destroy(dummy_gc_t *dgc)
|
---|
| 96 | {
|
---|
| 97 | gfx_context_delete(dgc->gc);
|
---|
| 98 | free(dgc);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Get generic graphic context from dummy GC.
|
---|
| 102 | *
|
---|
| 103 | * @param dgc Dummy GC
|
---|
| 104 | * @return Graphic context
|
---|
| 105 | */
|
---|
| 106 | gfx_context_t *dummygc_get_ctx(dummy_gc_t *dgc)
|
---|
| 107 | {
|
---|
| 108 | return dgc->gc;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[7470d97] | 111 | /** Set clipping rectangle on dummy GC
|
---|
| 112 | *
|
---|
| 113 | * @param arg Argument (dummy_gc_t)
|
---|
| 114 | * @param rect Rectangle
|
---|
| 115 | * @return EOK on success or an error code
|
---|
| 116 | */
|
---|
| 117 | static errno_t dummygc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
| 118 | {
|
---|
| 119 | (void) arg;
|
---|
| 120 | (void) rect;
|
---|
| 121 | return EOK;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[f7a90df] | 124 | /** Set color on dummy GC
|
---|
| 125 | *
|
---|
| 126 | * @param arg Argument (dummy_gc_t)
|
---|
| 127 | * @param color Color
|
---|
| 128 | * @return EOK on success or an error code
|
---|
| 129 | */
|
---|
| 130 | static errno_t dummygc_set_color(void *arg, gfx_color_t *color)
|
---|
| 131 | {
|
---|
| 132 | (void) arg;
|
---|
| 133 | (void) color;
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Fill rectangle on dummy GC
|
---|
| 138 | *
|
---|
| 139 | * @param arg Argument (dummy_gc_t)
|
---|
| 140 | * @param rect Rectangle
|
---|
| 141 | * @return EOK on success or an error code
|
---|
| 142 | */
|
---|
| 143 | static errno_t dummygc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 144 | {
|
---|
| 145 | (void) arg;
|
---|
| 146 | (void) rect;
|
---|
| 147 | return EOK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[f0ccb2ab] | 150 | /** Update dummy GC
|
---|
| 151 | *
|
---|
| 152 | * @param arg Argument (dummy_gc_t)
|
---|
| 153 | * @return EOK on success or an error code
|
---|
| 154 | */
|
---|
| 155 | static errno_t dummygc_update(void *arg)
|
---|
| 156 | {
|
---|
| 157 | (void) arg;
|
---|
| 158 | return EOK;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[f7a90df] | 161 | /** Create bitmap on dummy GC
|
---|
| 162 | *
|
---|
| 163 | * @param arg Argument (dummy_gc_t)
|
---|
| 164 | * @param params Bitmap parameters
|
---|
| 165 | * @param alloc Bitmap allocation info or @c NULL
|
---|
| 166 | * @param rbm Place to store pointer to new bitmap
|
---|
| 167 | * @return EOK on success or an error code
|
---|
| 168 | */
|
---|
| 169 | static errno_t dummygc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 170 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 171 | {
|
---|
| 172 | dummy_gc_t *dgc = (dummy_gc_t *) arg;
|
---|
| 173 | dummygc_bitmap_t *tbm;
|
---|
| 174 |
|
---|
| 175 | tbm = calloc(1, sizeof(dummygc_bitmap_t));
|
---|
| 176 | if (tbm == NULL)
|
---|
| 177 | return ENOMEM;
|
---|
| 178 |
|
---|
| 179 | if (alloc == NULL) {
|
---|
| 180 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 181 | sizeof(uint32_t);
|
---|
| 182 | tbm->alloc.off0 = 0;
|
---|
[1fa6292] | 183 | tbm->alloc.pixels = calloc(
|
---|
[f7a90df] | 184 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
[1fa6292] | 185 | (params->rect.p1.y - params->rect.p0.y),
|
---|
| 186 | sizeof(uint32_t));
|
---|
[f7a90df] | 187 | tbm->myalloc = true;
|
---|
| 188 | if (tbm->alloc.pixels == NULL) {
|
---|
| 189 | free(tbm);
|
---|
| 190 | return ENOMEM;
|
---|
| 191 | }
|
---|
| 192 | } else {
|
---|
| 193 | tbm->alloc = *alloc;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | tbm->dgc = dgc;
|
---|
| 197 | dgc->bm_created = true;
|
---|
| 198 | dgc->bm_params = *params;
|
---|
| 199 | dgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 200 | *rbm = (void *)tbm;
|
---|
| 201 | return EOK;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /** Destroy bitmap on dummy GC
|
---|
| 205 | *
|
---|
| 206 | * @param bm Bitmap
|
---|
| 207 | * @return EOK on success or an error code
|
---|
| 208 | */
|
---|
| 209 | static errno_t dummygc_bitmap_destroy(void *bm)
|
---|
| 210 | {
|
---|
| 211 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
| 212 | if (tbm->myalloc)
|
---|
| 213 | free(tbm->alloc.pixels);
|
---|
| 214 | tbm->dgc->bm_destroyed = true;
|
---|
| 215 | free(tbm);
|
---|
| 216 | return EOK;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Render bitmap on dummy GC
|
---|
| 220 | *
|
---|
| 221 | * @param bm Bitmap
|
---|
| 222 | * @param srect Source rectangle or @c NULL
|
---|
| 223 | * @param offs Offset or @c NULL
|
---|
| 224 | * @return EOK on success or an error code
|
---|
| 225 | */
|
---|
| 226 | static errno_t dummygc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 227 | gfx_coord2_t *offs)
|
---|
| 228 | {
|
---|
| 229 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
[2ab8ab3] | 230 |
|
---|
[f7a90df] | 231 | tbm->dgc->bm_rendered = true;
|
---|
[2ab8ab3] | 232 |
|
---|
| 233 | tbm->dgc->bm_srect.p0.x = 0;
|
---|
| 234 | tbm->dgc->bm_srect.p0.y = 0;
|
---|
| 235 | tbm->dgc->bm_srect.p1.x = 0;
|
---|
| 236 | tbm->dgc->bm_srect.p1.y = 0;
|
---|
| 237 |
|
---|
| 238 | tbm->dgc->bm_offs.x = 0;
|
---|
| 239 | tbm->dgc->bm_offs.y = 0;
|
---|
| 240 |
|
---|
| 241 | if (srect != NULL)
|
---|
| 242 | tbm->dgc->bm_srect = *srect;
|
---|
| 243 |
|
---|
| 244 | if (offs != NULL)
|
---|
| 245 | tbm->dgc->bm_offs = *offs;
|
---|
| 246 |
|
---|
[f7a90df] | 247 | return EOK;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** Get bitmap allocation info on dummy GC
|
---|
| 251 | *
|
---|
| 252 | * @param bm Bitmap
|
---|
| 253 | * @param alloc Place to store allocation info
|
---|
| 254 | * @return EOK on success or an error code
|
---|
| 255 | */
|
---|
| 256 | static errno_t dummygc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 257 | {
|
---|
| 258 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
| 259 | *alloc = tbm->alloc;
|
---|
| 260 | tbm->dgc->bm_got_alloc = true;
|
---|
| 261 | return EOK;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /** @}
|
---|
| 265 | */
|
---|