Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4456)

Unified Diff: chrome/common/gtk_util.cc

Issue 195071: GTK: Cache our GdkCursor objects, since building them hits the disk. (Closed)
Patch Set: Put in gtk_util Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/gtk_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « chrome/common/gtk_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698