Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / renderer_frame_manager.cc
blob0a93e2989438d20b8373ae99c6fe51a2b051a159
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"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/sys_info.h"
12 #include "content/common/host_shared_bitmap_manager.h"
14 namespace content {
16 RendererFrameManager* RendererFrameManager::GetInstance() {
17 return Singleton<RendererFrameManager>::get();
20 void RendererFrameManager::AddFrame(RendererFrameManagerClient* frame,
21 bool locked) {
22 RemoveFrame(frame);
23 if (locked)
24 locked_frames_[frame] = 1;
25 else
26 unlocked_frames_.push_front(frame);
27 CullUnlockedFrames();
30 void RendererFrameManager::RemoveFrame(RendererFrameManagerClient* frame) {
31 std::map<RendererFrameManagerClient*, size_t>::iterator locked_iter =
32 locked_frames_.find(frame);
33 if (locked_iter != locked_frames_.end())
34 locked_frames_.erase(locked_iter);
35 unlocked_frames_.remove(frame);
38 void RendererFrameManager::LockFrame(RendererFrameManagerClient* frame) {
39 std::list<RendererFrameManagerClient*>::iterator unlocked_iter =
40 std::find(unlocked_frames_.begin(), unlocked_frames_.end(), frame);
41 if (unlocked_iter != unlocked_frames_.end()) {
42 DCHECK(locked_frames_.find(frame) == locked_frames_.end());
43 unlocked_frames_.remove(frame);
44 locked_frames_[frame] = 1;
45 } else {
46 DCHECK(locked_frames_.find(frame) != locked_frames_.end());
47 locked_frames_[frame]++;
51 void RendererFrameManager::UnlockFrame(RendererFrameManagerClient* frame) {
52 DCHECK(locked_frames_.find(frame) != locked_frames_.end());
53 size_t locked_count = locked_frames_[frame];
54 DCHECK(locked_count);
55 if (locked_count > 1) {
56 locked_frames_[frame]--;
57 } else {
58 RemoveFrame(frame);
59 unlocked_frames_.push_front(frame);
60 CullUnlockedFrames();
64 RendererFrameManager::RendererFrameManager() {
65 max_number_of_saved_frames_ =
66 #if defined(OS_ANDROID)
68 #else
69 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256));
70 #endif
71 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f;
74 RendererFrameManager::~RendererFrameManager() {}
76 void RendererFrameManager::CullUnlockedFrames() {
77 uint32 saved_frame_limit = max_number_of_saved_frames();
79 if (unlocked_frames_.size() + locked_frames_.size() > 0) {
80 float handles_per_frame =
81 HostSharedBitmapManager::current()->AllocatedBitmapCount() * 1.0f /
82 (unlocked_frames_.size() + locked_frames_.size());
84 saved_frame_limit = std::max(
86 static_cast<int>(std::min(static_cast<float>(saved_frame_limit),
87 max_handles_ / handles_per_frame)));
89 while (!unlocked_frames_.empty() &&
90 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) {
91 size_t old_size = unlocked_frames_.size();
92 // Should remove self from list.
93 unlocked_frames_.back()->EvictCurrentFrame();
94 DCHECK_EQ(unlocked_frames_.size() + 1, old_size);
98 } // namespace content