Commit 6c6ec25a authored by Nigel Tao's avatar Nigel Tao Committed by Commit Bot

Add quantization option to converting dip / px

Change-Id: I0b0b5d4680ebd4c78d362fa62ecec72629835ffc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1752492
Commit-Queue: Nigel Tao <nigeltao@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#686711}
parent 72fcd602
...@@ -141,11 +141,9 @@ void RunCallbackWithCompressedDataFromExtension( ...@@ -141,11 +141,9 @@ void RunCallbackWithCompressedDataFromExtension(
// compressed icons (i.e. PNG-formatted data), not uncompressed // compressed icons (i.e. PNG-formatted data), not uncompressed
// (i.e. a gfx::ImageSkia). // (i.e. a gfx::ImageSkia).
const gfx::Size dip_size = gfx::Size(size_hint_in_dip, size_hint_in_dip); constexpr bool quantize_to_supported_scale_factor = true;
float scale = int size_hint_in_px = apps_util::ConvertDipToPx(
ui::GetScaleForScaleFactor(apps_util::GetPrimaryDisplayUIScaleFactor()); size_hint_in_dip, quantize_to_supported_scale_factor);
int size_hint_in_px = gfx::ScaleToFlooredSize(dip_size, scale).width();
extensions::ExtensionResource ext_resource = extensions::ExtensionResource ext_resource =
extensions::IconsInfo::GetIconResource(extension, size_hint_in_px, extensions::IconsInfo::GetIconResource(extension, size_hint_in_px,
ExtensionIconSet::MATCH_BIGGER); ExtensionIconSet::MATCH_BIGGER);
......
...@@ -78,7 +78,9 @@ void AppIconSource::StartDataRequest( ...@@ -78,7 +78,9 @@ void AppIconSource::StartDataRequest(
LoadDefaultImage(callback); LoadDefaultImage(callback);
return; return;
} }
int size_in_dip = apps_util::ConvertPxToDip(size); constexpr bool quantize_to_supported_scale_factor = false;
int size_in_dip =
apps_util::ConvertPxToDip(size, quantize_to_supported_scale_factor);
apps::AppServiceProxy* app_service_proxy = apps::AppServiceProxy* app_service_proxy =
apps::AppServiceProxyFactory::GetForProfile(profile_); apps::AppServiceProxyFactory::GetForProfile(profile_);
......
...@@ -516,11 +516,14 @@ void ArcApps::LoadIconFromVM(const std::string app_id, ...@@ -516,11 +516,14 @@ void ArcApps::LoadIconFromVM(const std::string app_id,
if (prefs) { if (prefs) {
std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(app_id); std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(app_id);
if (app_info) { if (app_info) {
constexpr bool quantize_to_supported_scale_factor = false;
base::OnceCallback<void(apps::ArcApps::AppConnectionHolder*)> pending = base::OnceCallback<void(apps::ArcApps::AppConnectionHolder*)> pending =
base::BindOnce(&LoadIcon0, icon_compression, base::BindOnce(
apps_util::ConvertDipToPx(size_hint_in_dip), &LoadIcon0, icon_compression,
app_info->package_name, app_info->activity, apps_util::ConvertDipToPx(size_hint_in_dip,
app_info->icon_resource_id, std::move(callback)); quantize_to_supported_scale_factor),
app_info->package_name, app_info->activity,
app_info->icon_resource_id, std::move(callback));
AppConnectionHolder* app_connection_holder = AppConnectionHolder* app_connection_holder =
prefs->app_connection_holder(); prefs->app_connection_holder();
...@@ -544,7 +547,9 @@ void ArcApps::LoadPlayStoreIcon(apps::mojom::IconCompression icon_compression, ...@@ -544,7 +547,9 @@ void ArcApps::LoadPlayStoreIcon(apps::mojom::IconCompression icon_compression,
IconEffects icon_effects, IconEffects icon_effects,
LoadIconCallback callback) { LoadIconCallback callback) {
// Use overloaded Chrome icon for Play Store that is adapted to Chrome style. // Use overloaded Chrome icon for Play Store that is adapted to Chrome style.
int size_hint_in_px = apps_util::ConvertDipToPx(size_hint_in_dip); constexpr bool quantize_to_supported_scale_factor = false;
int size_hint_in_px = apps_util::ConvertDipToPx(
size_hint_in_dip, quantize_to_supported_scale_factor);
int resource_id = (size_hint_in_px <= 32) ? IDR_ARC_SUPPORT_ICON_32 int resource_id = (size_hint_in_px <= 32) ? IDR_ARC_SUPPORT_ICON_32
: IDR_ARC_SUPPORT_ICON_192; : IDR_ARC_SUPPORT_ICON_192;
constexpr bool is_placeholder_icon = false; constexpr bool is_placeholder_icon = false;
......
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
#include "chrome/browser/apps/app_service/dip_px_util.h" #include "chrome/browser/apps/app_service/dip_px_util.h"
#include <cmath>
#include "base/numerics/safe_conversions.h"
#include "ui/base/layout.h" #include "ui/base/layout.h"
#include "ui/display/display.h" #include "ui/display/display.h"
#include "ui/display/screen.h" #include "ui/display/screen.h"
#include "ui/gfx/geometry/size.h"
// TODO(crbug.com/826982): plumb through enough information to use one of // TODO(crbug.com/826982): plumb through enough information to use one of
// Screen::GetDisplayNearest{Window/View/Point}. That way in multi-monitor // Screen::GetDisplayNearest{Window/View/Point}. That way in multi-monitor
...@@ -26,18 +24,30 @@ float GetPrimaryDisplayScaleFactor() { ...@@ -26,18 +24,30 @@ float GetPrimaryDisplayScaleFactor() {
return screen->GetPrimaryDisplay().device_scale_factor(); return screen->GetPrimaryDisplay().device_scale_factor();
} }
int ConvertBetweenDipAndPx(int value,
bool quantize_to_supported_scale_factor,
bool invert) {
float scale = GetPrimaryDisplayScaleFactor();
if (quantize_to_supported_scale_factor) {
scale = ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(scale));
}
DCHECK_NE(0.0f, scale);
if (invert) {
scale = 1 / scale;
}
return gfx::ScaleToFlooredSize(gfx::Size(value, value), scale).width();
}
} // namespace } // namespace
namespace apps_util { namespace apps_util {
int ConvertDipToPx(int dip) { int ConvertDipToPx(int dip, bool quantize_to_supported_scale_factor) {
return base::saturated_cast<int>( return ConvertBetweenDipAndPx(dip, quantize_to_supported_scale_factor, false);
std::floor(static_cast<float>(dip) * GetPrimaryDisplayScaleFactor()));
} }
int ConvertPxToDip(int px) { int ConvertPxToDip(int px, bool quantize_to_supported_scale_factor) {
return base::saturated_cast<int>( return ConvertBetweenDipAndPx(px, quantize_to_supported_scale_factor, true);
std::floor(static_cast<float>(px) / GetPrimaryDisplayScaleFactor()));
} }
ui::ScaleFactor GetPrimaryDisplayUIScaleFactor() { ui::ScaleFactor GetPrimaryDisplayUIScaleFactor() {
......
...@@ -7,13 +7,17 @@ ...@@ -7,13 +7,17 @@
// Utility functions for converting between DIP (device independent pixels) and // Utility functions for converting between DIP (device independent pixels) and
// PX (physical pixels). // PX (physical pixels).
//
// "Supported scale factor" means a ui::ScaleFactor enum value (representing
// one of a finite number of floating point values) returned by
// ui::GetSupportedScaleFactor, defined in ui/base/layout.h.
#include "ui/base/resource/scale_factor.h" #include "ui/base/resource/scale_factor.h"
namespace apps_util { namespace apps_util {
int ConvertDipToPx(int dip); int ConvertDipToPx(int dip, bool quantize_to_supported_scale_factor);
int ConvertPxToDip(int px); int ConvertPxToDip(int px, bool quantize_to_supported_scale_factor);
ui::ScaleFactor GetPrimaryDisplayUIScaleFactor(); ui::ScaleFactor GetPrimaryDisplayUIScaleFactor();
} // namespace apps_util } // namespace apps_util
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment