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

Side by Side Diff: cc/surfaces/surface_factory_unittest.cc

Issue 2802023002: Remove SurfaceFactory And SurfaceFactoryClient (Closed)
Patch Set: Rebase SurfaceSynchronizationTest Created 3 years, 7 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
« no previous file with comments | « cc/surfaces/surface_factory_client.h ('k') | cc/surfaces/surface_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 "cc/surfaces/surface_factory.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <utility>
11 #include <vector>
12
13 #include "base/bind.h"
14 #include "base/macros.h"
15 #include "cc/output/compositor_frame.h"
16 #include "cc/output/copy_output_request.h"
17 #include "cc/output/copy_output_result.h"
18 #include "cc/resources/resource_provider.h"
19 #include "cc/surfaces/frame_sink_manager_client.h"
20 #include "cc/surfaces/surface.h"
21 #include "cc/surfaces/surface_factory_client.h"
22 #include "cc/surfaces/surface_info.h"
23 #include "cc/surfaces/surface_manager.h"
24 #include "cc/surfaces/surface_resource_holder_client.h"
25 #include "cc/test/compositor_frame_helpers.h"
26 #include "cc/test/fake_surface_resource_holder_client.h"
27 #include "cc/test/scheduler_test_common.h"
28 #include "cc/test/stub_surface_factory_client.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/gfx/geometry/size.h"
31
32 namespace cc {
33 namespace {
34
35 static constexpr FrameSinkId kArbitraryFrameSinkId(1, 1);
36 static constexpr FrameSinkId kAnotherArbitraryFrameSinkId(2, 2);
37 static const base::UnguessableToken kArbitraryToken =
38 base::UnguessableToken::Create();
39 static auto kArbitrarySourceId1 =
40 base::UnguessableToken::Deserialize(0xdead, 0xbeef);
41 static auto kArbitrarySourceId2 =
42 base::UnguessableToken::Deserialize(0xdead, 0xbee0);
43
44 gpu::SyncToken GenTestSyncToken(int id) {
45 gpu::SyncToken token;
46 token.Set(gpu::CommandBufferNamespace::GPU_IO, 0,
47 gpu::CommandBufferId::FromUnsafeValue(id), 1);
48 return token;
49 }
50
51 class SurfaceFactoryTest : public testing::Test, public SurfaceObserver {
52 public:
53 SurfaceFactoryTest()
54 : factory_(new SurfaceFactory(kArbitraryFrameSinkId,
55 &manager_,
56 &stub_surface_factory_client_,
57 &fake_surface_resource_holder_client_)),
58 local_surface_id_(3, kArbitraryToken),
59 frame_sync_token_(GenTestSyncToken(4)),
60 consumer_sync_token_(GenTestSyncToken(5)) {
61 manager_.AddObserver(this);
62 }
63
64 const SurfaceId& last_created_surface_id() const {
65 return last_created_surface_id_;
66 }
67
68 // SurfaceObserver implementation.
69 void OnSurfaceCreated(const SurfaceInfo& surface_info) override {
70 EXPECT_EQ(kArbitraryFrameSinkId, surface_info.id().frame_sink_id());
71 last_created_surface_id_ = surface_info.id();
72 last_surface_info_ = surface_info;
73 }
74
75 void OnSurfaceDamaged(const SurfaceId& id, bool* changed) override {
76 *changed = true;
77 }
78
79 ~SurfaceFactoryTest() override {
80 manager_.RemoveObserver(this);
81 factory_->EvictSurface();
82 }
83
84 void SubmitCompositorFrameWithResources(ResourceId* resource_ids,
85 size_t num_resource_ids) {
86 CompositorFrame frame = test::MakeCompositorFrame();
87 for (size_t i = 0u; i < num_resource_ids; ++i) {
88 TransferableResource resource;
89 resource.id = resource_ids[i];
90 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
91 resource.mailbox_holder.sync_token = frame_sync_token_;
92 frame.resource_list.push_back(resource);
93 }
94 factory_->SubmitCompositorFrame(local_surface_id_, std::move(frame),
95 SurfaceFactory::DrawCallback(),
96 SurfaceFactory::WillDrawCallback());
97 EXPECT_EQ(last_created_surface_id_.local_surface_id(), local_surface_id_);
98 }
99
100 void UnrefResources(ResourceId* ids_to_unref,
101 int* counts_to_unref,
102 size_t num_ids_to_unref) {
103 ReturnedResourceArray unref_array;
104 for (size_t i = 0; i < num_ids_to_unref; ++i) {
105 ReturnedResource resource;
106 resource.sync_token = consumer_sync_token_;
107 resource.id = ids_to_unref[i];
108 resource.count = counts_to_unref[i];
109 unref_array.push_back(resource);
110 }
111 factory_->UnrefResources(unref_array);
112 }
113
114 void CheckReturnedResourcesMatchExpected(ResourceId* expected_returned_ids,
115 int* expected_returned_counts,
116 size_t expected_resources,
117 gpu::SyncToken expected_sync_token) {
118 const ReturnedResourceArray& actual_resources =
119 fake_surface_resource_holder_client_.returned_resources();
120 ASSERT_EQ(expected_resources, actual_resources.size());
121 for (size_t i = 0; i < expected_resources; ++i) {
122 ReturnedResource resource = actual_resources[i];
123 EXPECT_EQ(expected_sync_token, resource.sync_token);
124 EXPECT_EQ(expected_returned_ids[i], resource.id);
125 EXPECT_EQ(expected_returned_counts[i], resource.count);
126 }
127 fake_surface_resource_holder_client_.clear_returned_resources();
128 }
129
130 void RefCurrentFrameResources() {
131 Surface* surface = manager_.GetSurfaceForId(
132 SurfaceId(factory_->frame_sink_id(), local_surface_id_));
133 factory_->RefResources(surface->GetActiveFrame().resource_list);
134 }
135
136 protected:
137 SurfaceManager manager_;
138 StubSurfaceFactoryClient stub_surface_factory_client_;
139 FakeSurfaceResourceHolderClient fake_surface_resource_holder_client_;
140 std::unique_ptr<SurfaceFactory> factory_;
141 LocalSurfaceId local_surface_id_;
142 SurfaceId last_created_surface_id_;
143 SurfaceInfo last_surface_info_;
144
145 // This is the sync token submitted with the frame. It should never be
146 // returned to the client.
147 const gpu::SyncToken frame_sync_token_;
148
149 // This is the sync token returned by the consumer. It should always be
150 // returned to the client.
151 const gpu::SyncToken consumer_sync_token_;
152 };
153
154 // Tests submitting a frame with resources followed by one with no resources
155 // with no resource provider action in between.
156 TEST_F(SurfaceFactoryTest, ResourceLifetimeSimple) {
157 ResourceId first_frame_ids[] = {1, 2, 3};
158 SubmitCompositorFrameWithResources(first_frame_ids,
159 arraysize(first_frame_ids));
160
161 // All of the resources submitted in the first frame are still in use at this
162 // time by virtue of being in the pending frame, so none can be returned to
163 // the client yet.
164 EXPECT_EQ(0u,
165 fake_surface_resource_holder_client_.returned_resources().size());
166 fake_surface_resource_holder_client_.clear_returned_resources();
167
168 // The second frame references no resources of first frame and thus should
169 // make all resources of first frame available to be returned.
170 SubmitCompositorFrameWithResources(NULL, 0);
171
172 ResourceId expected_returned_ids[] = {1, 2, 3};
173 int expected_returned_counts[] = {1, 1, 1};
174 // Resources were never consumed so no sync token should be set.
175 CheckReturnedResourcesMatchExpected(
176 expected_returned_ids, expected_returned_counts,
177 arraysize(expected_returned_counts), gpu::SyncToken());
178
179 ResourceId third_frame_ids[] = {4, 5, 6};
180 SubmitCompositorFrameWithResources(third_frame_ids,
181 arraysize(third_frame_ids));
182
183 // All of the resources submitted in the third frame are still in use at this
184 // time by virtue of being in the pending frame, so none can be returned to
185 // the client yet.
186 EXPECT_EQ(0u,
187 fake_surface_resource_holder_client_.returned_resources().size());
188 fake_surface_resource_holder_client_.clear_returned_resources();
189
190 // The forth frame references no resources of third frame and thus should
191 // make all resources of third frame available to be returned.
192 ResourceId forth_frame_ids[] = {7, 8, 9};
193 SubmitCompositorFrameWithResources(forth_frame_ids,
194 arraysize(forth_frame_ids));
195
196 ResourceId forth_expected_returned_ids[] = {4, 5, 6};
197 int forth_expected_returned_counts[] = {1, 1, 1};
198 // Resources were never consumed so no sync token should be set.
199 CheckReturnedResourcesMatchExpected(
200 forth_expected_returned_ids, forth_expected_returned_counts,
201 arraysize(forth_expected_returned_counts), gpu::SyncToken());
202 }
203
204 // Tests submitting a frame with resources followed by one with no resources
205 // with the resource provider holding everything alive.
206 TEST_F(SurfaceFactoryTest, ResourceLifetimeSimpleWithProviderHoldingAlive) {
207 ResourceId first_frame_ids[] = {1, 2, 3};
208 SubmitCompositorFrameWithResources(first_frame_ids,
209 arraysize(first_frame_ids));
210
211 // All of the resources submitted in the first frame are still in use at this
212 // time by virtue of being in the pending frame, so none can be returned to
213 // the client yet.
214 EXPECT_EQ(0u,
215 fake_surface_resource_holder_client_.returned_resources().size());
216 fake_surface_resource_holder_client_.clear_returned_resources();
217
218 // Hold on to everything.
219 RefCurrentFrameResources();
220
221 // The second frame references no resources and thus should make all resources
222 // available to be returned as soon as the resource provider releases them.
223 SubmitCompositorFrameWithResources(NULL, 0);
224
225 EXPECT_EQ(0u,
226 fake_surface_resource_holder_client_.returned_resources().size());
227 fake_surface_resource_holder_client_.clear_returned_resources();
228
229 int release_counts[] = {1, 1, 1};
230 UnrefResources(first_frame_ids, release_counts, arraysize(first_frame_ids));
231
232 ResourceId expected_returned_ids[] = {1, 2, 3};
233 int expected_returned_counts[] = {1, 1, 1};
234 CheckReturnedResourcesMatchExpected(
235 expected_returned_ids, expected_returned_counts,
236 arraysize(expected_returned_counts), consumer_sync_token_);
237 }
238
239 // Tests referencing a resource, unref'ing it to zero, then using it again
240 // before returning it to the client.
241 TEST_F(SurfaceFactoryTest, ResourceReusedBeforeReturn) {
242 ResourceId first_frame_ids[] = {7};
243 SubmitCompositorFrameWithResources(first_frame_ids,
244 arraysize(first_frame_ids));
245
246 // This removes all references to resource id 7.
247 SubmitCompositorFrameWithResources(NULL, 0);
248
249 // This references id 7 again.
250 SubmitCompositorFrameWithResources(first_frame_ids,
251 arraysize(first_frame_ids));
252
253 // This removes it again.
254 SubmitCompositorFrameWithResources(NULL, 0);
255
256 // Now it should be returned.
257 // We don't care how many entries are in the returned array for 7, so long as
258 // the total returned count matches the submitted count.
259 const ReturnedResourceArray& returned =
260 fake_surface_resource_holder_client_.returned_resources();
261 size_t return_count = 0;
262 for (size_t i = 0; i < returned.size(); ++i) {
263 EXPECT_EQ(7u, returned[i].id);
264 return_count += returned[i].count;
265 }
266 EXPECT_EQ(2u, return_count);
267 }
268
269 // Tests having resources referenced multiple times, as if referenced by
270 // multiple providers.
271 TEST_F(SurfaceFactoryTest, ResourceRefMultipleTimes) {
272 ResourceId first_frame_ids[] = {3, 4};
273 SubmitCompositorFrameWithResources(first_frame_ids,
274 arraysize(first_frame_ids));
275
276 // Ref resources from the first frame twice.
277 RefCurrentFrameResources();
278 RefCurrentFrameResources();
279
280 ResourceId second_frame_ids[] = {4, 5};
281 SubmitCompositorFrameWithResources(second_frame_ids,
282 arraysize(second_frame_ids));
283
284 // Ref resources from the second frame 3 times.
285 RefCurrentFrameResources();
286 RefCurrentFrameResources();
287 RefCurrentFrameResources();
288
289 // Submit a frame with no resources to remove all current frame refs from
290 // submitted resources.
291 SubmitCompositorFrameWithResources(NULL, 0);
292
293 EXPECT_EQ(0u,
294 fake_surface_resource_holder_client_.returned_resources().size());
295 fake_surface_resource_holder_client_.clear_returned_resources();
296
297 // Expected current refs:
298 // 3 -> 2
299 // 4 -> 2 + 3 = 5
300 // 5 -> 3
301 {
302 SCOPED_TRACE("unref all 3");
303 ResourceId ids_to_unref[] = {3, 4, 5};
304 int counts[] = {1, 1, 1};
305 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
306
307 EXPECT_EQ(0u,
308 fake_surface_resource_holder_client_.returned_resources().size());
309 fake_surface_resource_holder_client_.clear_returned_resources();
310
311 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
312
313 ResourceId expected_returned_ids[] = {3};
314 int expected_returned_counts[] = {1};
315 CheckReturnedResourcesMatchExpected(
316 expected_returned_ids, expected_returned_counts,
317 arraysize(expected_returned_counts), consumer_sync_token_);
318 }
319
320 // Expected refs remaining:
321 // 4 -> 3
322 // 5 -> 1
323 {
324 SCOPED_TRACE("unref 4 and 5");
325 ResourceId ids_to_unref[] = {4, 5};
326 int counts[] = {1, 1};
327 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
328
329 ResourceId expected_returned_ids[] = {5};
330 int expected_returned_counts[] = {1};
331 CheckReturnedResourcesMatchExpected(
332 expected_returned_ids, expected_returned_counts,
333 arraysize(expected_returned_counts), consumer_sync_token_);
334 }
335
336 // Now, just 2 refs remaining on resource 4. Unref both at once and make sure
337 // the returned count is correct.
338 {
339 SCOPED_TRACE("unref only 4");
340 ResourceId ids_to_unref[] = {4};
341 int counts[] = {2};
342 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
343
344 ResourceId expected_returned_ids[] = {4};
345 int expected_returned_counts[] = {2};
346 CheckReturnedResourcesMatchExpected(
347 expected_returned_ids, expected_returned_counts,
348 arraysize(expected_returned_counts), consumer_sync_token_);
349 }
350 }
351
352 TEST_F(SurfaceFactoryTest, ResourceLifetime) {
353 ResourceId first_frame_ids[] = {1, 2, 3};
354 SubmitCompositorFrameWithResources(first_frame_ids,
355 arraysize(first_frame_ids));
356
357 // All of the resources submitted in the first frame are still in use at this
358 // time by virtue of being in the pending frame, so none can be returned to
359 // the client yet.
360 EXPECT_EQ(0u,
361 fake_surface_resource_holder_client_.returned_resources().size());
362 fake_surface_resource_holder_client_.clear_returned_resources();
363
364 // The second frame references some of the same resources, but some different
365 // ones. We expect to receive back resource 1 with a count of 1 since it was
366 // only referenced by the first frame.
367 ResourceId second_frame_ids[] = {2, 3, 4};
368 SubmitCompositorFrameWithResources(second_frame_ids,
369 arraysize(second_frame_ids));
370
371 {
372 SCOPED_TRACE("second frame");
373 ResourceId expected_returned_ids[] = {1};
374 int expected_returned_counts[] = {1};
375 CheckReturnedResourcesMatchExpected(
376 expected_returned_ids, expected_returned_counts,
377 arraysize(expected_returned_counts), gpu::SyncToken());
378 }
379
380 // The third frame references a disjoint set of resources, so we expect to
381 // receive back all resources from the first and second frames. Resource IDs 2
382 // and 3 will have counts of 2, since they were used in both frames, and
383 // resource ID 4 will have a count of 1.
384 ResourceId third_frame_ids[] = {10, 11, 12, 13};
385 SubmitCompositorFrameWithResources(third_frame_ids,
386 arraysize(third_frame_ids));
387
388 {
389 SCOPED_TRACE("third frame");
390 ResourceId expected_returned_ids[] = {2, 3, 4};
391 int expected_returned_counts[] = {2, 2, 1};
392 CheckReturnedResourcesMatchExpected(
393 expected_returned_ids, expected_returned_counts,
394 arraysize(expected_returned_counts), gpu::SyncToken());
395 }
396
397 // Simulate a ResourceProvider taking a ref on all of the resources.
398 RefCurrentFrameResources();
399
400 ResourceId fourth_frame_ids[] = {12, 13};
401 SubmitCompositorFrameWithResources(fourth_frame_ids,
402 arraysize(fourth_frame_ids));
403
404 EXPECT_EQ(0u,
405 fake_surface_resource_holder_client_.returned_resources().size());
406
407 RefCurrentFrameResources();
408
409 // All resources are still being used by the external reference, so none can
410 // be returned to the client.
411 EXPECT_EQ(0u,
412 fake_surface_resource_holder_client_.returned_resources().size());
413
414 // Release resources associated with the first RefCurrentFrameResources() call
415 // first.
416 {
417 ResourceId ids_to_unref[] = {10, 11, 12, 13};
418 int counts[] = {1, 1, 1, 1};
419 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
420 }
421
422 {
423 SCOPED_TRACE("fourth frame, first unref");
424 ResourceId expected_returned_ids[] = {10, 11};
425 int expected_returned_counts[] = {1, 1};
426 CheckReturnedResourcesMatchExpected(
427 expected_returned_ids, expected_returned_counts,
428 arraysize(expected_returned_counts), consumer_sync_token_);
429 }
430
431 {
432 ResourceId ids_to_unref[] = {12, 13};
433 int counts[] = {1, 1};
434 UnrefResources(ids_to_unref, counts, arraysize(ids_to_unref));
435 }
436
437 // Resources 12 and 13 are still in use by the current frame, so they
438 // shouldn't be available to be returned.
439 EXPECT_EQ(0u,
440 fake_surface_resource_holder_client_.returned_resources().size());
441
442 // If we submit an empty frame, however, they should become available.
443 SubmitCompositorFrameWithResources(NULL, 0u);
444
445 {
446 SCOPED_TRACE("fourth frame, second unref");
447 ResourceId expected_returned_ids[] = {12, 13};
448 int expected_returned_counts[] = {2, 2};
449 CheckReturnedResourcesMatchExpected(
450 expected_returned_ids, expected_returned_counts,
451 arraysize(expected_returned_counts), consumer_sync_token_);
452 }
453 }
454
455 void CreateSurfaceDrawCallback(SurfaceFactory* factory,
456 uint32_t* execute_count) {
457 LocalSurfaceId new_id(7, base::UnguessableToken::Create());
458 factory->SubmitCompositorFrame(new_id, test::MakeCompositorFrame(),
459 SurfaceFactory::DrawCallback(),
460 SurfaceFactory::WillDrawCallback());
461 factory->EvictSurface();
462 *execute_count += 1;
463 }
464
465 TEST_F(SurfaceFactoryTest, AddDuringEviction) {
466 LocalSurfaceId local_surface_id(6, kArbitraryToken);
467
468 uint32_t execute_count = 0;
469 factory_->SubmitCompositorFrame(
470 local_surface_id, test::MakeCompositorFrame(),
471 base::Bind(&CreateSurfaceDrawCallback, base::Unretained(factory_.get()),
472 &execute_count),
473 SurfaceFactory::WillDrawCallback());
474 EXPECT_EQ(0u, execute_count);
475 factory_->EvictSurface();
476 EXPECT_EQ(1u, execute_count);
477 }
478
479 void DrawCallback(uint32_t* execute_count) {
480 *execute_count += 1;
481 }
482
483 // Tests doing an EvictSurface before shutting down the factory.
484 TEST_F(SurfaceFactoryTest, EvictSurface) {
485 LocalSurfaceId local_surface_id(7, kArbitraryToken);
486 SurfaceId id(kArbitraryFrameSinkId, local_surface_id);
487
488 TransferableResource resource;
489 resource.id = 1;
490 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
491 CompositorFrame frame = test::MakeCompositorFrame();
492 frame.resource_list.push_back(resource);
493 uint32_t execute_count = 0;
494 factory_->SubmitCompositorFrame(local_surface_id, std::move(frame),
495 base::Bind(&DrawCallback, &execute_count),
496 SurfaceFactory::WillDrawCallback());
497 EXPECT_EQ(last_created_surface_id().local_surface_id(), local_surface_id);
498 local_surface_id_ = LocalSurfaceId();
499
500 EXPECT_TRUE(manager_.GetSurfaceForId(id));
501 EXPECT_TRUE(
502 fake_surface_resource_holder_client_.returned_resources().empty());
503 factory_->EvictSurface();
504 EXPECT_FALSE(manager_.GetSurfaceForId(id));
505 EXPECT_FALSE(
506 fake_surface_resource_holder_client_.returned_resources().empty());
507 EXPECT_EQ(1u, execute_count);
508 }
509
510 // Tests doing an EvictSurface which has unregistered dependency.
511 TEST_F(SurfaceFactoryTest, EvictSurfaceDependencyUnRegistered) {
512 LocalSurfaceId local_surface_id(7, kArbitraryToken);
513
514 TransferableResource resource;
515 resource.id = 1;
516 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
517 CompositorFrame frame = test::MakeCompositorFrame();
518 frame.resource_list.push_back(resource);
519 uint32_t execute_count = 0;
520 factory_->SubmitCompositorFrame(local_surface_id, std::move(frame),
521 base::Bind(&DrawCallback, &execute_count),
522 SurfaceFactory::WillDrawCallback());
523 EXPECT_EQ(last_created_surface_id().local_surface_id(), local_surface_id);
524 local_surface_id_ = LocalSurfaceId();
525
526 SurfaceId surface_id(kArbitraryFrameSinkId, local_surface_id);
527 Surface* surface = manager_.GetSurfaceForId(surface_id);
528 surface->AddDestructionDependency(
529 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4));
530
531 EXPECT_TRUE(manager_.GetSurfaceForId(surface_id));
532 EXPECT_TRUE(
533 fake_surface_resource_holder_client_.returned_resources().empty());
534 factory_->EvictSurface();
535 EXPECT_FALSE(manager_.GetSurfaceForId(surface_id));
536 EXPECT_FALSE(
537 fake_surface_resource_holder_client_.returned_resources().empty());
538 EXPECT_EQ(1u, execute_count);
539 }
540
541 // Tests doing an EvictSurface which has registered dependency.
542 TEST_F(SurfaceFactoryTest, EvictSurfaceDependencyRegistered) {
543 LocalSurfaceId local_surface_id(7, kArbitraryToken);
544
545 TransferableResource resource;
546 resource.id = 1;
547 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
548 CompositorFrame frame = test::MakeCompositorFrame();
549 frame.resource_list.push_back(resource);
550 uint32_t execute_count = 0;
551 factory_->SubmitCompositorFrame(local_surface_id, std::move(frame),
552 base::Bind(&DrawCallback, &execute_count),
553 SurfaceFactory::WillDrawCallback());
554 EXPECT_EQ(last_created_surface_id().local_surface_id(), local_surface_id);
555 local_surface_id_ = LocalSurfaceId();
556
557 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId);
558
559 SurfaceId surface_id(kArbitraryFrameSinkId, local_surface_id);
560 Surface* surface = manager_.GetSurfaceForId(surface_id);
561 surface->AddDestructionDependency(
562 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4));
563
564 EXPECT_TRUE(manager_.GetSurfaceForId(surface_id));
565 EXPECT_TRUE(
566 fake_surface_resource_holder_client_.returned_resources().empty());
567 factory_->EvictSurface();
568 EXPECT_TRUE(manager_.GetSurfaceForId(surface_id));
569 EXPECT_TRUE(
570 fake_surface_resource_holder_client_.returned_resources().empty());
571 EXPECT_EQ(0u, execute_count);
572
573 manager_.SatisfySequence(SurfaceSequence(kAnotherArbitraryFrameSinkId, 4));
574 EXPECT_FALSE(manager_.GetSurfaceForId(surface_id));
575 EXPECT_FALSE(
576 fake_surface_resource_holder_client_.returned_resources().empty());
577 }
578
579 TEST_F(SurfaceFactoryTest, DestroySequence) {
580 LocalSurfaceId local_surface_id2(5, kArbitraryToken);
581 std::unique_ptr<SurfaceFactory> factory2(new SurfaceFactory(
582 kArbitraryFrameSinkId, &manager_, &stub_surface_factory_client_,
583 &fake_surface_resource_holder_client_));
584 SurfaceId id2(kArbitraryFrameSinkId, local_surface_id2);
585 factory2->SubmitCompositorFrame(
586 local_surface_id2, test::MakeCompositorFrame(),
587 SurfaceFactory::DrawCallback(), SurfaceFactory::WillDrawCallback());
588
589 manager_.RegisterFrameSinkId(kArbitraryFrameSinkId);
590
591 // Check that waiting before the sequence is satisfied works.
592 manager_.GetSurfaceForId(id2)->AddDestructionDependency(
593 SurfaceSequence(kArbitraryFrameSinkId, 4));
594 factory2->EvictSurface();
595
596 DCHECK(manager_.GetSurfaceForId(id2));
597 manager_.SatisfySequence(SurfaceSequence(kArbitraryFrameSinkId, 4));
598 manager_.SatisfySequence(SurfaceSequence(kArbitraryFrameSinkId, 6));
599 DCHECK(!manager_.GetSurfaceForId(id2));
600
601 // Check that waiting after the sequence is satisfied works.
602 factory2->SubmitCompositorFrame(
603 local_surface_id2, test::MakeCompositorFrame(),
604 SurfaceFactory::DrawCallback(), SurfaceFactory::WillDrawCallback());
605 DCHECK(manager_.GetSurfaceForId(id2));
606 manager_.GetSurfaceForId(id2)->AddDestructionDependency(
607 SurfaceSequence(kAnotherArbitraryFrameSinkId, 6));
608 factory2->EvictSurface();
609 DCHECK(!manager_.GetSurfaceForId(id2));
610 }
611
612 // Tests that Surface ID namespace invalidation correctly allows
613 // Sequences to be ignored.
614 TEST_F(SurfaceFactoryTest, InvalidFrameSinkId) {
615 FrameSinkId frame_sink_id(1234, 5678);
616
617 LocalSurfaceId local_surface_id(5, kArbitraryToken);
618 SurfaceId id(factory_->frame_sink_id(), local_surface_id);
619 factory_->SubmitCompositorFrame(local_surface_id, test::MakeCompositorFrame(),
620 SurfaceFactory::DrawCallback(),
621 SurfaceFactory::WillDrawCallback());
622
623 manager_.RegisterFrameSinkId(frame_sink_id);
624 manager_.GetSurfaceForId(id)->AddDestructionDependency(
625 SurfaceSequence(frame_sink_id, 4));
626
627 factory_->EvictSurface();
628
629 // Verify the dependency has prevented the surface from getting destroyed.
630 EXPECT_TRUE(manager_.GetSurfaceForId(id));
631
632 manager_.InvalidateFrameSinkId(frame_sink_id);
633
634 // Verify that the invalidated namespace caused the unsatisfied sequence
635 // to be ignored.
636 EXPECT_FALSE(manager_.GetSurfaceForId(id));
637 }
638
639 TEST_F(SurfaceFactoryTest, DestroyCycle) {
640 LocalSurfaceId local_surface_id2(5, kArbitraryToken);
641 SurfaceId id2(kArbitraryFrameSinkId, local_surface_id2);
642 std::unique_ptr<SurfaceFactory> factory2(new SurfaceFactory(
643 kArbitraryFrameSinkId, &manager_, &stub_surface_factory_client_,
644 &fake_surface_resource_holder_client_));
645 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId);
646 // Give id2 a frame that references local_surface_id_.
647 {
648 std::unique_ptr<RenderPass> render_pass(RenderPass::Create());
649 CompositorFrame frame = test::MakeCompositorFrame();
650 frame.render_pass_list.push_back(std::move(render_pass));
651 frame.metadata.referenced_surfaces.push_back(
652 SurfaceId(factory_->frame_sink_id(), local_surface_id_));
653 factory2->SubmitCompositorFrame(local_surface_id2, std::move(frame),
654 SurfaceFactory::DrawCallback(),
655 SurfaceFactory::WillDrawCallback());
656 EXPECT_EQ(last_created_surface_id().local_surface_id(), local_surface_id2);
657 }
658 manager_.GetSurfaceForId(id2)->AddDestructionDependency(
659 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4));
660 factory2->EvictSurface();
661 // Give local_surface_id_ a frame that references id2.
662 {
663 std::unique_ptr<RenderPass> render_pass(RenderPass::Create());
664 CompositorFrame frame = test::MakeCompositorFrame();
665 frame.render_pass_list.push_back(std::move(render_pass));
666 frame.metadata.referenced_surfaces.push_back(id2);
667 factory_->SubmitCompositorFrame(local_surface_id_, std::move(frame),
668 SurfaceFactory::DrawCallback(),
669 SurfaceFactory::WillDrawCallback());
670 }
671 factory_->EvictSurface();
672 EXPECT_TRUE(manager_.GetSurfaceForId(id2));
673 // local_surface_id_ should be retained by reference from id2.
674 EXPECT_TRUE(manager_.GetSurfaceForId(
675 SurfaceId(factory_->frame_sink_id(), local_surface_id_)));
676
677 // Satisfy last destruction dependency for id2.
678 manager_.SatisfySequence(SurfaceSequence(kAnotherArbitraryFrameSinkId, 4));
679
680 // id2 and local_surface_id_ are in a reference cycle that has no surface
681 // sequences holding on to it, so they should be destroyed.
682 EXPECT_TRUE(!manager_.GetSurfaceForId(id2));
683 EXPECT_TRUE(!manager_.GetSurfaceForId(
684 SurfaceId(factory_->frame_sink_id(), local_surface_id_)));
685
686 local_surface_id_ = LocalSurfaceId();
687 }
688
689 void CopyRequestTestCallback(bool* called,
690 std::unique_ptr<CopyOutputResult> result) {
691 *called = true;
692 }
693
694 TEST_F(SurfaceFactoryTest, DuplicateCopyRequest) {
695 {
696 std::unique_ptr<RenderPass> render_pass(RenderPass::Create());
697 CompositorFrame frame = test::MakeCompositorFrame();
698 frame.render_pass_list.push_back(std::move(render_pass));
699 frame.metadata.referenced_surfaces.push_back(
700 SurfaceId(factory_->frame_sink_id(), local_surface_id_));
701 factory_->SubmitCompositorFrame(local_surface_id_, std::move(frame),
702 SurfaceFactory::DrawCallback(),
703 SurfaceFactory::WillDrawCallback());
704 EXPECT_EQ(last_created_surface_id().local_surface_id(), local_surface_id_);
705 }
706
707 bool called1 = false;
708 std::unique_ptr<CopyOutputRequest> request;
709 request = CopyOutputRequest::CreateRequest(
710 base::Bind(&CopyRequestTestCallback, &called1));
711 request->set_source(kArbitrarySourceId1);
712
713 factory_->RequestCopyOfSurface(std::move(request));
714 EXPECT_FALSE(called1);
715
716 bool called2 = false;
717 request = CopyOutputRequest::CreateRequest(
718 base::Bind(&CopyRequestTestCallback, &called2));
719 request->set_source(kArbitrarySourceId2);
720
721 factory_->RequestCopyOfSurface(std::move(request));
722 // Callbacks have different sources so neither should be called.
723 EXPECT_FALSE(called1);
724 EXPECT_FALSE(called2);
725
726 bool called3 = false;
727 request = CopyOutputRequest::CreateRequest(
728 base::Bind(&CopyRequestTestCallback, &called3));
729 request->set_source(kArbitrarySourceId1);
730
731 factory_->RequestCopyOfSurface(std::move(request));
732 // Two callbacks are from source1, so the first should be called.
733 EXPECT_TRUE(called1);
734 EXPECT_FALSE(called2);
735 EXPECT_FALSE(called3);
736
737 factory_->EvictSurface();
738 local_surface_id_ = LocalSurfaceId();
739 EXPECT_TRUE(called1);
740 EXPECT_TRUE(called2);
741 EXPECT_TRUE(called3);
742 }
743
744 // Check whether the SurfaceInfo object is created and populated correctly
745 // after the frame submission.
746 TEST_F(SurfaceFactoryTest, SurfaceInfo) {
747 CompositorFrame frame = test::MakeEmptyCompositorFrame();
748
749 auto render_pass = RenderPass::Create();
750 render_pass->SetNew(1, gfx::Rect(5, 6), gfx::Rect(), gfx::Transform());
751 frame.render_pass_list.push_back(std::move(render_pass));
752
753 render_pass = RenderPass::Create();
754 render_pass->SetNew(2, gfx::Rect(7, 8), gfx::Rect(), gfx::Transform());
755 frame.render_pass_list.push_back(std::move(render_pass));
756
757 frame.metadata.device_scale_factor = 2.5f;
758
759 factory_->SubmitCompositorFrame(local_surface_id_, std::move(frame),
760 SurfaceFactory::DrawCallback(),
761 SurfaceFactory::WillDrawCallback());
762 SurfaceId expected_surface_id(factory_->frame_sink_id(), local_surface_id_);
763 EXPECT_EQ(expected_surface_id, last_surface_info_.id());
764 EXPECT_EQ(2.5f, last_surface_info_.device_scale_factor());
765 EXPECT_EQ(gfx::Size(7, 8), last_surface_info_.size_in_pixels());
766 }
767
768 } // namespace
769 } // namespace cc
OLDNEW
« no previous file with comments | « cc/surfaces/surface_factory_client.h ('k') | cc/surfaces/surface_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698