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