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

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

Powered by Google App Engine
This is Rietveld 408576698