]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
- Fixed diag to work with new SFML version.
[casparcg] / common / diagnostics / graph.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "graph.h"
25
26 #include <boost/range/adaptor/transformed.hpp>
27 #include <boost/thread.hpp>
28
29 #include <tbb/spin_mutex.h>
30
31 #include <vector>
32
33 namespace caspar { namespace diagnostics {
34                 
35 int color(float r, float g, float b, float a)
36 {
37         int code = 0;
38         code |= static_cast<int>(r*255.0f+0.5f) << 24;
39         code |= static_cast<int>(g*255.0f+0.5f) << 16;
40         code |= static_cast<int>(b*255.0f+0.5f) <<  8;
41         code |= static_cast<int>(a*255.0f+0.5f) <<  0;
42         return code;
43 }
44
45 std::tuple<float, float, float, float> color(int code)
46 {
47         float r = static_cast<float>((code >> 24) & 255)/255.0f;
48         float g = static_cast<float>((code >> 16) & 255)/255.0f;
49         float b = static_cast<float>((code >>  8) & 255)/255.0f;
50         float a = static_cast<float>((code >>  0) & 255)/255.0f;
51         return std::make_tuple(r, g, b, a);
52 }
53
54 typedef std::vector<spi::sink_factory_t> sink_factories_t;
55 static boost::mutex g_sink_factories_mutex;
56 static sink_factories_t g_sink_factories;
57
58 std::vector<spl::shared_ptr<spi::graph_sink>> create_sinks()
59 {
60         boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
61         auto sinks = g_sink_factories | boost::adaptors::transformed([](const spi::sink_factory_t& s){ return s(); });
62         return std::vector<spl::shared_ptr<spi::graph_sink>>(std::begin(sinks), std::end(sinks));
63 }
64
65 struct graph::impl
66 {
67         std::vector<spl::shared_ptr<spi::graph_sink>> sinks_ = create_sinks();
68 public:
69         impl()
70         {
71         }
72
73         void activate()
74         {
75                 for (auto& sink : sinks_)
76                         sink->activate();
77         }
78
79         void set_text(const std::wstring& value)
80         {
81                 for (auto& sink : sinks_)
82                         sink->set_text(value);
83         }
84
85         void set_value(const std::string& name, double value)
86         {
87                 for (auto& sink : sinks_)
88                         sink->set_value(name, value);
89         }
90
91         void set_tag(const std::string& name)
92         {
93                 for (auto& sink : sinks_)
94                         sink->set_tag(name);
95         }
96
97         void set_color(const std::string& name, int color)
98         {
99                 for (auto& sink : sinks_)
100                         sink->set_color(name, color);
101         }
102
103         void auto_reset()
104         {
105                 for (auto& sink : sinks_)
106                         sink->auto_reset();
107         }
108                 
109 private:
110         impl(impl&);
111         impl& operator=(impl&);
112 };
113         
114 graph::graph() : impl_(new impl)
115 {
116 }
117
118 void graph::set_text(const std::wstring& value) { impl_->set_text(value); }
119 void graph::set_value(const std::string& name, double value) { impl_->set_value(name, value); }
120 void graph::set_color(const std::string& name, int color) { impl_->set_color(name, color); }
121 void graph::set_tag(const std::string& name) { impl_->set_tag(name); }
122 void graph::auto_reset() { impl_->auto_reset(); }
123
124 void register_graph(const spl::shared_ptr<graph>& graph)
125 {
126         graph->impl_->activate();
127 }
128
129 namespace spi {
130
131 void register_sink_factory(sink_factory_t factory)
132 {
133         boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
134
135         g_sink_factories.push_back(std::move(factory));
136 }
137
138 }
139
140 }}