OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/compositor/dip_util.h" | 5 #include "ui/compositor/dip_util.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "ui/base/ui_base_switches.h" | 8 #include "ui/base/ui_base_switches.h" |
9 #include "ui/compositor/compositor.h" | 9 #include "ui/compositor/compositor.h" |
10 #include "ui/compositor/compositor_switches.h" | 10 #include "ui/compositor/compositor_switches.h" |
11 #include "ui/compositor/layer.h" | 11 #include "ui/compositor/layer.h" |
12 #include "ui/gfx/monitor.h" | 12 #include "ui/gfx/monitor.h" |
13 #include "ui/gfx/point.h" | 13 #include "ui/gfx/point.h" |
14 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 | 16 |
17 namespace ui { | 17 namespace ui { |
18 namespace { | |
19 bool dip_enabled_for_test = false; | |
20 } // namespace | |
21 | |
22 namespace test { | |
23 | |
24 ScopedDIPEnablerForTest::ScopedDIPEnablerForTest() { | |
25 CHECK(!dip_enabled_for_test); | |
26 dip_enabled_for_test = true; | |
27 } | |
28 | |
29 ScopedDIPEnablerForTest::~ScopedDIPEnablerForTest() { | |
30 dip_enabled_for_test = false; | |
31 } | |
32 | |
33 } // namespace test | |
34 | |
35 bool IsDIPEnabled() { | |
36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
37 static const bool dip_enabled = | |
38 command_line.HasSwitch(switches::kDefaultDeviceScaleFactor) || | |
39 (command_line.HasSwitch(switches::kUIEnableDIP) && | |
40 command_line.GetSwitchValueASCII(switches::kUIEnableDIP) != "false"); | |
41 return dip_enabled || dip_enabled_for_test; | |
42 } | |
43 | 18 |
44 float GetDeviceScaleFactor(const Layer* layer) { | 19 float GetDeviceScaleFactor(const Layer* layer) { |
45 if (!IsDIPEnabled()) | |
46 return 1.0f; | |
47 return layer->device_scale_factor(); | 20 return layer->device_scale_factor(); |
48 } | 21 } |
49 | 22 |
50 gfx::Point ConvertPointToDIP(const Layer* layer, | 23 gfx::Point ConvertPointToDIP(const Layer* layer, |
51 const gfx::Point& point_in_pixel) { | 24 const gfx::Point& point_in_pixel) { |
52 if (IsDIPEnabled()) | 25 return point_in_pixel.Scale(1.0f / GetDeviceScaleFactor(layer)); |
53 return point_in_pixel.Scale(1.0f / GetDeviceScaleFactor(layer)); | |
54 else | |
55 return point_in_pixel; | |
56 } | 26 } |
57 | 27 |
58 gfx::Size ConvertSizeToDIP(const Layer* layer, | 28 gfx::Size ConvertSizeToDIP(const Layer* layer, |
59 const gfx::Size& size_in_pixel) { | 29 const gfx::Size& size_in_pixel) { |
60 if (IsDIPEnabled()) | 30 return size_in_pixel.Scale(1.0f / GetDeviceScaleFactor(layer)); |
61 return size_in_pixel.Scale(1.0f / GetDeviceScaleFactor(layer)); | |
62 else | |
63 return size_in_pixel; | |
64 } | 31 } |
65 | 32 |
66 gfx::Rect ConvertRectToDIP(const Layer* layer, | 33 gfx::Rect ConvertRectToDIP(const Layer* layer, |
67 const gfx::Rect& rect_in_pixel) { | 34 const gfx::Rect& rect_in_pixel) { |
68 if (IsDIPEnabled()) { | 35 float scale = 1.0f / GetDeviceScaleFactor(layer); |
69 float scale = 1.0f / GetDeviceScaleFactor(layer); | 36 return gfx::Rect(rect_in_pixel.origin().Scale(scale), |
70 return gfx::Rect(rect_in_pixel.origin().Scale(scale), | 37 rect_in_pixel.size().Scale(scale)); |
71 rect_in_pixel.size().Scale(scale)); | |
72 } else { | |
73 return rect_in_pixel; | |
74 } | |
75 } | 38 } |
76 | 39 |
77 gfx::Point ConvertPointToPixel(const Layer* layer, | 40 gfx::Point ConvertPointToPixel(const Layer* layer, |
78 const gfx::Point& point_in_dip) { | 41 const gfx::Point& point_in_dip) { |
79 if (IsDIPEnabled()) { | 42 return point_in_dip.Scale(GetDeviceScaleFactor(layer)); |
80 return point_in_dip.Scale(GetDeviceScaleFactor(layer)); | |
81 } else { | |
82 return point_in_dip; | |
83 } | |
84 } | 43 } |
85 | 44 |
86 gfx::Size ConvertSizeToPixel(const Layer* layer, | 45 gfx::Size ConvertSizeToPixel(const Layer* layer, |
87 const gfx::Size& size_in_dip) { | 46 const gfx::Size& size_in_dip) { |
88 if (IsDIPEnabled()) { | 47 return size_in_dip.Scale(GetDeviceScaleFactor(layer)); |
89 return size_in_dip.Scale(GetDeviceScaleFactor(layer)); | |
90 } else { | |
91 return size_in_dip; | |
92 } | |
93 } | 48 } |
94 | 49 |
95 gfx::Rect ConvertRectToPixel(const Layer* layer, | 50 gfx::Rect ConvertRectToPixel(const Layer* layer, |
96 const gfx::Rect& rect_in_dip) { | 51 const gfx::Rect& rect_in_dip) { |
97 if (IsDIPEnabled()) { | 52 float scale = GetDeviceScaleFactor(layer); |
98 float scale = GetDeviceScaleFactor(layer); | 53 return gfx::Rect(rect_in_dip.origin().Scale(scale), |
99 return gfx::Rect(rect_in_dip.origin().Scale(scale), | 54 rect_in_dip.size().Scale(scale)); |
100 rect_in_dip.size().Scale(scale)); | |
101 } else { | |
102 return rect_in_dip; | |
103 } | |
104 } | 55 } |
105 } // namespace ui | 56 } // namespace ui |
OLD | NEW |