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.
7 #include <libdrm/drm_fourcc.h>
8 #include <linux/videodev2.h>
10 #include <sys/eventfd.h>
11 #include <sys/ioctl.h>
14 #include "base/files/scoped_file.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/trace_event/trace_event.h"
17 #include "content/common/gpu/media/generic_v4l2_device.h"
18 #include "ui/gl/egl_util.h"
19 #include "ui/gl/gl_bindings.h"
21 #if defined(USE_LIBV4L2)
22 // Auto-generated for dlopen libv4l2 libraries
23 #include "content/common/gpu/media/v4l2_stubs.h"
24 #include "third_party/v4l-utils/lib/include/libv4l2.h"
26 using content_common_gpu_media::kModuleV4l2
;
27 using content_common_gpu_media::InitializeStubs
;
28 using content_common_gpu_media::StubPathMap
;
30 static const base::FilePath::CharType kV4l2Lib
[] =
31 FILE_PATH_LITERAL("/usr/lib/libv4l2.so");
37 const char kDecoderDevice
[] = "/dev/video-dec";
38 const char kEncoderDevice
[] = "/dev/video-enc";
39 const char kImageProcessorDevice
[] = "/dev/image-proc0";
40 const char kJpegDecoderDevice
[] = "/dev/jpeg-dec";
43 GenericV4L2Device::GenericV4L2Device(Type type
)
48 GenericV4L2Device::~GenericV4L2Device() {
49 #if defined(USE_LIBV4L2)
50 if (use_libv4l2_
&& device_fd_
.is_valid())
51 v4l2_close(device_fd_
.release());
55 int GenericV4L2Device::Ioctl(int request
, void* arg
) {
56 #if defined(USE_LIBV4L2)
58 return HANDLE_EINTR(v4l2_ioctl(device_fd_
.get(), request
, arg
));
60 return HANDLE_EINTR(ioctl(device_fd_
.get(), request
, arg
));
63 bool GenericV4L2Device::Poll(bool poll_device
, bool* event_pending
) {
64 struct pollfd pollfds
[2];
68 pollfds
[0].fd
= device_poll_interrupt_fd_
.get();
69 pollfds
[0].events
= POLLIN
| POLLERR
;
73 DVLOG(3) << "Poll(): adding device fd to poll() set";
74 pollfds
[nfds
].fd
= device_fd_
.get();
75 pollfds
[nfds
].events
= POLLIN
| POLLOUT
| POLLERR
| POLLPRI
;
80 if (HANDLE_EINTR(poll(pollfds
, nfds
, -1)) == -1) {
81 DPLOG(ERROR
) << "poll() failed";
84 *event_pending
= (pollfd
!= -1 && pollfds
[pollfd
].revents
& POLLPRI
);
88 void* GenericV4L2Device::Mmap(void* addr
,
92 unsigned int offset
) {
93 return mmap(addr
, len
, prot
, flags
, device_fd_
.get(), offset
);
96 void GenericV4L2Device::Munmap(void* addr
, unsigned int len
) {
100 bool GenericV4L2Device::SetDevicePollInterrupt() {
101 DVLOG(3) << "SetDevicePollInterrupt()";
103 const uint64 buf
= 1;
104 if (HANDLE_EINTR(write(device_poll_interrupt_fd_
.get(), &buf
, sizeof(buf
))) ==
106 DPLOG(ERROR
) << "SetDevicePollInterrupt(): write() failed";
112 bool GenericV4L2Device::ClearDevicePollInterrupt() {
113 DVLOG(3) << "ClearDevicePollInterrupt()";
116 if (HANDLE_EINTR(read(device_poll_interrupt_fd_
.get(), &buf
, sizeof(buf
))) ==
118 if (errno
== EAGAIN
) {
119 // No interrupt flag set, and we're reading nonblocking. Not an error.
122 DPLOG(ERROR
) << "ClearDevicePollInterrupt(): read() failed";
129 bool GenericV4L2Device::Initialize() {
130 const char* device_path
= NULL
;
131 static bool v4l2_functions_initialized
= PostSandboxInitialization();
132 if (!v4l2_functions_initialized
) {
133 LOG(ERROR
) << "Failed to initialize LIBV4L2 libs";
139 device_path
= kDecoderDevice
;
142 device_path
= kEncoderDevice
;
144 case kImageProcessor
:
145 device_path
= kImageProcessorDevice
;
148 device_path
= kJpegDecoderDevice
;
152 DVLOG(2) << "Initialize(): opening device: " << device_path
;
153 // Open the video device.
155 HANDLE_EINTR(open(device_path
, O_RDWR
| O_NONBLOCK
| O_CLOEXEC
)));
156 if (!device_fd_
.is_valid()) {
159 #if defined(USE_LIBV4L2)
160 if (type_
== kEncoder
&&
161 HANDLE_EINTR(v4l2_fd_open(device_fd_
.get(), V4L2_DISABLE_CONVERSION
)) !=
163 DVLOG(2) << "Using libv4l2 for " << device_path
;
168 device_poll_interrupt_fd_
.reset(eventfd(0, EFD_NONBLOCK
| EFD_CLOEXEC
));
169 if (!device_poll_interrupt_fd_
.is_valid()) {
175 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt
) {
176 static uint32_t kEGLImageDrmFmtsSupported
[] = {
178 #if defined(ARCH_CPU_ARMEL)
185 kEGLImageDrmFmtsSupported
,
186 kEGLImageDrmFmtsSupported
+ arraysize(kEGLImageDrmFmtsSupported
),
187 V4L2PixFmtToDrmFormat(v4l2_pixfmt
)) !=
188 kEGLImageDrmFmtsSupported
+ arraysize(kEGLImageDrmFmtsSupported
);
191 EGLImageKHR
GenericV4L2Device::CreateEGLImage(EGLDisplay egl_display
,
192 EGLContext
/* egl_context */,
194 gfx::Size frame_buffer_size
,
195 unsigned int buffer_index
,
196 uint32_t v4l2_pixfmt
,
197 size_t num_v4l2_planes
) {
198 DVLOG(3) << "CreateEGLImage()";
199 if (!CanCreateEGLImageFrom(v4l2_pixfmt
)) {
200 LOG(ERROR
) << "Unsupported V4L2 pixel format";
201 return EGL_NO_IMAGE_KHR
;
204 media::VideoPixelFormat vf_format
= V4L2PixFmtToVideoPixelFormat(v4l2_pixfmt
);
205 // Number of components, as opposed to the number of V4L2 planes, which is
206 // just a buffer count.
207 size_t num_planes
= media::VideoFrame::NumPlanes(vf_format
);
208 DCHECK_LE(num_planes
, 3u);
209 if (num_planes
< num_v4l2_planes
) {
210 // It's possible for more than one DRM plane to reside in one V4L2 plane,
211 // but not the other way around. We must use all V4L2 planes.
212 LOG(ERROR
) << "Invalid plane count";
213 return EGL_NO_IMAGE_KHR
;
216 scoped_ptr
<base::ScopedFD
[]> dmabuf_fds(new base::ScopedFD
[num_v4l2_planes
]);
217 // Export dmabuf fds so we can create an EGLImage from them.
218 for (size_t i
= 0; i
< num_v4l2_planes
; ++i
) {
219 struct v4l2_exportbuffer expbuf
;
220 memset(&expbuf
, 0, sizeof(expbuf
));
221 expbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
222 expbuf
.index
= buffer_index
;
224 expbuf
.flags
= O_CLOEXEC
;
225 if (Ioctl(VIDIOC_EXPBUF
, &expbuf
) != 0) {
226 return EGL_NO_IMAGE_KHR
;
228 dmabuf_fds
[i
].reset(expbuf
.fd
);
231 std::vector
<EGLint
> attrs
;
232 attrs
.push_back(EGL_WIDTH
);
233 attrs
.push_back(frame_buffer_size
.width());
234 attrs
.push_back(EGL_HEIGHT
);
235 attrs
.push_back(frame_buffer_size
.height());
236 attrs
.push_back(EGL_LINUX_DRM_FOURCC_EXT
);
237 attrs
.push_back(V4L2PixFmtToDrmFormat(v4l2_pixfmt
));
239 // For existing formats, if we have less buffers (V4L2 planes) than
240 // components (planes), the remaining planes are stored in the last
241 // V4L2 plane. Use one V4L2 plane per each component until we run out of V4L2
242 // planes, and use the last V4L2 plane for all remaining components, each
243 // with an offset equal to the size of the preceding planes in the same
245 size_t v4l2_plane
= 0;
246 size_t plane_offset
= 0;
247 for (size_t plane
= 0; plane
< num_planes
; ++plane
) {
248 attrs
.push_back(EGL_DMA_BUF_PLANE0_FD_EXT
+ plane
* 3);
249 attrs
.push_back(dmabuf_fds
[v4l2_plane
].get());
250 attrs
.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT
+ plane
* 3);
251 attrs
.push_back(plane_offset
);
252 attrs
.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT
+ plane
* 3);
253 attrs
.push_back(media::VideoFrame::RowBytes(plane
, vf_format
,
254 frame_buffer_size
.width()));
256 if (v4l2_plane
+ 1 < num_v4l2_planes
) {
259 plane_offset
+= media::VideoFrame::PlaneSize(
260 vf_format
, plane
, frame_buffer_size
).GetArea();
264 attrs
.push_back(EGL_NONE
);
266 EGLImageKHR egl_image
= eglCreateImageKHR(
267 egl_display
, EGL_NO_CONTEXT
, EGL_LINUX_DMA_BUF_EXT
, NULL
, &attrs
[0]);
268 if (egl_image
== EGL_NO_IMAGE_KHR
) {
269 LOG(ERROR
) << "Failed creating EGL image: " << ui::GetLastEGLErrorString();
272 glBindTexture(GL_TEXTURE_EXTERNAL_OES
, texture_id
);
273 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES
, egl_image
);
278 EGLBoolean
GenericV4L2Device::DestroyEGLImage(EGLDisplay egl_display
,
279 EGLImageKHR egl_image
) {
280 return eglDestroyImageKHR(egl_display
, egl_image
);
283 GLenum
GenericV4L2Device::GetTextureTarget() { return GL_TEXTURE_EXTERNAL_OES
; }
285 uint32
GenericV4L2Device::PreferredInputFormat() {
286 // TODO(posciak): We should support "dontcare" returns here once we
287 // implement proper handling (fallback, negotiation) for this in users.
288 CHECK_EQ(type_
, kEncoder
);
289 return V4L2_PIX_FMT_NV12M
;
293 bool GenericV4L2Device::PostSandboxInitialization() {
294 #if defined(USE_LIBV4L2)
296 paths
[kModuleV4l2
].push_back(kV4l2Lib
);
298 return InitializeStubs(paths
);
304 } // namespace content