\r
namespace caspar { namespace core {\r
\r
+struct render_item\r
+{\r
+ pixel_format_desc desc;\r
+ std::vector<safe_ptr<device_buffer>> textures;\r
+ core::image_transform transform;\r
+ int tag;\r
+};\r
+\r
struct image_mixer::implementation : boost::noncopyable\r
{ \r
static const size_t LOCAL_KEY_INDEX = 3;\r
static const size_t LAYER_KEY_INDEX = 4;\r
-\r
- struct render_item\r
- {\r
- pixel_format_desc desc;\r
- std::vector<safe_ptr<device_buffer>> textures;\r
- int tag;\r
- core::image_transform transform;\r
- };\r
-\r
+ \r
video_channel_context& channel_;\r
\r
std::stack<core::image_transform> transform_stack_;\r
- std::queue<std::queue<render_item>> render_queue_;\r
+\r
+ std::queue<std::queue<std::queue<render_item>>> render_queue_; // layer/stream/items\r
\r
image_kernel kernel_;\r
\r
safe_ptr<device_buffer> draw_buffer_;\r
safe_ptr<device_buffer> write_buffer_;\r
\r
- safe_ptr<device_buffer> local_key_buffer_;\r
+ safe_ptr<device_buffer> stream_key_buffer_;\r
safe_ptr<device_buffer> layer_key_buffer_;\r
\r
bool local_key_;\r
: channel_(video_channel)\r
, draw_buffer_(video_channel.ogl().create_device_buffer(video_channel.get_format_desc().width, channel_.get_format_desc().height, 4))\r
, write_buffer_ (video_channel.ogl().create_device_buffer(video_channel.get_format_desc().width, channel_.get_format_desc().height, 4))\r
- , local_key_buffer_(video_channel.ogl().create_device_buffer(video_channel.get_format_desc().width, channel_.get_format_desc().height, 1))\r
+ , stream_key_buffer_(video_channel.ogl().create_device_buffer(video_channel.get_format_desc().width, channel_.get_format_desc().height, 1))\r
, layer_key_buffer_(video_channel.ogl().create_device_buffer(video_channel.get_format_desc().width, channel_.get_format_desc().height, 1))\r
, local_key_(false)\r
, layer_key_(false)\r
\r
void visit(core::write_frame& frame)\r
{ \r
- render_item item = {frame.get_pixel_format_desc(), frame.get_textures(), frame.tag(), transform_stack_.top()*frame.get_image_transform()}; \r
- render_queue_.back().push(item);\r
+ render_item item = {frame.get_pixel_format_desc(), frame.get_textures(), transform_stack_.top()*frame.get_image_transform(), frame.tag()}; \r
+\r
+ auto& layer = render_queue_.back();\r
+\r
+ if(layer.empty() || (!layer.back().empty() && layer.back().back().tag != frame.tag()))\r
+ layer.push(std::queue<render_item>());\r
+ \r
+ layer.back().push(item);\r
}\r
\r
void end()\r
\r
void begin_layer()\r
{\r
- render_queue_.push(std::queue<render_item>());\r
+ render_queue_.push(std::queue<std::queue<render_item>>());\r
}\r
\r
void end_layer()\r
\r
void reinitialize_buffers()\r
{\r
- draw_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 4);\r
- write_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 4);\r
- local_key_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 1);\r
- layer_key_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 1);\r
+ draw_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 4);\r
+ write_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 4);\r
+ stream_key_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 1);\r
+ layer_key_buffer_ = channel_.ogl().create_device_buffer(channel_.get_format_desc().width, channel_.get_format_desc().height, 1);\r
channel_.ogl().gc();\r
}\r
\r
safe_ptr<host_buffer> render()\r
{ \r
- auto read_buffer = channel_.ogl().create_host_buffer(channel_.get_format_desc().size, host_buffer::read_only); \r
-\r
auto render_queue = std::move(render_queue_);\r
\r
- channel_.ogl().invoke([=]() mutable\r
+ return channel_.ogl().invoke([=]() mutable -> safe_ptr<host_buffer>\r
{\r
if(draw_buffer_->width() != channel_.get_format_desc().width || draw_buffer_->height() != channel_.get_format_desc().height)\r
reinitialize_buffers();\r
+ \r
+ return do_render(std::move(render_queue));\r
+ });\r
+ }\r
+ \r
+ safe_ptr<host_buffer> do_render(std::queue<std::queue<std::queue<render_item>>>&& render_queue)\r
+ {\r
+ auto read_buffer = channel_.ogl().create_host_buffer(channel_.get_format_desc().size, host_buffer::read_only);\r
\r
- local_key_ = false;\r
- layer_key_ = false;\r
-\r
- // Clear buffers.\r
- local_key_buffer_->clear();\r
- layer_key_buffer_->clear();\r
- draw_buffer_->clear();\r
+ layer_key_buffer_->clear();\r
+ draw_buffer_->clear();\r
\r
- // Draw items in device.\r
+ while(!render_queue.empty())\r
+ { \r
+ stream_key_buffer_->clear();\r
\r
- while(!render_queue.empty())\r
+ auto layer = std::move(render_queue.front());\r
+ render_queue.pop();\r
+ \r
+ while(!layer.empty())\r
{\r
- auto layer = render_queue.front();\r
- render_queue.pop();\r
- \r
- draw_buffer_->attach(); \r
- \r
- while(!layer.empty())\r
+ auto stream = std::move(layer.front());\r
+ layer.pop();\r
+\r
+ bool last_is_key = stream.empty() ? false : stream.back().transform.get_is_key();\r
+\r
+ while(!stream.empty())\r
{\r
- // Split layer into streams\r
- std::vector<render_item> stream;\r
- do\r
- {\r
- stream.push_back(layer.front());\r
- layer.pop();\r
- }\r
- while(!layer.empty() && layer.front().tag == stream.front().tag);\r
+ auto item = std::move(stream.front());\r
+ stream.pop();\r
\r
- draw(stream);\r
+ render(item);\r
\r
channel_.ogl().yield(); // Allow quick buffer allocation to execute.\r
- }\r
-\r
- layer_key_ = local_key_; // If there was only key in last layer then use it as key for the entire next layer.\r
- local_key_ = false;\r
+ } \r
\r
- std::swap(local_key_buffer_, layer_key_buffer_);\r
+ if(!last_is_key)\r
+ stream_key_buffer_->clear();\r
}\r
\r
- std::swap(draw_buffer_, write_buffer_);\r
+ std::swap(stream_key_buffer_, layer_key_buffer_);\r
+ }\r
\r
- // Start transfer from device to host. \r
- write_buffer_->write(*read_buffer);\r
- });\r
+ std::swap(draw_buffer_, write_buffer_);\r
+\r
+ // Start transfer from device to host. \r
+ write_buffer_->write(*read_buffer);\r
\r
return read_buffer;\r
}\r
- \r
- void draw(const std::vector<render_item>& stream)\r
- { \r
- if(stream.empty())\r
- return; \r
- \r
- BOOST_FOREACH(auto item, stream)\r
- {\r
- bool local_key = false;\r
- bool layer_key = false;\r
-\r
- // Setup key and kernel\r
- \r
- if(item.transform.get_is_key()) // This is a key frame, render it to the local_key buffer for later use.\r
- {\r
- if(!local_key_) // Initialize local-key if it is not active.\r
- {\r
- local_key_buffer_->clear();\r
- local_key_buffer_->attach();\r
- local_key_ = true;\r
- }\r
- } \r
- else // This is a normal frame. Use key buffers if they are active.\r
- { \r
- local_key = local_key_;\r
- layer_key = layer_key_;\r
-\r
- if(local_key_) // Use local key if we have it.\r
- {\r
- local_key_buffer_->bind(LOCAL_KEY_INDEX);\r
- draw_buffer_->attach(); \r
- } \r
\r
- if(layer_key_) // Use layer key if we have it.\r
- layer_key_buffer_->bind(LAYER_KEY_INDEX);\r
- } \r
-\r
- // Bind textures\r
-\r
- for(size_t n = 0; n < item.textures.size(); ++n)\r
- item.textures[n]->bind(n); \r
- \r
- // Draw\r
+ void render(render_item& item)\r
+ {\r
+ for(size_t n = 0; n < item.textures.size(); ++n)\r
+ item.textures[n]->bind(n); \r
+ \r
+ if(item.transform.get_is_key())\r
+ {\r
+ stream_key_buffer_->attach();\r
\r
- kernel_.draw(channel_.get_format_desc().width, channel_.get_format_desc().height, item.desc, item.transform, local_key, layer_key); \r
+ kernel_.draw(channel_.get_format_desc().width, channel_.get_format_desc().height, item.desc, item.transform, false, false); \r
}\r
+ else\r
+ {\r
+ stream_key_buffer_->bind(LOCAL_KEY_INDEX); \r
+ layer_key_buffer_->bind(LAYER_KEY_INDEX);\r
+ \r
+ draw_buffer_->attach(); \r
\r
- local_key_ = stream.back().transform.get_is_key(); // Keep for next layer if last frame is key\r
+ kernel_.draw(channel_.get_format_desc().width, channel_.get_format_desc().height, item.desc, item.transform, !stream_key_buffer_->empty(), !layer_key_buffer_->empty()); \r
+ }\r
}\r
- \r
+ \r
safe_ptr<write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc)\r
{\r
return make_safe<write_frame>(channel_.ogl(), reinterpret_cast<int>(tag), desc);\r