JeVois  1.20
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  //! Convert coordinates of a point from on-screen to within a rendered image
92  /*! Throws if draw() has not been previously called. */
93  ImVec2 d2i(ImVec2 const & p);
94 
95  //! Convert a 2D size from on-screen to within a rendered image
96  /*! Throws if draw() has not been previously called. */
97  ImVec2 d2is(ImVec2 const & p);
98 
99  //! Optional twirl and alpha fading effect to the image, useful mostly for demos/transitions
100  void twirl(float t, float alpha = 1.0F);
101 
102  protected:
103  void setInternal(unsigned int width, unsigned int height, unsigned int fmt, unsigned char const * data);
104 
105  EGLDisplay itsDisplay;
106  std::shared_ptr<GPUtexture> itsTexture;
107  std::shared_ptr<GPUprogram> itsProgram;
108 #ifdef JEVOIS_PLATFORM_PRO
109  void setWithDmaBuf(jevois::RawImage const & img, int dmafd, EGLDisplay display);
110  std::shared_ptr<GPUtextureDmaBuf> itsTextureDmaBuf;
111 #endif
112  GLuint itsLocation = 0;
113  GLuint itsVertexArray = 0;
114  GLuint itsVertexBuffers[2] = { };
115 
116  int itsDrawX = -1234567, itsDrawY = -6712345;
117  unsigned int itsDrawWidth = 0, itsDrawHeight = 0;
118  unsigned int itsTextureWidth = 0, itsTextureHeight = 0;
119  unsigned int itsFormat = 0;
121  GLenum itsGLtextureFmt = 0;
122  GLfloat itsTwirl = 0;
123  GLfloat itsAlpha = 0;
124  };
125 
126 } // namespace jevois
127 
128 #endif // JEVOIS_PRO
129 
jevois::GPUimage::itsDrawWidth
unsigned int itsDrawWidth
Definition: GPUimage.H:117
jevois::GPUimage::itsTexture
std::shared_ptr< GPUtexture > itsTexture
Definition: GPUimage.H:106
jevois::GPUimage::itsTextureHeight
unsigned int itsTextureHeight
Definition: GPUimage.H:118
jevois::GPUimage::itsDisplay
EGLDisplay itsDisplay
Definition: GPUimage.H:105
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:110
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:212
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:118
jevois::GPUimage::itsDrawHeight
unsigned int itsDrawHeight
Definition: GPUimage.H:117
OpenGL.H
jevois::GPUimage::itsGLtextureFmt
GLenum itsGLtextureFmt
Definition: GPUimage.H:121
jevois
Definition: Concepts.dox:1
jevois::GPUimage::itsAlpha
GLfloat itsAlpha
Definition: GPUimage.H:123
F
float F
Definition: GUIhelper.C:2373
jevois::GPUimage::twirl
void twirl(float t, float alpha=1.0F)
Optional twirl and alpha fading effect to the image, useful mostly for demos/transitions.
Definition: GPUimage.C:414
jevois::GPUimage::itsTwirl
GLfloat itsTwirl
Definition: GPUimage.H:122
jevois::GPUimage::GPUimage
GPUimage()
Constructor.
Definition: GPUimage.C:50
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:262
jevois::GPUimage::i2ds
ImVec2 i2ds(ImVec2 const &p)
Convert a 2D size from within a rendered image to on-screen.
Definition: GPUimage.C:393
jevois::GPUimage::itsDrawX
int itsDrawX
Definition: GPUimage.H:116
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: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:386
jevois::GPUimage::itsProgram
std::shared_ptr< GPUprogram > itsProgram
Definition: GPUimage.H:107
h
int h
Definition: GUIhelper.C:2373
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:119
jevois::GPUimage::itsVertexBuffers
GLuint itsVertexBuffers[2]
Definition: GPUimage.H:114
jevois::GPUimage::itsDrawY
int itsDrawY
Definition: GPUimage.H:116
jevois::GPUimage::itsVertexArray
GLuint itsVertexArray
Definition: GPUimage.H:113
jevois::GPUimage::itsGLtextureWidth
int itsGLtextureWidth
Definition: GPUimage.H:120
jevois::GPUimage::d2i
ImVec2 d2i(ImVec2 const &p)
Convert coordinates of a point from on-screen to within a rendered image.
Definition: GPUimage.C:400
jevois::GPUimage::itsLocation
GLuint itsLocation
Definition: GPUimage.H:112
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:186
jevois::GPUimage::d2is
ImVec2 d2is(ImVec2 const &p)
Convert a 2D size from on-screen to within a rendered image.
Definition: GPUimage.C:407