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 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
17 #include "content/public/common/gpu_memory_stats.h" | 17 #include "content/public/common/gpu_memory_stats.h" |
18 #include "gpu/command_buffer/common/gpu_memory_allocation.h" | 18 #include "gpu/command_buffer/common/gpu_memory_allocation.h" |
19 #include "gpu/command_buffer/service/memory_tracking.h" | 19 #include "gpu/command_buffer/service/memory_tracking.h" |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 class GpuChannelManager; | 23 class GpuChannelManager; |
24 class GpuMemoryManagerClient; | |
25 class GpuMemoryManagerClientState; | |
26 class GpuMemoryTrackingGroup; | 24 class GpuMemoryTrackingGroup; |
27 | 25 |
28 class CONTENT_EXPORT GpuMemoryManager : | 26 class CONTENT_EXPORT GpuMemoryManager : |
29 public base::SupportsWeakPtr<GpuMemoryManager> { | 27 public base::SupportsWeakPtr<GpuMemoryManager> { |
30 public: | 28 public: |
31 #if defined(OS_ANDROID) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) | 29 explicit GpuMemoryManager(GpuChannelManager* channel_manager); |
32 enum { kDefaultMaxSurfacesWithFrontbufferSoftLimit = 1 }; | |
33 #else | |
34 enum { kDefaultMaxSurfacesWithFrontbufferSoftLimit = 8 }; | |
35 #endif | |
36 enum ScheduleManageTime { | |
37 // Add a call to Manage to the thread's message loop immediately. | |
38 kScheduleManageNow, | |
39 // Add a Manage call to the thread's message loop for execution 1/60th of | |
40 // of a second from now. | |
41 kScheduleManageLater, | |
42 }; | |
43 | |
44 GpuMemoryManager(GpuChannelManager* channel_manager, | |
45 uint64 max_surfaces_with_frontbuffer_soft_limit); | |
46 ~GpuMemoryManager(); | 30 ~GpuMemoryManager(); |
47 | 31 |
48 // Schedule a Manage() call. If immediate is true, we PostTask without delay. | |
49 // Otherwise PostDelayedTask using a CancelableClosure and allow multiple | |
50 // delayed calls to "queue" up. This way, we do not spam clients in certain | |
51 // lower priority situations. An immediate schedule manage will cancel any | |
52 // queued delayed manage. | |
53 void ScheduleManage(ScheduleManageTime schedule_manage_time); | |
54 | |
55 // Retrieve GPU Resource consumption statistics for the task manager | 32 // Retrieve GPU Resource consumption statistics for the task manager |
56 void GetVideoMemoryUsageStats( | 33 void GetVideoMemoryUsageStats( |
57 content::GPUVideoMemoryUsageStats* video_memory_usage_stats) const; | 34 content::GPUVideoMemoryUsageStats* video_memory_usage_stats) const; |
58 | 35 |
59 GpuMemoryManagerClientState* CreateClientState( | |
60 GpuMemoryManagerClient* client, bool has_surface, bool visible); | |
61 | |
62 GpuMemoryTrackingGroup* CreateTrackingGroup( | 36 GpuMemoryTrackingGroup* CreateTrackingGroup( |
63 base::ProcessId pid, gpu::gles2::MemoryTracker* memory_tracker); | 37 base::ProcessId pid, gpu::gles2::MemoryTracker* memory_tracker); |
64 | 38 |
65 uint64 GetTrackerMemoryUsage(gpu::gles2::MemoryTracker* tracker) const; | 39 uint64 GetTrackerMemoryUsage(gpu::gles2::MemoryTracker* tracker) const; |
66 uint64 GetMaximumClientAllocation() const { | |
67 return client_hard_limit_bytes_; | |
68 } | |
69 | 40 |
70 private: | 41 private: |
71 friend class GpuMemoryManagerTest; | 42 friend class GpuMemoryManagerTest; |
72 friend class GpuMemoryTrackingGroup; | 43 friend class GpuMemoryTrackingGroup; |
73 friend class GpuMemoryManagerClientState; | 44 friend class GpuMemoryManagerClientState; |
74 | 45 |
75 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
76 TestManageBasicFunctionality); | |
77 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
78 TestManageChangingVisibility); | |
79 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
80 TestManageManyVisibleStubs); | |
81 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
82 TestManageManyNotVisibleStubs); | |
83 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
84 TestManageChangingLastUsedTime); | |
85 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
86 TestManageChangingImportanceShareGroup); | |
87 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
88 TestForegroundStubsGetBonusAllocation); | |
89 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
90 TestUpdateAvailableGpuMemory); | |
91 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
92 GpuMemoryAllocationCompareTests); | |
93 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
94 StubMemoryStatsForLastManageTests); | |
95 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
96 TestManagedUsageTracking); | |
97 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
98 BackgroundMru); | |
99 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
100 AllowNonvisibleMemory); | |
101 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
102 BackgroundDiscardPersistent); | |
103 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
104 UnmanagedTracking); | |
105 FRIEND_TEST_ALL_PREFIXES(GpuMemoryManagerTest, | |
106 DefaultAllocation); | |
107 | |
108 typedef std::map<gpu::gles2::MemoryTracker*, GpuMemoryTrackingGroup*> | 46 typedef std::map<gpu::gles2::MemoryTracker*, GpuMemoryTrackingGroup*> |
109 TrackingGroupMap; | 47 TrackingGroupMap; |
110 | 48 |
111 typedef std::list<GpuMemoryManagerClientState*> ClientStateList; | |
112 | |
113 void Manage(); | |
114 void SetClientsHibernatedState() const; | |
115 | |
116 // Send memory usage stats to the browser process. | 49 // Send memory usage stats to the browser process. |
117 void SendUmaStatsToBrowser(); | 50 void SendUmaStatsToBrowser(); |
118 | 51 |
119 // Get the current number of bytes allocated. | 52 // Get the current number of bytes allocated. |
120 uint64 GetCurrentUsage() const { | 53 uint64 GetCurrentUsage() const { |
121 return bytes_allocated_current_; | 54 return bytes_allocated_current_; |
122 } | 55 } |
123 | 56 |
124 // GpuMemoryTrackingGroup interface | 57 // GpuMemoryTrackingGroup interface |
125 void TrackMemoryAllocatedChange( | 58 void TrackMemoryAllocatedChange( |
126 GpuMemoryTrackingGroup* tracking_group, | 59 GpuMemoryTrackingGroup* tracking_group, |
127 uint64 old_size, | 60 uint64 old_size, |
128 uint64 new_size); | 61 uint64 new_size); |
129 void OnDestroyTrackingGroup(GpuMemoryTrackingGroup* tracking_group); | 62 void OnDestroyTrackingGroup(GpuMemoryTrackingGroup* tracking_group); |
130 bool EnsureGPUMemoryAvailable(uint64 size_needed); | 63 bool EnsureGPUMemoryAvailable(uint64 size_needed); |
131 | 64 |
132 // GpuMemoryManagerClientState interface | |
133 void SetClientStateVisible( | |
134 GpuMemoryManagerClientState* client_state, bool visible); | |
135 void OnDestroyClientState(GpuMemoryManagerClientState* client); | |
136 | |
137 // Add or remove a client from its clients list (visible, nonvisible, or | |
138 // nonsurface). When adding the client, add it to the front of the list. | |
139 void AddClientToList(GpuMemoryManagerClientState* client_state); | |
140 void RemoveClientFromList(GpuMemoryManagerClientState* client_state); | |
141 ClientStateList* GetClientList(GpuMemoryManagerClientState* client_state); | |
142 | |
143 // Interfaces for testing | |
144 void TestingDisableScheduleManage() { disable_schedule_manage_ = true; } | |
145 | |
146 GpuChannelManager* channel_manager_; | 65 GpuChannelManager* channel_manager_; |
147 | 66 |
148 // A list of all visible and nonvisible clients, in most-recently-used | |
149 // order (most recently used is first). | |
150 ClientStateList clients_visible_mru_; | |
151 ClientStateList clients_nonvisible_mru_; | |
152 | |
153 // A list of all clients that don't have a surface. | |
154 ClientStateList clients_nonsurface_; | |
155 | |
156 // All context groups' tracking structures | 67 // All context groups' tracking structures |
157 TrackingGroupMap tracking_groups_; | 68 TrackingGroupMap tracking_groups_; |
158 | 69 |
159 base::CancelableClosure delayed_manage_callback_; | |
160 bool manage_immediate_scheduled_; | |
161 bool disable_schedule_manage_; | |
162 | |
163 uint64 max_surfaces_with_frontbuffer_soft_limit_; | |
164 | |
165 // The maximum amount of memory that may be allocated for a single client. | |
166 uint64 client_hard_limit_bytes_; | |
167 | |
168 // The current total memory usage, and historical maximum memory usage | 70 // The current total memory usage, and historical maximum memory usage |
169 uint64 bytes_allocated_current_; | 71 uint64 bytes_allocated_current_; |
170 uint64 bytes_allocated_historical_max_; | 72 uint64 bytes_allocated_historical_max_; |
171 | 73 |
172 DISALLOW_COPY_AND_ASSIGN(GpuMemoryManager); | 74 DISALLOW_COPY_AND_ASSIGN(GpuMemoryManager); |
173 }; | 75 }; |
174 | 76 |
175 } // namespace content | 77 } // namespace content |
176 | 78 |
177 #endif // CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ | 79 #endif // CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_ |
OLD | NEW |