1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/renderer_frame_manager.h"
10 #include "base/logging.h"
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/memory/memory_pressure_monitor.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/sys_info.h"
15 #include "content/common/host_shared_bitmap_manager.h"
20 const int kModeratePressurePercentage
= 50;
21 const int kCriticalPressurePercentage
= 10;
25 RendererFrameManager
* RendererFrameManager::GetInstance() {
26 return Singleton
<RendererFrameManager
>::get();
29 void RendererFrameManager::AddFrame(RendererFrameManagerClient
* frame
,
33 locked_frames_
[frame
] = 1;
35 unlocked_frames_
.push_front(frame
);
36 CullUnlockedFrames(GetMaxNumberOfSavedFrames());
39 void RendererFrameManager::RemoveFrame(RendererFrameManagerClient
* frame
) {
40 std::map
<RendererFrameManagerClient
*, size_t>::iterator locked_iter
=
41 locked_frames_
.find(frame
);
42 if (locked_iter
!= locked_frames_
.end())
43 locked_frames_
.erase(locked_iter
);
44 unlocked_frames_
.remove(frame
);
47 void RendererFrameManager::LockFrame(RendererFrameManagerClient
* frame
) {
48 std::list
<RendererFrameManagerClient
*>::iterator unlocked_iter
=
49 std::find(unlocked_frames_
.begin(), unlocked_frames_
.end(), frame
);
50 if (unlocked_iter
!= unlocked_frames_
.end()) {
51 DCHECK(locked_frames_
.find(frame
) == locked_frames_
.end());
52 unlocked_frames_
.remove(frame
);
53 locked_frames_
[frame
] = 1;
55 DCHECK(locked_frames_
.find(frame
) != locked_frames_
.end());
56 locked_frames_
[frame
]++;
60 void RendererFrameManager::UnlockFrame(RendererFrameManagerClient
* frame
) {
61 DCHECK(locked_frames_
.find(frame
) != locked_frames_
.end());
62 size_t locked_count
= locked_frames_
[frame
];
64 if (locked_count
> 1) {
65 locked_frames_
[frame
]--;
68 unlocked_frames_
.push_front(frame
);
69 CullUnlockedFrames(GetMaxNumberOfSavedFrames());
73 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const {
74 base::MemoryPressureMonitor
* monitor
= base::MemoryPressureMonitor::Get();
77 return max_number_of_saved_frames_
;
79 // Until we have a global OnMemoryPressureChanged event we need to query the
80 // value from our specific pressure monitor.
82 switch (monitor
->GetCurrentPressureLevel()) {
83 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
:
86 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
:
87 percentage
= kModeratePressurePercentage
;
89 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
:
90 percentage
= kCriticalPressurePercentage
;
93 size_t frames
= (max_number_of_saved_frames_
* percentage
) / 100;
94 return std::max(static_cast<size_t>(1), frames
);
97 RendererFrameManager::RendererFrameManager()
98 : memory_pressure_listener_(
99 base::Bind(&RendererFrameManager::OnMemoryPressure
,
100 base::Unretained(this))) {
101 // Note: With the destruction of this class the |memory_pressure_listener_|
102 // gets destroyed and the observer will remove itself.
103 max_number_of_saved_frames_
=
104 #if defined(OS_ANDROID)
107 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256));
109 max_handles_
= base::SharedMemory::GetHandleLimit() / 8.0f
;
112 RendererFrameManager::~RendererFrameManager() {}
114 void RendererFrameManager::CullUnlockedFrames(size_t saved_frame_limit
) {
115 if (unlocked_frames_
.size() + locked_frames_
.size() > 0) {
116 float handles_per_frame
=
117 HostSharedBitmapManager::current()->AllocatedBitmapCount() * 1.0f
/
118 (unlocked_frames_
.size() + locked_frames_
.size());
120 saved_frame_limit
= std::max(
122 static_cast<int>(std::min(static_cast<float>(saved_frame_limit
),
123 max_handles_
/ handles_per_frame
)));
125 while (!unlocked_frames_
.empty() &&
126 unlocked_frames_
.size() + locked_frames_
.size() > saved_frame_limit
) {
127 size_t old_size
= unlocked_frames_
.size();
128 // Should remove self from list.
129 unlocked_frames_
.back()->EvictCurrentFrame();
130 DCHECK_EQ(unlocked_frames_
.size() + 1, old_size
);
134 void RendererFrameManager::OnMemoryPressure(
135 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level
) {
136 int saved_frame_limit
= max_number_of_saved_frames_
;
137 if (saved_frame_limit
<= 1)
139 int percentage
= 100;
140 switch (memory_pressure_level
) {
141 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
:
142 percentage
= kModeratePressurePercentage
;
144 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
:
145 percentage
= kCriticalPressurePercentage
;
147 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
:
148 // No need to change anything when there is no pressure.
151 CullUnlockedFrames(std::max(1, (saved_frame_limit
* percentage
) / 100));
154 } // namespace content