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 }
COpenGLBuffer.h
mrpt::opengl::internal::data
static struct FontData data
Definition: gltext.cpp:144
opengl_api.h
exceptions.h
mrpt::Clock::now
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
mrpt::opengl::COpenGLBuffer::RAII_Impl::RAII_Impl
RAII_Impl(COpenGLBuffer::Type t)
Definition: COpenGLBuffer.cpp:28
mrpt::opengl::COpenGLBuffer::release
void release()
Definition: COpenGLBuffer.h:71
mrpt::system::TCallStackBackTrace::asString
std::string asString() const
Prints all backtrace entries, one per line in a human-readable format.
Definition: backtrace.cpp:124
backtrace.h
mrpt::opengl::COpenGLBuffer::RAII_Impl::create
void create()
Definition: COpenGLBuffer.cpp:35
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::opengl::COpenGLBuffer::RAII_Impl::bind
void bind()
Definition: COpenGLBuffer.cpp:83
mrpt::opengl::COpenGLBuffer::RAII_Impl::destroy
void destroy()
Definition: COpenGLBuffer.cpp:47
mrpt::Clock::toDouble
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
mrpt::system::getCallStackBackTrace
void getCallStackBackTrace(TCallStackBackTrace &out_bt)
Returns a list of strings representing the current call stack backtrace.
Definition: backtrace.cpp:32
mrpt::system::TCallStackBackTrace
See: getCallStackBackTrace()
Definition: backtrace.h:33
mrpt::opengl::COpenGLBuffer::m_impl
std::shared_ptr< RAII_Impl > m_impl
Definition: COpenGLBuffer.h:102
mrpt::opengl::COpenGLBuffer::RAII_Impl::release
void release()
Definition: COpenGLBuffer.cpp:91
opengl-precomp.h
mrpt::opengl::COpenGLBuffer::COpenGLBuffer
COpenGLBuffer()
Definition: COpenGLBuffer.h:46
Clock.h
mrpt::opengl::COpenGLBuffer::Type
Type
Definition: COpenGLBuffer.h:24
mrpt::opengl::COpenGLBuffer::type
Type type() const
Definition: COpenGLBuffer.h:49
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
mrpt::opengl::COpenGLBuffer::destroy
void destroy()
Automatically called upon destructor, no need for the user to call it in normal situations.
Definition: COpenGLBuffer.h:68
mrpt::opengl::COpenGLBuffer::RAII_Impl::allocate
void allocate(const void *data, int byteCount)
Definition: COpenGLBuffer.cpp:101
mrpt::opengl::COpenGLBuffer::usage
Usage usage() const
Definition: COpenGLBuffer.h:51
mrpt::opengl::COpenGLBuffer::RAII_Impl::~RAII_Impl
~RAII_Impl()
Definition: COpenGLBuffer.cpp:29



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sun Jul 19 15:15:43 UTC 2020