OLD | NEW |
| (Empty) |
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 | |
5 #include "ui/base/layout.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 #include <limits> | |
10 | |
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/touch/touch_device.h" | |
16 #include "ui/base/ui_base_switches.h" | |
17 #include "ui/gfx/display.h" | |
18 #include "ui/gfx/image/image_skia.h" | |
19 #include "ui/gfx/screen.h" | |
20 | |
21 #if defined(OS_WIN) | |
22 #include "base/win/metro.h" | |
23 #include "ui/gfx/win/dpi.h" | |
24 #include <Windows.h> | |
25 #endif // defined(OS_WIN) | |
26 | |
27 namespace ui { | |
28 | |
29 namespace { | |
30 | |
31 bool ScaleFactorComparator(const ScaleFactor& lhs, const ScaleFactor& rhs){ | |
32 return GetScaleForScaleFactor(lhs) < GetScaleForScaleFactor(rhs); | |
33 } | |
34 | |
35 std::vector<ScaleFactor>* g_supported_scale_factors = NULL; | |
36 | |
37 const float kScaleFactorScales[] = {1.0f, 1.0f, 1.25f, 1.33f, 1.4f, 1.5f, 1.8f, | |
38 2.0f, 2.5f, 3.0f}; | |
39 COMPILE_ASSERT(NUM_SCALE_FACTORS == arraysize(kScaleFactorScales), | |
40 kScaleFactorScales_incorrect_size); | |
41 | |
42 } // namespace | |
43 | |
44 void SetSupportedScaleFactors( | |
45 const std::vector<ui::ScaleFactor>& scale_factors) { | |
46 if (g_supported_scale_factors != NULL) | |
47 delete g_supported_scale_factors; | |
48 | |
49 g_supported_scale_factors = new std::vector<ScaleFactor>(scale_factors); | |
50 std::sort(g_supported_scale_factors->begin(), | |
51 g_supported_scale_factors->end(), | |
52 ScaleFactorComparator); | |
53 | |
54 // Set ImageSkia's supported scales. | |
55 std::vector<float> scales; | |
56 for (std::vector<ScaleFactor>::const_iterator it = | |
57 g_supported_scale_factors->begin(); | |
58 it != g_supported_scale_factors->end(); ++it) { | |
59 scales.push_back(kScaleFactorScales[*it]); | |
60 } | |
61 gfx::ImageSkia::SetSupportedScales(scales); | |
62 } | |
63 | |
64 const std::vector<ScaleFactor>& GetSupportedScaleFactors() { | |
65 DCHECK(g_supported_scale_factors != NULL); | |
66 return *g_supported_scale_factors; | |
67 } | |
68 | |
69 ScaleFactor GetSupportedScaleFactor(float scale) { | |
70 DCHECK(g_supported_scale_factors != NULL); | |
71 ScaleFactor closest_match = SCALE_FACTOR_100P; | |
72 float smallest_diff = std::numeric_limits<float>::max(); | |
73 for (size_t i = 0; i < g_supported_scale_factors->size(); ++i) { | |
74 ScaleFactor scale_factor = (*g_supported_scale_factors)[i]; | |
75 float diff = std::abs(kScaleFactorScales[scale_factor] - scale); | |
76 if (diff < smallest_diff) { | |
77 closest_match = scale_factor; | |
78 smallest_diff = diff; | |
79 } | |
80 } | |
81 DCHECK_NE(closest_match, SCALE_FACTOR_NONE); | |
82 return closest_match; | |
83 } | |
84 | |
85 float GetImageScale(ScaleFactor scale_factor) { | |
86 #if defined(OS_WIN) | |
87 if (gfx::IsHighDPIEnabled()) | |
88 return gfx::win::GetDeviceScaleFactor(); | |
89 #endif | |
90 return GetScaleForScaleFactor(scale_factor); | |
91 } | |
92 | |
93 float GetScaleForScaleFactor(ScaleFactor scale_factor) { | |
94 return kScaleFactorScales[scale_factor]; | |
95 } | |
96 | |
97 namespace test { | |
98 | |
99 ScopedSetSupportedScaleFactors::ScopedSetSupportedScaleFactors( | |
100 const std::vector<ui::ScaleFactor>& new_scale_factors) { | |
101 if (g_supported_scale_factors) { | |
102 original_scale_factors_ = | |
103 new std::vector<ScaleFactor>(*g_supported_scale_factors); | |
104 } else { | |
105 original_scale_factors_ = NULL; | |
106 } | |
107 SetSupportedScaleFactors(new_scale_factors); | |
108 } | |
109 | |
110 ScopedSetSupportedScaleFactors::~ScopedSetSupportedScaleFactors() { | |
111 if (original_scale_factors_) { | |
112 SetSupportedScaleFactors(*original_scale_factors_); | |
113 delete original_scale_factors_; | |
114 } else { | |
115 delete g_supported_scale_factors; | |
116 g_supported_scale_factors = NULL; | |
117 } | |
118 } | |
119 | |
120 } // namespace test | |
121 | |
122 #if !defined(OS_MACOSX) | |
123 float GetScaleFactorForNativeView(gfx::NativeView view) { | |
124 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); | |
125 if (screen->IsDIPEnabled()) { | |
126 gfx::Display display = screen->GetDisplayNearestWindow(view); | |
127 return display.device_scale_factor(); | |
128 } | |
129 return 1.0f; | |
130 } | |
131 #endif // !defined(OS_MACOSX) | |
132 | |
133 } // namespace ui | |
OLD | NEW |