OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/gpu_command_buffer_stub.h" | 5 #include "content/common/gpu/gpu_command_buffer_stub.h" |
6 #include "content/common/gpu/gpu_memory_allocation.h" | 6 #include "content/common/gpu/gpu_memory_allocation.h" |
7 #include "content/common/gpu/gpu_memory_manager.h" | 7 #include "content/common/gpu/gpu_memory_manager.h" |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 static int32 GenerateUniqueSurfaceId() { | 107 static int32 GenerateUniqueSurfaceId() { |
108 static int32 surface_id_ = 1; | 108 static int32 surface_id_ = 1; |
109 return surface_id_++; | 109 return surface_id_++; |
110 } | 110 } |
111 | 111 |
112 static bool IsMoreImportant(GpuCommandBufferStubBase* lhs, | 112 static bool IsMoreImportant(GpuCommandBufferStubBase* lhs, |
113 GpuCommandBufferStubBase* rhs) { | 113 GpuCommandBufferStubBase* rhs) { |
114 return GpuMemoryManager::StubWithSurfaceComparator()(lhs, rhs); | 114 return GpuMemoryManager::StubWithSurfaceComparator()(lhs, rhs); |
115 } | 115 } |
116 | 116 |
117 static bool IsAllocationForegroundForSurfaceYes( | 117 bool IsAllocationForegroundForSurfaceYes( |
118 const GpuMemoryAllocation& alloc) { | 118 const GpuMemoryAllocation& alloc) { |
119 return alloc.suggest_have_frontbuffer && | 119 return alloc.suggest_have_frontbuffer && |
120 alloc.suggest_have_backbuffer && | 120 alloc.suggest_have_backbuffer && |
121 alloc.gpu_resource_size_in_bytes >= | 121 alloc.gpu_resource_size_in_bytes >= |
122 GpuMemoryManager::kMinimumAllocationForTab; | 122 GetMinimumTabAllocation(); |
greggman
2012/07/31 00:30:41
style: I don't think these are supposed to be inde
ccameron
2012/07/31 00:52:49
I'm not sure on this -- it'a a compound clause wit
| |
123 } | 123 } |
124 static bool IsAllocationBackgroundForSurfaceYes( | 124 bool IsAllocationBackgroundForSurfaceYes( |
125 const GpuMemoryAllocation& alloc) { | 125 const GpuMemoryAllocation& alloc) { |
126 return alloc.suggest_have_frontbuffer && | 126 return alloc.suggest_have_frontbuffer && |
127 !alloc.suggest_have_backbuffer && | 127 !alloc.suggest_have_backbuffer && |
128 alloc.gpu_resource_size_in_bytes == 0; | 128 alloc.gpu_resource_size_in_bytes == 0; |
129 } | 129 } |
130 static bool IsAllocationHibernatedForSurfaceYes( | 130 bool IsAllocationHibernatedForSurfaceYes( |
131 const GpuMemoryAllocation& alloc) { | 131 const GpuMemoryAllocation& alloc) { |
132 return !alloc.suggest_have_frontbuffer && | 132 return !alloc.suggest_have_frontbuffer && |
133 !alloc.suggest_have_backbuffer && | 133 !alloc.suggest_have_backbuffer && |
134 alloc.gpu_resource_size_in_bytes == 0; | 134 alloc.gpu_resource_size_in_bytes == 0; |
135 } | 135 } |
136 static bool IsAllocationForegroundForSurfaceNo( | 136 bool IsAllocationForegroundForSurfaceNo( |
137 const GpuMemoryAllocation& alloc) { | 137 const GpuMemoryAllocation& alloc) { |
138 return !alloc.suggest_have_frontbuffer && | 138 return !alloc.suggest_have_frontbuffer && |
139 !alloc.suggest_have_backbuffer && | 139 !alloc.suggest_have_backbuffer && |
140 alloc.gpu_resource_size_in_bytes == | 140 alloc.gpu_resource_size_in_bytes == |
141 GpuMemoryManager::kMinimumAllocationForTab; | 141 GetMinimumTabAllocation(); |
142 } | 142 } |
143 static bool IsAllocationBackgroundForSurfaceNo( | 143 bool IsAllocationBackgroundForSurfaceNo( |
144 const GpuMemoryAllocation& alloc) { | 144 const GpuMemoryAllocation& alloc) { |
145 return !alloc.suggest_have_frontbuffer && | 145 return !alloc.suggest_have_frontbuffer && |
146 !alloc.suggest_have_backbuffer && | 146 !alloc.suggest_have_backbuffer && |
147 alloc.gpu_resource_size_in_bytes == | 147 alloc.gpu_resource_size_in_bytes == |
148 GpuMemoryManager::kMinimumAllocationForTab; | 148 GetMinimumTabAllocation(); |
149 } | 149 } |
150 static bool IsAllocationHibernatedForSurfaceNo( | 150 bool IsAllocationHibernatedForSurfaceNo( |
151 const GpuMemoryAllocation& alloc) { | 151 const GpuMemoryAllocation& alloc) { |
152 return !alloc.suggest_have_frontbuffer && | 152 return !alloc.suggest_have_frontbuffer && |
153 !alloc.suggest_have_backbuffer && | 153 !alloc.suggest_have_backbuffer && |
154 alloc.gpu_resource_size_in_bytes == 0; | 154 alloc.gpu_resource_size_in_bytes == 0; |
155 } | 155 } |
156 | 156 |
157 void Manage() { | 157 void Manage() { |
158 memory_manager_.Manage(); | 158 memory_manager_.Manage(); |
159 } | 159 } |
160 | 160 |
161 size_t GetAvailableGpuMemory() { | |
162 return memory_manager_.GetAvailableGpuMemory(); | |
163 } | |
164 | |
165 size_t GetMinimumTabAllocation() { | |
166 return memory_manager_.GetMinimumTabAllocation(); | |
167 } | |
168 | |
161 base::TimeTicks older_, newer_, newest_; | 169 base::TimeTicks older_, newer_, newest_; |
162 FakeClient client_; | 170 FakeClient client_; |
163 GpuMemoryManager memory_manager_; | 171 GpuMemoryManager memory_manager_; |
164 }; | 172 }; |
165 | 173 |
166 // Create fake stubs with every combination of {visibilty,last_use_time} | 174 // Create fake stubs with every combination of {visibilty,last_use_time} |
167 // and make sure they compare correctly. Only compare stubs with surfaces. | 175 // and make sure they compare correctly. Only compare stubs with surfaces. |
168 // Expect {more visible, newer} surfaces to be more important, in that order. | 176 // Expect {more visible, newer} surfaces to be more important, in that order. |
169 TEST_F(GpuMemoryManagerTest, ComparatorTests) { | 177 TEST_F(GpuMemoryManagerTest, ComparatorTests) { |
170 FakeCommandBufferStub stub_true1(GenerateUniqueSurfaceId(), true, older_), | 178 FakeCommandBufferStub stub_true1(GenerateUniqueSurfaceId(), true, older_), |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
474 EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub1.allocation_)); | 482 EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub1.allocation_)); |
475 EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub2.allocation_)); | 483 EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub2.allocation_)); |
476 EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub3.allocation_)); | 484 EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub3.allocation_)); |
477 EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub4.allocation_)); | 485 EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub4.allocation_)); |
478 } | 486 } |
479 | 487 |
480 #if !defined(OS_ANDROID) | 488 #if !defined(OS_ANDROID) |
481 // Test GpuMemoryAllocation memory allocation bonuses: | 489 // Test GpuMemoryAllocation memory allocation bonuses: |
482 // When the number of visible tabs is small, each tab should get a | 490 // When the number of visible tabs is small, each tab should get a |
483 // gpu_resource_size_in_bytes allocation value that is greater than | 491 // gpu_resource_size_in_bytes allocation value that is greater than |
484 // kMinimumAllocationForTab, and when the number of tabs is large, each should | 492 // GetMinimumTabAllocation(), and when the number of tabs is large, each should |
485 // get exactly kMinimumAllocationForTab and not less. | 493 // get exactly GetMinimumTabAllocation() and not less. |
486 TEST_F(GpuMemoryManagerTest, TestForegroundStubsGetBonusAllocation) { | 494 TEST_F(GpuMemoryManagerTest, TestForegroundStubsGetBonusAllocation) { |
487 size_t max_stubs_before_no_bonus = | 495 size_t max_stubs_before_no_bonus = |
488 GpuMemoryManager::kMaximumAllocationForTabs / | 496 GetAvailableGpuMemory() / (GetMinimumTabAllocation() + 1); |
489 (GpuMemoryManager::kMinimumAllocationForTab + 1); | |
490 | 497 |
491 std::vector<FakeCommandBufferStub> stubs; | 498 std::vector<FakeCommandBufferStub> stubs; |
492 for (size_t i = 0; i < max_stubs_before_no_bonus; ++i) { | 499 for (size_t i = 0; i < max_stubs_before_no_bonus; ++i) { |
493 stubs.push_back( | 500 stubs.push_back( |
494 FakeCommandBufferStub(GenerateUniqueSurfaceId(), true, older_)); | 501 FakeCommandBufferStub(GenerateUniqueSurfaceId(), true, older_)); |
495 } | 502 } |
496 for (size_t i = 0; i < stubs.size(); ++i) { | 503 for (size_t i = 0; i < stubs.size(); ++i) { |
497 client_.stubs_.push_back(&stubs[i]); | 504 client_.stubs_.push_back(&stubs[i]); |
498 } | 505 } |
499 | 506 |
500 Manage(); | 507 Manage(); |
501 for (size_t i = 0; i < stubs.size(); ++i) { | 508 for (size_t i = 0; i < stubs.size(); ++i) { |
502 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i].allocation_)); | 509 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i].allocation_)); |
503 EXPECT_GT(stubs[i].allocation_.gpu_resource_size_in_bytes, | 510 EXPECT_GT(stubs[i].allocation_.gpu_resource_size_in_bytes, |
504 static_cast<size_t>(GpuMemoryManager::kMinimumAllocationForTab)); | 511 static_cast<size_t>(GetMinimumTabAllocation())); |
505 } | 512 } |
506 | 513 |
507 FakeCommandBufferStub extra_stub(GenerateUniqueSurfaceId(), true, older_); | 514 FakeCommandBufferStub extra_stub(GenerateUniqueSurfaceId(), true, older_); |
508 client_.stubs_.push_back(&extra_stub); | 515 client_.stubs_.push_back(&extra_stub); |
509 | 516 |
510 Manage(); | 517 Manage(); |
511 for (size_t i = 0; i < stubs.size(); ++i) { | 518 for (size_t i = 0; i < stubs.size(); ++i) { |
512 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i].allocation_)); | 519 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i].allocation_)); |
513 EXPECT_EQ(stubs[i].allocation_.gpu_resource_size_in_bytes, | 520 EXPECT_EQ(stubs[i].allocation_.gpu_resource_size_in_bytes, |
514 GpuMemoryManager::kMinimumAllocationForTab); | 521 GetMinimumTabAllocation()); |
515 } | 522 } |
516 } | 523 } |
517 #else | 524 #else |
518 // Test GpuMemoryAllocation memory allocation bonuses: | 525 // Test GpuMemoryAllocation memory allocation bonuses: |
519 // When the size of tab contents is small, bonus allocation should be 0. | 526 // When the size of tab contents is small, bonus allocation should be 0. |
520 // As the size of tab contents increases, bonus allocation should increase | 527 // As the size of tab contents increases, bonus allocation should increase |
521 // until finally reaching the maximum allocation limit. | 528 // until finally reaching the maximum allocation limit. |
522 TEST_F(GpuMemoryManagerTest, TestForegroundStubsGetBonusAllocationAndroid) { | 529 TEST_F(GpuMemoryManagerTest, TestForegroundStubsGetBonusAllocationAndroid) { |
523 FakeCommandBufferStub stub(GenerateUniqueSurfaceId(), true, older_); | 530 FakeCommandBufferStub stub(GenerateUniqueSurfaceId(), true, older_); |
524 client_.stubs_.push_back(&stub); | 531 client_.stubs_.push_back(&stub); |
525 | 532 |
526 stub.size_ = gfx::Size(1,1); | 533 stub.size_ = gfx::Size(1,1); |
527 Manage(); | 534 Manage(); |
528 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); | 535 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); |
529 EXPECT_EQ(stub.allocation_.gpu_resource_size_in_bytes, | 536 EXPECT_EQ(stub.allocation_.gpu_resource_size_in_bytes, |
530 GpuMemoryManager::kMinimumAllocationForTab); | 537 GetMinimumTabAllocation()); |
531 | 538 |
532 // Keep increasing size, making sure allocation is always increasing | 539 // Keep increasing size, making sure allocation is always increasing |
533 // Until it finally reaches the maximum. | 540 // Until it finally reaches the maximum. |
534 while (stub.allocation_.gpu_resource_size_in_bytes < | 541 while (stub.allocation_.gpu_resource_size_in_bytes < |
535 GpuMemoryManager::kMaximumAllocationForTabs) { | 542 GetAvailableGpuMemory()) { |
536 size_t previous_allocation = stub.allocation_.gpu_resource_size_in_bytes; | 543 size_t previous_allocation = stub.allocation_.gpu_resource_size_in_bytes; |
537 | 544 |
538 stub.size_ = stub.size_.Scale(1, 2); | 545 stub.size_ = stub.size_.Scale(1, 2); |
539 | 546 |
540 Manage(); | 547 Manage(); |
541 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); | 548 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); |
542 EXPECT_GE(stub.allocation_.gpu_resource_size_in_bytes, | 549 EXPECT_GE(stub.allocation_.gpu_resource_size_in_bytes, |
543 GpuMemoryManager::kMinimumAllocationForTab); | 550 GetMinimumTabAllocation()); |
544 EXPECT_LE(stub.allocation_.gpu_resource_size_in_bytes, | 551 EXPECT_LE(stub.allocation_.gpu_resource_size_in_bytes, |
545 GpuMemoryManager::kMaximumAllocationForTabs); | 552 GetAvailableGpuMemory()); |
546 EXPECT_GE(stub.allocation_.gpu_resource_size_in_bytes, | 553 EXPECT_GE(stub.allocation_.gpu_resource_size_in_bytes, |
547 previous_allocation); | 554 previous_allocation); |
548 } | 555 } |
549 | 556 |
550 // One final size increase to confirm it stays capped at maximum. | 557 // One final size increase to confirm it stays capped at maximum. |
551 stub.size_ = stub.size_.Scale(1, 2); | 558 stub.size_ = stub.size_.Scale(1, 2); |
552 | 559 |
553 Manage(); | 560 Manage(); |
554 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); | 561 EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub.allocation_)); |
555 EXPECT_EQ(stub.allocation_.gpu_resource_size_in_bytes, | 562 EXPECT_EQ(stub.allocation_.gpu_resource_size_in_bytes, |
556 GpuMemoryManager::kMaximumAllocationForTabs); | 563 GetAvailableGpuMemory()); |
557 } | 564 } |
558 #endif | 565 #endif |
559 | 566 |
560 // Test GpuMemoryAllocation comparison operators: Iterate over all possible | 567 // Test GpuMemoryAllocation comparison operators: Iterate over all possible |
561 // combinations of gpu_resource_size_in_bytes, suggest_have_backbuffer, and | 568 // combinations of gpu_resource_size_in_bytes, suggest_have_backbuffer, and |
562 // suggest_have_frontbuffer, and make sure allocations with equal values test | 569 // suggest_have_frontbuffer, and make sure allocations with equal values test |
563 // equal and non equal values test not equal. | 570 // equal and non equal values test not equal. |
564 TEST_F(GpuMemoryManagerTest, GpuMemoryAllocationCompareTests) { | 571 TEST_F(GpuMemoryManagerTest, GpuMemoryAllocationCompareTests) { |
565 std::vector<int> gpu_resource_size_in_bytes_values; | 572 std::vector<int> gpu_resource_size_in_bytes_values; |
566 gpu_resource_size_in_bytes_values.push_back(0); | 573 gpu_resource_size_in_bytes_values.push_back(0); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
666 EXPECT_EQ(stats.size(), 3ul); | 673 EXPECT_EQ(stats.size(), 3ul); |
667 EXPECT_FALSE(stats[&stub1].visible); | 674 EXPECT_FALSE(stats[&stub1].visible); |
668 EXPECT_FALSE(stats[&stub2].visible); | 675 EXPECT_FALSE(stats[&stub2].visible); |
669 EXPECT_TRUE(stats[&stub3].visible); | 676 EXPECT_TRUE(stats[&stub3].visible); |
670 EXPECT_EQ(stub1allocation4, 0ul); | 677 EXPECT_EQ(stub1allocation4, 0ul); |
671 EXPECT_GE(stub2allocation4, 0ul); | 678 EXPECT_GE(stub2allocation4, 0ul); |
672 EXPECT_GT(stub3allocation4, 0ul); | 679 EXPECT_GT(stub3allocation4, 0ul); |
673 if (compositors_get_bonus_allocation) | 680 if (compositors_get_bonus_allocation) |
674 EXPECT_GT(stub3allocation4, stub3allocation3); | 681 EXPECT_GT(stub3allocation4, stub3allocation3); |
675 } | 682 } |
OLD | NEW |