[00e8290] | 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 |
|
---|
[a3f63ac] | 29 | /** @addtogroup libguigfx
|
---|
[00e8290] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file GFX canvas backend
|
---|
| 34 | *
|
---|
| 35 | * This implements a graphics context over a libgui canvas.
|
---|
| 36 | * This is just for experimentation purposes and its kind of backwards.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
[e0545de] | 39 | #include <draw/drawctx.h>
|
---|
| 40 | #include <draw/source.h>
|
---|
[a3f63ac] | 41 | #include <gfx/color.h>
|
---|
[00e8290] | 42 | #include <gfx/context.h>
|
---|
| 43 | #include <gfx/render.h>
|
---|
[a3f63ac] | 44 | #include <guigfx/canvas.h>
|
---|
[00e8290] | 45 | #include <io/pixel.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[e0545de] | 47 | #include <transform.h>
|
---|
[a3f63ac] | 48 | #include "../private/canvas.h"
|
---|
| 49 | //#include "../../private/color.h"
|
---|
[00e8290] | 50 |
|
---|
[f8375f7] | 51 | static void canvas_gc_update_cb(void *, gfx_rect_t *);
|
---|
[00e8290] | 52 |
|
---|
| 53 | /** Create canvas GC.
|
---|
| 54 | *
|
---|
| 55 | * Create graphics context for rendering into a canvas.
|
---|
| 56 | *
|
---|
[d8e2485] | 57 | * @param canvas Canvas object
|
---|
[00e8290] | 58 | * @param fout File to which characters are written (canvas)
|
---|
| 59 | * @param rgc Place to store pointer to new GC.
|
---|
| 60 | *
|
---|
| 61 | * @return EOK on success or an error code
|
---|
| 62 | */
|
---|
| 63 | errno_t canvas_gc_create(canvas_t *canvas, surface_t *surface,
|
---|
| 64 | canvas_gc_t **rgc)
|
---|
| 65 | {
|
---|
| 66 | canvas_gc_t *cgc = NULL;
|
---|
[cea9f0c] | 67 | surface_coord_t w, h;
|
---|
| 68 | gfx_rect_t rect;
|
---|
| 69 | gfx_bitmap_alloc_t alloc;
|
---|
[00e8290] | 70 | errno_t rc;
|
---|
| 71 |
|
---|
[cea9f0c] | 72 | surface_get_resolution(surface, &w, &h);
|
---|
| 73 |
|
---|
[00e8290] | 74 | cgc = calloc(1, sizeof(canvas_gc_t));
|
---|
| 75 | if (cgc == NULL) {
|
---|
| 76 | rc = ENOMEM;
|
---|
| 77 | goto error;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[cea9f0c] | 80 | rect.p0.x = 0;
|
---|
| 81 | rect.p0.y = 0;
|
---|
| 82 | rect.p1.x = w;
|
---|
| 83 | rect.p1.y = h;
|
---|
| 84 |
|
---|
| 85 | alloc.pitch = w * sizeof(uint32_t);
|
---|
| 86 | alloc.off0 = 0;
|
---|
| 87 | alloc.pixels = surface_direct_access(surface);
|
---|
| 88 |
|
---|
[f8375f7] | 89 | rc = mem_gc_create(&rect, &alloc, canvas_gc_update_cb,
|
---|
| 90 | (void *)cgc, &cgc->mgc);
|
---|
[cea9f0c] | 91 | if (rc != EOK)
|
---|
| 92 | goto error;
|
---|
| 93 |
|
---|
[f8375f7] | 94 | cgc->gc = mem_gc_get_ctx(cgc->mgc);
|
---|
[cea9f0c] | 95 |
|
---|
[00e8290] | 96 | cgc->canvas = canvas;
|
---|
| 97 | cgc->surface = surface;
|
---|
| 98 | *rgc = cgc;
|
---|
| 99 | return EOK;
|
---|
| 100 | error:
|
---|
| 101 | if (cgc != NULL)
|
---|
| 102 | free(cgc);
|
---|
| 103 | return rc;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Delete canvas GC.
|
---|
| 107 | *
|
---|
| 108 | * @param cgc Canvas GC
|
---|
| 109 | */
|
---|
| 110 | errno_t canvas_gc_delete(canvas_gc_t *cgc)
|
---|
| 111 | {
|
---|
| 112 | errno_t rc;
|
---|
| 113 |
|
---|
[cea9f0c] | 114 | mem_gc_delete(cgc->mgc);
|
---|
| 115 |
|
---|
[00e8290] | 116 | rc = gfx_context_delete(cgc->gc);
|
---|
| 117 | if (rc != EOK)
|
---|
| 118 | return rc;
|
---|
| 119 |
|
---|
| 120 | free(cgc);
|
---|
| 121 | return EOK;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /** Get generic graphic context from canvas GC.
|
---|
| 125 | *
|
---|
| 126 | * @param cgc Canvas GC
|
---|
| 127 | * @return Graphic context
|
---|
| 128 | */
|
---|
| 129 | gfx_context_t *canvas_gc_get_ctx(canvas_gc_t *cgc)
|
---|
| 130 | {
|
---|
| 131 | return cgc->gc;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[f8375f7] | 134 | /** Canvas GC update callback called by memory GC.
|
---|
[e0545de] | 135 | *
|
---|
| 136 | * @param arg Canvas GC
|
---|
[f8375f7] | 137 | * @param rect Rectangle to update
|
---|
[e0545de] | 138 | */
|
---|
[f8375f7] | 139 | static void canvas_gc_update_cb(void *arg, gfx_rect_t *rect)
|
---|
[e0545de] | 140 | {
|
---|
[f8375f7] | 141 | canvas_gc_t *cgc = (canvas_gc_t *)arg;
|
---|
[cea9f0c] | 142 |
|
---|
[f8375f7] | 143 | update_canvas(cgc->canvas, cgc->surface);
|
---|
[e0545de] | 144 | }
|
---|
| 145 |
|
---|
[00e8290] | 146 | /** @}
|
---|
| 147 | */
|
---|