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

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

Rendering text in different colors via colorization

  • Property mode set to 100644
File size: 10.6 KB
Line 
1/*
2 * Copyright (c) 2020 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_color(void *, gfx_color_t *);
51static errno_t mem_gc_fill_rect(void *, gfx_rect_t *);
52static errno_t mem_gc_bitmap_create(void *, gfx_bitmap_params_t *,
53 gfx_bitmap_alloc_t *, void **);
54static errno_t mem_gc_bitmap_destroy(void *);
55static errno_t mem_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
56static errno_t mem_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
57static void mem_gc_invalidate_rect(mem_gc_t *, gfx_rect_t *);
58
59gfx_context_ops_t mem_gc_ops = {
60 .set_color = mem_gc_set_color,
61 .fill_rect = mem_gc_fill_rect,
62 .bitmap_create = mem_gc_bitmap_create,
63 .bitmap_destroy = mem_gc_bitmap_destroy,
64 .bitmap_render = mem_gc_bitmap_render,
65 .bitmap_get_alloc = mem_gc_bitmap_get_alloc
66};
67
68/** Set color on memory GC.
69 *
70 * Set drawing color on memory GC.
71 *
72 * @param arg Memory GC
73 * @param color Color
74 *
75 * @return EOK on success or an error code
76 */
77static errno_t mem_gc_set_color(void *arg, gfx_color_t *color)
78{
79 mem_gc_t *mgc = (mem_gc_t *) arg;
80 uint16_t r, g, b;
81
82 gfx_color_get_rgb_i16(color, &r, &g, &b);
83 mgc->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
84 return EOK;
85}
86
87/** Fill rectangle on memory GC.
88 *
89 * @param arg Memory GC
90 * @param rect Rectangle
91 *
92 * @return EOK on success or an error code
93 */
94static errno_t mem_gc_fill_rect(void *arg, gfx_rect_t *rect)
95{
96 mem_gc_t *mgc = (mem_gc_t *) arg;
97 gfx_rect_t crect;
98 gfx_coord_t x, y;
99 pixelmap_t pixelmap;
100
101 /* Make sure we have a sorted, clipped rectangle */
102 gfx_rect_clip(rect, &mgc->rect, &crect);
103
104 assert(mgc->rect.p0.x == 0);
105 assert(mgc->rect.p0.y == 0);
106 assert(mgc->alloc.pitch == mgc->rect.p1.x * (int)sizeof(uint32_t));
107 pixelmap.width = mgc->rect.p1.x;
108 pixelmap.height = mgc->rect.p1.y;
109 pixelmap.data = mgc->alloc.pixels;
110
111 for (y = crect.p0.y; y < crect.p1.y; y++) {
112 for (x = crect.p0.x; x < crect.p1.x; x++) {
113 pixelmap_put_pixel(&pixelmap, x, y, mgc->color);
114 }
115 }
116
117 mem_gc_invalidate_rect(mgc, &crect);
118 return EOK;
119}
120
121/** Create memory GC.
122 *
123 * Create graphics context for rendering into a block of memory.
124 *
125 * @param rect Bounding rectangle
126 * @param alloc Allocation info
127 * @param update_cb Function called to update a rectangle
128 * @param cb_arg Argument to callback function
129 * @param rgc Place to store pointer to new memory GC
130 *
131 * @return EOK on success or an error code
132 */
133errno_t mem_gc_create(gfx_rect_t *rect, gfx_bitmap_alloc_t *alloc,
134 mem_gc_update_cb_t update_cb, void *cb_arg, mem_gc_t **rgc)
135{
136 mem_gc_t *mgc = NULL;
137 gfx_context_t *gc = NULL;
138 errno_t rc;
139
140 mgc = calloc(1, sizeof(mem_gc_t));
141 if (mgc == NULL) {
142 rc = ENOMEM;
143 goto error;
144 }
145
146 rc = gfx_context_new(&mem_gc_ops, mgc, &gc);
147 if (rc != EOK)
148 goto error;
149
150 mgc->gc = gc;
151 mgc->rect = *rect;
152 mgc->alloc = *alloc;
153
154 mgc->update = update_cb;
155 mgc->cb_arg = cb_arg;
156
157 *rgc = mgc;
158 return EOK;
159error:
160 if (mgc != NULL)
161 free(mgc);
162 gfx_context_delete(gc);
163 return rc;
164}
165
166/** Delete memory GC.
167 *
168 * @param mgc Memory GC
169 */
170errno_t mem_gc_delete(mem_gc_t *mgc)
171{
172 errno_t rc;
173
174 rc = gfx_context_delete(mgc->gc);
175 if (rc != EOK)
176 return rc;
177
178 free(mgc);
179 return EOK;
180}
181
182/** Retarget memory GC to a different block of memory.
183 *
184 * @param mgc Memory GC
185 * @param rect New bounding rectangle
186 * @param alloc Allocation info of the new block
187 */
188void mem_gc_retarget(mem_gc_t *mgc, gfx_rect_t *rect,
189 gfx_bitmap_alloc_t *alloc)
190{
191 mgc->rect = *rect;
192 mgc->alloc = *alloc;
193}
194
195/** Get generic graphic context from memory GC.
196 *
197 * @param mgc Memory GC
198 * @return Graphic context
199 */
200gfx_context_t *mem_gc_get_ctx(mem_gc_t *mgc)
201{
202 return mgc->gc;
203}
204
205static void mem_gc_invalidate_rect(mem_gc_t *mgc, gfx_rect_t *rect)
206{
207 mgc->update(mgc->cb_arg, rect);
208}
209
210/** Create bitmap in memory GC.
211 *
212 * @param arg Memory GC
213 * @param params Bitmap params
214 * @param alloc Bitmap allocation info or @c NULL
215 * @param rbm Place to store pointer to new bitmap
216 * @return EOK on success or an error code
217 */
218errno_t mem_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
219 gfx_bitmap_alloc_t *alloc, void **rbm)
220{
221 mem_gc_t *mgc = (mem_gc_t *) arg;
222 mem_gc_bitmap_t *mbm = NULL;
223 gfx_coord2_t dim;
224 errno_t rc;
225
226 /* Check that we support all requested flags */
227 if ((params->flags & ~(bmpf_color_key | bmpf_colorize |
228 bmpf_direct_output)) != 0)
229 return ENOTSUP;
230
231 mbm = calloc(1, sizeof(mem_gc_bitmap_t));
232 if (mbm == NULL)
233 return ENOMEM;
234
235 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
236 mbm->rect = params->rect;
237 mbm->flags = params->flags;
238 mbm->key_color = params->key_color;
239
240 if ((params->flags & bmpf_direct_output) != 0) {
241 /* Caller cannot specify allocation for direct output */
242 if (alloc != NULL) {
243 rc = EINVAL;
244 goto error;
245 }
246
247 /* Bounding rectangle must be within GC bounding rectangle */
248 if (!gfx_rect_is_inside(&mbm->rect, &mgc->rect)) {
249 rc = EINVAL;
250 goto error;
251 }
252
253 mbm->alloc = mgc->alloc;
254
255 /* Don't free pixel array when destroying bitmap */
256 mbm->myalloc = false;
257 } else if (alloc == NULL) {
258#if 0
259 /*
260 * TODO: If the bitmap is not required to be sharable,
261 * we could allocate it with a simple malloc.
262 * Need to have a bitmap flag specifying that the
263 * allocation should be sharable. IPC GC could
264 * automatically add this flag
265 */
266 mbm->alloc.pitch = dim.x * sizeof(uint32_t);
267 mbm->alloc.off0 = 0;
268 mbm->alloc.pixels = malloc(mbm->alloc.pitch * dim.y);
269 mbm->myalloc = true;
270
271 if (mbm->alloc.pixels == NULL) {
272 rc = ENOMEM;
273 goto error;
274 }
275#endif
276 mbm->alloc.pitch = dim.x * sizeof(uint32_t);
277 mbm->alloc.off0 = 0;
278 mbm->alloc.pixels = as_area_create(AS_AREA_ANY,
279 dim.x * dim.y * sizeof(uint32_t), AS_AREA_READ |
280 AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
281 mbm->myalloc = true;
282
283 if (mbm->alloc.pixels == AS_MAP_FAILED) {
284 rc = ENOMEM;
285 goto error;
286 }
287 } else {
288 mbm->alloc = *alloc;
289 }
290
291 mbm->mgc = mgc;
292 *rbm = (void *)mbm;
293 return EOK;
294error:
295 if (mbm != NULL)
296 free(mbm);
297 return rc;
298}
299
300/** Destroy bitmap in memory GC.
301 *
302 * @param bm Bitmap
303 * @return EOK on success or an error code
304 */
305static errno_t mem_gc_bitmap_destroy(void *bm)
306{
307 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
308 if (mbm->myalloc) {
309#if 0
310 /* TODO: if we alloc allocating the bitmap with malloc */
311 free(mbm->alloc.pixels);
312#endif
313 as_area_destroy(mbm->alloc.pixels);
314 }
315
316 free(mbm);
317 return EOK;
318}
319
320/** Render bitmap in memory GC.
321 *
322 * @param bm Bitmap
323 * @param srect0 Source rectangle or @c NULL
324 * @param offs0 Offset or @c NULL
325 * @return EOK on success or an error code
326 */
327static errno_t mem_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
328 gfx_coord2_t *offs0)
329{
330 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
331 gfx_rect_t srect;
332 gfx_rect_t drect;
333 gfx_coord2_t offs;
334 gfx_coord_t x, y;
335 pixelmap_t smap;
336 pixelmap_t dmap;
337 pixel_t pixel;
338
339 if (srect0 != NULL)
340 gfx_rect_clip(srect0, &mbm->rect, &srect);
341 else
342 srect = mbm->rect;
343
344 if (offs0 != NULL) {
345 offs = *offs0;
346 } else {
347 offs.x = 0;
348 offs.y = 0;
349 }
350
351 /* Destination rectangle */
352 gfx_rect_translate(&offs, &srect, &drect);
353
354 assert(mbm->alloc.pitch == (mbm->rect.p1.x - mbm->rect.p0.x) *
355 (int)sizeof(uint32_t));
356 smap.width = mbm->rect.p1.x - mbm->rect.p0.x;
357 smap.height = mbm->rect.p1.y - mbm->rect.p0.y;
358 smap.data = mbm->alloc.pixels;
359
360 assert(mbm->mgc->rect.p0.x == 0);
361 assert(mbm->mgc->rect.p0.y == 0);
362 assert(mbm->mgc->alloc.pitch == mbm->mgc->rect.p1.x * (int)sizeof(uint32_t));
363 dmap.width = mbm->mgc->rect.p1.x;
364 dmap.height = mbm->mgc->rect.p1.y;
365 dmap.data = mbm->mgc->alloc.pixels;
366
367 if ((mbm->flags & bmpf_direct_output) != 0) {
368 /* Nothing to do */
369 } else if ((mbm->flags & bmpf_color_key) == 0) {
370 /* Simple copy */
371 for (y = drect.p0.y; y < drect.p1.y; y++) {
372 for (x = drect.p0.x; x < drect.p1.x; x++) {
373 pixel = pixelmap_get_pixel(&smap,
374 x - mbm->rect.p0.x - offs.x,
375 y - mbm->rect.p0.y - offs.y);
376 pixelmap_put_pixel(&dmap, x, y, pixel);
377 }
378 }
379 } else if ((mbm->flags & bmpf_colorize) == 0) {
380 /* Color key */
381 for (y = drect.p0.y; y < drect.p1.y; y++) {
382 for (x = drect.p0.x; x < drect.p1.x; x++) {
383 pixel = pixelmap_get_pixel(&smap,
384 x - mbm->rect.p0.x - offs.x,
385 y - mbm->rect.p0.y - offs.y);
386 if (pixel != mbm->key_color)
387 pixelmap_put_pixel(&dmap, x, y, pixel);
388 }
389 }
390 } else {
391 /* Color key & colorization */
392 for (y = drect.p0.y; y < drect.p1.y; y++) {
393 for (x = drect.p0.x; x < drect.p1.x; x++) {
394 pixel = pixelmap_get_pixel(&smap,
395 x - mbm->rect.p0.x - offs.x,
396 y - mbm->rect.p0.y - offs.y);
397 if (pixel != mbm->key_color)
398 pixelmap_put_pixel(&dmap, x, y,
399 mbm->mgc->color);
400 }
401 }
402 }
403
404 mem_gc_invalidate_rect(mbm->mgc, &drect);
405 return EOK;
406}
407
408/** Get allocation info for bitmap in memory GC.
409 *
410 * @param bm Bitmap
411 * @param alloc Place to store allocation info
412 * @return EOK on success or an error code
413 */
414static errno_t mem_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
415{
416 mem_gc_bitmap_t *mbm = (mem_gc_bitmap_t *)bm;
417 *alloc = mbm->alloc;
418 return EOK;
419}
420
421/** @}
422 */
Note: See TracBrowser for help on using the repository browser.