Fixed TrayBackgroundView::GetBubbleAnchorRect to handle left/right alignment when...
[chromium-blink-merge.git] / content / child / child_shared_bitmap_manager.cc
blob41c22610c4ae237b0ae465cd57ea023a716f6f2a
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"
11 namespace content {
13 namespace {
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()));
28 } // namespace
30 ChildSharedBitmapManager::ChildSharedBitmapManager(
31 scoped_refptr<ThreadSafeSender> sender)
32 : sender_(sender) {
35 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
37 scoped_ptr<cc::SharedBitmap> ChildSharedBitmapManager::AllocateSharedBitmap(
38 const gfx::Size& size) {
39 TRACE_EVENT2("renderer",
40 "ChildSharedBitmapManager::AllocateSharedMemory",
41 "width",
42 size.width(),
43 "height",
44 size.height());
45 size_t memory_size;
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;
50 #if defined(OS_POSIX)
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));
56 #else
57 memory.reset(ChildThread::AllocateSharedMemory(memory_size, sender_));
58 CHECK(memory);
59 base::SharedMemoryHandle handle_to_send = memory->handle();
60 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
61 memory_size, handle_to_send, id));
62 #endif
63 return scoped_ptr<cc::SharedBitmap>(new cc::SharedBitmap(
64 memory.release(), id, base::Bind(&FreeSharedMemory, sender_)));
67 scoped_ptr<cc::SharedBitmap> ChildSharedBitmapManager::GetSharedBitmapFromId(
68 const gfx::Size&,
69 const cc::SharedBitmapId&) {
70 NOTREACHED();
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();
78 #if defined(OS_POSIX)
79 if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send))
80 return scoped_ptr<cc::SharedBitmap>();
81 #endif
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