Chromium Code Reviews| Index: chrome/common/gtk_util.cc |
| diff --git a/chrome/common/gtk_util.cc b/chrome/common/gtk_util.cc |
| index e0268773d8770100255606b732437512c6c46ff1..663190c732b736abcece36f264c912a57f0bbf4a 100644 |
| --- a/chrome/common/gtk_util.cc |
| +++ b/chrome/common/gtk_util.cc |
| @@ -58,6 +58,38 @@ GList* GetIconList() { |
| return icon_list; |
| } |
| +// A process wide singleton that manages our usage of gdk |
| +// cursors. gdk_cursor_new() hits the disk in several places and GdkCursor |
| +// instances can be reused throughout the process. |
| +class GdkCursorCache { |
| + public: |
| + ~GdkCursorCache() { |
| + for (std::map<GdkCursorType, GdkCursor*>::iterator it = |
| + cursor_cache_.begin(); it != cursor_cache_.end(); ++it) { |
| + gdk_cursor_unref(it->second); |
| + } |
| + cursor_cache_.clear(); |
| + } |
| + |
| + GdkCursor* GetCursorImpl(GdkCursorType type) { |
| + std::map<GdkCursorType, GdkCursor*>::iterator it = cursor_cache_.find(type); |
| + GdkCursor* cursor = NULL; |
| + if (it == cursor_cache_.end()) { |
| + cursor = gdk_cursor_new(type); |
| + cursor_cache_.insert(std::make_pair(type, cursor)); |
| + } else { |
| + cursor = it->second; |
| + } |
| + |
| + // Add a reference to the returned cursor because our consumers mix us with |
| + // gdk_cursor_new() calls which return a +1 refcount. |
| + gdk_cursor_ref(cursor); |
|
Evan Martin
2009/09/12 00:37:05
Won't this cause a +2 refcount on a cache miss?
|
| + return cursor; |
| + } |
| + |
| + std::map<GdkCursorType, GdkCursor*> cursor_cache_; |
| +}; |
| + |
| } // namespace |
| namespace event_utils { |
| @@ -474,4 +506,9 @@ GdkColor AverageColors(GdkColor color_one, GdkColor color_two) { |
| return average_color; |
| } |
| +GdkCursor* GetCursor(GdkCursorType type) { |
| + static GdkCursorCache impl; |
| + return impl.GetCursorImpl(type); |
| +} |
| + |
| } // namespace gtk_util |