From 9a71f23ddd4296931a4aa4a7c254c8da4bd4d10b Mon Sep 17 00:00:00 2001 From: cnjinhao Date: Fri, 9 Jan 2015 08:12:20 +0800 Subject: [PATCH] Performance improvements --- include/nana/gui/detail/drawer.hpp | 10 +++--- .../nana/gui/detail/inner_fwd_implement.hpp | 2 +- include/nana/paint/graphics.hpp | 8 ++--- source/gui/detail/drawer.cpp | 6 ++-- source/gui/detail/window_layout.cpp | 2 +- source/gui/detail/window_manager.cpp | 12 +++---- source/gui/element.cpp | 2 +- source/gui/programming_interface.cpp | 2 +- source/gui/widgets/date_chooser.cpp | 6 ++-- source/gui/widgets/label.cpp | 2 +- source/gui/widgets/listbox.cpp | 2 +- source/gui/widgets/skeletons/text_editor.cpp | 10 +++--- source/gui/widgets/toolbar.cpp | 2 +- source/gui/widgets/treebox.cpp | 2 +- source/paint/graphics.cpp | 32 +++++++------------ source/paint/text_renderer.cpp | 2 +- 16 files changed, 46 insertions(+), 56 deletions(-) diff --git a/include/nana/gui/detail/drawer.hpp b/include/nana/gui/detail/drawer.hpp index 8cb5465a..7b11fc37 100644 --- a/include/nana/gui/detail/drawer.hpp +++ b/include/nana/gui/detail/drawer.hpp @@ -123,7 +123,7 @@ namespace nana void _m_bground_pre(); void _m_bground_end(); void _m_draw_dynamic_drawing_object(); - void _m_use_refresh(); + bool _m_lazy_decleared() const; template void _m_emit(event_code evt_code, const Arg& arg, Mfptr mfptr) @@ -144,9 +144,11 @@ namespace nana else (realizer_->*mfptr)(graphics, arg); - _m_use_refresh(); - _m_draw_dynamic_drawing_object(); - _m_bground_end(); + if (_m_lazy_decleared()) + { + _m_draw_dynamic_drawing_object(); + _m_bground_end(); + } } } } diff --git a/include/nana/gui/detail/inner_fwd_implement.hpp b/include/nana/gui/detail/inner_fwd_implement.hpp index 31b59733..9e782092 100644 --- a/include/nana/gui/detail/inner_fwd_implement.hpp +++ b/include/nana/gui/detail/inner_fwd_implement.hpp @@ -124,7 +124,7 @@ namespace nana{ root_misc(core_window_t * wd, unsigned width, unsigned height) : window(wd), - root_graph(width, height) + root_graph({ width, height }) {} };//end struct root_misc diff --git a/include/nana/paint/graphics.hpp b/include/nana/paint/graphics.hpp index 9829b826..b14a4cb7 100644 --- a/include/nana/paint/graphics.hpp +++ b/include/nana/paint/graphics.hpp @@ -74,7 +74,6 @@ namespace nana typedef ::nana::native_window_type native_window_type; graphics(); - graphics(unsigned width, unsigned height); ///< size in pixel graphics(const ::nana::size&); ///< size in pixel graphics(const graphics&); ///< the resource is not copyed, the two graphics objects refer to the *SAME* resource graphics& operator=(const graphics&); @@ -85,9 +84,10 @@ namespace nana drawable_type handle() const; const void* pixmap() const; const void* context() const; - void make(unsigned width, unsigned height); ///< Creates a bitmap resource that size is width by height in pixel - void resize(unsigned width, unsigned height); - void typeface(const font&); ///< Selects a specified font type into the graphics object. + + void make(const ::nana::size&); ///< Creates a bitmap resource that size is width by height in pixel + void resize(const ::nana::size&); + void typeface(const font&); ///< Selects a specified font type into the graphics object. font typeface() const; ::nana::size text_extent_size(const char_t*) const; ///< Computes the width and height of the specified string of text. ::nana::size text_extent_size(const string&) const; ///< Computes the width and height of the specified string of text. diff --git a/source/gui/detail/drawer.cpp b/source/gui/detail/drawer.cpp index d62f9921..63514af0 100644 --- a/source/gui/detail/drawer.cpp +++ b/source/gui/detail/drawer.cpp @@ -378,11 +378,9 @@ namespace nana dw->draw(graphics); } - //If the drawer_trigger didn't declear a lazy refresh, then use the refresh(). - void drawer::_m_use_refresh() + bool drawer::_m_lazy_decleared() const { - if (basic_window::update_state::refresh != core_window_->other.upd_state) - refresh(); + return (basic_window::update_state::refresh != core_window_->other.upd_state); } }//end namespace detail }//end namespace nana diff --git a/source/gui/detail/window_layout.cpp b/source/gui/detail/window_layout.cpp index 6e80adb3..6c52bebb 100644 --- a/source/gui/detail/window_layout.cpp +++ b/source/gui/detail/window_layout.cpp @@ -183,7 +183,7 @@ namespace nana //Enable the effect. data_sect.effects_bground_windows.push_back(wd); - wd->other.glass_buffer.make(wd->dimension.width, wd->dimension.height); + wd->other.glass_buffer.make(wd->dimension); make_bground(wd); return true; } diff --git a/source/gui/detail/window_manager.cpp b/source/gui/detail/window_manager.cpp index 583ab85f..ea956b24 100644 --- a/source/gui/detail/window_manager.cpp +++ b/source/gui/detail/window_manager.cpp @@ -546,8 +546,8 @@ namespace detail { wd->dimension.width = r.width; wd->dimension.height = r.height; - wd->drawer.graphics.make(r.width, r.height); - wd->root_graph->make(r.width, r.height); + wd->drawer.graphics.make(wd->dimension); + wd->root_graph->make(wd->dimension); native_interface::move_window(wd->root, r); arg_resized arg; @@ -612,7 +612,7 @@ namespace detail if(category::lite_widget_tag::value != wd->other.category) { bool graph_state = wd->drawer.graphics.empty(); - wd->drawer.graphics.make(sz.width, sz.height); + wd->drawer.graphics.make(sz); //It shall make a typeface_changed() call when the graphics state is changing. //Because when a widget is created with zero-size, it may get some wrong result in typeface_changed() call @@ -622,7 +622,7 @@ namespace detail if(category::root_tag::value == wd->other.category) { - wd->root_graph->make(sz.width, sz.height); + wd->root_graph->make(sz); if(false == passive) native_interface::window_size(wd->root, sz + nana::size(wd->extra_width, wd->extra_height)); } @@ -637,7 +637,7 @@ namespace detail //update the bground buffer of glass window. if(wd->effect.bground && wd->parent) { - wd->other.glass_buffer.make(sz.width, sz.height); + wd->other.glass_buffer.make(sz); wndlayout_type::make_bground(wd); } } @@ -767,7 +767,7 @@ namespace detail if (!impl_->wd_register.available(wd)) return false; - result.make(wd->drawer.graphics.width(), wd->drawer.graphics.height()); + result.make(wd->drawer.graphics.size()); result.bitblt(0, 0, wd->drawer.graphics); wndlayout_type::paste_children_to_graphics(wd, result); return true; diff --git a/source/gui/element.cpp b/source/gui/element.cpp index 6421d85c..42c0756a 100644 --- a/source/gui/element.cpp +++ b/source/gui/element.cpp @@ -547,7 +547,7 @@ namespace nana draw_method * clone() const override { auto p = new draw_graph; - p->graph.make(graph.width(), graph.height()); + p->graph.make(graph.size()); graph.paste(p->graph, 0, 0); return p; } diff --git a/source/gui/programming_interface.cpp b/source/gui/programming_interface.cpp index 286aafc1..8979c61f 100644 --- a/source/gui/programming_interface.cpp +++ b/source/gui/programming_interface.cpp @@ -172,7 +172,7 @@ namespace API internal_scope_guard isg; if(restrict::window_manager.available(iwd)) { - iwd->drawer.graphics.make(iwd->dimension.width, iwd->dimension.height); + iwd->drawer.graphics.make(iwd->dimension); iwd->drawer.graphics.rectangle(true, iwd->expr_colors->background.get_color()); iwd->drawer.attached(wd, dr); iwd->drawer.refresh(); //Always redrawe no matter it is visible or invisible. This can make the graphics data correctly. diff --git a/source/gui/widgets/date_chooser.cpp b/source/gui/widgets/date_chooser.cpp index 6fc0c8ea..3aff4923 100644 --- a/source/gui/widgets/date_chooser.cpp +++ b/source/gui/widgets/date_chooser.cpp @@ -102,7 +102,7 @@ namespace nana { nana::point refpos(1, static_cast(topbar_height) + 1); - nana::paint::graphics gbuf(width, graph.height() - 2 - topbar_height); + nana::paint::graphics gbuf({ width, graph.height() - 2 - topbar_height }); gbuf.rectangle(true, {0xf0, 0xf0, 0xf0}); switch(page_) @@ -599,12 +599,12 @@ namespace nana nana::point refpos(1, static_cast(topbar_height) + 1); nana::rectangle r(0, 0, graph.width() - 2, graph.height() - 2 - topbar_height); - nana::paint::graphics dirtybuf(r.width, r.height); + nana::paint::graphics dirtybuf({ r.width, r.height }); dirtybuf.bitblt(r, graph, refpos); _m_draw(graph); - nana::paint::graphics gbuf(r.width, r.height); + nana::paint::graphics gbuf({ r.width, r.height }); gbuf.bitblt(r, graph, refpos); _m_perf_transform(tfid, graph, dirtybuf, gbuf, refpos); diff --git a/source/gui/widgets/label.cpp b/source/gui/widgets/label.cpp index 6463a3e6..e6969835 100644 --- a/source/gui/widgets/label.cpp +++ b/source/gui/widgets/label.cpp @@ -831,7 +831,7 @@ namespace nana if(graph_ptr->empty()) { graph_ptr = &substitute; - graph_ptr->make(10, 10); + graph_ptr->make({ 10, 10 }); } return impl->renderer.measure(*graph_ptr, limited, impl->text_align, impl->text_align_v); diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index 30799cb5..828d4be2 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -2317,7 +2317,7 @@ namespace nana { const auto & item = essence_->header.column(essence_->pointer_where.second); - nana::paint::graphics ext_graph(item.pixels, essence_->header_size); + nana::paint::graphics ext_graph({ item.pixels, essence_->header_size }); ext_graph.typeface(essence_->graph->typeface()); int txtop = (essence_->header_size - essence_->text_height) / 2; diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index ad788001..786698d3 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -1473,7 +1473,7 @@ namespace nana{ namespace widgets text_area_.area = r; if(attributes_.enable_counterpart) - attributes_.counterpart.make(r.width, r.height); + attributes_.counterpart.make({ r.width, r.height }); behavior_->pre_calc_lines(width_pixels()); _m_scrollbar(); @@ -1529,7 +1529,7 @@ namespace nana{ namespace widgets { attributes_.enable_counterpart = enb; if(enb) - attributes_.counterpart.make(text_area_.area.width, text_area_.area.height); + attributes_.counterpart.make({ text_area_.area.width, text_area_.area.height }); else attributes_.counterpart.release(); } @@ -2716,7 +2716,7 @@ namespace nana{ namespace widgets auto px_w = std::accumulate(glyphs, glyphs + len, unsigned{}); ::nana::paint::graphics canvas; - canvas.make(px_w, px_h); + canvas.make({ px_w, px_h }); canvas.typeface(graph_.typeface()); ::nana::point canvas_text_pos; @@ -2832,7 +2832,7 @@ namespace nana{ namespace widgets this->_m_draw_parse_string(parser, true, strpos, clr, str, len); //Draw selected part - paint::graphics graph(glyph_selected, line_h_pixels); + paint::graphics graph({ glyph_selected, line_h_pixels }); graph.typeface(this->graph_.typeface()); graph.rectangle(true, scheme_->selection.get_color()); @@ -2920,8 +2920,6 @@ namespace nana{ namespace widgets graph_.set_text_color(scheme_->selection_text.get_color()); graph_.string(text_pos, ent.begin, endpos - pos); - //graph_.set_text_color(clr); //deprecated - //graph_.string(text_pos + ::nana::point(static_cast(sel_w), 0), ent.begin + (endpos - pos), str_end - endpos); _m_draw_parse_string(parser, false, text_pos + ::nana::point(static_cast(sel_w), 0), clr, ent.begin + (endpos - pos), str_end - endpos); } } diff --git a/source/gui/widgets/toolbar.cpp b/source/gui/widgets/toolbar.cpp index 33fd5df3..1f0706e7 100644 --- a/source/gui/widgets/toolbar.cpp +++ b/source/gui/widgets/toolbar.cpp @@ -172,7 +172,7 @@ namespace nana item.image.paste(size, graph, pos); if(item.enable == false) { - nana::paint::graphics gh(size.width, size.height); + nana::paint::graphics gh(size); gh.bitblt(size, graph, pos); gh.rgb_to_wb(); gh.paste(graph, pos.x, pos.y); diff --git a/source/gui/widgets/treebox.cpp b/source/gui/widgets/treebox.cpp index 1282847d..f0dee8fb 100644 --- a/source/gui/widgets/treebox.cpp +++ b/source/gui/widgets/treebox.cpp @@ -1391,7 +1391,7 @@ namespace nana comp_attribute_t attr; if(comp_attribute(component::text, attr)) { - nana::paint::graphics item_graph(item_r_.width, item_r_.height); + nana::paint::graphics item_graph({ item_r_.width, item_r_.height }); item_graph.typeface(graph_->typeface()); renderer_->set_color(widget_->bgcolor(), widget_->fgcolor()); diff --git a/source/paint/graphics.cpp b/source/paint/graphics.cpp index 48992dc0..da30c08f 100644 --- a/source/paint/graphics.cpp +++ b/source/paint/graphics.cpp @@ -198,16 +198,10 @@ namespace paint :handle_(nullptr), changed_(false) {} - graphics::graphics(unsigned width, unsigned height) - :handle_(nullptr), changed_(true) - { - make(width, height); - } - graphics::graphics(const nana::size& sz) :handle_(nullptr), changed_(true) { - make(sz.width, sz.height); + make(sz); } graphics::graphics(const graphics& rhs) @@ -255,9 +249,9 @@ namespace paint return (handle_? handle_->context : nullptr); } - void graphics::make(unsigned width, unsigned height) + void graphics::make(const ::nana::size& sz) { - if(handle_ == nullptr || size_ != nana::size(width, height)) + if(handle_ == nullptr || size_ != sz) { //The object will be delete while dwptr_ is performing a release. drawable_type dw = new nana::detail::drawable_impl_type; @@ -277,12 +271,12 @@ namespace paint BITMAPINFO bmi; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bmi.bmiHeader.biWidth = width; - bmi.bmiHeader.biHeight = -static_cast(height); + bmi.bmiHeader.biWidth = sz.width; + bmi.bmiHeader.biHeight = -static_cast(sz.height); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; // four 8-bit components bmi.bmiHeader.biCompression = BI_RGB; - bmi.bmiHeader.biSizeImage = (width * height) << 2; + bmi.bmiHeader.biSizeImage = (sz.width * sz.height) << 2; HBITMAP bmp = ::CreateDIBSection(cdc, &bmi, DIB_RGB_COLORS, reinterpret_cast(&(dw->pixbuf_ptr)), 0, 0); @@ -310,7 +304,7 @@ namespace paint Display* disp = spec.open_display(); int screen = DefaultScreen(disp); Window root = ::XRootWindow(disp, screen); - dw->pixmap = ::XCreatePixmap(disp, root, (width ? width : 1), (height ? height : 1), DefaultDepth(disp, screen)); + dw->pixmap = ::XCreatePixmap(disp, root, (sz.width ? sz.width : 1), (sz.height ? sz.height : 1), DefaultDepth(disp, screen)); dw->context = ::XCreateGC(disp, dw->pixmap, 0, 0); #if defined(NANA_UNICODE) dw->xftdraw = ::XftDrawCreate(disp, dw->pixmap, spec.screen_visual(), spec.colormap()); @@ -318,18 +312,16 @@ namespace paint #endif if(dw) { - //dw->fgcolor(0); dw->set_color(colors::black); dw->set_text_color(colors::black); #if defined(NANA_WINDOWS) - dw->bytes_per_line = width * sizeof(pixel_argb_t); + dw->bytes_per_line = sz.width * sizeof(pixel_argb_t); #else dw->update_text_color(); #endif - dwptr_ = std::shared_ptr(dw, detail::drawable_deleter()); + dwptr_.reset(dw, detail::drawable_deleter{}); handle_ = dw; - size_.width = width; - size_.height = height; + size_ = sz; handle_->string.tab_pixels = detail::raw_text_extent_size(handle_, STR("\t"), 1).width; handle_->string.whitespace_pixels = detail::raw_text_extent_size(handle_, STR(" "), 1).width; @@ -339,10 +331,10 @@ namespace paint if(changed_ == false) changed_ = true; } - void graphics::resize(unsigned width, unsigned height) + void graphics::resize(const ::nana::size& sz) { graphics duplicate(*this); - make(width, height); + make(sz); bitblt(0, 0, duplicate); } diff --git a/source/paint/text_renderer.cpp b/source/paint/text_renderer.cpp index cc1e3c27..9fbcf8f6 100644 --- a/source/paint/text_renderer.cpp +++ b/source/paint/text_renderer.cpp @@ -166,7 +166,7 @@ namespace nana r.width = endpos - pos.x; r.height = ts.height; - nana::paint::graphics dum_graph(r.width, r.height); + nana::paint::graphics dum_graph({ r.width, r.height }); dum_graph.bitblt(r, graph, pos); dum_graph.set_text_color(fgcolor);