Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / base / layout.cc
blob367dde33be2c539c9ecc1ac23f5939f18c2f79ce
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.
5 #include "ui/base/layout.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "build/build_config.h"
15 #include "ui/base/ui_base_switches.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/screen.h"
19 #if defined(OS_MACOSX) && !defined(OS_IOS)
20 #include "base/mac/mac_util.h"
21 #endif
23 #if defined(OS_WIN)
24 #include "base/win/metro.h"
25 #include <Windows.h>
26 #endif // defined(OS_WIN)
28 #if defined(OS_CHROMEOS)
29 #include "ui/base/resource/resource_bundle.h"
30 #endif
32 namespace ui {
34 namespace {
36 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){
37 return GetScaleFactorScale(lhs) < GetScaleFactorScale(rhs);
40 #if defined(OS_WIN)
41 // Helper function that determines whether we want to optimize the UI for touch.
42 bool UseTouchOptimizedUI() {
43 // If --touch-optimized-ui is specified and not set to "auto", then override
44 // the hardware-determined setting (eg. for testing purposes).
45 static bool has_touch_optimized_ui = CommandLine::ForCurrentProcess()->
46 HasSwitch(switches::kTouchOptimizedUI);
47 if (has_touch_optimized_ui) {
48 const std::string switch_value = CommandLine::ForCurrentProcess()->
49 GetSwitchValueASCII(switches::kTouchOptimizedUI);
51 // Note that simply specifying the switch is the same as enabled.
52 if (switch_value.empty() ||
53 switch_value == switches::kTouchOptimizedUIEnabled) {
54 return true;
55 } else if (switch_value == switches::kTouchOptimizedUIDisabled) {
56 return false;
57 } else if (switch_value != switches::kTouchOptimizedUIAuto) {
58 LOG(ERROR) << "Invalid --touch-optimized-ui option: " << switch_value;
62 // We use the touch layout only when we are running in Metro mode.
63 return base::win::IsMetroProcess() && base::win::IsTouchEnabled();
65 #endif // defined(OS_WIN)
67 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.4f, 1.8f, 2.0f};
68 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales),
69 kScaleFactorScales_incorrect_size);
70 const size_t kScaleFactorScalesLength = arraysize(kScaleFactorScales);
72 std::vector<ScaleFactor>& GetSupportedScaleFactorsInternal() {
73 static std::vector<ScaleFactor>* supported_scale_factors =
74 new std::vector<ScaleFactor>();
75 if (supported_scale_factors->empty()) {
76 #if !defined(OS_IOS)
77 // On platforms other than iOS, 100P is always a supported scale factor.
78 supported_scale_factors->push_back(SCALE_FACTOR_100P);
79 #endif
81 #if defined(OS_IOS)
82 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
83 if (display.device_scale_factor() > 1.0) {
84 DCHECK_EQ(2.0, display.device_scale_factor());
85 supported_scale_factors->push_back(SCALE_FACTOR_200P);
86 } else {
87 supported_scale_factors->push_back(SCALE_FACTOR_100P);
89 #elif defined(OS_MACOSX)
90 if (base::mac::IsOSLionOrLater())
91 supported_scale_factors->push_back(SCALE_FACTOR_200P);
92 #elif defined(OS_WIN) && defined(ENABLE_HIDPI)
93 if (base::win::IsMetroProcess() && base::win::IsTouchEnabled()) {
94 supported_scale_factors->push_back(SCALE_FACTOR_140P);
95 supported_scale_factors->push_back(SCALE_FACTOR_180P);
97 #elif defined(OS_CHROMEOS)
98 // TODO(oshima): Include 200P only if the device support 200P
99 supported_scale_factors->push_back(SCALE_FACTOR_200P);
100 #endif
101 std::sort(supported_scale_factors->begin(),
102 supported_scale_factors->end(),
103 ScaleFactorComparator);
105 return *supported_scale_factors;
108 } // namespace
110 // Note that this function should be extended to select
111 // LAYOUT_TOUCH when appropriate on more platforms than just
112 // Windows.
113 DisplayLayout GetDisplayLayout() {
114 #if defined(USE_ASH)
115 return LAYOUT_ASH;
116 #elif defined(OS_WIN)
117 if (UseTouchOptimizedUI())
118 return LAYOUT_TOUCH;
119 return LAYOUT_DESKTOP;
120 #else
121 return LAYOUT_DESKTOP;
122 #endif
125 ScaleFactor GetScaleFactorFromScale(float scale) {
126 ScaleFactor closest_match = SCALE_FACTOR_100P;
127 float smallest_diff = std::numeric_limits<float>::max();
128 const std::vector<ScaleFactor>& supported =
129 GetSupportedScaleFactorsInternal();
130 for (size_t i = 0; i < supported.size(); ++i) {
131 ScaleFactor scale_factor = supported[i];
132 float diff = std::abs(kScaleFactorScales[scale_factor] - scale);
133 if (diff < smallest_diff) {
134 closest_match = scale_factor;
135 smallest_diff = diff;
138 DCHECK_NE(closest_match, SCALE_FACTOR_NONE);
139 return closest_match;
142 float GetScaleFactorScale(ScaleFactor scale_factor) {
143 return kScaleFactorScales[scale_factor];
146 ScaleFactor GetMaxScaleFactor() {
147 #if defined(OS_CHROMEOS)
148 return ResourceBundle::GetSharedInstance().max_scale_factor();
149 #else
150 return GetSupportedScaleFactorsInternal().back();
151 #endif
154 std::vector<ScaleFactor> GetSupportedScaleFactors() {
155 return GetSupportedScaleFactorsInternal();
158 bool IsScaleFactorSupported(ScaleFactor scale_factor) {
159 const std::vector<ScaleFactor>& supported =
160 GetSupportedScaleFactorsInternal();
161 return std::find(supported.begin(), supported.end(), scale_factor) !=
162 supported.end();
165 namespace test {
167 void SetSupportedScaleFactors(
168 const std::vector<ui::ScaleFactor>& scale_factors) {
169 std::vector<ui::ScaleFactor>& supported_scale_factors =
170 GetSupportedScaleFactorsInternal();
171 supported_scale_factors = scale_factors;
172 std::sort(supported_scale_factors.begin(),
173 supported_scale_factors.end(),
174 ScaleFactorComparator);
177 } // namespace test
179 #if !defined(OS_MACOSX)
180 ScaleFactor GetScaleFactorForNativeView(gfx::NativeView view) {
181 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
182 if (screen->IsDIPEnabled()) {
183 gfx::Display display = screen->GetDisplayNearestWindow(view);
184 return GetScaleFactorFromScale(display.device_scale_factor());
186 return ui::SCALE_FACTOR_100P;
188 #endif // !defined(OS_MACOSX)
190 } // namespace ui