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