Index: ui/base/resource/resource_bundle.cc |
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc |
index 893d5f43198e6a56ef704965815df9ad3865a8d3..f62938a197a399793f057c6017da8c7533cf7d88 100644 |
--- a/ui/base/resource/resource_bundle.cc |
+++ b/ui/base/resource/resource_bundle.cc |
@@ -35,6 +35,14 @@ |
#include "ui/gfx/size_conversions.h" |
#include "ui/gfx/skbitmap_operations.h" |
+#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 { |
@@ -68,18 +76,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, |
@@ -100,7 +109,7 @@ class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
} |
} |
- return gfx::ImageSkiaRep(image, scale_factor); |
+ return gfx::ImageSkiaRep(image, scale); |
} |
private: |
@@ -113,9 +122,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); |
return result; |
@@ -124,9 +131,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); |
return result; |
} |
@@ -134,9 +139,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(); |
@@ -151,9 +154,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); |
} |
@@ -330,12 +331,12 @@ gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
// 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(); |
+ float scale_to_load = gfx::ImageSkia::GetMaxSupportedScale(); |
#else |
- ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; |
+ float scale_to_load = 1.0f; |
#endif |
gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), |
- scale_factor_to_load); |
+ scale_to_load); |
if (image_skia.isNull()) { |
LOG(WARNING) << "Unable to load image with id " << resource_id; |
NOTREACHED(); // Want to assert in debug mode. |
@@ -505,6 +506,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(); |
} |
@@ -537,8 +581,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(); |
} |