OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/gpu/gpu_memory_manager.h" | |
6 | |
7 #include "content/common/gpu/gpu_memory_manager_client.h" | |
8 #include "content/common/gpu/gpu_memory_tracking.h" | |
9 #include "gpu/command_buffer/common/gpu_memory_allocation.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "ui/gfx/geometry/size_conversions.h" | |
12 | |
13 using gpu::MemoryAllocation; | |
14 | |
15 class FakeMemoryTracker : public gpu::gles2::MemoryTracker { | |
16 public: | |
17 void TrackMemoryAllocatedChange( | |
18 size_t /* old_size */, | |
19 size_t /* new_size */) override {} | |
20 bool EnsureGPUMemoryAvailable(size_t /* size_needed */) override { | |
21 return true; | |
22 } | |
23 uint64_t ClientTracingId() const override { return 0; } | |
24 int ClientId() const override { return 0; } | |
25 uint64_t ShareGroupTracingGUID() const override { return 0; } | |
26 | |
27 private: | |
28 ~FakeMemoryTracker() override {} | |
29 }; | |
30 | |
31 namespace content { | |
32 | |
33 // This class is used to collect all stub assignments during a | |
34 // Manage() call. | |
35 class ClientAssignmentCollector { | |
36 public: | |
37 struct ClientMemoryStat { | |
38 MemoryAllocation allocation; | |
39 }; | |
40 typedef base::hash_map<GpuMemoryManagerClient*, ClientMemoryStat> | |
41 ClientMemoryStatMap; | |
42 | |
43 static const ClientMemoryStatMap& GetClientStatsForLastManage() { | |
44 return client_memory_stats_for_last_manage_; | |
45 } | |
46 static void ClearAllStats() { | |
47 client_memory_stats_for_last_manage_.clear(); | |
48 } | |
49 static void AddClientStat(GpuMemoryManagerClient* client, | |
50 const MemoryAllocation& allocation) { | |
51 DCHECK(!client_memory_stats_for_last_manage_.count(client)); | |
52 client_memory_stats_for_last_manage_[client].allocation = allocation; | |
53 } | |
54 | |
55 private: | |
56 static ClientMemoryStatMap client_memory_stats_for_last_manage_; | |
57 }; | |
58 | |
59 ClientAssignmentCollector::ClientMemoryStatMap | |
60 ClientAssignmentCollector::client_memory_stats_for_last_manage_; | |
61 | |
62 class FakeClient : public GpuMemoryManagerClient { | |
63 public: | |
64 GpuMemoryManager* memmgr_; | |
65 bool suggest_have_frontbuffer_; | |
66 MemoryAllocation allocation_; | |
67 uint64 total_gpu_memory_; | |
68 gfx::Size surface_size_; | |
69 GpuMemoryManagerClient* share_group_; | |
70 scoped_refptr<gpu::gles2::MemoryTracker> memory_tracker_; | |
71 scoped_ptr<GpuMemoryTrackingGroup> tracking_group_; | |
72 scoped_ptr<GpuMemoryManagerClientState> client_state_; | |
73 | |
74 // This will create a client with no surface | |
75 FakeClient(GpuMemoryManager* memmgr, GpuMemoryManagerClient* share_group) | |
76 : memmgr_(memmgr), | |
77 suggest_have_frontbuffer_(false), | |
78 total_gpu_memory_(0), | |
79 share_group_(share_group), | |
80 memory_tracker_(NULL) { | |
81 if (!share_group_) { | |
82 memory_tracker_ = new FakeMemoryTracker(); | |
83 tracking_group_.reset( | |
84 memmgr_->CreateTrackingGroup(0, memory_tracker_.get())); | |
85 } | |
86 client_state_.reset(memmgr_->CreateClientState(this, false, true)); | |
87 } | |
88 | |
89 // This will create a client with a surface | |
90 FakeClient(GpuMemoryManager* memmgr, int32 surface_id, bool visible) | |
91 : memmgr_(memmgr), | |
92 suggest_have_frontbuffer_(false), | |
93 total_gpu_memory_(0), | |
94 share_group_(NULL), | |
95 memory_tracker_(NULL) { | |
96 memory_tracker_ = new FakeMemoryTracker(); | |
97 tracking_group_.reset( | |
98 memmgr_->CreateTrackingGroup(0, memory_tracker_.get())); | |
99 client_state_.reset( | |
100 memmgr_->CreateClientState(this, surface_id != 0, visible)); | |
101 } | |
102 | |
103 ~FakeClient() override { | |
104 client_state_.reset(); | |
105 tracking_group_.reset(); | |
106 memory_tracker_ = NULL; | |
107 } | |
108 | |
109 void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer) override { | |
110 suggest_have_frontbuffer_ = suggest_have_frontbuffer; | |
111 } | |
112 | |
113 bool GetTotalGpuMemory(uint64* bytes) override { | |
114 if (total_gpu_memory_) { | |
115 *bytes = total_gpu_memory_; | |
116 return true; | |
117 } | |
118 return false; | |
119 } | |
120 void SetTotalGpuMemory(uint64 bytes) { total_gpu_memory_ = bytes; } | |
121 | |
122 gpu::gles2::MemoryTracker* GetMemoryTracker() const override { | |
123 if (share_group_) | |
124 return share_group_->GetMemoryTracker(); | |
125 return memory_tracker_.get(); | |
126 } | |
127 | |
128 gfx::Size GetSurfaceSize() const override { return surface_size_; } | |
129 void SetSurfaceSize(gfx::Size size) { surface_size_ = size; } | |
130 | |
131 void SetVisible(bool visible) { | |
132 client_state_->SetVisible(visible); | |
133 } | |
134 | |
135 uint64 BytesWhenVisible() const { | |
136 return allocation_.bytes_limit_when_visible; | |
137 } | |
138 }; | |
139 | |
140 class GpuMemoryManagerTest : public testing::Test { | |
141 protected: | |
142 static const uint64 kFrontbufferLimitForTest = 3; | |
143 | |
144 GpuMemoryManagerTest() | |
145 : memmgr_(0, kFrontbufferLimitForTest) { | |
146 memmgr_.TestingDisableScheduleManage(); | |
147 } | |
148 | |
149 void SetUp() override {} | |
150 | |
151 static int32 GenerateUniqueSurfaceId() { | |
152 static int32 surface_id_ = 1; | |
153 return surface_id_++; | |
154 } | |
155 | |
156 bool IsAllocationForegroundForSurfaceYes( | |
157 const MemoryAllocation& alloc) { | |
158 return true; | |
159 } | |
160 bool IsAllocationBackgroundForSurfaceYes( | |
161 const MemoryAllocation& alloc) { | |
162 return true; | |
163 } | |
164 bool IsAllocationHibernatedForSurfaceYes( | |
165 const MemoryAllocation& alloc) { | |
166 return true; | |
167 } | |
168 bool IsAllocationForegroundForSurfaceNo( | |
169 const MemoryAllocation& alloc) { | |
170 return alloc.bytes_limit_when_visible != 0; | |
171 } | |
172 bool IsAllocationBackgroundForSurfaceNo( | |
173 const MemoryAllocation& alloc) { | |
174 return alloc.bytes_limit_when_visible != 0; | |
175 } | |
176 bool IsAllocationHibernatedForSurfaceNo( | |
177 const MemoryAllocation& alloc) { | |
178 return alloc.bytes_limit_when_visible == 0; | |
179 } | |
180 | |
181 void Manage() { | |
182 ClientAssignmentCollector::ClearAllStats(); | |
183 memmgr_.Manage(); | |
184 } | |
185 | |
186 GpuMemoryManager memmgr_; | |
187 }; | |
188 | |
189 } // namespace content | |
OLD | NEW |