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 |
|
---|
52 | static ipc_gc_srv_bitmap_t *gc_bitmap_lookup(ipc_gc_srv_t *, sysarg_t);
|
---|
53 |
|
---|
54 | static 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 |
|
---|
75 | static 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 |
|
---|
89 | static 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, ¶ms, 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(¶ms.rect.p1, ¶ms.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, ¶ms, &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=%lu\n", srvbmp->bmp_id);
|
---|
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 |
|
---|
193 | async_answer_0(call, rc);
|
---|
194 | }
|
---|
195 |
|
---|
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 |
|
---|
239 | errno_t gc_conn(ipc_call_t *icall, gfx_context_t *gc)
|
---|
240 | {
|
---|
241 | ipc_gc_srv_t srvgc;
|
---|
242 |
|
---|
243 | /* Accept the connection */
|
---|
244 | async_accept_0(icall);
|
---|
245 |
|
---|
246 | printf("gc_conn: accepted connection\n");
|
---|
247 | srvgc.gc = gc;
|
---|
248 | list_initialize(&srvgc.bitmaps);
|
---|
249 | srvgc.next_bmp_id = 1;
|
---|
250 |
|
---|
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:
|
---|
264 | printf("gc_conn: set_rgb_color\n");
|
---|
265 | gc_set_rgb_color_srv(&srvgc, &call);
|
---|
266 | printf("gc_conn: done set_rgb_color\n");
|
---|
267 | break;
|
---|
268 | case GC_FILL_RECT:
|
---|
269 | printf("gc_conn: fill_rect_srv\n");
|
---|
270 | gc_fill_rect_srv(&srvgc, &call);
|
---|
271 | printf("gc_conn: done fill_rect_srv\n");
|
---|
272 | break;
|
---|
273 | case GC_BITMAP_CREATE:
|
---|
274 | printf("gc_conn: bitmap_create_srv\n");
|
---|
275 | gc_bitmap_create_srv(&srvgc, &call);
|
---|
276 | printf("gc_conn: done bitmap_create_srv\n");
|
---|
277 | break;
|
---|
278 | case GC_BITMAP_DESTROY:
|
---|
279 | printf("gc_conn: bitmap_destroy_srv\n");
|
---|
280 | gc_bitmap_destroy_srv(&srvgc, &call);
|
---|
281 | printf("gc_conn: done bitmap_destroy_srv\n");
|
---|
282 | break;
|
---|
283 | case GC_BITMAP_RENDER:
|
---|
284 | printf("gc_conn: bitmap_render_srv\n");
|
---|
285 | gc_bitmap_render_srv(&srvgc, &call);
|
---|
286 | printf("gc_conn: done bitmap_render_srv\n");
|
---|
287 | break;
|
---|
288 | default:
|
---|
289 | printf("gc_conn: answer einval\n");
|
---|
290 | async_answer_0(&call, EINVAL);
|
---|
291 | printf("gc_conn: done answer einval\n");
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | return EOK;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static ipc_gc_srv_bitmap_t *gc_bitmap_lookup(ipc_gc_srv_t *srvgc,
|
---|
299 | sysarg_t bmp_id)
|
---|
300 | {
|
---|
301 | link_t *link;
|
---|
302 | ipc_gc_srv_bitmap_t *bmp;
|
---|
303 |
|
---|
304 | link = list_first(&srvgc->bitmaps);
|
---|
305 | while (link != NULL) {
|
---|
306 | bmp = list_get_instance(link, ipc_gc_srv_bitmap_t, lbitmaps);
|
---|
307 | printf("gc_bitmap_lookup: %lu==%lu?\n", bmp->bmp_id, bmp_id);
|
---|
308 | if (bmp->bmp_id == bmp_id)
|
---|
309 | return bmp;
|
---|
310 | link = list_next(link, &srvgc->bitmaps);
|
---|
311 | }
|
---|
312 |
|
---|
313 | return NULL;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /** @}
|
---|
317 | */
|
---|