]> git.sesse.net Git - casparcg/blobdiff - common/diagnostics/graph.cpp
[streaming_consumer] Added support for sending recording activity via OSC like in...
[casparcg] / common / diagnostics / graph.cpp
index a3c7cd44dc3d222d518974d4be699281379e1108..539a92be63dd3016ddce77ebc189d00b0100a729 100644 (file)
-#include "../stdafx.h"\r
-\r
-#include "graph.h"\r
-\r
-#pragma warning (disable : 4244)\r
-\r
-#include "../concurrency/executor.h"\r
-#include "../utility/timer.h"\r
-\r
-#include <SFML/Graphics.hpp>\r
-\r
-#include <boost/foreach.hpp>\r
-#include <boost/circular_buffer.hpp>\r
-#include <boost/range/algorithm_ext/erase.hpp>\r
-\r
-#include <numeric>\r
-#include <map>\r
-#include <array>\r
-\r
-namespace caspar { namespace diagnostics {\r
-\r
-struct drawable\r
-{\r
-       virtual ~drawable(){}\r
-       virtual void draw(double dy, double y) = 0;\r
-};\r
-\r
-class context\r
-{      \r
-       timer timer_;\r
-       executor executor_;\r
-       sf::RenderWindow window_;\r
-       \r
-       std::list<std::weak_ptr<drawable>> drawables_;\r
-               \r
-       void tick()\r
-       {\r
-               sf::Event e;\r
-               while(window_.GetEvent(e)){}\r
-               window_.Clear();\r
-               render();\r
-               window_.Display();\r
-               timer_.tick(1.0/50.0);\r
-               executor_.begin_invoke([this]{tick();});\r
-       }\r
-\r
-       void render()\r
-       {\r
-               float dy = 1.0/static_cast<float>(std::max<int>(5, drawables_.size()));\r
-               float y = 1.0-dy;\r
-               for(auto it = drawables_.begin(); it != drawables_.end();)\r
-               {\r
-                       auto drawable = it->lock();\r
-                       if(drawable)\r
-                       {\r
-                               drawable->draw(dy, y);          \r
-                               y -= dy;// + 0.01;\r
-                               ++it;\r
-                       }\r
-                       else                    \r
-                               it = drawables_.erase(it);                      \r
-               }\r
-       }       \r
-       static context& get_instance()\r
-       {\r
-               static context impl;\r
-               return impl;\r
-       }\r
-\r
-       context()\r
-       {\r
-               executor_.start();\r
-               executor_.begin_invoke([this]\r
-               {\r
-                       window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
-                       window_.SetPosition(0, 0);\r
-                       window_.SetActive();\r
-                       glEnable(GL_BLEND);\r
-                       glEnable(GL_LINE_SMOOTH);\r
-                       glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
-                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
-                       tick();\r
-               });\r
-       }\r
-\r
-public:        \r
-                               \r
-       template<typename Func>\r
-       static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
-       {       \r
-               return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
-       }\r
-\r
-       static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
-       {\r
-               begin_invoke([=]\r
-               {\r
-                       get_instance().drawables_.push_back(drawable);\r
-               });\r
-       }\r
-};\r
-\r
-class line\r
-{\r
-       boost::circular_buffer<float> line_data_;\r
-       std::vector<float> tick_data_;\r
-       std::array<float, 3> color_;\r
-public:\r
-       line(size_t res = 600)\r
-               : line_data_(res)\r
-       {\r
-               color(1.0f, 1.0f, 1.0f);\r
-       }\r
-       \r
-       void update(float value)\r
-       {\r
-               tick_data_.push_back(value);\r
-       }\r
-       \r
-       void color(float r, float g, float b)\r
-       {\r
-               color_[0] = r; color_[1] = g; color_[2] = b;\r
-       }\r
-       \r
-       void draw(double dy, double y)\r
-       {\r
-               float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
-               float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
-\r
-               if(!tick_data_.empty())\r
-               {\r
-                       float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
-                       line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));\r
-                       tick_data_.clear();\r
-               }\r
-               else if(!line_data_.empty())\r
-               {\r
-                       line_data_.push_back(line_data_.back());\r
-               }\r
-               \r
-               glBegin(GL_LINE_STRIP);\r
-               glColor4f(color_[0], color_[1], color_[2], 1.0f);                       \r
-               for(size_t n = 0; n < line_data_.size(); ++n)                           \r
-                       glVertex3f((x+n*dx)*2.0f-1.0f, (y + dy * std::max(0.05f, std::min(0.95f, line_data_[n]*0.8f + 0.1f))) * 2.0f - 1.0f, 0.0f);             \r
-               glEnd();\r
-       }\r
-};\r
-       \r
-class guide\r
-{\r
-       std::array<float, 3> color_;\r
-       float value_;\r
-public:\r
-       guide() : value_(0.0f)\r
-       {\r
-               color_[0] = color_[1] = color_[2] = 0.0f;\r
-       }\r
-\r
-       guide(float value, float r, float g, float b) : value_(value)\r
-       {\r
-               color_[0] = r; color_[1] = g; color_[2] = b;\r
-       }\r
-                       \r
-       void draw(double dy, double y)\r
-       {               \r
-               glEnable(GL_LINE_STIPPLE);\r
-               glLineStipple(3, 0xAAAA);\r
-               glBegin(GL_LINE_STRIP);\r
-               glColor4f(color_[0], color_[1], color_[2], 1.0f);                               \r
-                       glVertex3f(0.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 0.0f);              \r
-                       glVertex3f(1.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 0.0f);      \r
-               glEnd();\r
-               glDisable(GL_LINE_STIPPLE);\r
-       }\r
-};\r
-\r
-struct graph::implementation : public drawable\r
-{\r
-       std::map<std::string, diagnostics::line> lines_;\r
-       std::map<std::string, diagnostics::guide> guides_;\r
-\r
-       implementation(const std::string&)\r
-       {\r
-               guides_["max"] = diagnostics::guide(1.0f, 0.4f, 0.4f, 0.4f);\r
-               guides_["min"] = diagnostics::guide(0.0f, 0.4f, 0.4f, 0.4f);\r
-       }\r
-\r
-       void update(const std::string& name, float value)\r
-       {\r
-               context::begin_invoke([=]\r
-               {\r
-                       lines_[name].update(value);\r
-               });\r
-       }\r
-\r
-       void color(const std::string& name, float r, float g, float b)\r
-       {\r
-               context::begin_invoke([=]\r
-               {\r
-                       lines_[name].color(r, g, b);\r
-               });\r
-       }\r
-       \r
-       void line(const std::string& name, float value, float r, float g, float b)\r
-       {\r
-               context::begin_invoke([=]\r
-               {\r
-                       guides_[name] = diagnostics::guide(value, r, g, b);\r
-               });\r
-       }\r
-\r
-private:\r
-       void draw(double dy, double y)\r
-       {\r
-               glBegin(GL_QUADS);\r
-                       glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
-                       glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);\r
-                       glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);\r
-                       glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);    \r
-                       glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);    \r
-               glEnd();\r
-               \r
-               for(auto it = guides_.begin(); it != guides_.end(); ++it)\r
-                       it->second.draw(dy, y);\r
-\r
-               for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
-                       it->second.draw(dy, y);\r
-       }\r
-\r
-       implementation(implementation&);\r
-       implementation& operator=(implementation&);\r
-};\r
-       \r
-graph::graph(const std::string& name) : impl_(new implementation(name))\r
-{\r
-       context::register_drawable(impl_);\r
-}\r
-void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
-void graph::color(const std::string& name, float r, float g, float b){impl_->color(name, r, g, b);}\r
-void graph::line(const std::string& name, float value, float r, float g, float b){impl_->line(name, value, r, g, b);}\r
-\r
-safe_ptr<graph> create_graph(const std::string& name)\r
-{\r
-       return safe_ptr<graph>(new graph(name));\r
-}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../stdafx.h"
+
+#include "graph.h"
+
+#include "../linq.h"
+
+#include <boost/thread.hpp>
+
+#include <tbb/spin_mutex.h>
+
+#include <vector>
+
+namespace caspar { namespace diagnostics {
+               
+int color(float r, float g, float b, float a)
+{
+       int code = 0;
+       code |= static_cast<int>(r*255.0f+0.5f) << 24;
+       code |= static_cast<int>(g*255.0f+0.5f) << 16;
+       code |= static_cast<int>(b*255.0f+0.5f) <<  8;
+       code |= static_cast<int>(a*255.0f+0.5f) <<  0;
+       return code;
+}
+
+std::tuple<float, float, float, float> color(int code)
+{
+       float r = static_cast<float>((code >> 24) & 255)/255.0f;
+       float g = static_cast<float>((code >> 16) & 255)/255.0f;
+       float b = static_cast<float>((code >>  8) & 255)/255.0f;
+       float a = static_cast<float>((code >>  0) & 255)/255.0f;
+       return std::make_tuple(r, g, b, a);
+}
+
+typedef std::vector<spi::sink_factory_t> sink_factories_t;
+static boost::mutex g_sink_factories_mutex;
+static sink_factories_t g_sink_factories;
+
+std::vector<spl::shared_ptr<spi::graph_sink>> create_sinks()
+{
+       boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
+
+       return cpplinq::from(g_sink_factories)
+               .select([](const spi::sink_factory_t& s){ return s(); })
+               .to_vector();
+}
+
+struct graph::impl
+{
+       std::vector<spl::shared_ptr<spi::graph_sink>> sinks_ = create_sinks();
+public:
+       impl()
+       {
+       }
+
+       void activate()
+       {
+               for (auto& sink : sinks_)
+                       sink->activate();
+       }
+
+       void set_text(const std::wstring& value)
+       {
+               for (auto& sink : sinks_)
+                       sink->set_text(value);
+       }
+
+       void set_value(const std::string& name, double value)
+       {
+               for (auto& sink : sinks_)
+                       sink->set_value(name, value);
+       }
+
+       void set_tag(tag_severity severity, const std::string& name)
+       {
+               for (auto& sink : sinks_)
+                       sink->set_tag(severity, name);
+       }
+
+       void set_color(const std::string& name, int color)
+       {
+               for (auto& sink : sinks_)
+                       sink->set_color(name, color);
+       }
+
+       void auto_reset()
+       {
+               for (auto& sink : sinks_)
+                       sink->auto_reset();
+       }
+               
+private:
+       impl(impl&);
+       impl& operator=(impl&);
+};
+       
+graph::graph() : impl_(new impl)
+{
+}
+
+void graph::set_text(const std::wstring& value) { impl_->set_text(value); }
+void graph::set_value(const std::string& name, double value) { impl_->set_value(name, value); }
+void graph::set_color(const std::string& name, int color) { impl_->set_color(name, color); }
+void graph::set_tag(tag_severity severity, const std::string& name) { impl_->set_tag(severity, name); }
+void graph::auto_reset() { impl_->auto_reset(); }
+
+void register_graph(const spl::shared_ptr<graph>& graph)
+{
+       graph->impl_->activate();
+}
+
+namespace spi {
+
+void register_sink_factory(sink_factory_t factory)
+{
+       boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
+
+       g_sink_factories.push_back(std::move(factory));
+}
+
+}
+
 }}
\ No newline at end of file