source: mainline/uspace/lib/ipcgfx/src/server.c@ 8b1ce56

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b1ce56 was bf22cb78, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Fix 32-bit build

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