Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / display / root_window_transformers.cc
blobfae4eec7ac2c7d5748088caa364347c1f66edc9b
1 // Copyright (c) 2013 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 "ash/display/root_window_transformers.h"
7 #include <cmath>
9 #include "ash/ash_switches.h"
10 #include "ash/display/display_info.h"
11 #include "ash/display/display_manager.h"
12 #include "ash/host/root_window_transformer.h"
13 #include "ash/magnifier/magnification_controller.h"
14 #include "ash/shell.h"
15 #include "base/basictypes.h"
16 #include "base/command_line.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "third_party/skia/include/utils/SkMatrix44.h"
19 #include "ui/aura/window_event_dispatcher.h"
20 #include "ui/aura/window_property.h"
21 #include "ui/compositor/dip_util.h"
22 #include "ui/gfx/display.h"
23 #include "ui/gfx/geometry/insets.h"
24 #include "ui/gfx/geometry/size_conversions.h"
25 #include "ui/gfx/transform.h"
26 #include "ui/gfx/transform.h"
28 DECLARE_WINDOW_PROPERTY_TYPE(gfx::Display::Rotation);
30 namespace ash {
31 namespace {
33 #if defined(OS_WIN)
34 DEFINE_WINDOW_PROPERTY_KEY(gfx::Display::Rotation, kRotationPropertyKey,
35 gfx::Display::ROTATE_0);
36 #endif
38 // Round near zero value to zero.
39 void RoundNearZero(gfx::Transform* transform) {
40 const float kEpsilon = 0.001f;
41 SkMatrix44& matrix = transform->matrix();
42 for (int x = 0; x < 4; ++x) {
43 for (int y = 0; y < 4; ++y) {
44 if (std::abs(SkMScalarToFloat(matrix.get(x, y))) < kEpsilon)
45 matrix.set(x, y, SkFloatToMScalar(0.0f));
50 // TODO(oshima): Transformers should be able to adjust itself
51 // when the device scale factor is changed, instead of
52 // precalculating the transform using fixed value.
54 gfx::Transform CreateRotationTransform(aura::Window* root_window,
55 const gfx::Display& display) {
56 DisplayInfo info =
57 Shell::GetInstance()->display_manager()->GetDisplayInfo(display.id());
59 // TODO(oshima): Add animation. (crossfade+rotation, or just cross-fade)
60 #if defined(OS_WIN)
61 // Windows 8 bots refused to resize the host window, and
62 // updating the transform results in incorrectly resizing
63 // the root window. Don't apply the transform unless
64 // necessary so that unit tests pass on win8 bots.
65 if (info.GetActiveRotation() ==
66 root_window->GetProperty(kRotationPropertyKey)) {
67 return gfx::Transform();
69 root_window->SetProperty(kRotationPropertyKey, info.GetActiveRotation());
70 #endif
72 gfx::Transform rotate;
73 // The origin is (0, 0), so the translate width/height must be reduced by
74 // 1 pixel.
75 float one_pixel = 1.0f / display.device_scale_factor();
76 switch (info.GetActiveRotation()) {
77 case gfx::Display::ROTATE_0:
78 break;
79 case gfx::Display::ROTATE_90:
80 rotate.Translate(display.bounds().height() - one_pixel, 0);
81 rotate.Rotate(90);
82 break;
83 case gfx::Display::ROTATE_270:
84 rotate.Translate(0, display.bounds().width() - one_pixel);
85 rotate.Rotate(270);
86 break;
87 case gfx::Display::ROTATE_180:
88 rotate.Translate(display.bounds().width() - one_pixel,
89 display.bounds().height() - one_pixel);
90 rotate.Rotate(180);
91 break;
94 RoundNearZero(&rotate);
95 return rotate;
98 gfx::Transform CreateMagnifierTransform(aura::Window* root_window) {
99 MagnificationController* magnifier =
100 Shell::GetInstance()->magnification_controller();
101 float magnifier_scale = 1.f;
102 gfx::Point magnifier_offset;
103 if (magnifier && magnifier->IsEnabled()) {
104 magnifier_scale = magnifier->GetScale();
105 magnifier_offset = magnifier->GetWindowPosition();
107 gfx::Transform transform;
108 if (magnifier_scale != 1.f) {
109 transform.Scale(magnifier_scale, magnifier_scale);
110 transform.Translate(-magnifier_offset.x(), -magnifier_offset.y());
112 return transform;
115 gfx::Transform CreateInsetsAndScaleTransform(const gfx::Insets& insets,
116 float device_scale_factor,
117 float ui_scale) {
118 gfx::Transform transform;
119 if (insets.top() != 0 || insets.left() != 0) {
120 float x_offset = insets.left() / device_scale_factor;
121 float y_offset = insets.top() / device_scale_factor;
122 transform.Translate(x_offset, y_offset);
124 float inverted_scale = 1.0f / ui_scale;
125 transform.Scale(inverted_scale, inverted_scale);
126 return transform;
129 gfx::Transform CreateMirrorTransform(const gfx::Display& display) {
130 gfx::Transform transform;
131 transform.matrix().set3x3(-1, 0, 0,
132 0, 1, 0,
133 0, 0, 1);
134 transform.Translate(-display.size().width(), 0);
135 return transform;
138 // RootWindowTransformer for ash environment.
139 class AshRootWindowTransformer : public RootWindowTransformer {
140 public:
141 AshRootWindowTransformer(aura::Window* root,
142 const gfx::Display& display)
143 : root_window_(root) {
144 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
145 DisplayInfo info = display_manager->GetDisplayInfo(display.id());
146 host_insets_ = info.GetOverscanInsetsInPixel();
147 root_window_ui_scale_ = info.GetEffectiveUIScale();
148 root_window_bounds_transform_ =
149 CreateInsetsAndScaleTransform(host_insets_,
150 display.device_scale_factor(),
151 root_window_ui_scale_) *
152 CreateRotationTransform(root, display);
153 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
154 switches::kAshEnableMirroredScreen)) {
155 // Apply the tranform that flips the screen image horizontally so that
156 // the screen looks normal when reflected on a mirror.
157 root_window_bounds_transform_ =
158 root_window_bounds_transform_ * CreateMirrorTransform(display);
160 transform_ = root_window_bounds_transform_ * CreateMagnifierTransform(root);
162 CHECK(transform_.GetInverse(&invert_transform_));
165 // aura::RootWindowTransformer overrides:
166 gfx::Transform GetTransform() const override { return transform_; }
167 gfx::Transform GetInverseTransform() const override {
168 return invert_transform_;
170 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
171 gfx::Rect bounds(host_size);
172 bounds.Inset(host_insets_);
173 bounds = ui::ConvertRectToDIP(root_window_->layer(), bounds);
174 gfx::RectF new_bounds(bounds);
175 root_window_bounds_transform_.TransformRect(&new_bounds);
176 // Apply |root_window_scale_| twice as the downscaling
177 // is already applied once in |SetTransformInternal()|.
178 // TODO(oshima): This is a bit ugly. Consider specifying
179 // the pseudo host resolution instead.
180 new_bounds.Scale(root_window_ui_scale_ * root_window_ui_scale_);
181 // Ignore the origin because RootWindow's insets are handled by
182 // the transform.
183 // Floor the size because the bounds is no longer aligned to
184 // backing pixel when |root_window_scale_| is specified
185 // (850 height at 1.25 scale becomes 1062.5 for example.)
186 return gfx::Rect(gfx::ToFlooredSize(new_bounds.size()));
189 gfx::Insets GetHostInsets() const override { return host_insets_; }
191 private:
192 ~AshRootWindowTransformer() override {}
194 aura::Window* root_window_;
195 gfx::Transform transform_;
197 // The accurate representation of the inverse of the |transform_|.
198 // This is used to avoid computation error caused by
199 // |gfx::Transform::GetInverse|.
200 gfx::Transform invert_transform_;
202 // The transform of the root window bounds. This is used to calculate
203 // the size of root window.
204 gfx::Transform root_window_bounds_transform_;
206 // The scale of the root window. See |display_info::ui_scale_|
207 // for more info.
208 float root_window_ui_scale_;
210 gfx::Insets host_insets_;
212 DISALLOW_COPY_AND_ASSIGN(AshRootWindowTransformer);
215 // RootWindowTransformer for mirror root window. We simply copy the
216 // texture (bitmap) of the source display into the mirror window, so
217 // the root window bounds is the same as the source display's
218 // pixel size (excluding overscan insets).
219 class MirrorRootWindowTransformer : public RootWindowTransformer {
220 public:
221 MirrorRootWindowTransformer(const DisplayInfo& source_display_info,
222 const DisplayInfo& mirror_display_info) {
223 root_bounds_ = gfx::Rect(source_display_info.bounds_in_native().size());
224 gfx::Rect mirror_display_rect =
225 gfx::Rect(mirror_display_info.bounds_in_native().size());
227 bool letterbox = root_bounds_.width() * mirror_display_rect.height() >
228 root_bounds_.height() * mirror_display_rect.width();
229 if (letterbox) {
230 float mirror_scale_ratio =
231 (static_cast<float>(root_bounds_.width()) /
232 static_cast<float>(mirror_display_rect.width()));
233 float inverted_scale = 1.0f / mirror_scale_ratio;
234 int margin = static_cast<int>(
235 (mirror_display_rect.height() -
236 root_bounds_.height() * inverted_scale) / 2);
237 insets_.Set(0, margin, 0, margin);
239 transform_.Translate(0, margin);
240 transform_.Scale(inverted_scale, inverted_scale);
241 } else {
242 float mirror_scale_ratio =
243 (static_cast<float>(root_bounds_.height()) /
244 static_cast<float>(mirror_display_rect.height()));
245 float inverted_scale = 1.0f / mirror_scale_ratio;
246 int margin = static_cast<int>(
247 (mirror_display_rect.width() -
248 root_bounds_.width() * inverted_scale) / 2);
249 insets_.Set(margin, 0, margin, 0);
251 transform_.Translate(margin, 0);
252 transform_.Scale(inverted_scale, inverted_scale);
256 // aura::RootWindowTransformer overrides:
257 gfx::Transform GetTransform() const override { return transform_; }
258 gfx::Transform GetInverseTransform() const override {
259 gfx::Transform invert;
260 CHECK(transform_.GetInverse(&invert));
261 return invert;
263 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
264 return root_bounds_;
266 gfx::Insets GetHostInsets() const override { return insets_; }
268 private:
269 ~MirrorRootWindowTransformer() override {}
271 gfx::Transform transform_;
272 gfx::Rect root_bounds_;
273 gfx::Insets insets_;
275 DISALLOW_COPY_AND_ASSIGN(MirrorRootWindowTransformer);
278 class PartialBoundsRootWindowTransformer : public RootWindowTransformer {
279 public:
280 PartialBoundsRootWindowTransformer(const gfx::Rect& screen_bounds,
281 const gfx::Display& display) {
282 gfx::Display unified_display =
283 Shell::GetInstance()->GetScreen()->GetPrimaryDisplay();
284 DisplayInfo display_info =
285 Shell::GetInstance()->display_manager()->GetDisplayInfo(display.id());
286 root_bounds_ = gfx::Rect(display_info.bounds_in_native().size());
287 float scale = root_bounds_.height() /
288 static_cast<float>(screen_bounds.height()) /
289 unified_display.device_scale_factor();
290 transform_.Scale(scale, scale);
291 transform_.Translate(-SkIntToMScalar(display.bounds().x()),
292 -SkIntToMScalar(display.bounds().y()));
295 // RootWindowTransformer:
296 gfx::Transform GetTransform() const override { return transform_; }
297 gfx::Transform GetInverseTransform() const override {
298 gfx::Transform invert;
299 CHECK(transform_.GetInverse(&invert));
300 return invert;
302 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
303 return root_bounds_;
305 gfx::Insets GetHostInsets() const override { return gfx::Insets(); }
307 private:
308 gfx::Transform transform_;
309 gfx::Rect root_bounds_;
311 DISALLOW_COPY_AND_ASSIGN(PartialBoundsRootWindowTransformer);
314 } // namespace
316 RootWindowTransformer* CreateRootWindowTransformerForDisplay(
317 aura::Window* root,
318 const gfx::Display& display) {
319 return new AshRootWindowTransformer(root, display);
322 RootWindowTransformer* CreateRootWindowTransformerForMirroredDisplay(
323 const DisplayInfo& source_display_info,
324 const DisplayInfo& mirror_display_info) {
325 return new MirrorRootWindowTransformer(source_display_info,
326 mirror_display_info);
329 RootWindowTransformer* CreateRootWindowTransformerForUnifiedDesktop(
330 const gfx::Rect& screen_bounds,
331 const gfx::Display& display) {
332 return new PartialBoundsRootWindowTransformer(screen_bounds, display);
335 } // namespace ash