| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "examples/ui/tile/tile_view.h" |
| 6 #include "mojo/services/surfaces/cpp/surfaces_utils.h" |
| 7 #include "mojo/services/surfaces/interfaces/quads.mojom.h" |
| 8 |
| 9 namespace examples { |
| 10 |
| 11 TileView::TileView(mojo::ApplicationImpl* app_impl, |
| 12 const std::vector<std::string>& view_urls, |
| 13 const mojo::ui::ViewProvider::CreateViewCallback& callback) |
| 14 : app_impl_(app_impl), |
| 15 view_urls_(view_urls), |
| 16 callback_(callback), |
| 17 binding_(this), |
| 18 surface_id_namespace_(0), |
| 19 pending_child_layout_count_(0), |
| 20 frame_pending_(false), |
| 21 weak_ptr_factory_(this) { |
| 22 app_impl_->ConnectToService("mojo:surfaces_service", &surfaces_); |
| 23 app_impl_->ConnectToService("mojo:view_manager_service", &view_manager_); |
| 24 |
| 25 surfaces_->GetIdNamespace(base::Bind(&TileView::OnSurfaceIdNamespaceAvailable, |
| 26 base::Unretained(this))); |
| 27 } |
| 28 |
| 29 TileView::~TileView() {} |
| 30 |
| 31 void TileView::OnSurfaceIdNamespaceAvailable(uint32_t id_namespace) { |
| 32 surface_id_namespace_ = id_namespace; |
| 33 InitView(); |
| 34 } |
| 35 |
| 36 void TileView::InitView() { |
| 37 // Register the view. |
| 38 mojo::ui::ViewPtr view; |
| 39 binding_.Bind(mojo::GetProxy(&view)); |
| 40 view_manager_->RegisterView(view.Pass(), mojo::GetProxy(&view_host_), |
| 41 callback_); |
| 42 |
| 43 // Connect to all child views. |
| 44 uint32_t child_key = 0; |
| 45 for (const auto& url : view_urls_) { |
| 46 // Start connecting to the view provider. |
| 47 mojo::ui::ViewProviderPtr provider; |
| 48 app_impl_->ConnectToService(url, &provider); |
| 49 provider.set_connection_error_handler( |
| 50 base::Bind(&TileView::OnChildConnectionError, base::Unretained(this), |
| 51 child_key, url)); |
| 52 |
| 53 // Create the view. |
| 54 // We include the provider reference in the callback so that the |
| 55 // binding will be kept alive until the callback completes. |
| 56 LOG(INFO) << "Connecting to view: child_key=" << child_key |
| 57 << ", url=" << url; |
| 58 provider->CreateView( |
| 59 nullptr, nullptr, |
| 60 base::Bind(&TileView::OnChildCreated, base::Unretained(this), child_key, |
| 61 url, base::Passed(provider.Pass()))); |
| 62 child_key++; |
| 63 } |
| 64 } |
| 65 |
| 66 void TileView::OnChildConnectionError(uint32_t child_key, |
| 67 const std::string& url) { |
| 68 LOG(ERROR) << "Could not connect to view: child_key=" << child_key |
| 69 << ", url=" << url; |
| 70 } |
| 71 |
| 72 void TileView::OnChildCreated(uint32_t child_key, |
| 73 const std::string& url, |
| 74 mojo::ui::ViewProviderPtr provider, |
| 75 mojo::ui::ViewTokenPtr token) { |
| 76 DCHECK(views_.find(child_key) == views_.end()); |
| 77 LOG(INFO) << "View created: child_key=" << child_key << ", url=" << url; |
| 78 |
| 79 view_host_->AddChild(child_key, token.Pass()); |
| 80 views_.emplace( |
| 81 std::make_pair(child_key, std::unique_ptr<ViewData>(new ViewData(url)))); |
| 82 |
| 83 // Note that the view provider will be destroyed once this function |
| 84 // returns which is fine now that we are done creating the view. |
| 85 } |
| 86 |
| 87 void TileView::OnChildUnavailable(uint32_t child_key, |
| 88 const OnChildUnavailableCallback& callback) { |
| 89 auto it = views_.find(child_key); |
| 90 DCHECK(it != views_.end()); |
| 91 LOG(ERROR) << "View died unexpectedly: child_key=" << child_key |
| 92 << ", url=" << it->second->url; |
| 93 |
| 94 std::unique_ptr<ViewData> view_data = std::move(it->second); |
| 95 views_.erase(it); |
| 96 |
| 97 view_host_->RemoveChild(child_key); |
| 98 |
| 99 if (view_data->layout_pending) { |
| 100 DCHECK(pending_child_layout_count_); |
| 101 pending_child_layout_count_--; |
| 102 FinishLayout(); |
| 103 } |
| 104 |
| 105 callback.Run(); |
| 106 } |
| 107 |
| 108 void TileView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params, |
| 109 mojo::Array<uint32_t> children_needing_layout, |
| 110 const OnLayoutCallback& callback) { |
| 111 // Create a new surface the first time or if the size has changed. |
| 112 mojo::Size new_size; |
| 113 new_size.width = layout_params->constraints->max_width; |
| 114 new_size.height = layout_params->constraints->max_height; |
| 115 if (!surface_id_ || !size_.Equals(new_size)) { |
| 116 if (!surface_id_) { |
| 117 surface_id_ = mojo::SurfaceId::New(); |
| 118 surface_id_->id_namespace = surface_id_namespace_; |
| 119 } else { |
| 120 surfaces_->DestroySurface(surface_id_->local); |
| 121 } |
| 122 surface_id_->local++; |
| 123 size_ = new_size; |
| 124 surfaces_->CreateSurface(surface_id_->local); |
| 125 } |
| 126 |
| 127 // Wipe out cached layout information for children needing layout. |
| 128 for (uint32_t child_key : children_needing_layout) { |
| 129 auto view_it = views_.find(child_key); |
| 130 if (view_it != views_.end()) |
| 131 view_it->second->layout_info.reset(); |
| 132 } |
| 133 |
| 134 // Layout all children in a row. |
| 135 if (!views_.empty()) { |
| 136 uint32_t index = 0; |
| 137 uint32_t base_width = new_size.width / views_.size(); |
| 138 uint32_t excess_width = new_size.width % views_.size(); |
| 139 uint32_t x = 0; |
| 140 for (auto it = views_.begin(); it != views_.end(); ++it, ++index) { |
| 141 ViewData* view_data = it->second.get(); |
| 142 DCHECK(!view_data->layout_pending); |
| 143 |
| 144 // Distribute any excess width among the leading children. |
| 145 uint32_t child_width = base_width; |
| 146 if (excess_width) { |
| 147 child_width++; |
| 148 excess_width--; |
| 149 } |
| 150 uint32_t child_height = new_size.height; |
| 151 uint32_t child_x = x; |
| 152 x += child_width; |
| 153 |
| 154 view_data->layout_bounds.x = child_x; |
| 155 view_data->layout_bounds.y = 0; |
| 156 view_data->layout_bounds.width = child_width; |
| 157 view_data->layout_bounds.height = child_height; |
| 158 |
| 159 mojo::ui::ViewLayoutParamsPtr params = mojo::ui::ViewLayoutParams::New(); |
| 160 params->constraints = mojo::ui::BoxConstraints::New(); |
| 161 params->constraints->min_width = child_width; |
| 162 params->constraints->max_width = child_width; |
| 163 params->constraints->min_height = child_height; |
| 164 params->constraints->max_height = child_height; |
| 165 params->device_pixel_ratio = layout_params->device_pixel_ratio; |
| 166 |
| 167 if (view_data->layout_info && view_data->layout_params.Equals(params)) |
| 168 continue; // no layout work to do |
| 169 |
| 170 pending_child_layout_count_++; |
| 171 view_data->layout_pending = true; |
| 172 view_data->layout_params = params.Clone(); |
| 173 view_data->layout_info.reset(); |
| 174 |
| 175 view_host_->LayoutChild(it->first, params.Pass(), |
| 176 base::Bind(&TileView::OnChildLayoutFinished, |
| 177 base::Unretained(this), it->first)); |
| 178 } |
| 179 } |
| 180 |
| 181 // Store the callback until layout of all children is finished. |
| 182 pending_layout_callback_ = callback; |
| 183 FinishLayout(); |
| 184 } |
| 185 |
| 186 void TileView::OnChildLayoutFinished( |
| 187 uint32_t child_key, |
| 188 mojo::ui::ViewLayoutInfoPtr child_layout_info) { |
| 189 auto it = views_.find(child_key); |
| 190 if (it != views_.end()) { |
| 191 ViewData* view_data = it->second.get(); |
| 192 DCHECK(view_data->layout_pending); |
| 193 DCHECK(pending_child_layout_count_); |
| 194 pending_child_layout_count_--; |
| 195 view_data->layout_pending = false; |
| 196 view_data->layout_info = child_layout_info.Pass(); |
| 197 FinishLayout(); |
| 198 } |
| 199 } |
| 200 |
| 201 void TileView::FinishLayout() { |
| 202 if (frame_pending_ || pending_layout_callback_.is_null()) |
| 203 return; |
| 204 |
| 205 // Wait until all children have laid out. |
| 206 // TODO(jeffbrown): There should be a timeout on this. |
| 207 if (pending_child_layout_count_) |
| 208 return; |
| 209 |
| 210 // Produce a new frame. |
| 211 mojo::FramePtr frame = mojo::Frame::New(); |
| 212 frame->resources.resize(0u); |
| 213 |
| 214 mojo::Rect bounds; |
| 215 bounds.width = size_.width; |
| 216 bounds.height = size_.height; |
| 217 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); |
| 218 pass->shared_quad_states.resize(0u); |
| 219 pass->quads.resize(0u); |
| 220 |
| 221 for (auto it = views_.cbegin(); it != views_.cend(); it++) { |
| 222 const ViewData& view_data = *(it->second.get()); |
| 223 |
| 224 mojo::QuadPtr quad = mojo::Quad::New(); |
| 225 quad->rect = view_data.layout_bounds.Clone(); |
| 226 quad->rect->x = 0; |
| 227 quad->rect->y = 0; |
| 228 quad->opaque_rect = quad->rect.Clone(); |
| 229 quad->visible_rect = quad->rect.Clone(); |
| 230 quad->shared_quad_state_index = pass->shared_quad_states.size(); |
| 231 |
| 232 mojo::Size size; |
| 233 size.width = view_data.layout_bounds.width; |
| 234 size.height = view_data.layout_bounds.height; |
| 235 |
| 236 mojo::SharedQuadStatePtr quad_state = mojo::CreateDefaultSQS(size); |
| 237 quad_state->content_to_target_transform->matrix[3] = |
| 238 view_data.layout_bounds.x; |
| 239 pass->shared_quad_states.push_back(quad_state.Pass()); |
| 240 |
| 241 if (it->second->layout_info) { |
| 242 quad->material = mojo::Material::SURFACE_CONTENT; |
| 243 quad->surface_quad_state = mojo::SurfaceQuadState::New(); |
| 244 quad->surface_quad_state->surface = |
| 245 view_data.layout_info->surface_id.Clone(); |
| 246 } else { |
| 247 quad->material = mojo::Material::SOLID_COLOR; |
| 248 quad->solid_color_quad_state = mojo::SolidColorQuadState::New(); |
| 249 quad->solid_color_quad_state->color = mojo::Color::New(); |
| 250 quad->solid_color_quad_state->color->rgba = 0xffff00ff; |
| 251 } |
| 252 |
| 253 pass->quads.push_back(quad.Pass()); |
| 254 } |
| 255 |
| 256 frame->passes.push_back(pass.Pass()); |
| 257 |
| 258 frame_pending_ = true; |
| 259 surfaces_->SubmitFrame( |
| 260 surface_id_->local, frame.Pass(), |
| 261 base::Bind(&TileView::OnFrameSubmitted, base::Unretained(this))); |
| 262 |
| 263 // Submit the new layout information. |
| 264 mojo::ui::ViewLayoutInfoPtr info = mojo::ui::ViewLayoutInfo::New(); |
| 265 info->size = size_.Clone(); |
| 266 info->surface_id = surface_id_->Clone(); |
| 267 pending_layout_callback_.Run(info.Pass()); |
| 268 pending_layout_callback_.reset(); |
| 269 } |
| 270 |
| 271 void TileView::OnFrameSubmitted() { |
| 272 DCHECK(frame_pending_); |
| 273 |
| 274 frame_pending_ = false; |
| 275 FinishLayout(); |
| 276 } |
| 277 |
| 278 TileView::ViewData::ViewData(const std::string& url) |
| 279 : url(url), layout_pending(false) {} |
| 280 |
| 281 TileView::ViewData::~ViewData() {} |
| 282 |
| 283 } // namespace examples |
| OLD | NEW |