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

Unified Diff: content/common/gpu/gpu_memory_manager_unittest.cc

Issue 9289052: Adding GpuMemoryManager to track GpuCommandBufferStub visibility and last_used_time and dictate mem… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Changing tests to use a test harness for setup, and splitting big test into several smaller. Created 8 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: content/common/gpu/gpu_memory_manager_unittest.cc
diff --git a/content/common/gpu/gpu_memory_manager_unittest.cc b/content/common/gpu/gpu_memory_manager_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..539593796df34953e0cd29af46890fb602ed55b2
--- /dev/null
+++ b/content/common/gpu/gpu_memory_manager_unittest.cc
@@ -0,0 +1,268 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/common/gpu/gpu_command_buffer_stub.h"
+#include "content/common/gpu/gpu_memory_allocation.h"
+#include "content/common/gpu/gpu_memory_manager.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+class FakeCommandBufferStub : public GpuCommandBufferStubBase {
+ public:
+ SurfaceState surface_state_;
+ std::vector<int32> affected_surface_ids_;
+ GpuMemoryAllocation allocation_;
+
+ FakeCommandBufferStub()
+ : surface_state_(0, false, base::TimeTicks()) {
+ }
+
+ FakeCommandBufferStub(int32 surface_id,
+ bool visible,
+ base::TimeTicks last_used_time)
+ : surface_state_(surface_id, visible, last_used_time) {
+ }
+
+ virtual SurfaceState* surface_state() {
+ return (surface_state_.surface_id != 0) ? &surface_state_ : NULL;
+ }
+ virtual const std::vector<int32>& affected_surface_ids() {
+ return affected_surface_ids_;
+ }
+ virtual void SendMemoryAllocationToProxy(const GpuMemoryAllocation& alloc) {
+ allocation_ = alloc;
+ }
+};
+
+class FakeClient : public GpuMemoryManagerClient {
+ public:
+ std::vector<GpuCommandBufferStubBase*> stubs_;
+
+ virtual void AppendAllCommandBufferStubs(
+ std::vector<GpuCommandBufferStubBase*>& stubs) {
+ stubs.insert(stubs.end(), stubs_.begin(), stubs_.end());
+ }
+};
+
+class GpuMemoryManagerTest : public testing::Test {
+ protected:
+ GpuMemoryManagerTest()
+ : memory_manager_(&client_) {
+ }
+
+ virtual void SetUp() {
+ first_ = base::TimeTicks::FromInternalValue(1);
nduca 2012/02/01 00:01:17 first_, second_, third_ need better names.
+ second_ = base::TimeTicks::FromInternalValue(2);
+ third_ = base::TimeTicks::FromInternalValue(3);
+
+ memory_manager_.SetMaxSurfacesWithFrontbufferSoftLimit(3);
+ }
+
+ static int32 GenerateUniqueSurfaceId() {
+ static int32 surface_id_ = 1;
+ return surface_id_++;
+ }
+
+ void Manage() {
+ memory_manager_.Manage();
+ }
+
+ base::TimeTicks first_, second_, third_;
+ FakeClient client_;
+ GpuMemoryManager memory_manager_;
+};
+
+
+// Create fake stubs with every combination of {visibilty,last_use_time}
+// and make sure they compare correctly. Only compare stubs with surfaces.
+// Expect {more visible, newer} surfaces to be more important, in that order.
+TEST_F(GpuMemoryManagerTest, ComparatorTests) {
+ FakeCommandBufferStub stub_true1(GenerateUniqueSurfaceId(), true, first_),
+ stub_true2(GenerateUniqueSurfaceId(), true, second_),
+ stub_true3(GenerateUniqueSurfaceId(), true, third_),
+ stub_false1(GenerateUniqueSurfaceId(), false, first_),
+ stub_false2(GenerateUniqueSurfaceId(), false, second_),
+ stub_false3(GenerateUniqueSurfaceId(), false, third_);
+
+ GpuMemoryManager::StubWithSurfaceComparator is_more_important;
+
+ // Should never be more important than self:
+ EXPECT_FALSE(is_more_important(&stub_true1,&stub_true1));
+ EXPECT_FALSE(is_more_important(&stub_true2,&stub_true2));
+ EXPECT_FALSE(is_more_important(&stub_true3,&stub_true3));
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_false1));
+ EXPECT_FALSE(is_more_important(&stub_false2,&stub_false2));
+ EXPECT_FALSE(is_more_important(&stub_false3,&stub_false3));
+
+ // Visible should always be more important than non visible:
+ EXPECT_TRUE(is_more_important(&stub_true1,&stub_false1));
+ EXPECT_TRUE(is_more_important(&stub_true1,&stub_false2));
+ EXPECT_TRUE(is_more_important(&stub_true1,&stub_false3));
+ EXPECT_TRUE(is_more_important(&stub_true2,&stub_false1));
+ EXPECT_TRUE(is_more_important(&stub_true2,&stub_false2));
+ EXPECT_TRUE(is_more_important(&stub_true2,&stub_false3));
+ EXPECT_TRUE(is_more_important(&stub_true3,&stub_false1));
+ EXPECT_TRUE(is_more_important(&stub_true3,&stub_false2));
+ EXPECT_TRUE(is_more_important(&stub_true3,&stub_false3));
+
+ // Not visible should never be more important than visible:
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_true1));
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_true2));
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_true3));
+ EXPECT_FALSE(is_more_important(&stub_false2,&stub_true1));
+ EXPECT_FALSE(is_more_important(&stub_false2,&stub_true2));
+ EXPECT_FALSE(is_more_important(&stub_false2,&stub_true3));
+ EXPECT_FALSE(is_more_important(&stub_false3,&stub_true1));
+ EXPECT_FALSE(is_more_important(&stub_false3,&stub_true2));
+ EXPECT_FALSE(is_more_important(&stub_false3,&stub_true3));
+
+ // Newer should always be more important than older:
+ EXPECT_TRUE(is_more_important(&stub_true2,&stub_true1));
+ EXPECT_TRUE(is_more_important(&stub_true3,&stub_true1));
+ EXPECT_TRUE(is_more_important(&stub_true3,&stub_true2));
+ EXPECT_TRUE(is_more_important(&stub_false2,&stub_false1));
+ EXPECT_TRUE(is_more_important(&stub_false3,&stub_false1));
+ EXPECT_TRUE(is_more_important(&stub_false3,&stub_false2));
+
+ // Older should never be more important than newer:
+ EXPECT_FALSE(is_more_important(&stub_true1,&stub_true2));
+ EXPECT_FALSE(is_more_important(&stub_true1,&stub_true3));
+ EXPECT_FALSE(is_more_important(&stub_true2,&stub_true3));
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_false2));
+ EXPECT_FALSE(is_more_important(&stub_false1,&stub_false3));
+ EXPECT_FALSE(is_more_important(&stub_false2,&stub_false3));
+}
+
+// Test GpuMemoryManager::Manage functionality: Basic test of stubs with
+// surfaces, and stubs without surfaces but with affected surfaces
nduca 2012/02/01 00:01:17 These two first lines dont add a lot of value.
+// Expect memory allocation to set hasFrontbuffer, hasBackbuffer according
+// to visibility and last used time.
+TEST_F(GpuMemoryManagerTest, TestManageBasicFunctionality) {
+ FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, first_),
+ stub2(GenerateUniqueSurfaceId(), false, first_);
+ client_.stubs_.push_back(&stub1);
+ client_.stubs_.push_back(&stub2);
+
+ Manage();
+ EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub1.allocation_.hasBackbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasBackbuffer, false);
+
+ FakeCommandBufferStub stub3, stub4, stub5;
+ stub3.affected_surface_ids_.push_back(stub1.surface_state()->surface_id);
+ stub4.affected_surface_ids_.push_back(stub2.surface_state()->surface_id);
+ stub5.affected_surface_ids_.push_back(stub1.surface_state()->surface_id);
+ stub5.affected_surface_ids_.push_back(stub2.surface_state()->surface_id);
+ client_.stubs_.push_back(&stub3);
+ client_.stubs_.push_back(&stub4);
+ client_.stubs_.push_back(&stub5);
+
+ Manage();
+ EXPECT_EQ(stub3.allocation_, stub1.allocation_);
+ EXPECT_EQ(stub4.allocation_, stub2.allocation_);
+ EXPECT_EQ(stub5.allocation_, stub1.allocation_);
+}
+
+// Test GpuMemoryManager::Manage functionality: Test changing visibility
+// Expect memory allocation to set hasFrontbuffer, hasBackbuffer according
+// to visibility and last used time.
+TEST_F(GpuMemoryManagerTest, TestManageChangingVisibility) {
+ FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, first_),
+ stub2(GenerateUniqueSurfaceId(), false, first_);
+ client_.stubs_.push_back(&stub1);
+ client_.stubs_.push_back(&stub2);
+
+ Manage();
+ EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub1.allocation_.hasBackbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasBackbuffer, false);
+
+ stub1.surface_state()->visible = false;
+ stub2.surface_state()->visible = true;
+
+ Manage();
+ EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub1.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasBackbuffer, true);
+}
+
+// Test GpuMemoryManager::Manage functionality: Test more than threshold number
+// of visible stubs.
+// Expect all allocations to continue to have frontbuffer.
+TEST_F(GpuMemoryManagerTest, TestManageManyVisibleStubs) {
+ FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), true, first_),
+ stub2(GenerateUniqueSurfaceId(), true, first_),
+ stub3(GenerateUniqueSurfaceId(), true, first_),
+ stub4(GenerateUniqueSurfaceId(), true, first_);
+ client_.stubs_.push_back(&stub1);
+ client_.stubs_.push_back(&stub2);
+ client_.stubs_.push_back(&stub3);
+ client_.stubs_.push_back(&stub4);
+
+ Manage();
+ EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub1.allocation_.hasBackbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasBackbuffer, true);
+ EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub3.allocation_.hasBackbuffer, true);
+ EXPECT_EQ(stub4.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub4.allocation_.hasBackbuffer, true);
+}
+
+// Test GpuMemoryManager::Manage functionality: Test more than threshold number
+// of not visible stubs.
+// Expect the stubs surpassing the threshold to not have a backbuffer.
+TEST_F(GpuMemoryManagerTest, TestManageManyNotVisibleStubs) {
+ FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), false, second_),
+ stub2(GenerateUniqueSurfaceId(), false, second_),
+ stub3(GenerateUniqueSurfaceId(), false, second_),
+ stub4(GenerateUniqueSurfaceId(), false, first_);
+ client_.stubs_.push_back(&stub1);
+ client_.stubs_.push_back(&stub2);
+ client_.stubs_.push_back(&stub3);
+ client_.stubs_.push_back(&stub4);
+
+ Manage();
+ EXPECT_EQ(stub1.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub1.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub2.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub2.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub3.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub4.allocation_.hasFrontbuffer, false);
+ EXPECT_EQ(stub4.allocation_.hasBackbuffer, false);
+}
+
+// Test GpuMemoryManager::Manage functionality: Test changing the last used
+// time of stubs when doing so causes change in which stubs surpass threshold.
+// Expect frontbuffer to be dropped for the older stub.
+TEST_F(GpuMemoryManagerTest, TestManageChangingLastUsedTime) {
+ FakeCommandBufferStub stub1(GenerateUniqueSurfaceId(), false, second_),
+ stub2(GenerateUniqueSurfaceId(), false, second_),
+ stub3(GenerateUniqueSurfaceId(), false, second_),
+ stub4(GenerateUniqueSurfaceId(), false, first_);
+ client_.stubs_.push_back(&stub1);
+ client_.stubs_.push_back(&stub2);
+ client_.stubs_.push_back(&stub3);
+ client_.stubs_.push_back(&stub4);
+
+ Manage();
+ EXPECT_EQ(stub3.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub3.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub4.allocation_.hasFrontbuffer, false);
+ EXPECT_EQ(stub4.allocation_.hasBackbuffer, false);
+
+ stub3.surface_state()->last_used_time = first_;
+ stub4.surface_state()->last_used_time = second_;
+
+ Manage();
+ EXPECT_EQ(stub3.allocation_.hasFrontbuffer, false);
+ EXPECT_EQ(stub3.allocation_.hasBackbuffer, false);
+ EXPECT_EQ(stub4.allocation_.hasFrontbuffer, true);
+ EXPECT_EQ(stub4.allocation_.hasBackbuffer, false);
+}

Powered by Google App Engine
This is Rietveld 408576698