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 backend
|
---|
34 | *
|
---|
35 | * This implements a graphics context via HelenOS IPC.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <as.h>
|
---|
39 | #include <ipcgfx/client.h>
|
---|
40 | #include <ipcgfx/ipc/gc.h>
|
---|
41 | #include <gfx/color.h>
|
---|
42 | #include <gfx/coord.h>
|
---|
43 | #include <gfx/context.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include "../private/client.h"
|
---|
46 |
|
---|
47 | static errno_t ipc_gc_set_color(void *, gfx_color_t *);
|
---|
48 | static errno_t ipc_gc_fill_rect(void *, gfx_rect_t *);
|
---|
49 | static errno_t ipc_gc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
50 | gfx_bitmap_alloc_t *, void **);
|
---|
51 | static errno_t ipc_gc_bitmap_destroy(void *);
|
---|
52 | static errno_t ipc_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
53 | static errno_t ipc_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
54 |
|
---|
55 | gfx_context_ops_t ipc_gc_ops = {
|
---|
56 | .set_color = ipc_gc_set_color,
|
---|
57 | .fill_rect = ipc_gc_fill_rect,
|
---|
58 | .bitmap_create = ipc_gc_bitmap_create,
|
---|
59 | .bitmap_destroy = ipc_gc_bitmap_destroy,
|
---|
60 | .bitmap_render = ipc_gc_bitmap_render,
|
---|
61 | .bitmap_get_alloc = ipc_gc_bitmap_get_alloc
|
---|
62 | };
|
---|
63 |
|
---|
64 | #include <stdio.h>
|
---|
65 |
|
---|
66 | /** Set color on IPC GC.
|
---|
67 | *
|
---|
68 | * Set drawing color on IPC GC.
|
---|
69 | *
|
---|
70 | * @param arg IPC GC
|
---|
71 | * @param color Color
|
---|
72 | *
|
---|
73 | * @return EOK on success or an error code
|
---|
74 | */
|
---|
75 | static errno_t ipc_gc_set_color(void *arg, gfx_color_t *color)
|
---|
76 | {
|
---|
77 | ipc_gc_t *ipcgc = (ipc_gc_t *) arg;
|
---|
78 | async_exch_t *exch;
|
---|
79 | uint16_t r, g, b;
|
---|
80 | errno_t rc;
|
---|
81 |
|
---|
82 | printf("ipc_gc_set_color\n");
|
---|
83 | gfx_color_get_rgb_i16(color, &r, &g, &b);
|
---|
84 |
|
---|
85 | exch = async_exchange_begin(ipcgc->sess);
|
---|
86 | rc = async_req_3_0(exch, GC_SET_RGB_COLOR, r, g, b);
|
---|
87 | async_exchange_end(exch);
|
---|
88 |
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Fill rectangle on IPC GC.
|
---|
93 | *
|
---|
94 | * @param arg IPC GC
|
---|
95 | * @param rect Rectangle
|
---|
96 | *
|
---|
97 | * @return EOK on success or an error code
|
---|
98 | */
|
---|
99 | static errno_t ipc_gc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
100 | {
|
---|
101 | ipc_gc_t *ipcgc = (ipc_gc_t *) arg;
|
---|
102 | async_exch_t *exch;
|
---|
103 | errno_t rc;
|
---|
104 |
|
---|
105 | printf("ipc_gc_fill_rect\n");
|
---|
106 | exch = async_exchange_begin(ipcgc->sess);
|
---|
107 | rc = async_req_4_0(exch, GC_FILL_RECT, rect->p0.x, rect->p0.y,
|
---|
108 | rect->p1.x, rect->p1.y);
|
---|
109 | async_exchange_end(exch);
|
---|
110 |
|
---|
111 | return rc;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Create bitmap in IPC GC.
|
---|
115 | *
|
---|
116 | * @param arg IPC GC
|
---|
117 | * @param params Bitmap params
|
---|
118 | * @param alloc Bitmap allocation info or @c NULL
|
---|
119 | * @param rbm Place to store pointer to new bitmap
|
---|
120 | * @return EOK on success or an error code
|
---|
121 | */
|
---|
122 | errno_t ipc_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
123 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
124 | {
|
---|
125 | ipc_gc_t *ipcgc = (ipc_gc_t *) arg;
|
---|
126 | ipc_gc_bitmap_t *ipcbm = NULL;
|
---|
127 | gfx_coord2_t dim;
|
---|
128 | async_exch_t *exch = NULL;
|
---|
129 | ipc_call_t answer;
|
---|
130 | aid_t req;
|
---|
131 | errno_t rc;
|
---|
132 |
|
---|
133 | ipcbm = calloc(1, sizeof(ipc_gc_bitmap_t));
|
---|
134 | if (ipcbm == NULL)
|
---|
135 | return ENOMEM;
|
---|
136 |
|
---|
137 | gfx_coord2_subtract(¶ms->rect.p1, ¶ms->rect.p0, &dim);
|
---|
138 | ipcbm->rect = params->rect;
|
---|
139 |
|
---|
140 | if (alloc == NULL) {
|
---|
141 | ipcbm->alloc.pitch = dim.x * sizeof(uint32_t);
|
---|
142 | ipcbm->alloc.off0 = 0;
|
---|
143 | ipcbm->alloc.pixels = as_area_create(AS_AREA_ANY,
|
---|
144 | dim.x * dim.y * sizeof(uint32_t), AS_AREA_READ |
|
---|
145 | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
|
---|
146 | if (ipcbm->alloc.pixels == NULL) {
|
---|
147 | rc = ENOMEM;
|
---|
148 | goto error;
|
---|
149 | }
|
---|
150 |
|
---|
151 | ipcbm->myalloc = true;
|
---|
152 | } else {
|
---|
153 | /*
|
---|
154 | * XXX We could allow this if the pixels point to a shareable
|
---|
155 | * area or we could do a copy of the data when rendering
|
---|
156 | */
|
---|
157 | rc = ENOTSUP;
|
---|
158 | goto error;
|
---|
159 | }
|
---|
160 |
|
---|
161 | exch = async_exchange_begin(ipcgc->sess);
|
---|
162 | req = async_send_0(exch, GC_BITMAP_CREATE, &answer);
|
---|
163 | rc = async_data_write_start(exch, params, sizeof (gfx_bitmap_params_t));
|
---|
164 | if (rc != EOK) {
|
---|
165 | async_forget(req);
|
---|
166 | goto error;
|
---|
167 | }
|
---|
168 |
|
---|
169 | rc = async_share_out_start(exch, ipcbm->alloc.pixels,
|
---|
170 | AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
171 | if (rc != EOK) {
|
---|
172 | async_forget(req);
|
---|
173 | goto error;
|
---|
174 | }
|
---|
175 | async_exchange_end(exch);
|
---|
176 | exch = NULL;
|
---|
177 |
|
---|
178 | async_wait_for(req, &rc);
|
---|
179 | if (rc != EOK)
|
---|
180 | goto error;
|
---|
181 |
|
---|
182 | ipcbm->ipcgc = ipcgc;
|
---|
183 | ipcbm->bmp_id = ipc_get_arg1(&answer);
|
---|
184 | *rbm = (void *)ipcbm;
|
---|
185 | return EOK;
|
---|
186 | error:
|
---|
187 | if (exch != NULL)
|
---|
188 | async_exchange_end(exch);
|
---|
189 | if (ipcbm != NULL) {
|
---|
190 | if (ipcbm->alloc.pixels != NULL)
|
---|
191 | as_area_destroy(ipcbm->alloc.pixels);
|
---|
192 | free(ipcbm);
|
---|
193 | }
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Destroy bitmap in IPC GC.
|
---|
198 | *
|
---|
199 | * @param bm Bitmap
|
---|
200 | * @return EOK on success or an error code
|
---|
201 | */
|
---|
202 | static errno_t ipc_gc_bitmap_destroy(void *bm)
|
---|
203 | {
|
---|
204 | ipc_gc_bitmap_t *ipcbm = (ipc_gc_bitmap_t *)bm;
|
---|
205 | async_exch_t *exch;
|
---|
206 | errno_t rc;
|
---|
207 |
|
---|
208 | printf("ipc_gc_bitmap_destroy\n");
|
---|
209 |
|
---|
210 | exch = async_exchange_begin(ipcbm->ipcgc->sess);
|
---|
211 | rc = async_req_1_0(exch, GC_BITMAP_DESTROY, ipcbm->bmp_id);
|
---|
212 | async_exchange_end(exch);
|
---|
213 |
|
---|
214 | if (rc != EOK)
|
---|
215 | return rc;
|
---|
216 |
|
---|
217 | if (ipcbm->myalloc)
|
---|
218 | as_area_destroy(ipcbm->alloc.pixels);
|
---|
219 | free(ipcbm);
|
---|
220 | return EOK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Render bitmap in IPC GC.
|
---|
224 | *
|
---|
225 | * @param bm Bitmap
|
---|
226 | * @param srect0 Source rectangle or @c NULL
|
---|
227 | * @param offs0 Offset or @c NULL
|
---|
228 | * @return EOK on success or an error code
|
---|
229 | */
|
---|
230 | static errno_t ipc_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
231 | gfx_coord2_t *offs0)
|
---|
232 | {
|
---|
233 | ipc_gc_bitmap_t *ipcbm = (ipc_gc_bitmap_t *)bm;
|
---|
234 | gfx_rect_t srect;
|
---|
235 | gfx_rect_t drect;
|
---|
236 | gfx_coord2_t offs;
|
---|
237 | async_exch_t *exch = NULL;
|
---|
238 | ipc_call_t answer;
|
---|
239 | aid_t req;
|
---|
240 | errno_t rc;
|
---|
241 |
|
---|
242 | if (srect0 != NULL)
|
---|
243 | srect = *srect0;
|
---|
244 | else
|
---|
245 | srect = ipcbm->rect;
|
---|
246 |
|
---|
247 | if (offs0 != NULL) {
|
---|
248 | offs = *offs0;
|
---|
249 | } else {
|
---|
250 | offs.x = 0;
|
---|
251 | offs.y = 0;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /* Destination rectangle */
|
---|
255 | gfx_rect_translate(&offs, &srect, &drect);
|
---|
256 |
|
---|
257 | exch = async_exchange_begin(ipcbm->ipcgc->sess);
|
---|
258 | req = async_send_3(exch, GC_BITMAP_RENDER, ipcbm->bmp_id, offs.x,
|
---|
259 | offs.y, &answer);
|
---|
260 |
|
---|
261 | rc = async_data_write_start(exch, &srect, sizeof (gfx_rect_t));
|
---|
262 | if (rc != EOK) {
|
---|
263 | async_forget(req);
|
---|
264 | goto error;
|
---|
265 | }
|
---|
266 |
|
---|
267 | async_exchange_end(exch);
|
---|
268 | exch = NULL;
|
---|
269 |
|
---|
270 | async_wait_for(req, &rc);
|
---|
271 | if (rc != EOK)
|
---|
272 | goto error;
|
---|
273 |
|
---|
274 | return EOK;
|
---|
275 | error:
|
---|
276 | if (exch != NULL)
|
---|
277 | async_exchange_end(exch);
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** Get allocation info for bitmap in IPC GC.
|
---|
282 | *
|
---|
283 | * @param bm Bitmap
|
---|
284 | * @param alloc Place to store allocation info
|
---|
285 | * @return EOK on success or an error code
|
---|
286 | */
|
---|
287 | static errno_t ipc_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
288 | {
|
---|
289 | ipc_gc_bitmap_t *ipcbm = (ipc_gc_bitmap_t *)bm;
|
---|
290 | *alloc = ipcbm->alloc;
|
---|
291 | return EOK;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** Create IPC GC.
|
---|
295 | *
|
---|
296 | * Create graphics context for rendering via IPC.
|
---|
297 | *
|
---|
298 | * @param sess Async session
|
---|
299 | * @param rgc Place to store pointer to new GC.
|
---|
300 | *
|
---|
301 | * @return EOK on success or an error code
|
---|
302 | */
|
---|
303 | errno_t ipc_gc_create(async_sess_t *sess, ipc_gc_t **rgc)
|
---|
304 | {
|
---|
305 | ipc_gc_t *ipcgc = NULL;
|
---|
306 | gfx_context_t *gc = NULL;
|
---|
307 | errno_t rc;
|
---|
308 |
|
---|
309 | ipcgc = calloc(1, sizeof(ipc_gc_t));
|
---|
310 | if (ipcgc == NULL) {
|
---|
311 | rc = ENOMEM;
|
---|
312 | goto error;
|
---|
313 | }
|
---|
314 |
|
---|
315 | rc = gfx_context_new(&ipc_gc_ops, ipcgc, &gc);
|
---|
316 | if (rc != EOK)
|
---|
317 | goto error;
|
---|
318 |
|
---|
319 | ipcgc->gc = gc;
|
---|
320 | ipcgc->sess = sess;
|
---|
321 | *rgc = ipcgc;
|
---|
322 | return EOK;
|
---|
323 | error:
|
---|
324 | if (ipcgc != NULL)
|
---|
325 | free(ipcgc);
|
---|
326 | gfx_context_delete(gc);
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /** Delete IPC GC.
|
---|
331 | *
|
---|
332 | * @param ipcgc IPC GC
|
---|
333 | */
|
---|
334 | errno_t ipc_gc_delete(ipc_gc_t *ipcgc)
|
---|
335 | {
|
---|
336 | errno_t rc;
|
---|
337 |
|
---|
338 | rc = gfx_context_delete(ipcgc->gc);
|
---|
339 | if (rc != EOK)
|
---|
340 | return rc;
|
---|
341 |
|
---|
342 | free(ipcgc);
|
---|
343 | return EOK;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Get generic graphic context from IPC GC.
|
---|
347 | *
|
---|
348 | * @param ipcgc IPC GC
|
---|
349 | * @return Graphic context
|
---|
350 | */
|
---|
351 | gfx_context_t *ipc_gc_get_ctx(ipc_gc_t *ipcgc)
|
---|
352 | {
|
---|
353 | return ipcgc->gc;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /** @}
|
---|
357 | */
|
---|