Port Android relocation packer to chromium build
[chromium-blink-merge.git] / ash / display / root_window_transformers.cc
blob509bb8e215e64f1098c4d7a92e74e1552f978bc0
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.rotation() == root_window->GetProperty(kRotationPropertyKey))
66 return gfx::Transform();
67 root_window->SetProperty(kRotationPropertyKey, info.rotation());
68 #endif
70 gfx::Transform rotate;
71 // The origin is (0, 0), so the translate width/height must be reduced by
72 // 1 pixel.
73 float one_pixel = 1.0f / display.device_scale_factor();
74 switch (info.rotation()) {
75 case gfx::Display::ROTATE_0:
76 break;
77 case gfx::Display::ROTATE_90:
78 rotate.Translate(display.bounds().height() - one_pixel, 0);
79 rotate.Rotate(90);
80 break;
81 case gfx::Display::ROTATE_270:
82 rotate.Translate(0, display.bounds().width() - one_pixel);
83 rotate.Rotate(270);
84 break;
85 case gfx::Display::ROTATE_180:
86 rotate.Translate(display.bounds().width() - one_pixel,
87 display.bounds().height() - one_pixel);
88 rotate.Rotate(180);
89 break;
92 RoundNearZero(&rotate);
93 return rotate;
96 gfx::Transform CreateMagnifierTransform(aura::Window* root_window) {
97 MagnificationController* magnifier =
98 Shell::GetInstance()->magnification_controller();
99 float magnifier_scale = 1.f;
100 gfx::Point magnifier_offset;
101 if (magnifier && magnifier->IsEnabled()) {
102 magnifier_scale = magnifier->GetScale();
103 magnifier_offset = magnifier->GetWindowPosition();
105 gfx::Transform transform;
106 if (magnifier_scale != 1.f) {
107 transform.Scale(magnifier_scale, magnifier_scale);
108 transform.Translate(-magnifier_offset.x(), -magnifier_offset.y());
110 return transform;
113 gfx::Transform CreateInsetsAndScaleTransform(const gfx::Insets& insets,
114 float device_scale_factor,
115 float ui_scale) {
116 gfx::Transform transform;
117 if (insets.top() != 0 || insets.left() != 0) {
118 float x_offset = insets.left() / device_scale_factor;
119 float y_offset = insets.top() / device_scale_factor;
120 transform.Translate(x_offset, y_offset);
122 float inverted_scale = 1.0f / ui_scale;
123 transform.Scale(inverted_scale, inverted_scale);
124 return transform;
127 gfx::Transform CreateMirrorTransform(const gfx::Display& display) {
128 gfx::Transform transform;
129 transform.matrix().set3x3(-1, 0, 0,
130 0, 1, 0,
131 0, 0, 1);
132 transform.Translate(-display.size().width(), 0);
133 return transform;
136 // RootWindowTransformer for ash environment.
137 class AshRootWindowTransformer : public RootWindowTransformer {
138 public:
139 AshRootWindowTransformer(aura::Window* root,
140 const gfx::Display& display)
141 : root_window_(root) {
142 DisplayInfo info = Shell::GetInstance()->display_manager()->
143 GetDisplayInfo(display.id());
144 host_insets_ = info.GetOverscanInsetsInPixel();
145 root_window_ui_scale_ = info.GetEffectiveUIScale();
146 root_window_bounds_transform_ =
147 CreateInsetsAndScaleTransform(host_insets_,
148 display.device_scale_factor(),
149 root_window_ui_scale_) *
150 CreateRotationTransform(root, display);
151 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
152 switches::kAshEnableMirroredScreen)) {
153 // Apply the tranform that flips the screen image horizontally so that
154 // the screen looks normal when reflected on a mirror.
155 root_window_bounds_transform_ =
156 root_window_bounds_transform_ * CreateMirrorTransform(display);
158 transform_ = root_window_bounds_transform_ * CreateMagnifierTransform(root);
160 CHECK(transform_.GetInverse(&invert_transform_));
163 // aura::RootWindowTransformer overrides:
164 gfx::Transform GetTransform() const override { return transform_; }
165 gfx::Transform GetInverseTransform() const override {
166 return invert_transform_;
168 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
169 gfx::Rect bounds(host_size);
170 bounds.Inset(host_insets_);
171 bounds = ui::ConvertRectToDIP(root_window_->layer(), bounds);
172 gfx::RectF new_bounds(bounds);
173 root_window_bounds_transform_.TransformRect(&new_bounds);
174 // Apply |root_window_scale_| twice as the downscaling
175 // is already applied once in |SetTransformInternal()|.
176 // TODO(oshima): This is a bit ugly. Consider specifying
177 // the pseudo host resolution instead.
178 new_bounds.Scale(root_window_ui_scale_ * root_window_ui_scale_);
179 // Ignore the origin because RootWindow's insets are handled by
180 // the transform.
181 // Floor the size because the bounds is no longer aligned to
182 // backing pixel when |root_window_scale_| is specified
183 // (850 height at 1.25 scale becomes 1062.5 for example.)
184 return gfx::Rect(gfx::ToFlooredSize(new_bounds.size()));
187 gfx::Insets GetHostInsets() const override { return host_insets_; }
189 private:
190 ~AshRootWindowTransformer() override {}
192 aura::Window* root_window_;
193 gfx::Transform transform_;
195 // The accurate representation of the inverse of the |transform_|.
196 // This is used to avoid computation error caused by
197 // |gfx::Transform::GetInverse|.
198 gfx::Transform invert_transform_;
200 // The transform of the root window bounds. This is used to calculate
201 // the size of root window.
202 gfx::Transform root_window_bounds_transform_;
204 // The scale of the root window. See |display_info::ui_scale_|
205 // for more info.
206 float root_window_ui_scale_;
208 gfx::Insets host_insets_;
210 DISALLOW_COPY_AND_ASSIGN(AshRootWindowTransformer);
213 // RootWindowTransformer for mirror root window. We simply copy the
214 // texture (bitmap) of the source display into the mirror window, so
215 // the root window bounds is the same as the source display's
216 // pixel size (excluding overscan insets).
217 class MirrorRootWindowTransformer : public RootWindowTransformer {
218 public:
219 MirrorRootWindowTransformer(const DisplayInfo& source_display_info,
220 const DisplayInfo& mirror_display_info) {
221 root_bounds_ = gfx::Rect(source_display_info.bounds_in_native().size());
222 gfx::Rect mirror_display_rect =
223 gfx::Rect(mirror_display_info.bounds_in_native().size());
225 bool letterbox = root_bounds_.width() * mirror_display_rect.height() >
226 root_bounds_.height() * mirror_display_rect.width();
227 if (letterbox) {
228 float mirror_scale_ratio =
229 (static_cast<float>(root_bounds_.width()) /
230 static_cast<float>(mirror_display_rect.width()));
231 float inverted_scale = 1.0f / mirror_scale_ratio;
232 int margin = static_cast<int>(
233 (mirror_display_rect.height() -
234 root_bounds_.height() * inverted_scale) / 2);
235 insets_.Set(0, margin, 0, margin);
237 transform_.Translate(0, margin);
238 transform_.Scale(inverted_scale, inverted_scale);
239 } else {
240 float mirror_scale_ratio =
241 (static_cast<float>(root_bounds_.height()) /
242 static_cast<float>(mirror_display_rect.height()));
243 float inverted_scale = 1.0f / mirror_scale_ratio;
244 int margin = static_cast<int>(
245 (mirror_display_rect.width() -
246 root_bounds_.width() * inverted_scale) / 2);
247 insets_.Set(margin, 0, margin, 0);
249 transform_.Translate(margin, 0);
250 transform_.Scale(inverted_scale, inverted_scale);
254 // aura::RootWindowTransformer overrides:
255 gfx::Transform GetTransform() const override { return transform_; }
256 gfx::Transform GetInverseTransform() const override {
257 gfx::Transform invert;
258 CHECK(transform_.GetInverse(&invert));
259 return invert;
261 gfx::Rect GetRootWindowBounds(const gfx::Size& host_size) const override {
262 return root_bounds_;
264 gfx::Insets GetHostInsets() const override { return insets_; }
266 private:
267 ~MirrorRootWindowTransformer() override {}
269 gfx::Transform transform_;
270 gfx::Rect root_bounds_;
271 gfx::Insets insets_;
273 DISALLOW_COPY_AND_ASSIGN(MirrorRootWindowTransformer);
276 } // namespace
278 RootWindowTransformer* CreateRootWindowTransformerForDisplay(
279 aura::Window* root,
280 const gfx::Display& display) {
281 return new AshRootWindowTransformer(root, display);
284 RootWindowTransformer* CreateRootWindowTransformerForMirroredDisplay(
285 const DisplayInfo& source_display_info,
286 const DisplayInfo& mirror_display_info) {
287 return new MirrorRootWindowTransformer(source_display_info,
288 mirror_display_info);
291 } // namespace ash