| [aac5069] | 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 libipcgfx
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file GFX IPC server
|
|---|
| 34 | *
|
|---|
| 35 | * Serve a graphics context via HelenOS IPC.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| [0008c0f] | 38 | #include <as.h>
|
|---|
| [aac5069] | 39 | #include <errno.h>
|
|---|
| [0008c0f] | 40 | #include <gfx/bitmap.h>
|
|---|
| [aac5069] | 41 | #include <gfx/color.h>
|
|---|
| 42 | #include <gfx/render.h>
|
|---|
| 43 | #include <ipc/bd.h>
|
|---|
| 44 | #include <ipcgfx/ipc/gc.h>
|
|---|
| 45 | #include <ipcgfx/server.h>
|
|---|
| 46 | #include <stdint.h>
|
|---|
| [0008c0f] | 47 | #include <stdlib.h>
|
|---|
| [c8cf261] | 48 | #include <stdio.h>
|
|---|
| 49 |
|
|---|
| [0008c0f] | 50 | #include "../private/server.h"
|
|---|
| 51 |
|
|---|
| 52 | static ipc_gc_srv_bitmap_t *gc_bitmap_lookup(ipc_gc_srv_t *, sysarg_t);
|
|---|
| [aac5069] | 53 |
|
|---|
| [0008c0f] | 54 | static void gc_set_rgb_color_srv(ipc_gc_srv_t *srvgc, ipc_call_t *call)
|
|---|
| [aac5069] | 55 | {
|
|---|
| 56 | uint16_t r, g, b;
|
|---|
| 57 | gfx_color_t *color;
|
|---|
| 58 | errno_t rc;
|
|---|
| 59 |
|
|---|
| 60 | r = ipc_get_arg1(call);
|
|---|
| 61 | g = ipc_get_arg2(call);
|
|---|
| 62 | b = ipc_get_arg3(call);
|
|---|
| 63 |
|
|---|
| 64 | rc = gfx_color_new_rgb_i16(r, g, b, &color);
|
|---|
| 65 | if (rc != EOK) {
|
|---|
| 66 | async_answer_0(call, ENOMEM);
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| [0008c0f] | 70 | rc = gfx_set_color(srvgc->gc, color);
|
|---|
| [aac5069] | 71 | async_answer_0(call, rc);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| [0008c0f] | 74 | static void gc_fill_rect_srv(ipc_gc_srv_t *srvgc, ipc_call_t *call)
|
|---|
| [aac5069] | 75 | {
|
|---|
| 76 | gfx_rect_t rect;
|
|---|
| 77 | errno_t rc;
|
|---|
| 78 |
|
|---|
| 79 | rect.p0.x = ipc_get_arg1(call);
|
|---|
| 80 | rect.p0.y = ipc_get_arg2(call);
|
|---|
| 81 | rect.p1.x = ipc_get_arg3(call);
|
|---|
| 82 | rect.p1.y = ipc_get_arg4(call);
|
|---|
| 83 |
|
|---|
| [0008c0f] | 84 | rc = gfx_fill_rect(srvgc->gc, &rect);
|
|---|
| 85 | async_answer_0(call, rc);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static void gc_bitmap_create_srv(ipc_gc_srv_t *srvgc, ipc_call_t *icall)
|
|---|
| 89 | {
|
|---|
| 90 | gfx_bitmap_params_t params;
|
|---|
| 91 | gfx_bitmap_alloc_t alloc;
|
|---|
| 92 | gfx_bitmap_t *bitmap;
|
|---|
| 93 | gfx_coord2_t dim;
|
|---|
| 94 | ipc_gc_srv_bitmap_t *srvbmp = NULL;
|
|---|
| 95 | ipc_call_t call;
|
|---|
| 96 | size_t size;
|
|---|
| 97 | unsigned int flags;
|
|---|
| 98 | void *pixels;
|
|---|
| 99 | errno_t rc;
|
|---|
| 100 |
|
|---|
| 101 | if (!async_data_write_receive(&call, &size)) {
|
|---|
| 102 | async_answer_0(&call, EREFUSED);
|
|---|
| 103 | async_answer_0(icall, EREFUSED);
|
|---|
| 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (size != sizeof(gfx_bitmap_params_t)) {
|
|---|
| 108 | async_answer_0(&call, EINVAL);
|
|---|
| 109 | async_answer_0(icall, EINVAL);
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | rc = async_data_write_finalize(&call, ¶ms, size);
|
|---|
| 114 | if (rc != EOK) {
|
|---|
| 115 | async_answer_0(&call, rc);
|
|---|
| 116 | async_answer_0(icall, rc);
|
|---|
| 117 | return;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /* Bitmap dimensions */
|
|---|
| 121 | gfx_coord2_subtract(¶ms.rect.p1, ¶ms.rect.p0, &dim);
|
|---|
| 122 |
|
|---|
| 123 | if (!async_share_out_receive(&call, &size, &flags)) {
|
|---|
| 124 | async_answer_0(icall, EINVAL);
|
|---|
| 125 | return;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /* Check size */
|
|---|
| 129 | if (size != PAGES2SIZE(SIZE2PAGES(dim.x * dim.y * sizeof(uint32_t)))) {
|
|---|
| 130 | printf("size=%zu, expected=%zu\n", size, dim.x * dim.y * sizeof(uint32_t));
|
|---|
| 131 | async_answer_0(icall, EINVAL);
|
|---|
| 132 | return;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | rc = async_share_out_finalize(&call, &pixels);
|
|---|
| 136 | if (rc != EOK || pixels == AS_MAP_FAILED) {
|
|---|
| 137 | async_answer_0(icall, ENOMEM);
|
|---|
| 138 | return;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | alloc.pitch = dim.x * sizeof(uint32_t);
|
|---|
| 142 | alloc.off0 = 0;
|
|---|
| 143 | alloc.pixels = pixels;
|
|---|
| 144 |
|
|---|
| 145 | srvbmp = calloc(1, sizeof(ipc_gc_srv_bitmap_t));
|
|---|
| 146 | if (srvbmp == NULL) {
|
|---|
| 147 | as_area_destroy(pixels);
|
|---|
| 148 | async_answer_0(icall, ENOMEM);
|
|---|
| 149 | return;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | rc = gfx_bitmap_create(srvgc->gc, ¶ms, &alloc, &bitmap);
|
|---|
| 153 | if (rc != EOK) {
|
|---|
| 154 | free(srvbmp);
|
|---|
| 155 | as_area_destroy(pixels);
|
|---|
| 156 | async_answer_0(icall, rc);
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | srvbmp->srvgc = srvgc;
|
|---|
| 161 | list_append(&srvbmp->lbitmaps, &srvgc->bitmaps);
|
|---|
| 162 | srvbmp->bmp = bitmap;
|
|---|
| 163 | srvbmp->bmp_id = srvgc->next_bmp_id++;
|
|---|
| [bf22cb78] | 164 | printf("gc_bitmap_create_srv: storing bmp_id=%u\n",
|
|---|
| 165 | (unsigned) srvbmp->bmp_id);
|
|---|
| [0008c0f] | 166 |
|
|---|
| 167 | async_answer_1(icall, EOK, srvbmp->bmp_id);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | static void gc_bitmap_destroy_srv(ipc_gc_srv_t *srvgc, ipc_call_t *call)
|
|---|
| 171 | {
|
|---|
| 172 | sysarg_t bmp_id;
|
|---|
| 173 | ipc_gc_srv_bitmap_t *bitmap;
|
|---|
| 174 | errno_t rc;
|
|---|
| 175 |
|
|---|
| 176 | bmp_id = ipc_get_arg1(call);
|
|---|
| 177 |
|
|---|
| 178 | bitmap = gc_bitmap_lookup(srvgc, bmp_id);
|
|---|
| 179 | if (bitmap == NULL) {
|
|---|
| 180 | async_answer_0(call, ENOENT);
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | rc = gfx_bitmap_destroy(bitmap->bmp);
|
|---|
| 185 | if (rc != EOK) {
|
|---|
| 186 | async_answer_0(call, rc);
|
|---|
| 187 | return;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | list_remove(&bitmap->lbitmaps);
|
|---|
| 191 | free(bitmap);
|
|---|
| 192 |
|
|---|
| [aac5069] | 193 | async_answer_0(call, rc);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| [0008c0f] | 196 | static void gc_bitmap_render_srv(ipc_gc_srv_t *srvgc, ipc_call_t *icall)
|
|---|
| 197 | {
|
|---|
| 198 | ipc_gc_srv_bitmap_t *bitmap;
|
|---|
| 199 | sysarg_t bmp_id;
|
|---|
| 200 | gfx_rect_t srect;
|
|---|
| 201 | gfx_coord2_t offs;
|
|---|
| 202 | ipc_call_t call;
|
|---|
| 203 | size_t size;
|
|---|
| 204 | errno_t rc;
|
|---|
| 205 |
|
|---|
| 206 | if (!async_data_write_receive(&call, &size)) {
|
|---|
| 207 | async_answer_0(&call, EREFUSED);
|
|---|
| 208 | async_answer_0(icall, EREFUSED);
|
|---|
| 209 | return;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | if (size != sizeof(gfx_rect_t)) {
|
|---|
| 213 | async_answer_0(&call, EINVAL);
|
|---|
| 214 | async_answer_0(icall, EINVAL);
|
|---|
| 215 | return;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | rc = async_data_write_finalize(&call, &srect, size);
|
|---|
| 219 | if (rc != EOK) {
|
|---|
| 220 | async_answer_0(&call, rc);
|
|---|
| 221 | async_answer_0(icall, rc);
|
|---|
| 222 | return;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | bmp_id = ipc_get_arg1(icall);
|
|---|
| 226 | offs.x = ipc_get_arg2(icall);
|
|---|
| 227 | offs.y = ipc_get_arg3(icall);
|
|---|
| 228 |
|
|---|
| 229 | bitmap = gc_bitmap_lookup(srvgc, bmp_id);
|
|---|
| 230 | if (bitmap == NULL) {
|
|---|
| 231 | async_answer_0(icall, ENOENT);
|
|---|
| 232 | return;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | rc = gfx_bitmap_render(bitmap->bmp, &srect, &offs);
|
|---|
| 236 | async_answer_0(icall, rc);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| [aac5069] | 239 | errno_t gc_conn(ipc_call_t *icall, gfx_context_t *gc)
|
|---|
| 240 | {
|
|---|
| [0008c0f] | 241 | ipc_gc_srv_t srvgc;
|
|---|
| 242 |
|
|---|
| [aac5069] | 243 | /* Accept the connection */
|
|---|
| 244 | async_accept_0(icall);
|
|---|
| 245 |
|
|---|
| [c8cf261] | 246 | printf("gc_conn: accepted connection\n");
|
|---|
| [0008c0f] | 247 | srvgc.gc = gc;
|
|---|
| 248 | list_initialize(&srvgc.bitmaps);
|
|---|
| 249 | srvgc.next_bmp_id = 1;
|
|---|
| [c8cf261] | 250 |
|
|---|
| [aac5069] | 251 | while (true) {
|
|---|
| 252 | ipc_call_t call;
|
|---|
| 253 | async_get_call(&call);
|
|---|
| 254 | sysarg_t method = ipc_get_imethod(&call);
|
|---|
| 255 |
|
|---|
| 256 | if (!method) {
|
|---|
| 257 | /* The other side has hung up */
|
|---|
| 258 | async_answer_0(&call, EOK);
|
|---|
| 259 | break;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | switch (method) {
|
|---|
| 263 | case GC_SET_RGB_COLOR:
|
|---|
| [0008c0f] | 264 | gc_set_rgb_color_srv(&srvgc, &call);
|
|---|
| [aac5069] | 265 | break;
|
|---|
| 266 | case GC_FILL_RECT:
|
|---|
| [0008c0f] | 267 | gc_fill_rect_srv(&srvgc, &call);
|
|---|
| [aac5069] | 268 | break;
|
|---|
| [0008c0f] | 269 | case GC_BITMAP_CREATE:
|
|---|
| 270 | gc_bitmap_create_srv(&srvgc, &call);
|
|---|
| 271 | break;
|
|---|
| 272 | case GC_BITMAP_DESTROY:
|
|---|
| 273 | gc_bitmap_destroy_srv(&srvgc, &call);
|
|---|
| 274 | break;
|
|---|
| 275 | case GC_BITMAP_RENDER:
|
|---|
| 276 | gc_bitmap_render_srv(&srvgc, &call);
|
|---|
| 277 | break;
|
|---|
| [aac5069] | 278 | default:
|
|---|
| 279 | async_answer_0(&call, EINVAL);
|
|---|
| [48f7e47] | 280 | break;
|
|---|
| [aac5069] | 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | return EOK;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| [0008c0f] | 287 | static ipc_gc_srv_bitmap_t *gc_bitmap_lookup(ipc_gc_srv_t *srvgc,
|
|---|
| 288 | sysarg_t bmp_id)
|
|---|
| 289 | {
|
|---|
| 290 | link_t *link;
|
|---|
| 291 | ipc_gc_srv_bitmap_t *bmp;
|
|---|
| 292 |
|
|---|
| 293 | link = list_first(&srvgc->bitmaps);
|
|---|
| 294 | while (link != NULL) {
|
|---|
| 295 | bmp = list_get_instance(link, ipc_gc_srv_bitmap_t, lbitmaps);
|
|---|
| 296 | if (bmp->bmp_id == bmp_id)
|
|---|
| 297 | return bmp;
|
|---|
| 298 | link = list_next(link, &srvgc->bitmaps);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return NULL;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| [aac5069] | 304 | /** @}
|
|---|
| 305 | */
|
|---|