| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/view_manager/display_manager.h" | 5 #include "components/view_manager/display_manager.h" |
| 6 | 6 |
| 7 #include "base/numerics/safe_conversions.h" | 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "components/view_manager/display_manager_factory.h" | 8 #include "components/view_manager/display_manager_factory.h" |
| 9 #include "components/view_manager/gles2/gpu_state.h" | 9 #include "components/view_manager/gles2/gpu_state.h" |
| 10 #include "components/view_manager/native_viewport/onscreen_context_provider.h" | 10 #include "components/view_manager/native_viewport/onscreen_context_provider.h" |
| 11 #include "components/view_manager/public/interfaces/gpu.mojom.h" | 11 #include "components/view_manager/public/interfaces/gpu.mojom.h" |
| 12 #include "components/view_manager/public/interfaces/quads.mojom.h" | 12 #include "components/view_manager/public/interfaces/quads.mojom.h" |
| 13 #include "components/view_manager/public/interfaces/surfaces.mojom.h" | 13 #include "components/view_manager/public/interfaces/surfaces.mojom.h" |
| 14 #include "components/view_manager/server_view.h" | 14 #include "components/view_manager/server_view.h" |
| 15 #include "components/view_manager/view_coordinate_conversions.h" | 15 #include "components/view_manager/view_coordinate_conversions.h" |
| 16 #include "mojo/application/public/cpp/application_connection.h" | 16 #include "mojo/application/public/cpp/application_connection.h" |
| 17 #include "mojo/application/public/cpp/application_impl.h" | 17 #include "mojo/application/public/cpp/application_impl.h" |
| 18 #include "mojo/converters/geometry/geometry_type_converters.h" | 18 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 19 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 20 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" |
| 19 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 21 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 20 #include "mojo/converters/surfaces/surfaces_utils.h" | 22 #include "mojo/converters/surfaces/surfaces_utils.h" |
| 21 #include "mojo/converters/transform/transform_type_converters.h" | 23 #include "mojo/converters/transform/transform_type_converters.h" |
| 24 #include "ui/events/event.h" |
| 25 #include "ui/events/event_utils.h" |
| 26 #include "ui/platform_window/platform_window.h" |
| 27 #include "ui/platform_window/stub/stub_window.h" |
| 28 |
| 29 #if defined(OS_WIN) |
| 30 #include "ui/platform_window/win/win_window.h" |
| 31 #elif defined(USE_X11) |
| 32 #include "ui/platform_window/x11/x11_window.h" |
| 33 #elif defined(OS_ANDROID) |
| 34 #include "ui/platform_window/android/platform_window_android.h" |
| 35 #endif |
| 22 | 36 |
| 23 using mojo::Rect; | 37 using mojo::Rect; |
| 24 using mojo::Size; | 38 using mojo::Size; |
| 25 | 39 |
| 26 namespace view_manager { | 40 namespace view_manager { |
| 27 namespace { | 41 namespace { |
| 28 | 42 |
| 29 void DrawViewTree(mojo::Pass* pass, | 43 void DrawViewTree(mojo::Pass* pass, |
| 30 const ServerView* view, | 44 const ServerView* view, |
| 31 const gfx::Vector2d& parent_to_root_origin_offset, | 45 const gfx::Vector2d& parent_to_root_origin_offset, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 79 |
| 66 auto sqs = mojo::CreateDefaultSQS(view->bounds().size()); | 80 auto sqs = mojo::CreateDefaultSQS(view->bounds().size()); |
| 67 sqs->blend_mode = mojo::SK_XFERMODE_kSrcOver_Mode; | 81 sqs->blend_mode = mojo::SK_XFERMODE_kSrcOver_Mode; |
| 68 sqs->opacity = combined_opacity; | 82 sqs->opacity = combined_opacity; |
| 69 sqs->quad_to_target_transform = mojo::Transform::From(node_transform); | 83 sqs->quad_to_target_transform = mojo::Transform::From(node_transform); |
| 70 | 84 |
| 71 pass->quads.push_back(surface_quad.Pass()); | 85 pass->quads.push_back(surface_quad.Pass()); |
| 72 pass->shared_quad_states.push_back(sqs.Pass()); | 86 pass->shared_quad_states.push_back(sqs.Pass()); |
| 73 } | 87 } |
| 74 | 88 |
| 89 float ConvertUIWheelValueToMojoValue(int offset) { |
| 90 // Mojo's event type takes a value between -1 and 1. Normalize by allowing |
| 91 // up to 20 of ui's offset. This is a bit arbitrary. |
| 92 return std::max( |
| 93 -1.0f, std::min(1.0f, static_cast<float>(offset) / |
| 94 (20 * static_cast<float>( |
| 95 ui::MouseWheelEvent::kWheelDelta)))); |
| 96 } |
| 97 |
| 75 } // namespace | 98 } // namespace |
| 76 | 99 |
| 77 // static | 100 // static |
| 78 DisplayManagerFactory* DisplayManager::factory_ = nullptr; | 101 DisplayManagerFactory* DisplayManager::factory_ = nullptr; |
| 79 | 102 |
| 80 // static | 103 // static |
| 81 DisplayManager* DisplayManager::Create( | 104 DisplayManager* DisplayManager::Create( |
| 82 bool is_headless, | 105 bool is_headless, |
| 83 mojo::ApplicationImpl* app_impl, | 106 mojo::ApplicationImpl* app_impl, |
| 84 const scoped_refptr<gles2::GpuState>& gpu_state) { | 107 const scoped_refptr<gles2::GpuState>& gpu_state) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 new native_viewport::OnscreenContextProvider(gpu_state)), | 124 new native_viewport::OnscreenContextProvider(gpu_state)), |
| 102 weak_factory_(this) { | 125 weak_factory_(this) { |
| 103 metrics_.size_in_pixels = mojo::Size::New(); | 126 metrics_.size_in_pixels = mojo::Size::New(); |
| 104 metrics_.size_in_pixels->width = 800; | 127 metrics_.size_in_pixels->width = 800; |
| 105 metrics_.size_in_pixels->height = 600; | 128 metrics_.size_in_pixels->height = 600; |
| 106 } | 129 } |
| 107 | 130 |
| 108 void DefaultDisplayManager::Init(DisplayManagerDelegate* delegate) { | 131 void DefaultDisplayManager::Init(DisplayManagerDelegate* delegate) { |
| 109 delegate_ = delegate; | 132 delegate_ = delegate; |
| 110 | 133 |
| 111 platform_viewport_ = | 134 gfx::Rect bounds(metrics_.size_in_pixels.To<gfx::Size>()); |
| 112 native_viewport::PlatformViewport::Create(this, is_headless_).Pass(); | 135 if (is_headless_) { |
| 113 platform_viewport_->Init(gfx::Rect(metrics_.size_in_pixels.To<gfx::Size>())); | 136 platform_window_.reset(new ui::StubWindow(this)); |
| 114 platform_viewport_->Show(); | 137 } else { |
| 138 #if defined(OS_WIN) |
| 139 platform_window_.reset(new ui::WinWindow(this, bounds)); |
| 140 #elif defined(USE_X11) |
| 141 platform_window_.reset(new ui::X11Window(this)); |
| 142 #elif defined(OS_ANDROID) |
| 143 platform_window_.reset(new ui::PlatformWindowAndroid(this)); |
| 144 #else |
| 145 NOTREACHED() << "Unsupported platform"; |
| 146 #endif |
| 147 } |
| 148 platform_window_->SetBounds(bounds); |
| 149 platform_window_->Show(); |
| 115 | 150 |
| 116 mojo::ContextProviderPtr context_provider; | 151 mojo::ContextProviderPtr context_provider; |
| 117 context_provider_->Bind(GetProxy(&context_provider).Pass()); | 152 context_provider_->Bind(GetProxy(&context_provider).Pass()); |
| 118 mojo::DisplayFactoryPtr display_factory; | 153 mojo::DisplayFactoryPtr display_factory; |
| 119 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 154 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 120 request->url = mojo::String::From("mojo:surfaces_service"); | 155 request->url = mojo::String::From("mojo:surfaces_service"); |
| 121 app_impl_->ConnectToService(request.Pass(), &display_factory); | 156 app_impl_->ConnectToService(request.Pass(), &display_factory); |
| 122 display_factory->Create(context_provider.Pass(), | 157 display_factory->Create(context_provider.Pass(), |
| 123 nullptr, // returner - we never submit resources. | 158 nullptr, // returner - we never submit resources. |
| 124 GetProxy(&display_)); | 159 GetProxy(&display_)); |
| 125 } | 160 } |
| 126 | 161 |
| 127 DefaultDisplayManager::~DefaultDisplayManager() { | 162 DefaultDisplayManager::~DefaultDisplayManager() { |
| 128 // Destroy before |platform_viewport_| because this will destroy | 163 // Destroy before |platform_window_| because this will destroy |
| 129 // CommandBufferDriver objects that contain child windows. Otherwise if this | 164 // CommandBufferDriver objects that contain child windows. Otherwise if this |
| 130 // class destroys its window first, X errors will occur. | 165 // class destroys its window first, X errors will occur. |
| 131 context_provider_.reset(); | 166 context_provider_.reset(); |
| 132 | 167 |
| 133 // Destroy the NativeViewport early on as it may call us back during | 168 // Destroy the PlatformWindow early on as it may call us back during |
| 134 // destruction and we want to be in a known state. | 169 // destruction and we want to be in a known state. |
| 135 platform_viewport_.reset(); | 170 platform_window_.reset(); |
| 136 } | 171 } |
| 137 | 172 |
| 138 void DefaultDisplayManager::SchedulePaint(const ServerView* view, | 173 void DefaultDisplayManager::SchedulePaint(const ServerView* view, |
| 139 const gfx::Rect& bounds) { | 174 const gfx::Rect& bounds) { |
| 140 DCHECK(view); | 175 DCHECK(view); |
| 141 if (!view->IsDrawn()) | 176 if (!view->IsDrawn()) |
| 142 return; | 177 return; |
| 143 const gfx::Rect root_relative_rect = | 178 const gfx::Rect root_relative_rect = |
| 144 ConvertRectBetweenViews(view, delegate_->GetRootView(), bounds); | 179 ConvertRectBetweenViews(view, delegate_->GetRootView(), bounds); |
| 145 if (root_relative_rect.IsEmpty()) | 180 if (root_relative_rect.IsEmpty()) |
| 146 return; | 181 return; |
| 147 dirty_rect_.Union(root_relative_rect); | 182 dirty_rect_.Union(root_relative_rect); |
| 148 WantToDraw(); | 183 WantToDraw(); |
| 149 } | 184 } |
| 150 | 185 |
| 151 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { | 186 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { |
| 152 platform_viewport_->SetBounds(gfx::Rect(size)); | 187 platform_window_->SetBounds(gfx::Rect(size)); |
| 153 } | 188 } |
| 154 | 189 |
| 155 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { | 190 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { |
| 156 return metrics_; | 191 return metrics_; |
| 157 } | 192 } |
| 158 | 193 |
| 159 void DefaultDisplayManager::Draw() { | 194 void DefaultDisplayManager::Draw() { |
| 160 gfx::Rect rect(metrics_.size_in_pixels.To<gfx::Size>()); | 195 gfx::Rect rect(metrics_.size_in_pixels.To<gfx::Size>()); |
| 161 auto pass = mojo::CreateDefaultPass(1, rect); | 196 auto pass = mojo::CreateDefaultPass(1, rect); |
| 162 pass->damage_rect = Rect::From(dirty_rect_); | 197 pass->damage_rect = Rect::From(dirty_rect_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 181 | 216 |
| 182 void DefaultDisplayManager::WantToDraw() { | 217 void DefaultDisplayManager::WantToDraw() { |
| 183 if (draw_timer_.IsRunning() || frame_pending_) | 218 if (draw_timer_.IsRunning() || frame_pending_) |
| 184 return; | 219 return; |
| 185 | 220 |
| 186 draw_timer_.Start( | 221 draw_timer_.Start( |
| 187 FROM_HERE, base::TimeDelta(), | 222 FROM_HERE, base::TimeDelta(), |
| 188 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); | 223 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); |
| 189 } | 224 } |
| 190 | 225 |
| 191 void DefaultDisplayManager::OnAcceleratedWidgetAvailable( | 226 void DefaultDisplayManager::UpdateMetrics(const gfx::Size& size, |
| 192 gfx::AcceleratedWidget widget, | 227 float device_pixel_ratio) { |
| 193 float device_pixel_ratio) { | 228 if (metrics_.size_in_pixels.To<gfx::Size>() == size && |
| 194 context_provider_->SetAcceleratedWidget(widget); | 229 metrics_.device_pixel_ratio == device_pixel_ratio) |
| 195 OnMetricsChanged(metrics_.size_in_pixels.To<gfx::Size>(), device_pixel_ratio); | |
| 196 } | |
| 197 | |
| 198 void DefaultDisplayManager::OnAcceleratedWidgetDestroyed() { | |
| 199 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
| 200 } | |
| 201 | |
| 202 void DefaultDisplayManager::OnEvent(mojo::EventPtr event) { | |
| 203 delegate_->OnEvent(event.Pass()); | |
| 204 } | |
| 205 | |
| 206 void DefaultDisplayManager::OnMetricsChanged(const gfx::Size& size, | |
| 207 float device_scale_factor) { | |
| 208 if ((metrics_.size_in_pixels.To<gfx::Size>() == size) && | |
| 209 (metrics_.device_pixel_ratio == device_scale_factor)) { | |
| 210 return; | 230 return; |
| 211 } | |
| 212 | |
| 213 mojo::ViewportMetrics metrics; | 231 mojo::ViewportMetrics metrics; |
| 214 metrics.size_in_pixels = mojo::Size::From(size); | 232 metrics.size_in_pixels = mojo::Size::From(size); |
| 215 metrics.device_pixel_ratio = device_scale_factor; | 233 metrics.device_pixel_ratio = device_pixel_ratio; |
| 216 | 234 |
| 217 delegate_->GetRootView()->SetBounds(gfx::Rect(size)); | 235 delegate_->GetRootView()->SetBounds(gfx::Rect(size)); |
| 218 delegate_->OnViewportMetricsChanged(metrics_, metrics); | 236 delegate_->OnViewportMetricsChanged(metrics_, metrics); |
| 219 | 237 |
| 220 metrics_.size_in_pixels = metrics.size_in_pixels.Clone(); | 238 metrics_.size_in_pixels = metrics.size_in_pixels.Clone(); |
| 221 metrics_.device_pixel_ratio = metrics.device_pixel_ratio; | 239 metrics_.device_pixel_ratio = metrics.device_pixel_ratio; |
| 222 } | 240 } |
| 223 | 241 |
| 224 void DefaultDisplayManager::OnDestroyed() { | 242 void DefaultDisplayManager::OnBoundsChanged(const gfx::Rect& new_bounds) { |
| 243 UpdateMetrics(new_bounds.size(), metrics_.device_pixel_ratio); |
| 244 } |
| 245 |
| 246 void DefaultDisplayManager::OnDamageRect(const gfx::Rect& damaged_region) { |
| 247 } |
| 248 |
| 249 void DefaultDisplayManager::DispatchEvent(ui::Event* event) { |
| 250 mojo::EventPtr mojo_event(mojo::Event::From(*event)); |
| 251 if (event->IsMouseWheelEvent()) { |
| 252 // Mojo's event type has a different meaning for wheel events. Convert |
| 253 // between the two. |
| 254 ui::MouseWheelEvent* wheel_event = |
| 255 static_cast<ui::MouseWheelEvent*>(event); |
| 256 DCHECK(mojo_event->pointer_data); |
| 257 mojo_event->pointer_data->horizontal_wheel = |
| 258 ConvertUIWheelValueToMojoValue(wheel_event->x_offset()); |
| 259 mojo_event->pointer_data->horizontal_wheel = |
| 260 ConvertUIWheelValueToMojoValue(wheel_event->y_offset()); |
| 261 } |
| 262 delegate_->OnEvent(mojo_event.Pass()); |
| 263 |
| 264 switch (event->type()) { |
| 265 case ui::ET_MOUSE_PRESSED: |
| 266 case ui::ET_TOUCH_PRESSED: |
| 267 platform_window_->SetCapture(); |
| 268 break; |
| 269 case ui::ET_MOUSE_RELEASED: |
| 270 case ui::ET_TOUCH_RELEASED: |
| 271 platform_window_->ReleaseCapture(); |
| 272 break; |
| 273 default: |
| 274 break; |
| 275 } |
| 276 |
| 277 #if defined(USE_X11) |
| 278 // We want to emulate the WM_CHAR generation behaviour of Windows. |
| 279 // |
| 280 // On Linux, we've previously inserted characters by having |
| 281 // InputMethodAuraLinux take all key down events and send a character event |
| 282 // to the TextInputClient. This causes a mismatch in code that has to be |
| 283 // shared between Windows and Linux, including blink code. Now that we're |
| 284 // trying to have one way of doing things, we need to standardize on and |
| 285 // emulate Windows character events. |
| 286 // |
| 287 // This is equivalent to what we're doing in the current Linux port, but |
| 288 // done once instead of done multiple times in different places. |
| 289 if (event->type() == ui::ET_KEY_PRESSED) { |
| 290 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); |
| 291 ui::KeyEvent char_event(key_press_event->GetCharacter(), |
| 292 key_press_event->key_code(), |
| 293 key_press_event->flags()); |
| 294 |
| 295 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); |
| 296 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); |
| 297 DCHECK_EQ(key_press_event->flags(), char_event.flags()); |
| 298 |
| 299 char_event.SetExtendedKeyEventData( |
| 300 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( |
| 301 key_press_event->GetLocatedWindowsKeyboardCode(), |
| 302 key_press_event->GetText(), |
| 303 key_press_event->GetUnmodifiedText()))); |
| 304 char_event.set_platform_keycode(key_press_event->platform_keycode()); |
| 305 |
| 306 delegate_->OnEvent(mojo::Event::From(char_event)); |
| 307 } |
| 308 #endif |
| 309 } |
| 310 |
| 311 void DefaultDisplayManager::OnCloseRequest() { |
| 312 platform_window_->Close(); |
| 313 } |
| 314 |
| 315 void DefaultDisplayManager::OnClosed() { |
| 225 delegate_->OnDisplayClosed(); | 316 delegate_->OnDisplayClosed(); |
| 226 } | 317 } |
| 227 | 318 |
| 319 void DefaultDisplayManager::OnWindowStateChanged( |
| 320 ui::PlatformWindowState new_state) { |
| 321 } |
| 322 |
| 323 void DefaultDisplayManager::OnLostCapture() { |
| 324 } |
| 325 |
| 326 void DefaultDisplayManager::OnAcceleratedWidgetAvailable( |
| 327 gfx::AcceleratedWidget widget, |
| 328 float device_pixel_ratio) { |
| 329 context_provider_->SetAcceleratedWidget(widget); |
| 330 UpdateMetrics(metrics_.size_in_pixels.To<gfx::Size>(), device_pixel_ratio); |
| 331 } |
| 332 |
| 333 void DefaultDisplayManager::OnActivationChanged(bool active) { |
| 334 } |
| 335 |
| 228 } // namespace view_manager | 336 } // namespace view_manager |
| OLD | NEW |