Perception Tool Kit (PTK) API Guide
Renderer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2018 Texas Instruments Incorporated
4  *
5  * All rights reserved not granted herein.
6  *
7  * Limited License.
8  *
9  * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10  * license under copyrights and patents it now or hereafter owns or controls to make,
11  * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12  * terms herein. With respect to the foregoing patent license, such license is granted
13  * solely to the extent that any such patent is necessary to Utilize the software alone.
14  * The patent license shall not apply to any combinations which include this software,
15  * other than combinations with devices manufactured by or for TI ("TI Devices").
16  * No hardware patent is licensed hereunder.
17  *
18  * Redistributions must preserve existing copyright notices and reproduce this license
19  * (including the above copyright notice and the disclaimer and (if applicable) source
20  * code license limitations below) in the documentation and/or other materials provided
21  * with the distribution
22  *
23  * Redistribution and use in binary form, without modification, are permitted provided
24  * that the following conditions are met:
25  *
26  * * No reverse engineering, decompilation, or disassembly of this software is
27  * permitted with respect to any software provided in binary form.
28  *
29  * * any redistribution and use are licensed by TI for use only with TI Devices.
30  *
31  * * Nothing shall obligate TI to provide you with source code for the software
32  * licensed and provided to you in object code.
33  *
34  * If software source code is provided to you, modification and redistribution of the
35  * source code are permitted provided that the following conditions are met:
36  *
37  * * any redistribution and use of the source code, including any resulting derivative
38  * works, are licensed by TI for use only with TI Devices.
39  *
40  * * any redistribution and use of any object code compiled from the source code
41  * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42  *
43  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44  *
45  * may be used to endorse or promote products derived from this software without
46  * specific prior written permission.
47  *
48  * DISCLAIMER.
49  *
50  * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53  * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59  * OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  */
62 #pragma once
63 #ifndef PTK_RENDERER_H
64 #define PTK_RENDERER_H
65 
71 #include <string>
72 #include <functional>
73 #include <map>
74 #include <list>
75 #include <vector>
76 #include <chrono>
77 #include <thread>
78 
79 #include <perception/gui/GLlibs.h>
80 
81 #if defined(PLATFORM_SDL)
82 #ifdef LINUX
83 # include <SDL2/SDL.h>
84 # include <SDL2/SDL_image.h>
85 #else
86 # include <SDL.h>
87 # include <SDL_image.h>
88 #endif
89 #define RENDERER_PLATFORM_WINDOW_TYPE SDL_Window*
90 #define RENDERER_PLATFORM_CONTEXT_TYPE SDL_GLContext
91 #define RENDERER_PLATFORM_EVENT_TYPE SDL_EventType
92 #elif defined(PLATFORM_EGL)
94 #define RENDERER_PLATFORM_WINDOW_TYPE EGLSurface
95 #define RENDERER_PLATFORM_CONTEXT_TYPE uint32_t
96 #define RENDERER_PLATFORM_EVENT_TYPE uint32_t
97 #elif defined(PLATFORM_X11)
98 #include "X11/Xlib.h"
99 #include "X11/Xutil.h"
100 #define RENDERER_PLATFORM_WINDOW_TYPE EGLSurface
101 #define RENDERER_PLATFORM_CONTEXT_TYPE uint32_t
102 #define RENDERER_PLATFORM_EVENT_TYPE uint32_t
103 #elif defined(PLATFORM_WAYLAND)
104 #include <wayland-client.h>
105 #include <wayland-server.h>
106 #include <wayland-egl.h>
107 #include <linux/input.h>
108 #define RENDERER_PLATFORM_WINDOW_TYPE EGLSurface
109 #define RENDERER_PLATFORM_CONTEXT_TYPE uint32_t
110 #define RENDERER_PLATFORM_EVENT_TYPE uint32_t
111 #elif defined(PLATFORM_GLFW)
112 #include <GLFW/glfw3.h>
113 #define RENDERER_PLATFORM_WINDOW_TYPE GLFWwindow*
114 #define RENDERER_PLATFORM_CONTEXT_TYPE uint32_t
115 #define RENDERER_PLATFORM_EVENT_TYPE uint32_t
116 #else
117 #error "Must define PLATFORM_SDL, PLATFORM_GLFW, PLATFORM_X11 or PLATFORM_WAYLAND"
118 #endif
119 
120 #if !defined(PLATFORM_SDL)
121 /* TODO: Ideally, we should use DevIL for all platforms
122  need to check Windows compatibility though
123 */
124 #include <IL/il.h>
125 #endif
126 
127 #include <glm/glm.hpp>
128 
129 #include <ft2build.h>
130 #include FT_FREETYPE_H
131 
132 #include <perception/gui/Shader.h>
134 
135 namespace ptk {
136 
137 class Renderable;
138 class Layout;
139 class Font;
140 class String;
141 class QuadRenderer;
145 
146 #ifdef DEBUG
147 # define GL_DEBUG(x) do { GLenum errorCode; \
148  while ((errorCode = glGetError()) != GL_NO_ERROR) \
149  { \
150  printf("Error in " #x "; gl error code: %d\n", errorCode); \
151  } } while (0)
152 #else
153 # define GL_DEBUG(x)
154 #endif
155 
156 #ifndef GUI_HISTORY_LENGTH
157 # define GUI_HISTORY_LENGTH 60
158 #endif
159 
161 {
162 #if defined(PLATFORM_SDL)
163  std::function<void(SDL_Event*)> cb[290];
164 #else
165  std::function<void(uint32_t *)> cb[290];
166 #endif
167 };
168 
169 class Renderer
170 {
171 public:
172 #if defined(PLATFORM_SDL)
173  typedef std::function<void(SDL_Event*)> EventCallback;
174 #else
175  typedef std::function<void(uint32_t *)> EventCallback;
176 #endif
177  typedef std::list<EventCallback> EventCallbackList;
178  typedef std::map<RENDERER_PLATFORM_EVENT_TYPE, EventCallbackList *> EventCallbackMap;
179  typedef std::pair<int, std::string> FontName;
180 
181 public:
182  Renderer();
183  virtual ~Renderer();
184 
185  // Disable copy constructor and assignment operator.
186  Renderer(const Renderer&) = delete;
187  Renderer& operator=(const Renderer&) = delete;
188 
189  PixelDimensions getCanvasDimensions() const;
190 
191  void run();
192  void step();
193  void render(uint64_t delta);
194  void handleEvents();
195  void createWindow(const std::string& name, uint32_t width, uint32_t height);
196  void createConsole();
197  FT_Library *getFreeTypeLibrary() { return &_ft; }
198 
199  void add(Renderable *r);
200  void add(Renderable *r, Dimensions d);
201  void add(UntexturedQuadRenderable *r);
202  void add(TexturedQuadRenderable *r);
203 
204  void remove(Renderable *r);
205  void remove(UntexturedQuadRenderable *r);
206  void remove(TexturedQuadRenderable *r);
207 
208  void setFPSTarget(uint32_t target);
209  void setWindowDimensions(uint32_t width, uint32_t height);
210  void setFovYZ(float fovY, float nearClip, float farClip);
211 
212  void setKeyHandlerTable(KeyHandlerTable *table) { _keyHandlers = table; }
213 
214  uint32_t getWidth() const { return _width; }
215  uint32_t getHeight() const { return _height; }
216  float getAspectRatio() const { return _aspectRatio; }
217  float getPixelWidth() const { return 1.0f / _width; }
218  float getPixelHeight() const { return 1.0f / _height; }
219 
220  void computeProjectionMatrix();
221  glm::mat4x4 getProjectionMatrix() const;
222 
223  int loadFont(const std::string& path, int size);
224  int getFontId(const std::string& path, int size) const;
225  Font *getFont(int fontId) const;
226  void setFont(int fontId);
227  void setColor(glm::vec4 color) { _curColor = color; }
228 
229  // String management interface
230  ptk::String *createString(const std::string& source);
231  ptk::String *createString(int fontId, const std::string& source);
232  ptk::String *createString(const std::string& source, float normX, float normY);
233  ptk::String *createString(int fontId, const std::string& source, float normX, float normY);
234  void deleteString(ptk::String *s);
235 
236  const Shader& getTextShader() const { return _textShader; }
237 
238  // Statistics
239  float getFPS() const { return _lastFps; }
240  GLint getGLPrimitivesGenerated() const { return _glPrimitives; }
241  GLint getGLSamplesPassed() const { return _glSamples; }
242  GLint getGLTimeElapsed() const { return _glTime; }
243 
244  // Texture/image loading features
245  GLuint loadImage(const std::string& path);
246  GLuint loadImage(void *mem, size_t size);
247  GLuint loadImageYUV(const std::string& path);
248  GLuint loadImageYUV(void *mem, size_t size);
249  // @todo reference counting for things that use textures
250 
251  GLuint createTexture();
252  void destroyTexture(GLuint texture);
253 
254  void platformSetupGraphicsContext(const std::string& title, uint32_t width, uint32_t height);
255  void platformDestroyGraphicsContext();
256  void platformAddEventHandler(RENDERER_PLATFORM_EVENT_TYPE type, EventCallback callback);
257  void platformRegisterEvents();
258  void platformHandleEvents();
259  void platformSwapBuffers();
260  void platformLoadImageTo(const std::string& path, GLuint tex);
261  void platformLoadImageTo(void *mem, size_t size, GLuint tex);
262  void platformGetWindowDimensions(uint32_t *width, uint32_t *height);
263 
264 #ifdef PLATFORM_EGL
265  void setEglFdandPitch(uint32_t fd, uint32_t fd_off, int32_t pitch);// {_eglDisplayFd=fd; _eglDisplayFdOff=fd_off;}
266 #endif
267 
268 protected:
269  uint32_t _fpsTarget;
270  bool _run;
271  uint32_t _width;
272  uint32_t _height;
274  float _fovY;
275  float _nearClip;
276  float _farClip;
277  glm::mat4x4 _projection;
278 
279  std::chrono::high_resolution_clock::time_point _previousFrame;
280  std::chrono::nanoseconds _tickGoal;
281  std::chrono::nanoseconds _sleepGoal;
282  std::chrono::nanoseconds _renderHistory[GUI_HISTORY_LENGTH];
283  uint32_t _historyIndex;
284 
285  EventCallbackMap _callbackMap;
287 
289 
290  RENDERER_PLATFORM_WINDOW_TYPE _mainWindow;
291  RENDERER_PLATFORM_CONTEXT_TYPE _glContext;
292 
293  FT_Library _ft;
294 
295  // Font and string management
296  std::map<FontName, int> _fontIds;
297  std::map<int, Font *> _fonts;
299  int _curFont;
300  glm::vec4 _curColor;
301  float _curZ;
302  std::map<int, std::list<ptk::String *>*> _strings;
304 
307 
308  GLuint _statsQueries[3];
309  GLint _glTime;
310  GLint _glSamples;
312  float _lastFps;
313 #ifdef PLATFORM_EGL
314  uint32_t _eglDisplayFd;
315  uint32_t _eglDisplayFdOff;
316  int32_t _eglDisplayPitch;
317 #endif
318 };
319 
320 };
321 
322 #endif
RENDERER_PLATFORM_WINDOW_TYPE _mainWindow
Definition: Renderer.h:290
Layout * _rootLayout
Definition: Renderer.h:288
int _nextFontId
Definition: Renderer.h:298
const Shader & getTextShader() const
Definition: Renderer.h:236
Definition: TexturedQuadRenderable.h:83
Definition: TexturedQuadRenderer.h:84
float getPixelHeight() const
Definition: Renderer.h:218
Definition: Dimensions.h:92
EventCallbackMap _callbackMap
Definition: Renderer.h:285
uint32_t _width
Definition: Renderer.h:271
Definition: Shader.h:77
std::map< int, std::list< ptk::String * > * > _strings
Definition: Renderer.h:302
GLint _glPrimitives
Definition: Renderer.h:311
Definition: Renderer.h:169
struct String String
Definition: c/Renderer.h:77
Definition: Renderable.h:76
Definition: QuadRenderer.h:83
glm::mat4x4 _projection
Definition: Renderer.h:277
GLint _glTime
Definition: Renderer.h:309
Definition: sensor.h:74
std::map< int, Font * > _fonts
Definition: Renderer.h:297
Definition: Renderer.h:160
Class for handling OpenGL shaders and allocating/deallocating them.
FT_Library _ft
Definition: Renderer.h:293
struct Renderer Renderer
Definition: c/Renderer.h:81
float getPixelWidth() const
Definition: Renderer.h:217
GLint getGLPrimitivesGenerated() const
Definition: Renderer.h:240
uint32_t _height
Definition: Renderer.h:272
KeyHandlerTable * _keyHandlers
Definition: Renderer.h:286
uint32_t _historyIndex
Definition: Renderer.h:283
Definition: Dimensions.h:74
void setColor(glm::vec4 color)
Definition: Renderer.h:227
uint32_t _fpsTarget
Definition: Renderer.h:269
QuadRenderer * _quadRenderer
Definition: Renderer.h:305
GLint getGLTimeElapsed() const
Definition: Renderer.h:242
float _curZ
Definition: Renderer.h:301
Definition: UntexturedQuadRenderable.h:82
float getFPS() const
Definition: Renderer.h:239
Shader _textShader
Definition: Renderer.h:303
struct Renderable Renderable
Definition: c/Renderable.h:73
float _nearClip
Definition: Renderer.h:275
std::map< RENDERER_PLATFORM_EVENT_TYPE, EventCallbackList * > EventCallbackMap
Definition: Renderer.h:178
float _aspectRatio
Definition: Renderer.h:273
Definition: Layout.h:73
GLint getGLSamplesPassed() const
Definition: Renderer.h:241
bool _run
Definition: Renderer.h:270
std::list< EventCallback > EventCallbackList
Definition: Renderer.h:177
float _lastFps
Definition: Renderer.h:312
std::function< void(uint32_t *)> cb[290]
Definition: Renderer.h:165
std::chrono::nanoseconds _tickGoal
Definition: Renderer.h:280
FT_Library * getFreeTypeLibrary()
Definition: Renderer.h:197
GLint _glSamples
Definition: Renderer.h:310
Definition: String.h:100
std::chrono::nanoseconds _sleepGoal
Definition: Renderer.h:281
RENDERER_PLATFORM_CONTEXT_TYPE _glContext
Definition: Renderer.h:291
uint32_t getWidth() const
Definition: Renderer.h:214
#define GUI_HISTORY_LENGTH
Definition: Renderer.h:157
std::pair< int, std::string > FontName
Definition: Renderer.h:179
uint32_t getHeight() const
Definition: Renderer.h:215
void setKeyHandlerTable(KeyHandlerTable *table)
Definition: Renderer.h:212
std::map< FontName, int > _fontIds
Definition: Renderer.h:296
std::chrono::high_resolution_clock::time_point _previousFrame
Definition: Renderer.h:279
TexturedQuadRenderer * _texturedQuadRenderer
Definition: Renderer.h:306
float getAspectRatio() const
Definition: Renderer.h:216
std::function< void(uint32_t *)> EventCallback
Definition: Renderer.h:175
Definition: Font.h:81
int _curFont
Definition: Renderer.h:299
float _farClip
Definition: Renderer.h:276
glm::vec4 _curColor
Definition: Renderer.h:300
float _fovY
Definition: Renderer.h:274