OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/services/view_manager/display_manager.h" |
| 6 |
| 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 9 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 10 #include "mojo/converters/transform/transform_type_converters.h" |
| 11 #include "mojo/public/cpp/application/application_connection.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/services/view_manager/connection_manager.h" |
| 14 #include "mojo/services/view_manager/server_view.h" |
| 15 #include "mojo/services/view_manager/view_coordinate_conversions.h" |
| 16 #include "third_party/mojo_services/src/gpu/public/interfaces/gpu.mojom.h" |
| 17 #include "third_party/mojo_services/src/surfaces/public/cpp/surfaces_utils.h" |
| 18 #include "third_party/mojo_services/src/surfaces/public/interfaces/quads.mojom.h
" |
| 19 #include "third_party/mojo_services/src/surfaces/public/interfaces/surfaces.mojo
m.h" |
| 20 |
| 21 using mojo::Rect; |
| 22 using mojo::Size; |
| 23 |
| 24 namespace view_manager { |
| 25 namespace { |
| 26 |
| 27 void DrawViewTree(mojo::Pass* pass, |
| 28 const ServerView* view, |
| 29 const gfx::Vector2d& parent_to_root_origin_offset, |
| 30 float opacity) { |
| 31 if (!view->visible()) |
| 32 return; |
| 33 |
| 34 const gfx::Rect absolute_bounds = |
| 35 view->bounds() + parent_to_root_origin_offset; |
| 36 std::vector<const ServerView*> children(view->GetChildren()); |
| 37 const float combined_opacity = opacity * view->opacity(); |
| 38 for (std::vector<const ServerView*>::reverse_iterator it = children.rbegin(); |
| 39 it != children.rend(); |
| 40 ++it) { |
| 41 DrawViewTree(pass, *it, absolute_bounds.OffsetFromOrigin(), |
| 42 combined_opacity); |
| 43 } |
| 44 |
| 45 cc::SurfaceId node_id = view->surface_id(); |
| 46 |
| 47 auto surface_quad_state = mojo::SurfaceQuadState::New(); |
| 48 surface_quad_state->surface = mojo::SurfaceId::From(node_id); |
| 49 |
| 50 gfx::Transform node_transform; |
| 51 node_transform.Translate(absolute_bounds.x(), absolute_bounds.y()); |
| 52 |
| 53 const gfx::Rect bounds_at_origin(view->bounds().size()); |
| 54 auto surface_quad = mojo::Quad::New(); |
| 55 surface_quad->material = mojo::Material::MATERIAL_SURFACE_CONTENT; |
| 56 surface_quad->rect = Rect::From(bounds_at_origin); |
| 57 surface_quad->opaque_rect = Rect::From(bounds_at_origin); |
| 58 surface_quad->visible_rect = Rect::From(bounds_at_origin); |
| 59 surface_quad->needs_blending = true; |
| 60 surface_quad->shared_quad_state_index = |
| 61 base::saturated_cast<int32_t>(pass->shared_quad_states.size()); |
| 62 surface_quad->surface_quad_state = surface_quad_state.Pass(); |
| 63 |
| 64 auto sqs = CreateDefaultSQS(*Size::From(view->bounds().size())); |
| 65 sqs->blend_mode = mojo::SK_XFERMODE_kSrcOver_Mode; |
| 66 sqs->opacity = combined_opacity; |
| 67 sqs->content_to_target_transform = mojo::Transform::From(node_transform); |
| 68 |
| 69 pass->quads.push_back(surface_quad.Pass()); |
| 70 pass->shared_quad_states.push_back(sqs.Pass()); |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 DefaultDisplayManager::DefaultDisplayManager( |
| 76 mojo::ApplicationImpl* app_impl, |
| 77 mojo::ApplicationConnection* app_connection, |
| 78 const mojo::Callback<void()>& native_viewport_closed_callback) |
| 79 : app_impl_(app_impl), |
| 80 app_connection_(app_connection), |
| 81 connection_manager_(nullptr), |
| 82 draw_timer_(false, false), |
| 83 frame_pending_(false), |
| 84 native_viewport_closed_callback_(native_viewport_closed_callback), |
| 85 weak_factory_(this) { |
| 86 metrics_.size = mojo::Size::New(); |
| 87 metrics_.size->width = 800; |
| 88 metrics_.size->height = 600; |
| 89 } |
| 90 |
| 91 void DefaultDisplayManager::Init(ConnectionManager* connection_manager) { |
| 92 connection_manager_ = connection_manager; |
| 93 app_impl_->ConnectToService("mojo:native_viewport_service", |
| 94 &native_viewport_); |
| 95 native_viewport_.set_error_handler(this); |
| 96 native_viewport_->Create(metrics_.size->Clone(), |
| 97 base::Bind(&DefaultDisplayManager::OnMetricsChanged, |
| 98 weak_factory_.GetWeakPtr())); |
| 99 native_viewport_->Show(); |
| 100 |
| 101 mojo::ContextProviderPtr context_provider; |
| 102 native_viewport_->GetContextProvider(GetProxy(&context_provider)); |
| 103 mojo::DisplayFactoryPtr display_factory; |
| 104 app_impl_->ConnectToService("mojo:surfaces_service", &display_factory); |
| 105 display_factory->Create(context_provider.Pass(), |
| 106 nullptr, // returner - we never submit resources. |
| 107 GetProxy(&display_)); |
| 108 |
| 109 mojo::NativeViewportEventDispatcherPtr event_dispatcher; |
| 110 app_connection_->ConnectToService(&event_dispatcher); |
| 111 native_viewport_->SetEventDispatcher(event_dispatcher.Pass()); |
| 112 } |
| 113 |
| 114 DefaultDisplayManager::~DefaultDisplayManager() { |
| 115 } |
| 116 |
| 117 void DefaultDisplayManager::SchedulePaint(const ServerView* view, |
| 118 const gfx::Rect& bounds) { |
| 119 if (!view->IsDrawn(connection_manager_->root())) |
| 120 return; |
| 121 const gfx::Rect root_relative_rect = |
| 122 ConvertRectBetweenViews(view, connection_manager_->root(), bounds); |
| 123 if (root_relative_rect.IsEmpty()) |
| 124 return; |
| 125 dirty_rect_.Union(root_relative_rect); |
| 126 WantToDraw(); |
| 127 } |
| 128 |
| 129 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { |
| 130 native_viewport_->SetSize(Size::From(size)); |
| 131 } |
| 132 |
| 133 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { |
| 134 return metrics_; |
| 135 } |
| 136 |
| 137 void DefaultDisplayManager::Draw() { |
| 138 Rect rect; |
| 139 rect.width = metrics_.size->width; |
| 140 rect.height = metrics_.size->height; |
| 141 auto pass = CreateDefaultPass(1, rect); |
| 142 pass->damage_rect = Rect::From(dirty_rect_); |
| 143 |
| 144 DrawViewTree(pass.get(), connection_manager_->root(), gfx::Vector2d(), 1.0f); |
| 145 |
| 146 auto frame = mojo::Frame::New(); |
| 147 frame->passes.push_back(pass.Pass()); |
| 148 frame->resources.resize(0u); |
| 149 frame_pending_ = true; |
| 150 display_->SubmitFrame( |
| 151 frame.Pass(), |
| 152 base::Bind(&DefaultDisplayManager::DidDraw, base::Unretained(this))); |
| 153 dirty_rect_ = gfx::Rect(); |
| 154 } |
| 155 |
| 156 void DefaultDisplayManager::DidDraw() { |
| 157 frame_pending_ = false; |
| 158 if (!dirty_rect_.IsEmpty()) |
| 159 WantToDraw(); |
| 160 } |
| 161 |
| 162 void DefaultDisplayManager::WantToDraw() { |
| 163 if (draw_timer_.IsRunning() || frame_pending_) |
| 164 return; |
| 165 |
| 166 draw_timer_.Start( |
| 167 FROM_HERE, base::TimeDelta(), |
| 168 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); |
| 169 } |
| 170 |
| 171 void DefaultDisplayManager::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
| 172 metrics_.size = metrics->size.Clone(); |
| 173 metrics_.device_pixel_ratio = metrics->device_pixel_ratio; |
| 174 gfx::Rect bounds(metrics_.size.To<gfx::Size>()); |
| 175 connection_manager_->root()->SetBounds(bounds); |
| 176 connection_manager_->ProcessViewportMetricsChanged(metrics_, *metrics); |
| 177 native_viewport_->RequestMetrics(base::Bind( |
| 178 &DefaultDisplayManager::OnMetricsChanged, weak_factory_.GetWeakPtr())); |
| 179 } |
| 180 |
| 181 void DefaultDisplayManager::OnConnectionError() { |
| 182 // This is called when the native_viewport is torn down before |
| 183 // ~DefaultDisplayManager may be called. |
| 184 native_viewport_closed_callback_.Run(); |
| 185 } |
| 186 |
| 187 } // namespace view_manager |
OLD | NEW |