Index: ui/base/resource/resource_bundle.cc |
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc |
index 31aed58d5bcedef99a2e4b67ddc097e188e64a7f..8d2815db437f6bba95bc60d82c83f444ae8d5ac1 100644 |
--- a/ui/base/resource/resource_bundle.cc |
+++ b/ui/base/resource/resource_bundle.cc |
@@ -40,6 +40,14 @@ |
#include "ui/gfx/platform_font_pango.h" |
#endif |
+#if defined(OS_WIN) |
+#include "ui/gfx/win/dpi.h" |
+#endif |
+ |
+#if defined(OS_MACOSX) && !defined(OS_IOS) |
+#include "base/mac/mac_util.h" |
+#endif |
+ |
namespace ui { |
namespace { |
@@ -80,18 +88,19 @@ class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
virtual ~ResourceBundleImageSource() {} |
// gfx::ImageSkiaSource overrides: |
- virtual gfx::ImageSkiaRep GetImageForScale( |
- ui::ScaleFactor scale_factor) OVERRIDE { |
+ virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
SkBitmap image; |
bool fell_back_to_1x = false; |
+ ScaleFactor scale_factor = GetSupportedScaleFactor(scale); |
bool found = rb_->LoadBitmap(resource_id_, &scale_factor, |
&image, &fell_back_to_1x); |
+ // Force to a supported scale. |
+ scale = ui::GetImageScale(scale_factor); |
if (!found) |
return gfx::ImageSkiaRep(); |
if (fell_back_to_1x) { |
// GRIT fell back to the 100% image, so rescale it to the correct size. |
- float scale = GetScaleFactorScale(scale_factor); |
image = skia::ImageOperations::Resize( |
image, |
skia::ImageOperations::RESIZE_LANCZOS3, |
@@ -112,7 +121,7 @@ class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
} |
} |
- return gfx::ImageSkiaRep(image, scale_factor); |
+ return gfx::ImageSkiaRep(image, scale); |
} |
private: |
@@ -125,9 +134,7 @@ class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
// static |
std::string ResourceBundle::InitSharedInstanceWithLocale( |
const std::string& pref_locale, Delegate* delegate) { |
- DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
- g_shared_instance_ = new ResourceBundle(delegate); |
- |
+ InitSharedInstance(delegate); |
g_shared_instance_->LoadCommonResources(); |
std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
InitDefaultFont(); |
@@ -137,9 +144,7 @@ std::string ResourceBundle::InitSharedInstanceWithLocale( |
// static |
std::string ResourceBundle::InitSharedInstanceLocaleOnly( |
const std::string& pref_locale, Delegate* delegate) { |
- DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
- g_shared_instance_ = new ResourceBundle(delegate); |
- |
+ InitSharedInstance(delegate); |
std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
InitDefaultFont(); |
return result; |
@@ -148,9 +153,7 @@ std::string ResourceBundle::InitSharedInstanceLocaleOnly( |
// static |
void ResourceBundle::InitSharedInstanceWithPakFile( |
base::PlatformFile pak_file, bool should_load_common_resources) { |
- DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
- g_shared_instance_ = new ResourceBundle(NULL); |
- |
+ InitSharedInstance(NULL); |
if (should_load_common_resources) |
g_shared_instance_->LoadCommonResources(); |
@@ -166,9 +169,7 @@ void ResourceBundle::InitSharedInstanceWithPakFile( |
// static |
void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) { |
- DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
- g_shared_instance_ = new ResourceBundle(NULL); |
- |
+ InitSharedInstance(NULL); |
g_shared_instance_->LoadTestResources(path, path); |
InitDefaultFont(); |
@@ -341,18 +342,20 @@ gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
DCHECK(!data_packs_.empty()) << |
"Missing call to SetResourcesDataDLL?"; |
+#if defined(OS_CHROMEOS) |
+ ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor(); |
+#else |
+ ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; |
+#endif |
+ |
+ float scale = GetImageScale(scale_factor_to_load); |
// 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), |
- scale_factor_to_load); |
+ scale); |
if (image_skia.isNull()) { |
LOG(WARNING) << "Unable to load image with id " << resource_id; |
NOTREACHED(); // Want to assert in debug mode. |
@@ -510,6 +513,14 @@ void ResourceBundle::ReloadFonts() { |
LoadFontsIfNecessary(); |
} |
+ScaleFactor ResourceBundle::GetMaxScaleFactor() const { |
+#if defined(OS_CHROMEOS) |
+ return max_scale_factor_; |
+#else |
+ return GetSupportedScaleFactors().back(); |
+#endif |
+} |
+ |
ResourceBundle::ResourceBundle(Delegate* delegate) |
: delegate_(delegate), |
images_and_fonts_lock_(new base::Lock), |
@@ -522,6 +533,49 @@ ResourceBundle::~ResourceBundle() { |
UnloadLocaleResources(); |
} |
+// static |
+void ResourceBundle::InitSharedInstance(Delegate* delegate) { |
+ DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
+ g_shared_instance_ = new ResourceBundle(delegate); |
+ static std::vector<ScaleFactor> supported_scale_factors; |
+#if !defined(OS_IOS) |
+ // On platforms other than iOS, 100P is always a supported scale factor. |
+ supported_scale_factors.push_back(SCALE_FACTOR_100P); |
+#endif |
+ |
+#if defined(OS_ANDROID) |
+ const gfx::Display display = |
+ gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); |
+ const float display_density = display.device_scale_factor(); |
+ const ScaleFactor closest = FindClosestScaleFactorUnsafe(display_density); |
+ if (closest != SCALE_FACTOR_100P) |
+ supported_scale_factors.push_back(closest); |
+#elif defined(OS_IOS) |
+ gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); |
+ if (display.device_scale_factor() > 1.0) { |
+ DCHECK_EQ(2.0, display.device_scale_factor()); |
+ supported_scale_factors.push_back(SCALE_FACTOR_200P); |
+ } else { |
+ supported_scale_factors.push_back(SCALE_FACTOR_100P); |
+ } |
+#elif defined(OS_MACOSX) |
+ if (base::mac::IsOSLionOrLater()) |
+ supported_scale_factors.push_back(SCALE_FACTOR_200P); |
+#elif defined(OS_WIN) |
+ // Have high-DPI resources for 140% and 180% scaling on Windows based on |
+ // default scaling for Metro mode. Round to nearest supported scale in |
+ // all cases. |
+ if (gfx::IsInHighDPIMode()) { |
+ supported_scale_factors.push_back(SCALE_FACTOR_140P); |
+ supported_scale_factors.push_back(SCALE_FACTOR_180P); |
+ } |
+#elif defined(OS_CHROMEOS) |
+ // TODO(oshima): Include 200P only if the device support 200P |
+ supported_scale_factors.push_back(SCALE_FACTOR_200P); |
+#endif |
+ ui::SetSupportedScaleFactors(supported_scale_factors); |
+} |
+ |
void ResourceBundle::FreeImages() { |
images_.clear(); |
} |
@@ -554,8 +608,8 @@ void ResourceBundle::AddDataPackFromPathInternal(const base::FilePath& path, |
void ResourceBundle::AddDataPack(DataPack* data_pack) { |
data_packs_.push_back(data_pack); |
- if (GetScaleFactorScale(data_pack->GetScaleFactor()) > |
- GetScaleFactorScale(max_scale_factor_)) |
+ if (GetImageScale(data_pack->GetScaleFactor()) > |
+ GetImageScale(max_scale_factor_)) |
max_scale_factor_ = data_pack->GetScaleFactor(); |
} |