| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 | 5 |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <libdrm/drm_fourcc.h> | 8 #include <libdrm/drm_fourcc.h> |
| 9 #include <linux/videodev2.h> | 9 #include <linux/videodev2.h> |
| 10 #include <poll.h> | 10 #include <poll.h> |
| 11 #include <string.h> | 11 #include <string.h> |
| 12 #include <sys/eventfd.h> | 12 #include <sys/eventfd.h> |
| 13 #include <sys/ioctl.h> | 13 #include <sys/ioctl.h> |
| 14 #include <sys/mman.h> | 14 #include <sys/mman.h> |
| 15 | 15 |
| 16 #include "base/files/scoped_file.h" | 16 #include "base/files/scoped_file.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
| 20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 21 #include "content/common/gpu/media/generic_v4l2_device.h" | 21 #include "media/gpu/generic_v4l2_device.h" |
| 22 #include "ui/gl/egl_util.h" | 22 #include "ui/gl/egl_util.h" |
| 23 #include "ui/gl/gl_bindings.h" | 23 #include "ui/gl/gl_bindings.h" |
| 24 | 24 |
| 25 #if defined(USE_LIBV4L2) | 25 #if defined(USE_LIBV4L2) |
| 26 // Auto-generated for dlopen libv4l2 libraries | 26 // Auto-generated for dlopen libv4l2 libraries |
| 27 #include "content/common/gpu/media/v4l2_stubs.h" | 27 #include "media/gpu/v4l2_stubs.h" |
| 28 #include "third_party/v4l-utils/lib/include/libv4l2.h" | 28 #include "third_party/v4l-utils/lib/include/libv4l2.h" |
| 29 | 29 |
| 30 using content_common_gpu_media::kModuleV4l2; | 30 using media_gpu::kModuleV4l2; |
| 31 using content_common_gpu_media::InitializeStubs; | 31 using media_gpu::InitializeStubs; |
| 32 using content_common_gpu_media::StubPathMap; | 32 using media_gpu::StubPathMap; |
| 33 | 33 |
| 34 static const base::FilePath::CharType kV4l2Lib[] = | 34 static const base::FilePath::CharType kV4l2Lib[] = |
| 35 FILE_PATH_LITERAL("/usr/lib/libv4l2.so"); | 35 FILE_PATH_LITERAL("/usr/lib/libv4l2.so"); |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 namespace content { | 38 namespace media { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 const char kDecoderDevice[] = "/dev/video-dec"; | 41 const char kDecoderDevice[] = "/dev/video-dec"; |
| 42 const char kEncoderDevice[] = "/dev/video-enc"; | 42 const char kEncoderDevice[] = "/dev/video-enc"; |
| 43 const char kImageProcessorDevice[] = "/dev/image-proc0"; | 43 const char kImageProcessorDevice[] = "/dev/image-proc0"; |
| 44 const char kJpegDecoderDevice[] = "/dev/jpeg-dec"; | 44 const char kJpegDecoderDevice[] = "/dev/jpeg-dec"; |
| 45 } | 45 } |
| 46 | 46 |
| 47 GenericV4L2Device::GenericV4L2Device(Type type) | 47 GenericV4L2Device::GenericV4L2Device(Type type) |
| 48 : V4L2Device(type), | 48 : V4L2Device(type), use_libv4l2_(false) {} |
| 49 use_libv4l2_(false) { | |
| 50 } | |
| 51 | 49 |
| 52 GenericV4L2Device::~GenericV4L2Device() { | 50 GenericV4L2Device::~GenericV4L2Device() { |
| 53 #if defined(USE_LIBV4L2) | 51 #if defined(USE_LIBV4L2) |
| 54 if (use_libv4l2_ && device_fd_.is_valid()) | 52 if (use_libv4l2_ && device_fd_.is_valid()) |
| 55 v4l2_close(device_fd_.release()); | 53 v4l2_close(device_fd_.release()); |
| 56 #endif | 54 #endif |
| 57 } | 55 } |
| 58 | 56 |
| 59 int GenericV4L2Device::Ioctl(int request, void* arg) { | 57 int GenericV4L2Device::Ioctl(int request, void* arg) { |
| 60 #if defined(USE_LIBV4L2) | 58 #if defined(USE_LIBV4L2) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 83 | 81 |
| 84 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { | 82 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { |
| 85 DPLOG(ERROR) << "poll() failed"; | 83 DPLOG(ERROR) << "poll() failed"; |
| 86 return false; | 84 return false; |
| 87 } | 85 } |
| 88 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); | 86 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); |
| 89 return true; | 87 return true; |
| 90 } | 88 } |
| 91 | 89 |
| 92 void* GenericV4L2Device::Mmap(void* addr, | 90 void* GenericV4L2Device::Mmap(void* addr, |
| 93 unsigned int len, | 91 unsigned int len, |
| 94 int prot, | 92 int prot, |
| 95 int flags, | 93 int flags, |
| 96 unsigned int offset) { | 94 unsigned int offset) { |
| 97 return mmap(addr, len, prot, flags, device_fd_.get(), offset); | 95 return mmap(addr, len, prot, flags, device_fd_.get(), offset); |
| 98 } | 96 } |
| 99 | 97 |
| 100 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { | 98 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { |
| 101 munmap(addr, len); | 99 munmap(addr, len); |
| 102 } | 100 } |
| 103 | 101 |
| 104 bool GenericV4L2Device::SetDevicePollInterrupt() { | 102 bool GenericV4L2Device::SetDevicePollInterrupt() { |
| 105 DVLOG(3) << "SetDevicePollInterrupt()"; | 103 DVLOG(3) << "SetDevicePollInterrupt()"; |
| 106 | 104 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { | 202 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { |
| 205 static uint32_t kEGLImageDrmFmtsSupported[] = { | 203 static uint32_t kEGLImageDrmFmtsSupported[] = { |
| 206 DRM_FORMAT_ARGB8888, | 204 DRM_FORMAT_ARGB8888, |
| 207 #if defined(ARCH_CPU_ARMEL) | 205 #if defined(ARCH_CPU_ARMEL) |
| 208 DRM_FORMAT_NV12, | 206 DRM_FORMAT_NV12, |
| 209 DRM_FORMAT_MT21, | 207 DRM_FORMAT_MT21, |
| 210 #endif | 208 #endif |
| 211 }; | 209 }; |
| 212 | 210 |
| 213 return std::find( | 211 return std::find( |
| 214 kEGLImageDrmFmtsSupported, | 212 kEGLImageDrmFmtsSupported, |
| 215 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported), | 213 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported), |
| 216 V4L2PixFmtToDrmFormat(v4l2_pixfmt)) != | 214 V4L2PixFmtToDrmFormat(v4l2_pixfmt)) != |
| 217 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported); | 215 kEGLImageDrmFmtsSupported + arraysize(kEGLImageDrmFmtsSupported); |
| 218 } | 216 } |
| 219 | 217 |
| 220 EGLImageKHR GenericV4L2Device::CreateEGLImage(EGLDisplay egl_display, | 218 EGLImageKHR GenericV4L2Device::CreateEGLImage(EGLDisplay egl_display, |
| 221 EGLContext /* egl_context */, | 219 EGLContext /* egl_context */, |
| 222 GLuint texture_id, | 220 GLuint texture_id, |
| 223 gfx::Size frame_buffer_size, | 221 gfx::Size frame_buffer_size, |
| 224 unsigned int buffer_index, | 222 unsigned int buffer_index, |
| 225 uint32_t v4l2_pixfmt, | 223 uint32_t v4l2_pixfmt, |
| 226 size_t num_v4l2_planes) { | 224 size_t num_v4l2_planes) { |
| 227 DVLOG(3) << "CreateEGLImage()"; | 225 DVLOG(3) << "CreateEGLImage()"; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // V4L2 plane. | 272 // V4L2 plane. |
| 275 size_t v4l2_plane = 0; | 273 size_t v4l2_plane = 0; |
| 276 size_t plane_offset = 0; | 274 size_t plane_offset = 0; |
| 277 for (size_t plane = 0; plane < num_planes; ++plane) { | 275 for (size_t plane = 0; plane < num_planes; ++plane) { |
| 278 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3); | 276 attrs.push_back(EGL_DMA_BUF_PLANE0_FD_EXT + plane * 3); |
| 279 attrs.push_back(dmabuf_fds[v4l2_plane].get()); | 277 attrs.push_back(dmabuf_fds[v4l2_plane].get()); |
| 280 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3); | 278 attrs.push_back(EGL_DMA_BUF_PLANE0_OFFSET_EXT + plane * 3); |
| 281 attrs.push_back(plane_offset); | 279 attrs.push_back(plane_offset); |
| 282 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3); | 280 attrs.push_back(EGL_DMA_BUF_PLANE0_PITCH_EXT + plane * 3); |
| 283 attrs.push_back(media::VideoFrame::RowBytes(plane, vf_format, | 281 attrs.push_back(media::VideoFrame::RowBytes(plane, vf_format, |
| 284 frame_buffer_size.width())); | 282 frame_buffer_size.width())); |
| 285 | 283 |
| 286 if (v4l2_plane + 1 < num_v4l2_planes) { | 284 if (v4l2_plane + 1 < num_v4l2_planes) { |
| 287 ++v4l2_plane; | 285 ++v4l2_plane; |
| 288 } else { | 286 } else { |
| 289 plane_offset += media::VideoFrame::PlaneSize( | 287 plane_offset += |
| 290 vf_format, plane, frame_buffer_size).GetArea(); | 288 media::VideoFrame::PlaneSize(vf_format, plane, frame_buffer_size) |
| 289 .GetArea(); |
| 291 } | 290 } |
| 292 } | 291 } |
| 293 | 292 |
| 294 attrs.push_back(EGL_NONE); | 293 attrs.push_back(EGL_NONE); |
| 295 | 294 |
| 296 EGLImageKHR egl_image = eglCreateImageKHR( | 295 EGLImageKHR egl_image = eglCreateImageKHR( |
| 297 egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, &attrs[0]); | 296 egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, &attrs[0]); |
| 298 if (egl_image == EGL_NO_IMAGE_KHR) { | 297 if (egl_image == EGL_NO_IMAGE_KHR) { |
| 299 LOG(ERROR) << "Failed creating EGL image: " << ui::GetLastEGLErrorString(); | 298 LOG(ERROR) << "Failed creating EGL image: " << ui::GetLastEGLErrorString(); |
| 300 return egl_image; | 299 return egl_image; |
| 301 } | 300 } |
| 302 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); | 301 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); |
| 303 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image); | 302 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image); |
| 304 | 303 |
| 305 return egl_image; | 304 return egl_image; |
| 306 } | 305 } |
| 307 | 306 |
| 308 EGLBoolean GenericV4L2Device::DestroyEGLImage(EGLDisplay egl_display, | 307 EGLBoolean GenericV4L2Device::DestroyEGLImage(EGLDisplay egl_display, |
| 309 EGLImageKHR egl_image) { | 308 EGLImageKHR egl_image) { |
| 310 return eglDestroyImageKHR(egl_display, egl_image); | 309 return eglDestroyImageKHR(egl_display, egl_image); |
| 311 } | 310 } |
| 312 | 311 |
| 313 GLenum GenericV4L2Device::GetTextureTarget() { return GL_TEXTURE_EXTERNAL_OES; } | 312 GLenum GenericV4L2Device::GetTextureTarget() { |
| 313 return GL_TEXTURE_EXTERNAL_OES; |
| 314 } |
| 314 | 315 |
| 315 uint32_t GenericV4L2Device::PreferredInputFormat() { | 316 uint32_t GenericV4L2Device::PreferredInputFormat() { |
| 316 // TODO(posciak): We should support "dontcare" returns here once we | 317 // TODO(posciak): We should support "dontcare" returns here once we |
| 317 // implement proper handling (fallback, negotiation) for this in users. | 318 // implement proper handling (fallback, negotiation) for this in users. |
| 318 CHECK_EQ(type_, kEncoder); | 319 CHECK_EQ(type_, kEncoder); |
| 319 return V4L2_PIX_FMT_NV12M; | 320 return V4L2_PIX_FMT_NV12M; |
| 320 } | 321 } |
| 321 | 322 |
| 322 // static | 323 // static |
| 323 bool GenericV4L2Device::PostSandboxInitialization() { | 324 bool GenericV4L2Device::PostSandboxInitialization() { |
| 324 #if defined(USE_LIBV4L2) | 325 #if defined(USE_LIBV4L2) |
| 325 StubPathMap paths; | 326 StubPathMap paths; |
| 326 paths[kModuleV4l2].push_back(kV4l2Lib); | 327 paths[kModuleV4l2].push_back(kV4l2Lib); |
| 327 | 328 |
| 328 return InitializeStubs(paths); | 329 return InitializeStubs(paths); |
| 329 #else | 330 #else |
| 330 return true; | 331 return true; |
| 331 #endif | 332 #endif |
| 332 } | 333 } |
| 333 | 334 |
| 334 } // namespace content | 335 } // namespace media |
| OLD | NEW |