From 285e3bfccb28fcd1d8e36648fbf8caadd667037c Mon Sep 17 00:00:00 2001 From: xianjimli Date: Mon, 30 Apr 2018 07:55:02 +0800 Subject: [PATCH] draw image support device pixel ratio --- demos/demo1_app.c | 2 +- src/base/canvas.c | 79 +++++++++++++++++++++------------------- src/base/lcd.h | 7 ++++ src/lcd/lcd_mem.inc | 1 + src/lcd/lcd_nanovg.c | 5 ++- src/lcd/lcd_reg.inc | 1 + src/lcd/lcd_vgcanvas.inc | 1 + 7 files changed, 56 insertions(+), 40 deletions(-) diff --git a/demos/demo1_app.c b/demos/demo1_app.c index a30df6031..f11ea43da 100644 --- a/demos/demo1_app.c +++ b/demos/demo1_app.c @@ -115,7 +115,7 @@ ret_t application_init() { progress_bar = progress_bar_create(win, 10, 80, 168, 20); widget_set_value(progress_bar, 40); - // timer_add(on_timer, progress_bar, 200); + timer_add(on_timer, progress_bar, 200); #ifndef WITH_STM32F103ZE_RAW progress_bar_set_show_text(progress_bar, TRUE); diff --git a/src/base/canvas.c b/src/base/canvas.c index 700ed0e65..a0229e3b2 100644 --- a/src/base/canvas.c +++ b/src/base/canvas.c @@ -478,17 +478,6 @@ static ret_t canvas_do_draw_image(canvas_t* c, bitmap_t* img, rect_t* s, rect_t* return lcd_draw_image(c->lcd, img, &src, &dst); } -ret_t canvas_draw_image_at(canvas_t* c, bitmap_t* img, xy_t x, xy_t y) { - rect_t src; - rect_t dst; - return_value_if_fail(c != NULL && img != NULL, RET_BAD_PARAMS); - - rect_init(src, 0, 0, img->w, img->h); - rect_init(dst, x, y, img->w, img->h); - - return canvas_do_draw_image(c, img, &src, &dst); -} - ret_t canvas_draw_image(canvas_t* c, bitmap_t* img, rect_t* src, rect_t* dst) { rect_t d; return_value_if_fail(c != NULL && img != NULL && src != NULL && dst != NULL, RET_BAD_PARAMS); @@ -959,25 +948,6 @@ ret_t canvas_draw_image_scale(canvas_t* c, bitmap_t* img, rect_t* dst) { return canvas_draw_image(c, img, &s, &d); } -ret_t canvas_draw_image_center(canvas_t* c, bitmap_t* img, rect_t* dst) { - rect_t s; - rect_t d; - return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); - - s.x = 0; - s.y = 0; - s.h = img->h; - s.w = img->w; - - d = *dst; - d.x = dst->x + ((dst->w - s.w) >> 1); - d.y = dst->y + ((dst->h - s.h) >> 1); - d.w = s.w; - d.h = s.h; - - return canvas_draw_image(c, img, &s, &d); -} - ret_t canvas_draw_image_ex(canvas_t* c, bitmap_t* img, image_draw_type_t draw_type, rect_t* dst) { rect_t src; return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); @@ -1019,18 +989,20 @@ ret_t canvas_draw_image_ex(canvas_t* c, bitmap_t* img, image_draw_type_t draw_ty ret_t canvas_draw_icon(canvas_t* c, bitmap_t* img, xy_t cx, xy_t cy) { rect_t src; rect_t dst; - vgcanvas_t* vg = NULL; wh_t hw = 0; wh_t hh = 0; - return_value_if_fail(c != NULL && img != NULL, RET_BAD_PARAMS); + float_t ratio = 0; + return_value_if_fail(c != NULL && c->lcd != NULL && img != NULL, RET_BAD_PARAMS); + ratio = c->lcd->ratio; rect_init(src, 0, 0, img->w, img->h); - vg = lcd_get_vgcanvas(c->lcd); - if (vg && vg->ratio > 1) { - float_t ratio = 1.0f / vg->ratio; - hw = img->w * ratio * 0.5; - hh = img->h * ratio * 0.5; - rect_init(dst, cx - hw, cy - hh, 2 * hw, 2 * hh); + if (ratio > 1) { + float_t w = (img->w / ratio); + float_t h = (img->h / ratio); + float_t hw = w * 0.5; + float_t hh = h * 0.5; + + rect_init(dst, cx - hw, cy - hh, w, h); } else { hw = img->w >> 1; hh = img->h >> 1; @@ -1039,3 +1011,34 @@ ret_t canvas_draw_icon(canvas_t* c, bitmap_t* img, xy_t cx, xy_t cy) { return canvas_draw_image(c, img, &src, &dst); } + +ret_t canvas_draw_image_center(canvas_t* c, bitmap_t* img, rect_t* dst) { + xy_t cx = 0; + xy_t cy = 0; + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + cx = dst->x + (dst->w >> 1); + cy = dst->y + (dst->h >> 1); + + return canvas_draw_icon(c, img, cx, cy); +} + + +ret_t canvas_draw_image_at(canvas_t* c, bitmap_t* img, xy_t x, xy_t y) { + rect_t src; + rect_t dst; + float_t ratio = 0; + return_value_if_fail(c != NULL && c->lcd != NULL && img != NULL, RET_BAD_PARAMS); + + ratio = c->lcd->ratio; + rect_init(src, 0, 0, img->w, img->h); + + if (ratio > 1) { + rect_init(dst, x, y, img->w / ratio, img->h / ratio); + } else { + rect_init(dst, x, y, img->w, img->h); + } + + return canvas_do_draw_image(c, img, &src, &dst); +} + diff --git a/src/base/lcd.h b/src/base/lcd.h index 094be9820..372168913 100644 --- a/src/base/lcd.h +++ b/src/base/lcd.h @@ -200,6 +200,13 @@ struct _lcd_t { */ lcd_type_t type; + /** + * @property {float_t} ratio + * @readonly + * 屏幕密度。 + */ + float_t ratio; + rect_t* dirty_rect; }; diff --git a/src/lcd/lcd_mem.inc b/src/lcd/lcd_mem.inc index 11c9ad3c8..f22b101e7 100644 --- a/src/lcd/lcd_mem.inc +++ b/src/lcd/lcd_mem.inc @@ -273,6 +273,7 @@ lcd_t* lcd_mem_create(wh_t w, wh_t h, bool_t alloc) { base->w = w; base->h = h; + base->ratio = 1; base->type = LCD_FRAMEBUFFER; info->lcd_w = base->w; diff --git a/src/lcd/lcd_nanovg.c b/src/lcd/lcd_nanovg.c index bb031881c..41700d779 100644 --- a/src/lcd/lcd_nanovg.c +++ b/src/lcd/lcd_nanovg.c @@ -31,9 +31,12 @@ lcd_t* lcd_nanovg_init(SDL_Window* sdl_window) { int w = 0; int h = 0; + vgcanvas_t* vg = NULL; return_value_if_fail(sdl_window != NULL, NULL); SDL_GetWindowSize(sdl_window, &w, &h); + vg = vgcanvas_create(w, h, sdl_window); + return_value_if_fail(vg != NULL, NULL); - return lcd_vgcanvas_init(w, h, vgcanvas_create(w, h, sdl_window)); + return lcd_vgcanvas_init(w, h, vg); } diff --git a/src/lcd/lcd_reg.inc b/src/lcd/lcd_reg.inc index 67ac1fa89..fd5612f24 100644 --- a/src/lcd/lcd_reg.inc +++ b/src/lcd/lcd_reg.inc @@ -181,6 +181,7 @@ lcd_t* lcd_reg_create(wh_t w, wh_t h) { lcd->w = w; lcd->h = h; + lcd->ratio = 1; lcd->type = LCD_REGISTER; info->lcd_w = lcd->w; info->lcd_h = lcd->h; diff --git a/src/lcd/lcd_vgcanvas.inc b/src/lcd/lcd_vgcanvas.inc index e5dcec0dc..2c89545e5 100644 --- a/src/lcd/lcd_vgcanvas.inc +++ b/src/lcd/lcd_vgcanvas.inc @@ -211,6 +211,7 @@ lcd_t* lcd_vgcanvas_init(wh_t w, wh_t h, vgcanvas_t* canvas) { base->w = (wh_t)w; base->h = (wh_t)h; + base->ratio = canvas->ratio; base->type = LCD_VGCANVAS; info->lcd_w = base->w;