Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "content/common/gpu/gpu_command_buffer_stub.h" | |
| 6 #include "content/common/gpu/gpu_memory_allocation.h" | |
| 7 #include "content/common/gpu/gpu_memory_manager.h" | |
| 8 | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 class FakeCommandBufferStub : public GpuCommandBufferStubBase { | |
| 12 public: | |
| 13 SurfaceState surface_state_; | |
| 14 std::vector<int32> affected_surface_ids_; | |
| 15 GpuMemoryAllocation allocation_; | |
| 16 | |
| 17 FakeCommandBufferStub() | |
| 18 : surface_state_(0, false, base::TimeTicks()) { | |
| 19 } | |
| 20 | |
| 21 FakeCommandBufferStub(int32 surface_id, | |
| 22 bool visible, | |
| 23 base::TimeTicks last_used_time) | |
| 24 : surface_state_(surface_id, visible, last_used_time) { | |
| 25 } | |
| 26 | |
| 27 virtual SurfaceState* surface_state() { | |
| 28 return (surface_state_.surface_id != 0) ? &surface_state_ : NULL; | |
| 29 } | |
| 30 virtual const std::vector<int32>& affected_surface_ids() { | |
| 31 return affected_surface_ids_; | |
| 32 } | |
| 33 virtual void SendMemoryAllocationToProxy(const GpuMemoryAllocation& alloc) { | |
| 34 allocation_ = alloc; | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 class FakeClient : public GpuMemoryManagerClient { | |
| 39 public: | |
| 40 std::vector<GpuCommandBufferStubBase*> stubs_; | |
| 41 | |
| 42 virtual void AppendAllCommandBufferStubs( | |
| 43 std::vector<GpuCommandBufferStubBase*>& stubs) { | |
| 44 stubs.insert(stubs.end(), stubs_.begin(), stubs_.end()); | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 class GpuMemoryManagerTest : public testing::Test { | |
| 49 protected: | |
| 50 static const size_t kFrontbufferLimitForTest = 3; | |
| 51 | |
| 52 GpuMemoryManagerTest() | |
| 53 : memory_manager_(&client_, kFrontbufferLimitForTest) { | |
| 54 } | |
| 55 | |
| 56 virtual void SetUp() { | |
| 57 older_ = base::TimeTicks::FromInternalValue(1); | |
| 58 newer_ = base::TimeTicks::FromInternalValue(2); | |
| 59 newest_ = base::TimeTicks::FromInternalValue(3); | |
| 60 } | |
| 61 | |
| 62 static int32 GenerateUniqueSurfaceId() { | |
| 63 static int32 surface_id_ = 1; | |
| 64 return surface_id_++; | |
| 65 } | |
| 66 | |
| 67 void Manage() { | |
| 68 memory_manager_.Manage(); | |
| 69 } | |
| 70 | |
| 71 base::TimeTicks older_, newer_, newest_; | |
| 72 FakeClient client_; | |
| 73 GpuMemoryManager memory_manager_; | |
| 74 }; | |
| 75 | |
| 76 | |
| 77 // Create fake stubs with every combination of {visibilty,last_use_time} | |
| 78 // and make sure they compare correctly. Only compare stubs with surfaces. | |
| 79 // Expect {more visible, newer} surfaces to be more important, in that order. | |
| 80 TEST_F(GpuMemoryManagerTest, ComparatorTests) { | |
| 81 FakeCommandBufferStub stub_true1(GenerateUniqueSurfaceId(), true, older_), | |
| 82 stub_true2(GenerateUniqueSurfaceId(), true, newer_), | |
| 83 stub_true3(GenerateUniqueSurfaceId(), true, newest_), | |
| 84 stub_false1(GenerateUniqueSurfaceId(), false, older_), | |
| 85 stub_false2(GenerateUniqueSurfaceId(), false, newer_), | |
| 86 stub_false3(GenerateUniqueSurfaceId(), false, newest_); | |
| 87 | |
| 88 GpuMemoryManager::StubWithSurfaceComparator is_more_important; | |
| 89 | |
| 90 // Should never be more important than self: | |
| 91 EXPECT_FALSE(is_more_important(&stub_true1,&stub_true1)); | |
|
jonathan.backer
2012/02/01 19:43:17
need space after comma
mmocny
2012/02/01 20:44:19
Done.
| |
| 92 EXPECT_FALSE(is_more_important(&stub_true2,&stub_true2)); | |
| 93 EXPECT_FALSE(is_more_important(&stub_true3,&stub_true3)); | |
| 94 EXPECT_FALSE(is_more_important(&stub_false1,&stub_false1)); | |
| 95 EXPECT_FALSE(is_more_important(&stub_false2,&stub_false2)); | |
| 96 EXPECT_FALSE(is_more_important(&stub_false3,&stub_false3)); | |
| 97 | |
| 98 // Visible should always be more important than non visible: | |
| 99 EXPECT_TRUE(is_more_important(&stub_true1,&stub_false1)); | |
| 100 EXPECT_TRUE(is_more_important(&stub_true1,&stub_false2)); | |
| 101 EXPECT_TRUE(is_more_important(&stub_true1,&stub_false3)); | |
| 102 EXPECT_TRUE(is_more_important(&stub_true2,&stub_false1)); | |
| 103 EXPECT_TRUE(is_more_important(&stub_true2,&stub_false2)); | |
| 104 EXPECT_TRUE(is_more_important(&stub_true2,&stub_false3)); | |
| 105 EXPECT_TRUE(is_more_important(&stub_true3,&stub_false1)); | |
| 106 EXPECT_TRUE(is_more_important(&stub_true3,&stub_false2)); | |
| 107 EXPECT_TRUE(is_more_important(&stub_true3,&stub_false3)); | |
| 108 | |
| 109 // Not visible should never be more important than visible: | |
| 110 EXPECT_FALSE(is_more_important(&stub_false1,&stub_true1)); | |
| 111 EXPECT_FALSE(is_more_important(&stub_false1,&stub_true2)); | |
| 112 EXPECT_FALSE(is_more_important(&stub_false1,&stub_true3)); | |
| 113 EXPECT_FALSE(is_more_important(&stub_false2,&stub_true1)); | |
| 114 EXPECT_FALSE(is_more_important(&stub_false2,&stub_true2)); | |
| 115 EXPECT_FALSE(is_more_important(&stub_false2,&stub_true3)); | |
| 116 EXPECT_FALSE(is_more_important(&stub_false3,&stub_true1)); | |
| 117 EXPECT_FALSE(is_more_important(&stub_false3,&stub_true2)); | |
| 118 EXPECT_FALSE(is_more_important(&stub_false3,&stub_true3)); | |
| 119 | |
| 120 // Newer should always be more important than older: | |
| 121 EXPECT_TRUE(is_more_important(&stub_true2,&stub_true1)); | |
| 122 EXPECT_TRUE(is_more_important(&stub_true3,&stub_true1)); | |
| 123 EXPECT_TRUE(is_more_important(&stub_true3,&stub_true2)); | |
| 124 EXPECT_TRUE(is_more_important(&stub_false2,&stub_false1)); | |
| 125 EXPECT_TRUE(is_more_important(&stub_false3,&stub_false1)); | |
| 126 EXPECT_TRUE(is_more_important(&stub_false3,&stub_false2)); | |
| 127 | |
| 128 // Older should never be more important than newer: | |
| 129 EXPECT_FALSE(is_more_important(&stub_true1,&stub_true2)); | |
| 130 EXPECT_FALSE(is_more_important(&stub_true1,&stub_true3)); | |
| 131 EXPECT_FALSE(is_more_important(&stub_true2,&stub_true3)); | |
| 132 EXPECT_FALSE(is_more_important(&stub_false1,&stub_false2)); | |
| 133 EXPECT_FALSE(is_more_important(&stub_false1,&stub_false3)); | |
| 134 EXPECT_FALSE(is_more_important(&stub_false2,&stub_false3)); | |
| 135 } | |
| 136 | |
| 137 // Test GpuMemoryManager::Manage basic functionality. | |
| 138 // Expect memory allocation to set hasFrontbuffer, hasBackbuffer according | |
| 139 // to visibility and last used time. | |
| 140 TEST_F(GpuMemoryManagerTest, TestManageBasicFunctionality) { | |
| 141 FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, older_), | |
| 142 stub2(GenerateUniqueSurfaceId(), false, older_); | |
| 143 client_.stubs_.push_back(&stub1); | |
| 144 client_.stubs_.push_back(&stub2); | |
| 145 | |
| 146 Manage(); | |
| 147 EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true); | |
| 148 EXPECT_EQ(stub1.allocation_.hasBackbuffer, true); | |
| 149 EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true); | |
| 150 EXPECT_EQ(stub2.allocation_.hasBackbuffer, false); | |
| 151 | |
| 152 FakeCommandBufferStub stub3, stub4, stub5; | |
| 153 stub3.affected_surface_ids_.push_back(stub1.surface_state()->surface_id); | |
| 154 stub4.affected_surface_ids_.push_back(stub2.surface_state()->surface_id); | |
| 155 stub5.affected_surface_ids_.push_back(stub1.surface_state()->surface_id); | |
| 156 stub5.affected_surface_ids_.push_back(stub2.surface_state()->surface_id); | |
| 157 client_.stubs_.push_back(&stub3); | |
| 158 client_.stubs_.push_back(&stub4); | |
| 159 client_.stubs_.push_back(&stub5); | |
| 160 | |
| 161 Manage(); | |
| 162 EXPECT_EQ(stub3.allocation_, stub1.allocation_); | |
| 163 EXPECT_EQ(stub4.allocation_, stub2.allocation_); | |
| 164 EXPECT_EQ(stub5.allocation_, stub1.allocation_); | |
| 165 } | |
| 166 | |
| 167 // Test GpuMemoryManager::Manage functionality: Test changing visibility | |
| 168 // Expect memory allocation to set hasFrontbuffer, hasBackbuffer according | |
| 169 // to visibility and last used time. | |
| 170 TEST_F(GpuMemoryManagerTest, TestManageChangingVisibility) { | |
| 171 FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, older_), | |
| 172 stub2(GenerateUniqueSurfaceId(), false, older_); | |
| 173 client_.stubs_.push_back(&stub1); | |
| 174 client_.stubs_.push_back(&stub2); | |
| 175 | |
| 176 Manage(); | |
| 177 EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true); | |
| 178 EXPECT_EQ(stub1.allocation_.hasBackbuffer, true); | |
| 179 EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true); | |
| 180 EXPECT_EQ(stub2.allocation_.hasBackbuffer, false); | |
| 181 | |
| 182 stub1.surface_state()->visible = false; | |
| 183 stub2.surface_state()->visible = true; | |
| 184 | |
| 185 Manage(); | |
| 186 EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true); | |
| 187 EXPECT_EQ(stub1.allocation_.hasBackbuffer, false); | |
| 188 EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true); | |
| 189 EXPECT_EQ(stub2.allocation_.hasBackbuffer, true); | |
| 190 } | |
| 191 | |
| 192 // Test GpuMemoryManager::Manage functionality: Test more than threshold number | |
| 193 // of visible stubs. | |
| 194 // Expect all allocations to continue to have frontbuffer. | |
| 195 TEST_F(GpuMemoryManagerTest, TestManageManyVisibleStubs) { | |
| 196 FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, older_), | |
| 197 stub2(GenerateUniqueSurfaceId(), true, older_), | |
| 198 stub3(GenerateUniqueSurfaceId(), true, older_), | |
| 199 stub4(GenerateUniqueSurfaceId(), true, older_); | |
| 200 client_.stubs_.push_back(&stub1); | |
| 201 client_.stubs_.push_back(&stub2); | |
| 202 client_.stubs_.push_back(&stub3); | |
| 203 client_.stubs_.push_back(&stub4); | |
| 204 | |
| 205 Manage(); | |
| 206 EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true); | |
| 207 EXPECT_EQ(stub1.allocation_.hasBackbuffer, true); | |
| 208 EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true); | |
| 209 EXPECT_EQ(stub2.allocation_.hasBackbuffer, true); | |
| 210 EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true); | |
| 211 EXPECT_EQ(stub3.allocation_.hasBackbuffer, true); | |
| 212 EXPECT_EQ(stub4.allocation_.hasFrontbuffer, true); | |
| 213 EXPECT_EQ(stub4.allocation_.hasBackbuffer, true); | |
| 214 } | |
| 215 | |
| 216 // Test GpuMemoryManager::Manage functionality: Test more than threshold number | |
| 217 // of not visible stubs. | |
| 218 // Expect the stubs surpassing the threshold to not have a backbuffer. | |
| 219 TEST_F(GpuMemoryManagerTest, TestManageManyNotVisibleStubs) { | |
| 220 FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), false, newer_), | |
| 221 stub2(GenerateUniqueSurfaceId(), false, newer_), | |
| 222 stub3(GenerateUniqueSurfaceId(), false, newer_), | |
| 223 stub4(GenerateUniqueSurfaceId(), false, older_); | |
| 224 client_.stubs_.push_back(&stub1); | |
| 225 client_.stubs_.push_back(&stub2); | |
| 226 client_.stubs_.push_back(&stub3); | |
| 227 client_.stubs_.push_back(&stub4); | |
| 228 | |
| 229 Manage(); | |
| 230 EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true); | |
| 231 EXPECT_EQ(stub1.allocation_.hasBackbuffer, false); | |
| 232 EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true); | |
| 233 EXPECT_EQ(stub2.allocation_.hasBackbuffer, false); | |
| 234 EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true); | |
| 235 EXPECT_EQ(stub3.allocation_.hasBackbuffer, false); | |
| 236 EXPECT_EQ(stub4.allocation_.hasFrontbuffer, false); | |
| 237 EXPECT_EQ(stub4.allocation_.hasBackbuffer, false); | |
| 238 } | |
| 239 | |
| 240 // Test GpuMemoryManager::Manage functionality: Test changing the last used | |
| 241 // time of stubs when doing so causes change in which stubs surpass threshold. | |
| 242 // Expect frontbuffer to be dropped for the older stub. | |
| 243 TEST_F(GpuMemoryManagerTest, TestManageChangingLastUsedTime) { | |
| 244 FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), false, newer_), | |
| 245 stub2(GenerateUniqueSurfaceId(), false, newer_), | |
| 246 stub3(GenerateUniqueSurfaceId(), false, newer_), | |
| 247 stub4(GenerateUniqueSurfaceId(), false, older_); | |
| 248 client_.stubs_.push_back(&stub1); | |
| 249 client_.stubs_.push_back(&stub2); | |
| 250 client_.stubs_.push_back(&stub3); | |
| 251 client_.stubs_.push_back(&stub4); | |
| 252 | |
| 253 Manage(); | |
| 254 EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true); | |
| 255 EXPECT_EQ(stub3.allocation_.hasBackbuffer, false); | |
| 256 EXPECT_EQ(stub4.allocation_.hasFrontbuffer, false); | |
| 257 EXPECT_EQ(stub4.allocation_.hasBackbuffer, false); | |
| 258 | |
| 259 stub3.surface_state()->last_used_time = older_; | |
| 260 stub4.surface_state()->last_used_time = newer_; | |
| 261 | |
| 262 Manage(); | |
| 263 EXPECT_EQ(stub3.allocation_.hasFrontbuffer, false); | |
| 264 EXPECT_EQ(stub3.allocation_.hasBackbuffer, false); | |
| 265 EXPECT_EQ(stub4.allocation_.hasFrontbuffer, true); | |
| 266 EXPECT_EQ(stub4.allocation_.hasBackbuffer, false); | |
| 267 } | |
| OLD | NEW |