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 #ifndef CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_ | |
7 | |
8 #include <queue> | |
9 #include <vector> | |
10 | |
11 #include "base/memory/linked_ptr.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/threading/thread.h" | |
16 #include "content/common/content_export.h" | |
17 #include "content/common/gpu/media/v4l2_device.h" | |
18 #include "media/base/bitstream_buffer.h" | |
19 #include "media/base/video_frame.h" | |
20 #include "media/video/jpeg_decode_accelerator.h" | |
21 | |
22 namespace content { | |
23 | |
24 class CONTENT_EXPORT V4L2JpegDecodeAccelerator | |
25 : public media::JpegDecodeAccelerator { | |
26 public: | |
27 V4L2JpegDecodeAccelerator( | |
28 const scoped_refptr<V4L2Device>& device, | |
29 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | |
30 ~V4L2JpegDecodeAccelerator() override; | |
31 | |
32 // Note: Initialize() and Destroy() are synchronous. | |
kcwu
2015/06/08 10:04:29
no Destroy()
henryhsu
2015/06/09 10:20:05
Done.
| |
33 bool Initialize(Client* client) override; | |
34 | |
35 void Decode(const media::BitstreamBuffer& bitstream_buffer, | |
36 const scoped_refptr<media::VideoFrame>& video_frame) override; | |
37 | |
38 private: | |
39 // Record for input buffers. | |
40 struct InputRecord { | |
41 InputRecord(); | |
42 ~InputRecord(); | |
43 void* address; // mmap() address. | |
44 size_t length; // mmap() length. | |
45 bool at_device; | |
46 }; | |
47 | |
48 // Record for output buffers. | |
49 struct OutputRecord { | |
50 OutputRecord(); | |
51 ~OutputRecord(); | |
52 void* address; // mmap() address. | |
53 size_t length; // mmap() length. | |
54 bool at_device; | |
55 }; | |
56 | |
57 // Job record. Jobs are processed in a FIFO order. This is separate from | |
58 // InputRecord, because an InputRecord may be returned before we dequeue | |
59 // the corresponding output buffer. It can't always be associated with | |
60 // an OutputRecord immediately either, because at the time of submission we | |
61 // may not have one available (and don't need one to submit input to the | |
62 // device). | |
63 struct JobRecord { | |
64 JobRecord(media::BitstreamBuffer bitstream_buffer, | |
65 scoped_refptr<media::VideoFrame> video_frame); | |
66 ~JobRecord(); | |
67 media::BitstreamBuffer bitstream_buffer; | |
68 scoped_refptr<media::VideoFrame> frame; | |
69 }; | |
70 | |
71 enum { | |
72 // Arbitrarily tuned. | |
73 kInputBufferCount = 2, | |
74 kOutputBufferCount = 2, | |
75 }; | |
76 | |
77 enum { | |
78 kResetInputBuffer = 1 << 0, | |
79 kResetOutputBuffer = 1 << 1, | |
80 }; | |
81 | |
82 void Enqueue(); | |
83 void Dequeue(); | |
84 bool EnqueueInputRecord(); | |
85 bool EnqueueOutputRecord(); | |
86 bool CheckBufferAttributes(); | |
87 bool CreateInputBuffers(); | |
88 bool CreateOutputBuffers(); | |
89 void DestroyInputBuffers(); | |
90 void DestroyOutputBuffers(); | |
91 void ResetBuffers(); | |
92 | |
93 void NotifyError(int32_t bitstream_buffer_id, Error error); | |
94 void NotifyErrorFromDecoderThread(int32_t bitstream_buffer_id, Error error); | |
95 void DestroyTask(); | |
96 | |
97 void DecodeTask(scoped_ptr<JobRecord> job_record); | |
98 void ServiceDeviceTask(); | |
99 | |
100 // Attempt to start/stop device_poll_thread_. | |
101 void StartDevicePoll(); | |
102 bool StopDevicePoll(bool keep_input_queue); | |
103 | |
104 // Ran on device_poll_thread_ to wait for device events. | |
105 void DevicePollTask(bool poll_device); | |
106 | |
107 media::VideoFrame::Format output_format_; | |
108 // Record current image size for checking image size is changed or not. | |
109 gfx::Size image_coded_size_; | |
110 // Set to true when input or output buffer have to re-allocate. | |
111 uint32_t reset_buffer_flag_; | |
112 | |
113 // ChildThread's task runner. | |
114 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
115 | |
116 // GPU IO task runner. | |
117 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
118 | |
119 // The client of this class. | |
120 Client* client_; | |
121 | |
122 // The V4L2Device this class is operating upon. | |
123 scoped_refptr<V4L2Device> device_; | |
124 | |
125 // Thread to communicate with the device on. | |
126 base::Thread decoder_thread_; | |
127 // Decode task runner. | |
128 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; | |
129 // Thread used to poll the V4L2 for events only. | |
130 base::Thread device_poll_thread_; | |
131 // Device poll task runner. | |
132 scoped_refptr<base::SingleThreadTaskRunner> device_poll_task_runner_; | |
133 | |
134 // All the below members are to be accessed from decoder_thread_ only | |
135 // (if it's running). | |
136 std::queue<linked_ptr<JobRecord> > input_queue_; | |
137 std::queue<linked_ptr<JobRecord> > running_jobs_; | |
138 | |
139 // Input queue state. | |
140 bool input_streamon_; | |
141 // Number of input buffers enqueued to the device. | |
142 int input_buffer_queued_count_; | |
143 // Input buffers ready to use; LIFO since we don't care about ordering. | |
144 std::vector<int> free_input_buffers_; | |
145 // Mapping of int index to an input buffer record. | |
146 std::vector<InputRecord> input_buffer_map_; | |
147 | |
148 // Output queue state. | |
149 bool output_streamon_; | |
150 // Number of output buffers enqueued to the device. | |
151 int output_buffer_queued_count_; | |
152 // Output buffers ready to use; LIFO since we don't care about ordering. | |
153 std::vector<int> free_output_buffers_; | |
154 // Mapping of int index to an output buffer record. | |
155 std::vector<OutputRecord> output_buffer_map_; | |
156 | |
157 // Weak factory for producing weak pointers on the decoder_thread_ | |
158 base::WeakPtrFactory<V4L2JpegDecodeAccelerator> device_weak_factory_; | |
159 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder | |
160 // thread back to the ChildThread. Because the decoder thread is a member of | |
161 // this class, any task running on the decoder thread is guaranteed that this | |
162 // object is still alive. As a result, tasks posted from ChildThread to | |
163 // decoder thread should use base::Unretained(this), and tasks posted from | |
164 // the decoder thread to the ChildThread should use |device_weak_|. | |
165 base::WeakPtr<V4L2JpegDecodeAccelerator> device_weak_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(V4L2JpegDecodeAccelerator); | |
168 }; | |
169 | |
170 } // namespace content | |
171 | |
172 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_ | |
OLD | NEW |