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