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 #include <linux/videodev2.h> |
| 9 |
| 10 #include "base/debug/trace_event.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 TegraV4L2Device::TegraV4L2Device(EGLContext egl_context) |
| 53 : device_fd_(-1), egl_context_(egl_context) {} |
| 54 |
| 55 TegraV4L2Device::~TegraV4L2Device() { |
| 56 if (device_fd_ != -1) { |
| 57 TegraV4L2_Close(device_fd_); |
| 58 device_fd_ = -1; |
| 59 } |
| 60 } |
| 61 |
| 62 int TegraV4L2Device::Ioctl(int flags, void* arg) { |
| 63 return TegraV4L2_Ioctl(device_fd_, flags, arg); |
| 64 } |
| 65 |
| 66 bool TegraV4L2Device::Poll(bool poll_device, bool* event_pending) { |
| 67 if (HANDLE_EINTR(TegraV4L2_Poll(device_fd_, poll_device, event_pending)) == |
| 68 -1) { |
| 69 DLOG(ERROR) << "TegraV4L2Poll returned -1 "; |
| 70 return false; |
| 71 } |
| 72 return true; |
| 73 } |
| 74 |
| 75 void* TegraV4L2Device::Mmap(void* addr, |
| 76 unsigned int len, |
| 77 int prot, |
| 78 int flags, |
| 79 unsigned int offset) { |
| 80 return TegraV4L2_Mmap(addr, len, prot, flags, device_fd_, offset); |
| 81 } |
| 82 |
| 83 void TegraV4L2Device::Munmap(void* addr, unsigned int len) { |
| 84 TegraV4L2_Munmap(addr, len); |
| 85 } |
| 86 |
| 87 bool TegraV4L2Device::SetDevicePollInterrupt() { |
| 88 if (HANDLE_EINTR(TegraV4L2_SetDevicePollInterrupt(device_fd_)) == -1) { |
| 89 DLOG(ERROR) << "Error in calling TegraV4L2SetDevicePollInterrupt"; |
| 90 return false; |
| 91 } |
| 92 return true; |
| 93 } |
| 94 |
| 95 bool TegraV4L2Device::ClearDevicePollInterrupt() { |
| 96 if (HANDLE_EINTR(TegraV4L2_ClearDevicePollInterrupt(device_fd_)) == -1) { |
| 97 DLOG(ERROR) << "Error in calling TegraV4L2ClearDevicePollInterrupt"; |
| 98 return false; |
| 99 } |
| 100 return true; |
| 101 } |
| 102 |
| 103 bool TegraV4L2Device::Initialize() { |
| 104 static bool functions_initialized = InitializeLibrarySymbols(); |
| 105 |
| 106 if (!functions_initialized) { |
| 107 DLOG(ERROR) << "Unable to initialize functions "; |
| 108 return false; |
| 109 } |
| 110 device_fd_ = |
| 111 HANDLE_EINTR(TegraV4L2_Open(kDevice, O_RDWR | O_NONBLOCK | O_CLOEXEC)); |
| 112 if (device_fd_ == -1) { |
| 113 DLOG(ERROR) << "Unable to open tegra_v4l2_open "; |
| 114 return false; |
| 115 } |
| 116 return true; |
| 117 } |
| 118 |
| 119 EGLImageKHR TegraV4L2Device::CreateEGLImage(EGLDisplay egl_display, |
| 120 GLuint texture_id, |
| 121 gfx::Size /* frame_buffer_size */, |
| 122 unsigned int buffer_index) { |
| 123 DVLOG(3) << "CreateEGLImage()"; |
| 124 EGLint attr = EGL_NONE; |
| 125 EGLImageKHR egl_image = |
| 126 eglCreateImageKHR(egl_display, |
| 127 egl_context_, |
| 128 EGL_GL_TEXTURE_2D_KHR, |
| 129 reinterpret_cast<EGLClientBuffer>(texture_id), |
| 130 &attr); |
| 131 if (egl_image == EGL_NO_IMAGE_KHR) { |
| 132 return egl_image; |
| 133 } |
| 134 if (TegraV4L2_UseEglImage(device_fd_, buffer_index, egl_image) != 0) { |
| 135 eglDestroyImageKHR(egl_display, egl_image); |
| 136 egl_image = EGL_NO_IMAGE_KHR; |
| 137 } |
| 138 return egl_image; |
| 139 } |
| 140 |
| 141 EGLBoolean TegraV4L2Device::DestroyEGLImage(EGLDisplay egl_display, |
| 142 EGLImageKHR egl_image) { |
| 143 return eglDestroyImageKHR(egl_display, egl_image); |
| 144 } |
| 145 |
| 146 GLenum TegraV4L2Device::GetTextureTarget() { return GL_TEXTURE_2D; } |
| 147 |
| 148 uint32 TegraV4L2Device::PreferredOutputFormat() { return V4L2_PIX_FMT_NV12M; } |
| 149 |
| 150 bool TegraV4L2Device::InitializeLibrarySymbols() { |
| 151 #define TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(name) \ |
| 152 do { \ |
| 153 TegraV4L2_##name = reinterpret_cast<TegraV4L2##name>( \ |
| 154 dlsym(RTLD_DEFAULT, "TegraV4L2_" #name)); \ |
| 155 if (TegraV4L2_##name == NULL) { \ |
| 156 LOG(ERROR) << "Failed to dlsym TegraV4L2_" #name; \ |
| 157 return false; \ |
| 158 } \ |
| 159 } while (0) |
| 160 |
| 161 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Open); |
| 162 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Close); |
| 163 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Ioctl); |
| 164 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Poll); |
| 165 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(SetDevicePollInterrupt); |
| 166 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(ClearDevicePollInterrupt); |
| 167 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Mmap); |
| 168 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Munmap); |
| 169 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(UseEglImage); |
| 170 #undef TEGRAV4L2_DLSYM |
| 171 return true; |
| 172 } |
| 173 |
| 174 } // namespace content |
OLD | NEW |