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