layout.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/base/layout.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <limits>
  9. #include "base/check_op.h"
  10. #include "build/build_config.h"
  11. #include "ui/base/pointer/pointer_device.h"
  12. #include "ui/display/display.h"
  13. #include "ui/display/screen.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. namespace ui {
  16. namespace {
  17. std::vector<ResourceScaleFactor>* g_supported_resource_scale_factors = nullptr;
  18. } // namespace
  19. void SetSupportedResourceScaleFactors(
  20. const std::vector<ResourceScaleFactor>& scale_factors) {
  21. if (g_supported_resource_scale_factors != nullptr)
  22. delete g_supported_resource_scale_factors;
  23. g_supported_resource_scale_factors =
  24. new std::vector<ResourceScaleFactor>(scale_factors);
  25. std::sort(g_supported_resource_scale_factors->begin(),
  26. g_supported_resource_scale_factors->end(),
  27. [](ResourceScaleFactor lhs, ResourceScaleFactor rhs) {
  28. return GetScaleForResourceScaleFactor(lhs) <
  29. GetScaleForResourceScaleFactor(rhs);
  30. });
  31. // Set ImageSkia's supported scales.
  32. std::vector<float> scales;
  33. for (std::vector<ResourceScaleFactor>::const_iterator it =
  34. g_supported_resource_scale_factors->begin();
  35. it != g_supported_resource_scale_factors->end(); ++it) {
  36. scales.push_back(GetScaleForResourceScaleFactor(*it));
  37. }
  38. gfx::ImageSkia::SetSupportedScales(scales);
  39. }
  40. const std::vector<ResourceScaleFactor>& GetSupportedResourceScaleFactors() {
  41. DCHECK(g_supported_resource_scale_factors != nullptr);
  42. return *g_supported_resource_scale_factors;
  43. }
  44. ResourceScaleFactor GetSupportedResourceScaleFactor(float scale) {
  45. DCHECK(g_supported_resource_scale_factors != nullptr);
  46. ResourceScaleFactor closest_match = k100Percent;
  47. float smallest_diff = std::numeric_limits<float>::max();
  48. for (auto scale_factor : *g_supported_resource_scale_factors) {
  49. float diff = std::abs(GetScaleForResourceScaleFactor(scale_factor) - scale);
  50. if (diff < smallest_diff) {
  51. closest_match = scale_factor;
  52. smallest_diff = diff;
  53. }
  54. }
  55. DCHECK_NE(closest_match, kScaleFactorNone);
  56. return closest_match;
  57. }
  58. bool IsSupportedScale(float scale) {
  59. for (auto scale_factor_idx : *g_supported_resource_scale_factors) {
  60. if (GetScaleForResourceScaleFactor(scale_factor_idx) == scale)
  61. return true;
  62. }
  63. return false;
  64. }
  65. namespace test {
  66. ScopedSetSupportedResourceScaleFactors::ScopedSetSupportedResourceScaleFactors(
  67. const std::vector<ResourceScaleFactor>& new_scale_factors) {
  68. if (g_supported_resource_scale_factors) {
  69. original_scale_factors_ = new std::vector<ResourceScaleFactor>(
  70. *g_supported_resource_scale_factors);
  71. } else {
  72. original_scale_factors_ = nullptr;
  73. }
  74. SetSupportedResourceScaleFactors(new_scale_factors);
  75. }
  76. ScopedSetSupportedResourceScaleFactors::
  77. ~ScopedSetSupportedResourceScaleFactors() {
  78. if (original_scale_factors_) {
  79. SetSupportedResourceScaleFactors(*original_scale_factors_);
  80. delete original_scale_factors_;
  81. } else {
  82. delete g_supported_resource_scale_factors;
  83. g_supported_resource_scale_factors = nullptr;
  84. }
  85. }
  86. } // namespace test
  87. float GetScaleFactorForNativeView(gfx::NativeView view) {
  88. // A number of unit tests do not setup the screen.
  89. if (!display::Screen::GetScreen())
  90. return 1.0f;
  91. display::Display display =
  92. display::Screen::GetScreen()->GetDisplayNearestView(view);
  93. // GetDisplayNearestView() may return null Display if the |view| is not shown
  94. // on the screen and there is no primary display. In that case use scale
  95. // factor 1.0.
  96. if (!display.is_valid())
  97. return 1.0f;
  98. return display.device_scale_factor();
  99. }
  100. } // namespace ui