From 1df08b63883acbcf6b10a32eb18f103c0e60833b Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 16 Feb 2019 19:45:59 +0800 Subject: [PATCH] perf(renderer): improve the selection of the rendering target --- include/LCUI/util/math.h | 2 +- src/gui/metrics.c | 8 ++-- src/gui/widget_paint.c | 97 ++++++++++++++++++++++++++-------------- src/util/math.c | 3 -- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/include/LCUI/util/math.h b/include/LCUI/util/math.h index ea739a83c..42d9b7a65 100644 --- a/include/LCUI/util/math.h +++ b/include/LCUI/util/math.h @@ -42,7 +42,7 @@ LCUI_BEGIN_HEADER /** round double to interger */ -LCUI_API int iround(double x); +#define iround(X) (X < 0 ? (int)(X - 0.5) : (int)(X + 0.5)) LCUI_END_HEADER diff --git a/src/gui/metrics.c b/src/gui/metrics.c index ecadb9c2a..e61fc11ee 100644 --- a/src/gui/metrics.c +++ b/src/gui/metrics.c @@ -60,10 +60,10 @@ int LCUIMetrics_ComputeActual(float value, LCUI_StyleType type) void LCUIMetrics_ComputeRectActual(LCUI_Rect *dst, const LCUI_RectF *src) { - dst->x = LCUIMetrics_ComputeActual(src->x, LCUI_STYPE_PX); - dst->y = LCUIMetrics_ComputeActual(src->y, LCUI_STYPE_PX); - dst->width = LCUIMetrics_ComputeActual(src->width, LCUI_STYPE_PX); - dst->height = LCUIMetrics_ComputeActual(src->height, LCUI_STYPE_PX); + dst->x = iround(src->x * metrics.scale); + dst->y = iround(src->y * metrics.scale); + dst->width = iround(src->width * metrics.scale); + dst->height = iround(src->height * metrics.scale); } float LCUIMetrics_GetScale(void) diff --git a/src/gui/widget_paint.c b/src/gui/widget_paint.c index 1c297f94f..3adcc9d4e 100644 --- a/src/gui/widget_paint.c +++ b/src/gui/widget_paint.c @@ -82,11 +82,15 @@ typedef struct LCUI_WidgetRendererRec_ { /* actual paint rectangle in widget canvas rectangle, it relative to * root canvas */ - LCUI_Rect paint_rect; + LCUI_Rect actual_paint_rect; /* actual paint rectangle in widget content rectangle, it relative to * root canvas */ - LCUI_Rect content_rect; + LCUI_Rect actual_content_rect; + + /* Paint rectangle in widget content rectangle, it relative to + * root canvas */ + LCUI_RectF content_rect; LCUI_BOOL has_content_graph; LCUI_BOOL has_self_graph; @@ -369,40 +373,49 @@ static LCUI_WidgetRenderer WidgetRenderer(LCUI_Widget w, that->content_left = w->box.padding.x - w->box.canvas.x; that->content_top = w->box.padding.y - w->box.canvas.y; /* convert position of paint rectangle to root canvas relative */ - that->paint_rect.x = that->style->canvas_box.x + that->paint->rect.x; - that->paint_rect.y = that->style->canvas_box.y + that->paint->rect.y; - that->paint_rect.width = that->paint->rect.width; - that->paint_rect.height = that->paint->rect.height; + that->actual_paint_rect.x = + that->style->canvas_box.x + that->paint->rect.x; + that->actual_paint_rect.y = + that->style->canvas_box.y + that->paint->rect.y; + that->actual_paint_rect.width = that->paint->rect.width; + that->actual_paint_rect.height = that->paint->rect.height; DEBUG_MSG("[%s] paint_rect: (%d, %d, %d, %d)\n", w->id, - that->paint_rect.x, that->paint_rect.y, - that->paint_rect.width, that->paint_rect.height); + that->actual_paint_rect.x, that->actual_paint_rect.y, + that->actual_paint_rect.width, + that->actual_paint_rect.height); /* get actual paint rectangle in widget content rectangle */ that->can_render_centent = LCUIRect_GetOverlayRect( - &that->style->padding_box, &that->paint_rect, &that->content_rect); + &that->style->padding_box, &that->actual_paint_rect, + &that->actual_content_rect); + ; + LCUIRect_ToRectF(&that->actual_content_rect, &that->content_rect, + 1.0f / LCUIMetrics_GetScale()); DEBUG_MSG("[%s] content_rect: (%d, %d, %d, %d)\n", w->id, - that->content_rect.x, that->content_rect.y, - that->content_rect.width, that->content_rect.height); + that->actual_content_rect.x, that->actual_content_rect.y, + that->actual_content_rect.width, + that->actual_content_rect.height); DEBUG_MSG("[%s] content_box: (%d, %d, %d, %d)\n", w->id, style->content_box.x, style->content_box.y, style->content_box.width, style->content_box.height); DEBUG_MSG("[%s] border_box: (%d, %d, %d, %d)\n", w->id, style->border_box.x, style->border_box.y, style->border_box.width, style->border_box.height); - DEBUG_MSG("[%s][%d/%d] content_rect: (%d,%d,%d,%d), " - "canvas_rect: (%d,%d,%d,%d)\n", - w->id, w->index, - w->parent ? w->parent->children_show.length : 1, - that->content_rect.x, that->content_rect.y, - that->content_rect.width, that->content_rect.height, - that->paint->canvas.quote.left, that->paint->canvas.quote.top, - that->paint->canvas.width, that->paint->canvas.height); + DEBUG_MSG( + "[%s][%d/%d] content_rect: (%d,%d,%d,%d), " + "canvas_rect: (%d,%d,%d,%d)\n", + w->id, w->index, w->parent ? w->parent->children_show.length : 1, + that->actual_content_rect.x, that->actual_content_rect.y, + that->actual_content_rect.width, that->actual_content_rect.height, + that->paint->canvas.quote.left, that->paint->canvas.quote.top, + that->paint->canvas.width, that->paint->canvas.height); if (!that->can_render_centent) { return that; } if (that->has_content_graph) { that->content_graph.color_type = LCUI_COLOR_TYPE_ARGB; - Graph_Create(&that->content_graph, that->content_rect.width, - that->content_rect.height); + Graph_Create(&that->content_graph, + that->actual_content_rect.width, + that->actual_content_rect.height); } return that; } @@ -464,6 +477,7 @@ static size_t WidgetRenderer_RenderChildren(LCUI_WidgetRenderer that) size_t total = 0, count = 0; LCUI_Widget child; LCUI_Rect paint_rect; + LCUI_RectF child_rect; LinkedListNode *node; LCUI_PaintContextRec paint; LCUI_WidgetRenderer renderer; @@ -481,23 +495,35 @@ static size_t WidgetRenderer_RenderChildren(LCUI_WidgetRenderer that) count > that->target->rules->max_render_children_count) { break; } + /* + * The actual style calculation is time consuming, so here we + * use the existing properties to determine whether we need to + * render. + */ style.x = that->x + that->content_left; style.y = that->y + that->content_top; + child_rect.x = style.x + child->box.canvas.x; + child_rect.y = style.y + child->box.canvas.y; + child_rect.width = child->box.canvas.width; + child_rect.height = child->box.canvas.height; + if (!LCUIRectF_GetOverlayRect(&that->content_rect, &child_rect, + &child_rect)) { + continue; + } Widget_ComputeActualBorderBox(child, &style); Widget_ComputeActualCanvasBox(child, &style); DEBUG_MSG("content: %g, %g\n", that->content_left, that->content_top); DEBUG_MSG("content rect: (%d, %d, %d, %d)\n", - that->content_rect.x, that->content_rect.y, - that->content_rect.width, that->content_rect.height); + that->actual_content_rect.x, + that->actual_content_rect.y, + that->actual_content_rect.width, + that->actual_content_rect.height); DEBUG_MSG("child canvas rect: (%d, %d, %d, %d)\n", style.canvas_box.x, style.canvas_box.y, style.canvas_box.width, style.canvas_box.height); - if (!LCUIRect_GetOverlayRect(&that->content_rect, + if (!LCUIRect_GetOverlayRect(&that->actual_content_rect, &style.canvas_box, &paint_rect)) { - if (count > 0) { - ++count; - } continue; } ++count; @@ -545,7 +571,7 @@ static size_t WidgetRenderer_RenderContent(LCUI_WidgetRenderer that) * to this widget canvas */ style.x = that->style->x - that->style->canvas_box.x; style.y = that->style->y - that->style->canvas_box.y; - paint.rect = that->content_rect; + paint.rect = that->actual_content_rect; paint.rect.x -= that->style->canvas_box.x; paint.rect.y -= that->style->canvas_box.y; Widget_ComputeActualBorderBox(that->target, &style); @@ -623,15 +649,18 @@ static size_t WidgetRenderer_Render(LCUI_WidgetRenderer renderer) */ if (that->can_render_self) { Graph_Copy(&that->layer_graph, &that->self_graph); - Graph_Mix(&that->layer_graph, &that->content_graph, - that->content_rect.x - that->paint_rect.x, - that->content_rect.y - that->paint_rect.y, TRUE); + Graph_Mix( + &that->layer_graph, &that->content_graph, + that->actual_content_rect.x - that->actual_paint_rect.x, + that->actual_content_rect.y - that->actual_paint_rect.y, + TRUE); } else { Graph_Create(&that->layer_graph, that->paint->rect.width, that->paint->rect.height); - Graph_Replace(&that->layer_graph, &that->content_graph, - that->content_rect.x - that->paint_rect.x, - that->content_rect.y - that->paint_rect.y); + Graph_Replace( + &that->layer_graph, &that->content_graph, + that->actual_content_rect.x - that->actual_paint_rect.x, + that->actual_content_rect.y - that->actual_paint_rect.y); } that->layer_graph.opacity = that->target->computed_style.opacity; Graph_Mix(&that->paint->canvas, &that->layer_graph, 0, 0, diff --git a/src/util/math.c b/src/util/math.c index a5550b0c4..eae57e00d 100644 --- a/src/util/math.c +++ b/src/util/math.c @@ -26,6 +26,3 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - -#include -#include