root_window_transformers.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#include "ash/display/root_window_transformers.h"
6
7#include <cmath>
8
9#include "ash/display/display_info.h"
10#include "ash/display/display_manager.h"
11#include "ash/magnifier/magnification_controller.h"
12#include "ash/shell.h"
13#include "base/basictypes.h"
14#include "base/memory/scoped_ptr.h"
15#include "third_party/skia/include/utils/SkMatrix44.h"
16#include "ui/aura/root_window.h"
17#include "ui/aura/root_window_transformer.h"
18#include "ui/aura/window_property.h"
19#include "ui/compositor/dip_util.h"
20#include "ui/gfx/display.h"
21#include "ui/gfx/insets.h"
22#include "ui/gfx/size_conversions.h"
23#include "ui/gfx/transform.h"
24#include "ui/gfx/transform.h"
25
26DECLARE_WINDOW_PROPERTY_TYPE(gfx::Display::Rotation);
27
28namespace ash {
29namespace internal {
30namespace {
31
32#if defined(OS_WIN)
33DEFINE_WINDOW_PROPERTY_KEY(gfx::Display::Rotation, kRotationPropertyKey,
34                           gfx::Display::ROTATE_0);
35#endif
36
37// Round near zero value to zero.
38void RoundNearZero(gfx::Transform* transform) {
39  const float kEpsilon = 0.001f;
40  SkMatrix44& matrix = transform->matrix();
41  for (int x = 0; x < 4; ++x) {
42    for (int y = 0; y < 4; ++y) {
43      if (std::abs(SkMScalarToFloat(matrix.get(x, y))) < kEpsilon)
44        matrix.set(x, y, SkFloatToMScalar(0.0f));
45    }
46  }
47}
48
49// TODO(oshima): Transformers should be able to adjust itself
50// when the device scale factor is changed, instead of
51// precalculating the transform using fixed value.
52
53gfx::Transform CreateRotationTransform(aura::Window* root_window,
54                                       const gfx::Display& display) {
55  DisplayInfo info =
56      Shell::GetInstance()->display_manager()->GetDisplayInfo(display.id());
57
58  // TODO(oshima): Add animation. (crossfade+rotation, or just cross-fade)
59#if defined(OS_WIN)
60  // Windows 8 bots refused to resize the host window, and
61  // updating the transform results in incorrectly resizing
62  // the root window. Don't apply the transform unless
63  // necessary so that unit tests pass on win8 bots.
64  if (info.rotation() == root_window->GetProperty(kRotationPropertyKey))
65    return gfx::Transform();
66  root_window->SetProperty(kRotationPropertyKey, info.rotation());
67#endif
68
69  gfx::Transform rotate;
70  // The origin is (0, 0), so the translate width/height must be reduced by
71  // 1 pixel.
72  float one_pixel = 1.0f / display.device_scale_factor();
73  switch (info.rotation()) {
74    case gfx::Display::ROTATE_0:
75      break;
76    case gfx::Display::ROTATE_90:
77      rotate.Translate(display.bounds().height() - one_pixel, 0);
78      rotate.Rotate(90);
79      break;
80    case gfx::Display::ROTATE_270:
81      rotate.Translate(0, display.bounds().width() - one_pixel);
82      rotate.Rotate(270);
83      break;
84    case gfx::Display::ROTATE_180:
85      rotate.Translate(display.bounds().width() - one_pixel,
86                       display.bounds().height() - one_pixel);
87      rotate.Rotate(180);
88      break;
89  }
90
91  RoundNearZero(&rotate);
92  return rotate;
93}
94
95gfx::Transform CreateMagnifierTransform(aura::Window* root_window) {
96  MagnificationController* magnifier =
97      Shell::GetInstance()->magnification_controller();
98  float magnifier_scale = 1.f;
99  gfx::Point magnifier_offset;
100  if (magnifier && magnifier->IsEnabled()) {
101    magnifier_scale = magnifier->GetScale();
102    magnifier_offset = magnifier->GetWindowPosition();
103  }
104  gfx::Transform transform;
105  if (magnifier_scale != 1.f) {
106    transform.Scale(magnifier_scale, magnifier_scale);
107    transform.Translate(-magnifier_offset.x(), -magnifier_offset.y());
108  }
109  return transform;
110}
111
112gfx::Transform CreateInsetsAndScaleTransform(const gfx::Insets& insets,
113                                             float device_scale_factor,
114                                             float ui_scale) {
115  gfx::Transform transform;
116  if (insets.top() != 0 || insets.left() != 0) {
117    float x_offset = insets.left() / device_scale_factor;
118    float y_offset = insets.top() / device_scale_factor;
119    transform.Translate(x_offset, y_offset);
120  }
121  float inverted_scale = 1.0f / ui_scale;
122  transform.Scale(inverted_scale, inverted_scale);
123  return transform;
124}
125
126gfx::Transform CreateOverscanAndUIScaleTransform(aura::Window* root_window,
127                                                 const gfx::Display& display) {
128  DisplayInfo info =
129      Shell::GetInstance()->display_manager()->GetDisplayInfo(display.id());
130  return CreateInsetsAndScaleTransform(
131      info.GetOverscanInsetsInPixel(),
132      ui::GetDeviceScaleFactor(root_window->layer()),
133      info.ui_scale());
134}
135
136// RootWindowTransformer for ash environment.
137class AshRootWindowTransformer : public aura::RootWindowTransformer {
138 public:
139  AshRootWindowTransformer(aura::Window* root,
140                           const gfx::Display& display)
141      : root_window_(root) {
142    root_window_bounds_transform_ =
143        CreateOverscanAndUIScaleTransform(root, display) *
144        CreateRotationTransform(root, display);
145    transform_ = root_window_bounds_transform_ * CreateMagnifierTransform(root);
146    CHECK(transform_.GetInverse(&invert_transform_));
147
148    DisplayInfo info = Shell::GetInstance()->display_manager()->
149        GetDisplayInfo(display.id());
150    root_window_ui_scale_ = info.ui_scale();
151    host_insets_ = info.GetOverscanInsetsInPixel();
152  }
153
154  // aura::RootWindowTransformer overrides:
155  virtual gfx::Transform GetTransform() const OVERRIDE {
156    return transform_;
157  }
158  virtual gfx::Transform GetInverseTransform() const OVERRIDE {
159    return invert_transform_;
160  }
161  virtual gfx::Rect GetRootWindowBounds(
162      const gfx::Size& host_size) const OVERRIDE {
163    gfx::Rect bounds(host_size);
164    bounds.Inset(host_insets_);
165    bounds = ui::ConvertRectToDIP(root_window_->layer(), bounds);
166    gfx::RectF new_bounds(bounds);
167    root_window_bounds_transform_.TransformRect(&new_bounds);
168    // Apply |root_window_scale_| twice as the downscaling
169    // is already applied once in |SetTransformInternal()|.
170    // TODO(oshima): This is a bit ugly. Consider specifying
171    // the pseudo host resolution instead.
172    new_bounds.Scale(root_window_ui_scale_ * root_window_ui_scale_);
173    // Ignore the origin because RootWindow's insets are handled by
174    // the transform.
175    // Floor the size because the bounds is no longer aligned to
176    // backing pixel when |root_window_scale_| is specified
177    // (850 height at 1.25 scale becomes 1062.5 for example.)
178    return gfx::Rect(gfx::ToFlooredSize(new_bounds.size()));
179  }
180
181  virtual gfx::Insets GetHostInsets() const OVERRIDE {
182    return host_insets_;
183  }
184
185 private:
186  virtual ~AshRootWindowTransformer() {}
187
188  aura::Window* root_window_;
189  gfx::Transform transform_;
190
191  // The accurate representation of the inverse of the |transform_|.
192  // This is used to avoid computation error caused by
193  // |gfx::Transform::GetInverse|.
194  gfx::Transform invert_transform_;
195
196  // The transform of the root window bounds. This is used to calculate
197  // the size of root window.
198  gfx::Transform root_window_bounds_transform_;
199
200  // The scale of the root window. See |display_info::ui_scale_|
201  // for more info.
202  float root_window_ui_scale_;
203
204  gfx::Insets host_insets_;
205
206  DISALLOW_COPY_AND_ASSIGN(AshRootWindowTransformer);
207};
208
209// RootWindowTransformer for mirror root window. We simply copy the
210// texture (bitmap) of the source display into the mirror window, so
211// the root window bounds is the same as the source display's
212// pixel size (excluding overscan insets).
213class MirrorRootWindowTransformer : public aura::RootWindowTransformer {
214 public:
215  MirrorRootWindowTransformer(const DisplayInfo& source_display_info,
216                              const DisplayInfo& mirror_display_info) {
217    root_bounds_ = gfx::Rect(source_display_info.bounds_in_native().size());
218    gfx::Rect mirror_display_rect =
219        gfx::Rect(mirror_display_info.bounds_in_native().size());
220
221    bool letterbox = root_bounds_.width() * mirror_display_rect.height() >
222        root_bounds_.height() * mirror_display_rect.width();
223    if (letterbox) {
224      float mirror_scale_ratio =
225          (static_cast<float>(root_bounds_.width()) /
226           static_cast<float>(mirror_display_rect.width()));
227      float inverted_scale = 1.0f / mirror_scale_ratio;
228      int margin = static_cast<int>(
229          (mirror_display_rect.height() -
230           root_bounds_.height() * inverted_scale) / 2);
231      insets_.Set(0, margin, 0, margin);
232
233      transform_.Translate(0,  margin);
234      transform_.Scale(inverted_scale, inverted_scale);
235    } else {
236      float mirror_scale_ratio =
237          (static_cast<float>(root_bounds_.height()) /
238           static_cast<float>(mirror_display_rect.height()));
239      float inverted_scale = 1.0f / mirror_scale_ratio;
240      int margin = static_cast<int>(
241          (mirror_display_rect.width() -
242           root_bounds_.width() * inverted_scale) / 2);
243      insets_.Set(margin, 0, margin, 0);
244
245      transform_.Translate(margin, 0);
246      transform_.Scale(inverted_scale, inverted_scale);
247    }
248  }
249
250  // aura::RootWindowTransformer overrides:
251  virtual gfx::Transform GetTransform() const OVERRIDE {
252    return transform_;
253  }
254  virtual gfx::Transform GetInverseTransform() const OVERRIDE {
255    gfx::Transform invert;
256    CHECK(transform_.GetInverse(&invert));
257    return invert;
258  }
259  virtual gfx::Rect GetRootWindowBounds(
260      const gfx::Size& host_size) const OVERRIDE {
261    return root_bounds_;
262  }
263  virtual gfx::Insets GetHostInsets() const OVERRIDE {
264    return insets_;
265  }
266
267 private:
268  virtual ~MirrorRootWindowTransformer() {}
269
270  gfx::Transform transform_;
271  gfx::Rect root_bounds_;
272  gfx::Insets insets_;
273
274  DISALLOW_COPY_AND_ASSIGN(MirrorRootWindowTransformer);
275};
276
277}  // namespace
278
279aura::RootWindowTransformer* CreateRootWindowTransformerForDisplay(
280    aura::Window* root,
281    const gfx::Display& display) {
282  return new AshRootWindowTransformer(root, display);
283}
284
285aura::RootWindowTransformer* CreateRootWindowTransformerForMirroredDisplay(
286    const DisplayInfo& source_display_info,
287    const DisplayInfo& mirror_display_info) {
288  return new MirrorRootWindowTransformer(source_display_info,
289                                         mirror_display_info);
290}
291
292}  // namespace internal
293}  // namespace ash
294