JeVois  1.16
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
GPUimage.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 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 #pragma once
19 
20 #ifdef JEVOIS_PRO
21 
22 #include <jevois/GPU/OpenGL.H>
23 #include <jevois/Core/InputFrame.H>
24 #include <imgui.h>
25 #include <glm/fwd.hpp>
26 #include <opencv2/core/core.hpp>
27 
28 namespace jevois
29 {
30  class GPUtexture;
31  class GPUprogram;
32 #ifdef JEVOIS_PLATFORM_PRO
33  class GPUtextureDmaBuf;
34 #endif
35 
36  //! Class to hold a GPUtexture, GPUprogram, and other data associated with rendering an image in OpenGL
37  /*! OpenGL should have been properly setup before this class is used. \ingroup gpu */
38  class GPUimage
39  {
40  public:
41  //! Constructor
42  GPUimage();
43 
44  //! Destructor
45  ~GPUimage();
46 
47  //! Set pixel data from a vanilla RawImage, pixel data will be copied to texture
48  /*! The caller must guarantee that the pixel data will not be de-allocated until after draw() is called. */
49  void set(RawImage const & img);
50 
51  //! Set pixel data from a vanilla OpenCV Mat, pixel data will be copied to texture
52  /*! If image had three or four 8-bit channels, interpret as RGB[A] if rgb is true, otherwise BGR[A]. If two 8-bit
53  channels, interpret at YUYV. If one, interpret at GRAY. The caller must guarantee that the pixel data will not
54  be de-allocated until after draw() is called. */
55  void set(cv::Mat const & img, bool rgb);
56 
57  //! Set pixel data from camera input frame, sharing data with camera kernel driver using zero-copy
58  /*! The caller must guarantee that the pixel data will not be de-allocated (e.g., by calling done() on the
59  InputFrame) until after draw() is called. Zero-copy requires DMABUF sharing between V4L2 and OpenGL-ES and is
60  only available on JeVois-Pro platform. On other hardware configs, this falls back to set(RawImage) using the
61  RawImage from the InputFrame. */
62  void set(InputFrame const & frame, EGLDisplay display);
63 
64  //! Set pixel data from camera input second (scaled) frame, sharing data with camera kernel driver using zero-copy
65  /*! The caller must guarantee that the pixel data will not be de-allocated (e.g., by calling done() on the
66  InputFrame) until after draw() is called. Zero-copy requires DMABUF sharing between V4L2 and OpenGL-ES and is
67  only available on JeVois-Pro platform. On other hardware configs, this falls back to set(RawImage) using the
68  RawImage from the InputFrame. Throws unless we are JeVois-Pro Platform and the camera is set to CropScale
69  mode. */
70  void set2(InputFrame const & frame, EGLDisplay display);
71 
72  //! Draw to OpenGL
73  /*! If w=0 or h=0 then the image will be rescaled to fill the display as much as possible without changing the
74  aspect ratio, and the actually used x,y,w,h will be returned. Otherwise, x,y,w,h are not modified. Throws if
75  set() has not been previously called. If noalias is specified, the scaling factor will be rounded down to the
76  nearest integer to prevent aliasing in the display. This may reduce the displayed image size. For example,
77  with a 1920x1080 window, a 640x480 image would be letterboxed to 1440x1080 when noalias is false. But that is
78  a scaling factor of 2.25 which may create rendering aliasing. When noalias is true, the letterboxed image size
79  will be 1280x960 (scale factor of 2.0). The matrix pvm is the projection-view-model matrix to use for
80  rendering. */
81  void draw(int & x, int & y, unsigned short & w, unsigned short & h, bool noalias, glm::mat4 const & pvm);
82 
83  //! Convert coordinates of a point from within a rendered image to on-screen
84  /*! Throws if draw() has not been previously called. */
85  ImVec2 i2d(ImVec2 const & p);
86 
87  //! Convert a 2D size from within a rendered image to on-screen
88  /*! Throws if draw() has not been previously called. */
89  ImVec2 i2ds(ImVec2 const & p);
90 
91  protected:
92  void setInternal(unsigned int width, unsigned int height, unsigned int fmt, unsigned char const * data);
93 
94  EGLDisplay itsDisplay;
95  std::shared_ptr<GPUtexture> itsTexture;
96  std::shared_ptr<GPUprogram> itsProgram;
97 #ifdef JEVOIS_PLATFORM_PRO
98  void setWithDmaBuf(jevois::RawImage const & img, int dmafd, EGLDisplay display);
99  std::shared_ptr<GPUtextureDmaBuf> itsTextureDmaBuf;
100 #endif
101  GLuint itsLocation = 0;
102  GLuint itsVertexArray = 0;
103  GLuint itsVertexBuffers[2] = { };
104 
105  int itsDrawX = -1234567, itsDrawY = -6712345;
106  unsigned int itsDrawWidth = 0, itsDrawHeight = 0;
107  unsigned int itsTextureWidth = 0, itsTextureHeight = 0;
108  unsigned int itsFormat = 0;
110  GLenum itsGLtextureFmt = 0;
111  };
112 
113 } // namespace jevois
114 
115 #endif // JEVOIS_PRO
116 
jevois::GPUimage::itsDrawWidth
unsigned int itsDrawWidth
Definition: GPUimage.H:106
jevois::GPUimage::itsTexture
std::shared_ptr< GPUtexture > itsTexture
Definition: GPUimage.H:95
jevois::GPUimage::itsTextureHeight
unsigned int itsTextureHeight
Definition: GPUimage.H:107
jevois::GPUimage::itsDisplay
EGLDisplay itsDisplay
Definition: GPUimage.H:94
jevois::GPUimage
Class to hold a GPUtexture, GPUprogram, and other data associated with rendering an image in OpenGL.
Definition: GPUimage.H:38
jevois::GPUimage::~GPUimage
~GPUimage()
Destructor.
Definition: GPUimage.C:49
jevois::RawImage
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
jevois::GPUimage::itsTextureWidth
unsigned int itsTextureWidth
Definition: GPUimage.H:107
jevois::GPUimage::itsDrawHeight
unsigned int itsDrawHeight
Definition: GPUimage.H:106
OpenGL.H
jevois::GPUimage::itsGLtextureFmt
GLenum itsGLtextureFmt
Definition: GPUimage.H:110
jevois
Definition: Concepts.dox:1
jevois::GPUimage::GPUimage
GPUimage()
Constructor.
Definition: GPUimage.C:45
jevois::GPUimage::draw
void draw(int &x, int &y, unsigned short &w, unsigned short &h, bool noalias, glm::mat4 const &pvm)
Draw to OpenGL.
Definition: GPUimage.C:257
jevois::GPUimage::i2ds
ImVec2 i2ds(ImVec2 const &p)
Convert a 2D size from within a rendered image to on-screen.
Definition: GPUimage.C:382
jevois::GPUimage::itsDrawX
int itsDrawX
Definition: GPUimage.H:105
InputFrame.H
jevois::InputFrame
Exception-safe wrapper around a raw camera input frame.
Definition: InputFrame.H:50
jevois::GPUimage::setInternal
void setInternal(unsigned int width, unsigned int height, unsigned int fmt, unsigned char const *data)
Definition: GPUimage.C:56
jevois::GPUimage::i2d
ImVec2 i2d(ImVec2 const &p)
Convert coordinates of a point from within a rendered image to on-screen.
Definition: GPUimage.C:375
jevois::GPUimage::itsProgram
std::shared_ptr< GPUprogram > itsProgram
Definition: GPUimage.H:96
h
int h
Definition: GUIhelper.C:2150
jevois::GPUimage::set
void set(RawImage const &img)
Set pixel data from a vanilla RawImage, pixel data will be copied to texture.
Definition: GPUimage.C:130
jevois::GPUimage::itsFormat
unsigned int itsFormat
Definition: GPUimage.H:108
jevois::GPUimage::itsVertexBuffers
GLuint itsVertexBuffers[2]
Definition: GPUimage.H:103
jevois::GPUimage::itsDrawY
int itsDrawY
Definition: GPUimage.H:105
jevois::GPUimage::itsVertexArray
GLuint itsVertexArray
Definition: GPUimage.H:102
jevois::GPUimage::itsGLtextureWidth
int itsGLtextureWidth
Definition: GPUimage.H:109
jevois::GPUimage::itsLocation
GLuint itsLocation
Definition: GPUimage.H:101
jevois::GPUimage::set2
void set2(InputFrame const &frame, EGLDisplay display)
Set pixel data from camera input second (scaled) frame, sharing data with camera kernel driver using ...
Definition: GPUimage.C:247