Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / media / generic_v4l2_device.cc
blob0cf00a2712d2a99a907a27097eb5bd95b2b65c9f
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 //
6 #include <fcntl.h>
7 #include <libdrm/drm_fourcc.h>
8 #include <linux/videodev2.h>
9 #include <poll.h>
10 #include <sys/eventfd.h>
11 #include <sys/ioctl.h>
12 #include <sys/mman.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");
32 #endif
34 namespace content {
36 namespace {
37 const char kDecoderDevice[] = "/dev/video-dec";
38 const char kEncoderDevice[] = "/dev/video-enc";
39 const char kImageProcessorDevice[] = "/dev/gsc0";
42 GenericV4L2Device::GenericV4L2Device(Type type)
43 : type_(type),
44 use_libv4l2_(false) {
47 GenericV4L2Device::~GenericV4L2Device() {
48 #if defined(USE_LIBV4L2)
49 if (use_libv4l2_ && device_fd_.is_valid())
50 v4l2_close(device_fd_.release());
51 #endif
54 int GenericV4L2Device::Ioctl(int request, void* arg) {
55 #if defined(USE_LIBV4L2)
56 if (use_libv4l2_)
57 return HANDLE_EINTR(v4l2_ioctl(device_fd_.get(), request, arg));
58 #endif
59 return HANDLE_EINTR(ioctl(device_fd_.get(), request, arg));
62 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) {
63 struct pollfd pollfds[2];
64 nfds_t nfds;
65 int pollfd = -1;
67 pollfds[0].fd = device_poll_interrupt_fd_.get();
68 pollfds[0].events = POLLIN | POLLERR;
69 nfds = 1;
71 if (poll_device) {
72 DVLOG(3) << "Poll(): adding device fd to poll() set";
73 pollfds[nfds].fd = device_fd_.get();
74 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI;
75 pollfd = nfds;
76 nfds++;
79 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) {
80 DPLOG(ERROR) << "poll() failed";
81 return false;
83 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI);
84 return true;
87 void* GenericV4L2Device::Mmap(void* addr,
88 unsigned int len,
89 int prot,
90 int flags,
91 unsigned int offset) {
92 return mmap(addr, len, prot, flags, device_fd_.get(), offset);
95 void GenericV4L2Device::Munmap(void* addr, unsigned int len) {
96 munmap(addr, len);
99 bool GenericV4L2Device::SetDevicePollInterrupt() {
100 DVLOG(3) << "SetDevicePollInterrupt()";
102 const uint64 buf = 1;
103 if (HANDLE_EINTR(write(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) ==
104 -1) {
105 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed";
106 return false;
108 return true;
111 bool GenericV4L2Device::ClearDevicePollInterrupt() {
112 DVLOG(3) << "ClearDevicePollInterrupt()";
114 uint64 buf;
115 if (HANDLE_EINTR(read(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) ==
116 -1) {
117 if (errno == EAGAIN) {
118 // No interrupt flag set, and we're reading nonblocking. Not an error.
119 return true;
120 } else {
121 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed";
122 return false;
125 return true;
128 bool GenericV4L2Device::Initialize() {
129 const char* device_path = NULL;
130 static bool v4l2_functions_initialized = PostSandboxInitialization();
131 if (!v4l2_functions_initialized) {
132 LOG(ERROR) << "Failed to initialize LIBV4L2 libs";
133 return false;
136 switch (type_) {
137 case kDecoder:
138 device_path = kDecoderDevice;
139 break;
140 case kEncoder:
141 device_path = kEncoderDevice;
142 break;
143 case kImageProcessor:
144 device_path = kImageProcessorDevice;
145 break;
148 DVLOG(2) << "Initialize(): opening device: " << device_path;
149 // Open the video device.
150 device_fd_.reset(
151 HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC)));
152 if (!device_fd_.is_valid()) {
153 return false;
155 #if defined(USE_LIBV4L2)
156 if (HANDLE_EINTR(v4l2_fd_open(device_fd_.get(), V4L2_DISABLE_CONVERSION)) !=
157 -1) {
158 DVLOG(2) << "Using libv4l2 for " << device_path;
159 use_libv4l2_ = true;
161 #endif
163 device_poll_interrupt_fd_.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
164 if (!device_poll_interrupt_fd_.is_valid()) {
165 return false;
167 return true;
170 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) {
171 static uint32_t kEGLImageDrmFmtsSupported[] = {
172 DRM_FORMAT_ARGB8888,
173 #if defined(ARCH_CPU_ARMEL)
174 DRM_FORMAT_NV12,
175 #endif
178 return std::find(
179 kEGLImageDrmFmtsSupported,
180 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported),
181 V4L2PixFmtToDrmFormat(v4l2_pixfmt)) !=
182 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported);
185 EGLImageKHR GenericV4L2Device::CreateEGLImage(EGLDisplay egl_display,
186 EGLContext /* egl_context */,
187 GLuint texture_id,
188 gfx::Size frame_buffer_size,
189 unsigned int buffer_index,
190 uint32_t v4l2_pixfmt,
191 size_t num_v4l2_planes) {
192 DVLOG(3) << "CreateEGLImage()";
193 if (!CanCreateEGLImageFrom(v4l2_pixfmt)) {
194 LOG(ERROR) << "Unsupported V4L2 pixel format";
195 return EGL_NO_IMAGE_KHR;
198 media::VideoFrame::Format vf_format =
199 V4L2PixFmtToVideoFrameFormat(v4l2_pixfmt);
200 // Number of components, as opposed to the number of V4L2 planes, which is
201 // just a buffer count.
202 size_t num_planes = media::VideoFrame::NumPlanes(vf_format);
203 DCHECK_LE(num_planes, 3u);
204 if (num_planes < num_v4l2_planes) {
205 // It's possible for more than one DRM plane to reside in one V4L2 plane,
206 // but not the other way around. We must use all V4L2 planes.
207 LOG(ERROR) << "Invalid plane count";
208 return EGL_NO_IMAGE_KHR;
211 scoped_ptr<base::ScopedFD[]> dmabuf_fds(new base::ScopedFD[num_v4l2_planes]);
212 // Export dmabuf fds so we can create an EGLImage from them.
213 for (size_t i = 0; i < num_v4l2_planes; ++i) {
214 struct v4l2_exportbuffer expbuf;
215 memset(&expbuf, 0, sizeof(expbuf));
216 expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
217 expbuf.index = buffer_index;
218 expbuf.plane = i;
219 expbuf.flags = O_CLOEXEC;
220 if (Ioctl(VIDIOC_EXPBUF, &expbuf) != 0) {
221 return EGL_NO_IMAGE_KHR;
223 dmabuf_fds[i].reset(expbuf.fd);
226 std::vector<EGLint> attrs;
227 attrs.push_back(EGL_WIDTH);
228 attrs.push_back(frame_buffer_size.width());
229 attrs.push_back(EGL_HEIGHT);
230 attrs.push_back(frame_buffer_size.height());
231 attrs.push_back(EGL_LINUX_DRM_FOURCC_EXT);
232 attrs.push_back(V4L2PixFmtToDrmFormat(v4l2_pixfmt));
234 // For existing formats, if we have less buffers (V4L2 planes) than
235 // components (planes), the remaining planes are stored in the last
236 // V4L2 plane. Use one V4L2 plane per each component until we run out of V4L2
237 // planes, and use the last V4L2 plane for all remaining components, each
238 // with an offset equal to the size of the preceding planes in the same
239 // V4L2 plane.
240 size_t v4l2_plane = 0;
241 size_t plane_offset = 0;
242 for (size_t plane = 0; plane < num_planes; ++plane) {
243 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3);
244 attrs.push_back(dmabuf_fds[v4l2_plane].get());
245 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3);
246 attrs.push_back(plane_offset);
247 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3);
248 attrs.push_back(media::VideoFrame::RowBytes(plane, vf_format,
249 frame_buffer_size.width()));
251 if (v4l2_plane + 1 < num_v4l2_planes) {
252 ++v4l2_plane;
253 } else {
254 plane_offset += media::VideoFrame::PlaneAllocationSize(
255 vf_format, plane, frame_buffer_size);
259 attrs.push_back(EGL_NONE);
261 EGLImageKHR egl_image = eglCreateImageKHR(
262 egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, &attrs[0]);
263 if (egl_image == EGL_NO_IMAGE_KHR) {
264 LOG(ERROR) << "Failed creating EGL image: " << ui::GetLastEGLErrorString();
265 return egl_image;
267 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id);
268 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image);
270 return egl_image;
273 EGLBoolean GenericV4L2Device::DestroyEGLImage(EGLDisplay egl_display,
274 EGLImageKHR egl_image) {
275 return eglDestroyImageKHR(egl_display, egl_image);
278 GLenum GenericV4L2Device::GetTextureTarget() { return GL_TEXTURE_EXTERNAL_OES; }
280 uint32 GenericV4L2Device::PreferredInputFormat() {
281 // TODO(posciak): We should support "dontcare" returns here once we
282 // implement proper handling (fallback, negotiation) for this in users.
283 CHECK_EQ(type_, kEncoder);
284 return V4L2_PIX_FMT_NV12M;
287 // static
288 bool GenericV4L2Device::PostSandboxInitialization() {
289 #if defined(USE_LIBV4L2)
290 StubPathMap paths;
291 paths[kModuleV4l2].push_back(kV4l2Lib);
293 return InitializeStubs(paths);
294 #else
295 return true;
296 #endif
299 } // namespace content