]> git.sesse.net Git - casparcg/blob - core/mixer/write_frame.cpp
2.0: Minor cleanup.
[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 : boost::noncopyable\r
34 {                               \r
35         ogl_device&                                                             ogl_;\r
36         std::vector<safe_ptr<host_buffer>>              buffers_;\r
37         std::vector<safe_ptr<device_buffer>>    textures_;\r
38         std::vector<int16_t>                                    audio_data_;\r
39         const core::pixel_format_desc                   desc_;\r
40         int                                                                             tag_;\r
41 \r
42 public:\r
43         implementation(ogl_device& ogl, int tag, const core::pixel_format_desc& desc) \r
44                 : ogl_(ogl)\r
45                 , desc_(desc)\r
46                 , tag_(tag)\r
47         {\r
48                 ogl_.invoke([&]\r
49                 {\r
50                         std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(buffers_), [&](const core::pixel_format_desc::plane& plane)\r
51                         {\r
52                                 return ogl_.create_host_buffer(plane.size, host_buffer::write_only);\r
53                         });\r
54                         std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(textures_), [&](const core::pixel_format_desc::plane& plane)\r
55                         {\r
56                                 return ogl_.create_device_buffer(plane.width, plane.height, plane.channels);\r
57                         });\r
58                 }, high_priority);\r
59         }\r
60 \r
61         ~implementation()\r
62         {\r
63                 ogl_.invoke([=]\r
64                 {\r
65                         buffers_.clear();\r
66                         textures_.clear();\r
67                 }, high_priority);\r
68         }\r
69         \r
70         void accept(write_frame& self, core::frame_visitor& visitor)\r
71         {\r
72                 visitor.begin(self);\r
73                 visitor.visit(self);\r
74                 visitor.end();\r
75         }\r
76 \r
77         boost::iterator_range<uint8_t*> image_data(size_t index)\r
78         {\r
79                 if(index >= buffers_.size() || !buffers_[index]->data())\r
80                         return boost::iterator_range<const uint8_t*>();\r
81                 auto ptr = static_cast<uint8_t*>(buffers_[index]->data());\r
82                 return boost::iterator_range<uint8_t*>(ptr, ptr+buffers_[index]->size());\r
83         }\r
84 \r
85         const boost::iterator_range<const uint8_t*> image_data(size_t index) const\r
86         {\r
87                 if(index >= buffers_.size() || !buffers_[index]->data())\r
88                         return boost::iterator_range<const uint8_t*>();\r
89                 auto ptr = static_cast<const uint8_t*>(buffers_[index]->data());\r
90                 return boost::iterator_range<const uint8_t*>(ptr, ptr+buffers_[index]->size());\r
91         }\r
92 \r
93         void commit()\r
94         {\r
95                 for(size_t n = 0; n < buffers_.size(); ++n)\r
96                         commit(n);\r
97         }\r
98 \r
99         void commit(size_t plane_index)\r
100         {\r
101                 if(plane_index >= buffers_.size())\r
102                         return;\r
103                                 \r
104                 auto texture = textures_[plane_index];\r
105                 auto buffer = std::move(buffers_[plane_index]); // Release buffer once done.\r
106 \r
107                 ogl_.begin_invoke([=]\r
108                 {\r
109                         texture->read(*buffer);\r
110                 });\r
111         }\r
112 };\r
113         \r
114 write_frame::write_frame(ogl_device& ogl, int32_t tag, const core::pixel_format_desc& desc) \r
115         : impl_(new implementation(ogl, tag, desc)){}\r
116 void write_frame::accept(core::frame_visitor& visitor){impl_->accept(*this, visitor);}\r
117 \r
118 boost::iterator_range<uint8_t*> write_frame::image_data(size_t index){return impl_->image_data(index);}\r
119 std::vector<int16_t>& write_frame::audio_data() { return impl_->audio_data_; }\r
120 const boost::iterator_range<const uint8_t*> write_frame::image_data(size_t index) const\r
121 {\r
122         return boost::iterator_range<const uint8_t*>(impl_->image_data(index).begin(), impl_->image_data(index).end());\r
123 }\r
124 const boost::iterator_range<const int16_t*> write_frame::audio_data() const\r
125 {\r
126         return boost::iterator_range<const int16_t*>(impl_->audio_data_.data(), impl_->audio_data_.data() + impl_->audio_data_.size());\r
127 }\r
128 int write_frame::tag() const {return impl_->tag_;}\r
129 const core::pixel_format_desc& write_frame::get_pixel_format_desc() const{return impl_->desc_;}\r
130 const std::vector<safe_ptr<device_buffer>>& write_frame::get_textures() const{return impl_->textures_;}\r
131 void write_frame::commit(size_t plane_index){impl_->commit(plane_index);}\r
132 void write_frame::commit(){impl_->commit();}\r
133 }}