JeVois  1.19
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  /*! If enable_twirl is true, the image can twirl, by an amount set using setTwirl(). Useful only for demos and
43  screen saving. */
44  GPUimage(bool enable_twirl = false);
45 
46  //! Destructor
47  ~GPUimage();
48 
49  //! Set pixel data from a vanilla RawImage, pixel data will be copied to texture
50  /*! The caller must guarantee that the pixel data will not be de-allocated until after draw() is called. */
51  void set(RawImage const & img);
52 
53  //! Set pixel data from a vanilla OpenCV Mat, pixel data will be copied to texture
54  /*! If image had three or four 8-bit channels, interpret as RGB[A] if rgb is true, otherwise BGR[A]. If two 8-bit
55  channels, interpret at YUYV. If one, interpret at GRAY. The caller must guarantee that the pixel data will not
56  be de-allocated until after draw() is called. */
57  void set(cv::Mat const & img, bool rgb);
58 
59  //! Set pixel data from camera input frame, sharing data with camera kernel driver using zero-copy
60  /*! The caller must guarantee that the pixel data will not be de-allocated (e.g., by calling done() on the
61  InputFrame) until after draw() is called. Zero-copy requires DMABUF sharing between V4L2 and OpenGL-ES and is
62  only available on JeVois-Pro platform. On other hardware configs, this falls back to set(RawImage) using the
63  RawImage from the InputFrame. */
64  void set(InputFrame const & frame, EGLDisplay display);
65 
66  //! Set pixel data from camera input second (scaled) frame, sharing data with camera kernel driver using zero-copy
67  /*! The caller must guarantee that the pixel data will not be de-allocated (e.g., by calling done() on the
68  InputFrame) until after draw() is called. Zero-copy requires DMABUF sharing between V4L2 and OpenGL-ES and is
69  only available on JeVois-Pro platform. On other hardware configs, this falls back to set(RawImage) using the
70  RawImage from the InputFrame. Throws unless we are JeVois-Pro Platform and the camera is set to CropScale
71  mode. */
72  void set2(InputFrame const & frame, EGLDisplay display);
73 
74  //! Draw to OpenGL
75  /*! If w=0 or h=0 then the image will be rescaled to fill the display as much as possible without changing the
76  aspect ratio, and the actually used x,y,w,h will be returned. Otherwise, x,y,w,h are not modified. Throws if
77  set() has not been previously called. If noalias is specified, the scaling factor will be rounded down to the
78  nearest integer to prevent aliasing in the display. This may reduce the displayed image size. For example,
79  with a 1920x1080 window, a 640x480 image would be letterboxed to 1440x1080 when noalias is false. But that is
80  a scaling factor of 2.25 which may create rendering aliasing. When noalias is true, the letterboxed image size
81  will be 1280x960 (scale factor of 2.0). The matrix pvm is the projection-view-model matrix to use for
82  rendering. */
83  void draw(int & x, int & y, unsigned short & w, unsigned short & h, bool noalias, glm::mat4 const & pvm);
84 
85  //! Convert coordinates of a point from within a rendered image to on-screen
86  /*! Throws if draw() has not been previously called. */
87  ImVec2 i2d(ImVec2 const & p);
88 
89  //! Convert a 2D size from within a rendered image to on-screen
90  /*! Throws if draw() has not been previously called. */
91  ImVec2 i2ds(ImVec2 const & p);
92 
93  //! Convert coordinates of a point from on-screen to within a rendered image
94  /*! Throws if draw() has not been previously called. */
95  ImVec2 d2i(ImVec2 const & p);
96 
97  //! Convert a 2D size from on-screen to within a rendered image
98  /*! Throws if draw() has not been previously called. */
99  ImVec2 d2is(ImVec2 const & p);
100 
101  //! Set the twirl amount if the image was constructed with twirl enabled
102  void setTwirl(float t);
103 
104  protected:
105  void setInternal(unsigned int width, unsigned int height, unsigned int fmt, unsigned char const * data);
106 
107  EGLDisplay itsDisplay;
108  std::shared_ptr<GPUtexture> itsTexture;
109  std::shared_ptr<GPUprogram> itsProgram;
110 #ifdef JEVOIS_PLATFORM_PRO
111  void setWithDmaBuf(jevois::RawImage const & img, int dmafd, EGLDisplay display);
112  std::shared_ptr<GPUtextureDmaBuf> itsTextureDmaBuf;
113 #endif
114  GLuint itsLocation = 0;
115  GLuint itsVertexArray = 0;
116  GLuint itsVertexBuffers[2] = { };
117 
118  int itsDrawX = -1234567, itsDrawY = -6712345;
119  unsigned int itsDrawWidth = 0, itsDrawHeight = 0;
120  unsigned int itsTextureWidth = 0, itsTextureHeight = 0;
121  unsigned int itsFormat = 0;
123  GLenum itsGLtextureFmt = 0;
124  bool itsTwirl;
125  GLfloat itsTwirlAmount = 0;
126  };
127 
128 } // namespace jevois
129 
130 #endif // JEVOIS_PRO
131 
jevois::GPUimage::itsDrawWidth
unsigned int itsDrawWidth
Definition: GPUimage.H:119
jevois::GPUimage::itsTexture
std::shared_ptr< GPUtexture > itsTexture
Definition: GPUimage.H:108
jevois::GPUimage::itsTextureHeight
unsigned int itsTextureHeight
Definition: GPUimage.H:120
jevois::GPUimage::itsDisplay
EGLDisplay itsDisplay
Definition: GPUimage.H:107
jevois::GPUimage
Class to hold a GPUtexture, GPUprogram, and other data associated with rendering an image in OpenGL.
Definition: GPUimage.H:38
jevois::GPUimage::itsTextureDmaBuf
std::shared_ptr< GPUtextureDmaBuf > itsTextureDmaBuf
Definition: GPUimage.H:112
jevois::GPUimage::~GPUimage
~GPUimage()
Destructor.
Definition: GPUimage.C:54
jevois::GPUimage::setWithDmaBuf
void setWithDmaBuf(jevois::RawImage const &img, int dmafd, EGLDisplay display)
Definition: GPUimage.C:214
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:120
jevois::GPUimage::itsDrawHeight
unsigned int itsDrawHeight
Definition: GPUimage.H:119
jevois::GPUimage::setTwirl
void setTwirl(float t)
Set the twirl amount if the image was constructed with twirl enabled.
Definition: GPUimage.C:412
OpenGL.H
jevois::GPUimage::itsGLtextureFmt
GLenum itsGLtextureFmt
Definition: GPUimage.H:123
jevois
Definition: Concepts.dox:1
jevois::GPUimage::itsTwirlAmount
GLfloat itsTwirlAmount
Definition: GPUimage.H:125
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:264
jevois::GPUimage::itsTwirl
bool itsTwirl
Definition: GPUimage.H:124
jevois::GPUimage::i2ds
ImVec2 i2ds(ImVec2 const &p)
Convert a 2D size from within a rendered image to on-screen.
Definition: GPUimage.C:391
jevois::GPUimage::itsDrawX
int itsDrawX
Definition: GPUimage.H:118
InputFrame.H
jevois::InputFrame
Exception-safe wrapper around a raw camera input frame.
Definition: InputFrame.H:50
jevois::GPUimage::GPUimage
GPUimage(bool enable_twirl=false)
Constructor.
Definition: GPUimage.C:50
jevois::GPUimage::setInternal
void setInternal(unsigned int width, unsigned int height, unsigned int fmt, unsigned char const *data)
Definition: GPUimage.C:61
jevois::GPUimage::i2d
ImVec2 i2d(ImVec2 const &p)
Convert coordinates of a point from within a rendered image to on-screen.
Definition: GPUimage.C:384
jevois::GPUimage::itsProgram
std::shared_ptr< GPUprogram > itsProgram
Definition: GPUimage.H:109
h
int h
Definition: GUIhelper.C:2022
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:136
jevois::GPUimage::itsFormat
unsigned int itsFormat
Definition: GPUimage.H:121
jevois::GPUimage::itsVertexBuffers
GLuint itsVertexBuffers[2]
Definition: GPUimage.H:116
jevois::GPUimage::itsDrawY
int itsDrawY
Definition: GPUimage.H:118
jevois::GPUimage::itsVertexArray
GLuint itsVertexArray
Definition: GPUimage.H:115
jevois::GPUimage::itsGLtextureWidth
int itsGLtextureWidth
Definition: GPUimage.H:122
jevois::GPUimage::d2i
ImVec2 d2i(ImVec2 const &p)
Convert coordinates of a point from on-screen to within a rendered image.
Definition: GPUimage.C:398
jevois::GPUimage::itsLocation
GLuint itsLocation
Definition: GPUimage.H:114
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:187
jevois::GPUimage::d2is
ImVec2 d2is(ImVec2 const &p)
Convert a 2D size from on-screen to within a rendered image.
Definition: GPUimage.C:405