source: mainline/uspace/lib/memgfx/src/memgc.c@ 241ab7e

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was 1215db9, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Memory GC needs to be able to forward cursor control

  • Property mode set to 100644
File size: 12.9 KB
Line 
1/*
2 * Copyright (c) 2021 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 libmemgfx
30 * @{
31 */
32/**
33 * @file GFX memory backend
34 *
35 * This implements a graphics context over a block of memory (i.e. a simple
36 * software renderer).
37 */
38
39#include <as.h>
40#include <assert.h>
41#include <gfx/color.h>
42#include <gfx/context.h>
43#include <gfx/render.h>
44#include <io/pixel.h>
45#include <io/pixelmap.h>
46#include <memgfx/memgc.h>
47#include <stdlib.h>
48#include "../private/memgc.h"
49
50static errno_t mem_gc_set_clip_rect(void *, gfx_rect_t *);
51static errno_t mem_gc_set_color(void *, gfx_color_t *);
52static errno_t mem_gc_fill_rect(void *, gfx_rect_t *);
53static errno_t mem_gc_update(void *);
54static errno_t mem_gc_bitmap_create(void *, gfx_bitmap_params_t *,
55 gfx_bitmap_alloc_t *, void **);
56static errno_t mem_gc_bitmap_destroy(void *);
57static errno_t mem_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
58static errno_t mem_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
59static errno_t mem_gc_cursor_get_pos(void *, gfx_coord2_t *);
60static errno_t mem_gc_cursor_set_pos(void *, gfx_coord2_t *);
61static errno_t mem_gc_cursor_set_visible(void *, bool);
62static void mem_gc_invalidate_rect(mem_gc_t *, gfx_rect_t *);
63
64gfx_context_ops_t mem_gc_ops = {
65 .set_clip_rect = mem_gc_set_clip_rect,
66 .set_color = mem_gc_set_color,
67 .fill_rect = mem_gc_fill_rect,
68 .update = mem_gc_update,
69 .bitmap_create = mem_gc_bitmap_create,
70 .bitmap_destroy = mem_gc_bitmap_destroy,
71 .bitmap_render = mem_gc_bitmap_render,
72 .bitmap_get_alloc = mem_gc_bitmap_get_alloc,
73 .cursor_get_pos = mem_gc_cursor_get_pos,
74 .cursor_set_pos = mem_gc_cursor_set_pos,
75 .cursor_set_visible = mem_gc_cursor_set_visible
76};
77
78/** Set clipping rectangle on memory GC.
79 *
80 * @param arg Memory GC
81 * @param rect Rectangle
82 *
83 * @return EOK on success or an error code
84 */
85static errno_t mem_gc_set_clip_rect(void *arg, gfx_rect_t *rect)
86{
87 mem_gc_t *mgc = (mem_gc_t *) arg;
88
89 if (rect != NULL)
90 gfx_rect_clip(rect, &mgc->rect, &mgc->clip_rect);
91 else
92 mgc->clip_rect = mgc->rect;
93
94 return EOK;
95}
96
97/** Set color on memory GC.
98 *
99 * Set drawing color on memory GC.
100 *
101 * @param arg Memory GC
102 * @param color Color
103 *
104 * @return EOK on success or an error code
105 */
106static errno_t mem_gc_set_color(void *arg, gfx_color_t *color)
107{
108 mem_gc_t *mgc = (mem_gc_t *) arg;
109 uint16_t r, g, b;
110
111 gfx_color_get_rgb_i16(color, &r, &g, &b);
112 mgc->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
113 return EOK;
114}
115
116/** Fill rectangle on memory GC.
117 *
118 * @param arg Memory GC
119 * @param rect Rectangle
120 *
121 * @return EOK on success or an error code
122 */
123static errno_t mem_gc_fill_rect(void *arg, gfx_rect_t *rect)
124{
125 mem_gc_t *mgc = (mem_gc_t *) arg;
126 gfx_rect_t crect;
127 gfx_coord_t x, y;
128 pixelmap_t pixelmap;
129
130 /* Make sure we have a sorted, clipped rectangle */
131 gfx_rect_clip(rect, &mgc->clip_rect, &crect);
132
133 assert(mgc->rect.p0.x == 0);
134 assert(mgc->rect.p0.y == 0);
135 assert(mgc->alloc.pitch == mgc->rect.p1.x * (int)sizeof(uint32_t));
136 pixelmap.width = mgc->rect.p1.x;
137 pixelmap.height = mgc->rect.p1.y;
138 pixelmap.data = mgc->alloc.pixels;
139
140 for (y = crect.p0.y; y < crect.p1.y; y++) {
141 for (x = crect.p0.x; x < crect.p1.x; x++) {
142 pixelmap_put_pixel(&pixelmap, x, y, mgc->color);
143 }
144 }
145
146 mem_gc_invalidate_rect(mgc, &crect);
147 return EOK;
148}
149
150/** Update memory GC.
151 *
152 * @param arg Memory GC
153 *
154 * @return EOK on success or an error code
155 */
156static errno_t mem_gc_update(void *arg)
157{
158 mem_gc_t *mgc = (mem_gc_t *) arg;
159
160 mgc->cb->update(mgc->cb_arg);
161 return EOK;
162}
163
164/** Create memory GC.
165 *
166 * Create graphics context for rendering into a block of memory.
167 *
168 * @param rect Bounding rectangle
169 * @param alloc Allocation info
170 * @param cb Pointer to memory GC callbacks
171 * @param cb_arg Argument to callback functions
172 * @param rgc Place to store pointer to new memory GC
173 *
174 * @return EOK on success or an error code
175 */
176errno_t mem_gc_create(gfx_rect_t *rect, gfx_bitmap_alloc_t *alloc,
177 mem_gc_cb_t *cb, void *cb_arg, mem_gc_t **rgc)
178{
179 mem_gc_t *mgc = NULL;
180 gfx_context_t *gc = NULL;
181 errno_t rc;
182
183 mgc = calloc(1, sizeof(mem_gc_t));
184 if (mgc == NULL) {
185 rc = ENOMEM;
186 goto error;
187 }
188
189 rc = gfx_context_new(&mem_gc_ops, mgc, &gc);
190 if (rc != EOK)
191 goto error;
192
193 mgc->gc = gc;
194 mgc->rect = *rect;
195 mgc->clip_rect = *rect;
196 mgc->alloc = *alloc;
197
198 mgc->cb = cb;
199 mgc->cb_arg = cb_arg;
200
201 *rgc = mgc;
202 return EOK;
203error:
204 if (mgc != NULL)
205 free(mgc);
206 gfx_context_delete(gc);
207 return rc;
208}
209
210/** Delete memory GC.
211 *
212 * @param mgc Memory GC
213 */
214errno_t mem_gc_delete(mem_gc_t *mgc)
215{
216 errno_t rc;
217
218 rc = gfx_context_delete(mgc->gc);
219 if (rc != EOK)
220 return rc;
221
222 free(mgc);
223 return EOK;
224}
225
226/** Retarget memory GC to a different block of memory.
227 *
228 * @param mgc Memory GC
229 * @param rect New bounding rectangle
230 * @param alloc Allocation info of the new block
231 */
232void mem_gc_retarget(mem_gc_t *mgc, gfx_rect_t *rect,
233 gfx_bitmap_alloc_t *alloc)
234{
235 mgc->rect = *rect;
236 mgc->clip_rect = *rect;
237 mgc->alloc = *alloc;
238}
239
240/** Get generic graphic context from memory GC.
241 *
242 * @param mgc Memory GC
243 * @return Graphic context
244 */
245gfx_context_t *mem_gc_get_ctx(mem_gc_t *mgc)
246{
247 return mgc->gc;
248}
249
250static void mem_gc_invalidate_rect(mem_gc_t *mgc, gfx_rect_t *rect)
251{
252 mgc->cb->invalidate(mgc->cb_arg, rect);
253}
254
255/** Create bitmap in memory GC.
256 *
257 * @param arg Memory GC
258 * @param params Bitmap params
259 * @param alloc Bitmap allocation info or @c NULL
260 * @param rbm Place to store pointer to new bitmap
261 * @return EOK on success or an error code
262 */
263errno_t mem_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
264 gfx_bitmap_alloc_t *alloc, void **rbm)
265{
266 mem_gc_t *mgc = (mem_gc_t *) arg;
267 mem_gc_bitmap_t *mbm = NULL;
268 gfx_coord2_t dim;
269 errno_t rc;
270
271 /* Check that we support all requested flags */
272 if ((params->flags & ~(bmpf_color_key | bmpf_colorize |
273 bmpf_direct_output)) != 0)
274 return ENOTSUP;
275
276 mbm = calloc(1, sizeof(mem_gc_bitmap_t));
277 if (mbm == NULL)
278 return ENOMEM;
279
280 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
281 mbm->rect = params->rect;
282 mbm->flags = params->flags;
283 mbm->key_color = params->key_color;
284
285 if ((params->flags & bmpf_direct_output) != 0) {
286 /* Caller cannot specify allocation for direct output */
287 if (alloc != NULL) {
288 rc = EINVAL;
289 goto error;
290 }
291
292 /* Bounding rectangle must be within GC bounding rectangle */
293 if (!gfx_rect_is_inside(&mbm->rect, &mgc->rect)) {
294 rc = EINVAL;
295 goto error;
296 }
297
298 mbm->alloc = mgc->alloc;
299
300 /* Don't free pixel array when destroying bitmap */
301 mbm->myalloc = false;
302 } else if (alloc == NULL) {
303#if 0
304 /*
305 * TODO: If the bitmap is not required to be sharable,
306 * we could allocate it with a simple malloc.
307 * Need to have a bitmap flag specifying that the
308 * allocation should be sharable. IPC GC could
309 * automatically add this flag
310 */
311 mbm->alloc.pitch = dim.x * sizeof(uint32_t);
312 mbm->alloc.off0 = 0;
313 mbm->alloc.pixels = malloc(mbm->alloc.pitch * dim.y);
314 mbm->myalloc = true;
315
316 if (mbm->alloc.pixels == NULL) {
317 rc = ENOMEM;
318 goto error;
319 }
320#endif
321 mbm->alloc.pitch = dim.x * sizeof(uint32_t);
322 mbm->alloc.off0 = 0;
323 mbm->alloc.pixels = as_area_create(AS_AREA_ANY,
324 dim.x * dim.y * sizeof(uint32_t), AS_AREA_READ |
325 AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
326 mbm->myalloc = true;
327
328 if (mbm->alloc.pixels == AS_MAP_FAILED) {
329 rc = ENOMEM;
330 goto error;
331 }
332 } else {
333 mbm->alloc = *alloc;
334 }
335
336 mbm->mgc = mgc;
337 *rbm = (void *)mbm;
338 return EOK;
339error:
340 if (mbm != NULL)
341 free(mbm);
342 return rc;
343}
344
345/** Destroy bitmap in memory GC.
346 *
347 * @param bm Bitmap
348 * @return EOK on success or an error code
349 */
350static errno_t mem_gc_bitmap_destroy(void *bm)
351{
352 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
353 if (mbm->myalloc) {
354#if 0
355 /* TODO: if we alloc allocating the bitmap with malloc */
356 free(mbm->alloc.pixels);
357#endif
358 as_area_destroy(mbm->alloc.pixels);
359 }
360
361 free(mbm);
362 return EOK;
363}
364
365/** Render bitmap in memory GC.
366 *
367 * @param bm Bitmap
368 * @param srect0 Source rectangle or @c NULL
369 * @param offs0 Offset or @c NULL
370 * @return EOK on success or an error code
371 */
372static errno_t mem_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
373 gfx_coord2_t *offs0)
374{
375 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
376 gfx_rect_t srect;
377 gfx_rect_t drect;
378 gfx_rect_t crect;
379 gfx_coord2_t offs;
380 gfx_coord_t x, y;
381 pixelmap_t smap;
382 pixelmap_t dmap;
383 pixel_t pixel;
384
385 if (srect0 != NULL)
386 gfx_rect_clip(srect0, &mbm->rect, &srect);
387 else
388 srect = mbm->rect;
389
390 if (offs0 != NULL) {
391 offs = *offs0;
392 } else {
393 offs.x = 0;
394 offs.y = 0;
395 }
396
397 /* Destination rectangle */
398 gfx_rect_translate(&offs, &srect, &drect);
399
400 /* Clip destination rectangle */
401 gfx_rect_clip(&drect, &mbm->mgc->clip_rect, &crect);
402
403 assert(mbm->alloc.pitch == (mbm->rect.p1.x - mbm->rect.p0.x) *
404 (int)sizeof(uint32_t));
405 smap.width = mbm->rect.p1.x - mbm->rect.p0.x;
406 smap.height = mbm->rect.p1.y - mbm->rect.p0.y;
407 smap.data = mbm->alloc.pixels;
408
409 assert(mbm->mgc->rect.p0.x == 0);
410 assert(mbm->mgc->rect.p0.y == 0);
411 assert(mbm->mgc->alloc.pitch == mbm->mgc->rect.p1.x * (int)sizeof(uint32_t));
412 dmap.width = mbm->mgc->rect.p1.x;
413 dmap.height = mbm->mgc->rect.p1.y;
414 dmap.data = mbm->mgc->alloc.pixels;
415
416 if ((mbm->flags & bmpf_direct_output) != 0) {
417 /* Nothing to do */
418 } else if ((mbm->flags & bmpf_color_key) == 0) {
419 /* Simple copy */
420 for (y = crect.p0.y; y < crect.p1.y; y++) {
421 for (x = crect.p0.x; x < crect.p1.x; x++) {
422 pixel = pixelmap_get_pixel(&smap,
423 x - mbm->rect.p0.x - offs.x,
424 y - mbm->rect.p0.y - offs.y);
425 pixelmap_put_pixel(&dmap, x, y, pixel);
426 }
427 }
428 } else if ((mbm->flags & bmpf_colorize) == 0) {
429 /* Color key */
430 for (y = crect.p0.y; y < crect.p1.y; y++) {
431 for (x = crect.p0.x; x < crect.p1.x; x++) {
432 pixel = pixelmap_get_pixel(&smap,
433 x - mbm->rect.p0.x - offs.x,
434 y - mbm->rect.p0.y - offs.y);
435 if (pixel != mbm->key_color)
436 pixelmap_put_pixel(&dmap, x, y, pixel);
437 }
438 }
439 } else {
440 /* Color key & colorization */
441 for (y = crect.p0.y; y < crect.p1.y; y++) {
442 for (x = crect.p0.x; x < crect.p1.x; x++) {
443 pixel = pixelmap_get_pixel(&smap,
444 x - mbm->rect.p0.x - offs.x,
445 y - mbm->rect.p0.y - offs.y);
446 if (pixel != mbm->key_color)
447 pixelmap_put_pixel(&dmap, x, y,
448 mbm->mgc->color);
449 }
450 }
451 }
452
453 mem_gc_invalidate_rect(mbm->mgc, &crect);
454 return EOK;
455}
456
457/** Get allocation info for bitmap in memory GC.
458 *
459 * @param bm Bitmap
460 * @param alloc Place to store allocation info
461 * @return EOK on success or an error code
462 */
463static errno_t mem_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
464{
465 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
466 *alloc = mbm->alloc;
467 return EOK;
468}
469
470/** Get cursor position on memory GC.
471 *
472 * @param arg Memory GC
473 * @param pos Place to store position
474 *
475 * @return EOK on success or an error code
476 */
477static errno_t mem_gc_cursor_get_pos(void *arg, gfx_coord2_t *pos)
478{
479 mem_gc_t *mgc = (mem_gc_t *) arg;
480
481 if (mgc->cb->cursor_get_pos != NULL)
482 return mgc->cb->cursor_get_pos(mgc->cb_arg, pos);
483 else
484 return ENOTSUP;
485}
486
487/** Set cursor position on memory GC.
488 *
489 * @param arg Memory GC
490 * @param pos New position
491 *
492 * @return EOK on success or an error code
493 */
494static errno_t mem_gc_cursor_set_pos(void *arg, gfx_coord2_t *pos)
495{
496 mem_gc_t *mgc = (mem_gc_t *) arg;
497
498 if (mgc->cb->cursor_set_pos != NULL)
499 return mgc->cb->cursor_set_pos(mgc->cb_arg, pos);
500 else
501 return ENOTSUP;
502}
503
504/** Set cursor visibility on memory GC.
505 *
506 * @param arg Memory GC
507 * @param visible @c true iff cursor should be made visible
508 *
509 * @return EOK on success or an error code
510 */
511static errno_t mem_gc_cursor_set_visible(void *arg, bool visible)
512{
513 mem_gc_t *mgc = (mem_gc_t *) arg;
514
515 if (mgc->cb->cursor_set_visible != NULL)
516 return mgc->cb->cursor_set_visible(mgc->cb_arg, visible);
517 else
518 return ENOTSUP;
519}
520
521/** @}
522 */
Note: See TracBrowser for help on using the repository browser.