Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/memgfx/test/memgfx.c

    r1215db9 rf8375f7  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232#include <gfx/coord.h>
    3333#include <gfx/context.h>
    34 #include <gfx/cursor.h>
    3534#include <gfx/render.h>
    3635#include <io/pixelmap.h>
     
    4342PCUT_TEST_SUITE(memgfx);
    4443
    45 static void test_invalidate_rect(void *arg, gfx_rect_t *rect);
    46 static void test_update(void *arg);
    47 static errno_t test_cursor_get_pos(void *arg, gfx_coord2_t *);
    48 static errno_t test_cursor_set_pos(void *arg, gfx_coord2_t *);
    49 static errno_t test_cursor_set_visible(void *arg, bool);
    50 
    51 static mem_gc_cb_t test_mem_gc_cb = {
    52         .invalidate = test_invalidate_rect,
    53         .update = test_update,
    54         .cursor_get_pos = test_cursor_get_pos,
    55         .cursor_set_pos = test_cursor_set_pos,
    56         .cursor_set_visible = test_cursor_set_visible
    57 };
     44static void test_update_rect(void *arg, gfx_rect_t *rect);
    5845
    5946typedef struct {
    60         /** Return code to return */
    61         errno_t rc;
    62         /** True if invalidate was called */
    63         bool invalidate_called;
    64         /** Invalidate rectangle */
    65         gfx_rect_t inv_rect;
    6647        /** True if update was called */
    6748        bool update_called;
    68         /** True if cursor_get_pos was called */
    69         bool cursor_get_pos_called;
    70         /** Position to return from cursor_get_pos */
    71         gfx_coord2_t get_pos_pos;
    72         /** True if cursor_set_pos was called */
    73         bool cursor_set_pos_called;
    74         /** Position passed to cursor_set_pos */
    75         gfx_coord2_t set_pos_pos;
    76         /** True if cursor_set_visible was called */
    77         bool cursor_set_visible_called;
    78         /** Value passed to cursor_set_visible */
    79         bool set_visible_vis;
    80 } test_resp_t;
     49        /** Update rectangle */
     50        gfx_rect_t rect;
     51} test_update_t;
    8152
    8253/** Test creating and deleting a memory GC */
     
    11889        pixel_t pixel;
    11990        pixel_t expected;
    120         test_resp_t resp;
     91        test_update_t update;
    12192        errno_t rc;
    12293
     
    132103        PCUT_ASSERT_NOT_NULL(alloc.pixels);
    133104
    134         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
     105        rc = mem_gc_create(&rect, &alloc, test_update_rect, &update, &mgc);
    135106        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    136107
     
    151122        frect.p1.y = 5;
    152123
    153         memset(&resp, 0, sizeof(resp));
     124        memset(&update, 0, sizeof(update));
    154125
    155126        rc = gfx_fill_rect(gc, &frect);
     
    170141        }
    171142
    172         /* Check that the invalidate rect is equal to the filled rect */
    173         PCUT_ASSERT_TRUE(resp.invalidate_called);
    174         PCUT_ASSERT_INT_EQUALS(frect.p0.x, resp.inv_rect.p0.x);
    175         PCUT_ASSERT_INT_EQUALS(frect.p0.y, resp.inv_rect.p0.y);
    176         PCUT_ASSERT_INT_EQUALS(frect.p1.x, resp.inv_rect.p1.x);
    177         PCUT_ASSERT_INT_EQUALS(frect.p1.y, resp.inv_rect.p1.y);
     143        /* Check that the update rect is equal to the filled rect */
     144        PCUT_ASSERT_TRUE(update.update_called);
     145        PCUT_ASSERT_INT_EQUALS(frect.p0.x, update.rect.p0.x);
     146        PCUT_ASSERT_INT_EQUALS(frect.p0.y, update.rect.p0.y);
     147        PCUT_ASSERT_INT_EQUALS(frect.p1.x, update.rect.p1.x);
     148        PCUT_ASSERT_INT_EQUALS(frect.p1.y, update.rect.p1.y);
    178149
    179150        /* TODO: Check clipping once memgc can support pitch != width etc. */
     
    198169        pixel_t pixel;
    199170        pixel_t expected;
    200         test_resp_t resp;
     171        test_update_t update;
    201172        errno_t rc;
    202173
     
    212183        PCUT_ASSERT_NOT_NULL(alloc.pixels);
    213184
    214         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
     185        rc = mem_gc_create(&rect, &alloc, test_update_rect, &update, &mgc);
    215186        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    216187
     
    220191        /* Create bitmap */
    221192
    222         gfx_bitmap_params_init(&params);
    223193        params.rect.p0.x = 0;
    224194        params.rect.p0.y = 0;
     
    249219        dpmap.data = alloc.pixels;
    250220
    251         memset(&resp, 0, sizeof(resp));
     221        memset(&update, 0, sizeof(update));
    252222
    253223        /* Render the bitmap */
     
    267237        }
    268238
    269         /* Check that the invalidate rect is equal to the filled rect */
    270         PCUT_ASSERT_TRUE(resp.invalidate_called);
    271         PCUT_ASSERT_INT_EQUALS(params.rect.p0.x, resp.inv_rect.p0.x);
    272         PCUT_ASSERT_INT_EQUALS(params.rect.p0.y, resp.inv_rect.p0.y);
    273         PCUT_ASSERT_INT_EQUALS(params.rect.p1.x, resp.inv_rect.p1.x);
    274         PCUT_ASSERT_INT_EQUALS(params.rect.p1.y, resp.inv_rect.p1.y);
     239        /* Check that the update rect is equal to the filled rect */
     240        PCUT_ASSERT_TRUE(update.update_called);
     241        PCUT_ASSERT_INT_EQUALS(params.rect.p0.x, update.rect.p0.x);
     242        PCUT_ASSERT_INT_EQUALS(params.rect.p0.y, update.rect.p0.y);
     243        PCUT_ASSERT_INT_EQUALS(params.rect.p1.x, update.rect.p1.x);
     244        PCUT_ASSERT_INT_EQUALS(params.rect.p1.y, update.rect.p1.y);
    275245
    276246        /* TODO: Check clipping once memgc can support pitch != width etc. */
     
    280250}
    281251
    282 /** Test gfx_update() on a memory GC */
    283 PCUT_TEST(gfx_update)
    284 {
    285         mem_gc_t *mgc;
    286         gfx_rect_t rect;
    287         gfx_bitmap_alloc_t alloc;
    288         gfx_context_t *gc;
    289         test_resp_t resp;
    290         errno_t rc;
    291 
    292         /* Bounding rectangle for memory GC */
    293         rect.p0.x = 0;
    294         rect.p0.y = 0;
    295         rect.p1.x = 10;
    296         rect.p1.y = 10;
    297 
    298         alloc.pitch = (rect.p1.x - rect.p0.x) * sizeof(uint32_t);
    299         alloc.off0 = 0;
    300         alloc.pixels = calloc(1, alloc.pitch * (rect.p1.y - rect.p0.y));
    301         PCUT_ASSERT_NOT_NULL(alloc.pixels);
    302 
    303         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
    304         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    305 
    306         gc = mem_gc_get_ctx(mgc);
    307         PCUT_ASSERT_NOT_NULL(gc);
    308 
    309         memset(&resp, 0, sizeof(resp));
    310         PCUT_ASSERT_FALSE(resp.update_called);
    311 
    312         gfx_update(gc);
    313         PCUT_ASSERT_TRUE(resp.update_called);
    314 
    315         mem_gc_delete(mgc);
    316         free(alloc.pixels);
    317 }
    318 
    319 /** Test gfx_cursor_get_pos() on a memory GC */
    320 PCUT_TEST(gfx_cursor_get_pos)
    321 {
    322         mem_gc_t *mgc;
    323         gfx_rect_t rect;
    324         gfx_bitmap_alloc_t alloc;
    325         gfx_coord2_t pos;
    326         gfx_context_t *gc;
    327         test_resp_t resp;
    328         errno_t rc;
    329 
    330         /* Bounding rectangle for memory GC */
    331         rect.p0.x = 0;
    332         rect.p0.y = 0;
    333         rect.p1.x = 10;
    334         rect.p1.y = 10;
    335 
    336         alloc.pitch = (rect.p1.x - rect.p0.x) * sizeof(uint32_t);
    337         alloc.off0 = 0;
    338         alloc.pixels = calloc(1, alloc.pitch * (rect.p1.y - rect.p0.y));
    339         PCUT_ASSERT_NOT_NULL(alloc.pixels);
    340 
    341         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
    342         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    343 
    344         gc = mem_gc_get_ctx(mgc);
    345         PCUT_ASSERT_NOT_NULL(gc);
    346 
    347         memset(&resp, 0, sizeof(resp));
    348         resp.rc = EOK;
    349         resp.get_pos_pos.x = 1;
    350         resp.get_pos_pos.y = 2;
    351         PCUT_ASSERT_FALSE(resp.cursor_get_pos_called);
    352 
    353         rc = gfx_cursor_get_pos(gc, &pos);
    354         PCUT_ASSERT_TRUE(resp.cursor_get_pos_called);
    355         PCUT_ASSERT_INT_EQUALS(resp.get_pos_pos.x, pos.x);
    356         PCUT_ASSERT_INT_EQUALS(resp.get_pos_pos.y, pos.y);
    357 
    358         mem_gc_delete(mgc);
    359         free(alloc.pixels);
    360 }
    361 
    362 /** Test gfx_cursor_set_pos() on a memory GC */
    363 PCUT_TEST(gfx_cursor_set_pos)
    364 {
    365         mem_gc_t *mgc;
    366         gfx_rect_t rect;
    367         gfx_bitmap_alloc_t alloc;
    368         gfx_coord2_t pos;
    369         gfx_context_t *gc;
    370         test_resp_t resp;
    371         errno_t rc;
    372 
    373         /* Bounding rectangle for memory GC */
    374         rect.p0.x = 0;
    375         rect.p0.y = 0;
    376         rect.p1.x = 10;
    377         rect.p1.y = 10;
    378 
    379         alloc.pitch = (rect.p1.x - rect.p0.x) * sizeof(uint32_t);
    380         alloc.off0 = 0;
    381         alloc.pixels = calloc(1, alloc.pitch * (rect.p1.y - rect.p0.y));
    382         PCUT_ASSERT_NOT_NULL(alloc.pixels);
    383 
    384         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
    385         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    386 
    387         gc = mem_gc_get_ctx(mgc);
    388         PCUT_ASSERT_NOT_NULL(gc);
    389 
    390         memset(&resp, 0, sizeof(resp));
    391         resp.rc = EOK;
    392         pos.x = 1;
    393         pos.y = 2;
    394         PCUT_ASSERT_FALSE(resp.cursor_set_pos_called);
    395 
    396         rc = gfx_cursor_set_pos(gc, &pos);
    397         PCUT_ASSERT_TRUE(resp.cursor_set_pos_called);
    398         PCUT_ASSERT_INT_EQUALS(pos.x, resp.set_pos_pos.x);
    399         PCUT_ASSERT_INT_EQUALS(pos.y, resp.set_pos_pos.y);
    400 
    401         mem_gc_delete(mgc);
    402         free(alloc.pixels);
    403 }
    404 
    405 /** Test gfx_cursor_set_visible() on a memory GC */
    406 PCUT_TEST(gfx_cursor_set_visible)
    407 {
    408         mem_gc_t *mgc;
    409         gfx_rect_t rect;
    410         gfx_bitmap_alloc_t alloc;
    411         gfx_context_t *gc;
    412         test_resp_t resp;
    413         errno_t rc;
    414 
    415         /* Bounding rectangle for memory GC */
    416         rect.p0.x = 0;
    417         rect.p0.y = 0;
    418         rect.p1.x = 10;
    419         rect.p1.y = 10;
    420 
    421         alloc.pitch = (rect.p1.x - rect.p0.x) * sizeof(uint32_t);
    422         alloc.off0 = 0;
    423         alloc.pixels = calloc(1, alloc.pitch * (rect.p1.y - rect.p0.y));
    424         PCUT_ASSERT_NOT_NULL(alloc.pixels);
    425 
    426         rc = mem_gc_create(&rect, &alloc, &test_mem_gc_cb, &resp, &mgc);
    427         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    428 
    429         gc = mem_gc_get_ctx(mgc);
    430         PCUT_ASSERT_NOT_NULL(gc);
    431 
    432         memset(&resp, 0, sizeof(resp));
    433         resp.rc = EOK;
    434         PCUT_ASSERT_FALSE(resp.cursor_set_visible_called);
    435 
    436         rc = gfx_cursor_set_visible(gc, true);
    437         PCUT_ASSERT_TRUE(resp.cursor_set_visible_called);
    438         PCUT_ASSERT_TRUE(resp.set_visible_vis);
    439 
    440         resp.cursor_set_visible_called = false;
    441 
    442         rc = gfx_cursor_set_visible(gc, false);
    443         PCUT_ASSERT_TRUE(resp.cursor_set_visible_called);
    444         PCUT_ASSERT_FALSE(resp.set_visible_vis);
    445 
    446         mem_gc_delete(mgc);
    447         free(alloc.pixels);
    448 }
    449 
    450 /** Called by memory GC when a rectangle is modified. */
    451 static void test_invalidate_rect(void *arg, gfx_rect_t *rect)
    452 {
    453         test_resp_t *resp = (test_resp_t *)arg;
    454 
    455         resp->invalidate_called = true;
    456         resp->inv_rect = *rect;
    457 }
    458 
    459 /** Called by memory GC when update is called. */
    460 static void test_update(void *arg)
    461 {
    462         test_resp_t *resp = (test_resp_t *)arg;
    463 
    464         resp->update_called = true;
    465 }
    466 
    467 /** Called by memory GC when cursor_get_pos is called. */
    468 static errno_t test_cursor_get_pos(void *arg, gfx_coord2_t *pos)
    469 {
    470         test_resp_t *resp = (test_resp_t *)arg;
    471 
    472         resp->cursor_get_pos_called = true;
    473         *pos = resp->get_pos_pos;
    474         return resp->rc;
    475 }
    476 
    477 /** Called by memory GC when cursor_set_pos is called. */
    478 static errno_t test_cursor_set_pos(void *arg, gfx_coord2_t *pos)
    479 {
    480         test_resp_t *resp = (test_resp_t *)arg;
    481 
    482         resp->cursor_set_pos_called = true;
    483         resp->set_pos_pos = *pos;
    484         return resp->rc;
    485 }
    486 
    487 /** Called by memory GC when cursor_set_visible is called. */
    488 static errno_t test_cursor_set_visible(void *arg, bool visible)
    489 {
    490         test_resp_t *resp = (test_resp_t *)arg;
    491 
    492         resp->cursor_set_visible_called = true;
    493         resp->set_visible_vis = visible;
    494         return resp->rc;
     252/** Called by memory GC when a rectangle is updated. */
     253static void test_update_rect(void *arg, gfx_rect_t *rect)
     254{
     255        test_update_t *update = (test_update_t *)arg;
     256
     257        update->update_called = true;
     258        update->rect = *rect;
    495259}
    496260
Note: See TracChangeset for help on using the changeset viewer.