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. 4 5 #include "content/browser/renderer_host/renderer_frame_manager.h" 6 7 #include <algorithm> 8 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" 13 14 namespace content { 15 GetInstance()16RendererFrameManager* RendererFrameManager::GetInstance() { 17 return Singleton<RendererFrameManager>::get(); 18 } 19 AddFrame(RendererFrameManagerClient * frame,bool locked)20void 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(); 28 } 29 RemoveFrame(RendererFrameManagerClient * frame)30void 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); 36 } 37 LockFrame(RendererFrameManagerClient * frame)38void 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]++; 48 } 49 } 50 UnlockFrame(RendererFrameManagerClient * frame)51void 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(); 61 } 62 } 63 RendererFrameManager()64RendererFrameManager::RendererFrameManager() { 65 max_number_of_saved_frames_ = 66 #if defined(OS_ANDROID) 67 1; 68 #else 69 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256)); 70 #endif 71 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f; 72 } 73 ~RendererFrameManager()74RendererFrameManager::~RendererFrameManager() {} 75 CullUnlockedFrames()76void RendererFrameManager::CullUnlockedFrames() { 77 uint32 saved_frame_limit = max_number_of_saved_frames(); 78 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()); 83 84 saved_frame_limit = std::max( 85 1, 86 static_cast<int>(std::min(static_cast<float>(saved_frame_limit), 87 max_handles_ / handles_per_frame))); 88 } 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); 95 } 96 } 97 98 } // namespace content 99