Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: mojo/services/gfx/images/cpp/image_pipe_endpoint.cc

Issue 1595773002: Added ImagePipe (Closed) Base URL: https://github.com/domokit/mojo.git@submit-2
Patch Set: minor changes to make it build with GCC on fnl Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "image_pipe_endpoint.h"
6
7 namespace image_pipe {
8
9 void ImagePipeEndpoint::ImagePipeLogError(const char* entity,
10 const char* 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 char* message, uint32_t id) {
17 if (is_checked_) {
18 ImagePipeLogError("Producer", message, id);
19 if (is_producer_) {
20 MOJO_CHECK(false);
21 } else {
22 CloseEndpoint();
23 }
24 }
25 }
26
27 void ImagePipeEndpoint::ConsumerFatalError(const char* message, uint32_t id) {
28 ImagePipeLogError("Consumer", message, id);
29 if (is_producer_) {
30 CloseEndpoint();
31 } else {
32 if (is_checked_) {
33 MOJO_CHECK(false);
34 } else {
35 CloseEndpoint();
36 }
37 }
38 }
39
40 ImagePipeEndpoint::ImagePipeEndpoint(bool is_producer, bool is_checked)
41 : is_producer_(is_producer), is_checked_(is_checked) {}
42
43 ImagePipeEndpoint::~ImagePipeEndpoint() {}
44
45 void ImagePipeEndpoint::ProducerAdd(uint32_t id) {
46 if (IsInPool(id)) {
47 ProducerFatalError("Attempting to add an image that is already in the pool",
48 id);
49 } else {
50 image_pool_ids_.push_back(id);
51 if (is_producer_) {
52 producer_acquirable_ids_.push_back(id);
53 } else {
54 producer_owned_ids_.push_back(id);
55 }
56 }
57 }
58
59 bool ImagePipeEndpoint::AcquireNextImage(uint32_t& id_out) {
60 auto acquirable_ids_ =
61 is_producer_ ? &producer_acquirable_ids_ : &consumer_acquirable_ids_;
62 auto owned_ids_ = is_producer_ ? &producer_owned_ids_ : &consumer_owned_ids_;
63
64 if (acquirable_ids_->empty()) {
65 return false;
66 }
67
68 int id = acquirable_ids_->front();
69 acquirable_ids_->pop_front();
70 owned_ids_->push_back(id);
71 id_out = id;
72 return true;
73 }
74
75 void ImagePipeEndpoint::ProducerRemove(uint32_t id) {
76 if (IsInPool(id)) {
77 if ((IsConsumerOwned(id) || IsConsumerAcquirable(id))) {
78 ProducerFatalError(
79 "Attempting to remove an image that has been presented "
80 "but has not been released by the consumer",
81 id);
82 } else {
83 auto image_pool_ids_iter =
84 std::find(image_pool_ids_.begin(), image_pool_ids_.end(), id);
85 image_pool_ids_.erase(image_pool_ids_iter);
86 MOJO_DCHECK(!is_checked_ || IsProducerOwned(id) ||
87 IsProducerAcquirable(id));
88 if (IsProducerOwned(id)) {
89 MOJO_DCHECK(!is_checked_ || !IsProducerAcquirable(id));
90 auto producer_owned_ids_iter = std::find(producer_owned_ids_.begin(),
91 producer_owned_ids_.end(), id);
92 producer_owned_ids_.erase(producer_owned_ids_iter);
93 MOJO_DCHECK(!is_checked_ || !IsProducerOwned(id));
94 } else {
95 MOJO_DCHECK(!is_checked_ || IsProducerAcquirable(id));
96 auto producer_acquirable_ids_iter =
97 std::find(producer_acquirable_ids_.begin(),
98 producer_acquirable_ids_.end(), id);
99 producer_acquirable_ids_.erase(producer_acquirable_ids_iter);
100 MOJO_DCHECK(!is_checked_ || !IsProducerAcquirable(id));
101 }
102 }
103 } else {
104 ProducerFatalError(
105 "Attempting to remove an image that is not in the image pool", id);
106 }
107 }
108
109 // private method to ensure that produce/release logic is symmetric between
110 // producer and consumer since they represent the same action from a state
111 // tracking perspective
112 void ImagePipeEndpoint::ReleaseInternal(uint32_t id,
113 bool released_by_producer) {
114 auto releaser_owned_ids =
115 released_by_producer ? &producer_owned_ids_ : &consumer_owned_ids_;
116 auto releasee_owned_ids =
117 !released_by_producer ? &producer_owned_ids_ : &consumer_owned_ids_;
118
119 auto releasee_acquirable_ids = !released_by_producer
120 ? &producer_acquirable_ids_
121 : &consumer_acquirable_ids_;
122
123 auto IsReleaserOwned = released_by_producer
124 ? &ImagePipeEndpoint::IsProducerOwned
125 : &ImagePipeEndpoint::IsConsumerOwned;
126
127 MOJO_DCHECK(!is_checked_ || (this->*IsReleaserOwned)(id));
128 auto releaser_owned_ids_iter =
129 std::find(releaser_owned_ids->begin(), releaser_owned_ids->end(), id);
130 releaser_owned_ids->erase(releaser_owned_ids_iter);
131 MOJO_DCHECK(!is_checked_ || !(this->*IsReleaserOwned)(id));
132
133 // if the release action is coming from our side of the pipe, we wont see the
134 // acquire events, so we just pretend the other side immediately aquires
135 // everything to simplify state tracking
136 if (released_by_producer == is_producer_) {
137 releasee_owned_ids->push_back(id);
138 } else {
139 releasee_acquirable_ids->push_back(id);
140 }
141 }
142
143 void ImagePipeEndpoint::ProducerPresent(
144 uint32_t id,
145 mojo::gfx::ImagePipe::PresentImageCallback callback) {
146 if (IsProducerOwned(id)) {
147 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsConsumerOwned(id) &&
148 !IsConsumerAcquirable(id)));
149 ReleaseInternal(id, true);
150 present_callback_map_[id] = callback;
151 } else if (!IsInPool(id)) {
152 ProducerFatalError(
153 "Attempting to present an image that is not in the image pool", id);
154 } else if (IsProducerAcquirable(id)) {
155 ProducerFatalError(
156 "Attempting to present an image that has not been acquired", id);
157 } else if (IsConsumerOwned(id) || IsConsumerAcquirable(id)) {
158 ProducerFatalError(
159 "Attempting to present an image that has already been presented", id);
160 }
161 }
162
163 void ImagePipeEndpoint::ConsumerRelease(uint32_t id,
164 mojo::gfx::PresentationStatus status) {
165 if (IsConsumerOwned(id)) {
166 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsProducerOwned(id) &&
167 !IsProducerAcquirable(id)));
168 ReleaseInternal(id, false);
169 CallPresentCallback(id, status);
170 } else if (!IsInPool(id)) {
171 ConsumerFatalError(
172 "Attempting to release an image that is not in the image pool", id);
173 } else if (IsConsumerAcquirable(id)) {
174 ConsumerFatalError(
175 "Attempting to release an image that has not been acquired", id);
176 } else if (IsProducerOwned(id) || IsProducerAcquirable(id)) {
177 ConsumerFatalError(
178 "Attempting to release an image that has not been presented", id);
179 }
180 }
181
182 void ImagePipeEndpoint::ProducerFlush() {
183 if (!is_producer_) {
184 for (auto id : consumer_acquirable_ids_) {
185 MOJO_DCHECK(!is_checked_ || (IsInPool(id) && !IsConsumerOwned(id) &&
186 !IsProducerOwned(id)));
187 CallPresentCallback(id,
188 mojo::gfx::PresentationStatus::NOT_PRESENTED_FLUSHED);
189 }
190 consumer_acquirable_ids_.clear();
191 }
192 }
193
194 void ImagePipeEndpoint::CallPresentCallback(
195 uint32_t id,
196 mojo::gfx::PresentationStatus status) {
197 auto present_callback_iter = present_callback_map_.find(id);
198 MOJO_DCHECK(present_callback_iter != present_callback_map_.end());
199 auto present_callback = present_callback_iter->second;
200 present_callback_map_.erase(present_callback_iter);
201 present_callback.Run(id, status);
202 }
203
204 bool ImagePipeEndpoint::IsInPool(uint32_t id) const {
205 auto container = image_pool_ids_;
206 return std::find(container.begin(), container.end(), id) != container.end();
207 }
208
209 bool ImagePipeEndpoint::IsConsumerOwned(uint32_t id) const {
210 auto container = consumer_owned_ids_;
211 return std::find(container.begin(), container.end(), id) != container.end();
212 }
213
214 bool ImagePipeEndpoint::IsConsumerAcquirable(uint32_t id) const {
215 auto container = consumer_acquirable_ids_;
216 return std::find(container.begin(), container.end(), id) != container.end();
217 }
218
219 bool ImagePipeEndpoint::IsProducerOwned(uint32_t id) const {
220 auto container = producer_owned_ids_;
221 return std::find(container.begin(), container.end(), id) != container.end();
222 }
223
224 bool ImagePipeEndpoint::IsProducerAcquirable(uint32_t id) const {
225 auto container = producer_acquirable_ids_;
226 return std::find(container.begin(), container.end(), id) != container.end();
227 }
228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698