OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" | 5 #include "media/gpu/ipc/service/gpu_jpeg_decode_accelerator.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | |
13 #include "base/command_line.h" | 12 #include "base/command_line.h" |
14 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
15 #include "base/logging.h" | 14 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
17 #include "base/memory/shared_memory.h" | 16 #include "base/memory/shared_memory.h" |
18 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
21 #include "build/build_config.h" | 20 #include "build/build_config.h" |
22 #include "gpu/ipc/service/gpu_channel.h" | |
23 #include "ipc/ipc_message_macros.h" | |
24 #include "ipc/message_filter.h" | |
25 #include "media/base/media_switches.h" | 21 #include "media/base/media_switches.h" |
26 #include "media/filters/jpeg_parser.h" | 22 #include "media/filters/jpeg_parser.h" |
27 #include "media/gpu/fake_jpeg_decode_accelerator.h" | 23 #include "media/gpu/fake_jpeg_decode_accelerator.h" |
28 #include "media/gpu/ipc/common/media_messages.h" | 24 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 25 #include "mojo/public/cpp/system/platform_handle.h" |
29 #include "ui/gfx/geometry/size.h" | 26 #include "ui/gfx/geometry/size.h" |
30 | 27 |
31 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
32 #if defined(ARCH_CPU_X86_FAMILY) | 29 #if defined(ARCH_CPU_X86_FAMILY) |
33 #include "media/gpu/vaapi_jpeg_decode_accelerator.h" | 30 #include "media/gpu/vaapi_jpeg_decode_accelerator.h" |
34 #endif | 31 #endif |
35 #if defined(USE_V4L2_CODEC) && defined(ARCH_CPU_ARM_FAMILY) | 32 #if defined(USE_V4L2_CODEC) && defined(ARCH_CPU_ARM_FAMILY) |
36 #include "media/gpu/v4l2_device.h" | 33 #include "media/gpu/v4l2_device.h" |
37 #include "media/gpu/v4l2_jpeg_decode_accelerator.h" | 34 #include "media/gpu/v4l2_jpeg_decode_accelerator.h" |
38 #endif | 35 #endif |
(...skipping 30 matching lines...) Expand all Loading... |
69 return base::MakeUnique<media::FakeJpegDecodeAccelerator>( | 66 return base::MakeUnique<media::FakeJpegDecodeAccelerator>( |
70 std::move(io_task_runner)); | 67 std::move(io_task_runner)); |
71 } | 68 } |
72 | 69 |
73 void DecodeFinished(std::unique_ptr<base::SharedMemory> shm) { | 70 void DecodeFinished(std::unique_ptr<base::SharedMemory> shm) { |
74 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this | 71 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this |
75 // function is to just keep reference of |shm| to make sure it lives until | 72 // function is to just keep reference of |shm| to make sure it lives until |
76 // decode finishes. | 73 // decode finishes. |
77 } | 74 } |
78 | 75 |
79 bool VerifyDecodeParams(const AcceleratedJpegDecoderMsg_Decode_Params& params) { | 76 bool VerifyDecodeParams(const gfx::Size& coded_size, |
| 77 mojo::ScopedSharedBufferHandle* output_handle, |
| 78 uint32_t output_buffer_size) { |
80 const int kJpegMaxDimension = UINT16_MAX; | 79 const int kJpegMaxDimension = UINT16_MAX; |
81 if (params.coded_size.IsEmpty() || | 80 if (coded_size.IsEmpty() || coded_size.width() > kJpegMaxDimension || |
82 params.coded_size.width() > kJpegMaxDimension || | 81 coded_size.height() > kJpegMaxDimension) { |
83 params.coded_size.height() > kJpegMaxDimension) { | 82 LOG(ERROR) << "invalid coded_size " << coded_size.ToString(); |
84 LOG(ERROR) << "invalid coded_size " << params.coded_size.ToString(); | |
85 return false; | 83 return false; |
86 } | 84 } |
87 | 85 |
88 if (!base::SharedMemory::IsHandleValid(params.output_video_frame_handle)) { | 86 if (!output_handle->is_valid()) { |
89 LOG(ERROR) << "invalid output_video_frame_handle"; | 87 LOG(ERROR) << "invalid output_handle"; |
90 return false; | 88 return false; |
91 } | 89 } |
92 | 90 |
93 if (params.output_buffer_size < | 91 if (output_buffer_size < |
94 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, | 92 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, coded_size)) { |
95 params.coded_size)) { | 93 LOG(ERROR) << "output_buffer_size is too small: " << output_buffer_size; |
96 LOG(ERROR) << "output_buffer_size is too small: " | |
97 << params.output_buffer_size; | |
98 return false; | 94 return false; |
99 } | 95 } |
100 | 96 |
101 return true; | 97 return true; |
102 } | 98 } |
103 | 99 |
104 } // namespace | 100 } // namespace |
105 | 101 |
106 namespace media { | 102 namespace media { |
107 | 103 |
108 class GpuJpegDecodeAccelerator::Client : public JpegDecodeAccelerator::Client { | |
109 public: | |
110 Client(base::WeakPtr<GpuJpegDecodeAccelerator> owner, | |
111 int32_t route_id, | |
112 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | |
113 : owner_(std::move(owner)), | |
114 route_id_(route_id), | |
115 io_task_runner_(std::move(io_task_runner)) { | |
116 DCHECK(thread_checker_.CalledOnValidThread()); | |
117 } | |
118 | |
119 ~Client() override { DCHECK(thread_checker_.CalledOnValidThread()); } | |
120 | |
121 // JpegDecodeAccelerator::Client implementation. | |
122 void VideoFrameReady(int32_t bitstream_buffer_id) override { | |
123 DCHECK(thread_checker_.CalledOnValidThread()); | |
124 if (owner_) | |
125 owner_->NotifyDecodeStatus(route_id_, bitstream_buffer_id, | |
126 JpegDecodeAccelerator::NO_ERRORS); | |
127 } | |
128 | |
129 void NotifyError(int32_t bitstream_buffer_id, | |
130 JpegDecodeAccelerator::Error error) override { | |
131 DCHECK(thread_checker_.CalledOnValidThread()); | |
132 if (owner_) | |
133 owner_->NotifyDecodeStatus(route_id_, bitstream_buffer_id, error); | |
134 } | |
135 | |
136 void Decode(const BitstreamBuffer& bitstream_buffer, | |
137 const scoped_refptr<VideoFrame>& video_frame) { | |
138 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
139 DCHECK(accelerator_); | |
140 accelerator_->Decode(bitstream_buffer, video_frame); | |
141 } | |
142 | |
143 void set_accelerator(std::unique_ptr<JpegDecodeAccelerator> accelerator) { | |
144 DCHECK(thread_checker_.CalledOnValidThread()); | |
145 accelerator_ = std::move(accelerator); | |
146 } | |
147 | |
148 private: | |
149 base::ThreadChecker thread_checker_; | |
150 base::WeakPtr<GpuJpegDecodeAccelerator> owner_; | |
151 const int32_t route_id_; | |
152 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
153 std::unique_ptr<JpegDecodeAccelerator> accelerator_; | |
154 }; | |
155 | |
156 // Create, destroy, and RemoveClient run on child thread. All other methods run | |
157 // on IO thread. | |
158 // TODO(chfremer): Migrate this to Mojo. See https://crbug.com/699255 | |
159 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { | |
160 public: | |
161 explicit MessageFilter(GpuJpegDecodeAccelerator* owner) | |
162 : owner_(owner->AsWeakPtr()), | |
163 child_task_runner_(owner_->child_task_runner_), | |
164 io_task_runner_(owner_->io_task_runner_) {} | |
165 | |
166 void OnChannelError() override { sender_ = nullptr; } | |
167 | |
168 void OnChannelClosing() override { sender_ = nullptr; } | |
169 | |
170 void OnFilterAdded(IPC::Channel* channel) override { sender_ = channel; } | |
171 | |
172 bool OnMessageReceived(const IPC::Message& msg) override { | |
173 const int32_t route_id = msg.routing_id(); | |
174 if (client_map_.find(route_id) == client_map_.end()) | |
175 return false; | |
176 | |
177 bool handled = true; | |
178 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MessageFilter, msg, &route_id) | |
179 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Decode, OnDecodeOnIOThread) | |
180 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, | |
181 OnDestroyOnIOThread) | |
182 IPC_MESSAGE_UNHANDLED(handled = false) | |
183 IPC_END_MESSAGE_MAP() | |
184 return handled; | |
185 } | |
186 | |
187 bool SendOnIOThread(IPC::Message* message) { | |
188 DCHECK(!message->is_sync()); | |
189 if (!sender_) { | |
190 delete message; | |
191 return false; | |
192 } | |
193 return sender_->Send(message); | |
194 } | |
195 | |
196 void AddClientOnIOThread(int32_t route_id, | |
197 Client* client, | |
198 base::Callback<void(bool)> response) { | |
199 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
200 DCHECK(client_map_.count(route_id) == 0); | |
201 | |
202 // See the comment on GpuJpegDecodeAccelerator::AddClient. | |
203 client_map_[route_id] = base::WrapUnique(client); | |
204 response.Run(true); | |
205 } | |
206 | |
207 void OnDestroyOnIOThread(const int32_t* route_id) { | |
208 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
209 const auto& it = client_map_.find(*route_id); | |
210 DCHECK(it != client_map_.end()); | |
211 std::unique_ptr<Client> client = std::move(it->second); | |
212 DCHECK(client); | |
213 client_map_.erase(it); | |
214 | |
215 child_task_runner_->PostTask( | |
216 FROM_HERE, | |
217 base::Bind(&MessageFilter::DestroyClient, this, base::Passed(&client))); | |
218 } | |
219 | |
220 void DestroyClient(std::unique_ptr<Client> client) { | |
221 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
222 if (owner_) | |
223 owner_->ClientRemoved(); | |
224 // |client| is destroyed when the scope of this function is left. | |
225 } | |
226 | |
227 void NotifyDecodeStatusOnIOThread(int32_t route_id, | |
228 int32_t buffer_id, | |
229 JpegDecodeAccelerator::Error error) { | |
230 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
231 SendOnIOThread(new AcceleratedJpegDecoderHostMsg_DecodeAck( | |
232 route_id, buffer_id, error)); | |
233 } | |
234 | |
235 void OnDecodeOnIOThread( | |
236 const int32_t* route_id, | |
237 const AcceleratedJpegDecoderMsg_Decode_Params& params) { | |
238 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
239 DCHECK(route_id); | |
240 TRACE_EVENT0("jpeg", "GpuJpegDecodeAccelerator::MessageFilter::OnDecode"); | |
241 | |
242 if (!VerifyDecodeParams(params)) { | |
243 NotifyDecodeStatusOnIOThread(*route_id, params.input_buffer.id(), | |
244 JpegDecodeAccelerator::INVALID_ARGUMENT); | |
245 if (base::SharedMemory::IsHandleValid(params.output_video_frame_handle)) | |
246 base::SharedMemory::CloseHandle(params.output_video_frame_handle); | |
247 return; | |
248 } | |
249 | |
250 // For handles in |params|, from now on, |params.output_video_frame_handle| | |
251 // is taken cared by scoper. |params.input_buffer.handle()| need to be | |
252 // closed manually for early exits. | |
253 std::unique_ptr<base::SharedMemory> output_shm( | |
254 new base::SharedMemory(params.output_video_frame_handle, false)); | |
255 if (!output_shm->Map(params.output_buffer_size)) { | |
256 LOG(ERROR) << "Could not map output shared memory for input buffer id " | |
257 << params.input_buffer.id(); | |
258 NotifyDecodeStatusOnIOThread(*route_id, params.input_buffer.id(), | |
259 JpegDecodeAccelerator::PLATFORM_FAILURE); | |
260 base::SharedMemory::CloseHandle(params.input_buffer.handle()); | |
261 return; | |
262 } | |
263 | |
264 uint8_t* shm_memory = static_cast<uint8_t*>(output_shm->memory()); | |
265 scoped_refptr<VideoFrame> frame = VideoFrame::WrapExternalSharedMemory( | |
266 PIXEL_FORMAT_I420, // format | |
267 params.coded_size, // coded_size | |
268 gfx::Rect(params.coded_size), // visible_rect | |
269 params.coded_size, // natural_size | |
270 shm_memory, // data | |
271 params.output_buffer_size, // data_size | |
272 params.output_video_frame_handle, // handle | |
273 0, // data_offset | |
274 base::TimeDelta()); // timestamp | |
275 if (!frame.get()) { | |
276 LOG(ERROR) << "Could not create VideoFrame for input buffer id " | |
277 << params.input_buffer.id(); | |
278 NotifyDecodeStatusOnIOThread(*route_id, params.input_buffer.id(), | |
279 JpegDecodeAccelerator::PLATFORM_FAILURE); | |
280 base::SharedMemory::CloseHandle(params.input_buffer.handle()); | |
281 return; | |
282 } | |
283 frame->AddDestructionObserver( | |
284 base::Bind(DecodeFinished, base::Passed(&output_shm))); | |
285 | |
286 DCHECK_GT(client_map_.count(*route_id), 0u); | |
287 Client* client = client_map_[*route_id].get(); | |
288 client->Decode(params.input_buffer, frame); | |
289 } | |
290 | |
291 protected: | |
292 ~MessageFilter() override { | |
293 if (client_map_.empty()) | |
294 return; | |
295 | |
296 if (child_task_runner_->BelongsToCurrentThread()) { | |
297 client_map_.clear(); | |
298 } else { | |
299 // Make sure |Client| are deleted on child thread. | |
300 std::unique_ptr<ClientMap> client_map(new ClientMap); | |
301 client_map->swap(client_map_); | |
302 | |
303 child_task_runner_->PostTask( | |
304 FROM_HERE, | |
305 base::Bind(&DeleteClientMapOnChildThread, base::Passed(&client_map))); | |
306 } | |
307 } | |
308 | |
309 private: | |
310 using ClientMap = base::hash_map<int32_t, std::unique_ptr<Client>>; | |
311 | |
312 // Must be static because this method runs after destructor. | |
313 static void DeleteClientMapOnChildThread( | |
314 std::unique_ptr<ClientMap> client_map) { | |
315 // |client_map| is cleared when the scope of this function is left. | |
316 } | |
317 | |
318 base::WeakPtr<GpuJpegDecodeAccelerator> owner_; | |
319 | |
320 // GPU child task runner. | |
321 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
322 | |
323 // GPU IO task runner. | |
324 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
325 | |
326 // The sender to which this filter was added. | |
327 IPC::Sender* sender_; | |
328 | |
329 // A map from route id to JpegDecodeAccelerator. | |
330 // Unless in destructor (maybe on child thread), |client_map_| should | |
331 // only be accessed on IO thread. | |
332 ClientMap client_map_; | |
333 }; | |
334 | |
335 // static | 104 // static |
336 bool GpuJpegDecodeAcceleratorFactoryProvider:: | 105 bool GpuJpegDecodeAcceleratorFactoryProvider:: |
337 IsAcceleratedJpegDecodeSupported() { | 106 IsAcceleratedJpegDecodeSupported() { |
338 auto accelerator_factory_functions = GetAcceleratorFactories(); | 107 auto accelerator_factory_functions = GetAcceleratorFactories(); |
339 for (const auto& create_jda_function : accelerator_factory_functions) { | 108 for (const auto& create_jda_function : accelerator_factory_functions) { |
340 std::unique_ptr<JpegDecodeAccelerator> accelerator = | 109 std::unique_ptr<JpegDecodeAccelerator> accelerator = |
341 create_jda_function.Run(base::ThreadTaskRunnerHandle::Get()); | 110 create_jda_function.Run(base::ThreadTaskRunnerHandle::Get()); |
342 if (accelerator && accelerator->IsSupported()) | 111 if (accelerator && accelerator->IsSupported()) |
343 return true; | 112 return true; |
344 } | 113 } |
345 return false; | 114 return false; |
346 } | 115 } |
347 | 116 |
348 // static | 117 // static |
349 std::vector<GpuJpegDecodeAcceleratorFactoryProvider::CreateAcceleratorCB> | 118 std::vector<GpuJpegDecodeAcceleratorFactoryProvider::CreateAcceleratorCB> |
350 GpuJpegDecodeAcceleratorFactoryProvider::GetAcceleratorFactories() { | 119 GpuJpegDecodeAcceleratorFactoryProvider::GetAcceleratorFactories() { |
351 // This list is ordered by priority of use. | 120 // This list is ordered by priority of use. |
352 std::vector<CreateAcceleratorCB> result; | 121 std::vector<CreateAcceleratorCB> result; |
353 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 122 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
354 switches::kUseFakeJpegDecodeAccelerator)) { | 123 switches::kUseFakeJpegDecodeAccelerator)) { |
355 result.push_back(base::Bind(&CreateFakeJDA)); | 124 result.push_back(base::Bind(&CreateFakeJDA)); |
356 } else { | 125 } else { |
357 result.push_back(base::Bind(&CreateV4L2JDA)); | 126 result.push_back(base::Bind(&CreateV4L2JDA)); |
358 result.push_back(base::Bind(&CreateVaapiJDA)); | 127 result.push_back(base::Bind(&CreateVaapiJDA)); |
359 } | 128 } |
360 return result; | 129 return result; |
361 } | 130 } |
362 | 131 |
363 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( | 132 // static |
364 gpu::FilteredSender* channel, | 133 void GpuJpegDecodeAccelerator::Create( |
365 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 134 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
366 : GpuJpegDecodeAccelerator( | 135 const service_manager::BindSourceInfo& source_info, |
367 channel, | 136 mojom::GpuJpegDecodeAcceleratorRequest request) { |
368 std::move(io_task_runner), | 137 auto* jpeg_decoder = new GpuJpegDecodeAccelerator(std::move(io_task_runner)); |
369 GpuJpegDecodeAcceleratorFactoryProvider::GetAcceleratorFactories()) {} | 138 mojo::MakeStrongBinding(base::WrapUnique(jpeg_decoder), std::move(request)); |
| 139 } |
370 | 140 |
371 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( | 141 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( |
372 gpu::FilteredSender* channel, | 142 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
373 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 143 : accelerator_factory_functions_( |
374 std::vector<GpuJpegDecodeAcceleratorFactoryProvider::CreateAcceleratorCB> | 144 GpuJpegDecodeAcceleratorFactoryProvider::GetAcceleratorFactories()), |
375 accelerator_factory_functions) | |
376 : accelerator_factory_functions_(accelerator_factory_functions), | |
377 channel_(channel), | |
378 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 145 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
379 io_task_runner_(std::move(io_task_runner)), | 146 io_task_runner_(std::move(io_task_runner)) {} |
380 client_number_(0) {} | |
381 | 147 |
382 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { | 148 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { |
383 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 149 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
384 if (filter_) { | |
385 channel_->RemoveFilter(filter_.get()); | |
386 } | |
387 } | 150 } |
388 | 151 |
389 void GpuJpegDecodeAccelerator::AddClient(int32_t route_id, | 152 void GpuJpegDecodeAccelerator::NotifyDecodeStatus(int32_t bitstream_buffer_id, |
390 base::Callback<void(bool)> response) { | 153 mojom::Error error) { |
| 154 if (io_task_runner_->BelongsToCurrentThread()) { |
| 155 child_task_runner_->PostTask( |
| 156 FROM_HERE, |
| 157 base::Bind(&GpuJpegDecodeAccelerator::NotifyDecodeStatus, |
| 158 base::Unretained(this), bitstream_buffer_id, error)); |
| 159 return; |
| 160 } |
| 161 |
| 162 DCHECK(decode_cb_); |
| 163 std::move(decode_cb_).Run(bitstream_buffer_id, error); |
| 164 } |
| 165 |
| 166 void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { |
| 167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 168 NotifyDecodeStatus(bitstream_buffer_id, mojom::Error::NO_ERRORS); |
| 169 } |
| 170 |
| 171 void GpuJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
| 172 JpegDecodeAccelerator::Error error) { |
| 173 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 174 NotifyDecodeStatus(bitstream_buffer_id, static_cast<mojom::Error>(error)); |
| 175 } |
| 176 |
| 177 void GpuJpegDecodeAccelerator::Initialize(InitializeCallback callback) { |
391 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 178 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
392 | 179 |
393 // When adding non-chromeos platforms, VideoCaptureGpuJpegDecoder::Initialize | 180 // When adding non-chromeos platforms, VideoCaptureGpuJpegDecoder::Initialize |
394 // needs to be updated. | 181 // needs to be updated. |
395 | 182 |
396 std::unique_ptr<Client> client( | |
397 new Client(AsWeakPtr(), route_id, io_task_runner_)); | |
398 std::unique_ptr<JpegDecodeAccelerator> accelerator; | 183 std::unique_ptr<JpegDecodeAccelerator> accelerator; |
399 for (const auto& create_jda_function : accelerator_factory_functions_) { | 184 for (const auto& create_jda_function : accelerator_factory_functions_) { |
400 std::unique_ptr<JpegDecodeAccelerator> tmp_accelerator = | 185 std::unique_ptr<JpegDecodeAccelerator> tmp_accelerator = |
401 create_jda_function.Run(io_task_runner_); | 186 create_jda_function.Run(io_task_runner_); |
402 if (tmp_accelerator && tmp_accelerator->Initialize(client.get())) { | 187 if (tmp_accelerator && tmp_accelerator->Initialize(this)) { |
403 accelerator = std::move(tmp_accelerator); | 188 accelerator = std::move(tmp_accelerator); |
404 break; | 189 break; |
405 } | 190 } |
406 } | 191 } |
407 | 192 |
408 if (!accelerator) { | 193 if (!accelerator) { |
409 DLOG(ERROR) << "JPEG accelerator Initialize failed"; | 194 DLOG(ERROR) << "JPEG accelerator Initialize failed"; |
410 response.Run(false); | 195 std::move(callback).Run(false); |
411 return; | 196 return; |
412 } | 197 } |
413 client->set_accelerator(std::move(accelerator)); | |
414 | 198 |
415 if (!filter_) { | 199 accelerator_ = std::move(accelerator); |
416 DCHECK_EQ(client_number_, 0); | 200 std::move(callback).Run(true); |
417 filter_ = new MessageFilter(this); | |
418 // This should be before AddClientOnIOThread. | |
419 channel_->AddFilter(filter_.get()); | |
420 } | |
421 client_number_++; | |
422 | |
423 // In this PostTask, |client| may leak if |io_task_runner_| is destroyed | |
424 // before |client| reached AddClientOnIOThread. However we cannot use scoper | |
425 // to protect it because |client| can only be deleted on child thread. The IO | |
426 // thread is destroyed at termination, at which point it's ok to leak since | |
427 // we're going to tear down the process anyway. So we just crossed fingers | |
428 // here instead of making the code unnecessarily complicated. | |
429 io_task_runner_->PostTask( | |
430 FROM_HERE, base::Bind(&MessageFilter::AddClientOnIOThread, filter_, | |
431 route_id, client.release(), response)); | |
432 } | |
433 | |
434 void GpuJpegDecodeAccelerator::NotifyDecodeStatus( | |
435 int32_t route_id, | |
436 int32_t buffer_id, | |
437 JpegDecodeAccelerator::Error error) { | |
438 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
439 Send(new AcceleratedJpegDecoderHostMsg_DecodeAck(route_id, buffer_id, error)); | |
440 } | |
441 | |
442 void GpuJpegDecodeAccelerator::ClientRemoved() { | |
443 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
444 DCHECK_GT(client_number_, 0); | |
445 client_number_--; | |
446 if (client_number_ == 0) { | |
447 channel_->RemoveFilter(filter_.get()); | |
448 filter_ = nullptr; | |
449 } | |
450 } | |
451 | |
452 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { | |
453 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
454 return channel_->Send(message); | |
455 } | |
456 | |
457 void GpuJpegDecodeAccelerator::Initialize(InitializeCallback callback) { | |
458 // TODO(c.padhi): see http://crbug.com/699255. | |
459 NOTIMPLEMENTED(); | |
460 } | 201 } |
461 | 202 |
462 void GpuJpegDecodeAccelerator::Decode( | 203 void GpuJpegDecodeAccelerator::Decode( |
463 mojom::BitstreamBufferPtr input_buffer, | 204 mojom::BitstreamBufferPtr input_buffer, |
464 const gfx::Size& coded_size, | 205 const gfx::Size& coded_size, |
465 mojo::ScopedSharedBufferHandle output_handle, | 206 mojo::ScopedSharedBufferHandle output_handle, |
466 uint32_t output_buffer_size, | 207 uint32_t output_buffer_size, |
467 DecodeCallback callback) { | 208 DecodeCallback callback) { |
468 // TODO(c.padhi): see http://crbug.com/699255. | 209 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
469 NOTIMPLEMENTED(); | 210 decode_cb_ = std::move(callback); |
| 211 io_task_runner_->PostTask( |
| 212 FROM_HERE, |
| 213 base::Bind(&GpuJpegDecodeAccelerator::DecodeOnIOThread, |
| 214 base::Unretained(this), base::Passed(&input_buffer), |
| 215 coded_size, base::Passed(&output_handle), output_buffer_size)); |
| 216 } |
| 217 |
| 218 void GpuJpegDecodeAccelerator::DecodeOnIOThread( |
| 219 mojom::BitstreamBufferPtr input_buffer, |
| 220 const gfx::Size& coded_size, |
| 221 mojo::ScopedSharedBufferHandle output_handle, |
| 222 uint32_t output_buffer_size) { |
| 223 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 224 TRACE_EVENT0("jpeg", "GpuJpegDecodeAccelerator::DecodeOnIOThread"); |
| 225 |
| 226 if (!VerifyDecodeParams(coded_size, &output_handle, output_buffer_size)) { |
| 227 NotifyDecodeStatus(input_buffer->id, mojom::Error::INVALID_ARGUMENT); |
| 228 return; |
| 229 } |
| 230 |
| 231 base::SharedMemoryHandle memory_handle; |
| 232 size_t memory_size = 0; |
| 233 bool read_only_flag = false; |
| 234 |
| 235 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 236 std::move(output_handle), &memory_handle, &memory_size, &read_only_flag); |
| 237 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 238 |
| 239 std::unique_ptr<base::SharedMemory> output_shm( |
| 240 new base::SharedMemory(memory_handle, false)); |
| 241 if (!output_shm->Map(output_buffer_size)) { |
| 242 LOG(ERROR) << "Could not map output shared memory for input buffer id " |
| 243 << input_buffer->id; |
| 244 NotifyDecodeStatus(input_buffer->id, mojom::Error::PLATFORM_FAILURE); |
| 245 return; |
| 246 } |
| 247 |
| 248 uint8_t* shm_memory = static_cast<uint8_t*>(output_shm->memory()); |
| 249 scoped_refptr<VideoFrame> frame = VideoFrame::WrapExternalSharedMemory( |
| 250 PIXEL_FORMAT_I420, // format |
| 251 coded_size, // coded_size |
| 252 gfx::Rect(coded_size), // visible_rect |
| 253 coded_size, // natural_size |
| 254 shm_memory, // data |
| 255 output_buffer_size, // data_size |
| 256 memory_handle, // handle |
| 257 0, // data_offset |
| 258 base::TimeDelta()); // timestamp |
| 259 if (!frame.get()) { |
| 260 LOG(ERROR) << "Could not create VideoFrame for input buffer id " |
| 261 << input_buffer->id; |
| 262 NotifyDecodeStatus(input_buffer->id, mojom::Error::PLATFORM_FAILURE); |
| 263 return; |
| 264 } |
| 265 frame->AddDestructionObserver( |
| 266 base::Bind(DecodeFinished, base::Passed(&output_shm))); |
| 267 |
| 268 result = mojo::UnwrapSharedMemoryHandle( |
| 269 std::move(input_buffer->memory_handle), &memory_handle, &memory_size, |
| 270 &read_only_flag); |
| 271 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 272 |
| 273 BitstreamBuffer bitstream_buffer(input_buffer->id, memory_handle, |
| 274 input_buffer->size, input_buffer->offset, |
| 275 input_buffer->timestamp); |
| 276 |
| 277 bitstream_buffer.SetDecryptConfig(DecryptConfig( |
| 278 input_buffer->key_id, input_buffer->iv, input_buffer->subsamples)); |
| 279 |
| 280 DCHECK(accelerator_); |
| 281 accelerator_->Decode(bitstream_buffer, frame); |
470 } | 282 } |
471 | 283 |
472 void GpuJpegDecodeAccelerator::Uninitialize() { | 284 void GpuJpegDecodeAccelerator::Uninitialize() { |
473 // TODO(c.padhi): see http://crbug.com/699255. | 285 // TODO(c.padhi): see http://crbug.com/699255. |
474 NOTIMPLEMENTED(); | 286 NOTIMPLEMENTED(); |
475 } | 287 } |
476 | 288 |
477 } // namespace media | 289 } // namespace media |
OLD | NEW |