| 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/connection_manager.h" | 8 #include "components/view_manager/connection_manager.h" |
| 9 #include "components/view_manager/gles2/gpu_state.h" |
| 10 #include "components/view_manager/native_viewport/onscreen_context_provider.h" |
| 9 #include "components/view_manager/public/interfaces/gpu.mojom.h" | 11 #include "components/view_manager/public/interfaces/gpu.mojom.h" |
| 10 #include "components/view_manager/public/interfaces/quads.mojom.h" | 12 #include "components/view_manager/public/interfaces/quads.mojom.h" |
| 11 #include "components/view_manager/public/interfaces/surfaces.mojom.h" | 13 #include "components/view_manager/public/interfaces/surfaces.mojom.h" |
| 12 #include "components/view_manager/server_view.h" | 14 #include "components/view_manager/server_view.h" |
| 13 #include "components/view_manager/view_coordinate_conversions.h" | 15 #include "components/view_manager/view_coordinate_conversions.h" |
| 14 #include "mojo/application/public/cpp/application_connection.h" | 16 #include "mojo/application/public/cpp/application_connection.h" |
| 15 #include "mojo/application/public/cpp/application_impl.h" | 17 #include "mojo/application/public/cpp/application_impl.h" |
| 16 #include "mojo/converters/geometry/geometry_type_converters.h" | 18 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 17 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 19 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 18 #include "mojo/converters/surfaces/surfaces_utils.h" | 20 #include "mojo/converters/surfaces/surfaces_utils.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 sqs->opacity = combined_opacity; | 68 sqs->opacity = combined_opacity; |
| 67 sqs->content_to_target_transform = mojo::Transform::From(node_transform); | 69 sqs->content_to_target_transform = mojo::Transform::From(node_transform); |
| 68 | 70 |
| 69 pass->quads.push_back(surface_quad.Pass()); | 71 pass->quads.push_back(surface_quad.Pass()); |
| 70 pass->shared_quad_states.push_back(sqs.Pass()); | 72 pass->shared_quad_states.push_back(sqs.Pass()); |
| 71 } | 73 } |
| 72 | 74 |
| 73 } // namespace | 75 } // namespace |
| 74 | 76 |
| 75 DefaultDisplayManager::DefaultDisplayManager( | 77 DefaultDisplayManager::DefaultDisplayManager( |
| 78 bool is_headless, |
| 76 mojo::ApplicationImpl* app_impl, | 79 mojo::ApplicationImpl* app_impl, |
| 77 const mojo::Callback<void()>& native_viewport_closed_callback) | 80 const scoped_refptr<gles2::GpuState>& gpu_state, |
| 78 : app_impl_(app_impl), | 81 const mojo::Callback<void()>& platform_viewport_closed_callback) |
| 82 : is_headless_(is_headless), |
| 83 app_impl_(app_impl), |
| 84 gpu_state_(gpu_state), |
| 79 connection_manager_(nullptr), | 85 connection_manager_(nullptr), |
| 86 event_dispatcher_(nullptr), |
| 80 draw_timer_(false, false), | 87 draw_timer_(false, false), |
| 81 frame_pending_(false), | 88 frame_pending_(false), |
| 82 native_viewport_closed_callback_(native_viewport_closed_callback), | 89 context_provider_( |
| 90 new native_viewport::OnscreenContextProvider(gpu_state)), |
| 91 platform_viewport_closed_callback_(platform_viewport_closed_callback), |
| 83 weak_factory_(this) { | 92 weak_factory_(this) { |
| 84 metrics_.size = mojo::Size::New(); | 93 metrics_.size_in_pixels = mojo::Size::New(); |
| 85 metrics_.size->width = 800; | 94 metrics_.size_in_pixels->width = 800; |
| 86 metrics_.size->height = 600; | 95 metrics_.size_in_pixels->height = 600; |
| 87 } | 96 } |
| 88 | 97 |
| 89 void DefaultDisplayManager::Init( | 98 void DefaultDisplayManager::Init( |
| 90 ConnectionManager* connection_manager, | 99 ConnectionManager* connection_manager, |
| 91 mojo::NativeViewportEventDispatcherPtr event_dispatcher) { | 100 EventDispatcher* event_dispatcher) { |
| 92 connection_manager_ = connection_manager; | 101 connection_manager_ = connection_manager; |
| 93 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 102 event_dispatcher_ = event_dispatcher; |
| 94 // TODO(beng): should not need to connect to ourselves, should just | 103 |
| 95 // create the appropriate platform window directly. | 104 platform_viewport_ = |
| 96 request->url = mojo::String::From("mojo:view_manager"); | 105 native_viewport::PlatformViewport::Create(this, is_headless_).Pass(); |
| 97 app_impl_->ConnectToService(request.Pass(), | 106 platform_viewport_->Init(gfx::Rect(metrics_.size_in_pixels.To<gfx::Size>())); |
| 98 &native_viewport_); | 107 platform_viewport_->Show(); |
| 99 native_viewport_.set_error_handler(this); | |
| 100 native_viewport_->Create(metrics_.size->Clone(), | |
| 101 base::Bind(&DefaultDisplayManager::OnMetricsChanged, | |
| 102 weak_factory_.GetWeakPtr())); | |
| 103 native_viewport_->Show(); | |
| 104 | 108 |
| 105 mojo::ContextProviderPtr context_provider; | 109 mojo::ContextProviderPtr context_provider; |
| 106 native_viewport_->GetContextProvider(GetProxy(&context_provider)); | 110 context_provider_->Bind(GetProxy(&context_provider).Pass()); |
| 107 mojo::DisplayFactoryPtr display_factory; | 111 mojo::DisplayFactoryPtr display_factory; |
| 108 mojo::URLRequestPtr request2(mojo::URLRequest::New()); | 112 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 109 request2->url = mojo::String::From("mojo:surfaces_service"); | 113 request->url = mojo::String::From("mojo:surfaces_service"); |
| 110 app_impl_->ConnectToService(request2.Pass(), &display_factory); | 114 app_impl_->ConnectToService(request.Pass(), &display_factory); |
| 111 display_factory->Create(context_provider.Pass(), | 115 display_factory->Create(context_provider.Pass(), |
| 112 nullptr, // returner - we never submit resources. | 116 nullptr, // returner - we never submit resources. |
| 113 GetProxy(&display_)); | 117 GetProxy(&display_)); |
| 114 | |
| 115 native_viewport_->SetEventDispatcher(event_dispatcher.Pass()); | |
| 116 } | 118 } |
| 117 | 119 |
| 118 DefaultDisplayManager::~DefaultDisplayManager() { | 120 DefaultDisplayManager::~DefaultDisplayManager() { |
| 121 // Destroy before |platform_viewport_| because this will destroy |
| 122 // CommandBufferDriver objects that contain child windows. Otherwise if this |
| 123 // class destroys its window first, X errors will occur. |
| 124 context_provider_.reset(); |
| 125 |
| 126 // Destroy the NativeViewport early on as it may call us back during |
| 127 // destruction and we want to be in a known state. |
| 128 platform_viewport_.reset(); |
| 119 } | 129 } |
| 120 | 130 |
| 121 void DefaultDisplayManager::SchedulePaint(const ServerView* view, | 131 void DefaultDisplayManager::SchedulePaint(const ServerView* view, |
| 122 const gfx::Rect& bounds) { | 132 const gfx::Rect& bounds) { |
| 123 if (!view->IsDrawn(connection_manager_->root())) | 133 if (!view->IsDrawn(connection_manager_->root())) |
| 124 return; | 134 return; |
| 125 const gfx::Rect root_relative_rect = | 135 const gfx::Rect root_relative_rect = |
| 126 ConvertRectBetweenViews(view, connection_manager_->root(), bounds); | 136 ConvertRectBetweenViews(view, connection_manager_->root(), bounds); |
| 127 if (root_relative_rect.IsEmpty()) | 137 if (root_relative_rect.IsEmpty()) |
| 128 return; | 138 return; |
| 129 dirty_rect_.Union(root_relative_rect); | 139 dirty_rect_.Union(root_relative_rect); |
| 130 WantToDraw(); | 140 WantToDraw(); |
| 131 } | 141 } |
| 132 | 142 |
| 133 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { | 143 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { |
| 134 native_viewport_->SetSize(Size::From(size)); | 144 platform_viewport_->SetBounds(gfx::Rect(size)); |
| 135 } | 145 } |
| 136 | 146 |
| 137 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { | 147 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { |
| 138 return metrics_; | 148 return metrics_; |
| 139 } | 149 } |
| 140 | 150 |
| 141 void DefaultDisplayManager::Draw() { | 151 void DefaultDisplayManager::Draw() { |
| 142 gfx::Rect rect(metrics_.size->width, metrics_.size->height); | 152 gfx::Rect rect(metrics_.size_in_pixels.To<gfx::Size>()); |
| 143 auto pass = mojo::CreateDefaultPass(1, rect); | 153 auto pass = mojo::CreateDefaultPass(1, rect); |
| 144 pass->damage_rect = Rect::From(dirty_rect_); | 154 pass->damage_rect = Rect::From(dirty_rect_); |
| 145 | 155 |
| 146 DrawViewTree(pass.get(), connection_manager_->root(), gfx::Vector2d(), 1.0f); | 156 DrawViewTree(pass.get(), connection_manager_->root(), gfx::Vector2d(), 1.0f); |
| 147 | 157 |
| 148 auto frame = mojo::Frame::New(); | 158 auto frame = mojo::Frame::New(); |
| 149 frame->passes.push_back(pass.Pass()); | 159 frame->passes.push_back(pass.Pass()); |
| 150 frame->resources.resize(0u); | 160 frame->resources.resize(0u); |
| 151 frame_pending_ = true; | 161 frame_pending_ = true; |
| 152 display_->SubmitFrame( | 162 display_->SubmitFrame( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 163 | 173 |
| 164 void DefaultDisplayManager::WantToDraw() { | 174 void DefaultDisplayManager::WantToDraw() { |
| 165 if (draw_timer_.IsRunning() || frame_pending_) | 175 if (draw_timer_.IsRunning() || frame_pending_) |
| 166 return; | 176 return; |
| 167 | 177 |
| 168 draw_timer_.Start( | 178 draw_timer_.Start( |
| 169 FROM_HERE, base::TimeDelta(), | 179 FROM_HERE, base::TimeDelta(), |
| 170 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); | 180 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); |
| 171 } | 181 } |
| 172 | 182 |
| 173 void DefaultDisplayManager::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 183 void DefaultDisplayManager::OnAcceleratedWidgetAvailable( |
| 174 metrics_.size = metrics->size.Clone(); | 184 gfx::AcceleratedWidget widget, |
| 175 metrics_.device_pixel_ratio = metrics->device_pixel_ratio; | 185 float device_pixel_ratio) { |
| 176 gfx::Rect bounds(metrics_.size.To<gfx::Size>()); | 186 context_provider_->SetAcceleratedWidget(widget); |
| 177 connection_manager_->root()->SetBounds(bounds); | |
| 178 connection_manager_->ProcessViewportMetricsChanged(metrics_, *metrics); | |
| 179 native_viewport_->RequestMetrics(base::Bind( | |
| 180 &DefaultDisplayManager::OnMetricsChanged, weak_factory_.GetWeakPtr())); | |
| 181 } | 187 } |
| 182 | 188 |
| 183 void DefaultDisplayManager::OnConnectionError() { | 189 void DefaultDisplayManager::OnAcceleratedWidgetDestroyed() { |
| 184 // This is called when the native_viewport is torn down before | 190 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 185 // ~DefaultDisplayManager may be called. | 191 } |
| 186 native_viewport_closed_callback_.Run(); | 192 |
| 193 void DefaultDisplayManager::OnEvent(mojo::EventPtr event) { |
| 194 event_dispatcher_->OnEvent(event.Pass()); |
| 195 } |
| 196 |
| 197 void DefaultDisplayManager::OnMetricsChanged(const gfx::Size& size, |
| 198 float device_scale_factor) { |
| 199 if ((metrics_.size_in_pixels.To<gfx::Size>() == size) && |
| 200 (metrics_.device_pixel_ratio == device_scale_factor)) { |
| 201 return; |
| 202 } |
| 203 |
| 204 mojo::ViewportMetrics metrics; |
| 205 metrics.size_in_pixels = mojo::Size::From(size); |
| 206 metrics.device_pixel_ratio = device_scale_factor; |
| 207 |
| 208 connection_manager_->root()->SetBounds(gfx::Rect(size)); |
| 209 connection_manager_->ProcessViewportMetricsChanged(metrics_, metrics); |
| 210 |
| 211 metrics_.size_in_pixels = metrics.size_in_pixels.Clone(); |
| 212 metrics_.device_pixel_ratio = metrics.device_pixel_ratio; |
| 213 } |
| 214 |
| 215 void DefaultDisplayManager::OnDestroyed() { |
| 216 if (!platform_viewport_closed_callback_.is_null()) |
| 217 platform_viewport_closed_callback_.Run(); |
| 187 } | 218 } |
| 188 | 219 |
| 189 } // namespace view_manager | 220 } // namespace view_manager |
| OLD | NEW |