OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/resource_bundle.h" | 5 #include "app/resource_bundle.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "app/app_paths.h" | 9 #include "app/app_paths.h" |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
14 #include "base/lock.h" | |
15 #include "base/logging.h" | 14 #include "base/logging.h" |
16 #include "base/path_service.h" | 15 #include "base/path_service.h" |
17 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
18 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/synchronization/lock.h" |
19 #include "gfx/font.h" | 19 #include "gfx/font.h" |
20 #include "gfx/gtk_util.h" | 20 #include "gfx/gtk_util.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned | 25 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned |
26 // has a ref count of 1 so the caller must call g_object_unref to free the | 26 // has a ref count of 1 so the caller must call g_object_unref to free the |
27 // memory. | 27 // memory. |
28 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { | 28 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 return FilePath(); | 83 return FilePath(); |
84 return locale_file_path; | 84 return locale_file_path; |
85 } | 85 } |
86 | 86 |
87 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { | 87 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { |
88 // Use the negative |resource_id| for the key for BIDI-aware images. | 88 // Use the negative |resource_id| for the key for BIDI-aware images. |
89 int key = rtl_enabled ? -resource_id : resource_id; | 89 int key = rtl_enabled ? -resource_id : resource_id; |
90 | 90 |
91 // Check to see if we already have the pixbuf in the cache. | 91 // Check to see if we already have the pixbuf in the cache. |
92 { | 92 { |
93 AutoLock lock_scope(*lock_); | 93 base::AutoLock lock_scope(*lock_); |
94 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); | 94 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); |
95 if (found != gdk_pixbufs_.end()) | 95 if (found != gdk_pixbufs_.end()) |
96 return found->second; | 96 return found->second; |
97 } | 97 } |
98 | 98 |
99 scoped_refptr<RefCountedStaticMemory> data( | 99 scoped_refptr<RefCountedStaticMemory> data( |
100 LoadDataResourceBytes(resource_id)); | 100 LoadDataResourceBytes(resource_id)); |
101 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); | 101 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); |
102 | 102 |
103 // We loaded successfully. Cache the pixbuf. | 103 // We loaded successfully. Cache the pixbuf. |
104 if (pixbuf) { | 104 if (pixbuf) { |
105 AutoLock lock_scope(*lock_); | 105 base::AutoLock lock_scope(*lock_); |
106 | 106 |
107 // Another thread raced us, and has already cached the pixbuf. | 107 // Another thread raced us, and has already cached the pixbuf. |
108 if (gdk_pixbufs_.count(key)) { | 108 if (gdk_pixbufs_.count(key)) { |
109 g_object_unref(pixbuf); | 109 g_object_unref(pixbuf); |
110 return gdk_pixbufs_[key]; | 110 return gdk_pixbufs_[key]; |
111 } | 111 } |
112 | 112 |
113 gdk_pixbufs_[key] = pixbuf; | 113 gdk_pixbufs_[key] = pixbuf; |
114 return pixbuf; | 114 return pixbuf; |
115 } | 115 } |
116 | 116 |
117 // We failed to retrieve the bitmap, show a debugging red square. | 117 // We failed to retrieve the bitmap, show a debugging red square. |
118 { | 118 { |
119 LOG(WARNING) << "Unable to load GdkPixbuf with id " << resource_id; | 119 LOG(WARNING) << "Unable to load GdkPixbuf with id " << resource_id; |
120 NOTREACHED(); // Want to assert in debug mode. | 120 NOTREACHED(); // Want to assert in debug mode. |
121 | 121 |
122 AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. | 122 base::AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. |
123 | 123 |
124 static GdkPixbuf* empty_bitmap = NULL; | 124 static GdkPixbuf* empty_bitmap = NULL; |
125 if (!empty_bitmap) { | 125 if (!empty_bitmap) { |
126 // The placeholder bitmap is bright red so people notice the problem. | 126 // The placeholder bitmap is bright red so people notice the problem. |
127 // This bitmap will be leaked, but this code should never be hit. | 127 // This bitmap will be leaked, but this code should never be hit. |
128 scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap()); | 128 scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap()); |
129 skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 129 skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
130 skia_bitmap->allocPixels(); | 130 skia_bitmap->allocPixels(); |
131 skia_bitmap->eraseARGB(255, 255, 0, 0); | 131 skia_bitmap->eraseARGB(255, 255, 0, 0); |
132 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get()); | 132 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get()); |
133 } | 133 } |
134 return empty_bitmap; | 134 return empty_bitmap; |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { | 138 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { |
139 return GetPixbufImpl(resource_id, false); | 139 return GetPixbufImpl(resource_id, false); |
140 } | 140 } |
141 | 141 |
142 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { | 142 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { |
143 return GetPixbufImpl(resource_id, true); | 143 return GetPixbufImpl(resource_id, true); |
144 } | 144 } |
OLD | NEW |