| 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 "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/public/cpp/application/application_connection.h" | |
| 11 #include "mojo/public/cpp/application/application_impl.h" | |
| 12 #include "mojo/services/gpu/interfaces/gpu.mojom.h" | |
| 13 #include "mojo/services/surfaces/cpp/surfaces_utils.h" | |
| 14 #include "mojo/services/surfaces/interfaces/quads.mojom.h" | |
| 15 #include "mojo/services/surfaces/interfaces/surfaces.mojom.h" | |
| 16 #include "services/view_manager/connection_manager.h" | |
| 17 #include "services/view_manager/server_view.h" | |
| 18 #include "services/view_manager/view_coordinate_conversions.h" | |
| 19 | |
| 20 namespace view_manager { | |
| 21 namespace { | |
| 22 | |
| 23 void DrawViewTree(mojo::Pass* pass, | |
| 24 const ServerView* view, | |
| 25 const gfx::Vector2d& parent_to_root_origin_offset, | |
| 26 float opacity) { | |
| 27 if (!view->visible()) | |
| 28 return; | |
| 29 | |
| 30 const gfx::Rect absolute_bounds = | |
| 31 view->bounds() + parent_to_root_origin_offset; | |
| 32 std::vector<const ServerView*> children(view->GetChildren()); | |
| 33 const float combined_opacity = opacity * view->opacity(); | |
| 34 for (std::vector<const ServerView*>::reverse_iterator it = children.rbegin(); | |
| 35 it != children.rend(); | |
| 36 ++it) { | |
| 37 DrawViewTree(pass, *it, absolute_bounds.OffsetFromOrigin(), | |
| 38 combined_opacity); | |
| 39 } | |
| 40 | |
| 41 cc::SurfaceId node_id = view->surface_id(); | |
| 42 | |
| 43 auto surface_quad_state = mojo::SurfaceQuadState::New(); | |
| 44 surface_quad_state->surface = mojo::SurfaceId::From(node_id); | |
| 45 | |
| 46 gfx::Transform node_transform; | |
| 47 node_transform.Translate(absolute_bounds.x(), absolute_bounds.y()); | |
| 48 | |
| 49 const gfx::Rect bounds_at_origin(view->bounds().size()); | |
| 50 auto surface_quad = mojo::Quad::New(); | |
| 51 surface_quad->material = mojo::Material::SURFACE_CONTENT; | |
| 52 surface_quad->rect = mojo::Rect::From(bounds_at_origin); | |
| 53 surface_quad->opaque_rect = mojo::Rect::From(bounds_at_origin); | |
| 54 surface_quad->visible_rect = mojo::Rect::From(bounds_at_origin); | |
| 55 surface_quad->needs_blending = true; | |
| 56 surface_quad->shared_quad_state_index = | |
| 57 base::saturated_cast<int32_t>(pass->shared_quad_states.size()); | |
| 58 surface_quad->surface_quad_state = surface_quad_state.Pass(); | |
| 59 | |
| 60 auto sqs = CreateDefaultSQS(*mojo::Size::From(view->bounds().size())); | |
| 61 sqs->blend_mode = mojo::SkXfermode::kSrcOver_Mode; | |
| 62 sqs->opacity = combined_opacity; | |
| 63 sqs->content_to_target_transform = mojo::Transform::From(node_transform); | |
| 64 | |
| 65 pass->quads.push_back(surface_quad.Pass()); | |
| 66 pass->shared_quad_states.push_back(sqs.Pass()); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 DefaultDisplayManager::DefaultDisplayManager( | |
| 72 mojo::ApplicationImpl* app_impl, | |
| 73 mojo::ApplicationConnection* app_connection, | |
| 74 const mojo::Closure& native_viewport_closed_callback) | |
| 75 : app_impl_(app_impl), | |
| 76 app_connection_(app_connection), | |
| 77 connection_manager_(nullptr), | |
| 78 draw_timer_(false, false), | |
| 79 frame_pending_(false), | |
| 80 native_viewport_closed_callback_(native_viewport_closed_callback), | |
| 81 weak_factory_(this) { | |
| 82 metrics_.size = mojo::Size::New(); | |
| 83 metrics_.size->width = 800; | |
| 84 metrics_.size->height = 600; | |
| 85 } | |
| 86 | |
| 87 void DefaultDisplayManager::Init(ConnectionManager* connection_manager) { | |
| 88 connection_manager_ = connection_manager; | |
| 89 app_impl_->ConnectToService("mojo:native_viewport_service", | |
| 90 &native_viewport_); | |
| 91 // The connection error handler will be called if native_viewport_ is torn | |
| 92 // down before this object is destroyed. | |
| 93 native_viewport_.set_connection_error_handler( | |
| 94 [this]() { native_viewport_closed_callback_.Run(); }); | |
| 95 native_viewport_->Create(metrics_.size->Clone(), | |
| 96 mojo::SurfaceConfiguration::New(), | |
| 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(mojo::Size::From(size)); | |
| 131 } | |
| 132 | |
| 133 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { | |
| 134 return metrics_; | |
| 135 } | |
| 136 | |
| 137 void DefaultDisplayManager::Draw() { | |
| 138 mojo::Rect rect; | |
| 139 rect.width = metrics_.size->width; | |
| 140 rect.height = metrics_.size->height; | |
| 141 auto pass = CreateDefaultPass(1, rect); | |
| 142 pass->damage_rect = mojo::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 } // namespace view_manager | |
| OLD | NEW |