[d8e2485] | 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 | /** @addtogroup libmemgfx
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file GFX memory backend
|
---|
| 34 | *
|
---|
| 35 | * This implements a graphics context over a block of memory (i.e. a simple
|
---|
| 36 | * software renderer).
|
---|
| 37 | */
|
---|
| 38 |
|
---|
[afcf704] | 39 | #include <as.h>
|
---|
[d8e2485] | 40 | #include <assert.h>
|
---|
| 41 | #include <gfx/color.h>
|
---|
| 42 | #include <gfx/context.h>
|
---|
| 43 | #include <gfx/render.h>
|
---|
| 44 | #include <io/pixel.h>
|
---|
| 45 | #include <io/pixelmap.h>
|
---|
| 46 | #include <memgfx/memgc.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include "../private/memgc.h"
|
---|
| 49 |
|
---|
| 50 | static errno_t mem_gc_set_color(void *, gfx_color_t *);
|
---|
| 51 | static errno_t mem_gc_fill_rect(void *, gfx_rect_t *);
|
---|
| 52 | static errno_t mem_gc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 53 | gfx_bitmap_alloc_t *, void **);
|
---|
| 54 | static errno_t mem_gc_bitmap_destroy(void *);
|
---|
| 55 | static errno_t mem_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 56 | static errno_t mem_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 57 | static void mem_gc_invalidate_rect(mem_gc_t *, gfx_rect_t *);
|
---|
| 58 |
|
---|
| 59 | gfx_context_ops_t mem_gc_ops = {
|
---|
| 60 | .set_color = mem_gc_set_color,
|
---|
| 61 | .fill_rect = mem_gc_fill_rect,
|
---|
| 62 | .bitmap_create = mem_gc_bitmap_create,
|
---|
| 63 | .bitmap_destroy = mem_gc_bitmap_destroy,
|
---|
| 64 | .bitmap_render = mem_gc_bitmap_render,
|
---|
| 65 | .bitmap_get_alloc = mem_gc_bitmap_get_alloc
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | /** Set color on memory GC.
|
---|
| 69 | *
|
---|
| 70 | * Set drawing color on memory GC.
|
---|
| 71 | *
|
---|
| 72 | * @param arg Canvas GC
|
---|
| 73 | * @param color Color
|
---|
| 74 | *
|
---|
| 75 | * @return EOK on success or an error code
|
---|
| 76 | */
|
---|
| 77 | static errno_t mem_gc_set_color(void *arg, gfx_color_t *color)
|
---|
| 78 | {
|
---|
| 79 | mem_gc_t *mgc = (mem_gc_t *) arg;
|
---|
| 80 | uint16_t r, g, b;
|
---|
| 81 |
|
---|
| 82 | gfx_color_get_rgb_i16(color, &r, &g, &b);
|
---|
| 83 | mgc->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
|
---|
| 84 | return EOK;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /** Fill rectangle on memory GC.
|
---|
| 88 | *
|
---|
| 89 | * @param arg Canvas GC
|
---|
| 90 | * @param rect Rectangle
|
---|
| 91 | *
|
---|
| 92 | * @return EOK on success or an error code
|
---|
| 93 | */
|
---|
| 94 | static errno_t mem_gc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 95 | {
|
---|
| 96 | mem_gc_t *mgc = (mem_gc_t *) arg;
|
---|
| 97 | gfx_rect_t crect;
|
---|
| 98 | gfx_coord_t x, y;
|
---|
| 99 | pixelmap_t pixelmap;
|
---|
| 100 |
|
---|
| 101 | /* Make sure we have a sorted, clipped rectangle */
|
---|
| 102 | gfx_rect_clip(rect, &mgc->rect, &crect);
|
---|
| 103 |
|
---|
[dbef30f] | 104 | assert(mgc->rect.p0.x == 0);
|
---|
| 105 | assert(mgc->rect.p0.y == 0);
|
---|
| 106 | assert(mgc->alloc.pitch == mgc->rect.p1.x * (int)sizeof(uint32_t));
|
---|
| 107 | pixelmap.width = mgc->rect.p1.x;
|
---|
| 108 | pixelmap.height = mgc->rect.p1.y;
|
---|
[d8e2485] | 109 | pixelmap.data = mgc->alloc.pixels;
|
---|
| 110 |
|
---|
| 111 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
| 112 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
| 113 | pixelmap_put_pixel(&pixelmap, x, y, mgc->color);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | mem_gc_invalidate_rect(mgc, &crect);
|
---|
| 118 | return EOK;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /** Create memory GC.
|
---|
| 122 | *
|
---|
| 123 | * Create graphics context for rendering into a block of memory.
|
---|
| 124 | *
|
---|
| 125 | * @param rect Bounding rectangle
|
---|
| 126 | * @param alloc Allocation info
|
---|
[f8375f7] | 127 | * @param update_cb Function called to update a rectangle
|
---|
| 128 | * @param cb_arg Argument to callback function
|
---|
[d8e2485] | 129 | * @param rgc Place to store pointer to new memory GC
|
---|
| 130 | *
|
---|
| 131 | * @return EOK on success or an error code
|
---|
| 132 | */
|
---|
| 133 | errno_t mem_gc_create(gfx_rect_t *rect, gfx_bitmap_alloc_t *alloc,
|
---|
[f8375f7] | 134 | mem_gc_update_cb_t update_cb, void *cb_arg, mem_gc_t **rgc)
|
---|
[d8e2485] | 135 | {
|
---|
| 136 | mem_gc_t *mgc = NULL;
|
---|
| 137 | gfx_context_t *gc = NULL;
|
---|
| 138 | errno_t rc;
|
---|
| 139 |
|
---|
| 140 | mgc = calloc(1, sizeof(mem_gc_t));
|
---|
| 141 | if (mgc == NULL) {
|
---|
| 142 | rc = ENOMEM;
|
---|
| 143 | goto error;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | rc = gfx_context_new(&mem_gc_ops, mgc, &gc);
|
---|
| 147 | if (rc != EOK)
|
---|
| 148 | goto error;
|
---|
| 149 |
|
---|
| 150 | mgc->gc = gc;
|
---|
| 151 | mgc->rect = *rect;
|
---|
| 152 | mgc->alloc = *alloc;
|
---|
| 153 |
|
---|
[f8375f7] | 154 | mgc->update = update_cb;
|
---|
| 155 | mgc->cb_arg = cb_arg;
|
---|
[d8e2485] | 156 |
|
---|
| 157 | *rgc = mgc;
|
---|
| 158 | return EOK;
|
---|
| 159 | error:
|
---|
| 160 | if (mgc != NULL)
|
---|
| 161 | free(mgc);
|
---|
| 162 | gfx_context_delete(gc);
|
---|
| 163 | return rc;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /** Delete memory GC.
|
---|
| 167 | *
|
---|
| 168 | * @param mgc Canvas GC
|
---|
| 169 | */
|
---|
| 170 | errno_t mem_gc_delete(mem_gc_t *mgc)
|
---|
| 171 | {
|
---|
| 172 | errno_t rc;
|
---|
| 173 |
|
---|
| 174 | rc = gfx_context_delete(mgc->gc);
|
---|
| 175 | if (rc != EOK)
|
---|
| 176 | return rc;
|
---|
| 177 |
|
---|
| 178 | free(mgc);
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[dbef30f] | 182 | /** Retarget memory GC to a different block of memory.
|
---|
| 183 | *
|
---|
| 184 | * @param mgc Memory GC
|
---|
| 185 | * @param rect New bounding rectangle
|
---|
| 186 | * @param alloc Allocation info of the new block
|
---|
| 187 | */
|
---|
| 188 | void mem_gc_retarget(mem_gc_t *mgc, gfx_rect_t *rect,
|
---|
| 189 | gfx_bitmap_alloc_t *alloc)
|
---|
| 190 | {
|
---|
| 191 | mgc->rect = *rect;
|
---|
| 192 | mgc->alloc = *alloc;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[d8e2485] | 195 | /** Get generic graphic context from memory GC.
|
---|
| 196 | *
|
---|
| 197 | * @param mgc Canvas GC
|
---|
| 198 | * @return Graphic context
|
---|
| 199 | */
|
---|
| 200 | gfx_context_t *mem_gc_get_ctx(mem_gc_t *mgc)
|
---|
| 201 | {
|
---|
| 202 | return mgc->gc;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | static void mem_gc_invalidate_rect(mem_gc_t *mgc, gfx_rect_t *rect)
|
---|
| 206 | {
|
---|
[f8375f7] | 207 | mgc->update(mgc->cb_arg, rect);
|
---|
[d8e2485] | 208 | }
|
---|
| 209 |
|
---|
| 210 | /** Create bitmap in memory GC.
|
---|
| 211 | *
|
---|
| 212 | * @param arg Canvas GC
|
---|
| 213 | * @param params Bitmap params
|
---|
| 214 | * @param alloc Bitmap allocation info or @c NULL
|
---|
| 215 | * @param rbm Place to store pointer to new bitmap
|
---|
| 216 | * @return EOK on success or an error code
|
---|
| 217 | */
|
---|
| 218 | errno_t mem_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 219 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 220 | {
|
---|
| 221 | mem_gc_t *mgc = (mem_gc_t *) arg;
|
---|
| 222 | mem_gc_bitmap_t *mbm = NULL;
|
---|
| 223 | gfx_coord2_t dim;
|
---|
| 224 | errno_t rc;
|
---|
| 225 |
|
---|
[afcf704] | 226 | /* Check that we support all requested flags */
|
---|
| 227 | if ((params->flags & ~(bmpf_color_key | bmpf_direct_output)) != 0)
|
---|
| 228 | return ENOTSUP;
|
---|
| 229 |
|
---|
[d8e2485] | 230 | mbm = calloc(1, sizeof(mem_gc_bitmap_t));
|
---|
| 231 | if (mbm == NULL)
|
---|
| 232 | return ENOMEM;
|
---|
| 233 |
|
---|
| 234 | gfx_coord2_subtract(¶ms->rect.p1, ¶ms->rect.p0, &dim);
|
---|
| 235 | mbm->rect = params->rect;
|
---|
| 236 | mbm->flags = params->flags;
|
---|
| 237 | mbm->key_color = params->key_color;
|
---|
| 238 |
|
---|
[afcf704] | 239 | if ((params->flags & bmpf_direct_output) != 0) {
|
---|
| 240 | /* Caller cannot specify allocation for direct output */
|
---|
| 241 | if (alloc != NULL) {
|
---|
| 242 | rc = EINVAL;
|
---|
| 243 | goto error;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /* Bounding rectangle must be within GC bounding rectangle */
|
---|
| 247 | if (!gfx_rect_is_inside(&mbm->rect, &mgc->rect)) {
|
---|
| 248 | rc = EINVAL;
|
---|
| 249 | goto error;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | mbm->alloc = mgc->alloc;
|
---|
| 253 |
|
---|
| 254 | /* Don't free pixel array when destroying bitmap */
|
---|
| 255 | mbm->myalloc = false;
|
---|
| 256 | } else if (alloc == NULL) {
|
---|
| 257 | #if 0
|
---|
| 258 | /*
|
---|
| 259 | * TODO: If the bitmap is not required to be sharable,
|
---|
| 260 | * we could allocate it with a simple malloc.
|
---|
| 261 | * Need to have a bitmap flag specifying that the
|
---|
| 262 | * allocation should be sharable. IPC GC could
|
---|
| 263 | * automatically add this flag
|
---|
| 264 | */
|
---|
[d8e2485] | 265 | mbm->alloc.pitch = dim.x * sizeof(uint32_t);
|
---|
| 266 | mbm->alloc.off0 = 0;
|
---|
| 267 | mbm->alloc.pixels = malloc(mbm->alloc.pitch * dim.y);
|
---|
| 268 | mbm->myalloc = true;
|
---|
| 269 |
|
---|
| 270 | if (mbm->alloc.pixels == NULL) {
|
---|
| 271 | rc = ENOMEM;
|
---|
| 272 | goto error;
|
---|
| 273 | }
|
---|
[afcf704] | 274 | #endif
|
---|
| 275 | mbm->alloc.pitch = dim.x * sizeof(uint32_t);
|
---|
| 276 | mbm->alloc.off0 = 0;
|
---|
| 277 | mbm->alloc.pixels = as_area_create(AS_AREA_ANY,
|
---|
| 278 | dim.x * dim.y * sizeof(uint32_t), AS_AREA_READ |
|
---|
| 279 | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
|
---|
| 280 | mbm->myalloc = true;
|
---|
| 281 |
|
---|
| 282 | if (mbm->alloc.pixels == AS_MAP_FAILED) {
|
---|
| 283 | rc = ENOMEM;
|
---|
| 284 | goto error;
|
---|
| 285 | }
|
---|
[d8e2485] | 286 | } else {
|
---|
| 287 | mbm->alloc = *alloc;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | mbm->mgc = mgc;
|
---|
| 291 | *rbm = (void *)mbm;
|
---|
| 292 | return EOK;
|
---|
| 293 | error:
|
---|
| 294 | if (mbm != NULL)
|
---|
| 295 | free(mbm);
|
---|
| 296 | return rc;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /** Destroy bitmap in memory GC.
|
---|
| 300 | *
|
---|
| 301 | * @param bm Bitmap
|
---|
| 302 | * @return EOK on success or an error code
|
---|
| 303 | */
|
---|
| 304 | static errno_t mem_gc_bitmap_destroy(void *bm)
|
---|
| 305 | {
|
---|
| 306 | mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
|
---|
[afcf704] | 307 | if (mbm->myalloc) {
|
---|
| 308 | #if 0
|
---|
| 309 | /* TODO: if we alloc allocating the bitmap with malloc */
|
---|
[d8e2485] | 310 | free(mbm->alloc.pixels);
|
---|
[afcf704] | 311 | #endif
|
---|
| 312 | as_area_destroy(mbm->alloc.pixels);
|
---|
| 313 | }
|
---|
[d8e2485] | 314 |
|
---|
| 315 | free(mbm);
|
---|
| 316 | return EOK;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /** Render bitmap in memory GC.
|
---|
| 320 | *
|
---|
| 321 | * @param bm Bitmap
|
---|
| 322 | * @param srect0 Source rectangle or @c NULL
|
---|
| 323 | * @param offs0 Offset or @c NULL
|
---|
| 324 | * @return EOK on success or an error code
|
---|
| 325 | */
|
---|
| 326 | static errno_t mem_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
| 327 | gfx_coord2_t *offs0)
|
---|
| 328 | {
|
---|
| 329 | mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
|
---|
| 330 | gfx_rect_t srect;
|
---|
| 331 | gfx_rect_t drect;
|
---|
| 332 | gfx_coord2_t offs;
|
---|
| 333 | gfx_coord_t x, y;
|
---|
| 334 | pixelmap_t smap;
|
---|
| 335 | pixelmap_t dmap;
|
---|
| 336 | pixel_t pixel;
|
---|
| 337 |
|
---|
| 338 | if (srect0 != NULL)
|
---|
| 339 | gfx_rect_clip(srect0, &mbm->rect, &srect);
|
---|
| 340 | else
|
---|
| 341 | srect = mbm->rect;
|
---|
| 342 |
|
---|
| 343 | if (offs0 != NULL) {
|
---|
| 344 | offs = *offs0;
|
---|
| 345 | } else {
|
---|
| 346 | offs.x = 0;
|
---|
| 347 | offs.y = 0;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | /* Destination rectangle */
|
---|
| 351 | gfx_rect_translate(&offs, &srect, &drect);
|
---|
| 352 |
|
---|
[8aef01c] | 353 | assert(mbm->alloc.pitch == (mbm->rect.p1.x - mbm->rect.p0.x) *
|
---|
| 354 | (int)sizeof(uint32_t));
|
---|
| 355 | smap.width = mbm->rect.p1.x - mbm->rect.p0.x;
|
---|
| 356 | smap.height = mbm->rect.p1.y - mbm->rect.p0.y;
|
---|
[d8e2485] | 357 | smap.data = mbm->alloc.pixels;
|
---|
| 358 |
|
---|
[dbef30f] | 359 | assert(mbm->mgc->rect.p0.x == 0);
|
---|
| 360 | assert(mbm->mgc->rect.p0.y == 0);
|
---|
| 361 | assert(mbm->mgc->alloc.pitch == mbm->mgc->rect.p1.x * (int)sizeof(uint32_t));
|
---|
| 362 | dmap.width = mbm->mgc->rect.p1.x;
|
---|
| 363 | dmap.height = mbm->mgc->rect.p1.y;
|
---|
[d8e2485] | 364 | dmap.data = mbm->mgc->alloc.pixels;
|
---|
| 365 |
|
---|
[afcf704] | 366 | if ((mbm->flags & bmpf_direct_output) != 0) {
|
---|
| 367 | /* Nothing to do */
|
---|
| 368 | } else if ((mbm->flags & bmpf_color_key) == 0) {
|
---|
[d8e2485] | 369 | for (y = drect.p0.y; y < drect.p1.y; y++) {
|
---|
| 370 | for (x = drect.p0.x; x < drect.p1.x; x++) {
|
---|
[8aef01c] | 371 | pixel = pixelmap_get_pixel(&smap,
|
---|
| 372 | x - mbm->rect.p0.x - offs.x,
|
---|
| 373 | y - mbm->rect.p0.y - offs.y);
|
---|
[d8e2485] | 374 | pixelmap_put_pixel(&dmap, x, y, pixel);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | } else {
|
---|
| 378 | for (y = drect.p0.y; y < drect.p1.y; y++) {
|
---|
| 379 | for (x = drect.p0.x; x < drect.p1.x; x++) {
|
---|
[8aef01c] | 380 | pixel = pixelmap_get_pixel(&smap,
|
---|
| 381 | x - mbm->rect.p0.x - offs.x,
|
---|
| 382 | y - mbm->rect.p0.y - offs.y);
|
---|
[d8e2485] | 383 | if (pixel != mbm->key_color)
|
---|
| 384 | pixelmap_put_pixel(&dmap, x, y, pixel);
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | mem_gc_invalidate_rect(mbm->mgc, &drect);
|
---|
| 390 | return EOK;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /** Get allocation info for bitmap in memory GC.
|
---|
| 394 | *
|
---|
| 395 | * @param bm Bitmap
|
---|
| 396 | * @param alloc Place to store allocation info
|
---|
| 397 | * @return EOK on success or an error code
|
---|
| 398 | */
|
---|
| 399 | static errno_t mem_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 400 | {
|
---|
| 401 | mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
|
---|
| 402 | *alloc = mbm->alloc;
|
---|
| 403 | return EOK;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /** @}
|
---|
| 407 | */
|
---|