]> git.sesse.net Git - casparcg/blob - core/mixer/write_frame.cpp
2.0.0.2: Further reduced frame latency at the cost of larger memory requirements.
[casparcg] / core / mixer / write_frame.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../stdafx.h"\r
21 \r
22 #include "write_frame.h"\r
23 \r
24 #include "gpu/ogl_device.h"\r
25 \r
26 #include <core/producer/frame/pixel_format.h>\r
27 \r
28 #include <common/gl/gl_check.h>\r
29 \r
30 #include <boost/range/algorithm.hpp>\r
31 \r
32 namespace caspar { namespace core {\r
33                                                                                                                                                                                                                                                                                                                         \r
34 struct write_frame::implementation : boost::noncopyable\r
35 {                               \r
36         std::vector<safe_ptr<host_buffer>> buffers_;\r
37         std::vector<safe_ptr<device_buffer>> textures_;\r
38         std::vector<short> audio_data_;\r
39         const core::pixel_format_desc desc_;\r
40         int tag_;\r
41 \r
42 public:\r
43         implementation(int tag, const core::pixel_format_desc& desc, const std::vector<safe_ptr<host_buffer>>& buffers, const std::vector<safe_ptr<device_buffer>>& textures) \r
44                 : desc_(desc)\r
45                 , buffers_(buffers)\r
46                 , textures_(textures)\r
47                 , tag_(tag)\r
48         {}\r
49         \r
50         void accept(write_frame& self, core::frame_visitor& visitor)\r
51         {\r
52                 visitor.begin(self);\r
53                 visitor.visit(self);\r
54                 visitor.end();\r
55         }\r
56 \r
57         boost::iterator_range<unsigned char*> image_data(size_t index)\r
58         {\r
59                 if(index >= buffers_.size() || !buffers_[index]->data())\r
60                         return boost::iterator_range<const unsigned char*>();\r
61                 auto ptr = static_cast<unsigned char*>(buffers_[index]->data());\r
62                 return boost::iterator_range<unsigned char*>(ptr, ptr+buffers_[index]->size());\r
63         }\r
64 \r
65         const boost::iterator_range<const unsigned char*> image_data(size_t index) const\r
66         {\r
67                 if(index >= buffers_.size() || !buffers_[index]->data())\r
68                         return boost::iterator_range<const unsigned char*>();\r
69                 auto ptr = static_cast<const unsigned char*>(buffers_[index]->data());\r
70                 return boost::iterator_range<const unsigned char*>(ptr, ptr+buffers_[index]->size());\r
71         }\r
72 \r
73         void commit()\r
74         {\r
75                 for(size_t n = 0; n < buffers_.size(); ++n)\r
76                         commit(n);\r
77         }\r
78 \r
79         void commit(size_t plane_index)\r
80         {\r
81                 if(plane_index >= buffers_.size())\r
82                         return;\r
83                                 \r
84                 auto texture = textures_[plane_index];\r
85                 auto buffer = std::move(buffers_[plane_index]);\r
86 \r
87                 ogl_device::begin_invoke([=]\r
88                 {\r
89                         texture->read(*buffer);\r
90                 });\r
91         }\r
92 };\r
93         \r
94 write_frame::write_frame(int tag, const core::pixel_format_desc& desc, const std::vector<safe_ptr<host_buffer>>& buffers, const std::vector<safe_ptr<device_buffer>>& textures) : impl_(new implementation(tag, desc, buffers, textures)){}\r
95 void write_frame::accept(core::frame_visitor& visitor){impl_->accept(*this, visitor);}\r
96 \r
97 boost::iterator_range<unsigned char*> write_frame::image_data(size_t index){return impl_->image_data(index);}\r
98 std::vector<short>& write_frame::audio_data() { return impl_->audio_data_; }\r
99 const boost::iterator_range<const unsigned char*> write_frame::image_data(size_t index) const\r
100 {\r
101         return boost::iterator_range<const unsigned char*>(impl_->image_data(index).begin(), impl_->image_data(index).end());\r
102 }\r
103 const boost::iterator_range<const short*> write_frame::audio_data() const\r
104 {\r
105         return boost::iterator_range<const short*>(impl_->audio_data_.data(), impl_->audio_data_.data() + impl_->audio_data_.size());\r
106 }\r
107 int write_frame::tag() const {return impl_->tag_;}\r
108 const core::pixel_format_desc& write_frame::get_pixel_format_desc() const{return impl_->desc_;}\r
109 const std::vector<safe_ptr<device_buffer>>& write_frame::get_textures() const{return impl_->textures_;}\r
110 const std::vector<safe_ptr<host_buffer>>& write_frame::get_buffers() const{return impl_->buffers_;}\r
111 void write_frame::commit(size_t plane_index){impl_->commit(plane_index);}\r
112 void write_frame::commit(){impl_->commit();}\r
113 }}