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>
34 #include <common/os/scheduling.h>
36 #include <SFML/Graphics.hpp>
38 #include <boost/optional.hpp>
39 #include <boost/circular_buffer.hpp>
40 #include <boost/lexical_cast.hpp>
42 #include <tbb/concurrent_unordered_map.h>
43 #include <tbb/atomic.h>
44 #include <tbb/spin_mutex.h>
52 namespace caspar { namespace core { namespace diagnostics { namespace osd {
54 static const int PREFERRED_VERTICAL_GRAPHS = 8;
55 static const int RENDERING_WIDTH = 750;
56 static const int RENDERING_HEIGHT = RENDERING_WIDTH / PREFERRED_VERTICAL_GRAPHS;
58 sf::Color get_sfml_color(int color)
60 auto c = caspar::diagnostics::color(color);
70 sf::Font& get_default_font()
72 static sf::Font DEFAULT_FONT = []()
75 if (!font.loadFromFile("LiberationSans-Regular.ttf"))
76 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("LiberationSans-Regular.ttf not found"));
83 struct drawable : public sf::Drawable, public sf::Transformable
86 virtual void render(sf::RenderTarget& target, sf::RenderStates states) = 0;
87 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override
89 states.transform *= getTransform();
90 const_cast<drawable*>(this)->render(target, states);
94 class context : public drawable
96 std::unique_ptr<sf::RenderWindow> window_;
99 std::list<std::weak_ptr<drawable>> drawables_;
100 int64_t refresh_rate_millis_ = 16;
101 boost::timer display_time_;
102 bool calculate_view_ = true;
103 int scroll_position_ = 0;
104 bool dragging_ = false;
105 int last_mouse_y_ = 0;
107 executor executor_ { L"diagnostics" };
110 static void register_drawable(const std::shared_ptr<drawable>& drawable)
115 get_instance()->executor_.begin_invoke([=]
117 get_instance()->do_register_drawable(drawable);
118 }, task_priority::high_priority);
121 static void show(bool value)
123 get_instance()->executor_.begin_invoke([=]
125 get_instance()->do_show(value);
126 }, task_priority::high_priority);
129 static void shutdown()
131 get_instance().reset();
136 executor_.begin_invoke([=]
138 set_priority_of_current_thread(thread_priority::LOW);
142 void do_show(bool value)
148 window_.reset(new sf::RenderWindow(sf::VideoMode(RENDERING_WIDTH, RENDERING_WIDTH), "CasparCG Diagnostics"));
149 window_->setPosition(sf::Vector2i(0, 0));
150 window_->setActive();
151 window_->setVerticalSyncEnabled(true);
152 calculate_view_ = true;
154 glEnable(GL_LINE_SMOOTH);
155 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
156 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
171 while (window_->pollEvent(e))
175 case sf::Event::Closed:
178 case sf::Event::Resized:
179 calculate_view_ = true;
181 case sf::Event::MouseButtonPressed:
183 last_mouse_y_ = e.mouseButton.y;
185 case sf::Event::MouseButtonReleased:
188 case sf::Event::MouseMoved:
191 auto delta_y = e.mouseMove.y - last_mouse_y_;
192 scroll_position_ += delta_y;
193 last_mouse_y_ = e.mouseMove.y;
194 calculate_view_ = true;
198 case sf::Event::MouseWheelMoved:
199 scroll_position_ += e.mouseWheel.delta * 15;
200 calculate_view_ = true;
209 view_.setViewport(sf::FloatRect(0, 0, 1.0, 1.0));
210 view_.setSize(RENDERING_WIDTH, window_->getSize().y);
211 view_.setCenter(RENDERING_WIDTH / 2, window_->getSize().y / 2 - scroll_position_);
212 window_->setView(view_);
213 calculate_view_ = false;
216 window_->draw(*this);
218 static const auto THRESHOLD = 1;
219 int64_t since_last_refresh = display_time_.elapsed() * 1000;
220 int64_t until_next_refresh = refresh_rate_millis_ - since_last_refresh;
221 int64_t sleep_for = until_next_refresh - THRESHOLD;
226 timer.tick_millis(0);
227 timer.tick_millis(sleep_for);
231 display_time_.restart();
232 executor_.begin_invoke([this]{tick();});
235 void render(sf::RenderTarget& target, sf::RenderStates states)
239 for (auto it = drawables_.begin(); it != drawables_.end(); ++n)
241 auto drawable = it->lock();
244 float target_y = n * RENDERING_HEIGHT;
245 drawable->setPosition(0.0f, target_y);
246 target.draw(*drawable, states);
250 it = drawables_.erase(it);
254 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
256 drawables_.push_back(drawable);
257 auto it = drawables_.begin();
258 while(it != drawables_.end())
263 it = drawables_.erase(it);
267 static std::unique_ptr<context>& get_instance()
269 static auto impl = std::unique_ptr<context>(new context);
274 class line : public drawable
277 boost::circular_buffer<sf::Vertex> line_data_ { res_ };
278 boost::circular_buffer<boost::optional<sf::VertexArray>> line_tags_ { res_ };
280 tbb::atomic<float> tick_data_;
281 tbb::atomic<bool> tick_tag_;
282 tbb::atomic<int> color_;
284 double x_delta_ = 1.0 / (res_ - 1);
285 //double x_pos_ = 1.0;
287 line(size_t res = 750)
295 void set_value(float value)
305 void set_color(int color)
315 void render(sf::RenderTarget& target, sf::RenderStates states)
317 /*states.transform.translate(x_pos_, 0.f);
319 if (line_data_.size() == res_)
320 x_pos_ = -get_insertion_xcoord() + 1.0 - x_delta_; // Otherwise the graph will drift because of floating point precision
322 x_pos_ -= x_delta_;*/
324 for (auto& vertex : line_data_)
325 vertex.position.x -= x_delta_;
327 auto color = get_sfml_color(color_);
329 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));
333 sf::VertexArray vertical_dash(sf::LinesStrip);
334 vertical_dash.append(sf::Vertex(sf::Vector2f(get_insertion_xcoord() - x_delta_, 0.f), color));
335 vertical_dash.append(sf::Vertex(sf::Vector2f(get_insertion_xcoord() - x_delta_, 1.f), color));
336 line_tags_.push_back(vertical_dash);
339 line_tags_.push_back(boost::none);
343 if (tick_data_ > -0.5)
345 auto array_one = line_data_.array_one();
346 auto array_two = line_data_.array_two();
347 // since boost::circular_buffer guarantees two contigous views of the buffer we can provide raw access to SFML, which can use glDrawArrays.
348 target.draw(array_one.first, static_cast<unsigned int>(array_one.second), sf::LinesStrip, states);
349 target.draw(array_two.first, static_cast<unsigned int>(array_two.second), sf::LinesStrip, states);
351 if (array_one.second > 0 && array_two.second > 0)
353 // Connect the gap between the arrays
354 sf::VertexArray connecting_line(sf::LinesStrip);
355 connecting_line.append(*(array_one.first + array_one.second - 1));
356 connecting_line.append(*(array_two.first));
357 target.draw(connecting_line, states);
362 glEnable(GL_LINE_STIPPLE);
363 glLineStipple(3, 0xAAAA);
365 for (size_t n = 0; n < line_tags_.size(); ++n)
369 target.draw(*line_tags_[n], states);
373 glDisable(GL_LINE_STIPPLE);
377 double get_insertion_xcoord() const
379 return line_data_.empty() ? 1.0 : line_data_.back().position.x + x_delta_;
383 struct graph : public drawable, public caspar::diagnostics::spi::graph_sink, public std::enable_shared_from_this<graph>
385 call_context context_ = call_context::for_thread();
386 tbb::concurrent_unordered_map<std::string, line> lines_;
388 tbb::spin_mutex mutex_;
390 bool auto_reset_ = false;
396 void activate() override
398 context::register_drawable(shared_from_this());
401 void set_text(const std::wstring& value) override
406 text_ = std::move(temp);
410 void set_value(const std::string& name, double value) override
412 lines_[name].set_value(value);
415 void set_tag(const std::string& name) override
417 lines_[name].set_tag();
420 void set_color(const std::string& name, int color) override
422 lines_[name].set_color(color);
425 void auto_reset() override
434 void render(sf::RenderTarget& target, sf::RenderStates states) override
436 const size_t text_size = 15;
437 const size_t text_margin = 2;
438 const size_t text_offset = (text_size+text_margin*2)*2;
440 std::wstring text_str;
444 tbb::spin_mutex::scoped_lock lock(mutex_);
446 auto_reset = auto_reset_;
449 sf::Text text(text_str.c_str(), get_default_font(), text_size);
450 text.setStyle(sf::Text::Italic);
451 text.move(text_margin, text_margin);
453 target.draw(text, states);
455 if (context_.video_channel != -1)
457 auto ctx_str = boost::lexical_cast<std::string>(context_.video_channel);
459 if (context_.layer != -1)
460 ctx_str += "-" + boost::lexical_cast<std::string>(context_.layer);
462 sf::Text context_text(ctx_str, get_default_font(), text_size);
463 context_text.setStyle(sf::Text::Italic);
464 context_text.move(RENDERING_WIDTH - text_margin - 5 - context_text.getLocalBounds().width, text_margin);
466 target.draw(context_text, states);
469 float x_offset = text_margin;
471 for(auto it = lines_.begin(); it != lines_.end(); ++it)
473 sf::Text line_text(it->first, get_default_font(), text_size);
474 line_text.setPosition(x_offset, text_margin+text_offset/2);
475 line_text.setColor(get_sfml_color(it->second.get_color()));
476 target.draw(line_text, states);
477 x_offset += line_text.getLocalBounds().width + text_margin * 2;
480 static const auto rect = []()
482 sf::RectangleShape r(sf::Vector2f(RENDERING_WIDTH, RENDERING_HEIGHT - 2));
483 r.setFillColor(sf::Color(255, 255, 255, 51));
484 r.setOutlineThickness(0.00f);
488 target.draw(rect, states);
491 .translate(0, text_offset)
492 .scale(RENDERING_WIDTH, RENDERING_HEIGHT * (static_cast<float>(RENDERING_HEIGHT - text_offset) / static_cast<float>(RENDERING_HEIGHT)));
494 static const sf::Color guide_color(255, 255, 255, 127);
495 static const sf::VertexArray middle_guide = []()
497 sf::VertexArray result(sf::LinesStrip);
498 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.5f), guide_color));
499 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.5f), guide_color));
502 static const sf::VertexArray bottom_guide = []()
504 sf::VertexArray result(sf::LinesStrip);
505 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.9f), guide_color));
506 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.9f), guide_color));
509 static const sf::VertexArray top_guide = []()
511 sf::VertexArray result(sf::LinesStrip);
512 result.append(sf::Vertex(sf::Vector2f(0.0f, 0.1f), guide_color));
513 result.append(sf::Vertex(sf::Vector2f(1.0f, 0.1f), guide_color));
517 glEnable(GL_LINE_STIPPLE);
518 glLineStipple(3, 0xAAAA);
520 target.draw(middle_guide, states);
521 target.draw(bottom_guide, states);
522 target.draw(top_guide, states);
524 glDisable(GL_LINE_STIPPLE);
526 for (auto it = lines_.begin(); it != lines_.end(); ++it)
528 target.draw(it->second, states);
530 it->second.set_value(0.0f);
537 caspar::diagnostics::spi::register_sink_factory([]
539 return spl::make_shared<graph>();
543 void show_graphs(bool value)
545 context::show(value);