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