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

Side by Side Diff: services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.cc

Issue 2651443002: Fix potential shutdown deadlock in UI service (Closed)
Patch Set: . Created 3 years, 11 months 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
« no previous file with comments | « services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h" 5 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
11 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
12 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" 12 #include "gpu/ipc/client/gpu_memory_buffer_impl.h"
13 #include "mojo/public/cpp/system/buffer.h" 13 #include "mojo/public/cpp/system/buffer.h"
14 #include "mojo/public/cpp/system/platform_handle.h" 14 #include "mojo/public/cpp/system/platform_handle.h"
15 #include "services/service_manager/public/cpp/connector.h" 15 #include "services/service_manager/public/cpp/connector.h"
16 #include "services/ui/public/interfaces/constants.mojom.h" 16 #include "services/ui/public/interfaces/constants.mojom.h"
17 #include "ui/gfx/buffer_format_util.h" 17 #include "ui/gfx/buffer_format_util.h"
18 18
19 using DestructionCallback = base::Callback<void(const gpu::SyncToken& sync)>; 19 using DestructionCallback = base::Callback<void(const gpu::SyncToken& sync)>;
20 20
21 namespace ui { 21 namespace ui {
22 22
23 namespace { 23 namespace {
24 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( 25 void NotifyDestructionOnCorrectThread(
33 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 26 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
34 const DestructionCallback& callback, 27 const DestructionCallback& callback,
35 const gpu::SyncToken& sync_token) { 28 const gpu::SyncToken& sync_token) {
36 task_runner->PostTask(FROM_HERE, base::Bind(callback, sync_token)); 29 task_runner->PostTask(FROM_HERE, base::Bind(callback, sync_token));
37 } 30 }
38 31
39 } // namespace 32 } // namespace
40 33
41 ClientGpuMemoryBufferManager::ClientGpuMemoryBufferManager(mojom::GpuPtr gpu) 34 ClientGpuMemoryBufferManager::ClientGpuMemoryBufferManager(mojom::GpuPtr gpu)
42 : thread_("GpuMemoryThread"), weak_ptr_factory_(this) { 35 : thread_("GpuMemoryThread"), weak_ptr_factory_(this) {
43 CHECK(thread_.Start()); 36 CHECK(thread_.Start());
44 // The thread is owned by this object. Which means the task will not run if 37 // The thread is owned by this object. Which means the task will not run if
45 // the object has been destroyed. So Unretained() is safe. 38 // the object has been destroyed. So Unretained() is safe.
46 thread_.task_runner()->PostTask( 39 thread_.task_runner()->PostTask(
47 FROM_HERE, 40 FROM_HERE,
48 base::Bind(&ClientGpuMemoryBufferManager::InitThread, 41 base::Bind(&ClientGpuMemoryBufferManager::InitThread,
49 base::Unretained(this), base::Passed(gpu.PassInterface()))); 42 base::Unretained(this), base::Passed(gpu.PassInterface())));
50 } 43 }
51 44
52 ClientGpuMemoryBufferManager::~ClientGpuMemoryBufferManager() { 45 ClientGpuMemoryBufferManager::~ClientGpuMemoryBufferManager() {
53 thread_.task_runner()->PostTask( 46 thread_.task_runner()->PostTask(
54 FROM_HERE, base::Bind(&ClientGpuMemoryBufferManager::TearDownThread, 47 FROM_HERE, base::Bind(&ClientGpuMemoryBufferManager::TearDownThread,
55 base::Unretained(this))); 48 base::Unretained(this)));
56 thread_.Stop(); 49 thread_.Stop();
57 } 50 }
58 51
59 void ClientGpuMemoryBufferManager::InitThread(mojom::GpuPtrInfo gpu_info) { 52 void ClientGpuMemoryBufferManager::InitThread(mojom::GpuPtrInfo gpu_info) {
60 gpu_.Bind(std::move(gpu_info)); 53 gpu_.Bind(std::move(gpu_info));
54 gpu_.set_connection_error_handler(
55 base::Bind(&ClientGpuMemoryBufferManager::DisconnectGpuOnThread,
56 base::Unretained(this)));
61 weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); 57 weak_ptr_ = weak_ptr_factory_.GetWeakPtr();
62 } 58 }
63 59
64 void ClientGpuMemoryBufferManager::TearDownThread() { 60 void ClientGpuMemoryBufferManager::TearDownThread() {
65 weak_ptr_factory_.InvalidateWeakPtrs(); 61 weak_ptr_factory_.InvalidateWeakPtrs();
62 DisconnectGpuOnThread();
63 }
64
65 void ClientGpuMemoryBufferManager::DisconnectGpuOnThread() {
66 if (!gpu_.is_bound())
67 return;
66 gpu_.reset(); 68 gpu_.reset();
69 for (auto& waiter : pending_allocation_waiters_)
70 waiter->Signal();
71 pending_allocation_waiters_.clear();
67 } 72 }
68 73
69 void ClientGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread( 74 void ClientGpuMemoryBufferManager::AllocateGpuMemoryBufferOnThread(
70 const gfx::Size& size, 75 const gfx::Size& size,
71 gfx::BufferFormat format, 76 gfx::BufferFormat format,
72 gfx::BufferUsage usage, 77 gfx::BufferUsage usage,
73 gfx::GpuMemoryBufferHandle* handle, 78 gfx::GpuMemoryBufferHandle* handle,
74 base::WaitableEvent* wait) { 79 base::WaitableEvent* wait) {
75 DCHECK(thread_.task_runner()->BelongsToCurrentThread()); 80 DCHECK(thread_.task_runner()->BelongsToCurrentThread());
76 // |handle| and |wait| are both on the stack, and will be alive until |wait| 81 // |handle| and |wait| are both on the stack, and will be alive until |wait|
77 // is signaled. So it is safe for OnGpuMemoryBufferAllocated() to operate on 82 // is signaled. So it is safe for OnGpuMemoryBufferAllocated() to operate on
78 // these. 83 // these.
84 pending_allocation_waiters_.insert(wait);
79 gpu_->CreateGpuMemoryBuffer( 85 gpu_->CreateGpuMemoryBuffer(
80 gfx::GpuMemoryBufferId(++counter_), size, format, usage, 86 gfx::GpuMemoryBufferId(++counter_), size, format, usage,
81 base::Bind(&OnGpuMemoryBufferAllocated, handle, wait)); 87 base::Bind(
88 &ClientGpuMemoryBufferManager::OnGpuMemoryBufferAllocatedOnThread,
89 base::Unretained(this), handle, wait));
90 }
91
92 void ClientGpuMemoryBufferManager::OnGpuMemoryBufferAllocatedOnThread(
93 gfx::GpuMemoryBufferHandle* ret_handle,
94 base::WaitableEvent* wait,
95 const gfx::GpuMemoryBufferHandle& handle) {
96 auto it = pending_allocation_waiters_.find(wait);
97 DCHECK(it != pending_allocation_waiters_.end());
98 pending_allocation_waiters_.erase(it);
99
100 *ret_handle = handle;
101 wait->Signal();
82 } 102 }
83 103
84 void ClientGpuMemoryBufferManager::DeletedGpuMemoryBuffer( 104 void ClientGpuMemoryBufferManager::DeletedGpuMemoryBuffer(
85 gfx::GpuMemoryBufferId id, 105 gfx::GpuMemoryBufferId id,
86 const gpu::SyncToken& sync_token) { 106 const gpu::SyncToken& sync_token) {
87 if (!thread_.task_runner()->BelongsToCurrentThread()) { 107 if (!thread_.task_runner()->BelongsToCurrentThread()) {
88 thread_.task_runner()->PostTask( 108 thread_.task_runner()->PostTask(
89 FROM_HERE, 109 FROM_HERE,
90 base::Bind(&ClientGpuMemoryBufferManager::DeletedGpuMemoryBuffer, 110 base::Bind(&ClientGpuMemoryBufferManager::DeletedGpuMemoryBuffer,
91 base::Unretained(this), id, sync_token)); 111 base::Unretained(this), id, sync_token));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 152 }
133 153
134 void ClientGpuMemoryBufferManager::SetDestructionSyncToken( 154 void ClientGpuMemoryBufferManager::SetDestructionSyncToken(
135 gfx::GpuMemoryBuffer* buffer, 155 gfx::GpuMemoryBuffer* buffer,
136 const gpu::SyncToken& sync_token) { 156 const gpu::SyncToken& sync_token) {
137 static_cast<gpu::GpuMemoryBufferImpl*>(buffer)->set_destruction_sync_token( 157 static_cast<gpu::GpuMemoryBufferImpl*>(buffer)->set_destruction_sync_token(
138 sync_token); 158 sync_token);
139 } 159 }
140 160
141 } // namespace ui 161 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698