Changeset 12008adf in mainline


Ignore:
Timestamp:
2020-11-12T10:58:36Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2c9fdeed
Parents:
7a5825b
git-author:
Jiri Svoboda <jiri@…> (2020-11-11 23:58:55)
git-committer:
Jiri Svoboda <jiri@…> (2020-11-12 10:58:36)
Message:

Port barber to UI

Location:
uspace
Files:
5 edited

Legend:

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

    r7a5825b r12008adf  
    11/*
     2 * Copyright (c) 2020 Jiri Svoboda
    23 * Copyright (c) 2014 Martin Decky
    34 * All rights reserved.
     
    3334 */
    3435
     36#include <draw/surface.h>
     37#include <draw/codec.h>
     38#include <device/led_dev.h>
     39#include <errno.h>
     40#include <fibril_synch.h>
     41#include <io/pixel.h>
     42#include <loc.h>
     43#include <stats.h>
    3544#include <stdbool.h>
    36 #include <errno.h>
    3745#include <stdio.h>
    3846#include <stdlib.h>
    39 #include <task.h>
    40 #include <loc.h>
    41 #include <stats.h>
    4247#include <str.h>
    43 #include <fibril_synch.h>
    44 #include <io/pixel.h>
    45 #include <device/led_dev.h>
    46 #include <window.h>
    47 #include <canvas.h>
    48 #include <draw/surface.h>
    49 #include <draw/codec.h>
     48#include <ui/ui.h>
     49#include <ui/wdecor.h>
     50#include <ui/window.h>
     51#include <ui/image.h>
    5052#include "images.h"
    5153
     
    7274} led_dev_t;
    7375
    74 static char *winreg = NULL;
     76typedef struct {
     77        ui_t *ui;
     78} barber_t;
    7579
    7680static fibril_timer_t *led_timer = NULL;
     
    8993
    9094static fibril_timer_t *frame_timer = NULL;
    91 static canvas_t *frame_canvas;
     95static ui_image_t *frame_img;
    9296static surface_t *frames[FRAMES];
     97static gfx_bitmap_t *frame_bmp[FRAMES];
    9398
    9499static unsigned int frame = 0;
     
    98103static void frame_timer_callback(void *);
    99104
    100 static bool decode_frames(void)
    101 {
     105static void wnd_close(ui_window_t *, void *);
     106
     107static ui_window_cb_t window_cb = {
     108        .close = wnd_close
     109};
     110
     111/** Window close button was clicked.
     112 *
     113 * @param window Window
     114 * @param arg Argument (launcher)
     115 */
     116static void wnd_close(ui_window_t *window, void *arg)
     117{
     118        barber_t *barber = (barber_t *) arg;
     119
     120        ui_quit(barber->ui);
     121}
     122
     123static bool decode_frames(gfx_context_t *gc)
     124{
     125        gfx_bitmap_alloc_t alloc;
     126        surface_coord_t w, h;
     127        gfx_bitmap_params_t params;
     128        errno_t rc;
     129
    102130        for (unsigned int i = 0; i < FRAMES; i++) {
    103                 frames[i] = decode_tga_gz(images[i].addr, images[i].size, 0);
     131                frames[i] = decode_tga_gz(images[i].addr, images[i].size,
     132                    SURFACE_FLAG_SHARED);
    104133                if (frames[i] == NULL) {
    105134                        printf("Unable to decode frame %u.\n", i);
    106135                        return false;
    107136                }
     137
     138                surface_get_resolution(frames[i], &w, &h);
     139                gfx_bitmap_params_init(&params);
     140                params.rect.p1.x = w;
     141                params.rect.p1.y = h;
     142
     143                alloc.pitch = sizeof(uint32_t) * w;
     144                alloc.off0 = 0;
     145                alloc.pixels = surface_direct_access(frames[i]);
     146
     147                rc = gfx_bitmap_create(gc, &params, &alloc, &frame_bmp[i]);
     148                if (rc != EOK) {
     149                        printf("Error creating bitmap.\n");
     150                        return false;
     151                }
    108152        }
    109153
    110154        return true;
     155}
     156
     157static void destroy_frames(void)
     158{
     159        unsigned i;
     160
     161        for (i = 0; i < FRAMES; i++) {
     162                gfx_bitmap_destroy(frame_bmp[i]);
     163                frame_bmp[i] = NULL;
     164
     165                surface_destroy(frames[i]);
     166                frames[i] = NULL;
     167        }
    111168}
    112169
     
    192249{
    193250        struct timespec prev;
     251        gfx_rect_t rect;
    194252        getuptime(&prev);
    195253
     
    198256                frame = 0;
    199257
    200         update_canvas(frame_canvas, frames[frame]);
     258        rect.p0.x = 0;
     259        rect.p0.y = 0;
     260        rect.p1.x = FRAME_WIDTH;
     261        rect.p1.y = FRAME_HEIGHT;
     262
     263        ui_image_set_bmp(frame_img, frame_bmp[frame], &rect);
     264        (void) ui_image_paint(frame_img);
    201265
    202266        struct timespec cur;
     
    255319int main(int argc, char *argv[])
    256320{
    257         const char *display_svc = DISPLAY_DEFAULT;
     321        const char *display_spec = UI_DISPLAY_DEFAULT;
     322        barber_t barber;
     323        ui_t *ui;
     324        ui_wnd_params_t params;
     325        ui_window_t *window;
     326        ui_resource_t *ui_res;
     327        gfx_rect_t rect;
     328        gfx_rect_t wrect;
     329        gfx_rect_t app_rect;
     330        gfx_context_t *gc;
     331        gfx_coord2_t off;
    258332        int i;
    259333
     
    268342                        }
    269343
    270                         display_svc = argv[i++];
     344                        display_spec = argv[i++];
    271345                } else {
    272346                        printf("Invalid option '%s'.\n", argv[i]);
     
    295369        }
    296370
    297         if (!decode_frames())
    298                 return 1;
    299 
    300         winreg = argv[1];
    301         window_t *main_window = window_open(display_svc, NULL,
    302             WINDOW_MAIN | WINDOW_DECORATED, "barber");
    303         if (!main_window) {
    304                 printf("Cannot open main window.\n");
    305                 return 1;
    306         }
    307 
    308         frame_canvas = create_canvas(window_root(main_window), NULL,
    309             FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
    310 
    311         if (!frame_canvas) {
    312                 window_close(main_window);
    313                 printf("Cannot create widgets.\n");
    314                 return 1;
    315         }
    316 
    317         window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
    318             WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
    319         window_exec(main_window);
     371        rc = ui_create(display_spec, &ui);
     372        if (rc != EOK) {
     373                printf("Error creating UI on display %s.\n", display_spec);
     374                return 1;
     375        }
     376
     377        rect.p0.x = 0;
     378        rect.p0.y = 0;
     379        rect.p1.x = FRAME_WIDTH;
     380        rect.p1.y = FRAME_HEIGHT;
     381
     382        ui_wnd_params_init(&params);
     383        params.caption = "";
     384        /*
     385         * Compute window rectangle such that application area corresponds
     386         * to rect
     387         */
     388        ui_wdecor_rect_from_app(&rect, &wrect);
     389        off = wrect.p0;
     390        gfx_rect_rtranslate(&off, &wrect, &params.rect);
     391
     392        barber.ui = ui;
     393
     394        rc = ui_window_create(ui, &params, &window);
     395        if (rc != EOK) {
     396                printf("Error creating window.\n");
     397                return 1;
     398        }
     399
     400        ui_res = ui_window_get_res(window);
     401        gc = ui_window_get_gc(window);
     402        ui_window_get_app_rect(window, &app_rect);
     403        ui_window_set_cb(window, &window_cb, (void *) &barber);
     404
     405        if (!decode_frames(gc))
     406                return 1;
     407
     408        rc = ui_image_create(ui_res, frame_bmp[frame], &rect,
     409            &frame_img);
     410        if (rc != EOK) {
     411                printf("Error creating UI.\n");
     412                return 1;
     413        }
     414
     415        ui_image_set_rect(frame_img, &app_rect);
     416
     417        ui_window_add(window, ui_image_ctl(frame_img));
     418
     419        rc = ui_window_paint(window);
     420        if (rc != EOK) {
     421                printf("Error painting window.\n");
     422                return 1;
     423        }
    320424
    321425        plan_led_timer();
    322426        plan_frame_timer(0);
    323427
    324         task_retval(0);
    325         async_manager();
     428        ui_run(ui);
     429
     430        /* Unlink bitmap from image so it is not destroyed along with it */
     431        ui_image_set_bmp(frame_img, NULL, &rect);
     432
     433        ui_window_destroy(window);
     434        ui_destroy(ui);
     435
     436        destroy_frames();
    326437
    327438        return 0;
  • uspace/app/barber/meson.build

    r7a5825b r12008adf  
    2727#
    2828
    29 deps = [ 'gui', 'draw', 'compress', 'softrend', 'math' ]
     29deps = [ 'ui', 'draw', 'compress' ]
    3030
    3131_images = files(
  • uspace/lib/ui/include/ui/image.h

    r7a5825b r12008adf  
    4848extern void ui_image_destroy(ui_image_t *);
    4949extern ui_control_t *ui_image_ctl(ui_image_t *);
     50extern void ui_image_set_bmp(ui_image_t *, gfx_bitmap_t *, gfx_rect_t *);
    5051extern void ui_image_set_rect(ui_image_t *, gfx_rect_t *);
    5152extern errno_t ui_image_paint(ui_image_t *);
  • uspace/lib/ui/src/image.c

    r7a5825b r12008adf  
    134134        gfx_coord2_t offs;
    135135
     136        if (image->bitmap == NULL)
     137                return EOK;
     138
    136139        /*
    137140         * UI image position does not depend on bitmap rectangle p0, so
     
    149152}
    150153
     154/** Change image bitmap.
     155 *
     156 * Note that the caller must have saved the pointer to the previous bitmap
     157 * in the image, because this causes it to be unlinked from the image and
     158 * not destroyed (the ownership is transferred back to the caller).
     159 *
     160 * @param image Image
     161 * @param bitmap New bitmap (ownership transferred to image) or @c NULL
     162 * @param brect New bitmap rectangle
     163 */
     164void ui_image_set_bmp(ui_image_t *image, gfx_bitmap_t *bitmap,
     165    gfx_rect_t *brect)
     166{
     167        image->bitmap = bitmap;
     168        image->brect = *brect;
     169}
     170
    151171/** Destroy image control.
    152172 *
  • uspace/lib/ui/test/image.c

    r7a5825b r12008adf  
    8787PCUT_TEST(set_rect)
    8888{
    89 
    9089        ui_image_t *image = NULL;
    9190        gfx_rect_t brect;
     
    107106        PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
    108107        PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
     108
     109        ui_image_destroy(image);
     110}
     111
     112/** Set image bitmap */
     113PCUT_TEST(set_bmp)
     114{
     115        ui_image_t *image = NULL;
     116        gfx_rect_t brect;
     117        gfx_rect_t rect;
     118        gfx_bitmap_t *bitmap;
     119        gfx_bitmap_params_t params;
     120        dummy_gc_t *dgc;
     121        gfx_context_t *gc;
     122        errno_t rc;
     123
     124        rc = dummygc_create(&dgc);
     125        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     126
     127        gc = dummygc_get_ctx(dgc);
     128
     129        rc = ui_image_create(NULL, NULL, &brect, &image);
     130        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     131        PCUT_ASSERT_NOT_NULL(image);
     132
     133        rect.p0.x = 1;
     134        rect.p0.y = 2;
     135        rect.p1.x = 3;
     136        rect.p1.y = 4;
     137
     138        ui_image_set_rect(image, &rect);
     139        PCUT_ASSERT_INT_EQUALS(rect.p0.x, image->rect.p0.x);
     140        PCUT_ASSERT_INT_EQUALS(rect.p0.y, image->rect.p0.y);
     141        PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
     142        PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
     143
     144        gfx_bitmap_params_init(&params);
     145        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     146        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     147
     148        ui_image_set_bmp(image, bitmap, &brect);
     149        PCUT_ASSERT_EQUALS(bitmap, image->bitmap);
     150
     151        rc = ui_image_paint(image);
     152        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    109153
    110154        ui_image_destroy(image);
     
    138182        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    139183
     184        /* Check that we can paint image after setting bitmap to NULL */
     185
     186        ui_image_set_bmp(image, NULL, &brect);
     187
     188        rc = ui_image_paint(image);
     189        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     190
    140191        ui_image_destroy(image);
    141192}
Note: See TracChangeset for help on using the changeset viewer.