]> git.sesse.net Git - casparcg/blobdiff - accelerator/ogl/util/buffer.cpp
Add call tracing at selected placed in the code to help debug deadlocks in OpenGL.
[casparcg] / accelerator / ogl / util / buffer.cpp
index dea71e6cfc035b76c02406bd9d5dd1ca64ae27fd..b6b4abc125c5a0358959ee632f244c3173eea657 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "../../stdafx.h"\r
-\r
-#include "buffer.h"\r
-\r
-#include "texture.h"\r
-#include "device.h"\r
-\r
-#include <common/except.h>\r
-#include <common/gl/gl_check.h>\r
-\r
-#include <gl/glew.h>\r
-\r
-#include <tbb/atomic.h>\r
-\r
-namespace caspar { namespace accelerator { namespace ogl {\r
-\r
-static tbb::atomic<int> g_w_total_count;\r
-static tbb::atomic<int> g_r_total_count;\r
-                                                                                                                                                                                                                                                                                                                               \r
-struct buffer::impl : boost::noncopyable\r
-{      \r
-       GLuint                                          pbo_;\r
-       const std::size_t                       size_;\r
-       tbb::atomic<uint8_t*>           data_;\r
-       GLenum                                          usage_;\r
-       GLenum                                          target_;\r
-\r
-public:\r
-       impl(std::size_t size, buffer::usage usage) \r
-               : size_(size)\r
-               , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
-               , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
-       {\r
-               data_ = nullptr;\r
-               GL(glGenBuffers(1, &pbo_));\r
-               bind(); \r
-               GL(glBufferData(target_, size_, NULL, usage_));         \r
-               if(usage_ == GL_STREAM_DRAW)    \r
-                       data_ = (uint8_t*)GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));  \r
-               unbind();\r
-\r
-               if(!pbo_)\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
-\r
-               //CASPAR_LOG(trace) << "[buffer] [" << ++(usage_ == buffer::usage::write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == buffer::usage::write_only ? "write_only" : "read_only");\r
-       }       \r
-\r
-       ~impl()\r
-       {\r
-               glDeleteBuffers(1, &pbo_);\r
-       }\r
-\r
-       void* map()\r
-       {\r
-               if(data_ != nullptr)\r
-                       return data_;\r
-\r
-               GL(glBindBuffer(target_, pbo_));\r
-               if(usage_ == GL_STREAM_DRAW)                    \r
-                       GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
-               \r
-               boost::timer timer;\r
-\r
-               data_ = (uint8_t*)GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));  \r
-\r
-               if(timer.elapsed() > 0.02)\r
-                       CASPAR_LOG(debug) << L"[buffer] Performance warning. Buffer mapping blocked more than 20 ms: " << timer.elapsed();\r
-\r
-               GL(glBindBuffer(target_, 0));\r
-               if(!data_)\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));\r
-\r
-               return data_;\r
-       }\r
-       \r
-       void unmap()\r
-       {\r
-               if(data_ == nullptr)\r
-                       return;\r
-               \r
-               GL(glBindBuffer(target_, pbo_));\r
-               GL(glUnmapBuffer(target_));     \r
-               if(usage_ == GL_STREAM_READ)                    \r
-                       GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.\r
-               data_ = nullptr;        \r
-               GL(glBindBuffer(target_, 0));\r
-       }\r
-\r
-       void bind()\r
-       {\r
-               GL(glBindBuffer(target_, pbo_));\r
-       }\r
-\r
-       void unbind()\r
-       {\r
-               GL(glBindBuffer(target_, 0));\r
-       }\r
-};\r
-\r
-buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}\r
-buffer::buffer(buffer&& other) : impl_(std::move(other.impl_)){}\r
-buffer::~buffer(){}\r
-buffer& buffer::operator=(buffer&& other){impl_ = std::move(other.impl_); return *this;}\r
-uint8_t* buffer::data(){return impl_->data_;}\r
-void buffer::map(){impl_->map();}\r
-void buffer::unmap(){impl_->unmap();}\r
-void buffer::bind() const{impl_->bind();}\r
-void buffer::unbind() const{impl_->unbind();}\r
-std::size_t buffer::size() const { return impl_->size_; }\r
-int buffer::id() const {return impl_->pbo_;}\r
-\r
-}}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../StdAfx.h"
+
+#include "buffer.h"
+
+#include "texture.h"
+#include "device.h"
+
+#include <common/except.h>
+#include <common/gl/gl_check.h>
+#include <common/timer.h>
+
+#include <GL/glew.h>
+
+#include <boost/property_tree/ptree.hpp>
+
+#include <tbb/atomic.h>
+
+namespace caspar { namespace accelerator { namespace ogl {
+
+static tbb::atomic<int>                        g_w_total_count;
+static tbb::atomic<std::size_t>        g_w_total_size;
+static tbb::atomic<int>                        g_r_total_count;
+static tbb::atomic<std::size_t>        g_r_total_size;
+
+struct buffer::impl : boost::noncopyable
+{      
+       GLuint                                          pbo_;
+       const std::size_t                       size_;
+       tbb::atomic<uint8_t*>           data_;
+       GLenum                                          usage_;
+       GLenum                                          target_;
+
+public:
+       impl(std::size_t size, buffer::usage usage) 
+               : size_(size)
+               , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
+               , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
+       {
+               CASPAR_LOG_CALL(trace) << "buffer::buffer() <- " << get_context();
+               caspar::timer timer;
+
+               data_ = nullptr;
+               GL(glGenBuffers(1, &pbo_));
+               bind(); 
+               GL(glBufferData(target_, size_, NULL, usage_));         
+               if(usage_ == GL_STREAM_DRAW)
+               {
+                       auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
+                       data_ = reinterpret_cast<uint8_t*>(result);
+               }
+               unbind();
+
+               if(!pbo_)
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
+
+               (usage == buffer::usage::write_only ? g_w_total_count : g_r_total_count)        ++;
+               (usage == buffer::usage::write_only ? g_w_total_size : g_r_total_size)          += size_;
+               
+               if(timer.elapsed() > 0.02)
+                       CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer allocation blocked: " << timer.elapsed();
+       
+               //CASPAR_LOG(trace) << "[buffer] [" << ++(usage_ == buffer::usage::write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == buffer::usage::write_only ? "write_only" : "read_only");
+       }       
+
+       ~impl()
+       {
+               CASPAR_LOG_CALL(trace) << "buffer::~buffer() <- " << get_context();
+               glDeleteBuffers(1, &pbo_);
+               (usage_ == GL_STREAM_DRAW ? g_w_total_size : g_r_total_size)    -= size_;
+               (usage_ == GL_STREAM_DRAW ? g_w_total_count : g_r_total_count)  --;
+       }
+
+       void* map()
+       {
+               if(data_ != nullptr)
+                       return data_;
+               
+               caspar::timer timer;
+
+               GL(glBindBuffer(target_, pbo_));
+               if(usage_ == GL_STREAM_DRAW)                    
+                       GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
+               
+               auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
+               data_ = (uint8_t*) result;
+
+               if(timer.elapsed() > 0.02)
+                       CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer mapping blocked: " << timer.elapsed();
+
+               GL(glBindBuffer(target_, 0));
+               if(!data_)
+                       CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));
+
+               return data_;
+       }
+       
+       void unmap()
+       {
+               if(data_ == nullptr)
+                       return;
+               
+               GL(glBindBuffer(target_, pbo_));
+               GL(glUnmapBuffer(target_));     
+               if(usage_ == GL_STREAM_READ)                    
+                       GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
+               data_ = nullptr;        
+               GL(glBindBuffer(target_, 0));
+       }
+
+       void bind()
+       {
+               GL(glBindBuffer(target_, pbo_));
+       }
+
+       void unbind()
+       {
+               GL(glBindBuffer(target_, 0));
+       }
+};
+
+buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}
+buffer::buffer(buffer&& other) : impl_(std::move(other.impl_)){}
+buffer::~buffer(){}
+buffer& buffer::operator=(buffer&& other){impl_ = std::move(other.impl_); return *this;}
+uint8_t* buffer::data(){return impl_->data_;}
+void buffer::map(){impl_->map();}
+void buffer::unmap(){impl_->unmap();}
+void buffer::bind() const{impl_->bind();}
+void buffer::unbind() const{impl_->unbind();}
+std::size_t buffer::size() const { return impl_->size_; }
+int buffer::id() const {return impl_->pbo_;}
+
+boost::property_tree::wptree buffer::info()
+{
+       boost::property_tree::wptree info;
+
+       info.add(L"total_read_count", g_r_total_count);
+       info.add(L"total_write_count", g_w_total_count);
+       info.add(L"total_read_size", g_r_total_size);
+       info.add(L"total_write_size", g_w_total_size);
+
+       return info;
+}
+
+}}}