| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/ui/gpu/gpu_service_mus.h" | |
| 6 | |
| 7 #include "base/memory/shared_memory.h" | |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "build/build_config.h" | |
| 12 #include "gpu/command_buffer/service/gpu_switches.h" | |
| 13 #include "gpu/command_buffer/service/sync_point_manager.h" | |
| 14 #include "gpu/config/gpu_info_collector.h" | |
| 15 #include "gpu/config/gpu_switches.h" | |
| 16 #include "gpu/config/gpu_util.h" | |
| 17 #include "gpu/ipc/common/gpu_memory_buffer_support.h" | |
| 18 #include "gpu/ipc/common/memory_stats.h" | |
| 19 #include "gpu/ipc/service/gpu_memory_buffer_factory.h" | |
| 20 #include "ipc/ipc_channel_handle.h" | |
| 21 #include "ipc/ipc_sync_message_filter.h" | |
| 22 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" | |
| 23 #include "media/gpu/ipc/service/gpu_video_decode_accelerator.h" | |
| 24 #include "media/gpu/ipc/service/gpu_video_encode_accelerator.h" | |
| 25 #include "media/gpu/ipc/service/media_service.h" | |
| 26 #include "services/ui/gpu/mus_gpu_memory_buffer_manager.h" | |
| 27 #include "ui/gl/gl_implementation.h" | |
| 28 #include "ui/gl/gl_switches.h" | |
| 29 #include "ui/gl/gpu_switching_manager.h" | |
| 30 #include "ui/gl/init/gl_factory.h" | |
| 31 #include "url/gurl.h" | |
| 32 | |
| 33 #if defined(USE_OZONE) | |
| 34 #include "ui/ozone/public/ozone_platform.h" | |
| 35 #endif | |
| 36 | |
| 37 namespace ui { | |
| 38 namespace { | |
| 39 | |
| 40 const int kLocalGpuChannelClientId = 1; | |
| 41 const uint64_t kLocalGpuChannelClientTracingId = 1; | |
| 42 | |
| 43 void EstablishGpuChannelDone( | |
| 44 int client_id, | |
| 45 mojo::ScopedMessagePipeHandle* channel_handle, | |
| 46 const GpuServiceMus::EstablishGpuChannelCallback& callback) { | |
| 47 callback.Run(client_id, std::move(*channel_handle)); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 GpuServiceMus::GpuServiceMus() | |
| 52 : next_client_id_(kLocalGpuChannelClientId), | |
| 53 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 54 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 55 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
| 56 gpu_thread_("GpuThread"), | |
| 57 io_thread_("GpuIOThread") { | |
| 58 Initialize(); | |
| 59 } | |
| 60 | |
| 61 GpuServiceMus::~GpuServiceMus() { | |
| 62 // Signal this event before destroying the child process. That way all | |
| 63 // background threads can cleanup. | |
| 64 // For example, in the renderer the RenderThread instances will be able to | |
| 65 // notice shutdown before the render process begins waiting for them to exit. | |
| 66 shutdown_event_.Signal(); | |
| 67 io_thread_.Stop(); | |
| 68 } | |
| 69 | |
| 70 void GpuServiceMus::EstablishGpuChannel( | |
| 71 uint64_t client_tracing_id, | |
| 72 bool preempts, | |
| 73 bool allow_view_command_buffers, | |
| 74 bool allow_real_time_streams, | |
| 75 const EstablishGpuChannelCallback& callback) { | |
| 76 DCHECK(CalledOnValidThread()); | |
| 77 | |
| 78 if (!gpu_channel_manager_) { | |
| 79 callback.Run(-1, mojo::ScopedMessagePipeHandle()); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 const int client_id = ++next_client_id_; | |
| 84 auto* channel_handle = new mojo::ScopedMessagePipeHandle; | |
| 85 gpu_thread_.task_runner()->PostTaskAndReply( | |
| 86 FROM_HERE, | |
| 87 base::Bind(&GpuServiceMus::EstablishGpuChannelOnGpuThread, | |
| 88 base::Unretained(this), client_id, client_tracing_id, preempts, | |
| 89 allow_view_command_buffers, allow_real_time_streams, | |
| 90 base::Unretained(channel_handle)), | |
| 91 base::Bind(&EstablishGpuChannelDone, client_id, | |
| 92 base::Owned(channel_handle), callback)); | |
| 93 } | |
| 94 | |
| 95 gfx::GpuMemoryBufferHandle GpuServiceMus::CreateGpuMemoryBuffer( | |
| 96 gfx::GpuMemoryBufferId id, | |
| 97 const gfx::Size& size, | |
| 98 gfx::BufferFormat format, | |
| 99 gfx::BufferUsage usage, | |
| 100 int client_id, | |
| 101 gpu::SurfaceHandle surface_handle) { | |
| 102 DCHECK(CalledOnValidThread()); | |
| 103 return gpu_memory_buffer_factory_->CreateGpuMemoryBuffer( | |
| 104 id, size, format, usage, client_id, surface_handle); | |
| 105 } | |
| 106 | |
| 107 void GpuServiceMus::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
| 108 int client_id, | |
| 109 const gpu::SyncToken& sync_token) { | |
| 110 DCHECK(CalledOnValidThread()); | |
| 111 | |
| 112 if (gpu_channel_manager_) | |
| 113 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); | |
| 114 } | |
| 115 | |
| 116 void GpuServiceMus::DidCreateOffscreenContext(const GURL& active_url) { | |
| 117 NOTIMPLEMENTED(); | |
| 118 } | |
| 119 | |
| 120 void GpuServiceMus::DidDestroyChannel(int client_id) { | |
| 121 media_service_->RemoveChannel(client_id); | |
| 122 NOTIMPLEMENTED(); | |
| 123 } | |
| 124 | |
| 125 void GpuServiceMus::DidDestroyOffscreenContext(const GURL& active_url) { | |
| 126 NOTIMPLEMENTED(); | |
| 127 } | |
| 128 | |
| 129 void GpuServiceMus::DidLoseContext(bool offscreen, | |
| 130 gpu::error::ContextLostReason reason, | |
| 131 const GURL& active_url) { | |
| 132 NOTIMPLEMENTED(); | |
| 133 } | |
| 134 | |
| 135 void GpuServiceMus::GpuMemoryUmaStats(const gpu::GPUMemoryUmaStats& params) { | |
| 136 NOTIMPLEMENTED(); | |
| 137 } | |
| 138 | |
| 139 void GpuServiceMus::StoreShaderToDisk(int client_id, | |
| 140 const std::string& key, | |
| 141 const std::string& shader) { | |
| 142 NOTIMPLEMENTED(); | |
| 143 } | |
| 144 | |
| 145 #if defined(OS_WIN) | |
| 146 void GpuServiceMus::SendAcceleratedSurfaceCreatedChildWindow( | |
| 147 gpu::SurfaceHandle parent_window, | |
| 148 gpu::SurfaceHandle child_window) { | |
| 149 ::SetParent(child_window, parent_window); | |
| 150 } | |
| 151 #endif | |
| 152 | |
| 153 void GpuServiceMus::SetActiveURL(const GURL& url) { | |
| 154 // TODO(penghuang): implement this function. | |
| 155 } | |
| 156 | |
| 157 void GpuServiceMus::Initialize() { | |
| 158 DCHECK(CalledOnValidThread()); | |
| 159 base::Thread::Options thread_options(base::MessageLoop::TYPE_DEFAULT, 0); | |
| 160 thread_options.priority = base::ThreadPriority::NORMAL; | |
| 161 CHECK(gpu_thread_.StartWithOptions(thread_options)); | |
| 162 | |
| 163 thread_options = base::Thread::Options(base::MessageLoop::TYPE_IO, 0); | |
| 164 thread_options.priority = base::ThreadPriority::NORMAL; | |
| 165 #if defined(OS_ANDROID) | |
| 166 // TODO(reveman): Remove this in favor of setting it explicitly for each type | |
| 167 // of process. | |
| 168 thread_options.priority = base::ThreadPriority::DISPLAY; | |
| 169 #endif | |
| 170 CHECK(io_thread_.StartWithOptions(thread_options)); | |
| 171 | |
| 172 mojo::ScopedMessagePipeHandle channel_handle; | |
| 173 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 174 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 175 gpu_thread_.task_runner()->PostTask( | |
| 176 FROM_HERE, base::Bind(&GpuServiceMus::InitializeOnGpuThread, | |
| 177 base::Unretained(this), &channel_handle, &event)); | |
| 178 event.Wait(); | |
| 179 | |
| 180 gpu_memory_buffer_manager_local_.reset( | |
| 181 new MusGpuMemoryBufferManager(this, kLocalGpuChannelClientId)); | |
| 182 gpu_channel_local_ = gpu::GpuChannelHost::Create( | |
| 183 this, kLocalGpuChannelClientId, gpu_info_, | |
| 184 IPC::ChannelHandle(channel_handle.release()), &shutdown_event_, | |
| 185 gpu_memory_buffer_manager_local_.get()); | |
| 186 } | |
| 187 | |
| 188 void GpuServiceMus::InitializeOnGpuThread( | |
| 189 mojo::ScopedMessagePipeHandle* channel_handle, | |
| 190 base::WaitableEvent* event) { | |
| 191 gpu_info_.video_decode_accelerator_capabilities = | |
| 192 media::GpuVideoDecodeAccelerator::GetCapabilities(gpu_preferences_); | |
| 193 gpu_info_.video_encode_accelerator_supported_profiles = | |
| 194 media::GpuVideoEncodeAccelerator::GetSupportedProfiles(gpu_preferences_); | |
| 195 gpu_info_.jpeg_decode_accelerator_supported = | |
| 196 media::GpuJpegDecodeAccelerator::IsSupported(); | |
| 197 | |
| 198 #if defined(USE_OZONE) | |
| 199 // TODO(rjkroege): Must plumb the shell::Connector* to here and pass into | |
| 200 // ozone. | |
| 201 ui::OzonePlatform::InitializeForGPU(); | |
| 202 #endif | |
| 203 | |
| 204 if (gpu::GetNativeGpuMemoryBufferType() != gfx::EMPTY_BUFFER) { | |
| 205 gpu_memory_buffer_factory_ = | |
| 206 gpu::GpuMemoryBufferFactory::CreateNativeType(); | |
| 207 } | |
| 208 | |
| 209 if (!gl::init::InitializeGLOneOff()) | |
| 210 VLOG(1) << "gl::init::InitializeGLOneOff failed"; | |
| 211 | |
| 212 DCHECK(!owned_sync_point_manager_); | |
| 213 const bool allow_threaded_wait = false; | |
| 214 owned_sync_point_manager_.reset( | |
| 215 new gpu::SyncPointManager(allow_threaded_wait)); | |
| 216 | |
| 217 // Defer creation of the render thread. This is to prevent it from handling | |
| 218 // IPC messages before the sandbox has been enabled and all other necessary | |
| 219 // initialization has succeeded. | |
| 220 // TODO(penghuang): implement a watchdog. | |
| 221 gpu::GpuWatchdog* watchdog = nullptr; | |
| 222 gpu_channel_manager_.reset(new gpu::GpuChannelManager( | |
| 223 gpu_preferences_, this, watchdog, | |
| 224 base::ThreadTaskRunnerHandle::Get().get(), io_thread_.task_runner().get(), | |
| 225 &shutdown_event_, owned_sync_point_manager_.get(), | |
| 226 gpu_memory_buffer_factory_.get())); | |
| 227 | |
| 228 media_service_.reset(new media::MediaService(gpu_channel_manager_.get())); | |
| 229 | |
| 230 const bool preempts = true; | |
| 231 const bool allow_view_command_buffers = true; | |
| 232 const bool allow_real_time_streams = true; | |
| 233 EstablishGpuChannelOnGpuThread( | |
| 234 kLocalGpuChannelClientId, kLocalGpuChannelClientTracingId, preempts, | |
| 235 allow_view_command_buffers, allow_real_time_streams, channel_handle); | |
| 236 event->Signal(); | |
| 237 } | |
| 238 | |
| 239 void GpuServiceMus::EstablishGpuChannelOnGpuThread( | |
| 240 int client_id, | |
| 241 uint64_t client_tracing_id, | |
| 242 bool preempts, | |
| 243 bool allow_view_command_buffers, | |
| 244 bool allow_real_time_streams, | |
| 245 mojo::ScopedMessagePipeHandle* channel_handle) { | |
| 246 if (gpu_channel_manager_) { | |
| 247 auto handle = gpu_channel_manager_->EstablishChannel( | |
| 248 client_id, client_tracing_id, preempts, allow_view_command_buffers, | |
| 249 allow_real_time_streams); | |
| 250 channel_handle->reset(handle.mojo_handle); | |
| 251 media_service_->AddChannel(client_id); | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 bool GpuServiceMus::IsMainThread() { | |
| 256 return main_task_runner_->BelongsToCurrentThread(); | |
| 257 } | |
| 258 | |
| 259 scoped_refptr<base::SingleThreadTaskRunner> | |
| 260 GpuServiceMus::GetIOThreadTaskRunner() { | |
| 261 return io_thread_.task_runner(); | |
| 262 } | |
| 263 | |
| 264 std::unique_ptr<base::SharedMemory> GpuServiceMus::AllocateSharedMemory( | |
| 265 size_t size) { | |
| 266 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
| 267 if (!shm->CreateAnonymous(size)) | |
| 268 return std::unique_ptr<base::SharedMemory>(); | |
| 269 return shm; | |
| 270 } | |
| 271 | |
| 272 // static | |
| 273 GpuServiceMus* GpuServiceMus::GetInstance() { | |
| 274 return base::Singleton<GpuServiceMus, | |
| 275 base::LeakySingletonTraits<GpuServiceMus>>::get(); | |
| 276 } | |
| 277 | |
| 278 } // namespace ui | |
| OLD | NEW |