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_decoder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "base/trace_event/trace_event.h" | |
13 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | |
14 #include "content/common/gpu/client/gpu_jpeg_decode_accelerator_host.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "media/base/video_frame.h" | |
17 | |
18 namespace content { | |
19 | |
20 // static | |
21 bool GpuJpegDecoder::Supported() { | |
22 // A lightweight check for caller to avoid IPC latency for known unsupported | |
23 // platform. Initialize() can do the real platform supporting check but it | |
24 // requires an IPC even for platforms that do not support HW decoder. | |
25 // TODO(kcwu): move this information to GpuInfo. | |
Pawel Osciak
2015/06/22 08:51:23
Please add a crbug for this.
kcwu
2015/06/23 13:58:24
Done.
| |
26 return false; | |
Pawel Osciak
2015/06/22 08:51:22
Are you planning to use a flag here for now?
kcwu
2015/06/22 09:18:48
Yes, there is another CL https://codereview.chromi
| |
27 } | |
28 | |
29 GpuJpegDecoder::GpuJpegDecoder(const DecodeDoneCB& decode_done_cb, | |
30 const ErrorCB& error_cb) | |
31 : decode_done_cb_(decode_done_cb), | |
32 error_cb_(error_cb), | |
33 next_bitstream_buffer_id_(0), | |
34 in_buffer_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId, | |
35 base::SharedMemory::NULLHandle(), | |
36 0) { | |
37 } | |
38 | |
39 GpuJpegDecoder::~GpuJpegDecoder() { | |
40 DCHECK(CalledOnValidThread()); | |
41 | |
42 // |decoder_| guarantees no more JpegDecodeAccelerator::Client callbacks | |
43 // on IO thread after deletion. | |
44 decoder_.reset(); | |
45 | |
46 // |gpu_channel_host_| should outlive |decoder_|, so |gpu_channel_host_| | |
47 // must be released after |decoder_| is been destroyed. | |
Pawel Osciak
2015/06/22 08:51:22
s/is/has/
kcwu
2015/06/23 13:58:24
Done.
| |
48 gpu_channel_host_ = nullptr; | |
49 } | |
50 | |
51 bool GpuJpegDecoder::IsDecoding_Locked() { | |
52 lock_.AssertAcquired(); | |
53 return !decode_done_closure_.is_null(); | |
54 } | |
55 | |
56 void GpuJpegDecoder::Initialize() { | |
57 DVLOG(3) << __func__; | |
58 DCHECK(CalledOnValidThread()); | |
59 | |
60 const scoped_refptr<base::SingleThreadTaskRunner> current_task_runner( | |
61 base::ThreadTaskRunnerHandle::Get()); | |
62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
63 base::Bind(&EstablishGpuChannelOnUIThread, | |
64 current_task_runner, AsWeakPtr())); | |
65 } | |
66 | |
67 // static | |
68 void GpuJpegDecoder::EstablishGpuChannelOnUIThread( | |
69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
70 base::WeakPtr<GpuJpegDecoder> weak_this) { | |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
72 DCHECK(BrowserGpuChannelHostFactory::instance()); | |
73 | |
74 BrowserGpuChannelHostFactory::instance()->EstablishGpuChannel( | |
75 CAUSE_FOR_GPU_LAUNCH_JPEGDECODEACCELERATOR_INITIALIZE, | |
76 base::Bind(&GpuJpegDecoder::GpuChannelEstablishedOnUIThread, task_runner, | |
77 weak_this)); | |
78 } | |
79 | |
80 // static | |
81 void GpuJpegDecoder::GpuChannelEstablishedOnUIThread( | |
82 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
83 base::WeakPtr<GpuJpegDecoder> weak_this) { | |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
85 | |
86 scoped_refptr<GpuChannelHost> gpu_channel_host( | |
87 BrowserGpuChannelHostFactory::instance()->GetGpuChannel()); | |
88 task_runner->PostTask(FROM_HERE, | |
89 base::Bind(&GpuJpegDecoder::InitializeDone, weak_this, | |
90 base::Passed(&gpu_channel_host))); | |
91 } | |
92 | |
93 void GpuJpegDecoder::InitializeDone( | |
94 scoped_refptr<GpuChannelHost> gpu_channel_host) { | |
95 DCHECK(CalledOnValidThread()); | |
96 if (!gpu_channel_host) { | |
97 LOG(ERROR) << "Failed to establish GPU channel for JPEG decoder"; | |
98 return; | |
99 } | |
100 gpu_channel_host_ = gpu_channel_host.Pass(); | |
101 | |
102 decoder_ = gpu_channel_host_->CreateJpegDecoder(this); | |
103 } | |
104 | |
105 void GpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { | |
106 DVLOG(3) << __func__; | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
108 TRACE_EVENT0("jpeg", "GpuJpegDecoder::VideoFrameReady"); | |
109 base::AutoLock lock(lock_); | |
110 | |
111 if (!IsDecoding_Locked()) { | |
112 LOG(ERROR) << "Got decode response while not decoding"; | |
113 return; | |
114 } | |
115 | |
116 if (bitstream_buffer_id != in_buffer_.id()) { | |
117 LOG(ERROR) << "Unexpected bitstream_buffer_id " << bitstream_buffer_id | |
Pawel Osciak
2015/06/22 08:51:23
Should we just store the id, instead of the whole
kcwu
2015/06/23 13:58:24
Replace in_buffer_ by in_buffer_id_. I don't use n
| |
118 << ", expected " << in_buffer_.id(); | |
119 return; | |
120 } | |
Pawel Osciak
2015/06/22 08:51:22
Should we clear in_buffer_ here?
kcwu
2015/06/23 13:58:24
Done.
| |
121 | |
122 decode_done_closure_.Run(); | |
123 decode_done_closure_.Reset(); | |
124 | |
125 TRACE_EVENT_ASYNC_END0("jpeg", "GpuJpegDecoder decoding", | |
126 bitstream_buffer_id); | |
127 } | |
128 | |
129 void GpuJpegDecoder::NotifyError(int32_t bitstream_buffer_id, | |
130 media::JpegDecodeAccelerator::Error error) { | |
131 DVLOG(3) << __func__; | |
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
133 LOG(ERROR) << "Decode error, bitstream_buffer_id=" << bitstream_buffer_id; | |
Pawel Osciak
2015/06/22 08:51:23
Should we print error here?
kcwu
2015/06/23 13:58:24
Done.
| |
134 | |
135 base::AutoLock lock(lock_); | |
136 decode_done_closure_.Reset(); | |
137 | |
138 error_cb_.Run( | |
139 base::StringPrintf("Decode error, bitstream_buffer_id=%d, error=%d", | |
Pawel Osciak
2015/06/22 08:51:23
s/Decode/JPEG Decode/
kcwu
2015/06/23 13:58:24
Done.
| |
140 bitstream_buffer_id, error)); | |
141 } | |
142 | |
143 void GpuJpegDecoder::DecodeCapturedData( | |
144 const uint8* data, | |
145 size_t in_buffer_size, | |
146 const media::VideoCaptureFormat& frame_format, | |
147 const base::TimeTicks& timestamp, | |
148 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> out_buffer) { | |
149 DCHECK(CalledOnValidThread()); | |
150 DCHECK(decoder_); | |
151 | |
152 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "GpuJpegDecoder decoding", | |
153 next_bitstream_buffer_id_); | |
154 TRACE_EVENT0("jpeg", "GpuJpegDecoder::DecodeCapturedData"); | |
155 | |
156 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough | |
157 // (say, if decoding time is longer than 16ms for 60fps 4k image) | |
Pawel Osciak
2015/06/22 08:51:23
I'd remove this, since 16ms would probably be too
kcwu
2015/06/22 09:18:48
Do you mean remove only line 157 or the whole TODO
| |
158 { | |
159 base::AutoLock lock(lock_); | |
160 if (IsDecoding_Locked()) { | |
161 DVLOG(1) << "Drop captured frame. Previous jpeg frame is still decoding"; | |
162 return; | |
163 } | |
164 } | |
165 | |
166 // Enlarge input buffer if necessary. | |
Pawel Osciak
2015/06/22 08:51:22
Why do we need a larger input buffer than the inpu
kcwu
2015/06/22 09:18:48
We are reusing the shared memory and want to avoid
| |
167 if (!in_shared_memory_.get() || | |
168 in_buffer_size > in_shared_memory_->mapped_size()) { | |
169 // Reserve 2x space to avoid frequent reallocations for initial frames. | |
170 const size_t reserved_size = 2 * in_buffer_size; | |
171 in_shared_memory_.reset(new base::SharedMemory); | |
172 if (!in_shared_memory_->CreateAndMapAnonymous(reserved_size)) { | |
173 base::AutoLock lock(lock_); | |
174 error_cb_.Run(base::StringPrintf("CreateAndMapAnonymous failed, size=%zd", | |
175 reserved_size)); | |
176 return; | |
177 } | |
178 } | |
179 memcpy(in_shared_memory_->memory(), data, in_buffer_size); | |
180 | |
181 // No need to lock for |in_buffer_| since IsDecoding_Locked() is false. | |
182 in_buffer_ = media::BitstreamBuffer( | |
183 next_bitstream_buffer_id_, in_shared_memory_->handle(), in_buffer_size); | |
184 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. | |
185 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
186 | |
187 const gfx::Size dimensions = frame_format.frame_size; | |
188 base::SharedMemoryHandle out_handle(out_buffer->AsPlatformFile(), true); | |
kcwu
2015/06/22 12:59:35
I found AsPlatformFile() become only available on
wuchengli
2015/06/25 07:42:27
mcasas had no time to reply this week. Since other
kcwu
2015/06/25 14:25:47
Done.
| |
189 scoped_refptr<media::VideoFrame> out_frame = | |
190 media::VideoFrame::WrapExternalSharedMemory( | |
191 media::VideoFrame::I420, // format | |
Pawel Osciak
2015/06/22 08:51:23
VideoCaptureFormat has a pixel_format member. Coul
kcwu
2015/06/22 09:18:48
Do you mean the argument |frame_format| ? If so, n
| |
192 dimensions, // coded_size | |
193 gfx::Rect(dimensions), // visible_rect | |
194 dimensions, // natural_size | |
195 static_cast<uint8*>(out_buffer->data()), // data | |
Pawel Osciak
2015/06/22 08:51:23
s/uint8/uin8_t/
kcwu
2015/06/23 13:58:24
Done.
| |
196 out_buffer->size(), // data_size | |
197 out_handle, // handle | |
198 0, // shared_memory_offset | |
199 base::TimeDelta()); // timestamp | |
Pawel Osciak
2015/06/22 08:51:23
Should we use timestamp from this method's argumen
kcwu
2015/06/22 09:18:48
The timestamp is propagated separatedly. VideoFram
| |
200 DCHECK(out_frame.get()); | |
Pawel Osciak
2015/06/22 08:51:23
I think we should handle this, instead of just DCH
kcwu
2015/06/23 13:58:24
Done.
| |
201 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | |
202 frame_format.frame_rate); | |
203 | |
204 { | |
205 base::AutoLock lock(lock_); | |
206 decode_done_closure_ = base::Bind( | |
207 decode_done_cb_, base::Passed(&out_buffer), out_frame, timestamp); | |
208 } | |
209 decoder_->Decode(in_buffer_, out_frame); | |
210 } | |
211 | |
212 } // namespace content | |
OLD | NEW |