Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / media / v4l2_jpeg_decode_accelerator.h
blobdfccfccf05867754b4f2de0f011e8fff2a5a04a2
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.
5 #ifndef CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_
8 #include <queue>
9 #include <vector>
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"
22 namespace content {
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;
32 // media::JpegDecodeAccelerator implementation.
33 bool Initialize(Client* client) override;
34 void Decode(const media::BitstreamBuffer& bitstream_buffer,
35 const scoped_refptr<media::VideoFrame>& video_frame) override;
36 bool IsSupported() override;
38 private:
39 // Record for input/output buffers.
40 struct BufferRecord {
41 BufferRecord();
42 ~BufferRecord();
43 void* address; // mmap() address.
44 size_t length; // mmap() length.
46 // Set true during QBUF and DQBUF. |address| will be accessed by hardware.
47 bool at_device;
50 // Job record. Jobs are processed in a FIFO order. This is separate from
51 // BufferRecord of input, because a BufferRecord of input may be returned
52 // before we dequeue the corresponding output buffer. It can't always be
53 // associated with a BufferRecord of output immediately either, because at
54 // the time of submission we may not have one available (and don't need one
55 // to submit input to the device).
56 struct JobRecord {
57 JobRecord(media::BitstreamBuffer bitstream_buffer,
58 scoped_refptr<media::VideoFrame> video_frame);
59 ~JobRecord();
61 // Input image buffer.
62 media::BitstreamBuffer bitstream_buffer;
63 // Output frame buffer.
64 scoped_refptr<media::VideoFrame> out_frame;
65 // Memory mapped from |bitstream_buffer|.
66 scoped_ptr<base::SharedMemory> shm;
69 void EnqueueInput();
70 void EnqueueOutput();
71 void Dequeue();
72 bool EnqueueInputRecord();
73 bool EnqueueOutputRecord();
74 bool CreateInputBuffers();
75 bool CreateOutputBuffers();
76 void DestroyInputBuffers();
77 void DestroyOutputBuffers();
78 void ResetQueues();
80 // Return the number of input/output buffers enqueued to the device.
81 size_t InputBufferQueuedCount();
82 size_t OutputBufferQueuedCount();
84 // Return true if input buffer should be re-created.
85 bool ShouldRecreateInputBuffers();
86 // Return true if output buffer should be re-created.
87 bool ShouldRecreateOutputBuffers();
88 // Create input and output buffer if needed. Return false means that an error
89 // has happened.
90 bool CreateBuffersIfNecessary();
92 void VideoFrameReady(int32_t bitstream_buffer_id);
93 void NotifyError(int32_t bitstream_buffer_id, Error error);
94 void PostNotifyError(int32_t bitstream_buffer_id, Error error);
96 // Run on |decoder_thread_| to enqueue the coming frame.
97 void DecodeTask(scoped_ptr<JobRecord> job_record);
99 // Run on |decoder_thread_| to dequeue last frame and enqueue next frame.
100 // This task is triggered by DevicePollTask.
101 void ServiceDeviceTask();
103 // Start/Stop |device_poll_thread_|.
104 void StartDevicePoll();
105 bool StopDevicePoll();
107 // Run on |device_poll_thread_| to wait for device events.
108 void DevicePollTask();
110 // Run on |decoder_thread_| to destroy input and output buffers.
111 void DestroyTask();
113 // The number of input buffers and output buffers.
114 const size_t kBufferCount = 2;
116 // Current image size used for checking the size is changed.
117 gfx::Size image_coded_size_;
119 // Set true when input or output buffers have to be re-allocated.
120 bool recreate_input_buffers_pending_;
121 bool recreate_output_buffers_pending_;
123 // ChildThread's task runner.
124 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_;
126 // GPU IO task runner.
127 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
129 // The client of this class.
130 Client* client_;
132 // The V4L2Device this class is operating upon.
133 scoped_refptr<V4L2Device> device_;
135 // Thread to communicate with the device.
136 base::Thread decoder_thread_;
137 // Decode task runner.
138 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
139 // Thread used to poll the V4L2 for events only.
140 base::Thread device_poll_thread_;
141 // Device poll task runner.
142 scoped_refptr<base::SingleThreadTaskRunner> device_poll_task_runner_;
144 // All the below members except |weak_factory_| are accessed from
145 // |decoder_thread_| only (if it's running).
146 std::queue<linked_ptr<JobRecord>> input_jobs_;
147 std::queue<linked_ptr<JobRecord>> running_jobs_;
149 // Input queue state.
150 bool input_streamon_;
151 // Mapping of int index to an input buffer record.
152 std::vector<BufferRecord> input_buffer_map_;
153 // Indices of input buffers ready to use; LIFO since we don't care about
154 // ordering.
155 std::vector<int> free_input_buffers_;
157 // Output queue state.
158 bool output_streamon_;
159 // Mapping of int index to an output buffer record.
160 std::vector<BufferRecord> output_buffer_map_;
161 // Indices of output buffers ready to use; LIFO since we don't care about
162 // ordering.
163 std::vector<int> free_output_buffers_;
165 // Weak factory for producing weak pointers on the child thread.
166 base::WeakPtrFactory<V4L2JpegDecodeAccelerator> weak_factory_;
167 // Point to |this| for use in posting tasks from the decoder thread back to
168 // the ChildThread.
169 base::WeakPtr<V4L2JpegDecodeAccelerator> weak_ptr_;
171 DISALLOW_COPY_AND_ASSIGN(V4L2JpegDecodeAccelerator);
174 } // namespace content
176 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_JPEG_DECODE_ACCELERATOR_H_