Changeset 12008adf in mainline
- Timestamp:
- 2020-11-12T10:58:36Z (4 years ago)
- 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)
- Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/barber/barber.c
r7a5825b r12008adf 1 1 /* 2 * Copyright (c) 2020 Jiri Svoboda 2 3 * Copyright (c) 2014 Martin Decky 3 4 * All rights reserved. … … 33 34 */ 34 35 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> 35 44 #include <stdbool.h> 36 #include <errno.h>37 45 #include <stdio.h> 38 46 #include <stdlib.h> 39 #include <task.h>40 #include <loc.h>41 #include <stats.h>42 47 #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> 50 52 #include "images.h" 51 53 … … 72 74 } led_dev_t; 73 75 74 static char *winreg = NULL; 76 typedef struct { 77 ui_t *ui; 78 } barber_t; 75 79 76 80 static fibril_timer_t *led_timer = NULL; … … 89 93 90 94 static fibril_timer_t *frame_timer = NULL; 91 static canvas_t *frame_canvas;95 static ui_image_t *frame_img; 92 96 static surface_t *frames[FRAMES]; 97 static gfx_bitmap_t *frame_bmp[FRAMES]; 93 98 94 99 static unsigned int frame = 0; … … 98 103 static void frame_timer_callback(void *); 99 104 100 static bool decode_frames(void) 101 { 105 static void wnd_close(ui_window_t *, void *); 106 107 static 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 */ 116 static 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 123 static 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 102 130 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); 104 133 if (frames[i] == NULL) { 105 134 printf("Unable to decode frame %u.\n", i); 106 135 return false; 107 136 } 137 138 surface_get_resolution(frames[i], &w, &h); 139 gfx_bitmap_params_init(¶ms); 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, ¶ms, &alloc, &frame_bmp[i]); 148 if (rc != EOK) { 149 printf("Error creating bitmap.\n"); 150 return false; 151 } 108 152 } 109 153 110 154 return true; 155 } 156 157 static 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 } 111 168 } 112 169 … … 192 249 { 193 250 struct timespec prev; 251 gfx_rect_t rect; 194 252 getuptime(&prev); 195 253 … … 198 256 frame = 0; 199 257 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); 201 265 202 266 struct timespec cur; … … 255 319 int main(int argc, char *argv[]) 256 320 { 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; 258 332 int i; 259 333 … … 268 342 } 269 343 270 display_s vc = argv[i++];344 display_spec = argv[i++]; 271 345 } else { 272 346 printf("Invalid option '%s'.\n", argv[i]); … … 295 369 } 296 370 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(¶ms); 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, ¶ms.rect); 391 392 barber.ui = ui; 393 394 rc = ui_window_create(ui, ¶ms, &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 } 320 424 321 425 plan_led_timer(); 322 426 plan_frame_timer(0); 323 427 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(); 326 437 327 438 return 0; -
uspace/app/barber/meson.build
r7a5825b r12008adf 27 27 # 28 28 29 deps = [ ' gui', 'draw', 'compress', 'softrend', 'math' ]29 deps = [ 'ui', 'draw', 'compress' ] 30 30 31 31 _images = files( -
uspace/lib/ui/include/ui/image.h
r7a5825b r12008adf 48 48 extern void ui_image_destroy(ui_image_t *); 49 49 extern ui_control_t *ui_image_ctl(ui_image_t *); 50 extern void ui_image_set_bmp(ui_image_t *, gfx_bitmap_t *, gfx_rect_t *); 50 51 extern void ui_image_set_rect(ui_image_t *, gfx_rect_t *); 51 52 extern errno_t ui_image_paint(ui_image_t *); -
uspace/lib/ui/src/image.c
r7a5825b r12008adf 134 134 gfx_coord2_t offs; 135 135 136 if (image->bitmap == NULL) 137 return EOK; 138 136 139 /* 137 140 * UI image position does not depend on bitmap rectangle p0, so … … 149 152 } 150 153 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 */ 164 void 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 151 171 /** Destroy image control. 152 172 * -
uspace/lib/ui/test/image.c
r7a5825b r12008adf 87 87 PCUT_TEST(set_rect) 88 88 { 89 90 89 ui_image_t *image = NULL; 91 90 gfx_rect_t brect; … … 107 106 PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x); 108 107 PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y); 108 109 ui_image_destroy(image); 110 } 111 112 /** Set image bitmap */ 113 PCUT_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(¶ms); 145 rc = gfx_bitmap_create(gc, ¶ms, 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); 109 153 110 154 ui_image_destroy(image); … … 138 182 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 139 183 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 140 191 ui_image_destroy(image); 141 192 }
Note:
See TracChangeset
for help on using the changeset viewer.