Chromium Code Reviews| 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 } | |
|
Ami GONE FROM CHROMIUM
2014/03/29 08:23:51
nit: newline after this
shivdasp
2014/03/29 17:47:37
Done.
| |
| 82 bool Initialized() { return initialized_; } | |
|
Ami GONE FROM CHROMIUM
2014/03/29 08:23:51
nit: simple accessors are typically lowercased:
b
shivdasp
2014/03/29 17:47:37
Done.
| |
| 83 | |
| 84 private: | |
| 85 bool initialized_; | |
| 86 }; | |
| 87 | |
| 88 base::LazyInstance<TegraFunctionSymbolFinder> g_tegra_function_symbol_finder_ = | |
| 89 LAZY_INSTANCE_INITIALIZER; | |
| 90 | |
| 91 TegraV4L2Device::TegraV4L2Device(EGLContext egl_context) | |
| 92 : device_fd_(-1), egl_context_(egl_context) {} | |
| 93 | |
| 94 TegraV4L2Device::~TegraV4L2Device() { | |
| 95 if (device_fd_ != -1) { | |
| 96 TegraV4L2_Close(device_fd_); | |
| 97 device_fd_ = -1; | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 int TegraV4L2Device::Ioctl(int flags, void* arg) { | |
| 102 return TegraV4L2_Ioctl(device_fd_, flags, arg); | |
| 103 } | |
| 104 | |
| 105 bool TegraV4L2Device::Poll(bool poll_device, bool* event_pending) { | |
| 106 if (HANDLE_EINTR(TegraV4L2_Poll(device_fd_, poll_device, event_pending)) == | |
| 107 -1) { | |
| 108 DLOG(ERROR) << "TegraV4L2Poll returned -1 "; | |
| 109 return false; | |
| 110 } | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void* TegraV4L2Device::Mmap(void* addr, | |
| 115 unsigned int len, | |
| 116 int prot, | |
| 117 int flags, | |
| 118 unsigned int offset) { | |
| 119 return TegraV4L2_Mmap(addr, len, prot, flags, device_fd_, offset); | |
| 120 } | |
| 121 | |
| 122 void TegraV4L2Device::Munmap(void* addr, unsigned int len) { | |
| 123 TegraV4L2_Munmap(addr, len); | |
| 124 } | |
| 125 | |
| 126 bool TegraV4L2Device::SetDevicePollInterrupt() { | |
| 127 if (HANDLE_EINTR(TegraV4L2_SetDevicePollInterrupt(device_fd_)) == -1) { | |
| 128 DLOG(ERROR) << "Error in calling TegraV4L2SetDevicePollInterrupt"; | |
| 129 return false; | |
| 130 } | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 bool TegraV4L2Device::ClearDevicePollInterrupt() { | |
| 135 if (HANDLE_EINTR(TegraV4L2_ClearDevicePollInterrupt(device_fd_)) == -1) { | |
| 136 DLOG(ERROR) << "Error in calling TegraV4L2ClearDevicePollInterrupt"; | |
| 137 return false; | |
| 138 } | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 bool TegraV4L2Device::Initialize() { | |
| 143 if (!g_tegra_function_symbol_finder_.Get().Initialized()) { | |
| 144 DLOG(ERROR) << "Unable to initialize functions "; | |
| 145 return false; | |
| 146 } | |
| 147 device_fd_ = | |
| 148 HANDLE_EINTR(TegraV4L2_Open(kDevice, O_RDWR | O_NONBLOCK | O_CLOEXEC)); | |
| 149 if (device_fd_ == -1) { | |
| 150 DLOG(ERROR) << "Unable to open tegra_v4l2_open "; | |
| 151 return false; | |
| 152 } | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 EGLImageKHR TegraV4L2Device::CreateEGLImage(EGLDisplay egl_display, | |
| 157 GLuint texture_id, | |
| 158 gfx::Size /* frame_buffer_size */, | |
| 159 unsigned int buffer_index, | |
| 160 size_t /* planes_count */) { | |
| 161 DVLOG(3) << "CreateEGLImage()"; | |
| 162 EGLint attr = EGL_NONE; | |
| 163 EGLImageKHR egl_image = | |
| 164 eglCreateImageKHR(egl_display, | |
| 165 egl_context_, | |
| 166 EGL_GL_TEXTURE_2D_KHR, | |
| 167 reinterpret_cast<EGLClientBuffer>(texture_id), | |
| 168 &attr); | |
| 169 if (egl_image == EGL_NO_IMAGE_KHR) { | |
| 170 return egl_image; | |
| 171 } | |
| 172 if (TegraV4L2_UseEglImage(device_fd_, buffer_index, egl_image) != 0) { | |
| 173 eglDestroyImageKHR(egl_display, egl_image); | |
| 174 egl_image = EGL_NO_IMAGE_KHR; | |
| 175 } | |
| 176 return egl_image; | |
| 177 } | |
| 178 | |
| 179 EGLBoolean TegraV4L2Device::DestroyEGLImage(EGLDisplay egl_display, | |
| 180 EGLImageKHR egl_image) { | |
| 181 return eglDestroyImageKHR(egl_display, egl_image); | |
| 182 } | |
| 183 | |
| 184 GLenum TegraV4L2Device::GetTextureTarget() { return GL_TEXTURE_2D; } | |
| 185 | |
| 186 uint32 TegraV4L2Device::PreferredOutputFormat() { return V4L2_PIX_FMT_NV12M; } | |
| 187 | |
| 188 } // namespace content | |
| OLD | NEW |