OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/common/gpu/client/gl_surface_capturer_host.h" |
| 6 |
| 7 #include "content/common/gpu/client/gpu_channel_host.h" |
| 8 #include "content/common/gpu/gpu_messages.h" |
| 9 #include "ipc/ipc_message_macros.h" |
| 10 |
| 11 const int kNumCaptureBuffers = 2; |
| 12 |
| 13 namespace content { |
| 14 |
| 15 GLSurfaceCapturerHost::GLSurfaceCapturerHost(int32 capturer_route_id, |
| 16 SurfaceCapturer::Client* client, |
| 17 CommandBufferProxyImpl* impl) |
| 18 : thread_checker_(), |
| 19 capturer_route_id_(capturer_route_id), |
| 20 weak_this_factory_(this), |
| 21 client_ptr_factory_(client), |
| 22 client_(client), |
| 23 impl_(impl), |
| 24 channel_(impl_->channel()), |
| 25 next_frame_id_(0) { |
| 26 DCHECK(thread_checker_.CalledOnValidThread()); |
| 27 channel_->AddRoute(capturer_route_id_, weak_this_factory_.GetWeakPtr()); |
| 28 impl_->AddDeletionObserver(this); |
| 29 } |
| 30 |
| 31 void GLSurfaceCapturerHost::OnChannelError() { |
| 32 DLOG(ERROR) << "OnChannelError()"; |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 34 OnNotifyError(kPlatformFailureError); |
| 35 if (channel_) { |
| 36 weak_this_factory_.InvalidateWeakPtrs(); |
| 37 channel_->RemoveRoute(capturer_route_id_); |
| 38 channel_ = NULL; |
| 39 } |
| 40 } |
| 41 |
| 42 bool GLSurfaceCapturerHost::OnMessageReceived(const IPC::Message& message) { |
| 43 bool handled = true; |
| 44 IPC_BEGIN_MESSAGE_MAP(GLSurfaceCapturerHost, message) |
| 45 IPC_MESSAGE_HANDLER(SurfaceCapturerHostMsg_NotifyCaptureParameters, |
| 46 OnNotifyCaptureParameters) |
| 47 IPC_MESSAGE_HANDLER(SurfaceCapturerHostMsg_NotifyCopyCaptureDone, |
| 48 OnNotifyCopyCaptureDone) |
| 49 IPC_MESSAGE_HANDLER(SurfaceCapturerHostMsg_NotifyError, OnNotifyError) |
| 50 IPC_MESSAGE_UNHANDLED(handled = false) |
| 51 IPC_END_MESSAGE_MAP() |
| 52 return handled; |
| 53 } |
| 54 |
| 55 void GLSurfaceCapturerHost::Initialize(media::VideoFrame::Format format) { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 Send(new SurfaceCapturerMsg_Initialize(capturer_route_id_, format)); |
| 58 } |
| 59 |
| 60 void GLSurfaceCapturerHost::TryCapture() { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 Send(new SurfaceCapturerMsg_TryCapture(capturer_route_id_)); |
| 63 } |
| 64 |
| 65 void GLSurfaceCapturerHost::CopyCaptureToVideoFrame( |
| 66 const scoped_refptr<media::VideoFrame>& frame) { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 if (!channel_) |
| 69 return; |
| 70 if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) { |
| 71 DLOG(ERROR) << "CopyCaptureToVideoFrame(): cannot capture to frame not " |
| 72 "backed by shared memory"; |
| 73 OnNotifyError(kPlatformFailureError); |
| 74 return; |
| 75 } |
| 76 base::SharedMemoryHandle handle = |
| 77 channel_->ShareToGpuProcess(frame->shared_memory_handle()); |
| 78 if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) { |
| 79 DLOG(ERROR) << "CopyCaptureToVideoFrame(): failed to duplicate buffer " |
| 80 "handle for GPU process"; |
| 81 OnNotifyError(kPlatformFailureError); |
| 82 return; |
| 83 } |
| 84 |
| 85 const size_t plane_count = media::VideoFrame::NumPlanes(frame->format()); |
| 86 size_t frame_size = 0; |
| 87 for (size_t i = 0; i < plane_count; ++i) { |
| 88 DCHECK_EQ(reinterpret_cast<void*>(frame->data(i)), |
| 89 reinterpret_cast<void*>((frame->data(0) + frame_size))) |
| 90 << "plane=" << i; |
| 91 frame_size += frame->stride(i) * frame->rows(i); |
| 92 } |
| 93 |
| 94 Send(new SurfaceCapturerMsg_CopyCaptureToVideoFrame( |
| 95 capturer_route_id_, next_frame_id_, handle, frame_size)); |
| 96 frame_map_[next_frame_id_] = frame; |
| 97 next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF; |
| 98 } |
| 99 |
| 100 void GLSurfaceCapturerHost::Destroy() { |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 102 client_ = NULL; |
| 103 Send(new SurfaceCapturerMsg_Destroy(capturer_route_id_)); |
| 104 } |
| 105 |
| 106 void GLSurfaceCapturerHost::OnWillDeleteImpl() { |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 impl_ = NULL; |
| 109 OnChannelError(); |
| 110 } |
| 111 |
| 112 GLSurfaceCapturerHost::~GLSurfaceCapturerHost() { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 weak_this_factory_.InvalidateWeakPtrs(); |
| 115 if (channel_) |
| 116 channel_->RemoveRoute(capturer_route_id_); |
| 117 if (impl_) |
| 118 impl_->RemoveDeletionObserver(this); |
| 119 } |
| 120 |
| 121 void GLSurfaceCapturerHost::NotifyError(Error error) { |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); |
| 123 base::MessageLoopProxy::current()->PostTask( |
| 124 FROM_HERE, |
| 125 base::Bind(&SurfaceCapturer::Client::NotifyError, |
| 126 client_ptr_factory_.GetWeakPtr(), |
| 127 error)); |
| 128 } |
| 129 |
| 130 void GLSurfaceCapturerHost::OnNotifyCaptureParameters( |
| 131 const gfx::Size& buffer_size, |
| 132 const gfx::Rect& visible_rect) { |
| 133 DVLOG(2) << "OnNotifyCaptureParameters(): " |
| 134 "buffer_size=" << buffer_size.ToString() |
| 135 << ", visible_rect=" << visible_rect.ToString(); |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 if (client_) |
| 138 client_->NotifyCaptureParameters(buffer_size, visible_rect); |
| 139 } |
| 140 |
| 141 void GLSurfaceCapturerHost::OnNotifyCopyCaptureDone(int32 frame_id) { |
| 142 DVLOG(3) << "OnNotifyCopyCaptureDone(): frame_id=" << frame_id; |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 FrameMap::iterator iter = frame_map_.find(frame_id); |
| 145 if (iter == frame_map_.end()) { |
| 146 DLOG(ERROR) << "OnNotifyCopyCaptureDone(): invalid frame_id=" << frame_id; |
| 147 OnNotifyError(kPlatformFailureError); |
| 148 return; |
| 149 } |
| 150 if (client_) |
| 151 client_->NotifyCopyCaptureDone(iter->second); |
| 152 frame_map_.erase(iter); |
| 153 } |
| 154 |
| 155 void GLSurfaceCapturerHost::OnNotifyError(Error error) { |
| 156 DVLOG(2) << "OnNotifyError(): error=" << error; |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 if (client_) { |
| 159 client_->NotifyError(error); |
| 160 client_ = NULL; |
| 161 client_ptr_factory_.InvalidateWeakPtrs(); |
| 162 } |
| 163 } |
| 164 |
| 165 void GLSurfaceCapturerHost::Send(IPC::Message* message) { |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 167 uint32 type = message->type(); |
| 168 if (!channel_) { |
| 169 DLOG(ERROR) << "Send(): no channel"; |
| 170 delete message; |
| 171 NotifyError(kPlatformFailureError); |
| 172 } else if (!channel_->Send(message)) { |
| 173 DLOG(ERROR) << "Send(): failed: message->type()=" << type; |
| 174 NotifyError(kPlatformFailureError); |
| 175 } |
| 176 } |
| 177 |
| 178 } // namespace content |
OLD | NEW |