| OLD | NEW |
| 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 "services/ui/public/cpp/gpu_service.h" | 5 #include "services/ui/public/cpp/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_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, | 23 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 24 const tracked_objects::Location& from_here, | 24 const tracked_objects::Location& from_here, |
| 25 const gpu::GpuChannelEstablishedCallback& callback, | 25 const gpu::GpuChannelEstablishedCallback& callback, |
| 26 scoped_refptr<gpu::GpuChannelHost> established_channel_host) { | 26 scoped_refptr<gpu::GpuChannelHost> established_channel_host) { |
| 27 runner->PostTask(from_here, | 27 runner->PostTask(from_here, |
| 28 base::Bind(callback, std::move(established_channel_host))); | 28 base::Bind(callback, std::move(established_channel_host))); |
| 29 } | 29 } |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 GpuService::GpuService(shell::Connector* connector) | 33 GpuService::GpuService(shell::Connector* connector, |
| 34 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 34 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 35 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 36 io_task_runner_(std::move(task_runner)), |
| 35 connector_(connector), | 37 connector_(connector), |
| 36 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 38 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 37 base::WaitableEvent::InitialState::NOT_SIGNALED), | 39 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 38 io_thread_("GPUIOThread"), | |
| 39 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), | 40 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), |
| 40 is_establishing_(false), | 41 is_establishing_(false), |
| 41 establishing_condition_(&lock_) { | 42 establishing_condition_(&lock_) { |
| 42 DCHECK(main_task_runner_); | 43 DCHECK(main_task_runner_); |
| 43 DCHECK(connector_); | 44 DCHECK(connector_); |
| 44 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 45 if (!io_task_runner_) { |
| 45 thread_options.priority = base::ThreadPriority::NORMAL; | 46 io_thread_.reset(new base::Thread("GPUIOThread")); |
| 46 CHECK(io_thread_.StartWithOptions(thread_options)); | 47 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 48 thread_options.priority = base::ThreadPriority::NORMAL; |
| 49 CHECK(io_thread_->StartWithOptions(thread_options)); |
| 50 io_task_runner_ = io_thread_->task_runner(); |
| 51 } |
| 47 } | 52 } |
| 48 | 53 |
| 49 GpuService::~GpuService() { | 54 GpuService::~GpuService() { |
| 50 DCHECK(IsMainThread()); | 55 DCHECK(IsMainThread()); |
| 51 if (gpu_channel_) | 56 if (gpu_channel_) |
| 52 gpu_channel_->DestroyChannel(); | 57 gpu_channel_->DestroyChannel(); |
| 53 } | 58 } |
| 54 | 59 |
| 55 // static | 60 // static |
| 56 std::unique_ptr<GpuService> GpuService::Initialize( | 61 std::unique_ptr<GpuService> GpuService::Create( |
| 57 shell::Connector* connector) { | 62 shell::Connector* connector, |
| 58 return base::WrapUnique(new GpuService(connector)); | 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 64 return base::WrapUnique(new GpuService(connector, std::move(task_runner))); |
| 59 } | 65 } |
| 60 | 66 |
| 61 void GpuService::EstablishGpuChannel( | 67 void GpuService::EstablishGpuChannel( |
| 62 const gpu::GpuChannelEstablishedCallback& callback) { | 68 const gpu::GpuChannelEstablishedCallback& callback) { |
| 63 base::AutoLock auto_lock(lock_); | 69 base::AutoLock auto_lock(lock_); |
| 64 auto runner = base::ThreadTaskRunnerHandle::Get(); | 70 auto runner = base::ThreadTaskRunnerHandle::Get(); |
| 65 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannelLocked(); | 71 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannelLocked(); |
| 66 if (channel) { | 72 if (channel) { |
| 67 PostTask(runner, FROM_HERE, callback, std::move(channel)); | 73 PostTask(runner, FROM_HERE, callback, std::move(channel)); |
| 68 return; | 74 return; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 i.Run(gpu_channel_); | 204 i.Run(gpu_channel_); |
| 199 establish_callbacks_.clear(); | 205 establish_callbacks_.clear(); |
| 200 } | 206 } |
| 201 | 207 |
| 202 bool GpuService::IsMainThread() { | 208 bool GpuService::IsMainThread() { |
| 203 return main_task_runner_->BelongsToCurrentThread(); | 209 return main_task_runner_->BelongsToCurrentThread(); |
| 204 } | 210 } |
| 205 | 211 |
| 206 scoped_refptr<base::SingleThreadTaskRunner> | 212 scoped_refptr<base::SingleThreadTaskRunner> |
| 207 GpuService::GetIOThreadTaskRunner() { | 213 GpuService::GetIOThreadTaskRunner() { |
| 208 return io_thread_.task_runner(); | 214 return io_task_runner_; |
| 209 } | 215 } |
| 210 | 216 |
| 211 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( | 217 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( |
| 212 size_t size) { | 218 size_t size) { |
| 213 mojo::ScopedSharedBufferHandle handle = | 219 mojo::ScopedSharedBufferHandle handle = |
| 214 mojo::SharedBufferHandle::Create(size); | 220 mojo::SharedBufferHandle::Create(size); |
| 215 if (!handle.is_valid()) | 221 if (!handle.is_valid()) |
| 216 return nullptr; | 222 return nullptr; |
| 217 | 223 |
| 218 base::SharedMemoryHandle platform_handle; | 224 base::SharedMemoryHandle platform_handle; |
| 219 size_t shared_memory_size; | 225 size_t shared_memory_size; |
| 220 bool readonly; | 226 bool readonly; |
| 221 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 227 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 222 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 228 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 223 if (result != MOJO_RESULT_OK) | 229 if (result != MOJO_RESULT_OK) |
| 224 return nullptr; | 230 return nullptr; |
| 225 DCHECK_EQ(shared_memory_size, size); | 231 DCHECK_EQ(shared_memory_size, size); |
| 226 | 232 |
| 227 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 233 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 228 } | 234 } |
| 229 | 235 |
| 230 } // namespace ui | 236 } // namespace ui |
| OLD | NEW |