[c8cf261] | 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 | /** @addtogroup display
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file GFX window backend
|
---|
| 34 | *
|
---|
| 35 | * This implements a graphics context over display server window.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[0008c0f] | 38 | #include <gfx/bitmap.h>
|
---|
[c8cf261] | 39 | #include <gfx/color.h>
|
---|
[0008c0f] | 40 | #include <gfx/coord.h>
|
---|
[c8cf261] | 41 | #include <gfx/context.h>
|
---|
| 42 | #include <gfx/render.h>
|
---|
| 43 | #include <io/log.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
[b3c185b6] | 45 | #include "client.h"
|
---|
[6af4b4f] | 46 | #include "display.h"
|
---|
| 47 | #include "window.h"
|
---|
[c8cf261] | 48 |
|
---|
[6af4b4f] | 49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
| 50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
[0008c0f] | 51 | static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 52 | gfx_bitmap_alloc_t *, void **);
|
---|
| 53 | static errno_t ds_window_bitmap_destroy(void *);
|
---|
| 54 | static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 55 | static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
[c8cf261] | 56 |
|
---|
[6af4b4f] | 57 | gfx_context_ops_t ds_window_ops = {
|
---|
| 58 | .set_color = ds_window_set_color,
|
---|
[0008c0f] | 59 | .fill_rect = ds_window_fill_rect,
|
---|
| 60 | .bitmap_create = ds_window_bitmap_create,
|
---|
| 61 | .bitmap_destroy = ds_window_bitmap_destroy,
|
---|
| 62 | .bitmap_render = ds_window_bitmap_render,
|
---|
| 63 | .bitmap_get_alloc = ds_window_bitmap_get_alloc
|
---|
[c8cf261] | 64 | };
|
---|
| 65 |
|
---|
| 66 | /** Set color on window GC.
|
---|
| 67 | *
|
---|
| 68 | * Set drawing color on window GC.
|
---|
| 69 | *
|
---|
| 70 | * @param arg Console GC
|
---|
| 71 | * @param color Color
|
---|
| 72 | *
|
---|
| 73 | * @return EOK on success or an error code
|
---|
| 74 | */
|
---|
[6af4b4f] | 75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
[c8cf261] | 76 | {
|
---|
[6af4b4f] | 77 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
[c8cf261] | 78 |
|
---|
[b3c185b6] | 79 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p", wnd->client->display->gc);
|
---|
| 80 | return gfx_set_color(wnd->client->display->gc, color);
|
---|
[c8cf261] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Fill rectangle on window GC.
|
---|
| 84 | *
|
---|
[6af4b4f] | 85 | * @param arg Window GC
|
---|
[c8cf261] | 86 | * @param rect Rectangle
|
---|
| 87 | *
|
---|
| 88 | * @return EOK on success or an error code
|
---|
| 89 | */
|
---|
[6af4b4f] | 90 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
[c8cf261] | 91 | {
|
---|
[6af4b4f] | 92 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
[22faaf2] | 93 | gfx_rect_t drect;
|
---|
[c8cf261] | 94 |
|
---|
| 95 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
[22faaf2] | 96 | gfx_rect_translate(&wnd->dpos, rect, &drect);
|
---|
[b3c185b6] | 97 | return gfx_fill_rect(wnd->client->display->gc, &drect);
|
---|
[c8cf261] | 98 | }
|
---|
| 99 |
|
---|
[0008c0f] | 100 | /** Create bitmap in canvas GC.
|
---|
| 101 | *
|
---|
| 102 | * @param arg Canvas GC
|
---|
| 103 | * @param params Bitmap params
|
---|
| 104 | * @param alloc Bitmap allocation info or @c NULL
|
---|
| 105 | * @param rbm Place to store pointer to new bitmap
|
---|
| 106 | * @return EOK on success or an error code
|
---|
| 107 | */
|
---|
| 108 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 109 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 110 | {
|
---|
| 111 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
| 112 | ds_window_bitmap_t *cbm = NULL;
|
---|
| 113 | errno_t rc;
|
---|
| 114 |
|
---|
| 115 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
---|
| 116 | if (cbm == NULL)
|
---|
| 117 | return ENOMEM;
|
---|
| 118 |
|
---|
[b3c185b6] | 119 | rc = gfx_bitmap_create(wnd->client->display->gc, params, alloc,
|
---|
| 120 | &cbm->bitmap);
|
---|
[0008c0f] | 121 | if (rc != EOK)
|
---|
| 122 | goto error;
|
---|
| 123 |
|
---|
| 124 | cbm->wnd = wnd;
|
---|
| 125 | *rbm = (void *)cbm;
|
---|
| 126 | return EOK;
|
---|
| 127 | error:
|
---|
| 128 | if (cbm != NULL)
|
---|
| 129 | free(cbm);
|
---|
| 130 | return rc;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /** Destroy bitmap in canvas GC.
|
---|
| 134 | *
|
---|
| 135 | * @param bm Bitmap
|
---|
| 136 | * @return EOK on success or an error code
|
---|
| 137 | */
|
---|
| 138 | static errno_t ds_window_bitmap_destroy(void *bm)
|
---|
| 139 | {
|
---|
| 140 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
| 141 |
|
---|
| 142 | gfx_bitmap_destroy(cbm->bitmap);
|
---|
| 143 | free(cbm);
|
---|
| 144 | return EOK;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /** Render bitmap in canvas GC.
|
---|
| 148 | *
|
---|
| 149 | * @param bm Bitmap
|
---|
| 150 | * @param srect0 Source rectangle or @c NULL
|
---|
| 151 | * @param offs0 Offset or @c NULL
|
---|
| 152 | * @return EOK on success or an error code
|
---|
| 153 | */
|
---|
| 154 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
| 155 | gfx_coord2_t *offs0)
|
---|
| 156 | {
|
---|
| 157 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
[22faaf2] | 158 | gfx_coord2_t doffs;
|
---|
[0008c0f] | 159 |
|
---|
[22faaf2] | 160 | if (offs0 != NULL)
|
---|
| 161 | gfx_coord2_add(&cbm->wnd->dpos, offs0, &doffs);
|
---|
| 162 | else
|
---|
| 163 | doffs = cbm->wnd->dpos;
|
---|
| 164 |
|
---|
| 165 | return gfx_bitmap_render(cbm->bitmap, srect0, &doffs);
|
---|
[0008c0f] | 166 | }
|
---|
| 167 |
|
---|
| 168 | /** Get allocation info for bitmap in canvas GC.
|
---|
| 169 | *
|
---|
| 170 | * @param bm Bitmap
|
---|
| 171 | * @param alloc Place to store allocation info
|
---|
| 172 | * @return EOK on success or an error code
|
---|
| 173 | */
|
---|
| 174 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 175 | {
|
---|
| 176 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
| 177 |
|
---|
| 178 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[6af4b4f] | 181 | /** Create window.
|
---|
[c8cf261] | 182 | *
|
---|
| 183 | * Create graphics context for rendering into a window.
|
---|
| 184 | *
|
---|
[b3c185b6] | 185 | * @param client Client owning the window
|
---|
[c8cf261] | 186 | * @param rgc Place to store pointer to new GC.
|
---|
| 187 | *
|
---|
| 188 | * @return EOK on success or an error code
|
---|
| 189 | */
|
---|
[b3c185b6] | 190 | errno_t ds_window_create(ds_client_t *client, ds_window_t **rgc)
|
---|
[c8cf261] | 191 | {
|
---|
[6af4b4f] | 192 | ds_window_t *wnd = NULL;
|
---|
[c8cf261] | 193 | gfx_context_t *gc = NULL;
|
---|
| 194 | errno_t rc;
|
---|
| 195 |
|
---|
[6af4b4f] | 196 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
| 197 | if (wnd == NULL) {
|
---|
[c8cf261] | 198 | rc = ENOMEM;
|
---|
| 199 | goto error;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[6af4b4f] | 202 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
[c8cf261] | 203 | if (rc != EOK)
|
---|
| 204 | goto error;
|
---|
| 205 |
|
---|
[b3c185b6] | 206 | ds_client_add_window(client, wnd);
|
---|
[fd777a2] | 207 | ds_display_add_window(client->display, wnd);
|
---|
[6af4b4f] | 208 |
|
---|
| 209 | wnd->gc = gc;
|
---|
| 210 | *rgc = wnd;
|
---|
[c8cf261] | 211 | return EOK;
|
---|
| 212 | error:
|
---|
[6af4b4f] | 213 | if (wnd != NULL)
|
---|
| 214 | free(wnd);
|
---|
[c8cf261] | 215 | gfx_context_delete(gc);
|
---|
| 216 | return rc;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Delete window GC.
|
---|
| 220 | *
|
---|
[6af4b4f] | 221 | * @param wnd Window GC
|
---|
[c8cf261] | 222 | */
|
---|
[da412547] | 223 | void ds_window_destroy(ds_window_t *wnd)
|
---|
[c8cf261] | 224 | {
|
---|
[b3c185b6] | 225 | ds_client_remove_window(wnd);
|
---|
[fd777a2] | 226 | ds_display_remove_window(wnd);
|
---|
[da412547] | 227 | (void) gfx_context_delete(wnd->gc);
|
---|
[c8cf261] | 228 |
|
---|
[6af4b4f] | 229 | free(wnd);
|
---|
[c8cf261] | 230 | }
|
---|
| 231 |
|
---|
[6af4b4f] | 232 | /** Get generic graphic context from window.
|
---|
[c8cf261] | 233 | *
|
---|
[6af4b4f] | 234 | * @param wnd Window
|
---|
[c8cf261] | 235 | * @return Graphic context
|
---|
| 236 | */
|
---|
[6af4b4f] | 237 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
[c8cf261] | 238 | {
|
---|
[6af4b4f] | 239 | return wnd->gc;
|
---|
[c8cf261] | 240 | }
|
---|
| 241 |
|
---|
| 242 | /** @}
|
---|
| 243 | */
|
---|