]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/buffer.cpp
Qualify buffer::usage enum
[casparcg] / accelerator / ogl / util / buffer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "buffer.h"
25
26 #include "texture.h"
27 #include "device.h"
28
29 #include <common/except.h>
30 #include <common/gl/gl_check.h>
31 #include <common/timer.h>
32
33 #include <GL/glew.h>
34
35 #include <boost/property_tree/ptree.hpp>
36
37 #include <tbb/atomic.h>
38
39 namespace caspar { namespace accelerator { namespace ogl {
40
41 static tbb::atomic<int>                 g_w_total_count;
42 static tbb::atomic<std::size_t> g_w_total_size;
43 static tbb::atomic<int>                 g_r_total_count;
44 static tbb::atomic<std::size_t> g_r_total_size;
45
46 struct buffer::impl : boost::noncopyable
47 {       
48         GLuint                                          pbo_;
49         const std::size_t                       size_;
50         tbb::atomic<uint8_t*>           data_;
51         GLenum                                          usage_;
52         GLenum                                          target_;
53
54 public:
55         impl(std::size_t size, buffer::usage usage) 
56                 : size_(size)
57                 , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
58                 , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
59         {
60                 caspar::timer timer;
61
62                 data_ = nullptr;
63                 GL(glGenBuffers(1, &pbo_));
64                 bind(); 
65                 GL(glBufferData(target_, size_, NULL, usage_));         
66                 if(usage_ == GL_STREAM_DRAW)
67                 {
68                         auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
69                         data_ = reinterpret_cast<uint8_t*>(result);
70                 }
71                 unbind();
72
73                 if(!pbo_)
74                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
75
76                 (usage == buffer::usage::write_only ? g_w_total_count : g_r_total_count)        ++;
77                 (usage == buffer::usage::write_only ? g_w_total_size : g_r_total_size)          += size_;
78                 
79                 if(timer.elapsed() > 0.02)
80                         CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer allocation blocked: " << timer.elapsed();
81         
82                 //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");
83         }       
84
85         ~impl()
86         {
87                 glDeleteBuffers(1, &pbo_);
88                 (usage_ == GL_STREAM_DRAW ? g_w_total_size : g_r_total_size)    -= size_;
89                 (usage_ == GL_STREAM_DRAW ? g_w_total_count : g_r_total_count)  --;
90         }
91
92         void* map()
93         {
94                 if(data_ != nullptr)
95                         return data_;
96                 
97                 caspar::timer timer;
98
99                 GL(glBindBuffer(target_, pbo_));
100                 if(usage_ == GL_STREAM_DRAW)                    
101                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
102                 
103                 auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
104                 data_ = (uint8_t*) result;
105
106                 if(timer.elapsed() > 0.02)
107                         CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer mapping blocked: " << timer.elapsed();
108
109                 GL(glBindBuffer(target_, 0));
110                 if(!data_)
111                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));
112
113                 return data_;
114         }
115         
116         void unmap()
117         {
118                 if(data_ == nullptr)
119                         return;
120                 
121                 GL(glBindBuffer(target_, pbo_));
122                 GL(glUnmapBuffer(target_));     
123                 if(usage_ == GL_STREAM_READ)                    
124                         GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
125                 data_ = nullptr;        
126                 GL(glBindBuffer(target_, 0));
127         }
128
129         void bind()
130         {
131                 GL(glBindBuffer(target_, pbo_));
132         }
133
134         void unbind()
135         {
136                 GL(glBindBuffer(target_, 0));
137         }
138 };
139
140 buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}
141 buffer::buffer(buffer&& other) : impl_(std::move(other.impl_)){}
142 buffer::~buffer(){}
143 buffer& buffer::operator=(buffer&& other){impl_ = std::move(other.impl_); return *this;}
144 uint8_t* buffer::data(){return impl_->data_;}
145 void buffer::map(){impl_->map();}
146 void buffer::unmap(){impl_->unmap();}
147 void buffer::bind() const{impl_->bind();}
148 void buffer::unbind() const{impl_->unbind();}
149 std::size_t buffer::size() const { return impl_->size_; }
150 int buffer::id() const {return impl_->pbo_;}
151
152 boost::property_tree::wptree buffer::info()
153 {
154         boost::property_tree::wptree info;
155
156         info.add(L"total_read_count", g_r_total_count);
157         info.add(L"total_write_count", g_w_total_count);
158         info.add(L"total_read_size", g_r_total_size);
159         info.add(L"total_write_size", g_w_total_size);
160
161         return info;
162 }
163
164 }}}