base/threading: remove ScopedTracker placed for experiments
[chromium-blink-merge.git] / content / common / gpu / client / gpu_video_encode_accelerator_host.cc
blobf61e390fcae7870f5ccfd0b44cddd606bd18ddcc
1 // Copyright 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.
5 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h"
7 #include "base/logging.h"
8 #include "content/common/gpu/client/gpu_channel_host.h"
9 #include "content/common/gpu/gpu_messages.h"
10 #include "content/common/gpu/media/gpu_video_accelerator_util.h"
11 #include "media/base/video_frame.h"
13 namespace content {
15 #define NOTIFY_ERROR(error) \
16 PostNotifyError(error); \
17 DLOG(ERROR)
19 GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost(
20 GpuChannelHost* channel,
21 CommandBufferProxyImpl* impl)
22 : channel_(channel),
23 encoder_route_id_(MSG_ROUTING_NONE),
24 client_(NULL),
25 impl_(impl),
26 next_frame_id_(0),
27 weak_this_factory_(this) {
28 DCHECK(channel_);
29 DCHECK(impl_);
30 impl_->AddDeletionObserver(this);
33 GpuVideoEncodeAcceleratorHost::~GpuVideoEncodeAcceleratorHost() {
34 DCHECK(CalledOnValidThread());
35 if (channel_ && encoder_route_id_ != MSG_ROUTING_NONE)
36 channel_->RemoveRoute(encoder_route_id_);
37 if (impl_)
38 impl_->RemoveDeletionObserver(this);
41 bool GpuVideoEncodeAcceleratorHost::OnMessageReceived(
42 const IPC::Message& message) {
43 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost, message)
45 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers,
46 OnRequireBitstreamBuffers)
47 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInputDone,
48 OnNotifyInputDone)
49 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady,
50 OnBitstreamBufferReady)
51 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError,
52 OnNotifyError)
53 IPC_MESSAGE_UNHANDLED(handled = false)
54 IPC_END_MESSAGE_MAP()
55 DCHECK(handled);
56 // See OnNotifyError for why |this| mustn't be used after OnNotifyError might
57 // have been called above.
58 return handled;
61 void GpuVideoEncodeAcceleratorHost::OnChannelError() {
62 DCHECK(CalledOnValidThread());
63 if (channel_) {
64 if (encoder_route_id_ != MSG_ROUTING_NONE)
65 channel_->RemoveRoute(encoder_route_id_);
66 channel_ = NULL;
68 NOTIFY_ERROR(kPlatformFailureError) << "OnChannelError()";
71 media::VideoEncodeAccelerator::SupportedProfiles
72 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() {
73 DCHECK(CalledOnValidThread());
74 if (!channel_)
75 return media::VideoEncodeAccelerator::SupportedProfiles();
76 return GpuVideoAcceleratorUtil::ConvertGpuToMediaEncodeProfiles(
77 channel_->gpu_info().video_encode_accelerator_supported_profiles);
80 bool GpuVideoEncodeAcceleratorHost::Initialize(
81 media::VideoPixelFormat input_format,
82 const gfx::Size& input_visible_size,
83 media::VideoCodecProfile output_profile,
84 uint32 initial_bitrate,
85 Client* client) {
86 DCHECK(CalledOnValidThread());
87 client_ = client;
88 if (!impl_) {
89 DLOG(ERROR) << "impl_ destroyed";
90 return false;
93 int32 route_id = channel_->GenerateRouteID();
94 channel_->AddRoute(route_id, weak_this_factory_.GetWeakPtr());
96 bool succeeded = false;
97 Send(new GpuCommandBufferMsg_CreateVideoEncoder(
98 impl_->route_id(), input_format, input_visible_size, output_profile,
99 initial_bitrate, route_id, &succeeded));
100 if (!succeeded) {
101 DLOG(ERROR) << "Send(GpuCommandBufferMsg_CreateVideoEncoder()) failed";
102 channel_->RemoveRoute(route_id);
103 return false;
105 encoder_route_id_ = route_id;
106 return true;
109 void GpuVideoEncodeAcceleratorHost::Encode(
110 const scoped_refptr<media::VideoFrame>& frame,
111 bool force_keyframe) {
112 DCHECK(CalledOnValidThread());
113 if (!channel_)
114 return;
116 if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) {
117 NOTIFY_ERROR(kPlatformFailureError) << "EncodeSharedMemory(): cannot "
118 "encode frame with invalid shared "
119 "memory handle";
120 return;
123 AcceleratedVideoEncoderMsg_Encode_Params params;
124 params.frame_id = next_frame_id_;
125 params.buffer_handle =
126 channel_->ShareToGpuProcess(frame->shared_memory_handle());
127 if (!base::SharedMemory::IsHandleValid(params.buffer_handle)) {
128 NOTIFY_ERROR(kPlatformFailureError) << "EncodeSharedMemory(): failed to "
129 "duplicate buffer handle for GPU "
130 "process";
131 return;
133 params.buffer_offset =
134 base::checked_cast<uint32_t>(frame->shared_memory_offset());
135 // We assume that planar frame data passed here is packed and contiguous.
136 base::CheckedNumeric<uint32_t> buffer_size = 0u;
137 for (size_t i = 0; i < media::VideoFrame::NumPlanes(frame->format()); ++i) {
138 // Cast DCHECK parameters to void* to avoid printing uint8* as a string.
139 DCHECK_EQ(
140 reinterpret_cast<void*>(frame->data(i)),
141 reinterpret_cast<void*>((frame->data(0) + buffer_size.ValueOrDie())))
142 << "plane=" << i;
143 buffer_size +=
144 base::checked_cast<uint32_t>(frame->stride(i) * frame->rows(i));
146 params.buffer_size = buffer_size.ValueOrDie();
147 params.force_keyframe = force_keyframe;
149 Send(new AcceleratedVideoEncoderMsg_Encode(encoder_route_id_, params));
150 frame_map_[next_frame_id_] = frame;
152 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
153 next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF;
156 void GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer(
157 const media::BitstreamBuffer& buffer) {
158 DCHECK(CalledOnValidThread());
159 if (!channel_)
160 return;
162 base::SharedMemoryHandle handle =
163 channel_->ShareToGpuProcess(buffer.handle());
164 if (!base::SharedMemory::IsHandleValid(handle)) {
165 NOTIFY_ERROR(kPlatformFailureError)
166 << "UseOutputBitstreamBuffer(): failed to duplicate buffer handle "
167 "for GPU process: buffer.id()=" << buffer.id();
168 return;
170 Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer(
171 encoder_route_id_, buffer.id(), handle, buffer.size()));
174 void GpuVideoEncodeAcceleratorHost::RequestEncodingParametersChange(
175 uint32 bitrate,
176 uint32 framerate) {
177 DCHECK(CalledOnValidThread());
178 if (!channel_)
179 return;
181 Send(new AcceleratedVideoEncoderMsg_RequestEncodingParametersChange(
182 encoder_route_id_, bitrate, framerate));
185 void GpuVideoEncodeAcceleratorHost::Destroy() {
186 DCHECK(CalledOnValidThread());
187 if (channel_)
188 Send(new AcceleratedVideoEncoderMsg_Destroy(encoder_route_id_));
189 client_ = NULL;
190 delete this;
193 void GpuVideoEncodeAcceleratorHost::OnWillDeleteImpl() {
194 DCHECK(CalledOnValidThread());
195 impl_ = NULL;
197 // The CommandBufferProxyImpl is going away; error out this VEA.
198 OnChannelError();
201 void GpuVideoEncodeAcceleratorHost::PostNotifyError(Error error) {
202 DCHECK(CalledOnValidThread());
203 DVLOG(2) << "PostNotifyError(): error=" << error;
204 // Post the error notification back to this thread, to avoid re-entrancy.
205 base::ThreadTaskRunnerHandle::Get()->PostTask(
206 FROM_HERE, base::Bind(&GpuVideoEncodeAcceleratorHost::OnNotifyError,
207 weak_this_factory_.GetWeakPtr(), error));
210 void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) {
211 DCHECK(CalledOnValidThread());
212 uint32 message_type = message->type();
213 if (!channel_->Send(message)) {
214 NOTIFY_ERROR(kPlatformFailureError) << "Send(" << message_type
215 << ") failed";
219 void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers(
220 uint32 input_count,
221 const gfx::Size& input_coded_size,
222 uint32 output_buffer_size) {
223 DCHECK(CalledOnValidThread());
224 DVLOG(2) << "OnRequireBitstreamBuffers(): input_count=" << input_count
225 << ", input_coded_size=" << input_coded_size.ToString()
226 << ", output_buffer_size=" << output_buffer_size;
227 if (client_) {
228 client_->RequireBitstreamBuffers(
229 input_count, input_coded_size, output_buffer_size);
233 void GpuVideoEncodeAcceleratorHost::OnNotifyInputDone(int32 frame_id) {
234 DCHECK(CalledOnValidThread());
235 DVLOG(3) << "OnNotifyInputDone(): frame_id=" << frame_id;
236 // Fun-fact: std::hash_map is not spec'd to be re-entrant; since freeing a
237 // frame can trigger a further encode to be kicked off and thus an .insert()
238 // back into the map, we separate the frame's dtor running from the .erase()
239 // running by holding on to the frame temporarily. This isn't "just
240 // theoretical" - Android's std::hash_map crashes if we don't do this.
241 scoped_refptr<media::VideoFrame> frame = frame_map_[frame_id];
242 if (!frame_map_.erase(frame_id)) {
243 DLOG(ERROR) << "OnNotifyInputDone(): "
244 "invalid frame_id=" << frame_id;
245 // See OnNotifyError for why this needs to be the last thing in this
246 // function.
247 OnNotifyError(kPlatformFailureError);
248 return;
250 frame = NULL; // Not necessary but nice to be explicit; see fun-fact above.
253 void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady(
254 int32 bitstream_buffer_id,
255 uint32 payload_size,
256 bool key_frame) {
257 DCHECK(CalledOnValidThread());
258 DVLOG(3) << "OnBitstreamBufferReady(): "
259 "bitstream_buffer_id=" << bitstream_buffer_id
260 << ", payload_size=" << payload_size
261 << ", key_frame=" << key_frame;
262 if (client_)
263 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame);
266 void GpuVideoEncodeAcceleratorHost::OnNotifyError(Error error) {
267 DCHECK(CalledOnValidThread());
268 DVLOG(2) << "OnNotifyError(): error=" << error;
269 if (!client_)
270 return;
271 weak_this_factory_.InvalidateWeakPtrs();
273 // Client::NotifyError() may Destroy() |this|, so calling it needs to be the
274 // last thing done on this stack!
275 media::VideoEncodeAccelerator::Client* client = NULL;
276 std::swap(client_, client);
277 client->NotifyError(error);
280 } // namespace content