diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 07:28:40 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 07:28:40 +0000 |
commit | 0a4cf456d7ac8a546c72d43b259513e0dc07ab17 (patch) | |
tree | dccfaa43d1c090c9fe40f8814304d6355f595b75 /ui/base/resource | |
parent | acf2474114d7d07c35553a23f70212b66bcdaab7 (diff) | |
download | chromium_src-0a4cf456d7ac8a546c72d43b259513e0dc07ab17.zip chromium_src-0a4cf456d7ac8a546c72d43b259513e0dc07ab17.tar.gz chromium_src-0a4cf456d7ac8a546c72d43b259513e0dc07ab17.tar.bz2 |
Load the resources for max scale factor first.
- made SCALE_FACTOR_NONE != SCALE_FACTOR_100P
- updated a few places that used SCALE_FACTOR_NONE to load
100P images
- Don't include 200P to supported scale factor unless
we have 200P assets (on chromeos)
BUG=156569
TEST=covered by test.
Review URL: https://chromiumcodereview.appspot.com/11301007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167622 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/resource')
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 43 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 6 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_android.cc | 2 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_aurax11.cc | 2 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_gtk.cc | 2 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_mac.mm | 2 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_unittest.cc | 33 |
7 files changed, 61 insertions, 29 deletions
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index d6eaec2..4bfd4dc 100644 --- a/ui/base/resource/resource_bundle.cc +++ b/ui/base/resource/resource_bundle.cc @@ -123,7 +123,7 @@ class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { ui::ScaleFactor scale_factor) OVERRIDE { SkBitmap image; bool fell_back_to_1x = false; - bool found = rb_->LoadBitmap(resource_id_, scale_factor, + bool found = rb_->LoadBitmap(resource_id_, &scale_factor, &image, &fell_back_to_1x); if (!found) return gfx::ImageSkiaRep(); @@ -375,14 +375,18 @@ gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { DCHECK(!delegate_ && !data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; - // TODO(oshima): This should be GetPrimaryDisplay().device_scale_factor(), - // but GetPrimaryDisplay() crashes at startup. - ScaleFactor primary_scale_factor = SCALE_FACTOR_100P; + // TODO(oshima): Consider reading the image size from png IHDR chunk and + // skip decoding here and remove #ifdef below. // ResourceBundle::GetSharedInstance() is destroyed after the // BrowserMainLoop has finished running. |image_skia| is guaranteed to be // destroyed before the resource bundle is destroyed. +#if defined(OS_CHROMEOS) + ui::ScaleFactor scale_factor_to_load = ui::GetMaxScaleFactor(); +#else + ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; +#endif gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), - primary_scale_factor); + scale_factor_to_load); if (image_skia.isNull()) { LOG(WARNING) << "Unable to load image with id " << resource_id; NOTREACHED(); // Want to assert in debug mode. @@ -452,7 +456,8 @@ base::StringPiece ResourceBundle::GetRawDataResourceForScale( } } for (size_t i = 0; i < data_packs_.size(); i++) { - if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && + if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P || + data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) && data_packs_[i]->GetStringPiece(resource_id, &data)) return data; } @@ -577,15 +582,6 @@ void ResourceBundle::AddDataPackFromPathInternal(const FilePath& path, void ResourceBundle::AddDataPack(DataPack* data_pack) { data_packs_.push_back(data_pack); -#if defined(OS_CHROMEOS) - // When Chrome is running on desktop and force-device-scale-factor is not - // specified, use SCALE_FACTOR_100P as |max_scale_factor_|. - if (!base::chromeos::IsRunningOnChromeOS() && - !CommandLine::ForCurrentProcess()->HasSwitch( - switches::kForceDeviceScaleFactor)) - return; -#endif - if (GetScaleFactorScale(data_pack->GetScaleFactor()) > GetScaleFactorScale(max_scale_factor_)) max_scale_factor_ = data_pack->GetScaleFactor(); @@ -674,14 +670,23 @@ bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, } bool ResourceBundle::LoadBitmap(int resource_id, - ScaleFactor scale_factor, + ScaleFactor* scale_factor, SkBitmap* bitmap, bool* fell_back_to_1x) const { DCHECK(fell_back_to_1x); for (size_t i = 0; i < data_packs_.size(); ++i) { - if (data_packs_[i]->GetScaleFactor() == scale_factor) { - if (LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) - return true; + // If the resource is in the package with SCALE_FACTOR_NONE, it + // can be used in any scale factor, but set 100P in ImageSkia so + // that it will be scaled property. + if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE && + LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { + *scale_factor = ui::SCALE_FACTOR_100P; + DCHECK(!*fell_back_to_1x); + return true; + } + if (data_packs_[i]->GetScaleFactor() == *scale_factor && + LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { + return true; } } return false; diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index de547fc..3c4f3cb 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -310,9 +310,11 @@ class UI_EXPORT ResourceBundle { bool* fell_back_to_1x) const; // Fills the |bitmap| given the |resource_id| and |scale_factor|. - // Returns false if the resource does not exist. + // Returns false if the resource does not exist. This may fall back to + // the data pack with SCALE_FACTOR_NONE, and when this happens, + // |scale_factor| will be set to SCALE_FACTOR_100P. bool LoadBitmap(int resource_id, - ScaleFactor scale_factor, + ScaleFactor* scale_factor, SkBitmap* bitmap, bool* fell_back_to_1x) const; diff --git a/ui/base/resource/resource_bundle_android.cc b/ui/base/resource/resource_bundle_android.cc index dc43957..bb1c9d7 100644 --- a/ui/base/resource/resource_bundle_android.cc +++ b/ui/base/resource/resource_bundle_android.cc @@ -20,7 +20,7 @@ void ResourceBundle::LoadCommonResources() { FilePath path; PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &path); AddDataPackFromPath(path.AppendASCII("chrome.pak"), - SCALE_FACTOR_100P); + SCALE_FACTOR_NONE); AddDataPackFromPath(path.AppendASCII("chrome_100_percent.pak"), SCALE_FACTOR_100P); } diff --git a/ui/base/resource/resource_bundle_aurax11.cc b/ui/base/resource/resource_bundle_aurax11.cc index ed2873f..810e011 100644 --- a/ui/base/resource/resource_bundle_aurax11.cc +++ b/ui/base/resource/resource_bundle_aurax11.cc @@ -35,7 +35,7 @@ void ResourceBundle::LoadCommonResources() { // scale factor to gfx::ImageSkia::AddRepresentation. AddDataPackFromPath(GetResourcesPakFilePath("chrome.pak"), - SCALE_FACTOR_100P); + SCALE_FACTOR_NONE); AddDataPackFromPath(GetResourcesPakFilePath( "chrome_100_percent.pak"), SCALE_FACTOR_100P); diff --git a/ui/base/resource/resource_bundle_gtk.cc b/ui/base/resource/resource_bundle_gtk.cc index 7040f9e..4bf1bde 100644 --- a/ui/base/resource/resource_bundle_gtk.cc +++ b/ui/base/resource/resource_bundle_gtk.cc @@ -66,7 +66,7 @@ FilePath GetResourcesPakFilePath(const std::string& pak_name) { void ResourceBundle::LoadCommonResources() { AddDataPackFromPath(GetResourcesPakFilePath("chrome.pak"), - SCALE_FACTOR_100P); + SCALE_FACTOR_NONE); AddDataPackFromPath(GetResourcesPakFilePath( "chrome_100_percent.pak"), SCALE_FACTOR_100P); diff --git a/ui/base/resource/resource_bundle_mac.mm b/ui/base/resource/resource_bundle_mac.mm index afaa47a..2ddd057 100644 --- a/ui/base/resource/resource_bundle_mac.mm +++ b/ui/base/resource/resource_bundle_mac.mm @@ -50,7 +50,7 @@ FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { void ResourceBundle::LoadCommonResources() { AddDataPackFromPath(GetResourcesPakFilePath(@"chrome", nil), - SCALE_FACTOR_100P); + SCALE_FACTOR_NONE); AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_100_percent", nil), SCALE_FACTOR_100P); AddDataPackFromPath(GetResourcesPakFilePath(@"webkit_resources_100_percent", diff --git a/ui/base/resource/resource_bundle_unittest.cc b/ui/base/resource/resource_bundle_unittest.cc index 339e59c..9478d5c 100644 --- a/ui/base/resource/resource_bundle_unittest.cc +++ b/ui/base/resource/resource_bundle_unittest.cc @@ -423,20 +423,29 @@ TEST_F(ResourceBundleImageTest, GetRawDataResource) { // Test requesting image reps at various scale factors from the image returned // via ResourceBundle::GetImageNamed(). TEST_F(ResourceBundleImageTest, GetImageNamed) { - FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); - FilePath data_2x_path = dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak")); + FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak"); + FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); // Create the pak files. - CreateDataPackWithSingleBitmap(data_path, 10, base::StringPiece()); + CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece()); CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece()); // Load the regular and 2x pak files. ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); - resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); + resource_bundle->AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P); resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); + EXPECT_EQ(SCALE_FACTOR_200P, resource_bundle->max_scale_factor()); + gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); +#if defined(OS_CHROMEOS) + // ChromeOS loads highest scale factor first. + EXPECT_EQ(ui::SCALE_FACTOR_200P, image_skia->image_reps()[0].scale_factor()); +#else + EXPECT_EQ(ui::SCALE_FACTOR_100P, image_skia->image_reps()[0].scale_factor()); +#endif + // Resource ID 3 exists in both 1x and 2x paks. Image reps should be // available for both scale factors in |image_skia|. gfx::ImageSkiaRep image_rep = @@ -482,4 +491,20 @@ TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) { EXPECT_EQ(20, image_rep.pixel_height()); } +TEST_F(ResourceBundleImageTest, FallbackToNone) { + FilePath data_default_path = dir_path().AppendASCII("sample.pak"); + + // Create the pak files. + CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece()); + + // Load the regular pak files only. + ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); + resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); + + gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); + EXPECT_EQ(1u, image_skia->image_reps().size()); + EXPECT_EQ(ui::SCALE_FACTOR_100P, + image_skia->image_reps()[0].scale_factor()); +} + } // namespace ui |