Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / chromeos / point_transformer.cc
blob2c8b89317a9f974816dbd3da74305bd7100be144
1 // Copyright 2014 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 "remoting/host/chromeos/point_transformer.h"
7 #include "ash/shell.h"
8 #include "ui/aura/window_tree_host.h"
9 #include "ui/compositor/dip_util.h"
11 namespace remoting {
13 PointTransformer::PointTransformer() {
14 root_window_ = ash::Shell::GetPrimaryRootWindow();
15 root_window_->AddObserver(this);
18 PointTransformer::~PointTransformer() {
19 root_window_->RemoveObserver(this);
22 void PointTransformer::OnWindowTransformed(aura::Window* window) {
23 CHECK_EQ(window, root_window_);
25 ui::Layer* layer = root_window_->layer();
26 float scale = ui::GetDeviceScaleFactor(layer);
28 // |layer->transform()| returns a transform comprising a rotation and a
29 // translation, but in DIPs, so we need to switch device pixels to
30 // DIPs, apply it, then switch from DIPs back to device pixels.
31 gfx::Transform rotation = layer->transform();
32 gfx::Transform inverse_rotation;
33 gfx::Transform to_device_pixels;
34 gfx::Transform to_dip;
36 CHECK(!rotation.GetInverse(&inverse_rotation))
37 << "Cannot inverse the root transform." << rotation.ToString();
39 to_device_pixels.Scale(scale, scale);
40 to_dip.Scale(1 / scale, 1 / scale);
42 // Matrix transformations are applied from right to left. See annotations.
43 // (3) (2) (1)
44 root_to_screen_ = to_device_pixels * rotation * to_dip;
45 screen_to_root_ = to_device_pixels * inverse_rotation * to_dip;
48 gfx::PointF PointTransformer::ToScreenCoordinates(
49 const gfx::PointF& root_location) {
50 gfx::Point3F screen_location(root_location);
51 root_to_screen_.TransformPoint(&screen_location);
52 return screen_location.AsPointF();
55 gfx::PointF PointTransformer::FromScreenCoordinates(
56 const gfx::PointF& screen_location) {
57 gfx::Point3F root_location(screen_location);
58 screen_to_root_.TransformPoint(&root_location);
59 return root_location.AsPointF();
62 } // namespace remoting