2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../stdafx.h"
24 #include "osd_graph.h"
26 #pragma warning (disable : 4244)
28 #include "call_context.h"
30 #include <common/executor.h>
31 #include <common/lock.h>
32 #include <common/env.h>
33 #include <common/prec_timer.h>
35 #include <SFML/Graphics.hpp>
37 #include <boost/optional.hpp>
38 #include <boost/circular_buffer.hpp>
39 #include <boost/lexical_cast.hpp>
41 #include <tbb/concurrent_unordered_map.h>
42 #include <tbb/atomic.h>
43 #include <tbb/spin_mutex.h>
53 namespace caspar { namespace core { namespace diagnostics { namespace osd {
55 static const int PREFERRED_VERTICAL_GRAPHS = 8;
56 static const int RENDERING_WIDTH = 750;
57 static const int RENDERING_HEIGHT = RENDERING_WIDTH / PREFERRED_VERTICAL_GRAPHS;
59 sf::Color get_sfml_color(int color)
61 auto c = caspar::diagnostics::color(color);
71 sf::Font& get_default_font()
73 static sf::Font DEFAULT_FONT = []()
76 if (!font.loadFromFile("LiberationSans-Regular.ttf"))
77 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("LiberationSans-Regular.ttf not found"));
84 struct drawable : public sf::Drawable, public sf::Transformable
87 virtual void render(sf::RenderTarget& target, sf::RenderStates states) = 0;
88 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override
90 states.transform *= getTransform();
91 const_cast<drawable*>(this)->render(target, states);
95 class context : public drawable
97 std::unique_ptr<sf::RenderWindow> window_;
100 std::list<std::weak_ptr<drawable>> drawables_;
101 int64_t refresh_rate_millis_ = 16;
102 boost::timer display_time_;
103 bool calculate_view_;
104 int scroll_position_ = 0;
105 bool dragging_ = false;
108 executor executor_ = L"diagnostics";
111 static void register_drawable(const std::shared_ptr<drawable>& drawable)
116 get_instance()->executor_.begin_invoke([=]
118 get_instance()->do_register_drawable(drawable);
119 }, task_priority::high_priority);
122 static void show(bool value)
124 get_instance()->executor_.begin_invoke([=]
126 get_instance()->do_show(value);
127 }, task_priority::high_priority);
130 static void shutdown()
132 get_instance().reset();
137 executor_.begin_invoke([=]
139 SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);
143 void do_show(bool value)
149 window_.reset(new sf::RenderWindow(sf::VideoMode(RENDERING_WIDTH, RENDERING_WIDTH), "CasparCG Diagnostics"));
150 window_->setPosition(sf::Vector2i(0, 0));
151 window_->setActive();
152 window_->setVerticalSyncEnabled(true);
153 calculate_view_ = true;
155 glEnable(GL_LINE_SMOOTH);
156 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
157 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 while (window_->pollEvent(e))
176 case sf::Event::Closed:
179 case sf::Event::Resized:
180 calculate_view_ = true;
182 case sf::Event::MouseButtonPressed:
184 last_mouse_y_ = e.mouseButton.y;
186 case sf::Event::MouseButtonReleased:
189 case sf::Event::MouseMoved:
192 auto delta_y = e.mouseMove.y - last_mouse_y_;
193 scroll_position_ += delta_y;
194 last_mouse_y_ = e.mouseMove.y;
195 calculate_view_ = true;
199 case sf::Event::MouseWheelMoved:
200 scroll_position_ += e.mouseWheel.delta * 15;
201 calculate_view_ = true;
210 view_.setViewport(sf::FloatRect(0, 0, 1.0, 1.0));
211 view_.setSize(RENDERING_WIDTH, window_->getSize().y);
212 view_.setCenter(RENDERING_WIDTH / 2, window_->getSize().y / 2 - scroll_position_);
213 window_->setView(view_);
214 calculate_view_ = false;
217 window_->draw(*this);
219 static const auto THRESHOLD = 1;
220 int64_t since_last_refresh = display_time_.elapsed() * 1000;
221 int64_t until_next_refresh = refresh_rate_millis_ - since_last_refresh;
222 int64_t sleep_for = until_next_refresh - THRESHOLD;
227 timer.tick_millis(0);
228 timer.tick_millis(sleep_for);
232 display_time_.restart();
233 executor_.begin_invoke([this]{tick();});
236 void render(sf::RenderTarget& target, sf::RenderStates states)
240 for (auto it = drawables_.begin(); it != drawables_.end(); ++n)
242 auto drawable = it->lock();
245 float target_y = n * RENDERING_HEIGHT;
246 drawable->setPosition(0.0f, target_y);
247 target.draw(*drawable, states);
251 it = drawables_.erase(it);
255 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
257 drawables_.push_back(drawable);
258 auto it = drawables_.begin();
259 while(it != drawables_.end())
264 it = drawables_.erase(it);
268 static std::unique_ptr<context>& get_instance()
270 static auto impl = std::unique_ptr<context>(new context);
275 class line : public drawable
278 boost::circular_buffer<sf::Vertex> line_data_ { res_ };
279 boost::circular_buffer<boost::optional<sf::VertexArray>> line_tags_ { res_ };
281 tbb::atomic<float> tick_data_;
282 tbb::atomic<bool> tick_tag_;
283 tbb::atomic<int> color_;
285 double x_delta_ = 1.0 / (res_ - 1);
286 //double x_pos_ = 1.0;
288 line(size_t res = 750)
296 void set_value(float value)
306 void set_color(int color)
316 void render(sf::RenderTarget& target, sf::RenderStates states)
318 /*states.transform.translate(x_pos_, 0.f);
320 if (line_data_.size() == res_)
321 x_pos_ = -get_insertion_xcoord() + 1.0 - x_delta_; // Otherwise the graph will drift because of floating point precision
323 x_pos_ -= x_delta_;*/
325 for (auto& vertex : line_data_)
326 vertex.position.x -= x_delta_;
328 auto color = get_sfml_color(color_);
330 line_data_.push_back(sf::Vertex(sf::Vector2f(get_insertion_xcoord(), std::max(0.05, std::min(0.95, (1.0f - tick_data_) * 0.8 + 0.1f))), color));
334 sf::VertexArray vertical_dash(sf::LinesStrip);
335 vertical_dash.append(sf::Vertex(sf::Vector2f(get_insertion_xcoord() - x_delta_, 0.f), color));
336 vertical_dash.append(sf::Vertex(sf::Vector2f(get_insertion_xcoord() - x_delta_, 1.f), color));
337 line_tags_.push_back(vertical_dash);
340 line_tags_.push_back(boost::none);
344 if (tick_data_ > -0.5)
346 auto array_one = line_data_.array_one();
347 auto array_two = line_data_.array_two();
348 // since boost::circular_buffer guarantees two contigous views of the buffer we can provide raw access to SFML, which can use glDrawArrays.
349 target.draw(array_one.first, static_cast<unsigned int>(array_one.second), sf::LinesStrip, states);
350 target.draw(array_two.first, static_cast<unsigned int>(array_two.second), sf::LinesStrip, states);
352 if (array_one.second > 0 && array_two.second > 0)
354 // Connect the gap between the arrays
355 sf::VertexArray connecting_line(sf::LinesStrip);
356 connecting_line.append(*(array_one.first + array_one.second - 1));
357 connecting_line.append(*(array_two.first));
358 target.draw(connecting_line, states);
363 glEnable(GL_LINE_STIPPLE);
364 glLineStipple(3, 0xAAAA);
366 for (size_t n = 0; n < line_tags_.size(); ++n)
370 target.draw(*line_tags_[n], states);
374 glDisable(GL_LINE_STIPPLE);
378 double get_insertion_xcoord() const
380 return line_data_.empty() ? 1.0 : line_data_.back().position.x + x_delta_;
384 struct graph : public drawable, public caspar::diagnostics::spi::graph_sink, public std::enable_shared_from_this<graph>
386 call_context context_ = call_context::for_thread();
387 tbb::concurrent_unordered_map<std::string, line> lines_;
389 tbb::spin_mutex mutex_;
391 bool auto_reset_ = false;
397 void activate() override
399 context::register_drawable(shared_from_this());
402 void set_text(const std::wstring& value) override
407 text_ = std::move(temp);
411 void set_value(const std::string& name, double value) override
413 lines_[name].set_value(value);
416 void set_tag(const std::string& name) override
418 lines_[name].set_tag();
421 void set_color(const std::string& name, int color) override
423 lines_[name].set_color(color);
426 void auto_reset() override
435 void render(sf::RenderTarget& target, sf::RenderStates states) override
437 const size_t text_size = 15;
438 const size_t text_margin = 2;
439 const size_t text_offset = (text_size+text_margin*2)*2;
441 std::wstring text_str;
445 tbb::spin_mutex::scoped_lock lock(mutex_);
447 auto_reset = auto_reset_;
450 sf::Text text(text_str.c_str(), get_default_font(), text_size);
451 text.setStyle(sf::Text::Italic);
452 text.move(text_margin, text_margin);
454 target.draw(text, states);
456 if (context_.video_channel != -1)
458 auto ctx_str = boost::lexical_cast<std::string>(context_.video_channel);
460 if (context_.layer != -1)
461 ctx_str += "-" + boost::lexical_cast<std::string>(context_.layer);
463 sf::Text context_text(ctx_str, get_default_font(), text_size);
464 context_text.setStyle(sf::Text::Italic);
465 context_text.move(RENDERING_WIDTH - text_margin - 5 - context_text.getLocalBounds().width, text_margin);
467 target.draw(context_text, states);
470 float x_offset = text_margin;
472 for(auto it = lines_.begin(); it != lines_.end(); ++it)
474 sf::Text line_text(it->first, get_default_font(), text_size);
475 line_text.setPosition(x_offset, text_margin+text_offset/2);
476 line_text.setColor(get_sfml_color(it->second.get_color()));
477 target.draw(line_text, states);
478 x_offset += line_text.getLocalBounds().width + text_margin * 2;
481 static const auto rect = []()
483 sf::RectangleShape r(sf::Vector2f(RENDERING_WIDTH, RENDERING_HEIGHT - 2));
484 r.setFillColor(sf::Color(255, 255, 255, 51));
485 r.setOutlineThickness(0.00f);
489 target.draw(rect, states);
492 .translate(0, text_offset)
493 .scale(RENDERING_WIDTH, RENDERING_HEIGHT * (static_cast<float>(RENDERING_HEIGHT - text_offset) / static_cast<float>(RENDERING_HEIGHT)));
495 static const sf::Color guide_color(255, 255, 255, 127);
496 static const sf::VertexArray middle_guide = []()
498 sf::VertexArray result(sf::LinesStrip);
499 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.5f), guide_color));
500 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.5f), guide_color));
503 static const sf::VertexArray bottom_guide = []()
505 sf::VertexArray result(sf::LinesStrip);
506 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.9f), guide_color));
507 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.9f), guide_color));
510 static const sf::VertexArray top_guide = []()
512 sf::VertexArray result(sf::LinesStrip);
513 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.1f), guide_color));
514 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.1f), guide_color));
518 glEnable(GL_LINE_STIPPLE);
519 glLineStipple(3, 0xAAAA);
521 target.draw(middle_guide, states);
522 target.draw(bottom_guide, states);
523 target.draw(top_guide, states);
525 glDisable(GL_LINE_STIPPLE);
527 for (auto it = lines_.begin(); it != lines_.end(); ++it)
529 target.draw(it->second, states);
531 it->second.set_value(0.0f);
538 caspar::diagnostics::spi::register_sink_factory([]
540 return spl::make_shared<graph>();
544 void show_graphs(bool value)
546 context::show(value);