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 <dlfcn.h> |
| 7 #include <fcntl.h> |
| 8 |
| 9 #include "base/debug/trace_event.h" |
| 10 #include "base/posix/eintr_wrapper.h" |
| 11 #include "content/common/gpu/media/tegra_v4l2_video_device.h" |
| 12 #include "ui/gl/gl_bindings.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 const char kDevice[] = "/dev/tegra_avpchannel"; |
| 18 } |
| 19 |
| 20 typedef int32 (*TegraV4L2Open)(const char* name, int32 flags); |
| 21 typedef int32 (*TegraV4L2Close)(int32 fd); |
| 22 typedef int32 (*TegraV4L2Ioctl)(int32 fd, unsigned long cmd, ...); |
| 23 typedef int32 (*TegraV4L2Poll)(int32 fd, bool poll_device, bool* event_pending); |
| 24 typedef int32 (*TegraV4L2SetDevicePollInterrupt)(int32 fd); |
| 25 typedef int32 (*TegraV4L2ClearDevicePollInterrupt)(int32 fd); |
| 26 typedef void* (*TegraV4L2Mmap)(void* addr, |
| 27 size_t length, |
| 28 int prot, |
| 29 int flags, |
| 30 int fd, |
| 31 unsigned int offset); |
| 32 typedef int32 (*TegraV4L2Munmap)(void* addr, size_t length); |
| 33 typedef int32 (*TegraV4L2UseEglImage)(int fd, |
| 34 unsigned int buffer_index, |
| 35 void* egl_image); |
| 36 |
| 37 #define TEGRAV4L2_SYM(name) TegraV4L2##name TegraV4L2_##name = NULL |
| 38 |
| 39 TEGRAV4L2_SYM(Open); |
| 40 TEGRAV4L2_SYM(Close); |
| 41 TEGRAV4L2_SYM(Ioctl); |
| 42 TEGRAV4L2_SYM(Poll); |
| 43 TEGRAV4L2_SYM(SetDevicePollInterrupt); |
| 44 TEGRAV4L2_SYM(ClearDevicePollInterrupt); |
| 45 TEGRAV4L2_SYM(Mmap); |
| 46 TEGRAV4L2_SYM(Munmap); |
| 47 TEGRAV4L2_SYM(UseEglImage); |
| 48 |
| 49 #undef TEGRAV4L2_SYM |
| 50 |
| 51 TegraV4L2Device::TegraV4L2Device(EGLContext egl_context) |
| 52 : device_fd_(-1), egl_context_(egl_context) {} |
| 53 |
| 54 TegraV4L2Device::~TegraV4L2Device() { |
| 55 if (device_fd_ != -1) { |
| 56 TegraV4L2_Close(device_fd_); |
| 57 device_fd_ = -1; |
| 58 } |
| 59 } |
| 60 |
| 61 int TegraV4L2Device::Ioctl(int flags, void* arg) { |
| 62 return TegraV4L2_Ioctl(device_fd_, flags, arg); |
| 63 } |
| 64 |
| 65 bool TegraV4L2Device::Poll(bool poll_device, bool* event_pending) { |
| 66 if (TegraV4L2_Poll(device_fd_, poll_device, event_pending) == -1) { |
| 67 DLOG(ERROR) << "TegraV4L2Poll returned -1 "; |
| 68 return false; |
| 69 } |
| 70 return true; |
| 71 } |
| 72 |
| 73 void* TegraV4L2Device::Mmap(void* addr, |
| 74 unsigned int len, |
| 75 int prot, |
| 76 int flags, |
| 77 unsigned int offset) { |
| 78 return TegraV4L2_Mmap(addr, len, prot, flags, device_fd_, offset); |
| 79 } |
| 80 |
| 81 void TegraV4L2Device::Munmap(void* addr, unsigned int len) { |
| 82 TegraV4L2_Munmap(addr, len); |
| 83 } |
| 84 |
| 85 bool TegraV4L2Device::SetDevicePollInterrupt() { |
| 86 if (HANDLE_EINTR(TegraV4L2_SetDevicePollInterrupt(device_fd_)) == -1) { |
| 87 DLOG(ERROR) << "Error in calling TegraV4L2SetDevicePollInterrupt"; |
| 88 return false; |
| 89 } |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool TegraV4L2Device::ClearDevicePollInterrupt() { |
| 94 if (HANDLE_EINTR(TegraV4L2_ClearDevicePollInterrupt(device_fd_)) == -1) { |
| 95 DLOG(ERROR) << "Error in calling TegraV4L2ClearDevicePollInterrupt"; |
| 96 return false; |
| 97 } |
| 98 return true; |
| 99 } |
| 100 |
| 101 bool TegraV4L2Device::Initialize() { |
| 102 #define TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(name) \ |
| 103 do { \ |
| 104 TegraV4L2_##name = reinterpret_cast<TegraV4L2##name>( \ |
| 105 dlsym(RTLD_DEFAULT, "TegraV4L2_" #name)); \ |
| 106 if (TegraV4L2_##name == NULL) { \ |
| 107 LOG(ERROR) << "Failed to dlsym TegraV4L2_" #name; \ |
| 108 return false; \ |
| 109 } \ |
| 110 } while (0) |
| 111 |
| 112 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Open); |
| 113 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Close); |
| 114 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Ioctl); |
| 115 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Poll); |
| 116 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(SetDevicePollInterrupt); |
| 117 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(ClearDevicePollInterrupt); |
| 118 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Mmap); |
| 119 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Munmap); |
| 120 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(UseEglImage); |
| 121 #undef TEGRAV4L2_DLSYM |
| 122 |
| 123 device_fd_ = |
| 124 HANDLE_EINTR(TegraV4L2_Open(kDevice, O_RDWR | O_NONBLOCK | O_CLOEXEC)); |
| 125 if (device_fd_ == -1) { |
| 126 DLOG(ERROR) << "Unable to open tegra_v4l2_open "; |
| 127 return false; |
| 128 } |
| 129 return true; |
| 130 } |
| 131 |
| 132 EGLImageKHR TegraV4L2Device::CreateEGLImage(EGLDisplay egl_display, |
| 133 EGLint[] /*attrib*/, |
| 134 unsigned int texture_id, |
| 135 unsigned int buffer_index) { |
| 136 EGLint attr = EGL_NONE; |
| 137 EGLImageKHR egl_image = eglCreateImageKHR(egl_display, |
| 138 egl_context_, |
| 139 EGL_GL_TEXTURE_2D_KHR, |
| 140 (EGLClientBuffer)(texture_id), |
| 141 &attr); |
| 142 if (egl_image == EGL_NO_IMAGE_KHR) { |
| 143 return egl_image; |
| 144 } |
| 145 if (TegraV4L2_UseEglImage(device_fd_, buffer_index, egl_image) != 0) { |
| 146 eglDestroyImageKHR(egl_display, egl_image); |
| 147 egl_image = EGL_NO_IMAGE_KHR; |
| 148 } |
| 149 return egl_image; |
| 150 } |
| 151 |
| 152 unsigned int TegraV4L2Device::GetTextureTarget() { return GL_TEXTURE_2D; } |
| 153 |
| 154 } // namespace content |
OLD | NEW |