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 // This file contains an implementation of VideoDecoderAccelerator | |
6 // that utilizes hardware video decoder present on Intel CPUs. | |
7 | |
8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ | |
9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ | |
10 | |
11 #include <map> | |
12 #include <queue> | |
13 #include <utility> | |
14 #include <vector> | |
15 | |
16 #include "base/logging.h" | |
17 #include "base/memory/linked_ptr.h" | |
18 #include "base/memory/shared_memory.h" | |
19 #include "base/memory/weak_ptr.h" | |
20 #include "base/message_loop/message_loop.h" | |
21 #include "base/synchronization/condition_variable.h" | |
22 #include "base/synchronization/lock.h" | |
23 #include "base/threading/non_thread_safe.h" | |
24 #include "base/threading/thread.h" | |
25 #include "content/common/content_export.h" | |
26 #include "content/common/gpu/media/vaapi_jpeg_decoder.h" | |
27 #include "content/common/gpu/media/vaapi_wrapper.h" | |
28 #include "media/base/bitstream_buffer.h" | |
29 #include "media/video/jpeg_decode_accelerator.h" | |
30 #include "media/video/picture.h" | |
31 | |
32 namespace content { | |
33 | |
34 // Class to provide JPEG decode acceleration for Intel systems with hardware | |
35 // support for it, and on which libva is available. | |
36 // Decoding tasks are performed in a separate decoding thread. | |
37 // | |
38 // Threading/life-cycle: this object is created & destroyed on the GPU | |
39 // ChildThread. A few methods on it are called on the decoder thread which is | |
40 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread | |
41 // can assume |*this| is still alive. See |weak_this_| below for more details. | |
42 class CONTENT_EXPORT VaapiJpegDecodeAccelerator | |
43 : public media::JpegDecodeAccelerator { | |
44 public: | |
45 VaapiJpegDecodeAccelerator(); | |
46 ~VaapiJpegDecodeAccelerator() override; | |
47 | |
48 // media::JpegDecodeAccelerator implementation. | |
49 bool Initialize(media::JpegDecodeAccelerator::Client* client) override; | |
50 void Decode(const media::BitstreamBuffer& bitstream_buffer, | |
51 const scoped_refptr<media::VideoFrame>& video_frame) override; | |
52 void Destroy() override; | |
53 | |
54 private: | |
55 // Notify the client that an error has occurred and decoding cannot continue. | |
56 void NotifyError(int32_t bitstream_buffer_id, Error error); | |
57 | |
58 // Map the received input buffer into this process' address space and | |
59 // queue it for decode. | |
60 void MapAndQueueNewInputBuffer( | |
61 const media::BitstreamBuffer& bitstream_buffer, | |
62 const scoped_refptr<media::VideoFrame>& video_frame); | |
63 | |
64 // Continue decoding given input buffers and sleep waiting for input/output | |
wuchengli
2015/04/15 07:11:59
There is no sleep waiting.
kcwu
2015/04/16 14:38:29
Done.
| |
65 // as needed. Will exit if a new set of surfaces or reset/flush/destroy | |
wuchengli
2015/04/15 07:11:59
Update the comment.
kcwu
2015/04/16 14:38:28
Done.
| |
66 // is requested. | |
67 void DecodeTask(); | |
68 | |
69 // Helper for Destroy(), doing all the actual work except for deleting self. | |
70 void Cleanup(); | |
71 | |
72 // TODO(kcwu) fix comment | |
wuchengli
2015/04/15 07:11:59
Remove TODO and update the comment.
kcwu
2015/04/16 14:38:29
Done.
| |
73 // Callback to be executed once we have a |va_surface| to be output and | |
74 // an available |tfp_picture| to use for output. | |
75 // Puts contents of |va_surface| into given |tfp_picture|, releases the | |
76 // surface and passes the resulting picture to client for output. | |
77 bool OutputPicture(VASurfaceID va_surface_id, | |
78 int32_t input_id, | |
79 const scoped_refptr<media::VideoFrame>& video_frame); | |
80 | |
81 // VAVDA state. | |
82 enum State { | |
83 // Initialize() not called yet or failed. | |
84 kUninitialized, | |
85 // DecodeTask running. | |
86 kDecoding, | |
87 // Idle, decoder in state ready to start/resume decoding. | |
88 kIdle, | |
89 // Destroying, waiting for the decoder to finish current task. | |
90 kDestroying, | |
91 }; | |
92 | |
93 // Protects input buffer and surface queues and state_. | |
wuchengli
2015/04/15 07:11:59
Please list all the exact variable names instead o
kcwu
2015/04/20 17:47:59
Done.
| |
94 base::Lock lock_; | |
95 State state_; | |
96 | |
97 // An input buffer awaiting consumption, provided by the client. | |
98 struct InputBuffer { | |
99 InputBuffer(); | |
100 ~InputBuffer(); | |
101 | |
102 int32 id; | |
103 size_t size; | |
104 scoped_ptr<base::SharedMemory> shm; | |
105 | |
wuchengli
2015/04/15 07:11:59
remove blank line
kcwu
2015/04/16 14:38:29
Done.
| |
106 scoped_refptr<media::VideoFrame> video_frame; | |
107 }; | |
108 | |
109 // Queue for incoming input buffers. | |
110 typedef std::queue<linked_ptr<InputBuffer>> InputBuffers; | |
111 InputBuffers input_buffers_; | |
112 // Signalled when input buffers are queued onto the input_buffers_ queue. | |
wuchengli
2015/04/15 07:11:59
s/input_buffers_/|input_buffers_|/. Same for other
kcwu
2015/04/16 14:38:28
Done.
| |
113 base::ConditionVariable input_ready_; | |
wuchengli
2015/04/15 07:11:59
not used. remove
kcwu
2015/04/16 14:38:29
Done.
| |
114 | |
115 // Current input buffer at decoder. | |
116 linked_ptr<InputBuffer> curr_input_buffer_; | |
117 | |
118 // ChildThread's message loop | |
119 base::MessageLoop* message_loop_; | |
120 | |
121 // To expose client callbacks from VideoDecodeAccelerator. | |
wuchengli
2015/04/15 07:11:59
s/VideoDecodeAccelerator/JpegDecodeAccelerator/
kcwu
2015/04/16 14:38:29
Done.
| |
122 // NOTE: all calls to these objects *MUST* be executed on message_loop_. | |
123 scoped_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | |
124 base::WeakPtr<Client> client_; | |
125 | |
126 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder | |
127 // thread back to the ChildThread. Because the decoder thread is a member of | |
128 // this class, any task running on the decoder thread is guaranteed that this | |
129 // object is still alive. As a result, tasks posted from ChildThread to | |
130 // decoder thread should use base::Unretained(this), and tasks posted from the | |
131 // decoder thread to the ChildThread should use |weak_this_|. | |
132 base::WeakPtr<VaapiJpegDecodeAccelerator> weak_this_; | |
133 | |
134 // Callback used when creating VASurface objects. | |
135 VASurface::ReleaseCB va_surface_release_cb_; | |
wuchengli
2015/04/15 07:11:59
not used
kcwu
2015/04/16 14:38:29
Done.
| |
136 | |
137 scoped_ptr<VaapiWrapper> vaapi_wrapper_; | |
138 | |
139 // Comes after vaapi_wrapper_ to ensure its destructor is executed before | |
140 // vaapi_wrapper_ is destroyed. | |
141 scoped_ptr<VaapiJpegDecoder> decoder_; | |
142 base::Thread decoder_thread_; | |
143 // Use this to post tasks to |decoder_thread_| instead of | |
144 // |decoder_thread_.message_loop()| because the latter will be NULL once | |
145 // |decoder_thread_.Stop()| returns. | |
146 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_; | |
147 | |
148 // The WeakPtrFactory for |weak_this_|. | |
149 base::WeakPtrFactory<VaapiJpegDecodeAccelerator> weak_this_factory_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecodeAccelerator); | |
152 }; | |
153 | |
154 } // namespace content | |
155 | |
156 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ | |
OLD | NEW |