Changeset e0545de in mainline for uspace/lib/guigfx/src/canvas.c


Ignore:
Timestamp:
2019-10-19T10:07:32Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d18f3b7
Parents:
dcc4cb31
Message:

Implement bitmaps in canvas GC, demo in gfxdemo

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/guigfx/src/canvas.c

    rdcc4cb31 re0545de  
    3737 */
    3838
     39#include <draw/drawctx.h>
     40#include <draw/source.h>
    3941#include <gfx/color.h>
    4042#include <gfx/context.h>
     
    4345#include <io/pixel.h>
    4446#include <stdlib.h>
     47#include <transform.h>
    4548#include "../private/canvas.h"
    4649//#include "../../private/color.h"
     
    4851static errno_t canvas_gc_set_color(void *, gfx_color_t *);
    4952static errno_t canvas_gc_fill_rect(void *, gfx_rect_t *);
     53static errno_t canvas_gc_bitmap_create(void *, gfx_bitmap_params_t *,
     54    gfx_bitmap_alloc_t *, void **);
     55static errno_t canvas_gc_bitmap_destroy(void *);
     56static errno_t canvas_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     57static errno_t canvas_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    5058
    5159gfx_context_ops_t canvas_gc_ops = {
    5260        .set_color = canvas_gc_set_color,
    53         .fill_rect = canvas_gc_fill_rect
     61        .fill_rect = canvas_gc_fill_rect,
     62        .bitmap_create = canvas_gc_bitmap_create,
     63        .bitmap_destroy = canvas_gc_bitmap_destroy,
     64        .bitmap_render = canvas_gc_bitmap_render,
     65        .bitmap_get_alloc = canvas_gc_bitmap_get_alloc
    5466};
    5567
     
    163175}
    164176
     177/** Create bitmap in canvas GC.
     178 *
     179 * @param arg Canvas GC
     180 * @param params Bitmap params
     181 * @param alloc Bitmap allocation info or @c NULL
     182 * @param rbm Place to store pointer to new bitmap
     183 * @return EOK on success or an error code
     184 */
     185errno_t canvas_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     186    gfx_bitmap_alloc_t *alloc, void **rbm)
     187{
     188        canvas_gc_t *cgc = (canvas_gc_t *) arg;
     189        canvas_gc_bitmap_t *cbm = NULL;
     190        int w, h;
     191        errno_t rc;
     192
     193        cbm = calloc(1, sizeof(canvas_gc_bitmap_t));
     194        if (cbm == NULL)
     195                return ENOMEM;
     196
     197        w = params->rect.p1.x - params->rect.p0.x;
     198        h = params->rect.p1.y - params->rect.p0.y;
     199        cbm->rect = params->rect;
     200
     201        if (alloc == NULL) {
     202                cbm->surface = surface_create(w, h, NULL, 0);
     203                if (cbm->surface == NULL) {
     204                        rc = ENOMEM;
     205                        goto error;
     206                }
     207
     208                cbm->alloc.pitch = w * sizeof(uint32_t);
     209                cbm->alloc.off0 = 0;
     210                cbm->alloc.pixels = surface_direct_access(cbm->surface);
     211                cbm->myalloc = true;
     212        } else {
     213                cbm->surface = surface_create(w, h, alloc->pixels, 0);
     214                if (cbm->surface == NULL) {
     215                        rc = ENOMEM;
     216                        goto error;
     217                }
     218
     219                cbm->alloc = *alloc;
     220        }
     221
     222        cbm->cgc = cgc;
     223        *rbm = (void *)cbm;
     224        return EOK;
     225error:
     226        if (cbm != NULL)
     227                free(cbm);
     228        return rc;
     229}
     230
     231/** Destroy bitmap in canvas GC.
     232 *
     233 * @param bm Bitmap
     234 * @return EOK on success or an error code
     235 */
     236static errno_t canvas_gc_bitmap_destroy(void *bm)
     237{
     238        canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
     239        if (cbm->myalloc)
     240                surface_destroy(cbm->surface);
     241        // XXX if !cbm->myalloc, surface is leaked - no way to destroy it
     242        // without destroying the pixel buffer
     243        free(cbm);
     244        return EOK;
     245}
     246
     247/** Render bitmap in canvas GC.
     248 *
     249 * @param bm Bitmap
     250 * @param srect0 Source rectangle or @c NULL
     251 * @param offs0 Offset or @c NULL
     252 * @return EOK on success or an error code
     253 */
     254static errno_t canvas_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
     255    gfx_coord2_t *offs0)
     256{
     257        canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
     258        gfx_rect_t srect;
     259        gfx_rect_t drect;
     260        gfx_coord2_t offs;
     261
     262        if (srect0 != NULL)
     263                srect = *srect0;
     264        else
     265                srect = cbm->rect;
     266
     267        if (offs0 != NULL) {
     268                offs = *offs0;
     269        } else {
     270                offs.x = 0;
     271                offs.y = 0;
     272        }
     273
     274        // XXX Add function to translate rectangle
     275        drect.p0.x = srect.p0.x + offs.x;
     276        drect.p0.y = srect.p0.y + offs.y;
     277        drect.p1.x = srect.p1.x + offs.x;
     278        drect.p1.y = srect.p1.y + offs.y;
     279
     280        transform_t transform;
     281        transform_identity(&transform);
     282        transform_translate(&transform, offs.x - cbm->rect.p0.x,
     283            offs.y - cbm->rect.p0.y);
     284
     285        source_t source;
     286        source_init(&source);
     287        source_set_transform(&source, transform);
     288        source_set_texture(&source, cbm->surface,
     289            PIXELMAP_EXTEND_TRANSPARENT_BLACK);
     290
     291        drawctx_t drawctx;
     292        drawctx_init(&drawctx, cbm->cgc->surface);
     293
     294        drawctx_set_source(&drawctx, &source);
     295        drawctx_transfer(&drawctx, drect.p0.x, drect.p0.y,
     296            drect.p1.x - drect.p0.x, drect.p1.y - drect.p0.y);
     297
     298        update_canvas(cbm->cgc->canvas, cbm->cgc->surface);
     299        return EOK;
     300}
     301
     302/** Get allocation info for bitmap in canvas 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 canvas_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     309{
     310        canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
     311        *alloc = cbm->alloc;
     312        return EOK;
     313}
     314
    165315/** @}
    166316 */
Note: See TracChangeset for help on using the changeset viewer.