Merge lp:~3v1n0/unity/massif-textures-sharing into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Brandon Schaefer
Approved revision: no longer in the source branch.
Merged at revision: 3289
Proposed branch: lp:~3v1n0/unity/massif-textures-sharing
Merge into: lp:unity
Diff against target: 672 lines (+133/-150)
12 files modified
launcher/Launcher.cpp (+2/-6)
panel/PanelView.cpp (+21/-24)
panel/PanelView.h (+1/-1)
plugins/unityshell/src/unityshell.cpp (+32/-53)
plugins/unityshell/src/unityshell.h (+2/-6)
tests/test_texture_cache.cpp (+3/-3)
unity-shared/DashStyle.cpp (+3/-13)
unity-shared/PanelStyle.cpp (+41/-19)
unity-shared/PanelStyle.h (+2/-1)
unity-shared/TextureCache.cpp (+17/-18)
unity-shared/TextureCache.h (+6/-4)
unity-shared/WindowButtons.cpp (+3/-2)
To merge this branch: bzr merge lp:~3v1n0/unity/massif-textures-sharing
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Brandon Schaefer (community) Approve
Review via email: mp+157774@code.launchpad.net

Commit message

Unity: share textures whenever possible, optimize memory usage.

And some code improvements in TextureCache, PanelStyle, PanelView and unityshell

Description of the change

I've run unity under valgrind's massif, and I've noticed (as expected) that the most of the unity memory is used by textures. However, massif also underlined that we were allocating some resources many times (especially when in multi-monitor).

So, I've moved many components to use TextureCache, including PanelView, DashStyle, WindowButtons.

Improved a little the code of TextureCache that now has a default factory function to fetch textures from the default unity location.

Improvements in the PanelView background texture (that was used also by unityshell to paint the "fake" panel under the dash, in case of maximized windows): before we were generating real-size textures (and in case of unityshell big as the entire screen, a problem especially in case of multi-monitor), now we only generate 1x24 px texture that is repeated when drawing.

A not-scientific comparison of massif: http://ubuntuone.com/3NFLnQxLhIuBYziH0GKUfJ
In that tests I've ran unity for 5 minutes in my machine performing some basic unity operations using AP tests. It underlines ~5MB saving, it won't change the world but it's still a win! :)

To post a comment you must log in.
Revision history for this message
Brandon Schaefer (brandontschaefer) wrote :

494 glib::Object<GtkStyleContext> _style_context;
495 glib::Object<GSettings> _gsettings;
496 + BaseTexturePtr _bg_texture;
497 std::string _theme_name;
498 nux::Color _text_color;

Someday the naming of these variables will have to be fixed :)

Revision history for this message
Brandon Schaefer (brandontschaefer) wrote :

Looks good to me, tests pass and I don't see any black textures anywhere :)

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'launcher/Launcher.cpp'
--- launcher/Launcher.cpp 2013-04-05 19:20:49 +0000
+++ launcher/Launcher.cpp 2013-04-09 01:09:21 +0000
@@ -198,12 +198,8 @@
198 icon_renderer->SetTargetSize(_icon_size, _icon_image_size, _space_between_icons);198 icon_renderer->SetTargetSize(_icon_size, _icon_image_size, _space_between_icons);
199199
200 TextureCache& cache = TextureCache::GetDefault();200 TextureCache& cache = TextureCache::GetDefault();
201 TextureCache::CreateTextureCallback cb = [&](std::string const& name, int width, int height) {201 launcher_sheen_ = cache.FindTexture("dash_sheen.png");
202 return nux::CreateTexture2DFromFile((PKGDATADIR"/" + name + ".png").c_str(), -1, true);202 launcher_pressure_effect_ = cache.FindTexture("launcher_pressure_effect.png");
203 };
204
205 launcher_sheen_ = cache.FindTexture("dash_sheen", 0, 0, cb);
206 launcher_pressure_effect_ = cache.FindTexture("launcher_pressure_effect", 0, 0, cb);
207203
208 options.changed.connect(sigc::mem_fun(this, &Launcher::OnOptionsChanged));204 options.changed.connect(sigc::mem_fun(this, &Launcher::OnOptionsChanged));
209 monitor.changed.connect(sigc::mem_fun(this, &Launcher::OnMonitorChanged));205 monitor.changed.connect(sigc::mem_fun(this, &Launcher::OnMonitorChanged));
210206
=== modified file 'panel/PanelView.cpp'
--- panel/PanelView.cpp 2013-03-19 18:47:01 +0000
+++ panel/PanelView.cpp 2013-04-09 01:09:21 +0000
@@ -25,7 +25,6 @@
25#include <Nux/WindowCompositor.h>25#include <Nux/WindowCompositor.h>
2626
27#include <NuxGraphics/CairoGraphics.h>27#include <NuxGraphics/CairoGraphics.h>
28#include <NuxGraphics/ImageSurface.h>
29#include <NuxCore/Logger.h>28#include <NuxCore/Logger.h>
30#include <UnityCore/GLibWrapper.h>29#include <UnityCore/GLibWrapper.h>
3130
@@ -35,6 +34,7 @@
35#include <glib.h>34#include <glib.h>
3635
37#include "unity-shared/PanelStyle.h"36#include "unity-shared/PanelStyle.h"
37#include "unity-shared/TextureCache.h"
38#include "unity-shared/WindowManager.h"38#include "unity-shared/WindowManager.h"
39#include "unity-shared/UBusMessages.h"39#include "unity-shared/UBusMessages.h"
40#include <UnityCore/Variant.h>40#include <UnityCore/Variant.h>
@@ -128,9 +128,10 @@
128 bg_effect_helper_.owner = this;128 bg_effect_helper_.owner = this;
129129
130 //FIXME (gord)- replace with async loading130 //FIXME (gord)- replace with async loading
131 panel_sheen_.Adopt(nux::CreateTexture2DFromFile(PKGDATADIR"/dash_sheen.png", -1, true));131 TextureCache& cache = TextureCache::GetDefault();
132 bg_refine_tex_.Adopt(nux::CreateTexture2DFromFile(PKGDATADIR"/refine_gradient_panel.png", -1, true));132 panel_sheen_ = cache.FindTexture("dash_sheen.png");
133 bg_refine_single_column_tex_.Adopt(nux::CreateTexture2DFromFile(PKGDATADIR"/refine_gradient_panel_single_column.png", -1, true));133 bg_refine_tex_ = cache.FindTexture("refine_gradient_panel.png");
134 bg_refine_single_column_tex_ = cache.FindTexture("refine_gradient_panel_single_column.png");
134135
135 rop.Blend = true;136 rop.Blend = true;
136 rop.SrcBlend = GL_ONE;137 rop.SrcBlend = GL_ONE;
@@ -188,7 +189,8 @@
188 bg_color_.blue = blue;189 bg_color_.blue = blue;
189 bg_color_.alpha = alpha;190 bg_color_.alpha = alpha;
190191
191 ForceUpdateBackground();192 if (overlay_is_open_)
193 ForceUpdateBackground();
192}194}
193195
194void PanelView::OnOverlayHidden(GVariant* data)196void PanelView::OnOverlayHidden(GVariant* data)
@@ -267,7 +269,7 @@
267269
268 GfxContext.PushClippingRectangle(geo);270 GfxContext.PushClippingRectangle(geo);
269271
270 if ((overlay_is_open_ || (opacity_ != 1.0f && opacity_ != 0.0f)))272 if (IsTransparent())
271 {273 {
272 nux::Geometry const& geo_absolute = GetAbsoluteGeometry();274 nux::Geometry const& geo_absolute = GetAbsoluteGeometry();
273 nux::Geometry blur_geo(geo_absolute.x, geo_absolute.y, geo.width, geo.height);275 nux::Geometry blur_geo(geo_absolute.x, geo_absolute.y, geo.width, geo.height);
@@ -281,7 +283,7 @@
281 bg_blur_texture_ = bg_effect_helper_.GetRegion(blur_geo);283 bg_blur_texture_ = bg_effect_helper_.GetRegion(blur_geo);
282 }284 }
283285
284 if (bg_blur_texture_.IsValid() && (overlay_is_open_ || opacity_ != 1.0f))286 if (bg_blur_texture_.IsValid())
285 {287 {
286 nux::TexCoordXForm texxform_blur_bg;288 nux::TexCoordXForm texxform_blur_bg;
287 texxform_blur_bg.flip_v_coord = true;289 texxform_blur_bg.flip_v_coord = true;
@@ -371,8 +373,7 @@
371 GfxContext.GetRenderStates().SetBlend(true);373 GfxContext.GetRenderStates().SetBlend(true);
372 GfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);374 GfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
373375
374 if (bg_blur_texture_.IsValid() &&376 if (bg_blur_texture_.IsValid() && IsTransparent())
375 (overlay_is_open_ || (opacity_ != 1.0f && opacity_ != 0.0f)))
376 {377 {
377 nux::Geometry const& geo_absolute = GetAbsoluteGeometry();378 nux::Geometry const& geo_absolute = GetAbsoluteGeometry();
378 nux::TexCoordXForm texxform_blur_bg;379 nux::TexCoordXForm texxform_blur_bg;
@@ -482,12 +483,9 @@
482void483void
483PanelView::UpdateBackground()484PanelView::UpdateBackground()
484{485{
485 nux::Geometry const& geo = GetGeometry();486 if (!is_dirty_)
486
487 if (!is_dirty_ && geo == last_geo_)
488 return;487 return;
489488
490 last_geo_ = geo;
491 is_dirty_ = false;489 is_dirty_ = false;
492490
493 nux::ROPConfig rop;491 nux::ROPConfig rop;
@@ -512,13 +510,13 @@
512 opacity = 1.0f;510 opacity = 1.0f;
513 }511 }
514512
515 auto tex = panel::Style::Instance().GetBackground(geo.width, geo.height, opacity);513 auto const& tex = panel::Style::Instance().GetBackground();
516 nux::TexCoordXForm texxform;514 nux::TexCoordXForm texxform;
517 texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);515 texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
518 texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);516 texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_CLAMP);
519517
520 bg_layer_.reset(new nux::TextureLayer(tex->GetDeviceTexture(), texxform,518 bg_layer_.reset(new nux::TextureLayer(tex->GetDeviceTexture(), texxform,
521 nux::color::White, true, rop));519 nux::color::White * opacity, true, rop));
522 }520 }
523}521}
524522
@@ -527,12 +525,6 @@
527 is_dirty_ = true;525 is_dirty_ = true;
528 UpdateBackground();526 UpdateBackground();
529527
530 indicators_->QueueDraw();
531 tray_->QueueDraw();
532
533 if (!overlay_is_open_)
534 menu_view_->QueueDraw();
535
536 QueueDraw();528 QueueDraw();
537}529}
538530
@@ -703,12 +695,17 @@
703 if (opacity_ == opacity)695 if (opacity_ == opacity)
704 return;696 return;
705697
706 opacity_ = opacity;698 opacity_ = (opacity <= 0.0f ? 0.0001f : opacity); // Not to get a black menu area
707 bg_effect_helper_.enabled = (opacity_ < 1.0f || overlay_is_open_);699 bg_effect_helper_.enabled = IsTransparent();
708700
709 ForceUpdateBackground();701 ForceUpdateBackground();
710}702}
711703
704bool PanelView::IsTransparent()
705{
706 return (opacity_ < 1.0f || overlay_is_open_);
707}
708
712void PanelView::SetMenuShowTimings(int fadein, int fadeout, int discovery,709void PanelView::SetMenuShowTimings(int fadein, int fadeout, int discovery,
713 int discovery_fadein, int discovery_fadeout)710 int discovery_fadein, int discovery_fadeout)
714{711{
715712
=== modified file 'panel/PanelView.h'
--- panel/PanelView.h 2013-03-04 23:56:40 +0000
+++ panel/PanelView.h 2013-04-09 01:09:21 +0000
@@ -91,6 +91,7 @@
91 void OnOverlayShown(GVariant *data);91 void OnOverlayShown(GVariant *data);
92 void OnOverlayHidden(GVariant *data);92 void OnOverlayHidden(GVariant *data);
9393
94 bool IsTransparent();
94 void UpdateBackground();95 void UpdateBackground();
95 void ForceUpdateBackground();96 void ForceUpdateBackground();
96 bool TrackMenuPointer();97 bool TrackMenuPointer();
@@ -116,7 +117,6 @@
116 BaseTexturePtr bg_refine_single_column_tex_;117 BaseTexturePtr bg_refine_single_column_tex_;
117 std::unique_ptr<nux::AbstractPaintLayer> bg_refine_single_column_layer_;118 std::unique_ptr<nux::AbstractPaintLayer> bg_refine_single_column_layer_;
118119
119 nux::Geometry last_geo_;
120 nux::Color bg_color_;120 nux::Color bg_color_;
121 std::string active_overlay_;121 std::string active_overlay_;
122 nux::Point tracked_pointer_pos_;122 nux::Point tracked_pointer_pos_;
123123
=== modified file 'plugins/unityshell/src/unityshell.cpp'
--- plugins/unityshell/src/unityshell.cpp 2013-03-29 13:59:11 +0000
+++ plugins/unityshell/src/unityshell.cpp 2013-04-09 01:09:21 +0000
@@ -146,8 +146,7 @@
146 , last_scroll_event_(0)146 , last_scroll_event_(0)
147 , hud_keypress_time_(0)147 , hud_keypress_time_(0)
148 , first_menu_keypress_time_(0)148 , first_menu_keypress_time_(0)
149 , panel_texture_has_changed_(true)149 , paint_panel_under_dash_(false)
150 , paint_panel_(false)
151 , scale_just_activated_(false)150 , scale_just_activated_(false)
152 , big_tick_(0)151 , big_tick_(0)
153 , screen_introspection_(screen)152 , screen_introspection_(screen)
@@ -705,8 +704,6 @@
705704
706void UnityScreen::OnPanelStyleChanged()705void UnityScreen::OnPanelStyleChanged()
707{706{
708 panel_texture_has_changed_ = true;
709
710 // Reload the windows themed textures707 // Reload the windows themed textures
711 UnityWindow::CleanupSharedTextures();708 UnityWindow::CleanupSharedTextures();
712709
@@ -723,7 +720,7 @@
723{720{
724 CompOutput *output = _last_output;721 CompOutput *output = _last_output;
725722
726 DrawTopPanelBackground();723 DrawPanelUnderDash();
727724
728 auto gpu_device = nux::GetGraphicsDisplay()->GetGpuDevice();725 auto gpu_device = nux::GetGraphicsDisplay()->GetGpuDevice();
729726
@@ -809,45 +806,29 @@
809 didShellRepaint = true;806 didShellRepaint = true;
810}807}
811808
812void UnityScreen::DrawTopPanelBackground()809void UnityScreen::DrawPanelUnderDash()
813{810{
811 if (!paint_panel_under_dash_ || !launcher_controller_->IsOverlayOpen())
812 return;
813
814 if (_last_output->id() != screen->currentOutputDev().id())
815 return;
816
814 auto graphics_engine = nux::GetGraphicsDisplay()->GetGraphicsEngine();817 auto graphics_engine = nux::GetGraphicsDisplay()->GetGraphicsEngine();
815818
816 if (!graphics_engine->UsingGLSLCodePath() || !launcher_controller_->IsOverlayOpen() || !paint_panel_)819 if (!graphics_engine->UsingGLSLCodePath())
817 return;820 return;
818821
819 if (TopPanelBackgroundTextureNeedsUpdate())822 graphics_engine->ResetModelViewMatrixStack();
820 UpdateTopPanelBackgroundTexture();823 graphics_engine->Push2DTranslationModelViewMatrix(0.0f, 0.0f, 0.0f);
821824 graphics_engine->ResetProjectionMatrix();
822 if (panel_texture_.IsValid())825 graphics_engine->SetOrthographicProjectionMatrix(screen->width(), screen->height());
823 {826
824 graphics_engine->ResetModelViewMatrixStack();827 nux::TexCoordXForm texxform;
825 graphics_engine->Push2DTranslationModelViewMatrix(0.0f, 0.0f, 0.0f);828 texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_CLAMP);
826 graphics_engine->ResetProjectionMatrix();829 int panel_height = panel_style_.panel_height;
827 graphics_engine->SetOrthographicProjectionMatrix(screen->width (), screen->height());830 auto const& texture = panel_style_.GetBackground()->GetDeviceTexture();
828831 graphics_engine->QRP_GLSL_1Tex(0, 0, screen->width(), panel_height, texture, texxform, nux::color::White);
829 nux::TexCoordXForm texxform;
830 int panel_height = panel_style_.panel_height;
831 graphics_engine->QRP_GLSL_1Tex(0, 0, screen->width (), panel_height, panel_texture_, texxform, nux::color::White);
832 }
833}
834
835bool UnityScreen::TopPanelBackgroundTextureNeedsUpdate() const
836{
837 return panel_texture_has_changed_ || !panel_texture_.IsValid();
838}
839
840void UnityScreen::UpdateTopPanelBackgroundTexture()
841{
842 auto &panel_style = panel::Style::Instance();
843
844 panel_texture_.Release();
845 auto texture = panel_style.GetBackground(screen->width(), screen->height(), 1.0f);
846
847 if (texture)
848 panel_texture_ = texture->GetDeviceTexture();
849
850 panel_texture_has_changed_ = false;
851}832}
852833
853bool UnityScreen::forcePaintOnTop ()834bool UnityScreen::forcePaintOnTop ()
@@ -1256,7 +1237,7 @@
12561237
1257 allowWindowPaint = true;1238 allowWindowPaint = true;
1258 _last_output = output;1239 _last_output = output;
1259 paint_panel_ = false;1240 paint_panel_under_dash_ = false;
12601241
1261 // CompRegion has no clear() method. So this is the fastest alternative.1242 // CompRegion has no clear() method. So this is the fastest alternative.
1262 fullscreenRegion = CompRegion();1243 fullscreenRegion = CompRegion();
@@ -2597,19 +2578,17 @@
2597 const CompRegion& region,2578 const CompRegion& region,
2598 unsigned int mask)2579 unsigned int mask)
2599{2580{
2600 if (uScreen->doShellRepaint && !uScreen->paint_panel_ && window->type() == CompWindowTypeNormalMask)2581 if (uScreen->doShellRepaint && !uScreen->paint_panel_under_dash_ && window->type() == CompWindowTypeNormalMask)
2601 {2582 {
2602 guint32 id = window->id();2583 if ((window->state() & MAXIMIZE_STATE) && window->onCurrentDesktop() && !window->overrideRedirect() && window->managed())
2603 bool maximized = WindowManager::Default().IsWindowMaximized(id);
2604 bool on_current = window->onCurrentDesktop();
2605 bool override_redirect = window->overrideRedirect();
2606 bool managed = window->managed();
2607 CompPoint viewport = window->defaultViewport();
2608 int output = window->outputDevice();
2609
2610 if (maximized && on_current && !override_redirect && managed && viewport == uScreen->screen->vp() && output == (int)uScreen->screen->currentOutputDev().id())
2611 {2584 {
2612 uScreen->paint_panel_ = true;2585 CompPoint const& viewport = window->defaultViewport();
2586 unsigned output = window->outputDevice();
2587
2588 if (viewport == uScreen->screen->vp() && output == uScreen->screen->currentOutputDev().id())
2589 {
2590 uScreen->paint_panel_under_dash_ = true;
2591 }
2613 }2592 }
2614 }2593 }
26152594
26162595
=== modified file 'plugins/unityshell/src/unityshell.h'
--- plugins/unityshell/src/unityshell.h 2013-03-11 17:46:46 +0000
+++ plugins/unityshell/src/unityshell.h 2013-04-09 01:09:21 +0000
@@ -243,9 +243,7 @@
243243
244 void InitGesturesSupport();244 void InitGesturesSupport();
245245
246 void DrawTopPanelBackground();246 void DrawPanelUnderDash();
247 bool TopPanelBackgroundTextureNeedsUpdate() const;
248 void UpdateTopPanelBackgroundTexture();
249247
250 unsigned CompizModifiersToNux(unsigned input) const;248 unsigned CompizModifiersToNux(unsigned input) const;
251 unsigned XModifiersToNux(unsigned input) const;249 unsigned XModifiersToNux(unsigned input) const;
@@ -331,9 +329,7 @@
331329
332 GLMatrix panel_shadow_matrix_;330 GLMatrix panel_shadow_matrix_;
333331
334 bool panel_texture_has_changed_;332 bool paint_panel_under_dash_;
335 bool paint_panel_;
336 nux::ObjectPtr<nux::IOpenGLBaseTexture> panel_texture_;
337 std::set<UnityWindow*> fake_decorated_windows_;333 std::set<UnityWindow*> fake_decorated_windows_;
338334
339 bool scale_just_activated_;335 bool scale_just_activated_;
340336
=== modified file 'tests/test_texture_cache.cpp'
--- tests/test_texture_cache.cpp 2011-09-02 02:24:56 +0000
+++ tests/test_texture_cache.cpp 2013-04-09 01:09:21 +0000
@@ -50,7 +50,7 @@
50 }50 }
51};51};
5252
53TEST(TestTextureCache, DISABLED_TestCallsCreateTextureCallback)53TEST(TestTextureCache, TestCallsCreateTextureCallback)
54{54{
55 // Another lambda issue. If the lambda takes a reference to any other55 // Another lambda issue. If the lambda takes a reference to any other
56 // variables, it seems incapable of assigning the function to the56 // variables, it seems incapable of assigning the function to the
@@ -80,7 +80,7 @@
80 }80 }
81};81};
8282
83TEST(TestTextureCache, DISABLED_TestCallbackOnlyCalledOnce)83TEST(TestTextureCache, TestCallbackOnlyCalledOnce)
84{84{
85 TextureCallbackCounter counter;85 TextureCallbackCounter counter;
86 TextureCache::CreateTextureCallback callback(sigc::mem_fun(counter, &TextureCallbackCounter::callback));86 TextureCache::CreateTextureCallback callback(sigc::mem_fun(counter, &TextureCallbackCounter::callback));
@@ -95,7 +95,7 @@
95 EXPECT_TRUE(t1 == t2);95 EXPECT_TRUE(t1 == t2);
96}96}
9797
98TEST(TestTextureCache, DISABLED_TestCacheRemovesDeletedObject)98TEST(TestTextureCache, TestCacheRemovesDeletedObject)
99{99{
100 // Note for others, if just using the lambda function, the return value is100 // Note for others, if just using the lambda function, the return value is
101 // lost in the type deduction that sigc uses. So we have the typedef101 // lost in the type deduction that sigc uses. So we have the typedef
102102
=== modified file 'unity-shared/DashStyle.cpp'
--- unity-shared/DashStyle.cpp 2013-03-11 22:18:58 +0000
+++ unity-shared/DashStyle.cpp 2013-04-09 01:09:21 +0000
@@ -42,6 +42,7 @@
4242
43#include "CairoTexture.h"43#include "CairoTexture.h"
44#include "JSONParser.h"44#include "JSONParser.h"
45#include "TextureCache.h"
45#include "UnitySettings.h"46#include "UnitySettings.h"
46#include "config.h"47#include "config.h"
4748
@@ -2547,19 +2548,8 @@
25472548
2548void LazyLoadTexture::LoadTexture()2549void LazyLoadTexture::LoadTexture()
2549{2550{
2550 std::string full_path = PKGDATADIR + filename_;2551 TextureCache& cache = TextureCache::GetDefault();
2551 glib::Object<GdkPixbuf> pixbuf;2552 texture_ = cache.FindTexture(filename_, size_, size_);
2552 glib::Error error;
2553
2554 pixbuf = ::gdk_pixbuf_new_from_file_at_size(full_path.c_str(), size_, size_, &error);
2555 if (error)
2556 {
2557 LOG_WARN(logger) << "Unable to texture " << full_path << ": " << error;
2558 }
2559 else
2560 {
2561 texture_.Adopt(nux::CreateTexture2DFromPixbuf(pixbuf, true));
2562 }
2563}2553}
25642554
2565} // anon namespace2555} // anon namespace
25662556
=== modified file 'unity-shared/PanelStyle.cpp'
--- unity-shared/PanelStyle.cpp 2012-12-14 15:17:24 +0000
+++ unity-shared/PanelStyle.cpp 2013-04-09 01:09:21 +0000
@@ -34,6 +34,7 @@
34#include "PanelStyle.h"34#include "PanelStyle.h"
3535
36#include <UnityCore/GLibWrapper.h>36#include <UnityCore/GLibWrapper.h>
37#include "unity-shared/TextureCache.h"
37#include "unity-shared/UnitySettings.h"38#include "unity-shared/UnitySettings.h"
3839
39namespace unity40namespace unity
@@ -138,7 +139,7 @@
138 GdkRGBA rgba_text_color;139 GdkRGBA rgba_text_color;
139 glib::String theme_name;140 glib::String theme_name;
140 bool updated = false;141 bool updated = false;
141 142
142 GtkSettings* settings = gtk_settings_get_default();143 GtkSettings* settings = gtk_settings_get_default();
143 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);144 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
144145
@@ -159,7 +160,10 @@
159 }160 }
160161
161 if (updated)162 if (updated)
163 {
164 _bg_texture.Release();
162 changed.emit();165 changed.emit();
166 }
163}167}
164168
165GtkStyleContext* Style::GetStyleContext()169GtkStyleContext* Style::GetStyleContext()
@@ -167,8 +171,14 @@
167 return _style_context;171 return _style_context;
168}172}
169173
170BaseTexturePtr Style::GetBackground(int width, int height, float opacity)174BaseTexturePtr Style::GetBackground()
171{175{
176 if (_bg_texture)
177 return _bg_texture;
178
179 int width = 1;
180 int height = panel_height();
181
172 nux::CairoGraphics context(CAIRO_FORMAT_ARGB32, width, height);182 nux::CairoGraphics context(CAIRO_FORMAT_ARGB32, width, height);
173183
174 // Use the internal context as we know it is good and shiny new.184 // Use the internal context as we know it is good and shiny new.
@@ -177,9 +187,11 @@
177 gtk_render_background(_style_context, cr, 0, 0, width, height);187 gtk_render_background(_style_context, cr, 0, 0, width, height);
178 gtk_render_frame(_style_context, cr, 0, 0, width, height);188 gtk_render_frame(_style_context, cr, 0, 0, width, height);
179 cairo_pop_group_to_source(cr);189 cairo_pop_group_to_source(cr);
180 cairo_paint_with_alpha(cr, opacity);190 cairo_paint(cr);
181191
182 return texture_ptr_from_cairo_graphics(context);192 _bg_texture = texture_ptr_from_cairo_graphics(context);
193
194 return _bg_texture;
183}195}
184196
185/*!197/*!
@@ -222,20 +234,30 @@
222234
223BaseTexturePtr Style::GetWindowButton(WindowButtonType type, WindowState state)235BaseTexturePtr Style::GetWindowButton(WindowButtonType type, WindowState state)
224{236{
225 BaseTexturePtr texture;237 auto texture_factory = [this, type, state] (std::string const&, int, int) {
226238 nux::BaseTexture* texture = nullptr;
227 for (auto const& file : GetWindowButtonFileNames(type, state))239
228 {240 for (auto const& file : GetWindowButtonFileNames(type, state))
229 texture.Adopt(nux::CreateTexture2DFromFile(file.c_str(), -1, true));241 {
230242 texture = nux::CreateTexture2DFromFile(file.c_str(), -1, true);
231 if (texture)243
232 break;244 if (texture)
233 }245 return texture;
234246 }
235 if (!texture)247
236 texture = GetFallbackWindowButton(type, state);248 auto const& fallback = GetFallbackWindowButton(type, state);
237249 texture = fallback.GetPointer();
238 return texture;250 texture->Reference();
251
252 return texture;
253 };
254
255 auto& cache = TextureCache::GetDefault();
256 std::string texture_id = "window-button-";
257 texture_id += std::to_string(static_cast<int>(type));
258 texture_id += std::to_string(static_cast<int>(state));
259
260 return cache.FindTexture(texture_id, 0, 0, texture_factory);
239}261}
240262
241BaseTexturePtr Style::GetFallbackWindowButton(WindowButtonType type,263BaseTexturePtr Style::GetFallbackWindowButton(WindowButtonType type,
242264
=== modified file 'unity-shared/PanelStyle.h'
--- unity-shared/PanelStyle.h 2012-12-14 14:41:16 +0000
+++ unity-shared/PanelStyle.h 2013-04-09 01:09:21 +0000
@@ -71,7 +71,7 @@
71 typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;71 typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;
7272
73 GtkStyleContext* GetStyleContext();73 GtkStyleContext* GetStyleContext();
74 BaseTexturePtr GetBackground(int width, int height, float opacity);74 BaseTexturePtr GetBackground();
75 BaseTexturePtr GetWindowButton(WindowButtonType type, WindowState state);75 BaseTexturePtr GetWindowButton(WindowButtonType type, WindowState state);
76 BaseTexturePtr GetFallbackWindowButton(WindowButtonType type, WindowState state);76 BaseTexturePtr GetFallbackWindowButton(WindowButtonType type, WindowState state);
77 std::vector<std::string> GetWindowButtonFileNames(WindowButtonType type, WindowState state);77 std::vector<std::string> GetWindowButtonFileNames(WindowButtonType type, WindowState state);
@@ -88,6 +88,7 @@
8888
89 glib::Object<GtkStyleContext> _style_context;89 glib::Object<GtkStyleContext> _style_context;
90 glib::Object<GSettings> _gsettings;90 glib::Object<GSettings> _gsettings;
91 BaseTexturePtr _bg_texture;
91 std::string _theme_name;92 std::string _theme_name;
92 nux::Color _text_color;93 nux::Color _text_color;
9394
9495
=== modified file 'unity-shared/TextureCache.cpp'
--- unity-shared/TextureCache.cpp 2012-10-29 09:34:54 +0000
+++ unity-shared/TextureCache.cpp 2013-04-09 01:09:21 +0000
@@ -22,25 +22,23 @@
2222
23#include <sstream>23#include <sstream>
24#include <NuxCore/Logger.h>24#include <NuxCore/Logger.h>
25#include "config.h"
2526
26namespace unity27namespace unity
27{28{
28DECLARE_LOGGER(logger, "unity.internal.texturecache");29DECLARE_LOGGER(logger, "unity.internal.texturecache");
2930
30TextureCache::TextureCache()
31{
32}
33
34TextureCache::~TextureCache()
35{
36}
37
38TextureCache& TextureCache::GetDefault()31TextureCache& TextureCache::GetDefault()
39{32{
40 static TextureCache instance;33 static TextureCache instance;
41 return instance;34 return instance;
42}35}
4336
37nux::BaseTexture* TextureCache::DefaultTexturesLoader(std::string const& name, int w, int h)
38{
39 return nux::CreateTexture2DFromFile((PKGDATADIR"/" + name).c_str(), -1, true);
40}
41
44std::string TextureCache::Hash(std::string const& id, int width, int height)42std::string TextureCache::Hash(std::string const& id, int width, int height)
45{43{
46 std::ostringstream sout;44 std::ostringstream sout;
@@ -50,17 +48,22 @@
5048
51TextureCache::BaseTexturePtr TextureCache::FindTexture(std::string const& texture_id,49TextureCache::BaseTexturePtr TextureCache::FindTexture(std::string const& texture_id,
52 int width, int height,50 int width, int height,
53 CreateTextureCallback slot)51 CreateTextureCallback factory)
54{52{
55 if (!slot)53 if (!factory)
56 return BaseTexturePtr();54 return BaseTexturePtr();
5755
58 std::string key = Hash(texture_id, width, height);56 std::string const& key = Hash(texture_id, width, height);
59 BaseTexturePtr texture(cache_[key]);57 auto texture_it = cache_.find(key);
58
59 BaseTexturePtr texture(texture_it != cache_.end() ? texture_it->second : nullptr);
6060
61 if (!texture)61 if (!texture)
62 {62 {
63 texture = slot(texture_id, width, height);63 texture.Adopt(factory(texture_id, width, height));
64
65 if (!texture)
66 return texture;
6467
65 // Now here is the magic.68 // Now here is the magic.
66 //69 //
@@ -79,10 +82,6 @@
79 // are destroyed first, then the sigc::trackable disconnects all methods82 // are destroyed first, then the sigc::trackable disconnects all methods
80 // created using mem_fun.83 // created using mem_fun.
8184
82 // Reduce the internal reference count of the texture, so the smart
83 // pointer is the sole owner of the object.
84 texture->UnReference();
85
86 cache_[key] = texture.GetPointer();85 cache_[key] = texture.GetPointer();
8786
88 auto on_destroy = sigc::mem_fun(this, &TextureCache::OnDestroyNotify);87 auto on_destroy = sigc::mem_fun(this, &TextureCache::OnDestroyNotify);
@@ -92,7 +91,7 @@
92 return texture;91 return texture;
93}92}
9493
95void TextureCache::OnDestroyNotify(nux::Trackable* Object, std::string key)94void TextureCache::OnDestroyNotify(nux::Trackable* Object, std::string const& key)
96{95{
97 cache_.erase(key);96 cache_.erase(key);
98}97}
9998
=== modified file 'unity-shared/TextureCache.h'
--- unity-shared/TextureCache.h 2012-07-16 11:03:32 +0000
+++ unity-shared/TextureCache.h 2013-04-09 01:09:21 +0000
@@ -40,24 +40,26 @@
40public:40public:
41 typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;41 typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;
4242
43 ~TextureCache() = default;
44
43 // id, width, height -> texture45 // id, width, height -> texture
44 typedef std::function<nux::BaseTexture*(std::string const&, int, int)> CreateTextureCallback;46 typedef std::function<nux::BaseTexture*(std::string const&, int, int)> CreateTextureCallback;
4547
46 static TextureCache& GetDefault();48 static TextureCache& GetDefault();
49 static nux::BaseTexture* DefaultTexturesLoader(std::string const&, int, int);
4750
48 BaseTexturePtr FindTexture(std::string const& texture_id, int width, int height, CreateTextureCallback callback);51 BaseTexturePtr FindTexture(std::string const& texture_id, int width = 0, int height = 0, CreateTextureCallback callback = DefaultTexturesLoader);
4952
50 // Return the current size of the cache.53 // Return the current size of the cache.
51 std::size_t Size() const;54 std::size_t Size() const;
5255
53private:56private:
54 TextureCache();57 TextureCache() = default;
55 ~TextureCache();
5658
57 std::string Hash(std::string const& , int width, int height);59 std::string Hash(std::string const& , int width, int height);
58 // Have the key passed by value not referece, as the key will be a bound60 // Have the key passed by value not referece, as the key will be a bound
59 // parameter in the slot passed to the texture on_destroy signal.61 // parameter in the slot passed to the texture on_destroy signal.
60 void OnDestroyNotify(nux::Trackable* Object, std::string key);62 void OnDestroyNotify(nux::Trackable* Object, std::string const& key);
6163
62 std::map<std::string, nux::BaseTexture*> cache_;64 std::map<std::string, nux::BaseTexture*> cache_;
63};65};
6466
=== modified file 'unity-shared/WindowButtons.cpp'
--- unity-shared/WindowButtons.cpp 2013-03-19 18:47:01 +0000
+++ unity-shared/WindowButtons.cpp 2013-04-09 01:09:21 +0000
@@ -29,6 +29,7 @@
29#include "WindowButtons.h"29#include "WindowButtons.h"
30#include "WindowButtonPriv.h"30#include "WindowButtonPriv.h"
3131
32#include "unity-shared/TextureCache.h"
32#include "unity-shared/UBusMessages.h"33#include "unity-shared/UBusMessages.h"
33#include "unity-shared/WindowManager.h"34#include "unity-shared/WindowManager.h"
3435
@@ -176,8 +177,8 @@
176177
177 std::string subpath = names[static_cast<int>(type)] + states[static_cast<int>(state)] + ".png";178 std::string subpath = names[static_cast<int>(type)] + states[static_cast<int>(state)] + ".png";
178179
179 glib::String filename(g_build_filename(PKGDATADIR, subpath.c_str(), NULL));180 auto& cache = TextureCache::GetDefault();
180 texture.Adopt(nux::CreateTexture2DFromFile(filename, -1, true));181 texture = cache.FindTexture(subpath);
181182
182 if (!texture)183 if (!texture)
183 texture = panel::Style::Instance().GetFallbackWindowButton(type, state);184 texture = panel::Style::Instance().GetFallbackWindowButton(type, state);