| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "services/ui/public/cpp/gpu/mojo_gpu_memory_buffer_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/memory/shared_memory.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 12 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" | |
| 13 #include "mojo/public/cpp/system/buffer.h" | |
| 14 #include "mojo/public/cpp/system/platform_handle.h" | |
| 15 #include "services/service_manager/public/cpp/connector.h" | |
| 16 #include "services/ui/public/interfaces/constants.mojom.h" | |
| 17 #include "ui/gfx/buffer_format_util.h" | |
| 18 | |
| 19 using DestructionCallback = base::Callback<void(const gpu::SyncToken& sync)>; | |
| 20 | |
| 21 namespace ui { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 void OnGpuMemoryBufferAllocated(gfx::GpuMemoryBufferHandle* ret_handle, | |
| 26 base::WaitableEvent* wait, | |
| 27 const gfx::GpuMemoryBufferHandle& handle) { | |
| 28 *ret_handle = handle; | |
| 29 wait->Signal(); | |
| 30 } | |
| 31 | |
| 32 void NotifyDestructionOnCorrectThread( | |
| 33 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 34 const DestructionCallback& callback, | |
| 35 const gpu::SyncToken& sync_token) { | |
| 36 task_runner->PostTask(FROM_HERE, base::Bind(callback, sync_token)); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 MojoGpuMemoryBufferManager::MojoGpuMemoryBufferManager( | |
| 42 mojom::GpuServicePtr gpu_service) | |
| 43 : thread_("GpuMemoryThread"), weak_ptr_factory_(this) { | |
| 44 CHECK(thread_.Start()); | |
| 45 // The thread is owned by this object. Which means the task will not run if | |
| 46 // the object has been destroyed. So Unretained() is safe. | |
| 47 thread_.task_runner()->PostTask( | |
| 48 FROM_HERE, base::Bind(&MojoGpuMemoryBufferManager::InitThread, | |
| 49 base::Unretained(this), | |
| 50 base::Passed(gpu_service.PassInterface()))); | |
| 51 } | |
| 52 | |
| 53 MojoGpuMemoryBufferManager::~MojoGpuMemoryBufferManager() { | |
| 54 thread_.task_runner()->PostTask( | |
| 55 FROM_HERE, base::Bind(&MojoGpuMemoryBufferManager::TearDownThread, | |
| 56 base::Unretained(this))); | |
| 57 thread_.Stop(); | |
| 58 } | |
| 59 | |
| 60 void MojoGpuMemoryBufferManager::InitThread( | |
| 61 mojo::InterfacePtrInfo<mojom::GpuService> gpu_service_info) { | |
| 62 gpu_service_.Bind(std::move(gpu_service_info)); | |
| 63 weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); | |
| 64 } | |
| 65 | |
| 66 void MojoGpuMemoryBufferManager::TearDownThread() { | |
| 67 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 68 gpu_service_.reset(); | |
| 69 } | |
| 70 | |
| 71 void MojoGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread( | |
| 72 const gfx::Size& size, | |
| 73 gfx::BufferFormat format, | |
| 74 gfx::BufferUsage usage, | |
| 75 gfx::GpuMemoryBufferHandle* handle, | |
| 76 base::WaitableEvent* wait) { | |
| 77 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); | |
| 78 // |handle| and |wait| are both on the stack, and will be alive until |wait| | |
| 79 // is signaled. So it is safe for OnGpuMemoryBufferAllocated() to operate on | |
| 80 // these. | |
| 81 gpu_service_->CreateGpuMemoryBuffer( | |
| 82 gfx::GpuMemoryBufferId(++counter_), size, format, usage, | |
| 83 base::Bind(&OnGpuMemoryBufferAllocated, handle, wait)); | |
| 84 } | |
| 85 | |
| 86 void MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer( | |
| 87 gfx::GpuMemoryBufferId id, | |
| 88 const gpu::SyncToken& sync_token) { | |
| 89 if (!thread_.task_runner()->BelongsToCurrentThread()) { | |
| 90 thread_.task_runner()->PostTask( | |
| 91 FROM_HERE, | |
| 92 base::Bind(&MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer, | |
| 93 base::Unretained(this), id, sync_token)); | |
| 94 return; | |
| 95 } | |
| 96 gpu_service_->DestroyGpuMemoryBuffer(id, sync_token); | |
| 97 } | |
| 98 | |
| 99 std::unique_ptr<gfx::GpuMemoryBuffer> | |
| 100 MojoGpuMemoryBufferManager::CreateGpuMemoryBuffer( | |
| 101 const gfx::Size& size, | |
| 102 gfx::BufferFormat format, | |
| 103 gfx::BufferUsage usage, | |
| 104 gpu::SurfaceHandle surface_handle) { | |
| 105 // Note: this can be called from multiple threads at the same time. Some of | |
| 106 // those threads may not have a TaskRunner set. | |
| 107 DCHECK_EQ(gpu::kNullSurfaceHandle, surface_handle); | |
| 108 CHECK(!thread_.task_runner()->BelongsToCurrentThread()); | |
| 109 gfx::GpuMemoryBufferHandle gmb_handle; | |
| 110 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 111 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 112 thread_.task_runner()->PostTask( | |
| 113 FROM_HERE, | |
| 114 base::Bind(&MojoGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread, | |
| 115 base::Unretained(this), size, format, usage, &gmb_handle, | |
| 116 &wait)); | |
| 117 wait.Wait(); | |
| 118 if (gmb_handle.is_null()) | |
| 119 return nullptr; | |
| 120 | |
| 121 DestructionCallback callback = | |
| 122 base::Bind(&MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer, weak_ptr_, | |
| 123 gmb_handle.id); | |
| 124 std::unique_ptr<gpu::GpuMemoryBufferImpl> buffer( | |
| 125 gpu::GpuMemoryBufferImpl::CreateFromHandle( | |
| 126 gmb_handle, size, format, usage, | |
| 127 base::Bind(&NotifyDestructionOnCorrectThread, thread_.task_runner(), | |
| 128 callback))); | |
| 129 if (!buffer) { | |
| 130 DeletedGpuMemoryBuffer(gmb_handle.id, gpu::SyncToken()); | |
| 131 return nullptr; | |
| 132 } | |
| 133 return std::move(buffer); | |
| 134 } | |
| 135 | |
| 136 std::unique_ptr<gfx::GpuMemoryBuffer> | |
| 137 MojoGpuMemoryBufferManager::CreateGpuMemoryBufferFromHandle( | |
| 138 const gfx::GpuMemoryBufferHandle& handle, | |
| 139 const gfx::Size& size, | |
| 140 gfx::BufferFormat format) { | |
| 141 NOTIMPLEMENTED(); | |
| 142 return nullptr; | |
| 143 } | |
| 144 | |
| 145 void MojoGpuMemoryBufferManager::SetDestructionSyncToken( | |
| 146 gfx::GpuMemoryBuffer* buffer, | |
| 147 const gpu::SyncToken& sync_token) { | |
| 148 static_cast<gpu::GpuMemoryBufferImpl*>(buffer)->set_destruction_sync_token( | |
| 149 sync_token); | |
| 150 } | |
| 151 | |
| 152 } // namespace ui | |
| OLD | NEW |