OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_IMAGE_PROCESSOR_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_V4L2_IMAGE_PROCESSOR_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/threading/thread.h" | |
19 #include "content/common/content_export.h" | |
20 #include "content/common/gpu/media/v4l2_device.h" | |
21 #include "media/base/video_frame.h" | |
22 | |
23 namespace content { | |
24 | |
25 // Handles image processing accelerators that expose a V4L2 memory-to-memory | |
26 // interface. The threading model of this class is the same as for other V4L2 | |
27 // hardware accelerators (see V4L2VideoDecodeAccelerator) for more details. | |
28 class CONTENT_EXPORT V4L2ImageProcessor { | |
29 public: | |
30 explicit V4L2ImageProcessor(const scoped_refptr<V4L2Device>& device); | |
31 virtual ~V4L2ImageProcessor(); | |
32 | |
33 // Initializes the processor to convert from |input_format| to |output_format| | |
34 // and/or scale from |input_visible_size| to |output_visible_size|. | |
35 // Request the output buffers to be of at least |output_allocated_size|. The | |
36 // number of input buffers and output buffers will be |num_buffers|. Provided | |
37 // |error_cb| will be called if an error occurs. Return true if the requested | |
38 // configuration is supported. | |
39 bool Initialize(media::VideoPixelFormat input_format, | |
40 media::VideoPixelFormat output_format, | |
41 gfx::Size input_visible_size, | |
42 gfx::Size output_visible_size, | |
43 gfx::Size output_allocated_size, | |
44 int num_buffers, | |
45 const base::Closure& error_cb); | |
46 | |
47 // Return a vector of dmabuf file descriptors, exported for V4L2 output buffer | |
48 // with |index|. The size of vector will be the number of planes of the | |
49 // buffer. Return an empty vector on failure. | |
50 std::vector<base::ScopedFD> GetDmabufsForOutputBuffer( | |
51 int output_buffer_index); | |
52 | |
53 // Returns input allocated size required by the processor to be fed with. | |
54 gfx::Size input_allocated_size() const { return input_allocated_size_; } | |
55 | |
56 // Returns output allocated size required by the processor. | |
57 gfx::Size output_allocated_size() const { return output_allocated_size_; } | |
58 | |
59 // Callback to be used to return the index of a processed image to the | |
60 // client. After the client is done with the frame, call Process with the | |
61 // index to return the output buffer to the image processor. | |
62 typedef base::Callback<void(int output_buffer_index)> FrameReadyCB; | |
63 | |
64 // Called by client to process |frame|. The resulting processed frame will be | |
65 // stored in |output_buffer_index| output buffer and notified via |cb|. The | |
66 // processor will drop all its references to |frame| after it finishes | |
67 // accessing it. | |
68 void Process(const scoped_refptr<media::VideoFrame>& frame, | |
69 int output_buffer_index, | |
70 const FrameReadyCB& cb); | |
71 | |
72 // Stop all processing and clean up. After this method returns no more | |
73 // callbacks will be invoked. Deletes |this| unconditionally, so make sure | |
74 // to drop all pointers to it! | |
75 void Destroy(); | |
76 | |
77 private: | |
78 // Record for input buffers. | |
79 struct InputRecord { | |
80 InputRecord(); | |
81 ~InputRecord(); | |
82 scoped_refptr<media::VideoFrame> frame; | |
83 bool at_device; | |
84 }; | |
85 | |
86 // Record for output buffers. | |
87 struct OutputRecord { | |
88 OutputRecord(); | |
89 ~OutputRecord(); | |
90 bool at_device; | |
91 }; | |
92 | |
93 // Job record. Jobs are processed in a FIFO order. This is separate from | |
94 // InputRecord, because an InputRecord may be returned before we dequeue | |
95 // the corresponding output buffer. The processed frame will be stored in | |
96 // |output_buffer_index| output buffer. | |
97 struct JobRecord { | |
98 JobRecord(); | |
99 ~JobRecord(); | |
100 scoped_refptr<media::VideoFrame> frame; | |
101 int output_buffer_index; | |
102 FrameReadyCB ready_cb; | |
103 }; | |
104 | |
105 void EnqueueInput(); | |
106 void EnqueueOutput(int index); | |
107 void Dequeue(); | |
108 bool EnqueueInputRecord(); | |
109 bool EnqueueOutputRecord(int index); | |
110 bool CreateInputBuffers(); | |
111 bool CreateOutputBuffers(); | |
112 void DestroyInputBuffers(); | |
113 void DestroyOutputBuffers(); | |
114 | |
115 void NotifyError(); | |
116 void NotifyErrorOnChildThread(const base::Closure& error_cb); | |
117 void DestroyTask(); | |
118 | |
119 void ProcessTask(std::unique_ptr<JobRecord> job_record); | |
120 void ServiceDeviceTask(); | |
121 | |
122 // Attempt to start/stop device_poll_thread_. | |
123 bool StartDevicePoll(); | |
124 bool StopDevicePoll(); | |
125 | |
126 // Ran on device_poll_thread_ to wait for device events. | |
127 void DevicePollTask(bool poll_device); | |
128 | |
129 // A processed frame is ready. | |
130 void FrameReady(const FrameReadyCB& cb, int output_buffer_index); | |
131 | |
132 // Size and format-related members remain constant after initialization. | |
133 // The visible/allocated sizes of the input frame. | |
134 gfx::Size input_visible_size_; | |
135 gfx::Size input_allocated_size_; | |
136 | |
137 // The visible/allocated sizes of the destination frame. | |
138 gfx::Size output_visible_size_; | |
139 gfx::Size output_allocated_size_; | |
140 | |
141 media::VideoPixelFormat input_format_; | |
142 media::VideoPixelFormat output_format_; | |
143 uint32_t input_format_fourcc_; | |
144 uint32_t output_format_fourcc_; | |
145 | |
146 size_t input_planes_count_; | |
147 size_t output_planes_count_; | |
148 | |
149 // Our original calling task runner for the child thread. | |
150 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
151 | |
152 // V4L2 device in use. | |
153 scoped_refptr<V4L2Device> device_; | |
154 | |
155 // Thread to communicate with the device on. | |
156 base::Thread device_thread_; | |
157 // Thread used to poll the V4L2 for events only. | |
158 base::Thread device_poll_thread_; | |
159 | |
160 // All the below members are to be accessed from device_thread_ only | |
161 // (if it's running). | |
162 std::queue<linked_ptr<JobRecord> > input_queue_; | |
163 std::queue<linked_ptr<JobRecord> > running_jobs_; | |
164 | |
165 // Input queue state. | |
166 bool input_streamon_; | |
167 // Number of input buffers enqueued to the device. | |
168 int input_buffer_queued_count_; | |
169 // Input buffers ready to use; LIFO since we don't care about ordering. | |
170 std::vector<int> free_input_buffers_; | |
171 // Mapping of int index to an input buffer record. | |
172 std::vector<InputRecord> input_buffer_map_; | |
173 | |
174 // Output queue state. | |
175 bool output_streamon_; | |
176 // Number of output buffers enqueued to the device. | |
177 int output_buffer_queued_count_; | |
178 // Mapping of int index to an output buffer record. | |
179 std::vector<OutputRecord> output_buffer_map_; | |
180 // The number of input or output buffers. | |
181 int num_buffers_; | |
182 | |
183 // Error callback to the client. | |
184 base::Closure error_cb_; | |
185 | |
186 // WeakPtr<> pointing to |this| for use in posting tasks from the device | |
187 // worker threads back to the child thread. Because the worker threads | |
188 // are members of this class, any task running on those threads is guaranteed | |
189 // that this object is still alive. As a result, tasks posted from the child | |
190 // thread to the device thread should use base::Unretained(this), | |
191 // and tasks posted the other way should use |weak_this_|. | |
192 base::WeakPtr<V4L2ImageProcessor> weak_this_; | |
193 | |
194 // Weak factory for producing weak pointers on the child thread. | |
195 base::WeakPtrFactory<V4L2ImageProcessor> weak_this_factory_; | |
196 | |
197 DISALLOW_COPY_AND_ASSIGN(V4L2ImageProcessor); | |
198 }; | |
199 | |
200 } // namespace content | |
201 | |
202 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_IMAGE_PROCESSOR_H_ | |
OLD | NEW |