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.
5 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_
8 #include "base/memory/linked_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/threading/thread.h"
14 #include "content/common/content_export.h"
15 #include "content/common/gpu/media/vaapi_jpeg_decoder.h"
16 #include "content/common/gpu/media/vaapi_wrapper.h"
17 #include "media/base/bitstream_buffer.h"
18 #include "media/video/jpeg_decode_accelerator.h"
22 // Class to provide JPEG decode acceleration for Intel systems with hardware
23 // support for it, and on which libva is available.
24 // Decoding tasks are performed in a separate decoding thread.
26 // Threading/life-cycle: this object is created & destroyed on the GPU
27 // ChildThread. A few methods on it are called on the decoder thread which is
28 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread
29 // can assume |*this| is still alive. See |weak_this_| below for more details.
30 class CONTENT_EXPORT VaapiJpegDecodeAccelerator
31 : public media::JpegDecodeAccelerator
{
33 VaapiJpegDecodeAccelerator(
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
);
35 ~VaapiJpegDecodeAccelerator() override
;
37 // media::JpegDecodeAccelerator implementation.
38 bool Initialize(media::JpegDecodeAccelerator::Client
* client
) override
;
39 void Decode(const media::BitstreamBuffer
& bitstream_buffer
,
40 const scoped_refptr
<media::VideoFrame
>& video_frame
) override
;
41 bool IsSupported() override
;
44 // An input buffer and the corresponding output video frame awaiting
45 // consumption, provided by the client.
46 struct DecodeRequest
{
47 DecodeRequest(const media::BitstreamBuffer
& bitstream_buffer
,
48 scoped_ptr
<base::SharedMemory
> shm
,
49 const scoped_refptr
<media::VideoFrame
>& video_frame
);
52 media::BitstreamBuffer bitstream_buffer
;
53 scoped_ptr
<base::SharedMemory
> shm
;
54 scoped_refptr
<media::VideoFrame
> video_frame
;
57 // Notifies the client that an error has occurred and decoding cannot
59 void NotifyError(int32_t bitstream_buffer_id
, Error error
);
60 void NotifyErrorFromDecoderThread(int32_t bitstream_buffer_id
, Error error
);
61 void VideoFrameReady(int32_t bitstream_buffer_id
);
63 // Processes one decode |request|.
64 void DecodeTask(const scoped_ptr
<DecodeRequest
>& request
);
66 // Puts contents of |va_surface| into given |video_frame|, releases the
67 // surface and passes the |input_buffer_id| of the resulting picture to
69 bool OutputPicture(VASurfaceID va_surface_id
,
70 int32_t input_buffer_id
,
71 const scoped_refptr
<media::VideoFrame
>& video_frame
);
73 // ChildThread's task runner.
74 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
76 // GPU IO task runner.
77 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
79 // The client of this class.
82 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder
83 // thread back to the ChildThread. Because the decoder thread is a member of
84 // this class, any task running on the decoder thread is guaranteed that this
85 // object is still alive. As a result, tasks posted from ChildThread to
86 // decoder thread should use base::Unretained(this), and tasks posted from the
87 // decoder thread to the ChildThread should use |weak_this_|.
88 base::WeakPtr
<VaapiJpegDecodeAccelerator
> weak_this_
;
90 scoped_ptr
<VaapiWrapper
> vaapi_wrapper_
;
92 // Comes after vaapi_wrapper_ to ensure its destructor is executed before
93 // |vaapi_wrapper_| is destroyed.
94 scoped_ptr
<VaapiJpegDecoder
> decoder_
;
95 base::Thread decoder_thread_
;
96 // Use this to post tasks to |decoder_thread_| instead of
97 // |decoder_thread_.task_runner()| because the latter will be NULL once
98 // |decoder_thread_.Stop()| returns.
99 scoped_refptr
<base::SingleThreadTaskRunner
> decoder_task_runner_
;
101 // The current VA surface for decoding.
102 VASurfaceID va_surface_id_
;
103 // The coded size associated with |va_surface_id_|.
104 gfx::Size coded_size_
;
105 // The VA RT format associated with |va_surface_id_|.
106 unsigned int va_rt_format_
;
108 // The WeakPtrFactory for |weak_this_|.
109 base::WeakPtrFactory
<VaapiJpegDecodeAccelerator
> weak_this_factory_
;
111 DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecodeAccelerator
);
114 } // namespace content
116 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_