]> git.sesse.net Git - casparcg/blob - core/mixer/write_frame.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 #include "gpu/host_buffer.h"\r
26 #include "gpu/device_buffer.h"\r
27 \r
28 #include <core/producer/frame/frame_visitor.h>\r
29 #include <core/producer/frame/pixel_format.h>\r
30 \r
31 namespace caspar { namespace core {\r
32                                                                                                                                                                                                                                                                                                                         \r
33 struct write_frame::implementation\r
34 {                               \r
35         ogl_device*                                                                             ogl_;\r
36         std::vector<std::shared_ptr<host_buffer>>               buffers_;\r
37         std::array<std::shared_ptr<device_buffer>, 4>   textures_;\r
38         std::vector<int16_t>                                                    audio_data_;\r
39         const core::pixel_format_desc                                   desc_;\r
40         int                                                                                             tag_;\r
41         core::video_mode::type                                                  mode_;\r
42 \r
43         implementation()\r
44         {\r
45         }\r
46 \r
47         implementation(ogl_device& ogl, int tag, const core::pixel_format_desc& desc) \r
48                 : ogl_(&ogl)\r
49                 , desc_(desc)\r
50                 , tag_(tag)\r
51                 , mode_(core::video_mode::progressive)\r
52         {\r
53                 ogl_->invoke([&]\r
54                 {\r
55                         std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(buffers_), [&](const core::pixel_format_desc::plane& plane)\r
56                         {\r
57                                 return ogl_->create_host_buffer(plane.size, host_buffer::write_only);\r
58                         });\r
59                         std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(textures_), [&](const core::pixel_format_desc::plane& plane)\r
60                         {\r
61                                 return ogl_->create_device_buffer(plane.width, plane.height, plane.channels);   \r
62                         });\r
63                 }, high_priority);\r
64         }\r
65                         \r
66         void accept(write_frame& self, core::frame_visitor& visitor)\r
67         {\r
68                 visitor.begin(self);\r
69                 if(!desc_.planes().empty())\r
70                         visitor.visit(self);\r
71                 visitor.end();\r
72         }\r
73 \r
74         boost::iterator_range<uint8_t*> image_data(size_t index)\r
75         {\r
76                 if(index >= buffers_.size() || !buffers_[index]->data())\r
77                         return boost::iterator_range<const uint8_t*>();\r
78                 auto ptr = static_cast<uint8_t*>(buffers_[index]->data());\r
79                 return boost::iterator_range<uint8_t*>(ptr, ptr+buffers_[index]->size());\r
80         }\r
81 \r
82         const boost::iterator_range<const uint8_t*> image_data(size_t index) const\r
83         {\r
84                 if(index >= buffers_.size() || !buffers_[index]->data())\r
85                         return boost::iterator_range<const uint8_t*>();\r
86                 auto ptr = static_cast<const uint8_t*>(buffers_[index]->data());\r
87                 return boost::iterator_range<const uint8_t*>(ptr, ptr+buffers_[index]->size());\r
88         }\r
89 \r
90         void commit()\r
91         {\r
92                 for(size_t n = 0; n < buffers_.size(); ++n)\r
93                         commit(n);\r
94         }\r
95 \r
96         void commit(size_t plane_index)\r
97         {\r
98                 if(plane_index >= buffers_.size())\r
99                         return;\r
100                                 \r
101                 auto buffer = std::move(buffers_[plane_index]); // Release buffer once done.\r
102 \r
103                 if(!buffer)\r
104                         return;\r
105 \r
106                 auto texture\r
107                 \r
108                 ogl_->begin_invoke([=]\r
109                 {\r
110                         auto plane = desc_.planes[plane_index];\r
111                         textures_[plane_index] = ogl_->create_device_buffer(plane.width, plane.height, plane.channels);                 \r
112                         textures_[plane_index]->read(*buffer);\r
113                 }, high_priority);\r
114         }\r
115 };\r
116         \r
117 write_frame::write_frame() : impl_(new implementation()){}\r
118 write_frame::write_frame(ogl_device& ogl, int32_t tag, const core::pixel_format_desc& desc) \r
119         : impl_(new implementation(ogl, tag, desc)){}\r
120 write_frame::write_frame(const write_frame& other) : impl_(new implementation(*other.impl_)){}\r
121 void write_frame::accept(core::frame_visitor& visitor){impl_->accept(*this, visitor);}\r
122 \r
123 boost::iterator_range<uint8_t*> write_frame::image_data(size_t index){return impl_->image_data(index);}\r
124 std::vector<int16_t>& write_frame::audio_data() { return impl_->audio_data_; }\r
125 const boost::iterator_range<const uint8_t*> write_frame::image_data(size_t index) const\r
126 {\r
127         return boost::iterator_range<const uint8_t*>(impl_->image_data(index).begin(), impl_->image_data(index).end());\r
128 }\r
129 const boost::iterator_range<const int16_t*> write_frame::audio_data() const\r
130 {\r
131         return boost::iterator_range<const int16_t*>(impl_->audio_data_.data(), impl_->audio_data_.data() + impl_->audio_data_.size());\r
132 }\r
133 int write_frame::tag() const {return impl_->tag_;}\r
134 const core::pixel_format_desc& write_frame::get_pixel_format_desc() const{return impl_->desc_;}\r
135 const std::vector<safe_ptr<device_buffer>> write_frame::get_textures() const\r
136 {\r
137         std::vector<safe_ptr<device_buffer>> textures;\r
138         BOOST_FOREACH(auto texture, impl_->textures_)\r
139         {\r
140                 if(texture)\r
141                         textures.push_back(make_safe(texture));\r
142         }\r
143 \r
144         return textures;\r
145 }\r
146 void write_frame::commit(size_t plane_index){impl_->commit(plane_index);}\r
147 void write_frame::commit(){impl_->commit();}\r
148 void write_frame::set_type(const video_mode::type& mode){impl_->mode_ = mode;}\r
149 core::video_mode::type write_frame::get_type() const{return impl_->mode_;}\r
150 \r
151 }}