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 class ChildSharedBitmap
: public cc::SharedBitmap
{
17 ChildSharedBitmap(scoped_refptr
<ThreadSafeSender
> sender
,
18 base::SharedMemory
* shared_memory
,
19 const cc::SharedBitmapId
& id
)
20 : SharedBitmap(static_cast<uint8
*>(shared_memory
->memory()), id
),
22 shared_memory_(shared_memory
) {}
24 ChildSharedBitmap(scoped_refptr
<ThreadSafeSender
> sender
,
25 scoped_ptr
<base::SharedMemory
> shared_memory_holder
,
26 const cc::SharedBitmapId
& id
)
27 : ChildSharedBitmap(sender
, shared_memory_holder
.get(), id
) {
28 shared_memory_holder_
= shared_memory_holder
.Pass();
31 ~ChildSharedBitmap() override
{
32 sender_
->Send(new ChildProcessHostMsg_DeletedSharedBitmap(id()));
35 base::SharedMemory
* memory() override
{ return shared_memory_
; }
38 scoped_refptr
<ThreadSafeSender
> sender_
;
39 base::SharedMemory
* shared_memory_
;
40 scoped_ptr
<base::SharedMemory
> shared_memory_holder_
;
45 ChildSharedBitmapManager::ChildSharedBitmapManager(
46 scoped_refptr
<ThreadSafeSender
> sender
)
50 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
52 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::AllocateSharedBitmap(
53 const gfx::Size
& size
) {
54 TRACE_EVENT2("renderer",
55 "ChildSharedBitmapManager::AllocateSharedMemory",
61 if (!cc::SharedBitmap::SizeInBytes(size
, &memory_size
))
62 return scoped_ptr
<cc::SharedBitmap
>();
63 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
64 scoped_ptr
<base::SharedMemory
> memory
;
66 base::SharedMemoryHandle handle
;
67 sender_
->Send(new ChildProcessHostMsg_SyncAllocateSharedBitmap(
68 memory_size
, id
, &handle
));
69 memory
= make_scoped_ptr(new base::SharedMemory(handle
, false));
70 if (!memory
->Map(memory_size
))
73 memory
= ChildThread::AllocateSharedMemory(memory_size
, sender_
.get());
75 if (!memory
->Map(memory_size
))
77 base::SharedMemoryHandle handle_to_send
= memory
->handle();
78 sender_
->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
79 memory_size
, handle_to_send
, id
));
81 return make_scoped_ptr(new ChildSharedBitmap(sender_
, memory
.Pass(), id
));
84 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::GetSharedBitmapFromId(
86 const cc::SharedBitmapId
&) {
88 return scoped_ptr
<cc::SharedBitmap
>();
91 scoped_ptr
<cc::SharedBitmap
> ChildSharedBitmapManager::GetBitmapForSharedMemory(
92 base::SharedMemory
* mem
) {
93 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
94 base::SharedMemoryHandle handle_to_send
= mem
->handle();
96 if (!mem
->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send
))
97 return scoped_ptr
<cc::SharedBitmap
>();
99 sender_
->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
100 mem
->mapped_size(), handle_to_send
, id
));
102 return make_scoped_ptr(new ChildSharedBitmap(sender_
, mem
, id
));
105 } // namespace content