JeVois  1.16
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
VideoDisplayGUI.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/GPU/GUIhelper.H>
22 #include <jevois/Debug/Log.H>
23 #include <jevois/Util/Utils.H>
25 
26 // ##############################################################################################################
27 jevois::VideoDisplayGUI::VideoDisplayGUI(std::shared_ptr<GUIhelper> helper, size_t nbufs) :
28  jevois::VideoOutput(), itsImageQueue(std::max(size_t(2), nbufs)), itsHelper(helper), itsStreaming(false)
29 {
30  // We defer OpenGL init to send() so that all OpenGL code is in the same thread.
31 }
32 
33 // ##############################################################################################################
34 void jevois::VideoDisplayGUI::setFormat(jevois::VideoMapping const & m)
35 {
36  // Nuke any old buffers:
37  itsStreaming.store(false);
38  itsBuffers.clear();
39  itsImageQueue.clear();
40  size_t const nbufs = itsImageQueue.size();
41 
42  // Allocate the buffers and make them all immediately available as RawImage:
43  unsigned int imsize = m.osize();
44 
45  for (size_t i = 0; i < nbufs; ++i)
46  {
47  itsBuffers.push_back(std::make_shared<jevois::VideoBuf>(-1, imsize, 0, -1));
48 
49  jevois::RawImage img;
50  img.width = m.ow;
51  img.height = m.oh;
52  img.fmt = m.ofmt;
53  img.fps = m.ofps;
54  img.buf = itsBuffers[i];
55  img.bufindex = i;
56 
57  // Push the RawImage to outside consumers:
58  itsImageQueue.push(img);
59  }
60 
61  LDEBUG("Allocated " << nbufs << " buffers");
62 }
63 
64 // ##############################################################################################################
66 {
67  // Free all our buffers:
68  for (auto & b : itsBuffers)
69  {
70  if (b.use_count() > 1) LERROR("Ref count non zero when attempting to free VideoBuf");
71  b.reset(); // VideoBuf destructor will do the memory freeing
72  }
73 
74  itsBuffers.clear();
75 }
76 
77 // ##############################################################################################################
79 {
80  if (itsStreaming.load() == false) LFATAL("Not streaming");
81 
82  // Take this buffer out of our queue and hand it over:
83  img = itsImageQueue.pop();
84  LDEBUG("Empty image " << img.bufindex << " handed over to application code for filling");
85 }
86 
87 // ##############################################################################################################
89 {
90  if (itsStreaming.load() == false) LFATAL("Not streaming");
91 
92  // Start the frame: Internally, this initializes or resizes the display as needed, polls and handles events (inputs,
93  // window resize, etc.), and clears the frame. Variables winw and winh are set by startFrame(0 to the current window
94  // size, and true is returned if no keyboard/mouse action in a while (can be used to hide any GUI elements):
95  unsigned short winw, winh;
96  itsHelper->startFrame(winw, winh);
97 
98  // Draw the image:
99  int x = 0, y = 0; unsigned short w = 0, h = 0;
100  itsHelper->drawImage("output", img, x, y, w, h, true); // true for no aliasing
101 
102  // Draw and render the GUI, swap buffers:
103  itsHelper->endFrame();
104 
105  // Just push the buffer back into our queue. Note: we do not bother clearing the data or checking that the image is
106  // legit, i.e., matches one that was obtained via get():
107  itsImageQueue.push(img);
108  LDEBUG("Empty image " << img.bufindex << " ready for filling in by application code");
109 }
110 
111 // ##############################################################################################################
113 { itsStreaming.store(true); }
114 
115 // ##############################################################################################################
117 { itsStreaming.store(false); }
118 
119 // ##############################################################################################################
121 { itsStreaming.store(false); }
122 
123 #endif // JEVOIS_PRO
124 
jevois::VideoDisplayGUI::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: VideoDisplayGUI.C:78
LDEBUG
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition: Log.H:160
RawImageOps.H
jevois::VideoDisplayGUI::VideoDisplayGUI
VideoDisplayGUI(std::shared_ptr< GUIhelper > helper, size_t nbufs=2)
Constructor.
Definition: VideoDisplayGUI.C:27
jevois::VideoOutput
Base class for video output. Gadget, MovieOutput, VideoDisplay, and VideoOutputNone derive from it.
Definition: VideoOutput.H:27
jevois::VideoDisplayGUI::streamOn
virtual void streamOn() override
Start streaming.
Definition: VideoDisplayGUI.C:112
jevois::VideoDisplayGUI::~VideoDisplayGUI
virtual ~VideoDisplayGUI()
Virtual destructor for safe inheritance.
Definition: VideoDisplayGUI.C:65
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
LERROR
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition: Log.H:198
jevois::VideoDisplayGUI::send
virtual void send(RawImage const &img) override
Send an image out to display.
Definition: VideoDisplayGUI.C:88
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.
Definition: Log.H:217
jevois::VideoDisplayGUI::streamOff
virtual void streamOff() override
Stop streaming.
Definition: VideoDisplayGUI.C:120
jevois::RawImage::height
unsigned int height
Image height in pixels.
Definition: RawImage.H:146
jevois::RawImage::fmt
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
Definition: RawImage.H:147
Utils.H
jevois::RawImage::buf
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
h
int h
Definition: GUIhelper.C:2150
jevois::VideoDisplayGUI::abortStream
virtual void abortStream() override
Abort streaming.
Definition: VideoDisplayGUI.C:116
VideoDisplayGUI.H
jevois::VideoDisplayGUI::setFormat
virtual void setFormat(VideoMapping const &m) override
Set the video format and frame rate, allocate the buffers.
Definition: VideoDisplayGUI.C:34
GUIhelper.H