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_endpoint.h" |
| 6 |
| 7 namespace image_pipe { |
| 8 |
| 9 void ImagePipeEndpoint::ImagePipeLogError(const std::string& entity, |
| 10 const std::string& message, |
| 11 uint32_t id) { |
| 12 MOJO_LOG(ERROR) << "ImagePipe " << entity << " Error on Image ID " << id |
| 13 << ": " << message; |
| 14 } |
| 15 |
| 16 void ImagePipeEndpoint::ProducerFatalError(const std::string& message, |
| 17 uint32_t id) { |
| 18 if (is_checked_) { |
| 19 ImagePipeLogError("Producer", message, id); |
| 20 if (is_producer_) { |
| 21 MOJO_CHECK(false); |
| 22 } else { |
| 23 fatal_error_handler_(); |
| 24 } |
| 25 } |
| 26 } |
| 27 |
| 28 void ImagePipeEndpoint::ConsumerFatalError(const std::string& message, |
| 29 uint32_t id) { |
| 30 ImagePipeLogError("Consumer", message, id); |
| 31 if (is_producer_) { |
| 32 fatal_error_handler_(); |
| 33 } else { |
| 34 if (is_checked_) { |
| 35 MOJO_CHECK(false); |
| 36 } else { |
| 37 fatal_error_handler_(); |
| 38 } |
| 39 } |
| 40 } |
| 41 |
| 42 ImagePipeEndpoint::ImagePipeEndpoint(bool is_producer, |
| 43 std::function<void()> fatal_error_handler) |
| 44 : is_producer_(is_producer), |
| 45 is_checked_(true), |
| 46 fatal_error_handler_(fatal_error_handler) {} |
| 47 |
| 48 ImagePipeEndpoint::~ImagePipeEndpoint() {} |
| 49 |
| 50 void ImagePipeEndpoint::ProducerAdd(uint32_t id) { |
| 51 if (IsInPool(id)) { |
| 52 ProducerFatalError("Attempting to add an image that is already in the pool", |
| 53 id); |
| 54 } else { |
| 55 image_pool_ids_.insert(id); |
| 56 if (is_producer_) { |
| 57 producer_acquirable_ids_.push_back(id); |
| 58 } else { |
| 59 producer_owned_ids_.insert(id); |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 bool ImagePipeEndpoint::AcquireNextImage(uint32_t* out_id) { |
| 65 auto acquirable_ids_ = |
| 66 is_producer_ ? &producer_acquirable_ids_ : &consumer_acquirable_ids_; |
| 67 auto owned_ids_ = is_producer_ ? &producer_owned_ids_ : &consumer_owned_ids_; |
| 68 |
| 69 if (acquirable_ids_->empty()) { |
| 70 return false; |
| 71 } |
| 72 |
| 73 int id = acquirable_ids_->front(); |
| 74 acquirable_ids_->pop_front(); |
| 75 owned_ids_->insert(id); |
| 76 *out_id = id; |
| 77 return true; |
| 78 } |
| 79 |
| 80 void ImagePipeEndpoint::ProducerRemove(uint32_t id) { |
| 81 if (IsInPool(id)) { |
| 82 if ((IsConsumerOwned(id) || IsConsumerAcquirable(id))) { |
| 83 ProducerFatalError( |
| 84 "Attempting to remove an image that has been presented " |
| 85 "but has not been released by the consumer", |
| 86 id); |
| 87 } else { |
| 88 auto image_pool_ids_iter = |
| 89 std::find(image_pool_ids_.begin(), image_pool_ids_.end(), id); |
| 90 image_pool_ids_.erase(image_pool_ids_iter); |
| 91 MOJO_DCHECK(!is_checked_ || IsProducerOwned(id) || |
| 92 IsProducerAcquirable(id)); |
| 93 if (IsProducerOwned(id)) { |
| 94 MOJO_DCHECK(!is_checked_ || !IsProducerAcquirable(id)); |
| 95 auto producer_owned_ids_iter = std::find(producer_owned_ids_.begin(), |
| 96 producer_owned_ids_.end(), id); |
| 97 producer_owned_ids_.erase(producer_owned_ids_iter); |
| 98 MOJO_DCHECK(!is_checked_ || !IsProducerOwned(id)); |
| 99 } else { |
| 100 MOJO_DCHECK(!is_checked_ || IsProducerAcquirable(id)); |
| 101 auto producer_acquirable_ids_iter = |
| 102 std::find(producer_acquirable_ids_.begin(), |
| 103 producer_acquirable_ids_.end(), id); |
| 104 producer_acquirable_ids_.erase(producer_acquirable_ids_iter); |
| 105 MOJO_DCHECK(!is_checked_ || !IsProducerAcquirable(id)); |
| 106 } |
| 107 } |
| 108 } else { |
| 109 ProducerFatalError( |
| 110 "Attempting to remove an image that is not in the image pool", id); |
| 111 } |
| 112 } |
| 113 |
| 114 // Private method to ensure that produce/release logic is symmetric between |
| 115 // producer and consumer since they represent the same action from a state |
| 116 // tracking perspective. |
| 117 void ImagePipeEndpoint::ReleaseInternal(uint32_t id, |
| 118 bool released_by_producer) { |
| 119 // Here we define the concept of a releaser and a releasee, where the releaser |
| 120 // is the entity that owns |id| before the function, and the releasee is the |
| 121 // entity on the other end of the ImagePipe. |
| 122 auto& releaser_owned_ids = |
| 123 released_by_producer ? producer_owned_ids_ : consumer_owned_ids_; |
| 124 auto& releasee_owned_ids = |
| 125 !released_by_producer ? producer_owned_ids_ : consumer_owned_ids_; |
| 126 |
| 127 auto& releasee_acquirable_ids = !released_by_producer |
| 128 ? producer_acquirable_ids_ |
| 129 : consumer_acquirable_ids_; |
| 130 |
| 131 auto IsReleaserOwned = released_by_producer |
| 132 ? &ImagePipeEndpoint::IsProducerOwned |
| 133 : &ImagePipeEndpoint::IsConsumerOwned; |
| 134 |
| 135 // Now that we have the releaser and the releasee containers, we do all of the |
| 136 // ownership change semantics generically based on those containers. |
| 137 MOJO_DCHECK(!is_checked_ || (this->*IsReleaserOwned)(id)); |
| 138 auto releaser_owned_ids_iter = |
| 139 std::find(releaser_owned_ids.begin(), releaser_owned_ids.end(), id); |
| 140 releaser_owned_ids.erase(releaser_owned_ids_iter); |
| 141 MOJO_DCHECK(!is_checked_ || !(this->*IsReleaserOwned)(id)); |
| 142 |
| 143 // If the release action is coming from our side of the pipe, we wont see the |
| 144 // acquire events, so we just pretend the other side immediately aquires |
| 145 // everything to simplify state tracking. |
| 146 if (released_by_producer == is_producer_) { |
| 147 releasee_owned_ids.insert(id); |
| 148 } else { |
| 149 releasee_acquirable_ids.push_back(id); |
| 150 } |
| 151 } |
| 152 |
| 153 void ImagePipeEndpoint::ProducerPresent( |
| 154 uint32_t id, |
| 155 mojo::gfx::ImagePipe::PresentImageCallback callback) { |
| 156 if (IsProducerOwned(id)) { |
| 157 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsConsumerOwned(id) && |
| 158 !IsConsumerAcquirable(id))); |
| 159 ReleaseInternal(id, true); |
| 160 present_callback_map_[id] = callback; |
| 161 } else if (!IsInPool(id)) { |
| 162 ProducerFatalError( |
| 163 "Attempting to present an image that is not in the image pool", id); |
| 164 } else if (IsProducerAcquirable(id)) { |
| 165 ProducerFatalError( |
| 166 "Attempting to present an image that has not been acquired", id); |
| 167 } else if (IsConsumerOwned(id) || IsConsumerAcquirable(id)) { |
| 168 ProducerFatalError( |
| 169 "Attempting to present an image that has already been presented", id); |
| 170 } |
| 171 } |
| 172 |
| 173 void ImagePipeEndpoint::ConsumerRelease(uint32_t id, |
| 174 mojo::gfx::PresentationStatus status) { |
| 175 if (IsConsumerOwned(id)) { |
| 176 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsProducerOwned(id) && |
| 177 !IsProducerAcquirable(id))); |
| 178 ReleaseInternal(id, false); |
| 179 CallPresentCallback(id, status); |
| 180 } else if (!IsInPool(id)) { |
| 181 ConsumerFatalError( |
| 182 "Attempting to release an image that is not in the image pool", id); |
| 183 } else if (IsConsumerAcquirable(id)) { |
| 184 ConsumerFatalError( |
| 185 "Attempting to release an image that has not been acquired", id); |
| 186 } else if (IsProducerOwned(id) || IsProducerAcquirable(id)) { |
| 187 ConsumerFatalError( |
| 188 "Attempting to release an image that has not been presented", id); |
| 189 } |
| 190 } |
| 191 |
| 192 void ImagePipeEndpoint::ProducerFlush() { |
| 193 if (!is_producer_) { |
| 194 for (auto id : consumer_acquirable_ids_) { |
| 195 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsConsumerOwned(id) && |
| 196 !IsProducerOwned(id))); |
| 197 CallPresentCallback(id, |
| 198 mojo::gfx::PresentationStatus::NOT_PRESENTED_FLUSHED); |
| 199 } |
| 200 consumer_acquirable_ids_.clear(); |
| 201 } |
| 202 } |
| 203 |
| 204 void ImagePipeEndpoint::CallPresentCallback( |
| 205 uint32_t id, |
| 206 mojo::gfx::PresentationStatus status) { |
| 207 auto present_callback_iter = present_callback_map_.find(id); |
| 208 MOJO_DCHECK(present_callback_iter != present_callback_map_.end()); |
| 209 auto present_callback = present_callback_iter->second; |
| 210 present_callback_map_.erase(present_callback_iter); |
| 211 present_callback.Run(status); |
| 212 } |
| 213 |
| 214 bool ImagePipeEndpoint::IsInPool(uint32_t id) const { |
| 215 auto& container = image_pool_ids_; |
| 216 return std::find(container.begin(), container.end(), id) != container.end(); |
| 217 } |
| 218 |
| 219 bool ImagePipeEndpoint::IsConsumerOwned(uint32_t id) const { |
| 220 auto& container = consumer_owned_ids_; |
| 221 return std::find(container.begin(), container.end(), id) != container.end(); |
| 222 } |
| 223 |
| 224 bool ImagePipeEndpoint::IsConsumerAcquirable(uint32_t id) const { |
| 225 auto& container = consumer_acquirable_ids_; |
| 226 return std::find(container.begin(), container.end(), id) != container.end(); |
| 227 } |
| 228 |
| 229 bool ImagePipeEndpoint::IsProducerOwned(uint32_t id) const { |
| 230 auto& container = producer_owned_ids_; |
| 231 return std::find(container.begin(), container.end(), id) != container.end(); |
| 232 } |
| 233 |
| 234 bool ImagePipeEndpoint::IsProducerAcquirable(uint32_t id) const { |
| 235 auto& container = producer_acquirable_ids_; |
| 236 return std::find(container.begin(), container.end(), id) != container.end(); |
| 237 } |
| 238 |
| 239 void ImagePipeEndpoint::DisableFatalErrorsForTesting() { |
| 240 is_checked_ = false; |
| 241 } |
| 242 |
| 243 } // namespace image_pipe |
OLD | NEW |