Changeset 7e38970d in mainline for uspace/app/gfxdemo/gfxdemo.c


Ignore:
Timestamp:
2020-12-07T00:08:37Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
25f26600
Parents:
7a873f0 (diff), 8596474 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'jxsvoboda-gfx' into master

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/gfxdemo/gfxdemo.c

    r7a873f0 r7e38970d  
    3333 */
    3434
    35 #include <canvas.h>
    3635#include <congfx/console.h>
    37 #include <draw/surface.h>
    3836#include <display.h>
    3937#include <fibril.h>
    40 #include <guigfx/canvas.h>
    4138#include <gfx/bitmap.h>
    4239#include <gfx/color.h>
    4340#include <gfx/render.h>
     41#include <gfx/font.h>
     42#include <gfx/text.h>
     43#include <gfx/typeface.h>
    4444#include <io/console.h>
    4545#include <io/pixelmap.h>
     
    4848#include <str.h>
    4949#include <task.h>
    50 #include <window.h>
     50#include <ui/ui.h>
     51#include <ui/window.h>
     52#include <ui/wdecor.h>
    5153
    5254static void wnd_close_event(void *);
     
    5860};
    5961
     62static void uiwnd_close_event(ui_window_t *, void *);
     63static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
     64
     65static ui_window_cb_t ui_window_cb = {
     66        .close = uiwnd_close_event,
     67        .kbd = uiwnd_kbd_event
     68};
     69
    6070static bool quit = false;
    6171
     
    109119        int i, j;
    110120        errno_t rc;
     121
     122        if (quit)
     123                return EOK;
    111124
    112125        rc = clear_scr(gc, w, h);
     
    265278        gfx_rect_t srect;
    266279        errno_t rc;
     280
     281        if (quit)
     282                return EOK;
    267283
    268284        rc = clear_scr(gc, w, h);
     
    299315
    300316                        if (quit)
    301                                 break;
     317                                goto out;
    302318                }
    303319        }
    304320
     321out:
    305322        gfx_bitmap_destroy(bitmap);
    306323
     
    324341        gfx_coord2_t offs;
    325342        errno_t rc;
     343
     344        if (quit)
     345                return EOK;
    326346
    327347        rc = clear_scr(gc, w, h);
     
    366386        return rc;
    367387}
     388
    368389/** Run bitmap color key demo on a graphic context.
    369390 *
     
    379400        gfx_coord2_t offs;
    380401        errno_t rc;
     402
     403        if (quit)
     404                return EOK;
    381405
    382406        rc = clear_scr(gc, w, h);
     
    424448}
    425449
    426 /** Run demo loop on a graphic context.
     450/** Run text demo on a graphic context.
    427451 *
    428452 * @param gc Graphic context
     
    430454 * @param h Height
    431455 */
     456static errno_t demo_text(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     457{
     458        gfx_color_t *color = NULL;
     459        gfx_rect_t rect;
     460        gfx_typeface_t *tface = NULL;
     461        gfx_font_info_t *finfo;
     462        gfx_font_t *font = NULL;
     463        gfx_coord2_t pos;
     464        gfx_text_fmt_t fmt;
     465        int i;
     466        errno_t rc;
     467
     468        if (quit)
     469                return EOK;
     470
     471        rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
     472        if (rc != EOK) {
     473                printf("Error opening typeface\n");
     474                goto error;
     475        }
     476
     477        finfo = gfx_typeface_first_font(tface);
     478        if (finfo == NULL) {
     479                printf("Typeface contains no font.\n");
     480                rc = ENOENT;
     481                goto error;
     482        }
     483
     484        rc = gfx_font_open(finfo, &font);
     485        if (rc != EOK) {
     486                printf("Error opening font.\n");
     487                goto error;
     488        }
     489
     490        rc = clear_scr(gc, w, h);
     491        if (rc != EOK)
     492                goto error;
     493
     494        /* Vertical bars */
     495
     496        for (i = 0; i < 20; i++) {
     497                rc = gfx_color_new_rgb_i16(0, 0x8000 * i / 20,
     498                    0x8000 * i / 20, &color);
     499                if (rc != EOK)
     500                        goto error;
     501
     502                rc = gfx_set_color(gc, color);
     503                if (rc != EOK)
     504                        goto error;
     505
     506                rect.p0.x = w * i / 20;
     507                rect.p0.y = 0;
     508                rect.p1.x = w * (i + 1) / 20;
     509                rect.p1.y = h;
     510
     511                rc = gfx_fill_rect(gc, &rect);
     512                if (rc != EOK)
     513                        goto error;
     514
     515                gfx_color_delete(color);
     516        }
     517
     518        rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
     519        if (rc != EOK)
     520                goto error;
     521
     522        rc = gfx_set_color(gc, color);
     523        if (rc != EOK)
     524                goto error;
     525
     526        rect.p0.x = w / 20;
     527        rect.p0.y = 2 * h / 15;
     528        rect.p1.x = w - w / 20;
     529        rect.p1.y = 5 * h / 15;
     530
     531        rc = gfx_fill_rect(gc, &rect);
     532        if (rc != EOK)
     533                goto error;
     534
     535        gfx_color_delete(color);
     536
     537        rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     538        if (rc != EOK)
     539                goto error;
     540
     541        rc = gfx_set_color(gc, color);
     542        if (rc != EOK)
     543                goto error;
     544
     545        gfx_text_fmt_init(&fmt);
     546
     547        pos.x = rect.p0.x;
     548        pos.y = rect.p0.y;
     549        rc = gfx_puttext(font, &pos, &fmt, "Top left");
     550        if (rc != EOK) {
     551                printf("Error rendering text.\n");
     552                goto error;
     553        }
     554
     555        pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
     556        pos.y = rect.p0.y;
     557        fmt.halign = gfx_halign_center;
     558        rc = gfx_puttext(font, &pos, &fmt, "Top center");
     559        if (rc != EOK)
     560                goto error;
     561
     562        pos.x = rect.p1.x - 1;
     563        pos.y = rect.p0.y;
     564        fmt.halign = gfx_halign_right;
     565        rc = gfx_puttext(font, &pos, &fmt, "Top right");
     566        if (rc != EOK)
     567                goto error;
     568
     569        fmt.valign = gfx_valign_center;
     570
     571        pos.x = rect.p0.x;
     572        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
     573        fmt.halign = gfx_halign_left;
     574        rc = gfx_puttext(font, &pos, &fmt, "Center left");
     575        if (rc != EOK)
     576                goto error;
     577
     578        pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
     579        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
     580        fmt.halign = gfx_halign_center;
     581        rc = gfx_puttext(font, &pos, &fmt, "Center");
     582        if (rc != EOK)
     583                goto error;
     584
     585        pos.x = rect.p1.x - 1;
     586        pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
     587        fmt.halign = gfx_halign_right;
     588        rc = gfx_puttext(font, &pos, &fmt, "Center right");
     589        if (rc != EOK)
     590                goto error;
     591
     592        fmt.valign = gfx_valign_bottom;
     593
     594        pos.x = rect.p0.x;
     595        pos.y = rect.p1.y - 1;
     596        fmt.halign = gfx_halign_left;
     597        rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
     598        if (rc != EOK)
     599                goto error;
     600
     601        pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
     602        pos.y = rect.p1.y - 1;
     603        fmt.halign = gfx_halign_center;
     604        rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
     605        if (rc != EOK)
     606                goto error;
     607
     608        pos.x = rect.p1.x - 1;
     609        pos.y = rect.p1.y - 1;
     610        fmt.halign = gfx_halign_right;
     611        rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
     612        if (rc != EOK)
     613                goto error;
     614
     615        gfx_color_delete(color);
     616
     617        gfx_text_fmt_init(&fmt);
     618
     619        for (i = 0; i < 8; i++) {
     620                rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0,
     621                    (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color);
     622                if (rc != EOK)
     623                        goto error;
     624
     625                rc = gfx_set_color(gc, color);
     626                if (rc != EOK)
     627                        goto error;
     628
     629                pos.x = w / 20;
     630                pos.y = (7 + i) * h / 15;
     631                rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
     632                if (rc != EOK)
     633                        goto error;
     634
     635                gfx_color_delete(color);
     636        }
     637
     638        for (i = 0; i < 10; i++) {
     639                fibril_usleep(500 * 1000);
     640                if (quit)
     641                        break;
     642        }
     643
     644        gfx_font_close(font);
     645        gfx_typeface_destroy(tface);
     646        return EOK;
     647error:
     648        if (font != NULL)
     649                gfx_font_close(font);
     650        if (tface != NULL)
     651                gfx_typeface_destroy(tface);
     652        return rc;
     653}
     654
     655/** Run demo loop on a graphic context.
     656 *
     657 * @param gc Graphic context
     658 * @param w Width
     659 * @param h Height
     660 */
    432661static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
    433662{
     
    448677
    449678                rc = demo_bitmap_kc(gc, w, h);
     679                if (rc != EOK)
     680                        return rc;
     681
     682                rc = demo_text(gc, w, h);
    450683                if (rc != EOK)
    451684                        return rc;
     
    486719}
    487720
    488 /** Run demo on canvas. */
    489 static errno_t demo_canvas(const char *display_svc)
    490 {
    491         canvas_gc_t *cgc = NULL;
     721/** Run demo on UI. */
     722static errno_t demo_ui(const char *display_spec)
     723{
     724        ui_t *ui = NULL;
     725        ui_wnd_params_t params;
     726        ui_window_t *window = NULL;
    492727        gfx_context_t *gc;
    493         window_t *window = NULL;
    494         pixel_t *pixbuf = NULL;
    495         surface_t *surface = NULL;
    496         canvas_t *canvas = NULL;
    497         gfx_coord_t vw, vh;
    498         errno_t rc;
    499 
    500         printf("Init canvas..\n");
    501 
    502         window = window_open(display_svc, NULL,
    503             WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
    504         if (window == NULL) {
     728        gfx_rect_t rect;
     729        gfx_rect_t wrect;
     730        gfx_coord2_t off;
     731        errno_t rc;
     732
     733        printf("Init UI..\n");
     734
     735        rc = ui_create(display_spec, &ui);
     736        if (rc != EOK) {
     737                printf("Error initializing UI (%s)\n", display_spec);
     738                goto error;
     739        }
     740
     741        rect.p0.x = 0;
     742        rect.p0.y = 0;
     743        rect.p1.x = 400;
     744        rect.p1.y = 300;
     745
     746        ui_wnd_params_init(&params);
     747        params.caption = "GFX Demo";
     748
     749        /*
     750         * Compute window rectangle such that application area corresponds
     751         * to rect
     752         */
     753        ui_wdecor_rect_from_app(params.style, &rect, &wrect);
     754        off = wrect.p0;
     755        gfx_rect_rtranslate(&off, &wrect, &params.rect);
     756
     757        rc = ui_window_create(ui, &params, &window);
     758        if (rc != EOK) {
    505759                printf("Error creating window.\n");
    506                 return -1;
    507         }
    508 
    509         vw = 400;
    510         vh = 300;
    511 
    512         pixbuf = calloc(vw * vh, sizeof(pixel_t));
    513         if (pixbuf == NULL) {
    514                 printf("Error allocating memory for pixel buffer.\n");
    515                 return ENOMEM;
    516         }
    517 
    518         surface = surface_create(vw, vh, pixbuf, 0);
    519         if (surface == NULL) {
    520                 printf("Error creating surface.\n");
    521                 return EIO;
    522         }
    523 
    524         canvas = create_canvas(window_root(window), NULL, vw, vh,
    525             surface);
    526         if (canvas == NULL) {
    527                 printf("Error creating canvas.\n");
    528                 return EIO;
    529         }
    530 
    531         window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
    532         window_exec(window);
    533 
    534         printf("Create canvas GC\n");
    535         rc = canvas_gc_create(canvas, surface, &cgc);
    536         if (rc != EOK)
    537                 return rc;
    538 
    539         gc = canvas_gc_get_ctx(cgc);
     760                goto error;
     761        }
     762
     763        ui_window_set_cb(window, &ui_window_cb, NULL);
     764
     765        rc = ui_window_get_app_gc(window, &gc);
     766        if (rc != EOK) {
     767                printf("Error creating graphic context.\n");
     768                goto error;
     769        }
    540770
    541771        task_retval(0);
    542772
    543         rc = demo_loop(gc, 400, 300);
    544         if (rc != EOK)
    545                 return rc;
    546 
    547         rc = canvas_gc_delete(cgc);
    548         if (rc != EOK)
    549                 return rc;
    550 
    551         return EOK;
     773        rc = demo_loop(gc, rect.p1.x, rect.p1.y);
     774        if (rc != EOK)
     775                goto error;
     776
     777        ui_window_destroy(window);
     778        ui_destroy(ui);
     779
     780        return EOK;
     781error:
     782        if (window != NULL)
     783                ui_window_destroy(window);
     784        if (ui != NULL)
     785                ui_destroy(ui);
     786        return rc;
    552787}
    553788
     
    610845
    611846static void wnd_kbd_event(void *arg, kbd_event_t *event)
     847{
     848        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     849        if (event->type == KEY_PRESS)
     850                quit = true;
     851}
     852
     853static void uiwnd_close_event(ui_window_t *window, void *arg)
     854{
     855        printf("Close event\n");
     856        quit = true;
     857}
     858
     859static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
    612860{
    613861        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     
    653901                if (rc != EOK)
    654902                        return 1;
    655         } else if (str_cmp(argv[i], "canvas") == 0) {
    656                 rc = demo_canvas(display_svc);
     903        } else if (str_cmp(argv[i], "ui") == 0) {
     904                rc = demo_ui(display_svc);
    657905                if (rc != EOK)
    658906                        return 1;
Note: See TracChangeset for help on using the changeset viewer.