Jetson Inference
DNN Vision Library
glDisplay.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __GL_VIEWPORT_H__
24 #define __GL_VIEWPORT_H__
25 
26 #include "videoOutput.h"
27 
28 #include "glUtility.h"
29 #include "glTexture.h"
30 #include "glEvents.h"
31 #include "glWidget.h"
32 
33 #include <time.h>
34 #include <vector>
35 
36 
47 class glDisplay : public videoOutput
48 {
49 public:
61  static glDisplay* Create( const char* title=NULL, int width=-1, int height=-1,
62  float r=0.05f, float g=0.05f, float b=0.05f, float a=1.0f );
63 
67  static glDisplay* Create( const videoOptions& options );
68 
72  ~glDisplay();
73 
78  virtual bool Open();
79 
83  inline bool IsOpen() const { return mStreaming; }
84 
88  inline bool IsClosed() const { return !mStreaming; }
89 
93  inline bool IsRendering() const { return mRendering; }
94 
98  inline float GetFPS() const { return 1000000000.0f / mAvgTime; }
99 
103  inline uint32_t GetID() const { return mID; }
104 
108  virtual inline uint32_t GetType() const { return Type; }
109 
113  static const uint32_t Type = (1 << 3);
114 
115 
119 
121 
126  void BeginRender( bool processEvents=true );
127 
131  void EndRender();
132 
134 
138 
140 
145  void Render( glTexture* texture, float x=5.0f, float y=30.0f );
146 
153  void Render( float* image, uint32_t width, uint32_t height, float x=0.0f, float y=30.0f, bool normalize=true );
154 
160  template<typename T> bool Render( T* image, uint32_t width, uint32_t height ) { return Render((void**)image, width, height, imageFormatFromType<T>()); }
161 
167  virtual bool Render( void* image, uint32_t width, uint32_t height, imageFormat format );
168 
175  void RenderImage( void* image, uint32_t width, uint32_t height, imageFormat format, float x=0.0f, float y=30.0f, bool normalize=true );
176 
184  void RenderOnce( void* image, uint32_t width, uint32_t height, imageFormat format, float x=5.0f, float y=30.0f, bool normalize=true );
185 
193  void RenderOnce( float* image, uint32_t width, uint32_t height, float x=5.0f, float y=30.0f, bool normalize=true );
194 
196 
200 
202 
207  void RenderLine( float x1, float y1, float x2, float y2, float r, float g, float b, float a=1.0f, float thickness=2.0f );
208 
213  void RenderOutline( float x, float y, float width, float height, float r, float g, float b, float a=1.0f, float thickness=2.0f );
214 
219  void RenderRect( float x, float y, float width, float height, float r, float g, float b, float a=1.0f );
220 
225  void RenderRect( float r, float g, float b, float a=1.0f );
226 
228 
232 
234 
245  void SetSize( uint32_t width, uint32_t height );
246 
250  void SetMaximized( bool maximized );
251 
255  bool IsMaximized();
256 
260  void SetFullscreen( bool fullscreen );
261 
265  bool IsFullscreen();
266 
268 
272 
274 
278  void GetBackgroundColor( float* r, float* g, float* b, float* a=NULL );
279 
288  void SetBackgroundColor( float r, float g, float b, float a=1.0f );
289 
299  void SetViewport( int left, int top, int right, int bottom );
300 
304  void ResetViewport();
305 
307 
311 
313 
317  virtual void SetStatus( const char* str );
318 
324  void SetTitle( const char* str );
325 
329  static const char* DEFAULT_TITLE;
330 
332 
336 
338 
350  void ProcessEvents();
351 
358  void AddEventHandler( glEventHandler callback, void* user=NULL );
359 
367  void RemoveEventHandler( glEventHandler callback, void* user=NULL );
368 
370 
374 
376 
380  inline const int* GetMousePosition() const { return mMousePos; }
381 
385  inline void GetMousePosition( int* x, int* y ) const { if(x) *x = mMousePos[0]; if(y) *y = mMousePos[1]; }
386 
402  inline bool GetMouseButton( uint32_t button ) const { if(button > sizeof(mMouseButtons)) return false; return mMouseButtons[button]; }
403 
425  inline bool GetKey( uint32_t key ) const { const uint32_t idx = key - KEY_OFFSET; if(idx > sizeof(mKeyStates)) return false; return mKeyStates[idx]; }
426 
428 
432 
434 
443  void SetCursor( uint32_t cursor );
444 
461  void SetDefaultCursor( uint32_t cursor, bool activate=true );
462 
470  void ResetCursor();
471 
484  void ResetDefaultCursor( bool activate=true );
485 
487 
491 
493 
500  enum DragMode
501  {
506  };
507 
511  inline DragMode GetDragMode() const { return mDragMode; }
512 
516  inline void SetDragMode( DragMode mode ) { mDragMode = mode; }
517 
521  inline bool IsDragging( DragMode mode=DragDefault ) const { return mode == DragCreate ? (mMouseDragOrigin[0] >= 0 && mMouseDragOrigin[1] >= 0) : (mMouseDrag[0] >= 0 && mMouseDrag[1] >= 0); }
522 
526  bool GetDragRect( int* x, int* y, int* width, int* height );
527 
531  bool GetDragCoords( int* x1, int* y1, int* x2, int* y2 );
532 
534 
538 
540 
544  glWidget* AddWidget( glWidget* widget );
545 
549  void RemoveWidget( glWidget* widget, bool deleteWidget=true );
550 
554  void RemoveWidget( uint32_t index, bool deleteWidget=true );
555 
559  void RemoveAllWidgets( bool deleteWidgets=true );
560 
564  inline uint32_t GetNumWidgets() const { return mWidgets.size(); }
565 
569  inline glWidget* GetWidget( const uint32_t index ) const { return mWidgets[index]; }
570 
574  int GetWidgetIndex( const glWidget* widget ) const;
575 
579  glWidget* FindWidget( int x, int y );
580 
584  std::vector<glWidget*> FindWidgets( int x, int y );
585 
587 
588 protected:
589  glDisplay( const videoOptions& options );
590 
591  bool initWindow();
592  bool initGL();
593 
594  glTexture* allocTexture( uint32_t width, uint32_t height, imageFormat format );
595 
596  void activateViewport();
597 
598  void dispatchEvent( uint16_t msg, int a, int b );
599  static bool onEvent( uint16_t msg, int a, int b, void* user );
600 
602  {
604  void* user;
605  };
606 
607  static const int screenIdx = 0;
608 
609  Display* mDisplayX;
610  Screen* mScreenX;
611  XVisualInfo* mVisualX;
612  Window mWindowX;
613  GLXContext mContextGL;
614  Cursor mCursors[256];
622 
623  uint32_t mID;
624  uint32_t mScreenWidth;
625  uint32_t mScreenHeight;
626 
627  timespec mLastTime;
628  float mAvgTime;
629  float mBgColor[4];
630  int mViewport[4];
631 
632  int mMousePos[2];
633  int mMouseDrag[2];
635  bool mMouseButtons[16];
636  bool mKeyStates[1024];
637 
641 
642  std::vector<glWidget*> mWidgets;
643  std::vector<glTexture*> mTextures;
644  std::vector<eventHandler> mEventHandlers;
645 };
646 
651 glDisplay* glGetDisplay( uint32_t display=0 );
652 
657 uint32_t glGetNumDisplays();
658 
659 #endif
660 
glDisplay::mMousePos
int mMousePos[2]
Definition: glDisplay.h:632
glTexture.h
glDisplay::dispatchEvent
void dispatchEvent(uint16_t msg, int a, int b)
glDisplay::mContextGL
GLXContext mContextGL
Definition: glDisplay.h:613
glDisplay::eventHandler
Definition: glDisplay.h:601
glDisplay::GetWidgetIndex
int GetWidgetIndex(const glWidget *widget) const
Retrieve the index of a widget (or -1 if not found)
glDisplay::RenderImage
void RenderImage(void *image, uint32_t width, uint32_t height, imageFormat format, float x=0.0f, float y=30.0f, bool normalize=true)
Render a CUDA image (uchar3, uchar4, float3, float4) using OpenGL interop.
glDisplay::RenderOutline
void RenderOutline(float x, float y, float width, float height, float r, float g, float b, float a=1.0f, float thickness=2.0f)
Render the outline of a rect in screen coordinates with the specified color.
glWidget
OpenGL graphics widget for rendering moveable/resizable shapes.
Definition: glWidget.h:40
glDisplay::GetDragMode
DragMode GetDragMode() const
Get the dragging behavior mode.
Definition: glDisplay.h:511
glGetNumDisplays
uint32_t glGetNumDisplays()
Return the number of created glDisplay windows.
glDisplay::DEFAULT_TITLE
static const char * DEFAULT_TITLE
Default title bar name.
Definition: glDisplay.h:329
glDisplay::ResetCursor
void ResetCursor()
Reset the mouse cursor back to it's default.
glDisplay::ProcessEvents
void ProcessEvents()
Process UI event messages.
glDisplay::Render
void Render(glTexture *texture, float x=5.0f, float y=30.0f)
Render an OpenGL texture.
glDisplay::mVisualX
XVisualInfo * mVisualX
Definition: glDisplay.h:611
glDisplay::RenderOnce
void RenderOnce(void *image, uint32_t width, uint32_t height, imageFormat format, float x=5.0f, float y=30.0f, bool normalize=true)
Begin the frame, render one CUDA image using OpenGL interop, and end the frame.
glDisplay::SetCursor
void SetCursor(uint32_t cursor)
Set the active mouse cursor.
glDisplay::DragDisabled
@ DragDisabled
Disable issuing all MOUSE_DRAG events.
Definition: glDisplay.h:503
glDisplay::SetDefaultCursor
void SetDefaultCursor(uint32_t cursor, bool activate=true)
Set the default mouse cursor that gets used by ResetCursor()
glDisplay::SetStatus
virtual void SetStatus(const char *str)
Set the window title string.
glDisplay
OpenGL display window and image/video renderer with CUDA interoperability.
Definition: glDisplay.h:47
glDisplay::mDefaultCursor
int mDefaultCursor
Definition: glDisplay.h:616
glDisplay::mNormalizedHeight
uint32_t mNormalizedHeight
Definition: glDisplay.h:640
glDisplay::mKeyStates
bool mKeyStates[1024]
Definition: glDisplay.h:636
glDisplay::mTextures
std::vector< glTexture * > mTextures
Definition: glDisplay.h:643
glDisplay::allocTexture
glTexture * allocTexture(uint32_t width, uint32_t height, imageFormat format)
glDisplay::glDisplay
glDisplay(const videoOptions &options)
glDisplay::mMouseDragOrigin
int mMouseDragOrigin[2]
Definition: glDisplay.h:634
glDisplay::eventHandler::user
void * user
Definition: glDisplay.h:604
glDisplay::SetBackgroundColor
void SetBackgroundColor(float r, float g, float b, float a=1.0f)
Set the window's background color.
glDisplay::mResizedToFeed
bool mResizedToFeed
Definition: glDisplay.h:619
glDisplay::mWindowX
Window mWindowX
Definition: glDisplay.h:612
glDisplay::mMouseButtons
bool mMouseButtons[16]
Definition: glDisplay.h:635
glEvents.h
glDisplay::SetFullscreen
void SetFullscreen(bool fullscreen)
Set the window to fullscreen mode or not.
glDisplay::GetNumWidgets
uint32_t GetNumWidgets() const
Retrieve the number of widgets.
Definition: glDisplay.h:564
glDisplay::GetType
virtual uint32_t GetType() const
Return the interface type (glDisplay::Type)
Definition: glDisplay.h:108
glDisplay::mRendering
bool mRendering
Definition: glDisplay.h:618
glDisplay::SetDragMode
void SetDragMode(DragMode mode)
Set the dragging behavior mode.
Definition: glDisplay.h:516
videoOutput::mStreaming
bool mStreaming
Definition: videoOutput.h:321
glDisplay::AddEventHandler
void AddEventHandler(glEventHandler callback, void *user=NULL)
Register an event message handler that will be called by ProcessEvents()
glDisplay::mID
uint32_t mID
Definition: glDisplay.h:623
glDisplay::ResetDefaultCursor
void ResetDefaultCursor(bool activate=true)
Reset the default cursor back to it's original, the arrow.
KEY_OFFSET
#define KEY_OFFSET
Definition: glEvents.h:45
glDisplay::SetTitle
void SetTitle(const char *str)
Set the window title string.
glDisplay::screenIdx
static const int screenIdx
Definition: glDisplay.h:607
glDisplay::GetWidget
glWidget * GetWidget(const uint32_t index) const
Retrieve a widget.
Definition: glDisplay.h:569
glDisplay::GetBackgroundColor
void GetBackgroundColor(float *r, float *g, float *b, float *a=NULL)
Retrieve the window's background color.
glDisplay::SetMaximized
void SetMaximized(bool maximized)
Maximize or un-maximize the window.
glDisplay::IsClosed
bool IsClosed() const
Returns true if the window has been closed.
Definition: glDisplay.h:88
glDisplay::mActiveCursor
int mActiveCursor
Definition: glDisplay.h:615
glDisplay::IsOpen
bool IsOpen() const
Returns true if the window is open.
Definition: glDisplay.h:83
glDisplay::FindWidgets
std::vector< glWidget * > FindWidgets(int x, int y)
Find all widgets by coordinate, or NULL if no widget overlaps with that coordinate.
glDisplay::GetMouseButton
bool GetMouseButton(uint32_t button) const
Get the mouse button state.
Definition: glDisplay.h:402
glDisplay::GetMousePosition
const int * GetMousePosition() const
Get the mouse position.
Definition: glDisplay.h:380
glDisplay::IsDragging
bool IsDragging(DragMode mode=DragDefault) const
Is the mouse currently dragging in the specified mode?
Definition: glDisplay.h:521
glDisplay::mNormalizedWidth
uint32_t mNormalizedWidth
Definition: glDisplay.h:639
glDisplay::Open
virtual bool Open()
Open the window.
glDisplay::onEvent
static bool onEvent(uint16_t msg, int a, int b, void *user)
glDisplay::GetID
uint32_t GetID() const
Get the ID of this display instance into glGetDisplay()
Definition: glDisplay.h:103
glDisplay::initGL
bool initGL()
glDisplay::Type
static const uint32_t Type
Unique type identifier of glDisplay class.
Definition: glDisplay.h:113
glDisplay::IsRendering
bool IsRendering() const
Returns true if between BeginRender() and EndRender()
Definition: glDisplay.h:93
glDisplay::mWindowClosedMsg
Atom mWindowClosedMsg
Definition: glDisplay.h:620
glDisplay::SetViewport
void SetViewport(int left, int top, int right, int bottom)
Set the active viewport being rendered to.
glDisplay::mMouseDrag
int mMouseDrag[2]
Definition: glDisplay.h:633
glDisplay::eventHandler::callback
glEventHandler callback
Definition: glDisplay.h:603
glDisplay::GetMousePosition
void GetMousePosition(int *x, int *y) const
Get the mouse position.
Definition: glDisplay.h:385
glDisplay::ResetViewport
void ResetViewport()
Reset to the full viewport (and change back GL_PROJECTION)
glDisplay::mNormalizedCUDA
float * mNormalizedCUDA
Definition: glDisplay.h:638
glEventHandler
bool(* glEventHandler)(uint16_t event, int a, int b, void *user)
Event message handler callback for recieving UI messages from a window.
Definition: glEvents.h:291
glDisplay::mInitialShow
bool mInitialShow
Definition: glDisplay.h:617
glDisplay::mScreenX
Screen * mScreenX
Definition: glDisplay.h:610
glDisplay::mViewport
int mViewport[4]
Definition: glDisplay.h:630
glDisplay::mCursors
Cursor mCursors[256]
Definition: glDisplay.h:614
glGetDisplay
glDisplay * glGetDisplay(uint32_t display=0)
Retrieve a display window object.
glDisplay::BeginRender
void BeginRender(bool processEvents=true)
Clear window and begin rendering a frame.
glDisplay::DragCreate
@ DragCreate
Create a new widget from a dragging rectangle, and issue WIDGET_CREATED events.
Definition: glDisplay.h:505
glDisplay::AddWidget
glWidget * AddWidget(glWidget *widget)
Add a widget to the window that recieves events and is rendered.
glDisplay::~glDisplay
~glDisplay()
Destroy window.
glDisplay::RemoveEventHandler
void RemoveEventHandler(glEventHandler callback, void *user=NULL)
Remove an event message handler from being called by ProcessEvents() RemoveEventHandler() will search...
glTexture
OpenGL texture with CUDA interoperability.
Definition: glTexture.h:51
glDisplay::mScreenWidth
uint32_t mScreenWidth
Definition: glDisplay.h:624
glDisplay::mScreenHeight
uint32_t mScreenHeight
Definition: glDisplay.h:625
glDisplay::mWidgets
std::vector< glWidget * > mWidgets
Definition: glDisplay.h:642
glDisplay::RemoveAllWidgets
void RemoveAllWidgets(bool deleteWidgets=true)
Remove all widgets from the window (and optionally delete them)
glDisplay::DragMode
DragMode
Drag behavior enum.
Definition: glDisplay.h:500
glWidget.h
glDisplay::GetFPS
float GetFPS() const
Get the average frame time (in milliseconds).
Definition: glDisplay.h:98
glDisplay::Render
bool Render(T *image, uint32_t width, uint32_t height)
Render a CUDA image (uchar3, uchar4, float3, float4) using OpenGL interop.
Definition: glDisplay.h:160
glDisplay::RenderRect
void RenderRect(float x, float y, float width, float height, float r, float g, float b, float a=1.0f)
Render a filled rect in screen coordinates with the specified color.
glDisplay::mBgColor
float mBgColor[4]
Definition: glDisplay.h:629
glDisplay::mDragMode
DragMode mDragMode
Definition: glDisplay.h:621
videoOutput
The videoOutput API is for rendering and transmitting frames to video input devices such as display w...
Definition: videoOutput.h:104
glDisplay::Create
static glDisplay * Create(const char *title=NULL, int width=-1, int height=-1, float r=0.05f, float g=0.05f, float b=0.05f, float a=1.0f)
Create a new OpenGL display window with the specified options.
glDisplay::IsFullscreen
bool IsFullscreen()
Determine if the window is fullscreen or not.
glDisplay::RenderLine
void RenderLine(float x1, float y1, float x2, float y2, float r, float g, float b, float a=1.0f, float thickness=2.0f)
Render a line in screen coordinates with the specified color.
glDisplay::DragDefault
@ DragDefault
Issue MOUSE_DRAG events, but nothing else.
Definition: glDisplay.h:502
glDisplay::initWindow
bool initWindow()
glDisplay::GetDragCoords
bool GetDragCoords(int *x1, int *y1, int *x2, int *y2)
Get the current dragging coordinates, or return false if not dragging.
glDisplay::IsMaximized
bool IsMaximized()
Determine if the window is maximized or not.
glDisplay::DragSelect
@ DragSelect
Select widgets from a dragging rectangle, and issue WIDGET_SELECTED events.
Definition: glDisplay.h:504
glDisplay::mEventHandlers
std::vector< eventHandler > mEventHandlers
Definition: glDisplay.h:644
videoOptions
The videoOptions struct contains common settings that are used to configure and query videoSource and...
Definition: videoOptions.h:37
glDisplay::RemoveWidget
void RemoveWidget(glWidget *widget, bool deleteWidget=true)
Remove a widget from the window (and optionally delete it)
glDisplay::activateViewport
void activateViewport()
glDisplay::GetDragRect
bool GetDragRect(int *x, int *y, int *width, int *height)
Get the current dragging rectangle, or return false if not dragging.
normalize
__host__ __device__ float2 normalize(float2 v)
Definition: cudaMath.h:1580
glDisplay::GetKey
bool GetKey(uint32_t key) const
Get the state of a key (lowercase, without modifiers applied)
Definition: glDisplay.h:425
glDisplay::mDisplayX
Display * mDisplayX
Definition: glDisplay.h:609
videoOutput.h
glDisplay::SetSize
void SetSize(uint32_t width, uint32_t height)
Set the window's size.
glDisplay::FindWidget
glWidget * FindWidget(int x, int y)
Find first widget by coordinate, or NULL if no widget overlaps with that coordinate.
glDisplay::mLastTime
timespec mLastTime
Definition: glDisplay.h:627
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
glUtility.h
glDisplay::EndRender
void EndRender()
Finish rendering and refresh / flip the backbuffer.
glDisplay::mAvgTime
float mAvgTime
Definition: glDisplay.h:628