child_shared_bitmap_manager.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright (c) 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/child/child_shared_bitmap_manager.h" 6 7#include "content/child/child_thread.h" 8#include "content/common/child_process_messages.h" 9#include "ui/gfx/size.h" 10 11namespace content { 12 13ChildSharedBitmapManager::ChildSharedBitmapManager( 14 scoped_refptr<ThreadSafeSender> sender) 15 : sender_(sender) {} 16 17ChildSharedBitmapManager::~ChildSharedBitmapManager() {} 18 19scoped_ptr<cc::SharedBitmap> ChildSharedBitmapManager::AllocateSharedBitmap( 20 const gfx::Size& size) { 21 TRACE_EVENT2("renderer", 22 "ChildSharedBitmapManager::AllocateSharedMemory", 23 "width", 24 size.width(), 25 "height", 26 size.height()); 27 size_t memory_size; 28 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) 29 return scoped_ptr<cc::SharedBitmap>(); 30 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 31 scoped_ptr<base::SharedMemory> memory; 32#if defined(OS_POSIX) 33 base::SharedMemoryHandle handle; 34 sender_->Send(new ChildProcessHostMsg_SyncAllocateSharedBitmap( 35 memory_size, id, &handle)); 36 memory = make_scoped_ptr(new base::SharedMemory(handle, false)); 37 memory->Map(memory_size); 38#else 39 memory.reset(ChildThread::AllocateSharedMemory(memory_size, sender_)); 40 CHECK(memory); 41 base::SharedMemoryHandle handle_to_send = memory->handle(); 42 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( 43 memory_size, handle_to_send, id)); 44#endif 45 // The compositor owning the SharedBitmap will be closed before the 46 // ChildThread containng this, making the use of base::Unretained safe. 47 return scoped_ptr<cc::SharedBitmap>(new cc::SharedBitmap( 48 memory.release(), 49 id, 50 base::Bind(&ChildSharedBitmapManager::FreeSharedMemory, 51 base::Unretained(this)))); 52} 53 54scoped_ptr<cc::SharedBitmap> ChildSharedBitmapManager::GetSharedBitmapFromId( 55 const gfx::Size&, 56 const cc::SharedBitmapId&) { 57 NOTREACHED(); 58 return scoped_ptr<cc::SharedBitmap>(); 59} 60 61scoped_ptr<cc::SharedBitmap> ChildSharedBitmapManager::GetBitmapForSharedMemory( 62 base::SharedMemory* mem) { 63 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 64 base::SharedMemoryHandle handle_to_send = mem->handle(); 65#if defined(OS_POSIX) 66 if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send)) 67 return scoped_ptr<cc::SharedBitmap>(); 68#endif 69 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( 70 mem->mapped_size(), handle_to_send, id)); 71 // The compositor owning the SharedBitmap will be closed before the 72 // ChildThread containng this, making the use of base::Unretained safe. 73 return scoped_ptr<cc::SharedBitmap>(new cc::SharedBitmap( 74 mem, 75 id, 76 base::Bind(&ChildSharedBitmapManager::ReleaseSharedBitmap, 77 base::Unretained(this)))); 78} 79 80void ChildSharedBitmapManager::FreeSharedMemory(cc::SharedBitmap* bitmap) { 81 TRACE_EVENT0("renderer", "ChildSharedBitmapManager::FreeSharedMemory"); 82 sender_->Send(new ChildProcessHostMsg_DeletedSharedBitmap(bitmap->id())); 83 delete bitmap->memory(); 84} 85 86void ChildSharedBitmapManager::ReleaseSharedBitmap(cc::SharedBitmap* handle) { 87 TRACE_EVENT0("renderer", "ChildSharedBitmapManager::ReleaseSharedBitmap"); 88 sender_->Send(new ChildProcessHostMsg_DeletedSharedBitmap(handle->id())); 89} 90 91} // namespace content 92