Merge pull request #1155 from yangfasheng/master

gui 引擎优化提交
This commit is contained in:
Bernard Xiong 2018-01-02 13:52:55 +08:00 committed by GitHub
commit 7678659b2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 183 additions and 89 deletions

View File

@ -329,7 +329,7 @@ enum rtgui_gesture_type
RTGUI_GESTURE_PINCH = 0x0002,
RTGUI_GESTURE_DRAG = 0x0004,
RTGUI_GESTURE_LONGPRESS = 0x0008,
RTGUI_GESTURE_DRAGGED = 0x0001 | 0x0004 | 0x0008,
RTGUI_GESTURE_DRAGGED = 0x0001 | 0x0004 | 0x0008,
/* PINCH, DRAG finished. */
RTGUI_GESTURE_FINISH = 0x8000,
/* The corresponding gesture should be canceled. */
@ -342,6 +342,8 @@ struct rtgui_event_gesture
_RTGUI_EVENT_WIN_ELEMENTS
enum rtgui_gesture_type type;
rt_uint32_t win_acti_cnt; /* win id */
};
/*
@ -359,6 +361,8 @@ struct rtgui_event_mouse
/* id of touch session(from down to up). Different session should have
* different id. id should never be 0. */
rt_uint32_t id;
rt_uint32_t win_acti_cnt; /* win id */
};
#define RTGUI_MOUSE_BUTTON_LEFT 0x01
#define RTGUI_MOUSE_BUTTON_RIGHT 0x02
@ -379,6 +383,8 @@ struct rtgui_event_kbd
{
_RTGUI_EVENT_WIN_ELEMENTS
rt_uint32_t win_acti_cnt; /* win id */
rt_uint16_t type; /* key down or up */
rt_uint16_t key; /* current key */
rt_uint16_t mod; /* current key modifiers */

View File

@ -81,6 +81,8 @@ struct rtgui_app
rtgui_idle_func_t on_idle;
unsigned int window_cnt;
/* window activate count */
unsigned int win_acti_cnt;
void *user_data;
};
@ -115,6 +117,9 @@ rt_err_t rtgui_app_set_as_wm(struct rtgui_app *app);
void rtgui_app_set_main_win(struct rtgui_app *app, struct rtgui_win *win);
struct rtgui_win* rtgui_app_get_main_win(struct rtgui_app *app);
/* get the topwin belong app window activate count */
unsigned int rtgui_app_get_win_acti_cnt(void);
#ifdef __cplusplus
}
#endif

View File

@ -54,6 +54,8 @@ DECLARE_CLASS_TYPE(win);
#define RTGUI_WIN_STYLE_ONBTM 0x0080 /* window is in the bottom layer */
#define RTGUI_WIN_STYLE_MAINWIN 0x0106 /* window is a main window */
#define RTGUI_WIN_MAGIC 0xA5A55A5A /* win magic flag */
#define RTGUI_WIN_STYLE_DEFAULT (RTGUI_WIN_STYLE_CLOSEBOX | RTGUI_WIN_STYLE_MINIBOX)
#define WINTITLE_HEIGHT 20
@ -88,8 +90,8 @@ struct rtgui_win
/* inherit from container */
rtgui_container_t parent;
/* update count */
rt_base_t update;
/* update count */
rt_base_t update;
/* drawing count */
rt_base_t drawing;
@ -140,11 +142,12 @@ struct rtgui_win
/* Private data. */
rt_base_t (*_do_show)(struct rtgui_win *win);
/* app ref_count */
rt_uint16_t app_ref_count;
/* win magic flag, magic value is 0xA5A55A5A */
rt_uint32_t magic;
/* app ref_count */
rt_uint16_t app_ref_count;
/* win magic flag, magic value is 0xA5A55A5A */
rt_uint32_t magic;
};
rtgui_win_t *rtgui_win_create(struct rtgui_win *parent_window, const char *title,

View File

@ -324,9 +324,9 @@ void rtgui_dc_draw_text(struct rtgui_dc *dc, const char *text, struct rtgui_rect
}
len = strlen((const char *)text);
if (len == 0)
return;
if (len == 0)
return;
rtgui_font_draw(font, dc, text, len, rect);
}
RTM_EXPORT(rtgui_dc_draw_text);
@ -1689,6 +1689,13 @@ void rtgui_dc_get_rect(struct rtgui_dc *dc, rtgui_rect_t *rect)
dc_hw = (struct rtgui_dc_hw *) dc;
owner = dc_hw->owner;
rtgui_widget_get_rect(owner, rect);
if (owner->extent.x1 + rect->x2 > dc_hw->hw_driver->width)
rect->x2 = dc_hw->hw_driver->width - owner->extent.x1;
if (owner->extent.y1 + rect->y2 > dc_hw->hw_driver->height)
rect->y2 = dc_hw->hw_driver->height - owner->extent.y1;
break;
}
case RTGUI_DC_BUFFER:
@ -1806,7 +1813,7 @@ extern void rtgui_mouse_hide_cursor(void);
struct rtgui_dc *rtgui_dc_begin_drawing(rtgui_widget_t *owner)
{
struct rtgui_dc *dc;
struct rtgui_widget *widget;
struct rtgui_widget *widget, *parent;
struct rtgui_win *win;
RT_ASSERT(owner != RT_NULL);
@ -1815,11 +1822,18 @@ struct rtgui_dc *rtgui_dc_begin_drawing(rtgui_widget_t *owner)
if (win == RT_NULL)
return RT_NULL;
parent = (struct rtgui_widget *)win;
if (!(win->flag & RTGUI_WIN_FLAG_ACTIVATE) &&
(win->outer_clip.extents.x1 == win->outer_clip.extents.x2 ||
win->outer_clip.extents.y1 == win->outer_clip.extents.y2))
return RT_NULL;
if (!(win->flag & RTGUI_WIN_FLAG_ACTIVATE) &&
(parent->clip.extents.x1 == parent->clip.extents.x2 ||
parent->clip.extents.y1 == parent->clip.extents.y2))
return RT_NULL;
/* increase drawing count */
if (win->drawing == 0)
{

View File

@ -216,32 +216,22 @@ static void rtgui_dc_hw_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect
return;
if (x1 < dc->owner->extent.x1)
x1 = dc->owner->extent.x1;
if (x1 < 0)
x1 = 0;
x2 = rect->x2 + dc->owner->extent.x1;
if (x2 < dc->owner->extent.x1)
return;
if (x2 > dc->owner->extent.x2)
x2 = dc->owner->extent.x2;
if (x2 > dc->hw_driver->width)
x2 = dc->hw_driver->width;
y1 = rect->y1 + dc->owner->extent.y1;
if (y1 > dc->owner->extent.y2)
return;
if (y1 < dc->owner->extent.y1)
y1 = dc->owner->extent.y1;
if (y1 < 0)
y1 = 0;
y2 = rect->y2 + dc->owner->extent.y1;
if (y2 < dc->owner->extent.y1)
return;
if (y2 > dc->owner->extent.y2)
y2 = dc->owner->extent.y2;
if (y2 > dc->hw_driver->height)
y2 = dc->hw_driver->height;
/* fill rect */
for (; y1 < y2; y1++)

View File

@ -103,7 +103,7 @@ static void rtgui_bitmap_font_draw_text(struct rtgui_font *font, struct rtgui_dc
RT_ASSERT(bmp_font != RT_NULL);
rtgui_font_get_metrics(rtgui_dc_get_gc(dc)->font, text, &text_rect);
rtgui_font_get_metrics(font, text, &text_rect);
rtgui_rect_move_to_align(rect, &text_rect, RTGUI_DC_TEXTALIGN(dc));
/* parameter check */

View File

@ -275,12 +275,12 @@ static void _rtgui_rect_move_to_align(const rtgui_rect_t *rect, rtgui_rect_t *to
to->x1 += dw;
to->x2 += dw;
}
/* align to center horizontal */
else if (align & RTGUI_ALIGN_CENTER_HORIZONTAL)
{
to->x1 += dw >> 1;
to->x2 += dw >> 1;
}
/* align to center horizontal */
else if (align & RTGUI_ALIGN_CENTER_HORIZONTAL)
{
to->x1 += dw >> 1;
to->x2 += dw >> 1;
}
/* align to bottom */
if (align & RTGUI_ALIGN_BOTTOM)
@ -393,7 +393,7 @@ static void _get_metrics(struct rtgui_ttf_font *ttf_font, const rt_uint16_t *tex
static void _draw_bitmap(struct rtgui_dc *dc,
FTC_SBit bitmap,
rt_int16_t ox, rt_int16_t btm_y,
rt_uint8_t a, rt_uint8_t r, rt_uint8_t g, rt_uint8_t b)
rtgui_color_t fgc, rt_uint16_t right, rt_uint16_t bottom)
{
rt_int16_t x_start, y_start;
struct rtgui_blit_info info = { 0 };
@ -403,10 +403,12 @@ static void _draw_bitmap(struct rtgui_dc *dc,
x_start = ox + bitmap->left;
y_start = btm_y - bitmap->top;
info.a = a;
info.r = r;
info.g = g;
info.b = b;
PINFO(" draw bitmap (x, y) -> (%d, %d)\n", x_start, y_start);
info.a = RTGUI_RGB_A(fgc);
info.r = RTGUI_RGB_R(fgc);
info.g = RTGUI_RGB_G(fgc);
info.b = RTGUI_RGB_B(fgc);
if (dc->type == RTGUI_DC_HW)
{
@ -444,18 +446,22 @@ static void _draw_bitmap(struct rtgui_dc *dc,
}
else if (dc->type == RTGUI_DC_BUFFER)
{
int max_right, max_bottom;
dest_buf = (struct rtgui_dc_buffer*)dc;
max_right = right > dest_buf->width ? dest_buf->width : right;
max_bottom = bottom > dest_buf->height ? dest_buf->height : bottom;
if (x_start + bitmap->width < 0 || y_start + bitmap->height < 0
|| x_start > dest_buf->width || y_start > dest_buf->height)
|| x_start > max_right || y_start > max_bottom)
return;
/* blit source */
info.src = (rt_uint8_t *)bitmap->buffer;
info.src = info.src + (y_start > 0 ? 0 : -y_start) * bitmap->width + (x_start > 0 ? 0 : -x_start);
info.src_fmt = RTGRAPHIC_PIXEL_FORMAT_ALPHA;
info.src_w = bitmap->width - (x_start > 0 ? 0 : -x_start) - (x_start + bitmap->width < dest_buf->width ? 0 : x_start + bitmap->width - dest_buf->width);
info.src_h = bitmap->height - (y_start > 0 ? 0 : -y_start) - (y_start + bitmap->height < dest_buf->height ? 0 : y_start + bitmap->height - dest_buf->height);
info.src_w = bitmap->width - (x_start > 0 ? 0 : -x_start) - (x_start + bitmap->width < max_right ? 0 : x_start + bitmap->width - max_right);
info.src_h = bitmap->height - (y_start > 0 ? 0 : -y_start) - (y_start + bitmap->height < max_bottom ? 0 : y_start + bitmap->height - max_bottom);
info.src_pitch = bitmap->width;
info.src_skip = info.src_pitch - info.src_w;
@ -473,7 +479,13 @@ static void _draw_bitmap(struct rtgui_dc *dc,
{
struct rtgui_rect text_rect;
struct rtgui_point dc_point = { 0, 0 };
struct rtgui_dc *text_dc = rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, bitmap->width, bitmap->height);
struct rtgui_dc *text_dc;
if (x_start + bitmap->width < 0 || y_start + bitmap->height < 0
|| x_start > right || y_start > bottom)
return;
text_dc = rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, bitmap->width, bitmap->height);
if (text_dc)
{
dest_buf = (struct rtgui_dc_buffer*)text_dc;
@ -503,7 +515,7 @@ static void _draw_bitmap(struct rtgui_dc *dc,
text_rect.y1 = y_start;
text_rect.y2 = text_rect.y1 + bitmap->height;
rtgui_dc_buffer_set_alpha(text_dc, a);
rtgui_dc_buffer_set_alpha(text_dc, RTGUI_RGB_A(fgc));
rtgui_dc_blit(text_dc, &dc_point, dc, &text_rect);
rtgui_dc_destory(text_dc);
@ -515,7 +527,7 @@ static void _draw_text(struct rtgui_dc *dc,
struct rtgui_ttf_font *ttf_font,
rt_uint16_t *text_short,
rt_int16_t begin_x, rt_int16_t btm_y,
rt_uint8_t a, rt_uint8_t r, rt_uint8_t g, rt_uint8_t b)
rtgui_color_t fgc, rt_uint16_t right, rt_uint16_t bottom)
{
int glyphIndex;
FTC_SBit ftcSBit = RT_NULL;
@ -531,7 +543,7 @@ static void _draw_text(struct rtgui_dc *dc,
/* render font */
begin_x -= (ftcSBit->left - (abs(ftcSBit->left) + 2) / 2);
_draw_bitmap(dc, ftcSBit, begin_x, btm_y, a, r, g, b);
_draw_bitmap(dc, ftcSBit, begin_x, btm_y, fgc, right, bottom);
begin_x += ftcSBit->width + ftcSBit->left;
@ -594,10 +606,6 @@ static void ftc_draw_text(struct rtgui_font *font,
#endif
fgc = RTGUI_DC_FC(dc);
a = RTGUI_RGB_A(fgc);
r = RTGUI_RGB_R(fgc);
g = RTGUI_RGB_G(fgc);
b = RTGUI_RGB_B(fgc);
/* text align */
_get_metrics(ttf_font, text_short, &text_rect);
@ -633,7 +641,7 @@ static void ftc_draw_text(struct rtgui_font *font,
goto _out;
}
_draw_text(dc, ttf_font, text_short, begin_x, btm_y, a, r, g, b);
_draw_text(dc, ttf_font, text_short, begin_x, btm_y, fgc, rect->x2, rect->y2);
_out:
/* release unicode buffer */
@ -698,9 +706,9 @@ static void ftc_get_metrics(struct rtgui_font *font, const char *text, struct rt
RT_ASSERT(ttf_font != RT_NULL);
len = strlen(text);
if (len == 0)
return;
if (len == 0)
return;
memset(rect, 0, sizeof(struct rtgui_rect));
/* allocate unicode buffer */
@ -717,7 +725,7 @@ static void ftc_get_metrics(struct rtgui_font *font, const char *text, struct rt
#else
rt_memset(text_short, 0x00, (utf8_to_unicode_len(text, len) + 1) * 2);
#endif
/* convert gbk to unicode */
#ifndef UTF8_TO_UNICODE
gbk_to_unicode(text_short, text, len);
@ -775,7 +783,7 @@ static struct rtgui_ttf *rtgui_ttf_load(const char *filename)
return RT_NULL;
}
}
/* face_id init */
ttf->face_id.pathname = rt_strdup(filename);
ttf->face_id.face_index = 0;
@ -847,11 +855,11 @@ rtgui_font_t *rtgui_freetype_font_create(const char *filename, rt_size_t size)
font = rtgui_font_refer(filename, size);
if (font)
{
if (font->height == size)
return font;
else
rtgui_font_derefer(font);
}
if (font->height == size)
return font;
else
rtgui_font_derefer(font);
}
}
font = (struct rtgui_font *)rtgui_malloc(sizeof(struct rtgui_font) + sizeof(struct rtgui_ttf_font));

View File

@ -29,7 +29,7 @@
#include <rtgui/rtgui_system.h>
#include <rtgui/rtgui_app.h>
#include <rtgui/widgets/window.h>
#include "topwin.h"
#include <topwin.h>
static void _rtgui_app_constructor(struct rtgui_app *app)
{
@ -43,6 +43,7 @@ static void _rtgui_app_constructor(struct rtgui_app *app)
app->state_flag = RTGUI_APP_FLAG_EXITED;
app->ref_count = 0;
app->window_cnt = 0;
app->win_acti_cnt = 0;
app->exit_code = 0;
app->tid = RT_NULL;
app->mq = RT_NULL;
@ -203,11 +204,11 @@ rt_inline rt_bool_t _rtgui_application_dest_handle(
if (wevent->wid == RT_NULL)
return RT_FALSE;
if (wevent->wid->magic != 0xA5A55A5A)
{
return RT_FALSE;
}
if (wevent->wid->magic != RTGUI_WIN_MAGIC)
{
return RT_FALSE;
}
/* this window has been closed. */
if (wevent->wid != RT_NULL && wevent->wid->flag & RTGUI_WIN_FLAG_CLOSED)
@ -249,19 +250,55 @@ rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *ev
switch (event->type)
{
case RTGUI_EVENT_PAINT:
case RTGUI_EVENT_VPAINT_REQ:
case RTGUI_EVENT_KBD:
{
struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd*)event;
if (kbd->win_acti_cnt != app->win_acti_cnt)
break;
_rtgui_application_dest_handle(app, event);
}
break;
case RTGUI_EVENT_MOUSE_BUTTON:
case RTGUI_EVENT_MOUSE_MOTION:
case RTGUI_EVENT_CLIP_INFO:
{
struct rtgui_event_mouse *wevent = (struct rtgui_event_mouse *)event;
if (wevent->win_acti_cnt != app->win_acti_cnt)
break;
_rtgui_application_dest_handle(app, event);
}
break;
case RTGUI_EVENT_GESTURE:
{
struct rtgui_event_gesture *wevent = (struct rtgui_event_gesture *)event;
if (wevent->win_acti_cnt != app->win_acti_cnt)
break;
_rtgui_application_dest_handle(app, event);
}
break;
case RTGUI_EVENT_WIN_ACTIVATE:
{
app->win_acti_cnt++;
_rtgui_application_dest_handle(app, event);
}
break;
case RTGUI_EVENT_PAINT:
case RTGUI_EVENT_VPAINT_REQ:
case RTGUI_EVENT_CLIP_INFO:
case RTGUI_EVENT_WIN_DEACTIVATE:
case RTGUI_EVENT_WIN_CLOSE:
case RTGUI_EVENT_WIN_MOVE:
case RTGUI_EVENT_WIN_SHOW:
case RTGUI_EVENT_WIN_HIDE:
case RTGUI_EVENT_KBD:
case RTGUI_EVENT_GESTURE:
_rtgui_application_dest_handle(app, event);
break;
@ -284,14 +321,14 @@ rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *ev
case RTGUI_EVENT_TIMER:
{
rt_base_t level;
rt_base_t level;
struct rtgui_timer *timer;
struct rtgui_event_timer *etimer = (struct rtgui_event_timer *) event;
timer = etimer->timer;
level = rt_hw_interrupt_disable();
level = rt_hw_interrupt_disable();
timer->pending_cnt--;
rt_hw_interrupt_enable(level);
rt_hw_interrupt_enable(level);
RT_ASSERT(timer->pending_cnt >= 0);
if (timer->state == RTGUI_TIMER_ST_DESTROY_PENDING)
{
@ -320,21 +357,21 @@ rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *ev
if (ecmd->wid != RT_NULL)
return _rtgui_application_dest_handle(app, event);
else
{
struct rtgui_topwin *wnd;
else
{
struct rtgui_topwin *wnd;
wnd = rtgui_topwin_get_focus();
if (wnd != RT_NULL)
{
RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
wnd = rtgui_topwin_get_focus();
if (wnd != RT_NULL)
{
RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
/* send to focus window */
ecmd->wid = wnd->wid;
/* send to focus window */
ecmd->wid = wnd->wid;
return _rtgui_application_dest_handle(app, event);
}
}
return _rtgui_application_dest_handle(app, event);
}
}
}
default:
return rtgui_object_event_handler(object, event);
@ -507,3 +544,16 @@ struct rtgui_win *rtgui_app_get_main_win(struct rtgui_app *app)
}
RTM_EXPORT(rtgui_app_get_main_win);
unsigned int rtgui_app_get_win_acti_cnt(void)
{
struct rtgui_app *app = rtgui_topwin_app_get_focus();
if (app)
{
return app->win_acti_cnt;
}
return 0;
}
RTM_EXPORT(rtgui_app_get_win_acti_cnt);

View File

@ -764,7 +764,7 @@ rt_err_t rtgui_topwin_move(struct rtgui_event_win_move *event)
old_rect = topwin->extent;
/* move window rect */
rtgui_rect_move(&(topwin->extent), dx, dy);
rtgui_rect_move(&(topwin->extent), dx, dy);
/* move the monitor rect list */
rtgui_list_foreach(node, &(topwin->monitor_list))
@ -772,7 +772,7 @@ rt_err_t rtgui_topwin_move(struct rtgui_event_win_move *event)
struct rtgui_mouse_monitor *monitor = rtgui_list_entry(node,
struct rtgui_mouse_monitor,
list);
rtgui_rect_move(&(monitor->rect), dx, dy);
rtgui_rect_move(&(monitor->rect), dx, dy);
}
/* update windows clip info */
@ -853,6 +853,19 @@ struct rtgui_topwin *rtgui_topwin_get_focus(void)
return _rtgui_topwin_get_focus_from_list(&_rtgui_topwin_list);
}
struct rtgui_app *rtgui_topwin_app_get_focus(void)
{
struct rtgui_app *topwin_app = RT_NULL;
struct rtgui_topwin *topwin = rtgui_topwin_get_focus();
if (topwin)
{
topwin_app = topwin->app;
}
return topwin_app;
}
static struct rtgui_topwin *_rtgui_topwin_get_wnd_from_tree(struct rt_list_node *list,
int x, int y,
rt_bool_t exclude_modaled)
@ -978,7 +991,8 @@ static void _rtgui_topwin_redraw_tree(struct rt_list_node *list,
if (rtgui_rect_is_intersect(rect, &(topwin->extent)) == RT_EOK)
{
epaint->wid = topwin->wid;
rtgui_send(topwin->app, &(epaint->parent), sizeof(*epaint));
// < XY do !!! >
//rtgui_send(topwin->app, &(epaint->parent), sizeof(*epaint));
}
_rtgui_topwin_redraw_tree(&topwin->child_list, rect, epaint);
@ -1026,7 +1040,8 @@ rt_err_t rtgui_topwin_modal_enter(struct rtgui_event_win_modal_enter *event)
parent_top->flag |= WINTITLE_MODALED;
parent_top->wid->flag |= RTGUI_WIN_FLAG_UNDER_MODAL;
parent_top = parent_top->parent;
} while (parent_top);
}
while (parent_top);
topwin->flag &= ~WINTITLE_MODALED;
topwin->wid->flag &= ~RTGUI_WIN_FLAG_UNDER_MODAL;

View File

@ -64,6 +64,9 @@ void rtgui_topwin_remove_monitor_rect(struct rtgui_win *wid, rtgui_rect_t *rect)
/* get the topwin that is currently focused */
struct rtgui_topwin *rtgui_topwin_get_focus(void);
/* get the topwin which app I belong */
struct rtgui_app *rtgui_topwin_app_get_focus(void);
struct rtgui_topwin *rtgui_topwin_get_topmost_window_shown_all(void);
#endif

View File

@ -41,7 +41,7 @@ static void _rtgui_win_constructor(rtgui_win_t *win)
RTGUI_WIDGET(win)->toplevel = win;
/* init win property */
win->update = 0;
win->update = 0;
win->drawing = 0;
RTGUI_WIDGET(win)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
@ -440,7 +440,7 @@ rt_base_t rtgui_win_show(struct rtgui_win *win, rt_bool_t is_modal)
{
RTGUI_WIDGET_UNHIDE(win);
win->magic = 0xA5A55A5A;
win->magic = RTGUI_WIN_MAGIC;
if (is_modal)
win->flag |= RTGUI_WIN_FLAG_MODAL;
@ -548,7 +548,7 @@ void rtgui_win_move(struct rtgui_win *win, int x, int y)
dy = y - wgt->extent.y1;
rtgui_widget_move_to_logic(wgt, dx, dy);
}
rtgui_rect_move(&win->outer_extent, dx, dy);
rtgui_rect_move(&win->outer_extent, dx, dy);
if (win->flag & RTGUI_WIN_FLAG_CONNECTED)
{