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

Side by Side Diff: services/ui/public/cpp/mojo_gpu_memory_buffer_manager.cc

Issue 2511143004: mus: Split the gpu client-lib into a separate component. (Closed)
Patch Set: . Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
(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/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 namespace ui {
20
21 namespace {
22
23 void OnGpuMemoryBufferAllocated(gfx::GpuMemoryBufferHandle* ret_handle,
24 base::WaitableEvent* wait,
25 const gfx::GpuMemoryBufferHandle& handle) {
26 *ret_handle = handle;
27 wait->Signal();
28 }
29
30 } // namespace
31
32 MojoGpuMemoryBufferManager::MojoGpuMemoryBufferManager(
33 service_manager::Connector* connector)
34 : thread_("GpuMemoryThread"),
35 connector_(connector->Clone()),
36 weak_ptr_factory_(this) {
37 CHECK(thread_.Start());
38 // The thread is owned by this object. Which means the test will not run if
39 // the object has been destroyed. So Unretained() is safe.
40 thread_.task_runner()->PostTask(
41 FROM_HERE, base::Bind(&MojoGpuMemoryBufferManager::InitThread,
42 base::Unretained(this)));
43 }
44
45 MojoGpuMemoryBufferManager::~MojoGpuMemoryBufferManager() {
46 thread_.task_runner()->PostTask(
47 FROM_HERE, base::Bind(&MojoGpuMemoryBufferManager::TearDownThread,
48 base::Unretained(this)));
49 thread_.Stop();
50 }
51
52 void MojoGpuMemoryBufferManager::InitThread() {
53 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_);
54 }
55
56 void MojoGpuMemoryBufferManager::TearDownThread() {
57 gpu_service_.reset();
58 }
59
60 void MojoGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread(
61 const gfx::Size& size,
62 gfx::BufferFormat format,
63 gfx::BufferUsage usage,
64 gfx::GpuMemoryBufferHandle* handle,
65 base::WaitableEvent* wait) {
66 DCHECK(thread_.task_runner()->BelongsToCurrentThread());
67 // |handle| and |wait| are both on the stack, and will be alive until |wait|
68 // is signaled. So it is safe for OnGpuMemoryBufferAllocated() to operate on
69 // these.
70 gpu_service_->CreateGpuMemoryBuffer(
71 gfx::GpuMemoryBufferId(++counter_), size, format, usage,
72 base::Bind(&OnGpuMemoryBufferAllocated, handle, wait));
73 }
74
75 void MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer(
76 gfx::GpuMemoryBufferId id,
77 const gpu::SyncToken& sync_token) {
78 if (!thread_.task_runner()->BelongsToCurrentThread()) {
79 thread_.task_runner()->PostTask(
80 FROM_HERE,
81 base::Bind(&MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer,
82 base::Unretained(this), id, sync_token));
83 return;
84 }
85 gpu_service_->DestroyGpuMemoryBuffer(id, sync_token);
86 }
87
88 std::unique_ptr<gfx::GpuMemoryBuffer>
89 MojoGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
90 const gfx::Size& size,
91 gfx::BufferFormat format,
92 gfx::BufferUsage usage,
93 gpu::SurfaceHandle surface_handle) {
94 // Note: this can be called from multiple threads at the same time. Some of
95 // those threads may not have a TaskRunner set.
96 DCHECK_EQ(gpu::kNullSurfaceHandle, surface_handle);
97 CHECK(!thread_.task_runner()->BelongsToCurrentThread());
98 gfx::GpuMemoryBufferHandle gmb_handle;
99 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC,
100 base::WaitableEvent::InitialState::NOT_SIGNALED);
101 thread_.task_runner()->PostTask(
102 FROM_HERE,
103 base::Bind(&MojoGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread,
104 base::Unretained(this), size, format, usage, &gmb_handle,
105 &wait));
106 wait.Wait();
107 if (gmb_handle.is_null())
108 return nullptr;
109 std::unique_ptr<gpu::GpuMemoryBufferImpl> buffer(
110 gpu::GpuMemoryBufferImpl::CreateFromHandle(
111 gmb_handle, size, format, usage,
112 base::Bind(&MojoGpuMemoryBufferManager::DeletedGpuMemoryBuffer,
113 weak_ptr_factory_.GetWeakPtr(), gmb_handle.id)));
114 if (!buffer) {
115 DeletedGpuMemoryBuffer(gmb_handle.id, gpu::SyncToken());
116 return nullptr;
117 }
118 return std::move(buffer);
119 }
120
121 std::unique_ptr<gfx::GpuMemoryBuffer>
122 MojoGpuMemoryBufferManager::CreateGpuMemoryBufferFromHandle(
123 const gfx::GpuMemoryBufferHandle& handle,
124 const gfx::Size& size,
125 gfx::BufferFormat format) {
126 NOTIMPLEMENTED();
127 return nullptr;
128 }
129
130 void MojoGpuMemoryBufferManager::SetDestructionSyncToken(
131 gfx::GpuMemoryBuffer* buffer,
132 const gpu::SyncToken& sync_token) {
133 static_cast<gpu::GpuMemoryBufferImpl*>(buffer)->set_destruction_sync_token(
134 sync_token);
135 }
136
137 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698