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.
5 #include "content/child/child_shared_bitmap_manager.h"
7 #include "content/child/child_thread.h"
8 #include "content/common/child_process_messages.h"
9 #include "ui/gfx/size.h"
15 void FreeSharedMemory(scoped_refptr
<ThreadSafeSender
> sender
,
16 cc::SharedBitmap
* bitmap
) {
17 TRACE_EVENT0("renderer", "ChildSharedBitmapManager::FreeSharedMemory");
18 sender
->Send(new ChildProcessHostMsg_DeletedSharedBitmap(bitmap
->id()));
19 delete bitmap
->memory();
22 void ReleaseSharedBitmap(scoped_refptr
<ThreadSafeSender
> sender
,
23 cc::SharedBitmap
* handle
) {
24 TRACE_EVENT0("renderer", "ChildSharedBitmapManager::ReleaseSharedBitmap");
25 sender
->Send(new ChildProcessHostMsg_DeletedSharedBitmap(handle
->id()));
30 ChildSharedBitmapManager::ChildSharedBitmapManager(
31 scoped_refptr
<ThreadSafeSender
> sender
)
35 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
37 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::AllocateSharedBitmap(
38 const gfx::Size
& size
) {
39 TRACE_EVENT2("renderer",
40 "ChildSharedBitmapManager::AllocateSharedMemory",
46 if (!cc::SharedBitmap::SizeInBytes(size
, &memory_size
))
47 return scoped_ptr
<cc::SharedBitmap
>();
48 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
49 scoped_ptr
<base::SharedMemory
> memory
;
51 base::SharedMemoryHandle handle
;
52 sender_
->Send(new ChildProcessHostMsg_SyncAllocateSharedBitmap(
53 memory_size
, id
, &handle
));
54 memory
= make_scoped_ptr(new base::SharedMemory(handle
, false));
55 CHECK(memory
->Map(memory_size
));
57 memory
.reset(ChildThread::AllocateSharedMemory(memory_size
, sender_
));
59 base::SharedMemoryHandle handle_to_send
= memory
->handle();
60 sender_
->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
61 memory_size
, handle_to_send
, id
));
63 return scoped_ptr
<cc::SharedBitmap
>(new cc::SharedBitmap(
64 memory
.release(), id
, base::Bind(&FreeSharedMemory
, sender_
)));
67 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::GetSharedBitmapFromId(
69 const cc::SharedBitmapId
&) {
71 return scoped_ptr
<cc::SharedBitmap
>();
74 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::GetBitmapForSharedMemory(
75 base::SharedMemory
* mem
) {
76 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
77 base::SharedMemoryHandle handle_to_send
= mem
->handle();
79 if (!mem
->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send
))
80 return scoped_ptr
<cc::SharedBitmap
>();
82 sender_
->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
83 mem
->mapped_size(), handle_to_send
, id
));
84 // The compositor owning the SharedBitmap will be closed before the
85 // ChildThread containng this, making the use of base::Unretained safe.
86 return scoped_ptr
<cc::SharedBitmap
>(
87 new cc::SharedBitmap(mem
, id
, base::Bind(&ReleaseSharedBitmap
, sender_
)));
90 } // namespace content