JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
GPUtextureDmaBuf.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #ifdef JEVOIS_PLATFORM_PRO
19 
21 #include <jevois/Util/Utils.H>
22 
23 #include <linux/videodev2.h>
24 #include <drm/drm_fourcc.h>
25 
26 // ####################################################################################################
27 jevois::GPUtextureDmaBuf::GPUtextureDmaBuf(EGLDisplay display, GLsizei width, GLsizei height,
28  unsigned int fmt, int dmafd) :
29  Width(width), Height(height), Display(display)
30 {
31  GL_CHECK(glGenTextures(1, &Id));
32 
33  // Convert V4L2 format to DRM format:
34  EGLint drm_fmt;
35  switch (fmt)
36  {
37  case V4L2_PIX_FMT_YUYV: drm_fmt = DRM_FORMAT_YUYV; break;
38  case V4L2_PIX_FMT_RGB32: drm_fmt = DRM_FORMAT_XBGR8888; break; // FIXME nasty byte swapping?
39  case V4L2_PIX_FMT_RGB565: drm_fmt = DRM_FORMAT_RGB565; break;
40  case V4L2_PIX_FMT_BGR24: drm_fmt = DRM_FORMAT_RGB888; break; // FIXME is ISP sending BGR?
41  case V4L2_PIX_FMT_RGB24: drm_fmt = DRM_FORMAT_BGR888; break; // FIXME is ISP sending BGR?
42  case V4L2_PIX_FMT_UYVY: drm_fmt = DRM_FORMAT_UYVY; break;
43 
44  // Darn, greyscale does not seem to be supported by MALI, eglCreateImageKHR() fails
45  //case V4L2_PIX_FMT_GREY: drm_fmt = DRM_FORMAT_R8; break; // FIXME should be Y8
46  //case V4L2_PIX_FMT_GREY: drm_fmt = fourcc_code('G','R','E','Y'); break; // missing from drm_fourcc.h, fails too
47 
48  // NV12 and YUV444 could be supported but we need 3 dmafd, 3 textures, etc
49  case V4L2_PIX_FMT_NV12: //drm_fmt = DRM_FORMAT_NV12; break;
50  case V4L2_PIX_FMT_YUV444: //drm_fmt = DRM_FORMAT_YUV444; break;
51 
52  // These are not supported: FIXME could add a debayer shader or is one already in MALI?
53  case V4L2_PIX_FMT_SRGGB8:
54  case V4L2_PIX_FMT_SBGGR16:
55  case V4L2_PIX_FMT_SGRBG16:
56  case V4L2_PIX_FMT_MJPEG:
57  case 0:
58  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fmt));
59  }
60 
61  // Create an image:
62  EGLint attrib_list[] =
63  {
64  EGL_WIDTH, width,
65  EGL_HEIGHT, height,
66  EGL_LINUX_DRM_FOURCC_EXT, drm_fmt,
67  EGL_DMA_BUF_PLANE0_FD_EXT, dmafd,
68  EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
69  EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(width * jevois::v4l2BytesPerPix(fmt)),
70  //EGL_YUV_COLOR_SPACE_HINT_EXT, EGL_ITU_REC601_EXT, // EGL_ITU_REC601_EXT, EGL_ITU_REC709_EXT, EGL_ITU_REC2020_EXT
71  //EGL_SAMPLE_RANGE_HINT_EXT, EGL_YUV_FULL_RANGE_EXT, // EGL_YUV_FULL_RANGE_EXT, EGL_YUV_NARROW_RANGE_EXT
72  EGL_NONE,
73  };
74 
75  Image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list);
76  if (Image == EGL_NO_IMAGE_KHR) LFATAL("eglCreateImageKHR() failed");
77 
78  glBindTexture(GL_TEXTURE_EXTERNAL_OES, Id);
79  GL_CHECK(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
80  GL_CHECK(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
81  GL_CHECK(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
82  GL_CHECK(glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
83  glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, Image);
84  glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
85 
86  LDEBUG("Created " << width << 'x' << height << " texture (id=" << Id << ", dmafd=" << dmafd << ')');
87 }
88 
89 // ####################################################################################################
91 {
92  glDeleteTextures(1, &Id);
93  eglDestroyImageKHR(Display, Image);
94 }
95 
96 #endif // JEVOIS_PLATFORM_PRO
jevois::GPUtextureDmaBuf::~GPUtextureDmaBuf
~GPUtextureDmaBuf()
Destructor, frees the texture in OpenGL.
Definition: GPUtextureDmaBuf.C:90
LDEBUG
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition: Log.H:173
GPUtextureDmaBuf.H
jevois::GPUtextureDmaBuf::Id
GLuint Id
Definition: GPUtextureDmaBuf.H:43
jevois::GPUtextureDmaBuf::Image
EGLImageKHR Image
Definition: GPUtextureDmaBuf.H:44
GL_CHECK
#define GL_CHECK(stmt)
Simple macro to check for OpenGL errors.
Definition: OpenGL.H:77
jevois::v4l2BytesPerPix
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:141
jevois::GPUtextureDmaBuf::GPUtextureDmaBuf
GPUtextureDmaBuf(EGLDisplay display, GLsizei width, GLsizei height, unsigned int fmt, int dmafd)
Constructor.
Definition: GPUtextureDmaBuf.C:27
jevois::fccstr
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:45
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Utils.H