source: mainline/uspace/lib/congfx/src/console.c@ 26853ebc

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

Efficient way of rendering to the console via shared buffer

Makes congfx reasonably fast

  • Property mode set to 100644
File size: 9.7 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 libcongfx
30 * @{
31 */
32/**
33 * @file GFX console backend
34 *
35 * This implements a graphics context over a classic console interface.
36 * This is just for experimentation purposes. In the end we want the
37 * console to actually directly suport GFX interface.
38 */
39
40#include <congfx/console.h>
41#include <gfx/context.h>
42#include <gfx/coord.h>
43#include <gfx/render.h>
44#include <io/pixel.h>
45#include <io/pixelmap.h>
46#include <stdlib.h>
47#include "../private/console.h"
48#include "../private/color.h"
49
50static errno_t console_gc_set_color(void *, gfx_color_t *);
51static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
52static errno_t console_gc_bitmap_create(void *, gfx_bitmap_params_t *,
53 gfx_bitmap_alloc_t *, void **);
54static errno_t console_gc_bitmap_destroy(void *);
55static errno_t console_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
56static errno_t console_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
57
58gfx_context_ops_t console_gc_ops = {
59 .set_color = console_gc_set_color,
60 .fill_rect = console_gc_fill_rect,
61 .bitmap_create = console_gc_bitmap_create,
62 .bitmap_destroy = console_gc_bitmap_destroy,
63 .bitmap_render = console_gc_bitmap_render,
64 .bitmap_get_alloc = console_gc_bitmap_get_alloc
65};
66
67/** Set color on console GC.
68 *
69 * Set drawing color on console GC.
70 *
71 * @param arg Console GC
72 * @param color Color
73 *
74 * @return EOK on success or an error code
75 */
76static errno_t console_gc_set_color(void *arg, gfx_color_t *color)
77{
78 console_gc_t *cgc = (console_gc_t *) arg;
79
80 cgc->clr = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
81 return EOK;
82}
83
84/** Fill rectangle on console GC.
85 *
86 * @param arg Console GC
87 * @param rect Rectangle
88 *
89 * @return EOK on success or an error code
90 */
91static errno_t console_gc_fill_rect(void *arg, gfx_rect_t *rect)
92{
93 console_gc_t *cgc = (console_gc_t *) arg;
94 gfx_coord_t x, y;
95 gfx_coord_t cols;
96 gfx_rect_t crect;
97 charfield_t ch;
98
99 /* Make sure rectangle is clipped and sorted */
100 gfx_rect_clip(rect, &cgc->rect, &crect);
101
102 cols = cgc->rect.p1.x - cgc->rect.p0.x;
103
104 ch.ch = 0;
105 ch.flags = CHAR_FLAG_DIRTY;
106 ch.attrs.type = CHAR_ATTR_RGB;
107 ch.attrs.val.rgb.fgcolor = cgc->clr;
108 ch.attrs.val.rgb.bgcolor = cgc->clr;
109
110 for (y = crect.p0.y; y < crect.p1.y; y++) {
111 for (x = crect.p0.x; x < crect.p1.x; x++) {
112 cgc->buf[y * cols + x] = ch;
113 }
114 }
115
116 console_update(cgc->con, crect.p0.x, crect.p0.y,
117 crect.p1.x, crect.p1.y);
118
119 return EOK;
120}
121
122/** Create console GC.
123 *
124 * Create graphics context for rendering into a console.
125 *
126 * @param con Console object
127 * @param fout File to which characters are written (console)
128 * @param rgc Place to store pointer to new GC.
129 *
130 * @return EOK on success or an error code
131 */
132errno_t console_gc_create(console_ctrl_t *con, FILE *fout,
133 console_gc_t **rgc)
134{
135 console_gc_t *cgc = NULL;
136 gfx_context_t *gc = NULL;
137 sysarg_t rows;
138 sysarg_t cols;
139 charfield_t *buf;
140 errno_t rc;
141
142 cgc = calloc(1, sizeof(console_gc_t));
143 if (cgc == NULL) {
144 rc = ENOMEM;
145 goto error;
146 }
147
148 rc = console_get_size(con, &cols, &rows);
149 if (rc != EOK)
150 goto error;
151
152 rc = console_map(con, cols, rows, &buf);
153 if (rc != EOK)
154 goto error;
155
156 rc = gfx_context_new(&console_gc_ops, cgc, &gc);
157 if (rc != EOK)
158 goto error;
159
160 cgc->gc = gc;
161 cgc->con = con;
162 cgc->fout = fout;
163 cgc->rect.p0.x = 0;
164 cgc->rect.p0.y = 0;
165 cgc->rect.p1.x = cols;
166 cgc->rect.p1.y = rows - 1; /* make sure we avoid bottom-right corner */
167 cgc->buf = buf;
168
169 *rgc = cgc;
170 return EOK;
171error:
172 if (cgc != NULL)
173 free(cgc);
174 gfx_context_delete(gc);
175 return rc;
176}
177
178/** Delete console GC.
179 *
180 * @param cgc Console GC
181 */
182errno_t console_gc_delete(console_gc_t *cgc)
183{
184 errno_t rc;
185
186 rc = gfx_context_delete(cgc->gc);
187 if (rc != EOK)
188 return rc;
189
190 free(cgc);
191 return EOK;
192}
193
194/** Get generic graphic context from console GC.
195 *
196 * @param cgc Console GC
197 * @return Graphic context
198 */
199gfx_context_t *console_gc_get_ctx(console_gc_t *cgc)
200{
201 return cgc->gc;
202}
203
204/** Create bitmap in console GC.
205 *
206 * @param arg console GC
207 * @param params Bitmap params
208 * @param alloc Bitmap allocation info or @c NULL
209 * @param rbm Place to store pointer to new bitmap
210 * @return EOK on success or an error code
211 */
212errno_t console_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
213 gfx_bitmap_alloc_t *alloc, void **rbm)
214{
215 console_gc_t *cgc = (console_gc_t *) arg;
216 console_gc_bitmap_t *cbm = NULL;
217 gfx_coord2_t dim;
218 errno_t rc;
219
220 /* Check that we support all requested flags */
221 if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
222 return ENOTSUP;
223
224 cbm = calloc(1, sizeof(console_gc_bitmap_t));
225 if (cbm == NULL)
226 return ENOMEM;
227
228 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
229 cbm->rect = params->rect;
230 cbm->flags = params->flags;
231 cbm->key_color = params->key_color;
232
233 if (alloc == NULL) {
234 cbm->alloc.pitch = dim.x * sizeof(uint32_t);
235 cbm->alloc.off0 = 0;
236 cbm->alloc.pixels = calloc(dim.x * dim.y, sizeof(uint32_t));
237 if (cbm->alloc.pixels == NULL) {
238 rc = ENOMEM;
239 goto error;
240 }
241
242 cbm->myalloc = true;
243 } else {
244 cbm->alloc = *alloc;
245 }
246
247 cbm->cgc = cgc;
248 *rbm = (void *)cbm;
249 return EOK;
250error:
251 if (cbm != NULL)
252 free(cbm);
253 return rc;
254}
255
256/** Destroy bitmap in console GC.
257 *
258 * @param bm Bitmap
259 * @return EOK on success or an error code
260 */
261static errno_t console_gc_bitmap_destroy(void *bm)
262{
263 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
264 if (cbm->myalloc)
265 free(cbm->alloc.pixels);
266 free(cbm);
267 return EOK;
268}
269
270/** Render bitmap in console GC.
271 *
272 * @param bm Bitmap
273 * @param srect0 Source rectangle or @c NULL
274 * @param offs0 Offset or @c NULL
275 * @return EOK on success or an error code
276 */
277static errno_t console_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
278 gfx_coord2_t *offs0)
279{
280 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
281 gfx_coord_t x, y;
282 pixel_t clr;
283 charfield_t ch;
284 pixelmap_t pixelmap;
285 gfx_rect_t srect;
286 gfx_rect_t drect;
287 gfx_rect_t crect;
288 gfx_coord2_t offs;
289 gfx_coord_t cols;
290
291 if (srect0 != NULL)
292 srect = *srect0;
293 else
294 srect = cbm->rect;
295
296 if (offs0 != NULL) {
297 offs = *offs0;
298 } else {
299 offs.x = 0;
300 offs.y = 0;
301 }
302
303 gfx_rect_translate(&offs, &srect, &drect);
304 gfx_rect_clip(&drect, &cbm->cgc->rect, &crect);
305
306 pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
307 pixelmap.height = cbm->rect.p1.y = cbm->rect.p1.y;
308 pixelmap.data = cbm->alloc.pixels;
309
310 cols = cbm->cgc->rect.p1.x - cbm->cgc->rect.p0.x;
311
312 if ((cbm->flags & bmpf_color_key) == 0) {
313 /* Simple copy */
314 for (y = crect.p0.y; y < crect.p1.y; y++) {
315 console_set_pos(cbm->cgc->con, crect.p0.x, y);
316
317 for (x = crect.p0.x; x < crect.p1.x; x++) {
318 clr = pixelmap_get_pixel(&pixelmap,
319 x - offs.x - cbm->rect.p0.x,
320 y - offs.y - cbm->rect.p0.y);
321
322 ch.ch = 0;
323 ch.flags = CHAR_FLAG_DIRTY;
324 ch.attrs.type = CHAR_ATTR_RGB;
325 ch.attrs.val.rgb.fgcolor = clr;
326 ch.attrs.val.rgb.bgcolor = clr;
327
328 cbm->cgc->buf[y * cols + x] = ch;
329 }
330 }
331 } else if ((cbm->flags & bmpf_colorize) == 0) {
332 /* Color key */
333 for (y = crect.p0.y; y < crect.p1.y; y++) {
334 for (x = crect.p0.x; x < crect.p1.x; x++) {
335
336 clr = pixelmap_get_pixel(&pixelmap,
337 x - offs.x - cbm->rect.p0.x,
338 y - offs.y - cbm->rect.p0.y);
339
340 ch.ch = 0;
341 ch.flags = CHAR_FLAG_DIRTY;
342 ch.attrs.type = CHAR_ATTR_RGB;
343 ch.attrs.val.rgb.fgcolor = clr;
344 ch.attrs.val.rgb.bgcolor = clr;
345
346 if (clr != cbm->key_color)
347 cbm->cgc->buf[y * cols + x] = ch;
348 }
349 }
350 } else {
351 /* Color key & colorize */
352 console_set_rgb_color(cbm->cgc->con, cbm->cgc->clr,
353 cbm->cgc->clr);
354 ch.ch = 0;
355 ch.flags = CHAR_FLAG_DIRTY;
356 ch.attrs.type = CHAR_ATTR_RGB;
357 ch.attrs.val.rgb.fgcolor = cbm->cgc->clr;
358 ch.attrs.val.rgb.bgcolor = cbm->cgc->clr;
359
360 for (y = crect.p0.y; y < crect.p1.y; y++) {
361 for (x = crect.p0.x; x < crect.p1.x; x++) {
362 clr = pixelmap_get_pixel(&pixelmap,
363 x - offs.x - cbm->rect.p0.x,
364 y - offs.y - cbm->rect.p0.y);
365
366 if (clr != cbm->key_color)
367 cbm->cgc->buf[y * cols + x] = ch;
368 }
369 }
370 }
371
372 console_update(cbm->cgc->con, crect.p0.x, crect.p0.y, crect.p1.x,
373 crect.p1.y);
374
375 return EOK;
376}
377
378/** Get allocation info for bitmap in console GC.
379 *
380 * @param bm Bitmap
381 * @param alloc Place to store allocation info
382 * @return EOK on success or an error code
383 */
384static errno_t console_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
385{
386 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
387 *alloc = cbm->alloc;
388 return EOK;
389}
390
391/** @}
392 */
Note: See TracBrowser for help on using the repository browser.