| 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 |
|
|---|
| 38 | #include <gfx/bitmap.h>
|
|---|
| 39 | #include <gfx/color.h>
|
|---|
| 40 | #include <gfx/coord.h>
|
|---|
| 41 | #include <gfx/context.h>
|
|---|
| 42 | #include <gfx/render.h>
|
|---|
| 43 | #include <io/log.h>
|
|---|
| 44 | #include <stdlib.h>
|
|---|
| 45 | #include "client.h"
|
|---|
| 46 | #include "display.h"
|
|---|
| 47 | #include "window.h"
|
|---|
| 48 |
|
|---|
| 49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
|---|
| 50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
|---|
| 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 *);
|
|---|
| 56 |
|
|---|
| 57 | gfx_context_ops_t ds_window_ops = {
|
|---|
| 58 | .set_color = ds_window_set_color,
|
|---|
| 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
|
|---|
| 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 | */
|
|---|
| 75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
|---|
| 76 | {
|
|---|
| 77 | ds_window_t *wnd = (ds_window_t *) arg;
|
|---|
| 78 |
|
|---|
| 79 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p",
|
|---|
| 80 | ds_display_get_gc(wnd->display));
|
|---|
| 81 | return gfx_set_color(ds_display_get_gc(wnd->display), color);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /** Fill rectangle on window GC.
|
|---|
| 85 | *
|
|---|
| 86 | * @param arg Window GC
|
|---|
| 87 | * @param rect Rectangle
|
|---|
| 88 | *
|
|---|
| 89 | * @return EOK on success or an error code
|
|---|
| 90 | */
|
|---|
| 91 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
|---|
| 92 | {
|
|---|
| 93 | ds_window_t *wnd = (ds_window_t *) arg;
|
|---|
| 94 | gfx_rect_t crect;
|
|---|
| 95 | gfx_rect_t drect;
|
|---|
| 96 |
|
|---|
| 97 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
|---|
| 98 | gfx_rect_clip(rect, &wnd->rect, &crect);
|
|---|
| 99 | gfx_rect_translate(&wnd->dpos, &crect, &drect);
|
|---|
| 100 | return gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Create bitmap in canvas GC.
|
|---|
| 104 | *
|
|---|
| 105 | * @param arg Canvas GC
|
|---|
| 106 | * @param params Bitmap params
|
|---|
| 107 | * @param alloc Bitmap allocation info or @c NULL
|
|---|
| 108 | * @param rbm Place to store pointer to new bitmap
|
|---|
| 109 | * @return EOK on success or an error code
|
|---|
| 110 | */
|
|---|
| 111 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
|---|
| 112 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
|---|
| 113 | {
|
|---|
| 114 | ds_window_t *wnd = (ds_window_t *) arg;
|
|---|
| 115 | ds_window_bitmap_t *cbm = NULL;
|
|---|
| 116 | errno_t rc;
|
|---|
| 117 |
|
|---|
| 118 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
|---|
| 119 | if (cbm == NULL)
|
|---|
| 120 | return ENOMEM;
|
|---|
| 121 |
|
|---|
| 122 | rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
|
|---|
| 123 | &cbm->bitmap);
|
|---|
| 124 | if (rc != EOK)
|
|---|
| 125 | goto error;
|
|---|
| 126 |
|
|---|
| 127 | cbm->wnd = wnd;
|
|---|
| 128 | cbm->rect = params->rect;
|
|---|
| 129 | *rbm = (void *)cbm;
|
|---|
| 130 | return EOK;
|
|---|
| 131 | error:
|
|---|
| 132 | if (cbm != NULL)
|
|---|
| 133 | free(cbm);
|
|---|
| 134 | return rc;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Destroy bitmap in canvas GC.
|
|---|
| 138 | *
|
|---|
| 139 | * @param bm Bitmap
|
|---|
| 140 | * @return EOK on success or an error code
|
|---|
| 141 | */
|
|---|
| 142 | static errno_t ds_window_bitmap_destroy(void *bm)
|
|---|
| 143 | {
|
|---|
| 144 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
|---|
| 145 |
|
|---|
| 146 | gfx_bitmap_destroy(cbm->bitmap);
|
|---|
| 147 | free(cbm);
|
|---|
| 148 | return EOK;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Render bitmap in canvas GC.
|
|---|
| 152 | *
|
|---|
| 153 | * @param bm Bitmap
|
|---|
| 154 | * @param srect0 Source rectangle or @c NULL
|
|---|
| 155 | * @param offs0 Offset or @c NULL
|
|---|
| 156 | * @return EOK on success or an error code
|
|---|
| 157 | */
|
|---|
| 158 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
|---|
| 159 | gfx_coord2_t *offs0)
|
|---|
| 160 | {
|
|---|
| 161 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
|---|
| 162 | gfx_coord2_t doffs;
|
|---|
| 163 | gfx_coord2_t offs;
|
|---|
| 164 | gfx_rect_t srect;
|
|---|
| 165 | gfx_rect_t swrect;
|
|---|
| 166 | gfx_rect_t crect;
|
|---|
| 167 |
|
|---|
| 168 | if (srect0 != NULL) {
|
|---|
| 169 | /* Clip source rectangle to bitmap rectangle */
|
|---|
| 170 | gfx_rect_clip(srect0, &cbm->rect, &srect);
|
|---|
| 171 | } else {
|
|---|
| 172 | /* Source is entire bitmap rectangle */
|
|---|
| 173 | srect = cbm->rect;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (offs0 != NULL) {
|
|---|
| 177 | offs = *offs0;
|
|---|
| 178 | } else {
|
|---|
| 179 | offs.x = 0;
|
|---|
| 180 | offs.y = 0;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /* Transform window rectangle back to bitmap coordinate system */
|
|---|
| 184 | gfx_rect_rtranslate(&offs, &cbm->wnd->rect, &swrect);
|
|---|
| 185 |
|
|---|
| 186 | /* Clip so that transformed rectangle will be inside the window */
|
|---|
| 187 | gfx_rect_clip(&srect, &swrect, &crect);
|
|---|
| 188 |
|
|---|
| 189 | /* Offset for rendering on screen = window pos + offs */
|
|---|
| 190 | gfx_coord2_add(&cbm->wnd->dpos, &offs, &doffs);
|
|---|
| 191 |
|
|---|
| 192 | return gfx_bitmap_render(cbm->bitmap, srect0, &doffs);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Get allocation info for bitmap in canvas GC.
|
|---|
| 196 | *
|
|---|
| 197 | * @param bm Bitmap
|
|---|
| 198 | * @param alloc Place to store allocation info
|
|---|
| 199 | * @return EOK on success or an error code
|
|---|
| 200 | */
|
|---|
| 201 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
|---|
| 202 | {
|
|---|
| 203 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
|---|
| 204 |
|
|---|
| 205 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /** Create window.
|
|---|
| 209 | *
|
|---|
| 210 | * Create graphics context for rendering into a window.
|
|---|
| 211 | *
|
|---|
| 212 | * @param client Client owning the window
|
|---|
| 213 | * @param params Window parameters
|
|---|
| 214 | * @param rgc Place to store pointer to new GC.
|
|---|
| 215 | *
|
|---|
| 216 | * @return EOK on success or an error code
|
|---|
| 217 | */
|
|---|
| 218 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
|---|
| 219 | ds_window_t **rgc)
|
|---|
| 220 | {
|
|---|
| 221 | ds_window_t *wnd = NULL;
|
|---|
| 222 | gfx_context_t *gc = NULL;
|
|---|
| 223 | errno_t rc;
|
|---|
| 224 |
|
|---|
| 225 | wnd = calloc(1, sizeof(ds_window_t));
|
|---|
| 226 | if (wnd == NULL) {
|
|---|
| 227 | rc = ENOMEM;
|
|---|
| 228 | goto error;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
|---|
| 232 | if (rc != EOK)
|
|---|
| 233 | goto error;
|
|---|
| 234 |
|
|---|
| 235 | ds_client_add_window(client, wnd);
|
|---|
| 236 | ds_display_add_window(client->display, wnd);
|
|---|
| 237 |
|
|---|
| 238 | wnd->rect = params->rect;
|
|---|
| 239 | wnd->gc = gc;
|
|---|
| 240 | *rgc = wnd;
|
|---|
| 241 | return EOK;
|
|---|
| 242 | error:
|
|---|
| 243 | if (wnd != NULL)
|
|---|
| 244 | free(wnd);
|
|---|
| 245 | gfx_context_delete(gc);
|
|---|
| 246 | return rc;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /** Delete window GC.
|
|---|
| 250 | *
|
|---|
| 251 | * @param wnd Window GC
|
|---|
| 252 | */
|
|---|
| 253 | void ds_window_destroy(ds_window_t *wnd)
|
|---|
| 254 | {
|
|---|
| 255 | ds_client_remove_window(wnd);
|
|---|
| 256 | ds_display_remove_window(wnd);
|
|---|
| 257 | (void) gfx_context_delete(wnd->gc);
|
|---|
| 258 |
|
|---|
| 259 | free(wnd);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** Get generic graphic context from window.
|
|---|
| 263 | *
|
|---|
| 264 | * @param wnd Window
|
|---|
| 265 | * @return Graphic context
|
|---|
| 266 | */
|
|---|
| 267 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
|---|
| 268 | {
|
|---|
| 269 | return wnd->gc;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** @}
|
|---|
| 273 | */
|
|---|