MRPT  2.0.4
COpenGLBuffer.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 //
12 #include <mrpt/core/Clock.h>
13 #include <mrpt/core/exceptions.h>
15 #include <mrpt/opengl/opengl_api.h>
16 #include <mrpt/system/backtrace.h>
17 
18 #include <cstdlib>
19 #include <iostream>
20 
21 using namespace mrpt::opengl;
22 
24 {
25  m_impl = std::make_shared<RAII_Impl>(type);
26 }
27 
30 {
31  // Free opengl resources:
32  destroy();
33 }
34 
36 {
37  destroy();
38 #if MRPT_HAS_OPENGL_GLUT
39  GLuint buffer;
40  glGenBuffers(1, &buffer);
41  this->buffer_id = buffer;
42  this->created_from = std::this_thread::get_id();
43  created = true;
44 #endif
45 }
46 
48 {
49  if (!created) return;
50 
51  static const bool showErrs =
52  (::getenv("MRPT_REVEAL_OPENGL_BUFFER_LEAKS") != nullptr);
53 
54 #if MRPT_HAS_OPENGL_GLUT
55  if (created_from == std::this_thread::get_id())
56  {
57  release();
58  glDeleteBuffers(1, &buffer_id);
59  }
60  else if (showErrs)
61  {
62  // at least, emit a warning:
63  static double tLast = 0;
65  if (tNow - tLast > 2.0)
66  {
67  tLast = tNow;
68 
71 
72  std::cerr << "[COpenGLBuffer::RAII_Impl] *Warning* Leaking memory "
73  "since Buffer was acquired from a different thread "
74  "and cannot free it from this thread, call stack:"
75  << bt.asString() << std::endl;
76  }
77  }
78 #endif
79  buffer_id = 0;
80  created = false;
81 }
82 
84 {
85 #if MRPT_HAS_OPENGL_GLUT
86  ASSERT_(created);
87  glBindBuffer(static_cast<GLenum>(type), buffer_id);
88 #endif
89 }
90 
92 {
93 #if MRPT_HAS_OPENGL_GLUT
94  if (!created) return;
95  if (created_from != std::this_thread::get_id()) return;
96 
97  glBindBuffer(static_cast<GLenum>(type), 0);
98 #endif
99 }
100 
101 void COpenGLBuffer::RAII_Impl::allocate(const void* data, int byteCount)
102 {
103 #if MRPT_HAS_OPENGL_GLUT
104  ASSERT_(created);
105  glBufferData(
106  static_cast<GLenum>(type), byteCount, data, static_cast<GLenum>(usage));
107 #endif
108 }
static double toDouble(const time_point t) noexcept
Converts a timestamp to a UNIX time_t-like number, with fractional part.
Definition: Clock.cpp:106
void destroy()
Automatically called upon destructor, no need for the user to call it in normal situations.
Definition: COpenGLBuffer.h:68
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
void getCallStackBackTrace(TCallStackBackTrace &out_bt)
Returns a list of strings representing the current call stack backtrace.
Definition: backtrace.cpp:32
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
std::shared_ptr< RAII_Impl > m_impl
void allocate(const void *data, int byteCount)
See: getCallStackBackTrace()
Definition: backtrace.h:33
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
std::string asString() const
Prints all backtrace entries, one per line in a human-readable format.
Definition: backtrace.cpp:124
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020