| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "headless/lib/browser/headless_screen.h" | 5 #include "headless/lib/browser/headless_screen.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/aura/env.h" | 10 #include "ui/aura/env.h" |
| 11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/window_event_dispatcher.h" | 12 #include "ui/aura/window_event_dispatcher.h" |
| 13 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/base/ime/input_method.h" | 14 #include "ui/base/ime/input_method.h" |
| 15 #include "ui/display/screen.h" |
| 15 #include "ui/gfx/geometry/rect_conversions.h" | 16 #include "ui/gfx/geometry/rect_conversions.h" |
| 16 #include "ui/gfx/geometry/size_conversions.h" | 17 #include "ui/gfx/geometry/size_conversions.h" |
| 17 #include "ui/gfx/native_widget_types.h" | 18 #include "ui/gfx/native_widget_types.h" |
| 18 #include "ui/gfx/screen.h" | |
| 19 | 19 |
| 20 namespace headless { | 20 namespace headless { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 bool IsRotationPortrait(gfx::Display::Rotation rotation) { | 24 bool IsRotationPortrait(display::Display::Rotation rotation) { |
| 25 return rotation == gfx::Display::ROTATE_90 || | 25 return rotation == display::Display::ROTATE_90 || |
| 26 rotation == gfx::Display::ROTATE_270; | 26 rotation == display::Display::ROTATE_270; |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 // static | 31 // static |
| 32 HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) { | 32 HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) { |
| 33 const gfx::Size kDefaultSize(800, 600); | 33 const gfx::Size kDefaultSize(800, 600); |
| 34 return new HeadlessScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size)); | 34 return new HeadlessScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size)); |
| 35 } | 35 } |
| 36 | 36 |
| 37 HeadlessScreen::~HeadlessScreen() {} | 37 HeadlessScreen::~HeadlessScreen() {} |
| 38 | 38 |
| 39 aura::WindowTreeHost* HeadlessScreen::CreateHostForPrimaryDisplay() { | 39 aura::WindowTreeHost* HeadlessScreen::CreateHostForPrimaryDisplay() { |
| 40 DCHECK(!host_); | 40 DCHECK(!host_); |
| 41 host_ = aura::WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel())); | 41 host_ = aura::WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel())); |
| 42 // Some tests don't correctly manage window focus/activation states. | 42 // Some tests don't correctly manage window focus/activation states. |
| 43 // Makes sure InputMethod is default focused so that IME basics can work. | 43 // Makes sure InputMethod is default focused so that IME basics can work. |
| 44 host_->GetInputMethod()->OnFocus(); | 44 host_->GetInputMethod()->OnFocus(); |
| 45 host_->window()->AddObserver(this); | 45 host_->window()->AddObserver(this); |
| 46 host_->InitHost(); | 46 host_->InitHost(); |
| 47 return host_; | 47 return host_; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void HeadlessScreen::SetDeviceScaleFactor(float device_scale_factor) { | 50 void HeadlessScreen::SetDeviceScaleFactor(float device_scale_factor) { |
| 51 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 51 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 52 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel); | 52 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void HeadlessScreen::SetDisplayRotation(gfx::Display::Rotation rotation) { | 55 void HeadlessScreen::SetDisplayRotation(display::Display::Rotation rotation) { |
| 56 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 56 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 57 gfx::Rect new_bounds(bounds_in_pixel); | 57 gfx::Rect new_bounds(bounds_in_pixel); |
| 58 if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) { | 58 if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) { |
| 59 new_bounds.set_width(bounds_in_pixel.height()); | 59 new_bounds.set_width(bounds_in_pixel.height()); |
| 60 new_bounds.set_height(bounds_in_pixel.width()); | 60 new_bounds.set_height(bounds_in_pixel.width()); |
| 61 } | 61 } |
| 62 display_.set_rotation(rotation); | 62 display_.set_rotation(rotation); |
| 63 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); | 63 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); |
| 64 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); | 64 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void HeadlessScreen::SetUIScale(float ui_scale) { | 67 void HeadlessScreen::SetUIScale(float ui_scale) { |
| 68 ui_scale_ = ui_scale; | 68 ui_scale_ = ui_scale; |
| 69 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 69 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 70 gfx::Rect new_bounds = gfx::ToNearestRect( | 70 gfx::Rect new_bounds = gfx::ToNearestRect( |
| 71 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale)); | 71 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale)); |
| 72 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); | 72 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); |
| 73 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); | 73 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void HeadlessScreen::SetWorkAreaInsets(const gfx::Insets& insets) { | 76 void HeadlessScreen::SetWorkAreaInsets(const gfx::Insets& insets) { |
| 77 display_.UpdateWorkAreaFromInsets(insets); | 77 display_.UpdateWorkAreaFromInsets(insets); |
| 78 } | 78 } |
| 79 | 79 |
| 80 gfx::Transform HeadlessScreen::GetRotationTransform() const { | 80 gfx::Transform HeadlessScreen::GetRotationTransform() const { |
| 81 gfx::Transform rotate; | 81 gfx::Transform rotate; |
| 82 switch (display_.rotation()) { | 82 switch (display_.rotation()) { |
| 83 case gfx::Display::ROTATE_0: | 83 case display::Display::ROTATE_0: |
| 84 break; | 84 break; |
| 85 case gfx::Display::ROTATE_90: | 85 case display::Display::ROTATE_90: |
| 86 rotate.Translate(display_.bounds().height(), 0); | 86 rotate.Translate(display_.bounds().height(), 0); |
| 87 rotate.Rotate(90); | 87 rotate.Rotate(90); |
| 88 break; | 88 break; |
| 89 case gfx::Display::ROTATE_270: | 89 case display::Display::ROTATE_270: |
| 90 rotate.Translate(0, display_.bounds().width()); | 90 rotate.Translate(0, display_.bounds().width()); |
| 91 rotate.Rotate(270); | 91 rotate.Rotate(270); |
| 92 break; | 92 break; |
| 93 case gfx::Display::ROTATE_180: | 93 case display::Display::ROTATE_180: |
| 94 rotate.Translate(display_.bounds().width(), display_.bounds().height()); | 94 rotate.Translate(display_.bounds().width(), display_.bounds().height()); |
| 95 rotate.Rotate(180); | 95 rotate.Rotate(180); |
| 96 break; | 96 break; |
| 97 } | 97 } |
| 98 | 98 |
| 99 return rotate; | 99 return rotate; |
| 100 } | 100 } |
| 101 | 101 |
| 102 gfx::Transform HeadlessScreen::GetUIScaleTransform() const { | 102 gfx::Transform HeadlessScreen::GetUIScaleTransform() const { |
| 103 gfx::Transform ui_scale; | 103 gfx::Transform ui_scale; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 130 const gfx::Point& point) { | 130 const gfx::Point& point) { |
| 131 if (!host_ || !host_->window()) | 131 if (!host_ || !host_->window()) |
| 132 return nullptr; | 132 return nullptr; |
| 133 return host_->window()->GetTopWindowContainingPoint(point); | 133 return host_->window()->GetTopWindowContainingPoint(point); |
| 134 } | 134 } |
| 135 | 135 |
| 136 int HeadlessScreen::GetNumDisplays() const { | 136 int HeadlessScreen::GetNumDisplays() const { |
| 137 return 1; | 137 return 1; |
| 138 } | 138 } |
| 139 | 139 |
| 140 std::vector<gfx::Display> HeadlessScreen::GetAllDisplays() const { | 140 std::vector<display::Display> HeadlessScreen::GetAllDisplays() const { |
| 141 return std::vector<gfx::Display>(1, display_); | 141 return std::vector<display::Display>(1, display_); |
| 142 } | 142 } |
| 143 | 143 |
| 144 gfx::Display HeadlessScreen::GetDisplayNearestWindow( | 144 display::Display HeadlessScreen::GetDisplayNearestWindow( |
| 145 gfx::NativeWindow window) const { | 145 gfx::NativeWindow window) const { |
| 146 return display_; | 146 return display_; |
| 147 } | 147 } |
| 148 | 148 |
| 149 gfx::Display HeadlessScreen::GetDisplayNearestPoint( | 149 display::Display HeadlessScreen::GetDisplayNearestPoint( |
| 150 const gfx::Point& point) const { | 150 const gfx::Point& point) const { |
| 151 return display_; | 151 return display_; |
| 152 } | 152 } |
| 153 | 153 |
| 154 gfx::Display HeadlessScreen::GetDisplayMatching( | 154 display::Display HeadlessScreen::GetDisplayMatching( |
| 155 const gfx::Rect& match_rect) const { | 155 const gfx::Rect& match_rect) const { |
| 156 return display_; | 156 return display_; |
| 157 } | 157 } |
| 158 | 158 |
| 159 gfx::Display HeadlessScreen::GetPrimaryDisplay() const { | 159 display::Display HeadlessScreen::GetPrimaryDisplay() const { |
| 160 return display_; | 160 return display_; |
| 161 } | 161 } |
| 162 | 162 |
| 163 void HeadlessScreen::AddObserver(gfx::DisplayObserver* observer) {} | 163 void HeadlessScreen::AddObserver(display::DisplayObserver* observer) {} |
| 164 | 164 |
| 165 void HeadlessScreen::RemoveObserver(gfx::DisplayObserver* observer) {} | 165 void HeadlessScreen::RemoveObserver(display::DisplayObserver* observer) {} |
| 166 | 166 |
| 167 HeadlessScreen::HeadlessScreen(const gfx::Rect& screen_bounds) | 167 HeadlessScreen::HeadlessScreen(const gfx::Rect& screen_bounds) |
| 168 : host_(NULL), ui_scale_(1.0f) { | 168 : host_(NULL), ui_scale_(1.0f) { |
| 169 static int64_t synthesized_display_id = 2000; | 169 static int64_t synthesized_display_id = 2000; |
| 170 display_.set_id(synthesized_display_id++); | 170 display_.set_id(synthesized_display_id++); |
| 171 display_.SetScaleAndBounds(1.0f, screen_bounds); | 171 display_.SetScaleAndBounds(1.0f, screen_bounds); |
| 172 } | 172 } |
| 173 | 173 |
| 174 } // namespace headless | 174 } // namespace headless |
| OLD | NEW |