Changeset d18f3b7 in mainline for uspace/lib/congfx/src/console.c


Ignore:
Timestamp:
2019-10-21T00:08:24Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1822545
Parents:
e0545de
Message:

Implement bitmaps in console GC

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/congfx/src/console.c

    re0545de rd18f3b7  
    4242#include <gfx/render.h>
    4343#include <io/pixel.h>
     44#include <io/pixelmap.h>
    4445#include <stdlib.h>
    4546#include "../private/console.h"
     
    4849static errno_t console_gc_set_color(void *, gfx_color_t *);
    4950static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
     51static errno_t console_gc_bitmap_create(void *, gfx_bitmap_params_t *,
     52    gfx_bitmap_alloc_t *, void **);
     53static errno_t console_gc_bitmap_destroy(void *);
     54static errno_t console_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     55static errno_t console_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    5056
    5157gfx_context_ops_t console_gc_ops = {
    5258        .set_color = console_gc_set_color,
    53         .fill_rect = console_gc_fill_rect
     59        .fill_rect = console_gc_fill_rect,
     60        .bitmap_create = console_gc_bitmap_create,
     61        .bitmap_destroy = console_gc_bitmap_destroy,
     62        .bitmap_render = console_gc_bitmap_render,
     63        .bitmap_get_alloc = console_gc_bitmap_get_alloc
    5464};
    5565
     
    6676{
    6777        console_gc_t *cgc = (console_gc_t *) arg;
    68         pixel_t clr;
    69 
    70         clr = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
    71 
    72         console_set_rgb_color(cgc->con, clr, clr);
     78
     79        cgc->clr = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
    7380        return EOK;
    7481}
     
    8895
    8996        // XXX We should handle p0.x > p1.x and p0.y > p1.y
     97
     98        console_set_rgb_color(cgc->con, cgc->clr, cgc->clr);
    9099
    91100        for (y = rect->p0.y; y < rect->p1.y; y++) {
     
    169178}
    170179
     180/** Create bitmap in console GC.
     181 *
     182 * @param arg console GC
     183 * @param params Bitmap params
     184 * @param alloc Bitmap allocation info or @c NULL
     185 * @param rbm Place to store pointer to new bitmap
     186 * @return EOK on success or an error code
     187 */
     188errno_t console_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     189    gfx_bitmap_alloc_t *alloc, void **rbm)
     190{
     191        console_gc_t *cgc = (console_gc_t *) arg;
     192        console_gc_bitmap_t *cbm = NULL;
     193        int w, h;
     194        errno_t rc;
     195
     196        cbm = calloc(1, sizeof(console_gc_bitmap_t));
     197        if (cbm == NULL)
     198                return ENOMEM;
     199
     200        w = params->rect.p1.x - params->rect.p0.x;
     201        h = params->rect.p1.y - params->rect.p0.y;
     202        cbm->rect = params->rect;
     203
     204        if (alloc == NULL) {
     205                cbm->alloc.pitch = w * sizeof(uint32_t);
     206                cbm->alloc.off0 = 0;
     207                cbm->alloc.pixels = calloc(w * h, sizeof(uint32_t));
     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;
     252        int x, y;
     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
     273        drect.p0.x = srect.p0.x + offs.x;
     274        drect.p0.y = srect.p0.y + offs.y;
     275        drect.p1.x = srect.p1.x + offs.x;
     276        drect.p1.y = srect.p1.y + offs.y;
     277
     278        pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
     279        pixelmap.height = cbm->rect.p1.y = cbm->rect.p1.y;
     280        pixelmap.data = cbm->alloc.pixels;
     281
     282        for (y = drect.p0.y; y < drect.p1.y; y++) {
     283                console_set_pos(cbm->cgc->con, drect.p0.x, y);
     284
     285                for (x = drect.p0.x; x < drect.p1.x; x++) {
     286                        clr = pixelmap_get_pixel(&pixelmap,
     287                            x - offs.x - cbm->rect.p0.x,
     288                            y - offs.y - cbm->rect.p0.y);
     289                        console_set_rgb_color(cbm->cgc->con, clr, clr);
     290
     291                        rv = fputc('X', cbm->cgc->fout);
     292                        if (rv < 0)
     293                                return EIO;
     294
     295                        console_flush(cbm->cgc->con);
     296                }
     297        }
     298
     299        return EOK;
     300}
     301
     302/** Get allocation info for bitmap in console GC.
     303 *
     304 * @param bm Bitmap
     305 * @param alloc Place to store allocation info
     306 * @return EOK on success or an error code
     307 */
     308static errno_t console_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     309{
     310        console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
     311        *alloc = cbm->alloc;
     312        return EOK;
     313}
     314
     315
    171316/** @}
    172317 */
Note: See TracChangeset for help on using the changeset viewer.