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