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

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

Issue 1595773002: Added ImagePipe (Closed) Base URL: https://github.com/domokit/mojo.git@submit-2
Patch Set: Expose InterfacePtr::encountered_error() through ImagePipeProducerEndpoint so the GL on ImagePipe i… 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 "mojo/public/cpp/application/application_impl.h"
6 #include "mojo/public/cpp/application/application_test_base.h"
7 #include "mojo/public/cpp/environment/logging.h"
8 #include "mojo/public/cpp/system/macros.h"
9 #include "mojo/public/cpp/utility/run_loop.h"
10 #include "mojo/services/gfx/images/cpp/image_pipe_consumer_endpoint.h"
11 #include "mojo/services/gfx/images/cpp/image_pipe_producer_endpoint.h"
12
13 namespace mojo {
14 namespace {
15
16 using namespace image_pipe;
jamesr 2016/02/18 20:40:58 this is banned by the style guide: http://google.
Forrest Reiling 2016/02/25 00:35:14 Done.
17
18 class ImagePipeApplicationTest : public test::ApplicationTestBase {
jamesr 2016/02/18 20:40:58 what's this class for? it doesn't seem to do anyth
Forrest Reiling 2016/02/25 00:35:13 Its used to do some kind of wicked crazy test macr
19 public:
20 ImagePipeApplicationTest() : ApplicationTestBase() {}
21 ~ImagePipeApplicationTest() override {}
22
23 private:
24 MOJO_DISALLOW_COPY_AND_ASSIGN(ImagePipeApplicationTest);
25 };
26
27 class TestImagePipe : private ImagePipeConsumerDelegate {
jamesr 2016/02/18 20:40:58 private inheritance is banned by the style guide (
Forrest Reiling 2016/02/25 00:35:14 Acknowledged.
28 public:
29 TestImagePipe(bool producer_checked, bool consumer_checked);
30 ~TestImagePipe() override;
31 mojo::gfx::ImagePtr CreateTestImage();
32
33 ImagePipeProducerEndpoint* producer_endpoint;
jamesr 2016/02/18 20:40:58 if it's a class members are private
Forrest Reiling 2016/02/25 00:35:14 Acknowledged.
34 ImagePipeConsumerEndpoint* consumer_endpoint;
35 mojo::gfx::SupportedImagePropertiesPtr supported_properties_;
jamesr 2016/02/18 20:40:58 this is a public member with private naming, doubl
Forrest Reiling 2016/02/25 00:35:14 Acknowledged.
36
37 bool HasExperiencedError() { return has_error_; }
38
39 std::function<void(mojo::gfx::ImagePtr image, uint32_t id)>
jamesr 2016/02/18 20:40:58 no public members
Forrest Reiling 2016/02/25 00:35:14 Acknowledged.
40 add_image_override;
41 std::function<void(uint32_t id)> remove_image_override;
42 std::function<void(uint32_t id)> present_image_override;
43
44 private:
45 // Inherited from ImagePipeConsumerDelegate //
jamesr 2016/02/18 20:40:58 odd trailing //
Forrest Reiling 2016/02/25 00:35:14 Done.
46 void AddImage(mojo::gfx::ImagePtr image, uint32_t id) override;
47 void RemoveImage(uint32_t id) override;
48 void PresentImage(uint32_t id) override;
49 void HandleEndpointClosed() override {
50 has_error_ = true;
51 mojo::RunLoop::current()->Quit();
52 }
53 bool has_error_;
54 };
55
56 void TestImagePipe::AddImage(mojo::gfx::ImagePtr image, uint32_t id) {
57 add_image_override(image.Pass(), id);
58 }
59 void TestImagePipe::RemoveImage(uint32_t id) {
60 remove_image_override(id);
61 }
62 void TestImagePipe::PresentImage(uint32_t id) {
63 present_image_override(id);
64 }
65
66 TestImagePipe::TestImagePipe(bool producer_checked, bool consumer_checked) {
67 present_image_override = [](uint32_t id) {};
jamesr 2016/02/18 20:40:58 seems silly - wouldn't it be cleaner to leave it d
Forrest Reiling 2016/02/25 00:35:14 I mean in general I prefer initializing the functi
68 remove_image_override = [](uint32_t id) {};
69 add_image_override = [](mojo::gfx::ImagePtr image, uint32_t id) {};
70
71 has_error_ = false;
72 supported_properties_ = mojo::gfx::SupportedImageProperties::New();
73 supported_properties_->size = mojo::Size::New();
74 supported_properties_->size->width = 256;
75 supported_properties_->size->height = 256;
76
77 supported_properties_->formats =
78 mojo::Array<mojo::gfx::ColorFormatPtr>::New(0);
79 mojo::gfx::ColorFormatPtr format = mojo::gfx::ColorFormat::New();
80 format->layout = mojo::gfx::PixelLayout::BGRA_8888;
81 format->color_space = mojo::gfx::ColorSpace::SRGB;
82 supported_properties_->formats.push_back(format.Pass());
83
84 mojo::gfx::ImagePipePtr image_pipe_ptr;
85 consumer_endpoint = new ImagePipeConsumerEndpoint(
86 GetProxy(&image_pipe_ptr), supported_properties_.Clone(), this);
87 if (!consumer_checked) {
88 consumer_endpoint->DisableFatalErrors();
89 }
90
91 producer_endpoint =
92 new ImagePipeProducerEndpoint(image_pipe_ptr.Pass(), [this] {
93 has_error_ = true;
94 mojo::RunLoop::current()->Quit();
95 });
96 if (!producer_checked) {
97 producer_endpoint->DisableFatalErrors();
98 }
99 }
100
101 TestImagePipe::~TestImagePipe() {
102 delete producer_endpoint;
jamesr 2016/02/18 20:40:58 std::unique_ptr<> instead?
Forrest Reiling 2016/02/25 00:35:13 Done.
103 delete consumer_endpoint;
104 }
105
106 mojo::gfx::ImagePtr TestImagePipe::CreateTestImage() {
107 mojo::MessagePipe pipe;
108
109 uint32_t bytes_per_pixel = 32;
110
111 mojo::gfx::ImageBufferPtr image_buffer = mojo::gfx::ImageBuffer::New();
112 image_buffer->size = supported_properties_->size->width *
113 supported_properties_->size->width * bytes_per_pixel;
114 image_buffer->data = mojo::ScopedHandle(pipe.handle0.Pass());
115
116 mojo::gfx::ImagePtr image = mojo::gfx::Image::New();
117 image->buffer = image_buffer.Pass();
118 image->format = supported_properties_->formats[0].Clone();
119 image->size = supported_properties_->size.Clone();
120 image->pitch = supported_properties_->size->width;
121 image->stride = image->pitch * bytes_per_pixel;
122
123 return image;
124 }
125
126 // Tests that the usual flow for creating, adding, presenting, and removing
127 // an image doesnt crash/break/cause-errors etc
128 TEST_F(ImagePipeApplicationTest, NormalImageLifeCycle) {
129 TestImagePipe image_pipe(true, true);
130
131 image_pipe.present_image_override = [&image_pipe](uint32_t id) {
132 uint32_t acquired_id;
133 MOJO_CHECK(image_pipe.consumer_endpoint->AcquireNextImage(acquired_id));
134 MOJO_CHECK(acquired_id == id);
135 image_pipe.consumer_endpoint->ReleaseImage(
136 id, mojo::gfx::PresentationStatus::PRESENTED);
137 };
138
139 uint32_t id = 0, acquired_id = UINT32_MAX;
140 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
141 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
142 MOJO_CHECK(acquired_id == id);
143 image_pipe.producer_endpoint->PresentImage(
144 id, [&image_pipe](unsigned int id, mojo::gfx::PresentationStatus status) {
145 EXPECT_EQ(mojo::gfx::PresentationStatus::PRESENTED, status);
146 mojo::RunLoop::current()->Quit();
147 });
148
149 mojo::RunLoop::current()->Run();
150 acquired_id = UINT32_MAX;
151 EXPECT_TRUE(image_pipe.producer_endpoint->AcquireImage(acquired_id));
152 MOJO_CHECK(acquired_id == id);
153 image_pipe.producer_endpoint->RemoveImage(acquired_id);
154
155 EXPECT_FALSE(image_pipe.HasExperiencedError());
156 }
157
158 // Tests that flushing returns images to the producer with NOT_PRESENTED_FLUSHED
159 TEST_F(ImagePipeApplicationTest, FlushImages) {
160 TestImagePipe image_pipe(true, true);
161
162 uint32_t id = 0, acquired_id = UINT32_MAX;
163 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
164 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
165 MOJO_CHECK(acquired_id == id);
166 image_pipe.producer_endpoint->PresentImage(
167 id,
168 [&id](unsigned int presented_id, mojo::gfx::PresentationStatus status) {
169 EXPECT_EQ(mojo::gfx::PresentationStatus::NOT_PRESENTED_FLUSHED, status);
170 EXPECT_TRUE(presented_id == id);
171 mojo::RunLoop::current()->Quit();
172 });
173 image_pipe.producer_endpoint->FlushImages();
174
175 mojo::RunLoop::current()->Run();
176 acquired_id = UINT32_MAX;
177 EXPECT_TRUE(image_pipe.producer_endpoint->AcquireImage(acquired_id));
178 EXPECT_TRUE(acquired_id == id);
179
180 EXPECT_FALSE(image_pipe.HasExperiencedError());
181 }
182
183 // Tests that you can safely try to acquire an image when none are available,
184 // and that you will safely fail
185 TEST_F(ImagePipeApplicationTest, AcquireImageFromEmptyPool) {
186 TestImagePipe image_pipe(true, true);
187
188 uint32_t id = 0xDEADBEEF, acquired_id = id;
189 EXPECT_FALSE(image_pipe.producer_endpoint->AcquireImage(acquired_id));
190 EXPECT_TRUE(acquired_id == id);
191 EXPECT_FALSE(image_pipe.HasExperiencedError());
192 }
193
194 // Tests that adding an image with an existing ID causes the pipe to error
195 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageWithReusedID) {
196 TestImagePipe image_pipe(false, true);
197
198 uint32_t id = 0;
199 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
200 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
201
202 mojo::RunLoop::current()->Run();
203 EXPECT_TRUE(image_pipe.HasExperiencedError());
204 }
205
206 // Tests that removing an image that hasnt been added causes the pipe to error
207 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageBeforeAdded) {
208 TestImagePipe image_pipe(false, true);
209
210 uint32_t id = 0;
211 image_pipe.producer_endpoint->RemoveImage(id);
212
213 mojo::RunLoop::current()->Run();
214 EXPECT_TRUE(image_pipe.HasExperiencedError());
215 }
216
217 // Tests that removing an image that has already been removed causes the pipe to
218 // error (essentially that removing and image takes it out of the pool)
219 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageThenRemoveTwice) {
220 TestImagePipe image_pipe(false, true);
221
222 uint32_t id = 0;
223 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
224 image_pipe.producer_endpoint->RemoveImage(id);
225 image_pipe.producer_endpoint->RemoveImage(id);
226
227 mojo::RunLoop::current()->Run();
228 EXPECT_TRUE(image_pipe.HasExperiencedError());
229 }
230
231 // Tests that removing an image owned by the consumer causes the pipe to error
232 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageOwnedByConsumer) {
233 TestImagePipe image_pipe(false, true);
234
235 uint32_t id = 0, acquired_id = UINT32_MAX;
236 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
237 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
238 MOJO_CHECK(acquired_id == id);
239 image_pipe.producer_endpoint->PresentImage(
240 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
241 image_pipe.producer_endpoint->RemoveImage(id);
242
243 mojo::RunLoop::current()->Run();
244 EXPECT_TRUE(image_pipe.HasExperiencedError());
245 }
246
247 // Tests that presenting an image that hasnt been added causes the pipe to error
248 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageNotAdded) {
249 TestImagePipe image_pipe(false, true);
250
251 uint32_t id = 0;
252 image_pipe.producer_endpoint->PresentImage(
253 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
254
255 mojo::RunLoop::current()->Run();
256 EXPECT_TRUE(image_pipe.HasExperiencedError());
257 }
258
259 // Tests that presenting an image that has already been presented causes the
260 // pipe to error
261 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageTwice) {
262 TestImagePipe image_pipe(false, true);
263
264 uint32_t id = 0, acquired_id = UINT32_MAX;
265 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
266 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
267 MOJO_CHECK(acquired_id == id);
268 image_pipe.producer_endpoint->PresentImage(
269 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
270 image_pipe.producer_endpoint->PresentImage(
271 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
272
273 mojo::RunLoop::current()->Run();
274 EXPECT_TRUE(image_pipe.HasExperiencedError());
275 }
276
277 // Tests that releasing an image that hasnt been added causes the pipe to error
278 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotInPool) {
279 TestImagePipe image_pipe(true, false);
280
281 image_pipe.present_image_override = [&image_pipe](uint32_t id) {
282 image_pipe.consumer_endpoint->ReleaseImage(
283 id + 1, mojo::gfx::PresentationStatus::PRESENTED);
284 };
285
286 uint32_t id = 0, acquired_id = UINT32_MAX;
287 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
288 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
289 MOJO_CHECK(acquired_id == id);
290 image_pipe.producer_endpoint->PresentImage(
291 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
292
293 mojo::RunLoop::current()->Run();
294 EXPECT_TRUE(image_pipe.HasExperiencedError());
295 }
296
297 // Tests that releasing an image before acquiring it causes the pipe to error
298 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseBeforeAcquire) {
299 TestImagePipe image_pipe(true, false);
300
301 image_pipe.present_image_override = [&image_pipe](uint32_t id) {
302 image_pipe.consumer_endpoint->ReleaseImage(
303 id, mojo::gfx::PresentationStatus::PRESENTED);
304 };
305
306 uint32_t id = 0, acquired_id = UINT32_MAX;
307 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
308 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
309 MOJO_CHECK(acquired_id == id);
310 image_pipe.producer_endpoint->PresentImage(
311 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
312
313 mojo::RunLoop::current()->Run();
314 EXPECT_TRUE(image_pipe.HasExperiencedError());
315 }
316
317 // Tests that releasing an image before its presented causes the pipe to error
318 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotPresented) {
319 TestImagePipe image_pipe(true, false);
320
321 image_pipe.present_image_override = [&image_pipe](uint32_t id) {
322 uint32_t acquired_id;
323 MOJO_CHECK(image_pipe.consumer_endpoint->AcquireNextImage(acquired_id));
324 MOJO_CHECK(acquired_id == id);
325 image_pipe.consumer_endpoint->ReleaseImage(
326 id + 1, mojo::gfx::PresentationStatus::PRESENTED);
327 };
328
329 uint32_t id = 0, acquired_id = UINT32_MAX;
330 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id);
331 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id + 1);
332 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id));
333 MOJO_CHECK(acquired_id == id);
334 image_pipe.producer_endpoint->PresentImage(
335 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
336
337 mojo::RunLoop::current()->Run();
338 EXPECT_TRUE(image_pipe.HasExperiencedError());
339 }
340
341 } // namespace image_pipe
342 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698