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 "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" | 5 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/public/cpp/shell/connect.h" | 9 #include "mojo/public/cpp/shell/connect.h" |
10 #include "mojo/public/interfaces/shell/shell.mojom.h" | 10 #include "mojo/public/interfaces/shell/shell.mojom.h" |
11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" | 11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" |
12 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 12 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" |
13 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" | 13 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" |
14 #include "mojo/services/public/cpp/view_manager/util.h" | 14 #include "mojo/services/public/cpp/view_manager/util.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/codec/png_codec.h" |
15 | 17 |
16 namespace mojo { | 18 namespace mojo { |
17 namespace view_manager { | 19 namespace view_manager { |
18 | 20 |
19 uint32_t MakeTransportId(uint16_t connection_id, uint16_t local_id) { | 21 uint32_t MakeTransportId(uint16_t connection_id, uint16_t local_id) { |
20 return (connection_id << 16) | local_id; | 22 return (connection_id << 16) | local_id; |
21 } | 23 } |
22 | 24 |
23 // Helper called to construct a local node/view object from transport data. | 25 // Helper called to construct a local node/view object from transport data. |
24 ViewTreeNode* AddNodeToViewManager(ViewManager* manager, | 26 ViewTreeNode* AddNodeToViewManager(ViewManager* manager, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 TYPE_DESTROY_VIEW, | 96 TYPE_DESTROY_VIEW, |
95 // Node creation and destruction. | 97 // Node creation and destruction. |
96 TYPE_CREATE_VIEW_TREE_NODE, | 98 TYPE_CREATE_VIEW_TREE_NODE, |
97 TYPE_DESTROY_VIEW_TREE_NODE, | 99 TYPE_DESTROY_VIEW_TREE_NODE, |
98 // Modifications to the hierarchy (addition of or removal of nodes from a | 100 // Modifications to the hierarchy (addition of or removal of nodes from a |
99 // parent.) | 101 // parent.) |
100 TYPE_HIERARCHY, | 102 TYPE_HIERARCHY, |
101 // View replacement. | 103 // View replacement. |
102 TYPE_SET_ACTIVE_VIEW, | 104 TYPE_SET_ACTIVE_VIEW, |
103 // Node bounds. | 105 // Node bounds. |
104 TYPE_SET_BOUNDS | 106 TYPE_SET_BOUNDS, |
| 107 // View contents |
| 108 TYPE_SET_VIEW_CONTENTS |
105 }; | 109 }; |
106 | 110 |
107 ViewManagerTransaction(TransactionType transaction_type, | 111 ViewManagerTransaction(TransactionType transaction_type, |
108 ViewManagerSynchronizer* synchronizer) | 112 ViewManagerSynchronizer* synchronizer) |
109 : transaction_type_(transaction_type), | 113 : transaction_type_(transaction_type), |
110 committed_(false), | 114 committed_(false), |
111 synchronizer_(synchronizer) { | 115 synchronizer_(synchronizer) { |
112 } | 116 } |
113 | 117 |
114 // Overridden to perform transaction-specific commit actions. | 118 // Overridden to perform transaction-specific commit actions. |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 virtual void DoActionCompleted(bool success) OVERRIDE { | 330 virtual void DoActionCompleted(bool success) OVERRIDE { |
327 // TODO(beng): recovery? | 331 // TODO(beng): recovery? |
328 } | 332 } |
329 | 333 |
330 const TransportNodeId node_id_; | 334 const TransportNodeId node_id_; |
331 const gfx::Rect bounds_; | 335 const gfx::Rect bounds_; |
332 | 336 |
333 DISALLOW_COPY_AND_ASSIGN(SetBoundsTransaction); | 337 DISALLOW_COPY_AND_ASSIGN(SetBoundsTransaction); |
334 }; | 338 }; |
335 | 339 |
| 340 class SetViewContentsTransaction : public ViewManagerTransaction { |
| 341 public: |
| 342 SetViewContentsTransaction(TransportViewId view_id, |
| 343 const SkBitmap& contents, |
| 344 ViewManagerSynchronizer* synchronizer) |
| 345 : ViewManagerTransaction(TYPE_SET_VIEW_CONTENTS, synchronizer), |
| 346 view_id_(view_id), |
| 347 contents_(contents) {} |
| 348 virtual ~SetViewContentsTransaction() {} |
| 349 |
| 350 private: |
| 351 // Overridden from ViewManagerTransaction: |
| 352 virtual void DoCommit() OVERRIDE { |
| 353 std::vector<unsigned char> data; |
| 354 gfx::PNGCodec::EncodeBGRASkBitmap(contents_, false, &data); |
| 355 |
| 356 void* memory = NULL; |
| 357 ScopedSharedBufferHandle duped; |
| 358 bool result = CreateMapAndDupSharedBuffer(data.size(), |
| 359 &memory, |
| 360 &shared_state_handle_, |
| 361 &duped); |
| 362 if (!result) |
| 363 return; |
| 364 |
| 365 memcpy(memory, &data[0], data.size()); |
| 366 |
| 367 AllocationScope scope; |
| 368 service()->SetViewContents(view_id_, duped.Pass(), data.size(), |
| 369 ActionCompletedCallback()); |
| 370 } |
| 371 virtual void DoActionCompleted(bool success) OVERRIDE { |
| 372 // TODO(beng): recovery? |
| 373 } |
| 374 |
| 375 bool CreateMapAndDupSharedBuffer(size_t size, |
| 376 void** memory, |
| 377 ScopedSharedBufferHandle* handle, |
| 378 ScopedSharedBufferHandle* duped) { |
| 379 MojoResult result = CreateSharedBuffer(NULL, size, handle); |
| 380 if (result != MOJO_RESULT_OK) |
| 381 return false; |
| 382 DCHECK(handle->is_valid()); |
| 383 |
| 384 result = DuplicateBuffer(handle->get(), NULL, duped); |
| 385 if (result != MOJO_RESULT_OK) |
| 386 return false; |
| 387 DCHECK(duped->is_valid()); |
| 388 |
| 389 result = MapBuffer( |
| 390 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); |
| 391 if (result != MOJO_RESULT_OK) |
| 392 return false; |
| 393 DCHECK(*memory); |
| 394 |
| 395 return true; |
| 396 } |
| 397 |
| 398 const TransportViewId view_id_; |
| 399 const SkBitmap contents_; |
| 400 ScopedSharedBufferHandle shared_state_handle_; |
| 401 |
| 402 DISALLOW_COPY_AND_ASSIGN(SetViewContentsTransaction); |
| 403 }; |
| 404 |
336 ViewManagerSynchronizer::ViewManagerSynchronizer(ViewManager* view_manager) | 405 ViewManagerSynchronizer::ViewManagerSynchronizer(ViewManager* view_manager) |
337 : view_manager_(view_manager), | 406 : view_manager_(view_manager), |
338 connected_(false), | 407 connected_(false), |
339 connection_id_(0), | 408 connection_id_(0), |
340 next_id_(1), | 409 next_id_(1), |
341 next_server_change_id_(0), | 410 next_server_change_id_(0), |
342 sync_factory_(this), | 411 sync_factory_(this), |
343 init_loop_(NULL) { | 412 init_loop_(NULL) { |
344 ConnectTo(ViewManagerPrivate(view_manager_).shell(), "mojo:mojo_view_manager", | 413 ConnectTo(ViewManagerPrivate(view_manager_).shell(), "mojo:mojo_view_manager", |
345 &service_); | 414 &service_); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 } | 497 } |
429 | 498 |
430 void ViewManagerSynchronizer::SetBounds(TransportNodeId node_id, | 499 void ViewManagerSynchronizer::SetBounds(TransportNodeId node_id, |
431 const gfx::Rect& bounds) { | 500 const gfx::Rect& bounds) { |
432 DCHECK(connected_); | 501 DCHECK(connected_); |
433 pending_transactions_.push_back( | 502 pending_transactions_.push_back( |
434 new SetBoundsTransaction(node_id, bounds, this)); | 503 new SetBoundsTransaction(node_id, bounds, this)); |
435 Sync(); | 504 Sync(); |
436 } | 505 } |
437 | 506 |
| 507 void ViewManagerSynchronizer::SetViewContents(TransportViewId view_id, |
| 508 const SkBitmap& contents) { |
| 509 DCHECK(connected_); |
| 510 pending_transactions_.push_back( |
| 511 new SetViewContentsTransaction(view_id, contents, this)); |
| 512 Sync(); |
| 513 } |
| 514 |
438 //////////////////////////////////////////////////////////////////////////////// | 515 //////////////////////////////////////////////////////////////////////////////// |
439 // ViewManagerSynchronizer, IViewManagerClient implementation: | 516 // ViewManagerSynchronizer, IViewManagerClient implementation: |
440 | 517 |
441 void ViewManagerSynchronizer::OnConnectionEstablished( | 518 void ViewManagerSynchronizer::OnConnectionEstablished( |
442 TransportConnectionId connection_id, | 519 TransportConnectionId connection_id, |
443 TransportChangeId next_server_change_id, | 520 TransportChangeId next_server_change_id, |
444 const Array<INode>& nodes) { | 521 const Array<INode>& nodes) { |
445 connected_ = true; | 522 connected_ = true; |
446 connection_id_ = connection_id; | 523 connection_id_ = connection_id; |
447 next_server_change_id_ = next_server_change_id; | 524 next_server_change_id_ = next_server_change_id; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 void ViewManagerSynchronizer::RemoveFromPendingQueue( | 616 void ViewManagerSynchronizer::RemoveFromPendingQueue( |
540 ViewManagerTransaction* transaction) { | 617 ViewManagerTransaction* transaction) { |
541 DCHECK_EQ(transaction, pending_transactions_.front()); | 618 DCHECK_EQ(transaction, pending_transactions_.front()); |
542 pending_transactions_.erase(pending_transactions_.begin()); | 619 pending_transactions_.erase(pending_transactions_.begin()); |
543 if (pending_transactions_.empty() && !changes_acked_callback_.is_null()) | 620 if (pending_transactions_.empty() && !changes_acked_callback_.is_null()) |
544 changes_acked_callback_.Run(); | 621 changes_acked_callback_.Run(); |
545 } | 622 } |
546 | 623 |
547 } // namespace view_manager | 624 } // namespace view_manager |
548 } // namespace mojo | 625 } // namespace mojo |
OLD | NEW |