JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
VideoDisplayGL.C
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 #ifdef JEVOIS_PRO
19 
21 #include <jevois/Debug/Log.H>
22 #include <jevois/Util/Utils.H>
23 #include <glm/glm.hpp>
24 #include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::perspective
25 
26 // ##############################################################################################################
28  jevois::VideoOutput(), itsImageQueue(std::max(size_t(2), nbufs)), itsStreaming(false)
29 { }
30 
31 // ##############################################################################################################
32 void jevois::VideoDisplayGL::setFormat(jevois::VideoMapping const & m)
33 {
34  // Nuke any old buffers:
35  itsStreaming.store(false);
36  itsBuffers.clear();
37  itsImageQueue.clear();
38  size_t const nbufs = itsImageQueue.size();
39 
40  // Allocate the buffers and make them all immediately available as RawImage:
41  unsigned int imsize = m.osize();
42 
43  for (size_t i = 0; i < nbufs; ++i)
44  {
45  itsBuffers.push_back(std::make_shared<jevois::VideoBuf>(-1, imsize, 0, -1));
46 
47  jevois::RawImage img;
48  img.width = m.ow;
49  img.height = m.oh;
50  img.fmt = m.ofmt;
51  img.fps = m.ofps;
52  img.buf = itsBuffers[i];
53  img.bufindex = i;
54 
55  // Push the RawImage to outside consumers:
56  itsImageQueue.push(img);
57  }
58 
59  LDEBUG("Allocated " << nbufs << " buffers");
60 }
61 
62 // ##############################################################################################################
64 {
65  // Free all our buffers:
66  for (auto & b : itsBuffers)
67  {
68  if (b.use_count() > 1) LERROR("Ref count non zero when attempting to free VideoBuf");
69  b.reset(); // VideoBuf destructor will do the memory freeing
70  }
71 
72  itsBuffers.clear();
73 }
74 
75 // ##############################################################################################################
77 {
78  if (itsStreaming.load() == false) LFATAL("Not streaming");
79 
80  // Take this buffer out of our queue and hand it over:
81  img = itsImageQueue.pop();
82  LDEBUG("Empty image " << img.bufindex << " handed over to application code for filling");
83 }
84 
85 // ##############################################################################################################
87 {
88  if (itsStreaming.load() == false) LFATAL("Not streaming");
89 
90  // Get current window size, will be 0x0 if not initialized yet:
91  unsigned short winw, winh;
92  itsBackend.getWindowSize(winw, winh);
93 
94  if (winw == 0)
95  {
96  // Need to init the display:
97  itsBackend.init(1920, 1080, true); ///////FIXME
98  itsBackend.getWindowSize(winw, winh); // Get the actual window size
99  }
100 
101  // Start a new frame and get its size. Will init the display if needed:
102  itsBackend.newFrame();
103 
104  // Poll any events. FIXME: for now just ignore requests to close:
105  bool shouldclose = false; itsBackend.pollEvents(shouldclose);
106 
107  // In this viewer, we do not use perspective. So just rescale our drawing from pixel coordinates to normalized device
108  // coordinates using a scaling matrix:
109 #ifdef JEVOIS_PLATFORM
110  // On platform, we need to translate a bit to avoid aliasing issues, which are problematic with our YUYV shader:
111  static glm::mat4 pvm = glm::translate(glm::scale(glm::mat4(1.0f), glm::vec3(2.0f / winw, 2.0f / winh, 1.0f)),
112  glm::vec3(0.375f, 0.375f, 0.0f));
113 
114 #else
115  static glm::mat4 pvm = glm::scale(glm::mat4(1.0f), glm::vec3(2.0f / winw, 2.0f / winh, 1.0f));
116 #endif
117 
118  // Draw the image, as big as possible on the screen:
119  itsImage.set(img);
120  int x = 0, y = 0; unsigned short w = 0, h = 0;
121  itsImage.draw(x, y, w, h, true, pvm); // true for no aliasing
122 
123  // Done, ask backend to swap buffers:
124  itsBackend.render();
125 
126  // Just push the buffer back into our queue. Note: we do not bother clearing the data or checking that the image is
127  // legit, i.e., matches one that was obtained via get():
128  itsImageQueue.push(img);
129  LDEBUG("Empty image " << img.bufindex << " ready for filling in by application code");
130 }
131 
132 // ##############################################################################################################
134 { itsStreaming.store(true); }
135 
136 // ##############################################################################################################
138 { itsStreaming.store(false); }
139 
140 // ##############################################################################################################
142 { itsStreaming.store(false); }
143 
144 #endif // JEVOIS_PRO
jevois::VideoDisplayGL::streamOff
virtual void streamOff() override
Stop streaming.
Definition: VideoDisplayGL.C:141
jevois::VideoDisplayGL::streamOn
virtual void streamOn() override
Start streaming.
Definition: VideoDisplayGL.C:133
LDEBUG
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition: Log.H:173
jevois::VideoOutput
Base class for video output. Gadget, MovieOutput, VideoDisplay, and VideoOutputNone derive from it.
Definition: VideoOutput.H:27
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::RawImage::bufindex
size_t bufindex
The index of the data buffer in the kernel driver.
Definition: RawImage.H:150
jevois::RawImage::fps
float fps
Programmed frames/s as given by current video mapping, may not be actual.
Definition: RawImage.H:148
jevois::VideoDisplayGL::~VideoDisplayGL
virtual ~VideoDisplayGL()
Virtual destructor for safe inheritance.
Definition: VideoDisplayGL.C:63
LERROR
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition: Log.H:211
jevois::VideoDisplayGL::VideoDisplayGL
VideoDisplayGL(size_t nbufs=2)
Constructor.
Definition: VideoDisplayGL.C:27
jevois::RawImage::width
unsigned int width
Image width in pixels.
Definition: RawImage.H:145
jevois
Definition: Concepts.dox:1
Log.H
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::RawImage::height
unsigned int height
Image height in pixels.
Definition: RawImage.H:146
jevois::VideoDisplayGL::abortStream
virtual void abortStream() override
Abort streaming.
Definition: VideoDisplayGL.C:137
jevois::VideoDisplayGL::get
virtual void get(RawImage &img) override
Get a pre-allocated image so that we can fill the pixel data and later send out using send()
Definition: VideoDisplayGL.C:76
jevois::RawImage::fmt
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
Definition: RawImage.H:147
jevois::VideoDisplayGL::send
virtual void send(RawImage const &img) override
Send an image out to display.
Definition: VideoDisplayGL.C:86
Utils.H
jevois::RawImage::buf
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
h
int h
Definition: GUIhelper.C:2373
jevois::VideoDisplayGL::setFormat
virtual void setFormat(VideoMapping const &m) override
Set the video format and frame rate, allocate the buffers.
Definition: VideoDisplayGL.C:32
VideoDisplayGL.H