source: mainline/uspace/lib/congfx/src/console.c@ 7b882c1f

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

Need some vector operations

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[9259d20]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
[a3f63ac]29/** @addtogroup libcongfx
[9259d20]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
[a3f63ac]40#include <congfx/console.h>
[9259d20]41#include <gfx/context.h>
[7b882c1f]42#include <gfx/coord.h>
[9259d20]43#include <gfx/render.h>
44#include <io/pixel.h>
[d18f3b7]45#include <io/pixelmap.h>
[9259d20]46#include <stdlib.h>
[a3f63ac]47#include "../private/console.h"
48#include "../private/color.h"
[9259d20]49
50static errno_t console_gc_set_color(void *, gfx_color_t *);
51static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
[d18f3b7]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 *);
[9259d20]57
58gfx_context_ops_t console_gc_ops = {
59 .set_color = console_gc_set_color,
[d18f3b7]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
[9259d20]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
[d18f3b7]80 cgc->clr = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
[9259d20]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 int rv;
[1822545]95 gfx_coord_t x, y;
[9259d20]96
97 // XXX We should handle p0.x > p1.x and p0.y > p1.y
98
[d18f3b7]99 console_set_rgb_color(cgc->con, cgc->clr, cgc->clr);
100
[9259d20]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 */
126errno_t console_gc_create(console_ctrl_t *con, FILE *fout,
[9be2358]127 console_gc_t **rgc)
[9259d20]128{
129 console_gc_t *cgc = NULL;
130 gfx_context_t *gc = NULL;
131 errno_t rc;
132
133 cgc = calloc(1, sizeof(console_gc_t));
134 if (cgc == NULL) {
135 rc = ENOMEM;
136 goto error;
137 }
138
139 rc = gfx_context_new(&console_gc_ops, cgc, &gc);
140 if (rc != EOK)
141 goto error;
142
[9be2358]143 cgc->gc = gc;
[9259d20]144 cgc->con = con;
145 cgc->fout = fout;
[9be2358]146 *rgc = cgc;
[9259d20]147 return EOK;
148error:
149 if (cgc != NULL)
150 free(cgc);
151 gfx_context_delete(gc);
152 return rc;
153}
154
[9be2358]155/** Delete console GC.
156 *
157 * @param cgc Console GC
158 */
159errno_t console_gc_delete(console_gc_t *cgc)
160{
161 errno_t rc;
162
163 rc = gfx_context_delete(cgc->gc);
164 if (rc != EOK)
165 return rc;
166
167 free(cgc);
168 return EOK;
169}
170
171/** Get generic graphic context from console GC.
172 *
173 * @param cgc Console GC
174 * @return Graphic context
175 */
176gfx_context_t *console_gc_get_ctx(console_gc_t *cgc)
177{
178 return cgc->gc;
179}
180
[d18f3b7]181/** Create bitmap in console GC.
182 *
183 * @param arg console GC
184 * @param params Bitmap params
185 * @param alloc Bitmap allocation info or @c NULL
186 * @param rbm Place to store pointer to new bitmap
187 * @return EOK on success or an error code
188 */
189errno_t console_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
190 gfx_bitmap_alloc_t *alloc, void **rbm)
191{
192 console_gc_t *cgc = (console_gc_t *) arg;
193 console_gc_bitmap_t *cbm = NULL;
[7b882c1f]194 gfx_coord2_t dim;
[d18f3b7]195 errno_t rc;
196
197 cbm = calloc(1, sizeof(console_gc_bitmap_t));
198 if (cbm == NULL)
199 return ENOMEM;
200
[7b882c1f]201 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
[d18f3b7]202 cbm->rect = params->rect;
203
204 if (alloc == NULL) {
[7b882c1f]205 cbm->alloc.pitch = dim.x * sizeof(uint32_t);
[d18f3b7]206 cbm->alloc.off0 = 0;
[7b882c1f]207 cbm->alloc.pixels = calloc(dim.x * dim.y, sizeof(uint32_t));
[d18f3b7]208 if (cbm->alloc.pixels == NULL) {
209 rc = ENOMEM;
210 goto error;
211 }
212
213 cbm->myalloc = true;
214 } else {
215 cbm->alloc = *alloc;
216 }
217
218 cbm->cgc = cgc;
219 *rbm = (void *)cbm;
220 return EOK;
221error:
222 if (cbm != NULL)
223 free(cbm);
224 return rc;
225}
226
227/** Destroy bitmap in console GC.
228 *
229 * @param bm Bitmap
230 * @return EOK on success or an error code
231 */
232static errno_t console_gc_bitmap_destroy(void *bm)
233{
234 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
235 if (cbm->myalloc)
236 free(cbm->alloc.pixels);
237 free(cbm);
238 return EOK;
239}
240
241/** Render bitmap in console GC.
242 *
243 * @param bm Bitmap
244 * @param srect0 Source rectangle or @c NULL
245 * @param offs0 Offset or @c NULL
246 * @return EOK on success or an error code
247 */
248static errno_t console_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
249 gfx_coord2_t *offs0)
250{
251 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
[1822545]252 gfx_coord_t x, y;
[d18f3b7]253 int rv;
254 pixel_t clr;
255 pixelmap_t pixelmap;
256 gfx_rect_t srect;
257 gfx_rect_t drect;
258 gfx_coord2_t offs;
259
260 if (srect0 != NULL)
261 srect = *srect0;
262 else
263 srect = cbm->rect;
264
265 if (offs0 != NULL) {
266 offs = *offs0;
267 } else {
268 offs.x = 0;
269 offs.y = 0;
270 }
271
272 // XXX Add function to translate rectangle
[7b882c1f]273 gfx_rect_translate(&offs, &srect, &drect);
[d18f3b7]274
275 pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
276 pixelmap.height = cbm->rect.p1.y = cbm->rect.p1.y;
277 pixelmap.data = cbm->alloc.pixels;
278
279 for (y = drect.p0.y; y < drect.p1.y; y++) {
280 console_set_pos(cbm->cgc->con, drect.p0.x, y);
281
282 for (x = drect.p0.x; x < drect.p1.x; x++) {
283 clr = pixelmap_get_pixel(&pixelmap,
284 x - offs.x - cbm->rect.p0.x,
285 y - offs.y - cbm->rect.p0.y);
286 console_set_rgb_color(cbm->cgc->con, clr, clr);
287
288 rv = fputc('X', cbm->cgc->fout);
289 if (rv < 0)
290 return EIO;
291
292 console_flush(cbm->cgc->con);
293 }
294 }
295
296 return EOK;
297}
298
299/** Get allocation info for bitmap in console GC.
300 *
301 * @param bm Bitmap
302 * @param alloc Place to store allocation info
303 * @return EOK on success or an error code
304 */
305static errno_t console_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
306{
307 console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
308 *alloc = cbm->alloc;
309 return EOK;
310}
311
[9259d20]312/** @}
313 */
Note: See TracBrowser for help on using the repository browser.