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 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 |
|
---|
50 | static errno_t console_gc_set_color(void *, gfx_color_t *);
|
---|
51 | static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
|
---|
52 | static errno_t console_gc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
53 | gfx_bitmap_alloc_t *, void **);
|
---|
54 | static errno_t console_gc_bitmap_destroy(void *);
|
---|
55 | static errno_t console_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
56 | static errno_t console_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
57 |
|
---|
58 | gfx_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 | */
|
---|
76 | static 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 | */
|
---|
91 | static errno_t console_gc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
92 | {
|
---|
93 | console_gc_t *cgc = (console_gc_t *) arg;
|
---|
94 | int rv;
|
---|
95 | gfx_coord_t x, y;
|
---|
96 |
|
---|
97 | // XXX We should handle p0.x > p1.x and p0.y > p1.y
|
---|
98 |
|
---|
99 | console_set_rgb_color(cgc->con, cgc->clr, cgc->clr);
|
---|
100 |
|
---|
101 | for (y = rect->p0.y; y < rect->p1.y; y++) {
|
---|
102 | console_set_pos(cgc->con, rect->p0.x, y);
|
---|
103 |
|
---|
104 | for (x = rect->p0.x; x < rect->p1.x; x++) {
|
---|
105 | rv = fputc('X', cgc->fout);
|
---|
106 | if (rv < 0)
|
---|
107 | return EIO;
|
---|
108 | }
|
---|
109 |
|
---|
110 | console_flush(cgc->con);
|
---|
111 | }
|
---|
112 |
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Create console GC.
|
---|
117 | *
|
---|
118 | * Create graphics context for rendering into a console.
|
---|
119 | *
|
---|
120 | * @param con Console object
|
---|
121 | * @param fout File to which characters are written (console)
|
---|
122 | * @param rgc Place to store pointer to new GC.
|
---|
123 | *
|
---|
124 | * @return EOK on success or an error code
|
---|
125 | */
|
---|
126 | errno_t console_gc_create(console_ctrl_t *con, FILE *fout,
|
---|
127 | console_gc_t **rgc)
|
---|
128 | {
|
---|
129 | console_gc_t *cgc = NULL;
|
---|
130 | gfx_context_t *gc = NULL;
|
---|
131 | sysarg_t rows;
|
---|
132 | sysarg_t cols;
|
---|
133 | errno_t rc;
|
---|
134 |
|
---|
135 | cgc = calloc(1, sizeof(console_gc_t));
|
---|
136 | if (cgc == NULL) {
|
---|
137 | rc = ENOMEM;
|
---|
138 | goto error;
|
---|
139 | }
|
---|
140 |
|
---|
141 | rc = console_get_size(con, &cols, &rows);
|
---|
142 | if (rc != EOK)
|
---|
143 | goto error;
|
---|
144 |
|
---|
145 | rc = gfx_context_new(&console_gc_ops, cgc, &gc);
|
---|
146 | if (rc != EOK)
|
---|
147 | goto error;
|
---|
148 |
|
---|
149 | cgc->gc = gc;
|
---|
150 | cgc->con = con;
|
---|
151 | cgc->fout = fout;
|
---|
152 | cgc->rect.p0.x = 0;
|
---|
153 | cgc->rect.p0.y = 0;
|
---|
154 | cgc->rect.p1.x = cols;
|
---|
155 | cgc->rect.p1.y = rows - 1; /* make sure we avoid bottom-right corner */
|
---|
156 |
|
---|
157 | *rgc = cgc;
|
---|
158 | return EOK;
|
---|
159 | error:
|
---|
160 | if (cgc != NULL)
|
---|
161 | free(cgc);
|
---|
162 | gfx_context_delete(gc);
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Delete console GC.
|
---|
167 | *
|
---|
168 | * @param cgc Console GC
|
---|
169 | */
|
---|
170 | errno_t console_gc_delete(console_gc_t *cgc)
|
---|
171 | {
|
---|
172 | errno_t rc;
|
---|
173 |
|
---|
174 | rc = gfx_context_delete(cgc->gc);
|
---|
175 | if (rc != EOK)
|
---|
176 | return rc;
|
---|
177 |
|
---|
178 | free(cgc);
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Get generic graphic context from console GC.
|
---|
183 | *
|
---|
184 | * @param cgc Console GC
|
---|
185 | * @return Graphic context
|
---|
186 | */
|
---|
187 | gfx_context_t *console_gc_get_ctx(console_gc_t *cgc)
|
---|
188 | {
|
---|
189 | return cgc->gc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Create bitmap in console GC.
|
---|
193 | *
|
---|
194 | * @param arg console GC
|
---|
195 | * @param params Bitmap params
|
---|
196 | * @param alloc Bitmap allocation info or @c NULL
|
---|
197 | * @param rbm Place to store pointer to new bitmap
|
---|
198 | * @return EOK on success or an error code
|
---|
199 | */
|
---|
200 | errno_t console_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
201 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
202 | {
|
---|
203 | console_gc_t *cgc = (console_gc_t *) arg;
|
---|
204 | console_gc_bitmap_t *cbm = NULL;
|
---|
205 | gfx_coord2_t dim;
|
---|
206 | errno_t rc;
|
---|
207 |
|
---|
208 | /* Check that we support all requested flags */
|
---|
209 | if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
|
---|
210 | return ENOTSUP;
|
---|
211 |
|
---|
212 | cbm = calloc(1, sizeof(console_gc_bitmap_t));
|
---|
213 | if (cbm == NULL)
|
---|
214 | return ENOMEM;
|
---|
215 |
|
---|
216 | gfx_coord2_subtract(¶ms->rect.p1, ¶ms->rect.p0, &dim);
|
---|
217 | cbm->rect = params->rect;
|
---|
218 | cbm->flags = params->flags;
|
---|
219 | cbm->key_color = params->key_color;
|
---|
220 |
|
---|
221 | if (alloc == NULL) {
|
---|
222 | cbm->alloc.pitch = dim.x * sizeof(uint32_t);
|
---|
223 | cbm->alloc.off0 = 0;
|
---|
224 | cbm->alloc.pixels = calloc(dim.x * dim.y, sizeof(uint32_t));
|
---|
225 | if (cbm->alloc.pixels == NULL) {
|
---|
226 | rc = ENOMEM;
|
---|
227 | goto error;
|
---|
228 | }
|
---|
229 |
|
---|
230 | cbm->myalloc = true;
|
---|
231 | } else {
|
---|
232 | cbm->alloc = *alloc;
|
---|
233 | }
|
---|
234 |
|
---|
235 | cbm->cgc = cgc;
|
---|
236 | *rbm = (void *)cbm;
|
---|
237 | return EOK;
|
---|
238 | error:
|
---|
239 | if (cbm != NULL)
|
---|
240 | free(cbm);
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Destroy bitmap in console GC.
|
---|
245 | *
|
---|
246 | * @param bm Bitmap
|
---|
247 | * @return EOK on success or an error code
|
---|
248 | */
|
---|
249 | static errno_t console_gc_bitmap_destroy(void *bm)
|
---|
250 | {
|
---|
251 | console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
|
---|
252 | if (cbm->myalloc)
|
---|
253 | free(cbm->alloc.pixels);
|
---|
254 | free(cbm);
|
---|
255 | return EOK;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Render bitmap in console GC.
|
---|
259 | *
|
---|
260 | * @param bm Bitmap
|
---|
261 | * @param srect0 Source rectangle or @c NULL
|
---|
262 | * @param offs0 Offset or @c NULL
|
---|
263 | * @return EOK on success or an error code
|
---|
264 | */
|
---|
265 | static errno_t console_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
266 | gfx_coord2_t *offs0)
|
---|
267 | {
|
---|
268 | console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
|
---|
269 | gfx_coord_t x, y;
|
---|
270 | int rv;
|
---|
271 | pixel_t clr;
|
---|
272 | pixelmap_t pixelmap;
|
---|
273 | gfx_rect_t srect;
|
---|
274 | gfx_rect_t drect;
|
---|
275 | gfx_rect_t crect;
|
---|
276 | gfx_coord2_t offs;
|
---|
277 |
|
---|
278 | if (srect0 != NULL)
|
---|
279 | srect = *srect0;
|
---|
280 | else
|
---|
281 | srect = cbm->rect;
|
---|
282 |
|
---|
283 | if (offs0 != NULL) {
|
---|
284 | offs = *offs0;
|
---|
285 | } else {
|
---|
286 | offs.x = 0;
|
---|
287 | offs.y = 0;
|
---|
288 | }
|
---|
289 |
|
---|
290 | gfx_rect_translate(&offs, &srect, &drect);
|
---|
291 | gfx_rect_clip(&drect, &cbm->cgc->rect, &crect);
|
---|
292 |
|
---|
293 | pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
|
---|
294 | pixelmap.height = cbm->rect.p1.y = cbm->rect.p1.y;
|
---|
295 | pixelmap.data = cbm->alloc.pixels;
|
---|
296 |
|
---|
297 | if ((cbm->flags & bmpf_color_key) == 0) {
|
---|
298 | /* Simple copy */
|
---|
299 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
300 | console_set_pos(cbm->cgc->con, crect.p0.x, y);
|
---|
301 |
|
---|
302 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
303 | clr = pixelmap_get_pixel(&pixelmap,
|
---|
304 | x - offs.x - cbm->rect.p0.x,
|
---|
305 | y - offs.y - cbm->rect.p0.y);
|
---|
306 | console_set_rgb_color(cbm->cgc->con, clr, clr);
|
---|
307 |
|
---|
308 | rv = fputc('X', cbm->cgc->fout);
|
---|
309 | if (rv < 0)
|
---|
310 | return EIO;
|
---|
311 |
|
---|
312 | console_flush(cbm->cgc->con);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | } else if ((cbm->flags & bmpf_colorize) == 0) {
|
---|
316 | /* Color key */
|
---|
317 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
318 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
319 |
|
---|
320 | clr = pixelmap_get_pixel(&pixelmap,
|
---|
321 | x - offs.x - cbm->rect.p0.x,
|
---|
322 | y - offs.y - cbm->rect.p0.y);
|
---|
323 | console_set_rgb_color(cbm->cgc->con, clr, clr);
|
---|
324 |
|
---|
325 | if (clr != cbm->key_color) {
|
---|
326 | console_set_pos(cbm->cgc->con, x, y);
|
---|
327 | rv = fputc('X', cbm->cgc->fout);
|
---|
328 | if (rv < 0)
|
---|
329 | return EIO;
|
---|
330 |
|
---|
331 | console_flush(cbm->cgc->con);
|
---|
332 | }
|
---|
333 |
|
---|
334 | }
|
---|
335 | }
|
---|
336 | } else {
|
---|
337 | /* Color key & colorize */
|
---|
338 | console_set_rgb_color(cbm->cgc->con, cbm->cgc->clr,
|
---|
339 | cbm->cgc->clr);
|
---|
340 |
|
---|
341 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
342 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
343 |
|
---|
344 | clr = pixelmap_get_pixel(&pixelmap,
|
---|
345 | x - offs.x - cbm->rect.p0.x,
|
---|
346 | y - offs.y - cbm->rect.p0.y);
|
---|
347 |
|
---|
348 | if (clr != cbm->key_color) {
|
---|
349 | console_set_pos(cbm->cgc->con, x, y);
|
---|
350 | rv = fputc('X', cbm->cgc->fout);
|
---|
351 | if (rv < 0)
|
---|
352 | return EIO;
|
---|
353 |
|
---|
354 | console_flush(cbm->cgc->con);
|
---|
355 | }
|
---|
356 |
|
---|
357 | }
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | return EOK;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /** Get allocation info for bitmap in console GC.
|
---|
365 | *
|
---|
366 | * @param bm Bitmap
|
---|
367 | * @param alloc Place to store allocation info
|
---|
368 | * @return EOK on success or an error code
|
---|
369 | */
|
---|
370 | static errno_t console_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
371 | {
|
---|
372 | console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
|
---|
373 | *alloc = cbm->alloc;
|
---|
374 | return EOK;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /** @}
|
---|
378 | */
|
---|