OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/browser/renderer_host/gpu_jpeg_decode_accelerator_adapter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | |
10 #include "content/common/gpu/client/gpu_jpeg_decode_accelerator_host.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "media/base/video_frame.h" | |
13 | |
14 namespace content { | |
15 | |
16 // static | |
17 bool GpuJpegDecodeAcceleratorAdapter::Supported() { | |
18 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) | |
19 return true; | |
20 #endif | |
21 return false; | |
22 } | |
23 | |
24 GpuJpegDecodeAcceleratorAdapter::GpuJpegDecodeAcceleratorAdapter( | |
25 media::VideoCaptureDevice::Client* device_client) | |
26 : device_client_(device_client), | |
27 device_thread_(base::MessageLoopProxy::current()), | |
28 fail_state_(NOT_FAIL), | |
29 next_bitstream_buffer_id_(0), | |
30 in_buffer_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId, | |
31 base::SharedMemory::NULLHandle(), | |
32 0) { | |
33 } | |
34 | |
35 GpuJpegDecodeAcceleratorAdapter::~GpuJpegDecodeAcceleratorAdapter() { | |
36 DCHECK(device_thread_->BelongsToCurrentThread()); | |
37 } | |
38 | |
39 bool GpuJpegDecodeAcceleratorAdapter::IsDecoding() { | |
40 base::AutoLock lock(lock_); | |
41 return out_frame_ != nullptr; | |
42 } | |
43 | |
44 bool GpuJpegDecodeAcceleratorAdapter::IsExpectedDecodeResponse( | |
45 int32 bitstream_buffer_id) { | |
46 if (bitstream_buffer_id != | |
47 media::JpegDecodeAccelerator::kInvalidBitstreamBufferId) { | |
48 if (!IsDecoding()) { | |
49 LOG(ERROR) << "Got decode response while not decoding"; | |
50 return false; | |
51 } | |
52 if (bitstream_buffer_id != in_buffer_.id()) { | |
53 LOG(ERROR) << "Unexpected bitstream_buffer_id " << bitstream_buffer_id | |
54 << ", expected " << in_buffer_.id(); | |
55 return false; | |
56 } | |
57 } | |
58 return true; | |
59 } | |
60 | |
61 void GpuJpegDecodeAcceleratorAdapter::DecodeDone() { | |
62 base::AutoLock lock(lock_); | |
63 memset(&captured_data_, 0, sizeof(captured_data_)); | |
64 out_buffer_ = nullptr; | |
65 out_frame_ = nullptr; | |
66 } | |
67 | |
68 bool GpuJpegDecodeAcceleratorAdapter::Initialize() { | |
69 DCHECK(device_thread_->BelongsToCurrentThread()); | |
70 DVLOG(3) << __func__; | |
wuchengli
2015/04/21 06:05:37
Let's call DVLOG first so we can know what functio
kcwu
2015/04/22 14:43:33
Done.
| |
71 GpuChannelHost* const host = | |
72 BrowserGpuChannelHostFactory::instance()->GetGpuChannel(); | |
73 decoder_ = host->CreateJpegDecoder( | |
74 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
75 if (!decoder_->Initialize(this)) { | |
76 decoder_.reset(); | |
77 fail_state_ = FAILED; | |
78 return false; | |
79 } | |
80 return true; | |
81 } | |
82 | |
83 bool GpuJpegDecodeAcceleratorAdapter::IsFailed() { | |
84 base::AutoLock lock(lock_); | |
85 return fail_state_ == FAILED; | |
86 } | |
87 | |
88 void GpuJpegDecodeAcceleratorAdapter::VideoFrameReady( | |
89 int32_t bitstream_buffer_id) { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
91 DVLOG(3) << __func__; | |
92 | |
93 // Verify parameter from GPU process. | |
mcasas
2015/04/22 01:23:52
Please remove superfluous comment.
kcwu
2015/04/22 14:43:33
Done.
| |
94 if (!IsExpectedDecodeResponse(bitstream_buffer_id)) | |
95 return; | |
96 | |
97 device_client_->OnIncomingCapturedVideoFrame(out_buffer_, out_frame_, | |
98 captured_data_.timestamp); | |
99 | |
100 DecodeDone(); | |
101 } | |
102 | |
103 void GpuJpegDecodeAcceleratorAdapter::FallbackToSoftwareDecode() { | |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) || | |
105 device_thread_->BelongsToCurrentThread()); | |
106 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) | |
107 DCHECK(IsDecoding()); | |
108 else | |
109 DCHECK(!IsDecoding()); | |
110 DVLOG(1) << "Fallback to software decode"; | |
111 | |
112 CapturedData captured_data; | |
113 { | |
114 base::AutoLock lock(lock_); | |
115 // See comment below. | |
116 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) | |
117 fail_state_ = FAILING; | |
118 else | |
119 fail_state_ = FAILED; | |
120 captured_data = captured_data_; | |
121 } | |
122 | |
123 // If we are on device thread, OnIncomingCapturedData is reentrant. But it's | |
124 // okay since |fail_state_| is FAILED and it won't go hardware decoding route | |
125 // this time. | |
126 // If we are on IO thread and next frame is coming on device thread, the next | |
127 // frame will call DecodeCapturedData() and drop immediately due to | |
128 // |fail_state_| is FAILING. This is to avoid next frame decoded before this | |
129 // frame. | |
130 device_client_->OnIncomingCapturedData(captured_data.data, | |
131 captured_data.length, | |
132 captured_data.frame_format, | |
133 captured_data.rotation, | |
134 captured_data.timestamp); | |
135 { | |
136 base::AutoLock lock(lock_); | |
137 fail_state_ = FAILED; | |
138 } | |
139 } | |
140 | |
141 void GpuJpegDecodeAcceleratorAdapter::NotifyError( | |
142 int32_t bitstream_buffer_id, | |
143 media::JpegDecodeAccelerator::Error error) { | |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
145 DVLOG(3) << __func__; | |
146 | |
147 // Verify parameter from GPU process. | |
148 if (!IsExpectedDecodeResponse(bitstream_buffer_id)) | |
149 return; | |
150 | |
151 // Error not associated to any bitstream buffer. For example, GPU channel | |
152 // is broken. | |
153 if (bitstream_buffer_id == | |
154 media::JpegDecodeAccelerator::kInvalidBitstreamBufferId) { | |
155 { | |
156 base::AutoLock lock(lock_); | |
157 fail_state_ = FAILED; | |
158 } | |
159 DecodeDone(); | |
160 return; | |
161 } | |
162 | |
163 FallbackToSoftwareDecode(); | |
164 DecodeDone(); | |
165 } | |
166 | |
167 void GpuJpegDecodeAcceleratorAdapter::DecodeCapturedData( | |
168 const uint8* data, | |
169 int length, | |
170 const media::VideoCaptureFormat& frame_format, | |
171 int rotation, | |
172 const base::TimeTicks& timestamp) { | |
173 DCHECK(device_thread_->BelongsToCurrentThread()); | |
174 DCHECK(decoder_); | |
175 if (IsDecoding()) { | |
176 DVLOG(1) << "Drop captured frame. Previous jpeg frame is still decoding"; | |
177 return; | |
178 } | |
179 DCHECK(!IsFailed()); | |
180 | |
181 const gfx::Size dimensions = frame_format.frame_size; | |
182 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> out_buffer = | |
183 device_client_->ReserveOutputBuffer(media::PIXEL_FORMAT_I420, dimensions); | |
184 if (!out_buffer.get()) { | |
185 DVLOG(1) << "Drop captured frame. No available buffers" | |
186 << " (all buffers are still queued to display)"; | |
187 return; | |
188 } | |
189 | |
190 CapturedData captured_data; | |
191 captured_data.data = const_cast<uint8*>(data); | |
192 captured_data.length = length; | |
193 captured_data.frame_format = frame_format; | |
194 captured_data.rotation = rotation; | |
195 captured_data.timestamp = timestamp; | |
196 | |
197 size_t in_buffer_size = captured_data.length; | |
198 | |
199 scoped_ptr<base::SharedMemory> in_shared_memory; | |
200 { | |
201 // Swap out to local variable in order to reduce lock time on IO thread. | |
202 base::AutoLock lock(lock_); | |
203 std::swap(in_shared_memory, in_shared_memory_); | |
204 } | |
205 // Enlarge input buffer if necessary. | |
206 if (!in_shared_memory.get() || | |
207 in_buffer_size > in_shared_memory->mapped_size()) { | |
208 in_shared_memory.reset(new base::SharedMemory); | |
209 if (!in_shared_memory->CreateAnonymous(in_buffer_size) || | |
210 !in_shared_memory->Map(in_buffer_size)) { | |
mcasas
2015/04/22 01:23:53
CreateAndMapAnonymous() [1]
[1] https://code.goog
kcwu
2015/04/22 14:43:33
Done.
| |
211 DLOG(ERROR) << "Create shared memory failed"; | |
212 captured_data_ = captured_data; | |
213 FallbackToSoftwareDecode(); | |
214 return; | |
215 } | |
216 } | |
217 | |
218 media::BitstreamBuffer in_buffer(next_bitstream_buffer_id_, | |
219 in_shared_memory->handle(), in_buffer_size); | |
220 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. | |
221 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
222 | |
223 scoped_refptr<media::VideoFrame> out_frame = | |
224 media::VideoFrame::WrapExternalPackedMemory( | |
225 media::VideoFrame::I420, | |
226 dimensions, | |
227 gfx::Rect(dimensions), | |
228 dimensions, | |
229 reinterpret_cast<uint8*>(out_buffer->data()), | |
230 out_buffer->size(), | |
231 out_buffer->handle(), | |
232 0, | |
233 base::TimeDelta(), | |
234 base::Closure()); | |
235 DCHECK(out_frame.get()); | |
236 | |
237 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | |
238 captured_data.frame_format.frame_rate); | |
239 | |
240 memcpy(in_shared_memory->memory(), captured_data.data, in_buffer_size); | |
241 captured_data.data = reinterpret_cast<uint8*>(in_shared_memory->memory()); | |
242 | |
243 base::AutoLock lock(lock_); | |
244 captured_data_ = captured_data; | |
245 in_buffer_ = in_buffer; | |
246 std::swap(in_shared_memory_, in_shared_memory); | |
247 out_buffer_ = out_buffer; | |
248 out_frame_ = out_frame; | |
249 | |
250 decoder_->Decode(in_buffer_, out_frame_); | |
251 } | |
252 | |
253 } // namespace content | |
OLD | NEW |