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