OLD | NEW |
(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 "content/browser/gpu/gpu_arc_video_service_host.h" |
| 6 |
| 7 #include "base/location.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/gpu/gpu_process_host.h" |
| 11 #include "content/common/gpu/gpu_messages.h" |
| 12 #include "content/public/browser/arc_video_service.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "ipc/ipc_message_macros.h" |
| 15 #include "ipc/ipc_message_utils.h" |
| 16 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 namespace { |
| 21 void CreateChannelOnIOThread( |
| 22 const GpuProcessHost::CreateArcVideoAcceleratorChannelCallback& callback) { |
| 23 GpuProcessHost* gpu_process_host = |
| 24 GpuProcessHost::Get(GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, |
| 25 CAUSE_FOR_GPU_LAUNCH_ARCVIDEOACCELERATOR); |
| 26 gpu_process_host->CreateArcVideoAcceleratorChannel(callback); |
| 27 } |
| 28 } |
| 29 |
| 30 arc::ArcVideoService* CreateArcVideoService() { |
| 31 return new GpuArcVideoServiceHost(); |
| 32 } |
| 33 |
| 34 GpuArcVideoServiceHost::GpuArcVideoServiceHost() |
| 35 : io_task_runner_(content::BrowserThread::GetMessageLoopProxyForThread( |
| 36 content::BrowserThread::IO)), |
| 37 binding_(this), |
| 38 weak_factory_(this) {} |
| 39 |
| 40 GpuArcVideoServiceHost::~GpuArcVideoServiceHost() { |
| 41 DCHECK(CalledOnValidThread()); |
| 42 |
| 43 arc::ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
| 44 if (bridge_service) { |
| 45 bridge_service->RemoveObserver(this); |
| 46 } |
| 47 } |
| 48 |
| 49 void GpuArcVideoServiceHost::Initialize() { |
| 50 DCHECK(CalledOnValidThread()); |
| 51 |
| 52 arc::ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
| 53 DCHECK(bridge_service); |
| 54 bridge_service->AddObserver(this); |
| 55 if (bridge_service->video_instance()) |
| 56 OnVideoInstanceReady(); |
| 57 } |
| 58 |
| 59 void GpuArcVideoServiceHost::OnStateChanged( |
| 60 arc::ArcBridgeService::State state) { |
| 61 DCHECK(CalledOnValidThread()); |
| 62 switch (state) { |
| 63 case arc::ArcBridgeService::State::STOPPING: |
| 64 Shutdown(); |
| 65 break; |
| 66 default: |
| 67 break; |
| 68 } |
| 69 } |
| 70 |
| 71 void GpuArcVideoServiceHost::OnVideoInstanceReady() { |
| 72 DCHECK(CalledOnValidThread()); |
| 73 arc::ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
| 74 DCHECK(bridge_service); |
| 75 |
| 76 arc::VideoHostPtr host; |
| 77 binding_.Bind(mojo::GetProxy(&host)); |
| 78 bridge_service->video_instance()->Init(std::move(host)); |
| 79 } |
| 80 |
| 81 void GpuArcVideoServiceHost::OnRequestArcVideoAcceleratorChannel( |
| 82 const OnRequestArcVideoAcceleratorChannelCallback& callback) { |
| 83 DCHECK(CalledOnValidThread()); |
| 84 |
| 85 io_task_runner_->PostTask( |
| 86 FROM_HERE, |
| 87 base::Bind(&CreateChannelOnIOThread, |
| 88 base::Bind(&GpuArcVideoServiceHost::ReplyChannelCreated, |
| 89 weak_factory_.GetWeakPtr(), callback))); |
| 90 } |
| 91 |
| 92 void GpuArcVideoServiceHost::Shutdown() { |
| 93 GpuProcessHost::SendOnIO(GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, |
| 94 CAUSE_FOR_GPU_LAUNCH_NO_LAUNCH, |
| 95 new GpuMsg_ShutdownArcVideoService()); |
| 96 } |
| 97 |
| 98 void GpuArcVideoServiceHost::ReplyChannelCreated( |
| 99 const OnRequestArcVideoAcceleratorChannelCallback& callback, |
| 100 const IPC::ChannelHandle& handle) { |
| 101 DCHECK(CalledOnValidThread()); |
| 102 |
| 103 MojoHandle wrapped_handle; |
| 104 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( |
| 105 mojo::embedder::ScopedPlatformHandle( |
| 106 mojo::embedder::PlatformHandle(handle.socket.fd)), |
| 107 &wrapped_handle); |
| 108 if (wrap_result != MOJO_RESULT_OK) { |
| 109 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; |
| 110 callback.Run(mojo::ScopedHandle()); |
| 111 return; |
| 112 } |
| 113 callback.Run(mojo::ScopedHandle(mojo::Handle(wrapped_handle))); |
| 114 } |
| 115 |
| 116 } // namespace content |
OLD | NEW |