Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: content/common/gpu/media/generic_v4l2_device.cc

Issue 1882373004: Migrate content/common/gpu/media code to media/gpu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix several more bot-identified build issues Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <libdrm/drm_fourcc.h>
9 #include <linux/videodev2.h>
10 #include <poll.h>
11 #include <string.h>
12 #include <sys/eventfd.h>
13 #include <sys/ioctl.h>
14 #include <sys/mman.h>
15
16 #include "base/files/scoped_file.h"
17 #include "base/macros.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/trace_event/trace_event.h"
20 #include "build/build_config.h"
21 #include "content/common/gpu/media/generic_v4l2_device.h"
22 #include "ui/gl/egl_util.h"
23 #include "ui/gl/gl_bindings.h"
24
25 #if defined(USE_LIBV4L2)
26 // Auto-generated for dlopen libv4l2 libraries
27 #include "content/common/gpu/media/v4l2_stubs.h"
28 #include "third_party/v4l-utils/lib/include/libv4l2.h"
29
30 using content_common_gpu_media::kModuleV4l2;
31 using content_common_gpu_media::InitializeStubs;
32 using content_common_gpu_media::StubPathMap;
33
34 static const base::FilePath::CharType kV4l2Lib[] =
35 FILE_PATH_LITERAL("/usr/lib/libv4l2.so");
36 #endif
37
38 namespace content {
39
40 namespace {
41 const char kDecoderDevice[] = "/dev/video-dec";
42 const char kEncoderDevice[] = "/dev/video-enc";
43 const char kImageProcessorDevice[] = "/dev/image-proc0";
44 const char kJpegDecoderDevice[] = "/dev/jpeg-dec";
45 }
46
47 GenericV4L2Device::GenericV4L2Device(Type type)
48 : V4L2Device(type),
49 use_libv4l2_(false) {
50 }
51
52 GenericV4L2Device::~GenericV4L2Device() {
53 #if defined(USE_LIBV4L2)
54 if (use_libv4l2_ && device_fd_.is_valid())
55 v4l2_close(device_fd_.release());
56 #endif
57 }
58
59 int GenericV4L2Device::Ioctl(int request, void* arg) {
60 #if defined(USE_LIBV4L2)
61 if (use_libv4l2_)
62 return HANDLE_EINTR(v4l2_ioctl(device_fd_.get(), request, arg));
63 #endif
64 return HANDLE_EINTR(ioctl(device_fd_.get(), request, arg));
65 }
66
67 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) {
68 struct pollfd pollfds[2];
69 nfds_t nfds;
70 int pollfd = -1;
71
72 pollfds[0].fd = device_poll_interrupt_fd_.get();
73 pollfds[0].events = POLLIN | POLLERR;
74 nfds = 1;
75
76 if (poll_device) {
77 DVLOG(3) << "Poll(): adding device fd to poll() set";
78 pollfds[nfds].fd = device_fd_.get();
79 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI;
80 pollfd = nfds;
81 nfds++;
82 }
83
84 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) {
85 DPLOG(ERROR) << "poll() failed";
86 return false;
87 }
88 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI);
89 return true;
90 }
91
92 void* GenericV4L2Device::Mmap(void* addr,
93 unsigned int len,
94 int prot,
95 int flags,
96 unsigned int offset) {
97 return mmap(addr, len, prot, flags, device_fd_.get(), offset);
98 }
99
100 void GenericV4L2Device::Munmap(void* addr, unsigned int len) {
101 munmap(addr, len);
102 }
103
104 bool GenericV4L2Device::SetDevicePollInterrupt() {
105 DVLOG(3) << "SetDevicePollInterrupt()";
106
107 const uint64_t buf = 1;
108 if (HANDLE_EINTR(write(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) ==
109 -1) {
110 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed";
111 return false;
112 }
113 return true;
114 }
115
116 bool GenericV4L2Device::ClearDevicePollInterrupt() {
117 DVLOG(3) << "ClearDevicePollInterrupt()";
118
119 uint64_t buf;
120 if (HANDLE_EINTR(read(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) ==
121 -1) {
122 if (errno == EAGAIN) {
123 // No interrupt flag set, and we're reading nonblocking. Not an error.
124 return true;
125 } else {
126 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed";
127 return false;
128 }
129 }
130 return true;
131 }
132
133 bool GenericV4L2Device::Initialize() {
134 const char* device_path = NULL;
135 static bool v4l2_functions_initialized = PostSandboxInitialization();
136 if (!v4l2_functions_initialized) {
137 LOG(ERROR) << "Failed to initialize LIBV4L2 libs";
138 return false;
139 }
140
141 switch (type_) {
142 case kDecoder:
143 device_path = kDecoderDevice;
144 break;
145 case kEncoder:
146 device_path = kEncoderDevice;
147 break;
148 case kImageProcessor:
149 device_path = kImageProcessorDevice;
150 break;
151 case kJpegDecoder:
152 device_path = kJpegDecoderDevice;
153 break;
154 }
155
156 DVLOG(2) << "Initialize(): opening device: " << device_path;
157 // Open the video device.
158 device_fd_.reset(
159 HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC)));
160 if (!device_fd_.is_valid()) {
161 return false;
162 }
163 #if defined(USE_LIBV4L2)
164 if (type_ == kEncoder &&
165 HANDLE_EINTR(v4l2_fd_open(device_fd_.get(), V4L2_DISABLE_CONVERSION)) !=
166 -1) {
167 DVLOG(2) << "Using libv4l2 for " << device_path;
168 use_libv4l2_ = true;
169 }
170 #endif
171
172 device_poll_interrupt_fd_.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
173 if (!device_poll_interrupt_fd_.is_valid()) {
174 return false;
175 }
176 return true;
177 }
178
179 std::vector<base::ScopedFD> GenericV4L2Device::GetDmabufsForV4L2Buffer(
180 int index,
181 size_t num_planes,
182 enum v4l2_buf_type type) {
183 DCHECK(V4L2_TYPE_IS_MULTIPLANAR(type));
184
185 std::vector<base::ScopedFD> dmabuf_fds;
186 for (size_t i = 0; i < num_planes; ++i) {
187 struct v4l2_exportbuffer expbuf;
188 memset(&expbuf, 0, sizeof(expbuf));
189 expbuf.type = type;
190 expbuf.index = index;
191 expbuf.plane = i;
192 expbuf.flags = O_CLOEXEC;
193 if (Ioctl(VIDIOC_EXPBUF, &expbuf) != 0) {
194 dmabuf_fds.clear();
195 break;
196 }
197
198 dmabuf_fds.push_back(base::ScopedFD(expbuf.fd));
199 }
200
201 return dmabuf_fds;
202 }
203
204 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) {
205 static uint32_t kEGLImageDrmFmtsSupported[] = {
206 DRM_FORMAT_ARGB8888,
207 #if defined(ARCH_CPU_ARMEL)
208 DRM_FORMAT_NV12,
209 DRM_FORMAT_MT21,
210 #endif
211 };
212
213 return std::find(
214 kEGLImageDrmFmtsSupported,
215 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported),
216 V4L2PixFmtToDrmFormat(v4l2_pixfmt)) !=
217 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported);
218 }
219
220 EGLImageKHR GenericV4L2Device::CreateEGLImage(EGLDisplay egl_display,
221 EGLContext /* egl_context */,
222 GLuint texture_id,
223 gfx::Size frame_buffer_size,
224 unsigned int buffer_index,
225 uint32_t v4l2_pixfmt,
226 size_t num_v4l2_planes) {
227 DVLOG(3) << "CreateEGLImage()";
228 if (!CanCreateEGLImageFrom(v4l2_pixfmt)) {
229 LOG(ERROR) << "Unsupported V4L2 pixel format";
230 return EGL_NO_IMAGE_KHR;
231 }
232
233 media::VideoPixelFormat vf_format = V4L2PixFmtToVideoPixelFormat(v4l2_pixfmt);
234 // Number of components, as opposed to the number of V4L2 planes, which is
235 // just a buffer count.
236 size_t num_planes = media::VideoFrame::NumPlanes(vf_format);
237 DCHECK_LE(num_planes, 3u);
238 if (num_planes < num_v4l2_planes) {
239 // It's possible for more than one DRM plane to reside in one V4L2 plane,
240 // but not the other way around. We must use all V4L2 planes.
241 LOG(ERROR) << "Invalid plane count";
242 return EGL_NO_IMAGE_KHR;
243 }
244
245 std::unique_ptr<base::ScopedFD[]> dmabuf_fds(
246 new base::ScopedFD[num_v4l2_planes]);
247 // Export dmabuf fds so we can create an EGLImage from them.
248 for (size_t i = 0; i < num_v4l2_planes; ++i) {
249 struct v4l2_exportbuffer expbuf;
250 memset(&expbuf, 0, sizeof(expbuf));
251 expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
252 expbuf.index = buffer_index;
253 expbuf.plane = i;
254 expbuf.flags = O_CLOEXEC;
255 if (Ioctl(VIDIOC_EXPBUF, &expbuf) != 0) {
256 return EGL_NO_IMAGE_KHR;
257 }
258 dmabuf_fds[i].reset(expbuf.fd);
259 }
260
261 std::vector<EGLint> attrs;
262 attrs.push_back(EGL_WIDTH);
263 attrs.push_back(frame_buffer_size.width());
264 attrs.push_back(EGL_HEIGHT);
265 attrs.push_back(frame_buffer_size.height());
266 attrs.push_back(EGL_LINUX_DRM_FOURCC_EXT);
267 attrs.push_back(V4L2PixFmtToDrmFormat(v4l2_pixfmt));
268
269 // For existing formats, if we have less buffers (V4L2 planes) than
270 // components (planes), the remaining planes are stored in the last
271 // V4L2 plane. Use one V4L2 plane per each component until we run out of V4L2
272 // planes, and use the last V4L2 plane for all remaining components, each
273 // with an offset equal to the size of the preceding planes in the same
274 // V4L2 plane.
275 size_t v4l2_plane = 0;
276 size_t plane_offset = 0;
277 for (size_t plane = 0; plane < num_planes; ++plane) {
278 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3);
279 attrs.push_back(dmabuf_fds[v4l2_plane].get());
280 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3);
281 attrs.push_back(plane_offset);
282 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3);
283 attrs.push_back(media::VideoFrame::RowBytes(plane, vf_format,
284 frame_buffer_size.width()));
285
286 if (v4l2_plane + 1 < num_v4l2_planes) {
287 ++v4l2_plane;
288 } else {
289 plane_offset += media::VideoFrame::PlaneSize(
290 vf_format, plane, frame_buffer_size).GetArea();
291 }
292 }
293
294 attrs.push_back(EGL_NONE);
295
296 EGLImageKHR egl_image = eglCreateImageKHR(
297 egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, &attrs[0]);
298 if (egl_image == EGL_NO_IMAGE_KHR) {
299 LOG(ERROR) << "Failed creating EGL image: " << ui::GetLastEGLErrorString();
300 return egl_image;
301 }
302 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id);
303 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image);
304
305 return egl_image;
306 }
307
308 EGLBoolean GenericV4L2Device::DestroyEGLImage(EGLDisplay egl_display,
309 EGLImageKHR egl_image) {
310 return eglDestroyImageKHR(egl_display, egl_image);
311 }
312
313 GLenum GenericV4L2Device::GetTextureTarget() { return GL_TEXTURE_EXTERNAL_OES; }
314
315 uint32_t GenericV4L2Device::PreferredInputFormat() {
316 // TODO(posciak): We should support "dontcare" returns here once we
317 // implement proper handling (fallback, negotiation) for this in users.
318 CHECK_EQ(type_, kEncoder);
319 return V4L2_PIX_FMT_NV12M;
320 }
321
322 // static
323 bool GenericV4L2Device::PostSandboxInitialization() {
324 #if defined(USE_LIBV4L2)
325 StubPathMap paths;
326 paths[kModuleV4l2].push_back(kV4l2Lib);
327
328 return InitializeStubs(paths);
329 #else
330 return true;
331 #endif
332 }
333
334 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698