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 <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <queue> | |
13 #include <vector> | |
14 | |
15 #include "base/macros.h" | |
16 #include "base/memory/linked_ptr.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/memory/weak_ptr.h" | |
19 #include "base/single_thread_task_runner.h" | |
20 #include "base/threading/thread.h" | |
21 #include "content/common/content_export.h" | |
22 #include "content/common/gpu/media/shared_memory_region.h" | |
23 #include "content/common/gpu/media/v4l2_device.h" | |
24 #include "media/base/bitstream_buffer.h" | |
25 #include "media/base/video_frame.h" | |
26 #include "media/video/jpeg_decode_accelerator.h" | |
27 | |
28 namespace content { | |
29 | |
30 class CONTENT_EXPORT V4L2JpegDecodeAccelerator | |
31 : public media::JpegDecodeAccelerator { | |
32 public: | |
33 V4L2JpegDecodeAccelerator( | |
34 const scoped_refptr<V4L2Device>& device, | |
35 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | |
36 ~V4L2JpegDecodeAccelerator() override; | |
37 | |
38 // media::JpegDecodeAccelerator implementation. | |
39 bool Initialize(Client* client) override; | |
40 void Decode(const media::BitstreamBuffer& bitstream_buffer, | |
41 const scoped_refptr<media::VideoFrame>& video_frame) override; | |
42 bool IsSupported() override; | |
43 | |
44 private: | |
45 // Record for input/output buffers. | |
46 struct BufferRecord { | |
47 BufferRecord(); | |
48 ~BufferRecord(); | |
49 void* address; // mmap() address. | |
50 size_t length; // mmap() length. | |
51 | |
52 // Set true during QBUF and DQBUF. |address| will be accessed by hardware. | |
53 bool at_device; | |
54 }; | |
55 | |
56 // Job record. Jobs are processed in a FIFO order. This is separate from | |
57 // BufferRecord of input, because a BufferRecord of input may be returned | |
58 // before we dequeue the corresponding output buffer. It can't always be | |
59 // associated with a BufferRecord of output immediately either, because at | |
60 // the time of submission we may not have one available (and don't need one | |
61 // to submit input to the device). | |
62 struct JobRecord { | |
63 JobRecord(const media::BitstreamBuffer& bitstream_buffer, | |
64 scoped_refptr<media::VideoFrame> video_frame); | |
65 ~JobRecord(); | |
66 | |
67 // Input image buffer ID. | |
68 int32_t bitstream_buffer_id; | |
69 // Memory mapped from |bitstream_buffer|. | |
70 SharedMemoryRegion shm; | |
71 // Output frame buffer. | |
72 scoped_refptr<media::VideoFrame> out_frame; | |
73 }; | |
74 | |
75 void EnqueueInput(); | |
76 void EnqueueOutput(); | |
77 void Dequeue(); | |
78 bool EnqueueInputRecord(); | |
79 bool EnqueueOutputRecord(); | |
80 bool CreateInputBuffers(); | |
81 bool CreateOutputBuffers(); | |
82 void DestroyInputBuffers(); | |
83 void DestroyOutputBuffers(); | |
84 | |
85 // Return the number of input/output buffers enqueued to the device. | |
86 size_t InputBufferQueuedCount(); | |
87 size_t OutputBufferQueuedCount(); | |
88 | |
89 // Return true if input buffer size is not enough. | |
90 bool ShouldRecreateInputBuffers(); | |
91 // Destroy and create input buffers. Return false on error. | |
92 bool RecreateInputBuffers(); | |
93 // Destroy and create output buffers. Return false on error. | |
94 bool RecreateOutputBuffers(); | |
95 | |
96 void VideoFrameReady(int32_t bitstream_buffer_id); | |
97 void NotifyError(int32_t bitstream_buffer_id, Error error); | |
98 void PostNotifyError(int32_t bitstream_buffer_id, Error error); | |
99 | |
100 // Run on |decoder_thread_| to enqueue the coming frame. | |
101 void DecodeTask(std::unique_ptr<JobRecord> job_record); | |
102 | |
103 // Run on |decoder_thread_| to dequeue last frame and enqueue next frame. | |
104 // This task is triggered by DevicePollTask. |event_pending| means that device | |
105 // has resolution change event or pixelformat change event. | |
106 void ServiceDeviceTask(bool event_pending); | |
107 | |
108 // Dequeue source change event. Return false on error. | |
109 bool DequeueSourceChangeEvent(); | |
110 | |
111 // Start/Stop |device_poll_thread_|. | |
112 void StartDevicePoll(); | |
113 bool StopDevicePoll(); | |
114 | |
115 // Run on |device_poll_thread_| to wait for device events. | |
116 void DevicePollTask(); | |
117 | |
118 // Run on |decoder_thread_| to destroy input and output buffers. | |
119 void DestroyTask(); | |
120 | |
121 // The number of input buffers and output buffers. | |
122 const size_t kBufferCount = 2; | |
123 | |
124 // Coded size of output buffer. | |
125 gfx::Size output_buffer_coded_size_; | |
126 | |
127 // Pixel format of output buffer. | |
128 uint32_t output_buffer_pixelformat_; | |
129 | |
130 // ChildThread's task runner. | |
131 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
132 | |
133 // GPU IO task runner. | |
134 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
135 | |
136 // The client of this class. | |
137 Client* client_; | |
138 | |
139 // The V4L2Device this class is operating upon. | |
140 scoped_refptr<V4L2Device> device_; | |
141 | |
142 // Thread to communicate with the device. | |
143 base::Thread decoder_thread_; | |
144 // Decode task runner. | |
145 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; | |
146 // Thread used to poll the V4L2 for events only. | |
147 base::Thread device_poll_thread_; | |
148 // Device poll task runner. | |
149 scoped_refptr<base::SingleThreadTaskRunner> device_poll_task_runner_; | |
150 | |
151 // All the below members except |weak_factory_| are accessed from | |
152 // |decoder_thread_| only (if it's running). | |
153 std::queue<linked_ptr<JobRecord>> input_jobs_; | |
154 std::queue<linked_ptr<JobRecord>> running_jobs_; | |
155 | |
156 // Input queue state. | |
157 bool input_streamon_; | |
158 // Mapping of int index to an input buffer record. | |
159 std::vector<BufferRecord> input_buffer_map_; | |
160 // Indices of input buffers ready to use; LIFO since we don't care about | |
161 // ordering. | |
162 std::vector<int> free_input_buffers_; | |
163 | |
164 // Output queue state. | |
165 bool output_streamon_; | |
166 // Mapping of int index to an output buffer record. | |
167 std::vector<BufferRecord> output_buffer_map_; | |
168 // Indices of output buffers ready to use; LIFO since we don't care about | |
169 // ordering. | |
170 std::vector<int> free_output_buffers_; | |
171 | |
172 // Weak factory for producing weak pointers on the child thread. | |
173 base::WeakPtrFactory<V4L2JpegDecodeAccelerator> weak_factory_; | |
174 // Point to |this| for use in posting tasks from the decoder thread back to | |
175 // the ChildThread. | |
176 base::WeakPtr<V4L2JpegDecodeAccelerator> weak_ptr_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(V4L2JpegDecodeAccelerator); | |
179 }; | |
180 | |
181 } // namespace content | |
182 | |
183 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_ | |
OLD | NEW |