Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Unified Diff: content/child/child_shared_bitmap_manager.cc

Issue 2549213005: Reland of placing allocate bitmap IPC messages with a new Mojo interface. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/child/child_shared_bitmap_manager.h ('k') | content/child/child_thread_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/child/child_shared_bitmap_manager.cc
diff --git a/content/child/child_shared_bitmap_manager.cc b/content/child/child_shared_bitmap_manager.cc
index 00b461c7e96baad1d89e7d8717ff523cdaf132b4..b0a46299d8951f66b64ca92a966f5a6b532a65ad 100644
--- a/content/child/child_shared_bitmap_manager.cc
+++ b/content/child/child_shared_bitmap_manager.cc
@@ -15,6 +15,7 @@
#include "build/build_config.h"
#include "content/child/child_thread_impl.h"
#include "content/common/child_process_messages.h"
+#include "mojo/public/cpp/system/platform_handle.h"
#include "ui/gfx/geometry/size.h"
namespace content {
@@ -23,27 +24,34 @@
class ChildSharedBitmap : public SharedMemoryBitmap {
public:
- ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender,
- base::SharedMemory* shared_memory,
- const cc::SharedBitmapId& id)
+ ChildSharedBitmap(
+ const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
+ render_message_filter_ptr,
+ base::SharedMemory* shared_memory,
+ const cc::SharedBitmapId& id)
: SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()),
id,
shared_memory),
- sender_(sender) {}
+ render_message_filter_ptr_(render_message_filter_ptr) {}
- ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender,
- std::unique_ptr<base::SharedMemory> shared_memory_holder,
- const cc::SharedBitmapId& id)
- : ChildSharedBitmap(sender, shared_memory_holder.get(), id) {
+ ChildSharedBitmap(
+ const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
+ render_message_filter_ptr,
+ std::unique_ptr<base::SharedMemory> shared_memory_holder,
+ const cc::SharedBitmapId& id)
+ : ChildSharedBitmap(render_message_filter_ptr,
+ shared_memory_holder.get(),
+ id) {
shared_memory_holder_ = std::move(shared_memory_holder);
}
~ChildSharedBitmap() override {
- sender_->Send(new ChildProcessHostMsg_DeletedSharedBitmap(id()));
+ (*render_message_filter_ptr_)->DeletedSharedBitmap(id());
}
private:
- scoped_refptr<ThreadSafeSender> sender_;
+ scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>
+ render_message_filter_ptr_;
std::unique_ptr<base::SharedMemory> shared_memory_holder_;
};
@@ -79,25 +87,14 @@
: SharedBitmap(pixels, id), shared_memory_(shared_memory) {}
ChildSharedBitmapManager::ChildSharedBitmapManager(
- scoped_refptr<ThreadSafeSender> sender)
- : sender_(sender) {
-}
+ const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
+ render_message_filter_ptr)
+ : render_message_filter_ptr_(render_message_filter_ptr) {}
ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
std::unique_ptr<cc::SharedBitmap>
ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) {
- std::unique_ptr<SharedMemoryBitmap> bitmap = AllocateSharedMemoryBitmap(size);
-#if defined(OS_POSIX)
- // Close file descriptor to avoid running out.
- if (bitmap)
- bitmap->shared_memory()->Close();
-#endif
- return std::move(bitmap);
-}
-
-std::unique_ptr<SharedMemoryBitmap>
-ChildSharedBitmapManager::AllocateSharedMemoryBitmap(const gfx::Size& size) {
TRACE_EVENT2("renderer",
"ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width",
size.width(), "height", size.height());
@@ -105,25 +102,10 @@
if (!cc::SharedBitmap::SizeInBytes(size, &memory_size))
return std::unique_ptr<SharedMemoryBitmap>();
cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
- std::unique_ptr<base::SharedMemory> memory;
-#if defined(OS_POSIX)
- base::SharedMemoryHandle handle;
- bool send_success =
- sender_->Send(new ChildProcessHostMsg_SyncAllocateSharedBitmap(
- memory_size, id, &handle));
- if (!send_success) {
- // Callers of this method are not prepared to handle failures during
- // shutdown. Exit immediately. This is expected behavior during the Fast
- // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121.
- exit(EXIT_SUCCESS);
- }
- memory = base::MakeUnique<base::SharedMemory>(handle, false);
- if (!memory->Map(memory_size))
- CollectMemoryUsageAndDie(size, memory_size);
-#else
- bool out_of_memory;
- memory = ChildThreadImpl::AllocateSharedMemory(memory_size, sender_.get(),
- &out_of_memory);
+ bool out_of_memory = false;
+ std::unique_ptr<base::SharedMemory> memory =
+ ChildThreadImpl::AllocateSharedMemory(memory_size, nullptr,
+ &out_of_memory);
if (!memory) {
if (out_of_memory) {
CollectMemoryUsageAndDie(size, memory_size);
@@ -138,11 +120,14 @@
if (!memory->Map(memory_size))
CollectMemoryUsageAndDie(size, memory_size);
- base::SharedMemoryHandle handle_to_send = memory->handle();
- sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
- memory_size, handle_to_send, id));
-#endif
- return base::MakeUnique<ChildSharedBitmap>(sender_, std::move(memory), id);
+ NotifyAllocatedSharedBitmap(memory.get(), id);
+
+ // Close the associated FD to save resources, the previously mapped memory
+ // remains available.
+ memory->Close();
+
+ return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
+ std::move(memory), id);
}
std::unique_ptr<cc::SharedBitmap>
@@ -155,15 +140,28 @@
std::unique_ptr<cc::SharedBitmap>
ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) {
cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
- base::SharedMemoryHandle handle_to_send = mem->handle();
-#if defined(OS_POSIX)
- if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send))
- return std::unique_ptr<cc::SharedBitmap>();
-#endif
- sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
- mem->mapped_size(), handle_to_send, id));
+ NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId());
+ return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
+ std::move(mem), id);
+}
- return base::MakeUnique<ChildSharedBitmap>(sender_, mem, id);
+// Notifies the browser process that a shared bitmap with the given ID was
+// allocated. Caller keeps ownership of |memory|.
+void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap(
+ base::SharedMemory* memory,
+ const cc::SharedBitmapId& id) {
+ base::SharedMemoryHandle handle_to_send =
+ base::SharedMemory::DuplicateHandle(memory->handle());
+ if (!base::SharedMemory::IsHandleValid(handle_to_send)) {
+ LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap.";
+ return;
+ }
+
+ mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle(
+ handle_to_send, memory->mapped_size(), true /* read_only */);
+
+ (*render_message_filter_ptr_)
+ ->AllocatedSharedBitmap(std::move(buffer_handle), id);
}
} // namespace content
« no previous file with comments | « content/child/child_shared_bitmap_manager.h ('k') | content/child/child_thread_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698