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