source: mainline/uspace/lib/ipcgfx/src/client.c@ 6c2aba3

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

Make bitmaps work with IPC GC / RFB

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