diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 17:59:47 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 17:59:47 +0000 |
commit | 6f01e95e2c326c01f573968b136781e87e62067a (patch) | |
tree | 4991623f5221c97147992f60a10b2147d230aebc /ui | |
parent | e4359be2f8068dce74234add3fb5268bbc90670f (diff) | |
download | chromium_src-6f01e95e2c326c01f573968b136781e87e62067a.zip chromium_src-6f01e95e2c326c01f573968b136781e87e62067a.tar.gz chromium_src-6f01e95e2c326c01f573968b136781e87e62067a.tar.bz2 |
Integrate gfx::Image into the ResourceBundle.
This changes the definition of GetNativeImageNamed to return a gfx::Image and
adds GetImageNamed in addition. GetBitmapNamed now goes through that method. On
Linux, GetNativeImageNamed will load directly into a GdkPixbuf-backed Image. Mac
will still first load through Skia (a future CL will load directly into an
NSImage). All other platforms use Skia natively, so GetNativeImageNamed is the
same as GetImageNamed.
Mac was the only platform that used the old GetNativeImageNamed, so this also
transitions a bunch of Mac files by including ui/gfx/image.h.
This also obviates the need for platform-specific image caches in the
ResourceBundle, so there's some additional cleanup around that.
BUG=carnitas
TEST=All UI images in Chromium show up as before.
Review URL: http://codereview.chromium.org/6541031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75605 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 95 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 51 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_linux.cc | 60 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_mac.mm | 14 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_posix.cc | 3 | ||||
-rw-r--r-- | ui/gfx/image.cc | 1 | ||||
-rw-r--r-- | ui/gfx/image.h | 7 |
7 files changed, 105 insertions, 126 deletions
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index ed5c14c..7d60cfc 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 ------------------------------------------------------------- diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index 9ff0e30..dd5a65f 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -33,6 +33,7 @@ class StringPiece; namespace gfx { class Font; +class Image; } #if defined(OS_MACOSX) @@ -98,8 +99,23 @@ class ResourceBundle { // Gets the bitmap with the specified resource_id from the current module // data. Returns a pointer to a shared instance of the SkBitmap. This shared // bitmap is owned by the resource bundle and should not be freed. + // + // !! THIS IS DEPRECATED. PLEASE USE THE METHOD BELOW. !! SkBitmap* GetBitmapNamed(int resource_id); + // Gets an image resource from the current module data. This will load the + // image in Skia format by default. The ResourceBundle owns this. + gfx::Image& GetImageNamed(int resource_id); + + // Similar to GetImageNamed, but rather than loading the image in Skia format, + // it will load in the native platform type. This can avoid conversion from + // one image type to another. ResourceBundle owns the result. + // + // Note that if the same resource has already been loaded in GetImageNamed(), + // gfx::Image will perform a conversion, rather than using the native image + // loading code of ResourceBundle. + gfx::Image& GetNativeImageNamed(int resource_id); + // Loads the raw bytes of a data resource into |bytes|, // without doing any processing or interpretation of // the resource. Returns whether we successfully read the resource. @@ -115,24 +131,12 @@ class ResourceBundle { // Returns the font for the specified style. const gfx::Font& GetFont(FontStyle style); - // Returns the gfx::NativeImage, the native platform type, named resource. - // Internally, this makes use of GetNSImageNamed(), GetPixbufNamed(), or - // GetBitmapNamed() depending on the platform (see gfx/native_widget_types.h). - // NOTE: On Mac the returned resource is autoreleased. - gfx::NativeImage GetNativeImageNamed(int resource_id); - #if defined(OS_WIN) // Loads and returns an icon from the app module. HICON LoadThemeIcon(int icon_id); // Loads and returns a cursor from the app module. HCURSOR LoadCursor(int cursor_id); -#elif defined(OS_MACOSX) - private: - // Wrapper for GetBitmapNamed. Converts the bitmap to an autoreleased NSImage. - // TODO(rsesek): Move implementation into GetNativeImageNamed(). - NSImage* GetNSImageNamed(int resource_id); - public: #elif defined(USE_X11) // Gets the GdkPixbuf with the specified resource_id from the main data pak // file. Returns a pointer to a shared instance of the GdkPixbuf. This @@ -149,7 +153,7 @@ class ResourceBundle { private: // Shared implementation for the above two functions. - GdkPixbuf* GetPixbufImpl(int resource_id, bool rtl_enabled); + gfx::Image* GetPixbufImpl(int resource_id, bool rtl_enabled); public: #endif @@ -200,11 +204,6 @@ class ResourceBundle { // Free skia_images_. void FreeImages(); -#if defined(USE_X11) - // Free gdkPixbufs_. - void FreeGdkPixBufs(); -#endif - // Load the main resources. void LoadCommonResources(); @@ -241,8 +240,12 @@ class ResourceBundle { // done. static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); + // Returns an empty image for when a resource cannot be loaded. This is a + // bright red bitmap. + gfx::Image* GetEmptyImage(); + // Class level lock. Used to protect internal data structures that may be - // accessed from other threads (e.g., skia_images_). + // accessed from other threads (e.g., images_). scoped_ptr<base::Lock> lock_; // Handles for data sources. @@ -252,14 +255,10 @@ class ResourceBundle { // References to extra data packs loaded via AddDataPackToSharedInstance. std::vector<LoadedDataPack*> data_packs_; - // Cached images. The ResourceBundle caches all retrieved bitmaps and keeps + // Cached images. The ResourceBundle caches all retrieved images and keeps // ownership of the pointers. - typedef std::map<int, SkBitmap*> SkImageMap; - SkImageMap skia_images_; -#if defined(USE_X11) - typedef std::map<int, GdkPixbuf*> GdkPixbufMap; - GdkPixbufMap gdk_pixbufs_; -#endif + typedef std::map<int, gfx::Image*> ImageMap; + ImageMap images_; // The various fonts used. Cached to avoid repeated GDI creation/destruction. scoped_ptr<gfx::Font> base_font_; diff --git a/ui/base/resource/resource_bundle_linux.cc b/ui/base/resource/resource_bundle_linux.cc index eff8bdc..437f725 100644 --- a/ui/base/resource/resource_bundle_linux.cc +++ b/ui/base/resource/resource_bundle_linux.cc @@ -19,6 +19,7 @@ #include "ui/base/ui_base_paths.h" #include "ui/gfx/font.h" #include "ui/gfx/gtk_util.h" +#include "ui/gfx/image.h" namespace ui { @@ -57,14 +58,6 @@ GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { } // namespace -void ResourceBundle::FreeGdkPixBufs() { - for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); - i != gdk_pixbufs_.end(); i++) { - g_object_unref(i->second); - } - gdk_pixbufs_.clear(); -} - // static FilePath ResourceBundle::GetResourcesFilePath() { FilePath resources_file_path; @@ -86,15 +79,19 @@ FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { return locale_file_path; } -GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { + return *GetPixbufImpl(resource_id, false); +} + +gfx::Image* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { // Use the negative |resource_id| for the key for BIDI-aware images. int key = rtl_enabled ? -resource_id : resource_id; - // Check to see if we already have the pixbuf in the cache. + // Check to see if the image is already in the cache. { base::AutoLock lock_scope(*lock_); - GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); - if (found != gdk_pixbufs_.end()) + ImageMap::const_iterator found = images_.find(key); + if (found != images_.end()) return found->second; } @@ -102,47 +99,32 @@ GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { LoadDataResourceBytes(resource_id)); GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); - // We loaded successfully. Cache the pixbuf. + // The load was successful, so cache the image. if (pixbuf) { base::AutoLock lock_scope(*lock_); - // Another thread raced us, and has already cached the pixbuf. - if (gdk_pixbufs_.count(key)) { + // Another thread raced the load and has already cached the image. + if (images_.count(key)) { g_object_unref(pixbuf); - return gdk_pixbufs_[key]; + return images_[key]; } - gdk_pixbufs_[key] = pixbuf; - return pixbuf; + gfx::Image* image = new gfx::Image(pixbuf); // Takes ownership. + images_[key] = image; + return image; } - // We failed to retrieve the bitmap, show a debugging red square. - { - LOG(WARNING) << "Unable to load GdkPixbuf with id " << resource_id; - NOTREACHED(); // Want to assert in debug mode. - - base::AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. - - static GdkPixbuf* 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. - scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap()); - skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); - skia_bitmap->allocPixels(); - skia_bitmap->eraseARGB(255, 255, 0, 0); - empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get()); - } - return empty_bitmap; - } + LOG(WARNING) << "Unable to pixbuf with id " << resource_id; + NOTREACHED(); // Want to assert in debug mode. + return GetEmptyImage(); } GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { - return GetPixbufImpl(resource_id, false); + return *GetPixbufImpl(resource_id, false); } GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { - return GetPixbufImpl(resource_id, true); + return *GetPixbufImpl(resource_id, true); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_mac.mm b/ui/base/resource/resource_bundle_mac.mm index e859b1c..f7bc0f8 100644 --- a/ui/base/resource/resource_bundle_mac.mm +++ b/ui/base/resource/resource_bundle_mac.mm @@ -58,15 +58,11 @@ FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { return GetResourcesPakFilePath(@"locale", mac_locale); } -NSImage* ResourceBundle::GetNSImageNamed(int resource_id) { - // Currently this doesn't make a cache holding these as NSImages because - // GetBitmapNamed has a cache, and we don't want to double cache. - SkBitmap* bitmap = GetBitmapNamed(resource_id); - if (!bitmap) - return nil; - - NSImage* nsimage = gfx::SkBitmapToNSImage(*bitmap); - return nsimage; +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { + // Currently this just returns the Skia-backed image, which will convert to + // NSImage and cache that result when necessary. + // TODO(rsesek): Load the raw bytes directly into an NSImage instead. + return GetImageNamed(resource_id); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_posix.cc b/ui/base/resource/resource_bundle_posix.cc index 11011e2..a5db6cb 100644 --- a/ui/base/resource/resource_bundle_posix.cc +++ b/ui/base/resource/resource_bundle_posix.cc @@ -31,9 +31,6 @@ DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { ResourceBundle::~ResourceBundle() { FreeImages(); -#if defined(OS_POSIX) && !defined(OS_MACOSX) - FreeGdkPixBufs(); -#endif UnloadLocaleResources(); STLDeleteContainerPointers(data_packs_.begin(), data_packs_.end()); diff --git a/ui/gfx/image.cc b/ui/gfx/image.cc index 9a9c808..25eab5b 100644 --- a/ui/gfx/image.cc +++ b/ui/gfx/image.cc @@ -5,6 +5,7 @@ #include "ui/gfx/image.h" #include "base/logging.h" +#include "third_party/skia/include/core/SkBitmap.h" #if defined(OS_LINUX) #include <gdk-pixbuf/gdk-pixbuf.h> diff --git a/ui/gfx/image.h b/ui/gfx/image.h index 33e363e..7666249 100644 --- a/ui/gfx/image.h +++ b/ui/gfx/image.h @@ -12,7 +12,8 @@ #include "base/gtest_prod_util.h" #include "build/build_config.h" #include "ui/gfx/native_widget_types.h" // Forward-declares GdkPixbuf and NSImage. -#include "third_party/skia/include/core/SkBitmap.h" + +class SkBitmap; namespace { class ImageTest; @@ -43,10 +44,10 @@ class Image { // ownership of the image. explicit Image(const SkBitmap* bitmap); #if defined(OS_LINUX) - // Does not increase |pixbuf|'s reference count. + // Does not increase |pixbuf|'s reference count; expects to take ownership. explicit Image(GdkPixbuf* pixbuf); #elif defined(OS_MACOSX) - // Does not retain |image|. + // Does not retain |image|; expects to take ownership. explicit Image(NSImage* image); #endif |