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

Side by Side Diff: ui/base/resource/resource_bundle_linux.cc

Issue 6541031: Integrate gfx::Image into the ResourceBundle. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ui/base/resource/resource_bundle.h" 5 #include "ui/base/resource/resource_bundle.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 8
9 #include "base/base_paths.h" 9 #include "base/base_paths.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/string_piece.h" 15 #include "base/string_piece.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "third_party/skia/include/core/SkBitmap.h" 18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/base/ui_base_paths.h" 19 #include "ui/base/ui_base_paths.h"
20 #include "ui/gfx/font.h" 20 #include "ui/gfx/font.h"
21 #include "ui/gfx/gtk_util.h" 21 #include "ui/gfx/gtk_util.h"
22 #include "ui/gfx/image.h"
22 23
23 namespace ui { 24 namespace ui {
24 25
25 namespace { 26 namespace {
26 27
27 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned 28 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned
28 // has a ref count of 1 so the caller must call g_object_unref to free the 29 // has a ref count of 1 so the caller must call g_object_unref to free the
29 // memory. 30 // memory.
30 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { 31 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) {
31 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); 32 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new());
(...skipping 18 matching lines...) Expand all
50 // The pixbuf is owned by the loader, so add a ref so when we delete the 51 // The pixbuf is owned by the loader, so add a ref so when we delete the
51 // loader (when the ScopedGObject goes out of scope), the pixbuf still 52 // loader (when the ScopedGObject goes out of scope), the pixbuf still
52 // exists. 53 // exists.
53 g_object_ref(pixbuf); 54 g_object_ref(pixbuf);
54 return pixbuf; 55 return pixbuf;
55 } 56 }
56 } 57 }
57 58
58 } // namespace 59 } // namespace
59 60
60 void ResourceBundle::FreeGdkPixBufs() {
61 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin();
62 i != gdk_pixbufs_.end(); i++) {
63 g_object_unref(i->second);
64 }
65 gdk_pixbufs_.clear();
66 }
67
68 // static 61 // static
69 FilePath ResourceBundle::GetResourcesFilePath() { 62 FilePath ResourceBundle::GetResourcesFilePath() {
70 FilePath resources_file_path; 63 FilePath resources_file_path;
71 PathService::Get(ui::FILE_RESOURCES_PAK, &resources_file_path); 64 PathService::Get(ui::FILE_RESOURCES_PAK, &resources_file_path);
72 return resources_file_path; 65 return resources_file_path;
73 } 66 }
74 67
75 // static 68 // static
76 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { 69 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
77 FilePath locale_file_path; 70 FilePath locale_file_path;
78 PathService::Get(ui::DIR_LOCALES, &locale_file_path); 71 PathService::Get(ui::DIR_LOCALES, &locale_file_path);
79 if (locale_file_path.empty()) 72 if (locale_file_path.empty())
80 return locale_file_path; 73 return locale_file_path;
81 if (app_locale.empty()) 74 if (app_locale.empty())
82 return FilePath(); 75 return FilePath();
83 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); 76 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak");
84 if (!file_util::PathExists(locale_file_path)) 77 if (!file_util::PathExists(locale_file_path))
85 return FilePath(); 78 return FilePath();
86 return locale_file_path; 79 return locale_file_path;
87 } 80 }
88 81
89 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { 82 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
83 return *GetPixbufImpl(resource_id, false);
84 }
85
86 gfx::Image* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) {
90 // Use the negative |resource_id| for the key for BIDI-aware images. 87 // Use the negative |resource_id| for the key for BIDI-aware images.
91 int key = rtl_enabled ? -resource_id : resource_id; 88 int key = rtl_enabled ? -resource_id : resource_id;
92 89
93 // Check to see if we already have the pixbuf in the cache. 90 // Check to see if the image is already in the cache.
94 { 91 {
95 base::AutoLock lock_scope(*lock_); 92 base::AutoLock lock_scope(*lock_);
96 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); 93 ImageMap::const_iterator found = images_.find(key);
97 if (found != gdk_pixbufs_.end()) 94 if (found != images_.end())
98 return found->second; 95 return found->second;
99 } 96 }
100 97
101 scoped_refptr<RefCountedStaticMemory> data( 98 scoped_refptr<RefCountedStaticMemory> data(
102 LoadDataResourceBytes(resource_id)); 99 LoadDataResourceBytes(resource_id));
103 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); 100 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled);
104 101
105 // We loaded successfully. Cache the pixbuf. 102 // The load was successful, so cache the image.
106 if (pixbuf) { 103 if (pixbuf) {
107 base::AutoLock lock_scope(*lock_); 104 base::AutoLock lock_scope(*lock_);
108 105
109 // Another thread raced us, and has already cached the pixbuf. 106 // Another thread raced the load and has already cached the image.
110 if (gdk_pixbufs_.count(key)) { 107 if (images_.count(key)) {
111 g_object_unref(pixbuf); 108 g_object_unref(pixbuf);
112 return gdk_pixbufs_[key]; 109 return images_[key];
113 } 110 }
114 111
115 gdk_pixbufs_[key] = pixbuf; 112 gfx::Image* image = new gfx::Image(pixbuf); // Takes ownership.
116 return pixbuf; 113 images_[key] = image;
114 return image;
117 } 115 }
118 116
119 // We failed to retrieve the bitmap, show a debugging red square. 117 LOG(WARNING) << "Unable to pixbuf with id " << resource_id;
120 { 118 NOTREACHED(); // Want to assert in debug mode.
121 LOG(WARNING) << "Unable to load GdkPixbuf with id " << resource_id; 119 return GetEmptyImage();
122 NOTREACHED(); // Want to assert in debug mode.
123
124 base::AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization.
125
126 static GdkPixbuf* empty_bitmap = NULL;
127 if (!empty_bitmap) {
128 // The placeholder bitmap is bright red so people notice the problem.
129 // This bitmap will be leaked, but this code should never be hit.
130 scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap());
131 skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
132 skia_bitmap->allocPixels();
133 skia_bitmap->eraseARGB(255, 255, 0, 0);
134 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get());
135 }
136 return empty_bitmap;
137 }
138 } 120 }
139 121
140 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { 122 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) {
141 return GetPixbufImpl(resource_id, false); 123 return *GetPixbufImpl(resource_id, false);
142 } 124 }
143 125
144 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { 126 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) {
145 return GetPixbufImpl(resource_id, true); 127 return *GetPixbufImpl(resource_id, true);
146 } 128 }
147 129
148 } // namespace ui 130 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698