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