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 #ifndef CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ | 5 #ifndef CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ |
6 #define CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ | 6 #define CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ |
7 | 7 |
8 #if defined(ENABLE_GPU) | 8 #if defined(ENABLE_GPU) |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
| 11 #include <set> |
11 | 12 |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "base/cancelable_callback.h" | 14 #include "base/cancelable_callback.h" |
14 #include "base/hash_tables.h" | 15 #include "base/hash_tables.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/process.h" |
16 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
17 #include "content/common/gpu/gpu_memory_allocation.h" | 19 #include "content/common/gpu/gpu_memory_allocation.h" |
| 20 #include "content/public/common/gpu_info.h" |
18 #include "ui/gfx/size.h" | 21 #include "ui/gfx/size.h" |
19 | 22 |
20 class GpuCommandBufferStubBase; | 23 class GpuCommandBufferStubBase; |
| 24 class GpuMemoryTrackingGroup; |
21 | 25 |
22 #if defined(COMPILER_GCC) | 26 #if defined(COMPILER_GCC) |
23 namespace BASE_HASH_NAMESPACE { | 27 namespace BASE_HASH_NAMESPACE { |
24 template<> | 28 template<> |
25 struct hash<GpuCommandBufferStubBase*> { | 29 struct hash<GpuCommandBufferStubBase*> { |
26 size_t operator()(GpuCommandBufferStubBase* ptr) const { | 30 size_t operator()(GpuCommandBufferStubBase* ptr) const { |
27 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 31 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
28 } | 32 } |
29 }; | 33 }; |
30 } // namespace BASE_HASH_NAMESPACE | 34 } // namespace BASE_HASH_NAMESPACE |
(...skipping 25 matching lines...) Expand all Loading... |
56 size_t max_surfaces_with_frontbuffer_soft_limit); | 60 size_t max_surfaces_with_frontbuffer_soft_limit); |
57 ~GpuMemoryManager(); | 61 ~GpuMemoryManager(); |
58 | 62 |
59 // Schedule a Manage() call. If immediate is true, we PostTask without delay. | 63 // Schedule a Manage() call. If immediate is true, we PostTask without delay. |
60 // Otherwise PostDelayedTask using a CancelableClosure and allow multiple | 64 // Otherwise PostDelayedTask using a CancelableClosure and allow multiple |
61 // delayed calls to "queue" up. This way, we do not spam clients in certain | 65 // delayed calls to "queue" up. This way, we do not spam clients in certain |
62 // lower priority situations. An immediate schedule manage will cancel any | 66 // lower priority situations. An immediate schedule manage will cancel any |
63 // queued delayed manage. | 67 // queued delayed manage. |
64 void ScheduleManage(bool immediate); | 68 void ScheduleManage(bool immediate); |
65 | 69 |
| 70 // Retrieve GPU Resource consumption statistics for the task manager |
| 71 void GetVidmem(content::GPUVidmem& vidmem) const; |
| 72 |
| 73 // Add and remove structures to track context groups' memory consumption |
| 74 void AddTrackingGroup(GpuMemoryTrackingGroup* tracking_group); |
| 75 void RemoveTrackingGroup(GpuMemoryTrackingGroup* tracking_group); |
| 76 |
66 // Returns StubMemoryStat's for each GpuCommandBufferStubBase, which were | 77 // Returns StubMemoryStat's for each GpuCommandBufferStubBase, which were |
67 // assigned during the most recent call to Manage(). | 78 // assigned during the most recent call to Manage(). |
68 // Useful for tracking the memory-allocation-related presumed state of the | 79 // Useful for tracking the memory-allocation-related presumed state of the |
69 // system, as seen by GpuMemoryManager. | 80 // system, as seen by GpuMemoryManager. |
70 typedef base::hash_map<GpuCommandBufferStubBase*, StubMemoryStat> | 81 typedef base::hash_map<GpuCommandBufferStubBase*, StubMemoryStat> |
71 StubMemoryStatMap; | 82 StubMemoryStatMap; |
72 const StubMemoryStatMap& stub_memory_stats_for_last_manage() const { | 83 const StubMemoryStatMap& stub_memory_stats_for_last_manage() const { |
73 return stub_memory_stats_for_last_manage_; | 84 return stub_memory_stats_for_last_manage_; |
74 } | 85 } |
75 | 86 |
76 // Track a change in memory allocated by any context | 87 // Track a change in memory allocated by any context |
77 void TrackMemoryAllocatedChange(size_t old_size, size_t new_size); | 88 void TrackMemoryAllocatedChange(size_t old_size, size_t new_size); |
78 | 89 |
79 private: | 90 private: |
80 friend class GpuMemoryManagerTest; | 91 friend class GpuMemoryManagerTest; |
81 | 92 |
82 void Manage(); | 93 void Manage(); |
83 | 94 |
| 95 // The context groups' tracking structures |
| 96 std::set<GpuMemoryTrackingGroup*> tracking_groups_; |
| 97 |
84 size_t CalculateBonusMemoryAllocationBasedOnSize(gfx::Size size) const; | 98 size_t CalculateBonusMemoryAllocationBasedOnSize(gfx::Size size) const; |
85 | 99 |
86 size_t GetAvailableGpuMemory() const { | 100 size_t GetAvailableGpuMemory() const { |
87 return bytes_available_gpu_memory_; | 101 return bytes_available_gpu_memory_; |
88 } | 102 } |
89 | 103 |
90 // The minimum non-zero amount of memory that a tab may be assigned | 104 // The minimum non-zero amount of memory that a tab may be assigned |
91 size_t GetMinimumTabAllocation() const { | 105 size_t GetMinimumTabAllocation() const { |
92 #if defined(OS_ANDROID) | 106 #if defined(OS_ANDROID) |
93 return 32 * 1024 * 1024; | 107 return 32 * 1024 * 1024; |
(...skipping 20 matching lines...) Expand all Loading... |
114 // The maximum amount of memory that may be allocated for GPU resources | 128 // The maximum amount of memory that may be allocated for GPU resources |
115 size_t bytes_available_gpu_memory_; | 129 size_t bytes_available_gpu_memory_; |
116 | 130 |
117 // The current total memory usage, and historical maximum memory usage | 131 // The current total memory usage, and historical maximum memory usage |
118 size_t bytes_allocated_current_; | 132 size_t bytes_allocated_current_; |
119 size_t bytes_allocated_historical_max_; | 133 size_t bytes_allocated_historical_max_; |
120 | 134 |
121 DISALLOW_COPY_AND_ASSIGN(GpuMemoryManager); | 135 DISALLOW_COPY_AND_ASSIGN(GpuMemoryManager); |
122 }; | 136 }; |
123 | 137 |
| 138 // All decoders in a context group point to a single GpuMemoryTrackingGroup, |
| 139 // which tracks GPU resource consumption for the entire context group. |
| 140 class GpuMemoryTrackingGroup { |
| 141 public: |
| 142 GpuMemoryTrackingGroup( |
| 143 base::ProcessId pid, GpuMemoryManager* memory_manager) : |
| 144 pid_(pid), |
| 145 size_(0), |
| 146 memory_manager_(memory_manager) { |
| 147 memory_manager_->AddTrackingGroup(this); |
| 148 } |
| 149 ~GpuMemoryTrackingGroup() { |
| 150 memory_manager_->RemoveTrackingGroup(this); |
| 151 } |
| 152 void TrackMemoryAllocatedChange(size_t old_size, size_t new_size) { |
| 153 if (old_size < new_size) { |
| 154 size_t delta = new_size - old_size; |
| 155 size_ += delta; |
| 156 } |
| 157 if (new_size < old_size) { |
| 158 size_t delta = old_size - new_size; |
| 159 DCHECK(size_ >= delta); |
| 160 size_ -= delta; |
| 161 } |
| 162 memory_manager_->TrackMemoryAllocatedChange(old_size, new_size); |
| 163 } |
| 164 base::ProcessId GetPid() const { |
| 165 return pid_; |
| 166 } |
| 167 size_t GetSize() const { |
| 168 return size_; |
| 169 } |
| 170 |
| 171 private: |
| 172 base::ProcessId pid_; |
| 173 size_t size_; |
| 174 GpuMemoryManager* memory_manager_; |
| 175 }; |
| 176 |
124 #endif | 177 #endif |
125 | 178 |
126 #endif // CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ | 179 #endif // CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ |
OLD | NEW |