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

Unified Diff: ui/base/resource/resource_bundle.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 side-by-side diff with in-line comments
Download patch
Index: ui/base/resource/resource_bundle.cc
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc
index ed5c14c30cba930f027fb8ef69ce73f22ac60862..7d60cfc44b5628a87b248eb2ee9b57364f914e2a 100644
--- a/ui/base/resource/resource_bundle.cc
+++ b/ui/base/resource/resource_bundle.cc
@@ -13,6 +13,7 @@
#include "ui/base/resource/data_pack.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/font.h"
+#include "ui/gfx/image.h"
namespace ui {
@@ -94,50 +95,48 @@ ResourceBundle& ResourceBundle::GetSharedInstance() {
}
SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) {
- // Check to see if we already have the Skia image in the cache.
+ const SkBitmap* bitmap =
+ static_cast<const SkBitmap*>(GetImageNamed(resource_id));
+ return const_cast<SkBitmap*>(bitmap);
+}
+
+gfx::Image& ResourceBundle::GetImageNamed(int resource_id) {
+ // Check to see if the image is already in the cache.
{
base::AutoLock lock_scope(*lock_);
- SkImageMap::const_iterator found = skia_images_.find(resource_id);
- if (found != skia_images_.end())
- return found->second;
+ ImageMap::const_iterator found = images_.find(resource_id);
+ if (found != images_.end())
+ return *found->second;
}
- scoped_ptr<SkBitmap> bitmap;
-
- bitmap.reset(LoadBitmap(resources_data_, resource_id));
-
+ scoped_ptr<SkBitmap> bitmap(LoadBitmap(resources_data_, resource_id));
if (bitmap.get()) {
- // We loaded successfully. Cache the Skia version of the bitmap.
+ // The load was successful, so cache the image.
base::AutoLock lock_scope(*lock_);
- // Another thread raced us, and has already cached the skia image.
- if (skia_images_.count(resource_id))
- return skia_images_[resource_id];
+ // Another thread raced the load and has already cached the image.
+ if (images_.count(resource_id))
+ return *images_[resource_id];
- skia_images_[resource_id] = bitmap.get();
- return bitmap.release();
+ gfx::Image* image = new gfx::Image(bitmap.release());
+ images_[resource_id] = image;
+ return *image;
}
- // We failed to retrieve the bitmap, show a debugging red square.
- {
- LOG(WARNING) << "Unable to load bitmap with id " << resource_id;
- NOTREACHED(); // Want to assert in debug mode.
-
- base::AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization.
-
- static SkBitmap* empty_bitmap = NULL;
- if (!empty_bitmap) {
- // The placeholder bitmap is bright red so people notice the problem.
- // This bitmap will be leaked, but this code should never be hit.
- empty_bitmap = new SkBitmap();
- empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
- empty_bitmap->allocPixels();
- empty_bitmap->eraseARGB(255, 255, 0, 0);
- }
- return empty_bitmap;
- }
+ // The load failed to retrieve the image; show a debugging red square.
+ LOG(WARNING) << "Unable to load image with id " << resource_id;
+ NOTREACHED(); // Want to assert in debug mode.
+ return *GetEmptyImage();
}
+#if !defined(OS_MACOSX) && !defined(OS_LINUX)
+// Only Mac and Linux have non-Skia native image types. All other platforms use
+// Skia natively, so just use GetImageNamed().
+gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
+ return GetImageNamed(resource_id);
+}
+#endif
+
RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes(
int resource_id) const {
RefCountedStaticMemory* bytes =
@@ -171,17 +170,6 @@ const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
}
}
-gfx::NativeImage ResourceBundle::GetNativeImageNamed(int resource_id) {
- ResourceBundle& rb = ResourceBundle::GetSharedInstance();
-#if defined(OS_MACOSX)
- return rb.GetNSImageNamed(resource_id);
-#elif defined(USE_X11) && !defined(TOOLKIT_VIEWS)
- return rb.GetPixbufNamed(resource_id);
-#else
- return rb.GetBitmapNamed(resource_id);
-#endif
-}
-
ResourceBundle::ResourceBundle()
: lock_(new base::Lock),
resources_data_(NULL),
@@ -189,9 +177,9 @@ ResourceBundle::ResourceBundle()
}
void ResourceBundle::FreeImages() {
- STLDeleteContainerPairSecondPointers(skia_images_.begin(),
- skia_images_.end());
- skia_images_.clear();
+ STLDeleteContainerPairSecondPointers(images_.begin(),
+ images_.end());
+ images_.clear();
}
void ResourceBundle::LoadFontsIfNecessary() {
@@ -235,6 +223,21 @@ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) {
return new SkBitmap(bitmap);
}
+gfx::Image* ResourceBundle::GetEmptyImage() {
+ base::AutoLock lock(*lock_);
+
+ static gfx::Image* empty_image = NULL;
+ if (!empty_image) {
+ // The placeholder bitmap is bright red so people notice the problem.
+ // This bitmap will be leaked, but this code should never be hit.
+ SkBitmap* bitmap = new SkBitmap();
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
+ bitmap->allocPixels();
+ bitmap->eraseARGB(255, 255, 0, 0);
+ empty_image = new gfx::Image(bitmap);
+ }
+ return empty_image;
+}
// LoadedDataPack -------------------------------------------------------------

Powered by Google App Engine
This is Rietveld 408576698