OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "base/callback_helpers.h" | |
6 #include "base/logging.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/gpu/arc_gpu_video_decode_accelerator.h" | |
9 | |
10 // TODO use Pawel's factory instead | |
kcwu
2016/03/14 09:37:53
Remove this TODO?
owenlin_google
2016/03/16 06:20:51
Done.
| |
11 #include "content/public/common/gpu_video_decode_accelerator_factory.h" | |
12 | |
13 #undef DVLOG | |
14 #define DVLOG VLOG | |
15 | |
16 namespace chromeos { | |
17 namespace arc { | |
18 | |
19 ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord( | |
20 int32_t bitstream_buffer_id, | |
21 uint32_t buffer_index, | |
22 int64_t timestamp) | |
23 : bitstream_buffer_id(bitstream_buffer_id), | |
24 buffer_index(buffer_index), | |
25 timestamp(timestamp) {} | |
26 | |
27 ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator() | |
28 : pending_eos_output_buffer_(false), | |
29 arc_client_(nullptr), | |
30 next_bitstream_buffer_id_(0), | |
31 output_buffer_size_(0) {} | |
32 | |
33 ArcGpuVideoDecodeAccelerator::~ArcGpuVideoDecodeAccelerator() {} | |
34 | |
35 // An arbitrary chosen limit of the number of buffers. The number of | |
36 // buffers used is requested from the client side (less trusted). | |
37 static const size_t kMaxBufferCount = 128; | |
38 | |
39 bool ArcGpuVideoDecodeAccelerator::Initialize( | |
40 const Config& config, | |
41 ArcVideoAccelerator::Client* client) { | |
42 DVLOG(5) << "Initialize(device=" << config.device_type | |
43 << ", input_pixel_format=" << config.input_pixel_format | |
44 << ", num_input_buffers=" << config.num_input_buffers << ")"; | |
45 DCHECK(thread_checker_.CalledOnValidThread()); | |
46 if (config.device_type != DEVICE_DECODER) | |
47 return false; | |
48 DCHECK(client); | |
49 DCHECK(!arc_client_); | |
50 arc_client_ = client; | |
51 | |
52 if (config.num_input_buffers > kMaxBufferCount) { | |
53 DLOG(ERROR) << "Request too many buffers: " << config.num_input_buffers; | |
54 return false; | |
55 } | |
56 input_buffer_info_.resize(config.num_input_buffers); | |
57 | |
58 media::VideoDecodeAccelerator::Config vda_config; | |
59 switch (config.input_pixel_format) { | |
60 case HAL_PIXEL_FORMAT_H264: | |
61 vda_config.profile = media::H264PROFILE_MAIN; | |
62 break; | |
63 case HAL_PIXEL_FORMAT_VP8: | |
64 vda_config.profile = media::VP8PROFILE_ANY; | |
65 break; | |
66 default: | |
67 DLOG(ERROR) << "Unsupported input format: " << config.input_pixel_format; | |
68 return false; | |
69 } | |
70 vda_config.output_mode = media::VideoDecodeAccelerator::Config::OutputMode::IM PORT; | |
71 | |
72 scoped_ptr<content::GpuVideoDecodeAcceleratorFactory> vda_factory = | |
73 content::GpuVideoDecodeAcceleratorFactory::CreateWithNoGL(); | |
74 vda_ = std::move(vda_factory->CreateVDA(this, vda_config)); | |
75 if (!vda_) { | |
76 DLOG(ERROR) << "Failed to create VDA."; | |
77 return false; | |
78 } | |
79 return true; | |
80 } | |
81 | |
82 void ArcGpuVideoDecodeAccelerator::SetNumberOfOutputBuffers(size_t number) { | |
83 DVLOG(5) << "SetNumberOfOutputBuffers(" << number << ")"; | |
84 DCHECK(thread_checker_.CalledOnValidThread()); | |
85 | |
86 if (number > kMaxBufferCount) { | |
87 DLOG(ERROR) << "Too many buffers: " << number; | |
88 arc_client_->OnError(INVALID_ARGUMENT); | |
89 return; | |
90 } | |
91 | |
92 std::vector<media::PictureBuffer> buffers; | |
93 for (int32_t id = 0, n = number; id < n; ++id) { | |
94 // TODO: Make sure the |coded_size| is what we want. | |
95 buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); | |
96 } | |
97 vda_->AssignPictureBuffers(buffers); | |
98 | |
99 pending_import_buffer_.clear(); | |
100 pending_import_buffer_.resize(number); | |
101 } | |
102 | |
103 void ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, | |
104 uint32_t index, | |
105 int ashmem_fd, | |
106 off_t offset, | |
107 size_t length) { | |
108 DVLOG(5) << "ArcGVDA::BindSharedMemory, offset: " << offset | |
109 << ", length: " << length; | |
110 DCHECK(thread_checker_.CalledOnValidThread()); | |
111 | |
112 // Make sure we will close the file descriptor. | |
113 base::ScopedFD handle(ashmem_fd); | |
114 if (port != PORT_INPUT) { | |
115 DLOG(ERROR) << "SharedBuffer is only supported for input"; | |
116 arc_client_->OnError(INVALID_ARGUMENT); | |
117 return; | |
118 } | |
119 if (!ValidatePortAndIndex(port, index)) { | |
120 arc_client_->OnError(INVALID_ARGUMENT); | |
121 return; | |
122 } | |
123 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
124 input_info->handle = std::move(handle); | |
125 input_info->offset = offset; | |
126 input_info->length = length; | |
127 } | |
128 | |
129 void ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, | |
130 uint32_t index, | |
131 int dmabuf_fd) { | |
132 DCHECK(thread_checker_.CalledOnValidThread()); | |
133 | |
134 // Make sure we will close the file descriptor. | |
135 base::ScopedFD handle(dmabuf_fd); | |
136 if (port != PORT_OUTPUT) { | |
137 DLOG(ERROR) << "GraphicBuffer is only supported for input"; | |
138 arc_client_->OnError(INVALID_ARGUMENT); | |
139 return; | |
140 } | |
141 if (!ValidatePortAndIndex(port, index)) { | |
142 arc_client_->OnError(INVALID_ARGUMENT); | |
143 return; | |
144 } | |
145 pending_import_buffer_[index] = std::move(handle); | |
146 } | |
147 | |
148 void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, | |
149 uint32_t index, | |
150 const BufferMetadata& metadata) { | |
151 DVLOG(5) << "UseBuffer(port=" << port << ", index=" << index | |
152 << ", metadata=(bytes_used=" << metadata.bytes_used | |
153 << ", timestamp=" << metadata.timestamp << ")"; | |
154 DCHECK(thread_checker_.CalledOnValidThread()); | |
155 if (!ValidatePortAndIndex(port, index)) { | |
156 arc_client_->OnError(INVALID_ARGUMENT); | |
157 return; | |
158 } | |
159 switch (port) { | |
160 case PORT_INPUT: { | |
161 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
162 int32_t bitstream_buffer_id = next_bitstream_buffer_id_; | |
163 // Mask against 30 bits, to avoid (undefined) wraparound on signed | |
164 // integer. | |
165 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
166 SetInputRecord(bitstream_buffer_id, index, metadata.timestamp); | |
167 int dup_fd = HANDLE_EINTR(dup(input_info->handle.get())); | |
168 if (dup_fd < 0) { | |
169 DLOG(ERROR) << "dup() failed."; | |
170 arc_client_->OnError(PLATFORM_FAILURE); | |
171 return; | |
172 } | |
173 vda_->Decode(media::BitstreamBuffer( | |
174 bitstream_buffer_id, base::SharedMemoryHandle(dup_fd, true), | |
175 metadata.bytes_used, input_info->offset)); | |
176 if (metadata.flags & BUFFER_FLAG_EOS) { | |
177 vda_->Flush(true); | |
178 } | |
179 break; | |
180 } | |
181 case PORT_OUTPUT: { | |
182 SendEosIfNeededOrReusePicture(index); | |
183 break; | |
184 } | |
185 default: | |
186 NOTREACHED(); | |
187 } | |
188 } | |
189 | |
190 void ArcGpuVideoDecodeAccelerator::Reset() { | |
191 DCHECK(thread_checker_.CalledOnValidThread()); | |
192 base::RunLoop loop; | |
193 reset_done_callback_ = loop.QuitClosure(); | |
194 DCHECK(vda_); | |
195 vda_->Reset(); | |
196 base::MessageLoop::ScopedNestableTaskAllower allow( | |
197 base::MessageLoop::current()); | |
198 // Wait for the ResetDone callback. | |
199 loop.Run(); | |
200 } | |
201 | |
202 void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( | |
203 size_t requested_num_of_buffers, | |
204 const gfx::Size& dimensions, | |
205 uint32_t texture_target) { | |
206 DVLOG(5) << "ProvidePictureBuffers(" | |
207 << "requested_num_of_buffers=" << requested_num_of_buffers | |
208 << ", dimensions=" << dimensions.ToString() << ")"; | |
209 DCHECK(thread_checker_.CalledOnValidThread()); | |
210 coded_size_ = dimensions; | |
211 // TODO(owenlin): use VDA::GetOutputFormat() here and calculate correct | |
212 // |buffer_size|. | |
213 VideoFormat video_format; | |
214 video_format.buffer_size = dimensions.GetArea() * 3 / 2; | |
215 output_buffer_size_ = video_format.buffer_size; | |
216 video_format.min_num_buffers = requested_num_of_buffers; | |
217 video_format.coded_width = dimensions.width(); | |
218 video_format.coded_height = dimensions.height(); | |
219 // TODO(owenlin): How to get visible size? | |
220 video_format.crop_top = 0; | |
221 video_format.crop_left = 0; | |
222 video_format.crop_width = dimensions.width(); | |
223 video_format.crop_height = dimensions.height(); | |
224 arc_client_->OnOutputFormatChanged(video_format); | |
225 } | |
226 | |
227 void ArcGpuVideoDecodeAccelerator::DismissPictureBuffer( | |
228 int32_t picture_buffer) { | |
229 // no-op | |
230 } | |
231 | |
232 void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { | |
233 DVLOG(5) << "PictureReady(picture_buffer_id=" << picture.picture_buffer_id() | |
234 << ", bitstream_buffer_id=" << picture.bitstream_buffer_id(); | |
235 DCHECK(thread_checker_.CalledOnValidThread()); | |
236 | |
237 // Empty buffer, returned in Flushing. | |
238 if (picture.bitstream_buffer_id() == -1) { | |
239 buffers_pending_eos_.push(picture.picture_buffer_id()); | |
240 return; | |
241 } | |
242 InputRecord* input_record = FindInputRecord(picture.bitstream_buffer_id()); | |
243 if (input_record == nullptr) { | |
244 DLOG(ERROR) << "Cannot find for bitstream buffer id: " | |
245 << picture.bitstream_buffer_id(); | |
246 arc_client_->OnError(PLATFORM_FAILURE); | |
247 return; | |
248 } | |
249 | |
250 BufferMetadata metadata; | |
251 metadata.timestamp = input_record->timestamp; | |
252 metadata.bytes_used = output_buffer_size_; | |
253 arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); | |
254 } | |
255 | |
256 void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
257 int32_t bitstream_buffer_id) { | |
258 DVLOG(5) << "NotifyEndOfBitstreamBuffer(" << bitstream_buffer_id << ")"; | |
259 DCHECK(thread_checker_.CalledOnValidThread()); | |
260 InputRecord* input_record = FindInputRecord(bitstream_buffer_id); | |
261 if (input_record == nullptr) { | |
262 arc_client_->OnError(PLATFORM_FAILURE); | |
263 return; | |
264 } | |
265 arc_client_->OnBufferDone(PORT_INPUT, input_record->buffer_index, | |
266 BufferMetadata()); | |
267 } | |
268 | |
269 void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { | |
270 DCHECK(thread_checker_.CalledOnValidThread()); | |
271 pending_eos_output_buffer_ = true; | |
272 while (!buffers_pending_eos_.empty()) { | |
273 SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); | |
274 buffers_pending_eos_.pop(); | |
275 } | |
276 } | |
277 | |
278 void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { | |
279 DCHECK(thread_checker_.CalledOnValidThread()); | |
280 base::ResetAndReturn(&reset_done_callback_).Run(); | |
281 } | |
282 | |
283 static ArcVideoAccelerator::Error ConvertErrorCode( | |
284 media::VideoDecodeAccelerator::Error error) { | |
285 switch (error) { | |
286 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
287 return ArcVideoAccelerator::ILLEGAL_STATE; | |
288 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
289 return ArcVideoAccelerator::INVALID_ARGUMENT; | |
290 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
291 return ArcVideoAccelerator::UNREADABLE_INPUT; | |
292 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
293 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
294 default: | |
295 DLOG(ERROR) << "Unknown error: " << error; | |
296 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
297 } | |
298 } | |
299 | |
300 void ArcGpuVideoDecodeAccelerator::NotifyError( | |
301 media::VideoDecodeAccelerator::Error error) { | |
302 DCHECK(thread_checker_.CalledOnValidThread()); | |
303 DLOG(ERROR) << "Error notified: " << error; | |
304 arc_client_->OnError(ConvertErrorCode(error)); | |
305 } | |
306 | |
kcwu
2016/03/14 09:37:53
extra blank line
owenlin_google
2016/03/16 06:20:51
Done.
| |
307 | |
308 void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( | |
309 uint32_t index) { | |
310 if (pending_eos_output_buffer_) { | |
311 BufferMetadata metadata; | |
312 metadata.flags = BUFFER_FLAG_EOS; | |
313 arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); | |
314 pending_eos_output_buffer_ = false; | |
315 } else { | |
316 if (pending_import_buffer_[index].is_valid()) { | |
317 std::vector<gfx::GpuMemoryBufferHandle> buffers; | |
318 buffers.push_back(gfx::GpuMemoryBufferHandle()); | |
319 buffers.back().native_pixmap_handle.fd = base::FileDescriptor( | |
320 pending_import_buffer_[index].release(), true); | |
321 vda_->ImportBufferForPicture(index, buffers); | |
322 DCHECK(!pending_import_buffer_[index].is_valid()); | |
323 } else { | |
324 vda_->ReusePictureBuffer(index); | |
325 } | |
326 } | |
327 } | |
328 | |
329 void ArcGpuVideoDecodeAccelerator::SetInputRecord(int32_t bitstream_buffer_id, | |
330 uint32_t buffer_index, | |
331 int64_t timestamp) { | |
332 input_records_.push_front( | |
333 InputRecord(bitstream_buffer_id, buffer_index, timestamp)); | |
334 // The same value copied from media::GpuVideoDecoder. | |
335 const size_t kMaxNumberOfInputRecords = 128; | |
336 if (input_records_.size() > kMaxNumberOfInputRecords) | |
337 input_records_.pop_back(); | |
338 } | |
339 | |
340 ArcGpuVideoDecodeAccelerator::InputRecord* | |
341 ArcGpuVideoDecodeAccelerator::FindInputRecord(int32_t bitstream_buffer_id) { | |
342 for (auto& record : input_records_) { | |
343 if (record.bitstream_buffer_id == bitstream_buffer_id) | |
344 return &record; | |
345 } | |
346 return nullptr; | |
347 } | |
348 | |
349 bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, | |
350 uint32_t index) { | |
351 switch (port) { | |
352 case PORT_INPUT: | |
353 if (index >= input_buffer_info_.size()) { | |
354 DLOG(ERROR) << "Invalid index: " << index; | |
355 return false; | |
356 } | |
357 return true; | |
358 case PORT_OUTPUT: | |
359 if (index >= pending_import_buffer_.size()) { | |
360 DLOG(ERROR) << "Invalid index: " << index; | |
361 return false; | |
362 } | |
363 return true; | |
364 default: | |
365 DLOG(ERROR) << "Invalid port: " << port; | |
366 return false; | |
367 } | |
368 } | |
369 | |
370 } // namespace arc | |
371 } // namespace chromeos | |
OLD | NEW |