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

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: rebased. the ImagePipe interface doesnt pass interfaces anyway so it didnt really change anything Created 4 years, 9 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 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 "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 class ImagePipeApplicationTest : public test::ApplicationTestBase {
17 public:
18 ImagePipeApplicationTest() : ApplicationTestBase() {}
19 ~ImagePipeApplicationTest() override {}
20
21 private:
22 MOJO_DISALLOW_COPY_AND_ASSIGN(ImagePipeApplicationTest);
23 };
24
25 class TestImagePipe : image_pipe::ImagePipeConsumerDelegate {
26 public:
27 TestImagePipe(bool producer_checked, bool consumer_checked);
28 ~TestImagePipe() override;
29 mojo::gfx::ImagePtr CreateTestImage();
30
31 bool HasExperiencedError() { return has_error_; }
32
33 image_pipe::ImagePipeProducerEndpoint* ProducerEndpoint() {
34 return producer_endpoint_.get();
35 }
36 image_pipe::ImagePipeConsumerEndpoint* ConsumerEndpoint() {
37 return consumer_endpoint_.get();
38 }
39
40 void OverrideAddImage(
41 std::function<void(mojo::gfx::ImagePtr image, uint32_t id)> add_func) {
42 add_image_override_ = add_func;
43 }
44 void OverrideRemoveImage(std::function<void(uint32_t id)> remove_func) {
45 remove_image_override_ = remove_func;
46 }
47 void OverridePresentImage(std::function<void(uint32_t id)> present_func) {
48 present_image_override_ = present_func;
49 }
50
51 private:
52 // Inherited from ImagePipeConsumerDelegate
53 void AddImage(mojo::gfx::ImagePtr image, uint32_t id) override;
54 void RemoveImage(uint32_t id) override;
55 void PresentImage(uint32_t id) override;
56 void HandleEndpointClosed() override {
57 has_error_ = true;
58 mojo::RunLoop::current()->Quit();
59 }
60
61 std::unique_ptr<image_pipe::ImagePipeProducerEndpoint> producer_endpoint_;
62 std::unique_ptr<image_pipe::ImagePipeConsumerEndpoint> consumer_endpoint_;
63 mojo::gfx::SupportedImagePropertiesPtr supported_properties_;
64
65 std::function<void(mojo::gfx::ImagePtr image, uint32_t id)>
66 add_image_override_;
67 std::function<void(uint32_t id)> remove_image_override_;
68 std::function<void(uint32_t id)> present_image_override_;
69
70 bool has_error_;
71 };
72
73 void TestImagePipe::AddImage(mojo::gfx::ImagePtr image, uint32_t id) {
74 if (add_image_override_)
75 add_image_override_(image.Pass(), id);
76 }
77 void TestImagePipe::RemoveImage(uint32_t id) {
78 if (remove_image_override_)
79 remove_image_override_(id);
80 }
81 void TestImagePipe::PresentImage(uint32_t id) {
82 if (present_image_override_)
83 present_image_override_(id);
84 }
85
86 TestImagePipe::TestImagePipe(bool producer_checked, bool consumer_checked) {
87 has_error_ = false;
88 supported_properties_ = mojo::gfx::SupportedImageProperties::New();
89 supported_properties_->size = mojo::Size::New();
90 supported_properties_->size->width = 256;
91 supported_properties_->size->height = 256;
92
93 supported_properties_->formats =
94 mojo::Array<mojo::gfx::ColorFormatPtr>::New(0);
95 mojo::gfx::ColorFormatPtr format = mojo::gfx::ColorFormat::New();
96 format->layout = mojo::gfx::PixelLayout::BGRA_8888;
97 format->color_space = mojo::gfx::ColorSpace::SRGB;
98 supported_properties_->formats.push_back(format.Pass());
99
100 mojo::gfx::ImagePipePtr image_pipe_ptr;
101 consumer_endpoint_.reset(new image_pipe::ImagePipeConsumerEndpoint(
102 GetProxy(&image_pipe_ptr), supported_properties_.Clone(), this));
103 if (!consumer_checked) {
104 consumer_endpoint_->DisableFatalErrorsForTesting();
105 }
106
107 producer_endpoint_.reset(
108 new image_pipe::ImagePipeProducerEndpoint(image_pipe_ptr.Pass(), [this] {
109 has_error_ = true;
110 mojo::RunLoop::current()->Quit();
111 }));
112 if (!producer_checked) {
113 producer_endpoint_->DisableFatalErrorsForTesting();
114 }
115 }
116
117 TestImagePipe::~TestImagePipe() {}
118
119 mojo::gfx::ImagePtr TestImagePipe::CreateTestImage() {
120 mojo::MessagePipe pipe;
121
122 uint32_t bytes_per_pixel = 32;
123
124 mojo::gfx::ImageBufferPtr image_buffer = mojo::gfx::ImageBuffer::New();
125 image_buffer->size = supported_properties_->size->width *
126 supported_properties_->size->width * bytes_per_pixel;
127 image_buffer->data = mojo::ScopedHandle(pipe.handle0.Pass());
128
129 mojo::gfx::ImagePtr image = mojo::gfx::Image::New();
130 image->buffer = image_buffer.Pass();
131 image->format = supported_properties_->formats[0].Clone();
132 image->size = supported_properties_->size.Clone();
133 image->pitch = supported_properties_->size->width;
134 image->stride = image->pitch * bytes_per_pixel;
135
136 return image;
137 }
138
139 // Tests that the usual flow for creating, adding, presenting, and removing
140 // an image doesnt crash/break/cause-errors etc
141 TEST_F(ImagePipeApplicationTest, NormalImageLifeCycle) {
142 TestImagePipe image_pipe(true, true);
143
144 image_pipe.OverridePresentImage([&image_pipe](uint32_t id) {
145 uint32_t acquired_id;
146 MOJO_CHECK(image_pipe.ConsumerEndpoint()->AcquireNextImage(&acquired_id));
147 MOJO_CHECK(acquired_id == id);
148 image_pipe.ConsumerEndpoint()->ReleaseImage(
149 id, mojo::gfx::PresentationStatus::PRESENTED);
150 });
151
152 uint32_t id = 0, acquired_id = UINT32_MAX;
153 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
154 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
155 MOJO_CHECK(acquired_id == id);
156 image_pipe.ProducerEndpoint()->PresentImage(
157 id, [&image_pipe](unsigned int id, mojo::gfx::PresentationStatus status) {
158 EXPECT_EQ(mojo::gfx::PresentationStatus::PRESENTED, status);
159 mojo::RunLoop::current()->Quit();
160 });
161
162 mojo::RunLoop::current()->Run();
163 acquired_id = UINT32_MAX;
164 EXPECT_TRUE(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
165 MOJO_CHECK(acquired_id == id);
166 image_pipe.ProducerEndpoint()->RemoveImage(acquired_id);
167
168 EXPECT_FALSE(image_pipe.HasExperiencedError());
169 }
170
171 // Tests that flushing returns images to the producer with NOT_PRESENTED_FLUSHED
172 TEST_F(ImagePipeApplicationTest, FlushImages) {
173 TestImagePipe image_pipe(true, true);
174
175 uint32_t id = 0, acquired_id = UINT32_MAX;
176 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
177 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
178 MOJO_CHECK(acquired_id == id);
179 image_pipe.ProducerEndpoint()->PresentImage(
180 id,
181 [&id](unsigned int presented_id, mojo::gfx::PresentationStatus status) {
182 EXPECT_EQ(mojo::gfx::PresentationStatus::NOT_PRESENTED_FLUSHED, status);
183 EXPECT_TRUE(presented_id == id);
184 mojo::RunLoop::current()->Quit();
185 });
186 image_pipe.ProducerEndpoint()->FlushImages();
187
188 mojo::RunLoop::current()->Run();
189 acquired_id = UINT32_MAX;
190 EXPECT_TRUE(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
191 EXPECT_TRUE(acquired_id == id);
192
193 EXPECT_FALSE(image_pipe.HasExperiencedError());
194 }
195
196 // Tests that you can safely try to acquire an image when none are available,
197 // and that you will safely fail
198 TEST_F(ImagePipeApplicationTest, AcquireImageFromEmptyPool) {
199 TestImagePipe image_pipe(true, true);
200
201 uint32_t id = 0xDEADBEEF, acquired_id = id;
202 EXPECT_FALSE(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
203 EXPECT_TRUE(acquired_id == id);
204 EXPECT_FALSE(image_pipe.HasExperiencedError());
205 }
206
207 // Tests that adding an image with an existing ID causes the pipe to error
208 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageWithReusedID) {
209 TestImagePipe image_pipe(false, true);
210
211 uint32_t id = 0;
212 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
213 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
214
215 mojo::RunLoop::current()->Run();
216 EXPECT_TRUE(image_pipe.HasExperiencedError());
217 }
218
219 // Tests that removing an image that hasnt been added causes the pipe to error
220 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageBeforeAdded) {
221 TestImagePipe image_pipe(false, true);
222
223 uint32_t id = 0;
224 image_pipe.ProducerEndpoint()->RemoveImage(id);
225
226 mojo::RunLoop::current()->Run();
227 EXPECT_TRUE(image_pipe.HasExperiencedError());
228 }
229
230 // Tests that removing an image that has already been removed causes the pipe to
231 // error (essentially that removing and image takes it out of the pool)
232 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageThenRemoveTwice) {
233 TestImagePipe image_pipe(false, true);
234
235 uint32_t id = 0;
236 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
237 image_pipe.ProducerEndpoint()->RemoveImage(id);
238 image_pipe.ProducerEndpoint()->RemoveImage(id);
239
240 mojo::RunLoop::current()->Run();
241 EXPECT_TRUE(image_pipe.HasExperiencedError());
242 }
243
244 // Tests that removing an image owned by the consumer causes the pipe to error
245 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageOwnedByConsumer) {
246 TestImagePipe image_pipe(false, true);
247
248 uint32_t id = 0, acquired_id = UINT32_MAX;
249 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
250 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
251 MOJO_CHECK(acquired_id == id);
252 image_pipe.ProducerEndpoint()->PresentImage(
253 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
254 image_pipe.ProducerEndpoint()->RemoveImage(id);
255
256 mojo::RunLoop::current()->Run();
257 EXPECT_TRUE(image_pipe.HasExperiencedError());
258 }
259
260 // Tests that presenting an image that hasnt been added causes the pipe to error
261 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageNotAdded) {
262 TestImagePipe image_pipe(false, true);
263
264 uint32_t id = 0;
265 image_pipe.ProducerEndpoint()->PresentImage(
266 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
267
268 mojo::RunLoop::current()->Run();
269 EXPECT_TRUE(image_pipe.HasExperiencedError());
270 }
271
272 // Tests that presenting an image that has already been presented causes the
273 // pipe to error
274 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageTwice) {
275 TestImagePipe image_pipe(false, true);
276
277 uint32_t id = 0, acquired_id = UINT32_MAX;
278 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
279 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
280 MOJO_CHECK(acquired_id == id);
281 image_pipe.ProducerEndpoint()->PresentImage(
282 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
283 image_pipe.ProducerEndpoint()->PresentImage(
284 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
285
286 mojo::RunLoop::current()->Run();
287 EXPECT_TRUE(image_pipe.HasExperiencedError());
288 }
289
290 // Tests that releasing an image that hasnt been added causes the pipe to error
291 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotInPool) {
292 TestImagePipe image_pipe(true, false);
293
294 image_pipe.OverridePresentImage([&image_pipe](uint32_t id) {
295 image_pipe.ConsumerEndpoint()->ReleaseImage(
296 id + 1, mojo::gfx::PresentationStatus::PRESENTED);
297 });
298
299 uint32_t id = 0, acquired_id = UINT32_MAX;
300 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
301 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
302 MOJO_CHECK(acquired_id == id);
303 image_pipe.ProducerEndpoint()->PresentImage(
304 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
305
306 mojo::RunLoop::current()->Run();
307 EXPECT_TRUE(image_pipe.HasExperiencedError());
308 }
309
310 // Tests that releasing an image before acquiring it causes the pipe to error
311 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseBeforeAcquire) {
312 TestImagePipe image_pipe(true, false);
313
314 image_pipe.OverridePresentImage([&image_pipe](uint32_t id) {
315 image_pipe.ConsumerEndpoint()->ReleaseImage(
316 id, mojo::gfx::PresentationStatus::PRESENTED);
317 });
318
319 uint32_t id = 0, acquired_id = UINT32_MAX;
320 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
321 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
322 MOJO_CHECK(acquired_id == id);
323 image_pipe.ProducerEndpoint()->PresentImage(
324 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
325
326 mojo::RunLoop::current()->Run();
327 EXPECT_TRUE(image_pipe.HasExperiencedError());
328 }
329
330 // Tests that releasing an image before its presented causes the pipe to error
331 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotPresented) {
332 TestImagePipe image_pipe(true, false);
333
334 image_pipe.OverridePresentImage([&image_pipe](uint32_t id) {
335 uint32_t acquired_id;
336 MOJO_CHECK(image_pipe.ConsumerEndpoint()->AcquireNextImage(&acquired_id));
337 MOJO_CHECK(acquired_id == id);
338 image_pipe.ConsumerEndpoint()->ReleaseImage(
339 id + 1, mojo::gfx::PresentationStatus::PRESENTED);
340 });
341
342 uint32_t id = 0, acquired_id = UINT32_MAX;
343 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id);
344 image_pipe.ProducerEndpoint()->AddImage(image_pipe.CreateTestImage(), id + 1);
345 MOJO_CHECK(image_pipe.ProducerEndpoint()->AcquireImage(&acquired_id));
346 MOJO_CHECK(acquired_id == id);
347 image_pipe.ProducerEndpoint()->PresentImage(
348 id, [](unsigned int, mojo::gfx::PresentationStatus) {});
349
350 mojo::RunLoop::current()->Run();
351 EXPECT_TRUE(image_pipe.HasExperiencedError());
352 }
353
354 } // namespace
355 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698