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

Side by Side Diff: components/mus/common/gpu_service.cc

Issue 2087333002: mus::GpuService: Support establish GpuChannel asynchronously. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 6 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 | « components/mus/common/gpu_service.h ('k') | components/mus/demo/mus_demo.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/mus/common/gpu_service.h" 5 #include "components/mus/common/gpu_service.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/singleton.h" 8 #include "base/memory/singleton.h"
9 #include "base/threading/thread_restrictions.h"
9 #include "components/mus/common/gpu_type_converters.h" 10 #include "components/mus/common/gpu_type_converters.h"
10 #include "components/mus/common/mojo_gpu_memory_buffer_manager.h"
11 #include "components/mus/common/switches.h" 11 #include "components/mus/common/switches.h"
12 #include "components/mus/public/interfaces/gpu_service.mojom.h" 12 #include "components/mus/public/interfaces/gpu_service.mojom.h"
13 #include "mojo/public/cpp/bindings/sync_call_restrictions.h"
14 #include "mojo/public/cpp/system/platform_handle.h" 13 #include "mojo/public/cpp/system/platform_handle.h"
15 #include "services/shell/public/cpp/connector.h" 14 #include "services/shell/public/cpp/connector.h"
16 15
17 namespace mus { 16 namespace mus {
18 17
19 GpuService::GpuService() 18 namespace {
19
20 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner,
21 const tracked_objects::Location& from_here,
22 const base::Closure& callback) {
23 runner->PostTask(from_here, callback);
24 }
25
26 GpuService* g_gpu_service = nullptr;
27 }
28
29 GpuService::GpuService(shell::Connector* connector)
20 : main_message_loop_(base::MessageLoop::current()), 30 : main_message_loop_(base::MessageLoop::current()),
31 connector_(connector),
21 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, 32 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
22 base::WaitableEvent::InitialState::NOT_SIGNALED), 33 base::WaitableEvent::InitialState::NOT_SIGNALED),
23 io_thread_("GPUIOThread"), 34 io_thread_("GPUIOThread"),
24 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager) { 35 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager),
36 is_establishing_(false),
37 establishing_condition_(&lock_) {
38 DCHECK(main_message_loop_);
39 DCHECK(connector_);
25 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); 40 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
26 thread_options.priority = base::ThreadPriority::NORMAL; 41 thread_options.priority = base::ThreadPriority::NORMAL;
27 CHECK(io_thread_.StartWithOptions(thread_options)); 42 CHECK(io_thread_.StartWithOptions(thread_options));
28 } 43 }
29 44
30 GpuService::~GpuService() {} 45 GpuService::~GpuService() {}
31 46
32 // static 47 // static
33 bool GpuService::UseChromeGpuCommandBuffer() { 48 bool GpuService::UseChromeGpuCommandBuffer() {
34 return base::CommandLine::ForCurrentProcess()->HasSwitch( 49 return base::CommandLine::ForCurrentProcess()->HasSwitch(
35 switches::kUseChromeGpuCommandBufferInMus); 50 switches::kUseChromeGpuCommandBufferInMus);
36 } 51 }
37 52
38 // static 53 // static
54 void GpuService::Initialize(shell::Connector* connector) {
55 DCHECK(!g_gpu_service);
56 g_gpu_service = new GpuService(connector);
57 }
58
59 // static
39 GpuService* GpuService::GetInstance() { 60 GpuService* GpuService::GetInstance() {
40 return base::Singleton<GpuService, 61 DCHECK(g_gpu_service);
41 base::LeakySingletonTraits<GpuService>>::get(); 62 return g_gpu_service;
42 } 63 }
43 64
44 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannel( 65 void GpuService::EstablishGpuChannel(const base::Closure& callback) {
45 shell::Connector* connector) {
46 base::AutoLock auto_lock(lock_); 66 base::AutoLock auto_lock(lock_);
67 auto runner = base::ThreadTaskRunnerHandle::Get();
68 if (GetGpuChannelLocked()) {
69 runner->PostTask(FROM_HERE, callback);
70 return;
71 }
72 base::Closure wrapper_callback =
73 IsMainThread() ? callback
74 : base::Bind(PostTask, runner, FROM_HERE, callback);
75 establish_callbacks_.push_back(wrapper_callback);
76
77 if (!is_establishing_) {
78 is_establishing_ = true;
79 main_message_loop_->task_runner()->PostTask(
80 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread,
81 base::Unretained(this)));
82 }
83 }
84
85 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync() {
86 base::AutoLock auto_lock(lock_);
87 if (GetGpuChannelLocked())
88 return gpu_channel_;
89
90 if (!IsMainThread()) {
91 if (!is_establishing_) {
92 // Create an establishing gpu channel task, if there isn't one.
93 is_establishing_ = true;
94 main_message_loop_->task_runner()->PostTask(
95 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread,
96 base::Unretained(this)));
97 }
98 // Wait until the pending establishing task is finished.
99 base::ThreadRestrictions::ScopedAllowWait allow_wait;
100 do {
101 establishing_condition_.Wait();
102 } while (is_establishing_);
103 return gpu_channel_;
104 }
105
106 if (!gpu_service_) {
107 // gpu_service_ is null, it means there is no unfinished async
108 // EstablishGpuChannel call, so we should issue one.
109 is_establishing_ = true;
110 EstablishGpuChannelOnMainThreadLocked();
111 }
112
113 base::AutoUnlock auto_unlock(lock_);
114 base::ThreadRestrictions::ScopedAllowWait allow_wait;
jam 2016/06/23 16:39:39 why isn't this a mojo::SyncCallRestrictions::Scope
jam 2016/06/23 16:42:58 This is undoing Yuzhu's change from yesterday that
Peng 2016/06/23 17:07:36 Becasue maybe there is unfinished pending async me
115 if (!gpu_service_.WaitForIncomingResponse()) {
116 DLOG(WARNING)
117 << "Channel encountered error while establishing gpu channel.";
118 return nullptr;
119 }
120 return gpu_channel_;
121 }
122
123 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() {
124 base::AutoLock auto_lock(lock_);
125 return GetGpuChannelLocked();
126 }
127
128 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() {
47 if (gpu_channel_ && gpu_channel_->IsLost()) { 129 if (gpu_channel_ && gpu_channel_->IsLost()) {
48 gpu_channel_->DestroyChannel(); 130 main_message_loop_->task_runner()->PostTask(
131 FROM_HERE,
132 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_));
49 gpu_channel_ = nullptr; 133 gpu_channel_ = nullptr;
50 } 134 }
135 return gpu_channel_;
136 }
51 137
52 if (gpu_channel_) 138 void GpuService::EstablishGpuChannelOnMainThread() {
53 return gpu_channel_; 139 base::AutoLock auto_lock(lock_);
140 DCHECK(IsMainThread());
141 EstablishGpuChannelOnMainThreadLocked();
142 }
54 143
55 mus::mojom::GpuServicePtr gpu_service; 144 void GpuService::EstablishGpuChannelOnMainThreadLocked() {
56 connector->ConnectToInterface("mojo:mus", &gpu_service); 145 DCHECK(IsMainThread());
146 // is_establishing_ is false, it means GpuService::EstablishGpuChannelSync()
147 // has been used, and we don't need try to establish a new GPU channel
148 // anymore.
149 if (!is_establishing_)
150 return;
151 connector_->ConnectToInterface("mojo:mus", &gpu_service_);
152 gpu_service_->EstablishGpuChannel(
153 base::Bind(&GpuService::EstablishGpuChannelOnMainThreadDone,
154 base::Unretained(this)));
155 }
57 156
58 int client_id = 0; 157 void GpuService::EstablishGpuChannelOnMainThreadDone(
59 mojom::ChannelHandlePtr channel_handle; 158 int client_id,
60 mojom::GpuInfoPtr gpu_info; 159 mojom::ChannelHandlePtr channel_handle,
61 { 160 mojom::GpuInfoPtr gpu_info) {
62 // TODO(penghuang): Remove the ScopedAllowSyncCall when HW rendering is 161 DCHECK(IsMainThread());
63 // enabled in mus chrome. 162 scoped_refptr<gpu::GpuChannelHost> gpu_channel;
64 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; 163 if (client_id) {
65 if (!gpu_service->EstablishGpuChannel(&client_id, &channel_handle, 164 // TODO(penghuang): Get the real gpu info from mus.
66 &gpu_info)) { 165 gpu_channel = gpu::GpuChannelHost::Create(
67 DLOG(WARNING) 166 this, client_id, gpu::GPUInfo(),
68 << "Channel encountered error while establishing gpu channel."; 167 channel_handle.To<IPC::ChannelHandle>(), &shutdown_event_,
69 return nullptr; 168 gpu_memory_buffer_manager_.get());
70 }
71 } 169 }
72 170
73 // TODO(penghuang): Get the real gpu info from mus. 171 base::AutoLock auto_lock(lock_);
74 gpu_channel_ = gpu::GpuChannelHost::Create( 172 DCHECK(is_establishing_);
75 this, client_id, gpu::GPUInfo(), channel_handle.To<IPC::ChannelHandle>(), 173 DCHECK(!gpu_channel_);
76 &shutdown_event_, gpu_memory_buffer_manager_.get()); 174
77 return gpu_channel_; 175 is_establishing_ = false;
176 gpu_channel_ = gpu_channel;
177 establishing_condition_.Broadcast();
178
179 for (const auto& i : establish_callbacks_)
180 i.Run();
181 establish_callbacks_.clear();
182 gpu_service_.reset();
78 } 183 }
79 184
80 bool GpuService::IsMainThread() { 185 bool GpuService::IsMainThread() {
81 return base::MessageLoop::current() == main_message_loop_; 186 return base::MessageLoop::current() == main_message_loop_;
82 } 187 }
83 188
84 scoped_refptr<base::SingleThreadTaskRunner> 189 scoped_refptr<base::SingleThreadTaskRunner>
85 GpuService::GetIOThreadTaskRunner() { 190 GpuService::GetIOThreadTaskRunner() {
86 return io_thread_.task_runner(); 191 return io_thread_.task_runner();
87 } 192 }
(...skipping 11 matching lines...) Expand all
99 MojoResult result = mojo::UnwrapSharedMemoryHandle( 204 MojoResult result = mojo::UnwrapSharedMemoryHandle(
100 std::move(handle), &platform_handle, &shared_memory_size, &readonly); 205 std::move(handle), &platform_handle, &shared_memory_size, &readonly);
101 if (result != MOJO_RESULT_OK) 206 if (result != MOJO_RESULT_OK)
102 return nullptr; 207 return nullptr;
103 DCHECK_EQ(shared_memory_size, size); 208 DCHECK_EQ(shared_memory_size, size);
104 209
105 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); 210 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly);
106 } 211 }
107 212
108 } // namespace mus 213 } // namespace mus
OLDNEW
« no previous file with comments | « components/mus/common/gpu_service.h ('k') | components/mus/demo/mus_demo.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698