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.
7 #include <linux/videodev2.h>
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"
18 const char kDecoderDevice
[] = "/dev/tegra_avpchannel";
19 const char kEncoderDevice
[] = "/dev/nvhost-msenc";
22 typedef int32 (*TegraV4L2Open
)(const char* name
, int32 flags
);
23 typedef int32 (*TegraV4L2Close
)(int32 fd
);
24 typedef int32 (*TegraV4L2Ioctl
)(int32 fd
, unsigned long cmd
, ...);
25 typedef int32 (*TegraV4L2Poll
)(int32 fd
, bool poll_device
, bool* event_pending
);
26 typedef int32 (*TegraV4L2SetDevicePollInterrupt
)(int32 fd
);
27 typedef int32 (*TegraV4L2ClearDevicePollInterrupt
)(int32 fd
);
28 typedef void* (*TegraV4L2Mmap
)(void* addr
,
34 typedef int32 (*TegraV4L2Munmap
)(void* addr
, size_t length
);
35 typedef int32 (*TegraV4L2UseEglImage
)(int fd
,
36 unsigned int buffer_index
,
39 #define TEGRAV4L2_SYM(name) TegraV4L2##name TegraV4L2_##name = NULL
45 TEGRAV4L2_SYM(SetDevicePollInterrupt
);
46 TEGRAV4L2_SYM(ClearDevicePollInterrupt
);
48 TEGRAV4L2_SYM(Munmap
);
49 TEGRAV4L2_SYM(UseEglImage
);
53 class TegraFunctionSymbolFinder
{
55 TegraFunctionSymbolFinder() : initialized_(false) {
56 if (!dlopen("/usr/lib/libtegrav4l2.so",
57 RTLD_NOW
| RTLD_GLOBAL
| RTLD_NODELETE
)) {
58 DLOG(ERROR
) << "Failed to load libtegrav4l2.so";
61 #define TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(name) \
63 TegraV4L2_##name = reinterpret_cast<TegraV4L2##name>( \
64 dlsym(RTLD_DEFAULT, "TegraV4L2_" #name)); \
65 if (TegraV4L2_##name == NULL) { \
66 LOG(ERROR) << "Failed to dlsym TegraV4L2_" #name; \
71 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Open
);
72 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Close
);
73 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Ioctl
);
74 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Poll
);
75 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(SetDevicePollInterrupt
);
76 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(ClearDevicePollInterrupt
);
77 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Mmap
);
78 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(Munmap
);
79 TEGRAV4L2_DLSYM_OR_RETURN_ON_ERROR(UseEglImage
);
80 #undef TEGRAV4L2_DLSYM
84 bool initialized() { return initialized_
; }
90 base::LazyInstance
<TegraFunctionSymbolFinder
> g_tegra_function_symbol_finder_
=
91 LAZY_INSTANCE_INITIALIZER
;
93 TegraV4L2Device::TegraV4L2Device(Type type
)
98 TegraV4L2Device::~TegraV4L2Device() {
99 if (device_fd_
!= -1) {
100 TegraV4L2_Close(device_fd_
);
105 int TegraV4L2Device::Ioctl(int flags
, void* arg
) {
106 return HANDLE_EINTR(TegraV4L2_Ioctl(device_fd_
, flags
, arg
));
109 bool TegraV4L2Device::Poll(bool poll_device
, bool* event_pending
) {
110 if (HANDLE_EINTR(TegraV4L2_Poll(device_fd_
, poll_device
, event_pending
)) ==
112 LOG(ERROR
) << "TegraV4L2Poll returned -1 ";
118 void* TegraV4L2Device::Mmap(void* addr
,
122 unsigned int offset
) {
123 return TegraV4L2_Mmap(addr
, len
, prot
, flags
, device_fd_
, offset
);
126 void TegraV4L2Device::Munmap(void* addr
, unsigned int len
) {
127 TegraV4L2_Munmap(addr
, len
);
130 bool TegraV4L2Device::SetDevicePollInterrupt() {
131 if (HANDLE_EINTR(TegraV4L2_SetDevicePollInterrupt(device_fd_
)) == -1) {
132 LOG(ERROR
) << "Error in calling TegraV4L2SetDevicePollInterrupt";
138 bool TegraV4L2Device::ClearDevicePollInterrupt() {
139 if (HANDLE_EINTR(TegraV4L2_ClearDevicePollInterrupt(device_fd_
)) == -1) {
140 LOG(ERROR
) << "Error in calling TegraV4L2ClearDevicePollInterrupt";
146 bool TegraV4L2Device::Initialize() {
147 const char* device_path
= NULL
;
150 device_path
= kDecoderDevice
;
153 device_path
= kEncoderDevice
;
155 case kImageProcessor
:
156 DVLOG(1) << "Device type " << type_
<< " not supported on this platform";
160 if (!g_tegra_function_symbol_finder_
.Get().initialized()) {
161 DLOG(ERROR
) << "Unable to initialize functions";
164 device_fd_
= HANDLE_EINTR(
165 TegraV4L2_Open(device_path
, O_RDWR
| O_NONBLOCK
| O_CLOEXEC
));
166 if (device_fd_
== -1) {
167 DLOG(ERROR
) << "Unable to open device " << device_path
;
173 EGLImageKHR
TegraV4L2Device::CreateEGLImage(EGLDisplay egl_display
,
174 EGLContext egl_context
,
176 gfx::Size
/* frame_buffer_size */,
177 unsigned int buffer_index
,
178 size_t /* planes_count */) {
179 DVLOG(3) << "CreateEGLImage()";
180 EGLint attr
= EGL_NONE
;
181 EGLImageKHR egl_image
=
182 eglCreateImageKHR(egl_display
,
184 EGL_GL_TEXTURE_2D_KHR
,
185 reinterpret_cast<EGLClientBuffer
>(texture_id
),
187 if (egl_image
== EGL_NO_IMAGE_KHR
) {
188 LOG(ERROR
) << "Unable to create EGL image";
191 if (TegraV4L2_UseEglImage(device_fd_
, buffer_index
, egl_image
) != 0) {
192 LOG(ERROR
) << "Unable to use EGL image";
193 eglDestroyImageKHR(egl_display
, egl_image
);
194 egl_image
= EGL_NO_IMAGE_KHR
;
199 EGLBoolean
TegraV4L2Device::DestroyEGLImage(EGLDisplay egl_display
,
200 EGLImageKHR egl_image
) {
201 return eglDestroyImageKHR(egl_display
, egl_image
);
204 GLenum
TegraV4L2Device::GetTextureTarget() { return GL_TEXTURE_2D
; }
206 uint32
TegraV4L2Device::PreferredInputFormat() {
207 // TODO(posciak): We should support "dontcare" returns here once we
208 // implement proper handling (fallback, negotiation) for this in users.
209 CHECK_EQ(type_
, kEncoder
);
210 return V4L2_PIX_FMT_YUV420M
;
213 uint32
TegraV4L2Device::PreferredOutputFormat() {
214 // TODO(posciak): We should support "dontcare" returns here once we
215 // implement proper handling (fallback, negotiation) for this in users.
216 CHECK_EQ(type_
, kDecoder
);
217 return V4L2_PIX_FMT_NV12M
;
220 } // namespace content