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