OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "image_pipe_producer_endpoint.h" |
| 6 |
| 7 namespace image_pipe { |
| 8 |
| 9 void ImagePipeProducerEndpoint::CloseEndpoint() { |
| 10 image_pipe_ptr_.reset(); |
| 11 endpoint_closed_callback_(); |
| 12 } |
| 13 |
| 14 ImagePipeProducerEndpoint::ImagePipeProducerEndpoint( |
| 15 mojo::gfx::ImagePipePtr image_pipe, |
| 16 std::function<void()> endpoint_closed_callback) |
| 17 : state_tracker_(true, [this]() { CloseEndpoint(); }), |
| 18 image_pipe_ptr_(image_pipe.Pass()) { |
| 19 endpoint_closed_callback_ = endpoint_closed_callback; |
| 20 image_pipe_ptr_.set_connection_error_handler([this] { CloseEndpoint(); }); |
| 21 } |
| 22 |
| 23 ImagePipeProducerEndpoint::~ImagePipeProducerEndpoint() {} |
| 24 |
| 25 bool ImagePipeProducerEndpoint::AcquireImage(uint32_t* out_id) { |
| 26 return state_tracker_.AcquireNextImage(out_id); |
| 27 } |
| 28 |
| 29 bool ImagePipeProducerEndpoint::AcquireImageBlocking(uint32_t* out_id, |
| 30 MojoDeadline deadline) { |
| 31 MojoDeadline remaining_deadline_ = deadline; |
| 32 bool have_image = false; |
| 33 do { |
| 34 have_image = state_tracker_.AcquireNextImage(out_id); |
| 35 if (!have_image && remaining_deadline_ > 0) { |
| 36 if (image_pipe_ptr_.encountered_error()) |
| 37 break; |
| 38 MojoTimeTicks wait_start = MojoGetTimeTicksNow(); |
| 39 image_pipe_ptr_.WaitForIncomingResponseWithTimeout(remaining_deadline_); |
| 40 MojoTimeTicks wait_end = MojoGetTimeTicksNow(); |
| 41 MojoTimeTicks wait_time = wait_end - wait_start; |
| 42 |
| 43 MOJO_DCHECK(wait_time >= 0); |
| 44 |
| 45 if (static_cast<MojoDeadline>(wait_time) > remaining_deadline_) { |
| 46 remaining_deadline_ = 0; // just to be safe |
| 47 } else { |
| 48 remaining_deadline_ -= wait_time; |
| 49 } |
| 50 |
| 51 if (image_pipe_ptr_.encountered_error()) |
| 52 break; |
| 53 } |
| 54 } while (!have_image); |
| 55 |
| 56 return have_image; |
| 57 } |
| 58 |
| 59 void ImagePipeProducerEndpoint::AddImage(mojo::gfx::ImagePtr image, |
| 60 uint32_t id) { |
| 61 state_tracker_.ProducerAdd(id); |
| 62 image_pipe_ptr_->AddImage(image.Pass(), id); |
| 63 } |
| 64 |
| 65 void ImagePipeProducerEndpoint::RemoveImage(uint32_t id) { |
| 66 state_tracker_.ProducerRemove(id); |
| 67 image_pipe_ptr_->RemoveImage(id); |
| 68 } |
| 69 |
| 70 void ImagePipeProducerEndpoint::ConsumerReleaseInternal( |
| 71 uint32_t id, |
| 72 mojo::gfx::PresentationStatus status) { |
| 73 state_tracker_.ConsumerRelease(id, status); |
| 74 } |
| 75 |
| 76 void ImagePipeProducerEndpoint::PresentImage( |
| 77 uint32_t id, |
| 78 const PresentImageCallback& callback) { |
| 79 state_tracker_.ProducerPresent(id, callback); |
| 80 image_pipe_ptr_->PresentImage( |
| 81 id, [this, id](mojo::gfx::PresentationStatus status) { |
| 82 ConsumerReleaseInternal(id, status); |
| 83 }); |
| 84 } |
| 85 |
| 86 void ImagePipeProducerEndpoint::FlushImages() { |
| 87 state_tracker_.ProducerFlush(); |
| 88 image_pipe_ptr_->FlushImages(); |
| 89 } |
| 90 |
| 91 void ImagePipeProducerEndpoint::DisableFatalErrorsForTesting() { |
| 92 state_tracker_.DisableFatalErrorsForTesting(); |
| 93 } |
| 94 |
| 95 bool ImagePipeProducerEndpoint::HasEncounteredError() { |
| 96 return image_pipe_ptr_.encountered_error(); |
| 97 } |
| 98 |
| 99 } // namespace image_pipe |
OLD | NEW |