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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « chrome/common/gtk_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/gtk_util.h" 5 #include "chrome/common/gtk_util.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h> 8 #include <gdk/gdkx.h>
9 9
10 #include <cstdarg> 10 #include <cstdarg>
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 51
52 // Ownership of |icon_list| is passed to the caller. 52 // Ownership of |icon_list| is passed to the caller.
53 GList* GetIconList() { 53 GList* GetIconList() {
54 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 54 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
55 GList* icon_list = NULL; 55 GList* icon_list = NULL;
56 icon_list = g_list_append(icon_list, rb.GetPixbufNamed(IDR_PRODUCT_ICON_32)); 56 icon_list = g_list_append(icon_list, rb.GetPixbufNamed(IDR_PRODUCT_ICON_32));
57 icon_list = g_list_append(icon_list, rb.GetPixbufNamed(IDR_PRODUCT_LOGO_16)); 57 icon_list = g_list_append(icon_list, rb.GetPixbufNamed(IDR_PRODUCT_LOGO_16));
58 return icon_list; 58 return icon_list;
59 } 59 }
60 60
61 // A process wide singleton that manages our usage of gdk
62 // cursors. gdk_cursor_new() hits the disk in several places and GdkCursor
63 // instances can be reused throughout the process.
64 class GdkCursorCache {
65 public:
66 ~GdkCursorCache() {
67 for (std::map<GdkCursorType, GdkCursor*>::iterator it =
68 cursor_cache_.begin(); it != cursor_cache_.end(); ++it) {
69 gdk_cursor_unref(it->second);
70 }
71 cursor_cache_.clear();
72 }
73
74 GdkCursor* GetCursorImpl(GdkCursorType type) {
75 std::map<GdkCursorType, GdkCursor*>::iterator it = cursor_cache_.find(type);
76 GdkCursor* cursor = NULL;
77 if (it == cursor_cache_.end()) {
78 cursor = gdk_cursor_new(type);
79 cursor_cache_.insert(std::make_pair(type, cursor));
80 } else {
81 cursor = it->second;
82 }
83
84 // Add a reference to the returned cursor because our consumers mix us with
85 // gdk_cursor_new() calls which return a +1 refcount.
86 gdk_cursor_ref(cursor);
Evan Martin 2009/09/12 00:37:05 Won't this cause a +2 refcount on a cache miss?
87 return cursor;
88 }
89
90 std::map<GdkCursorType, GdkCursor*> cursor_cache_;
91 };
92
61 } // namespace 93 } // namespace
62 94
63 namespace event_utils { 95 namespace event_utils {
64 96
65 WindowOpenDisposition DispositionFromEventFlags(guint event_flags) { 97 WindowOpenDisposition DispositionFromEventFlags(guint event_flags) {
66 if ((event_flags & GDK_BUTTON2_MASK) || (event_flags & GDK_CONTROL_MASK)) { 98 if ((event_flags & GDK_BUTTON2_MASK) || (event_flags & GDK_CONTROL_MASK)) {
67 return (event_flags & GDK_SHIFT_MASK) ? 99 return (event_flags & GDK_SHIFT_MASK) ?
68 NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB; 100 NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
69 } 101 }
70 102
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 499
468 GdkColor AverageColors(GdkColor color_one, GdkColor color_two) { 500 GdkColor AverageColors(GdkColor color_one, GdkColor color_two) {
469 GdkColor average_color; 501 GdkColor average_color;
470 average_color.pixel = 0; 502 average_color.pixel = 0;
471 average_color.red = (color_one.red + color_two.red) / 2; 503 average_color.red = (color_one.red + color_two.red) / 2;
472 average_color.green = (color_one.green + color_two.green) / 2; 504 average_color.green = (color_one.green + color_two.green) / 2;
473 average_color.blue = (color_one.blue + color_two.blue) / 2; 505 average_color.blue = (color_one.blue + color_two.blue) / 2;
474 return average_color; 506 return average_color;
475 } 507 }
476 508
509 GdkCursor* GetCursor(GdkCursorType type) {
510 static GdkCursorCache impl;
511 return impl.GetCursorImpl(type);
512 }
513
477 } // namespace gtk_util 514 } // namespace gtk_util
OLDNEW
« 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