X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=logicsignal.cpp;h=67d72bef8b7c741190cad954f84fddd9f42103ed;hp=48a9570157c083b2c928b6a30b7abfffdf1c4d6e;hb=e5335d4f0973a90ef7f7f69095e9db36e3feb0cb;hpb=28a4c9c5eb20296199fc3496bb40b7733dffac75 diff --git a/logicsignal.cpp b/logicsignal.cpp index 48a95701..67d72bef 100644 --- a/logicsignal.cpp +++ b/logicsignal.cpp @@ -18,12 +18,151 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#define GL_GLEXT_PROTOTYPES +#include +#include + +#include + +#include "logicdata.h" +#include "logicdatasnapshot.h" #include "logicsignal.h" -LogicSignal::LogicSignal(QString name, boost::shared_ptr data, +using namespace boost; +using namespace std; + +const float Log2 = logf(2.0f); + +const float LogicSignal::Margin = 10.0f; + +const float LogicSignal::EdgeColour[3] = {0.50f, 0.50f, 0.50f}; +const float LogicSignal::HighColour[3] = {0.00f, 0.75f, 0.00f}; +const float LogicSignal::LowColour[3] = {0.75f, 0.00f, 0.00f}; + +LogicSignal::LogicSignal(QString name, shared_ptr data, int probe_index) : - Signal(name, data), + Signal(name), + _data(data), _probe_index(probe_index) { assert(_probe_index >= 0); } + +void LogicSignal::paint(QGLWidget &widget, const QRect &rect, + double scale, double offset) +{ + Point2F *vertex; + + vector< pair > edges; + + assert(scale > 0); + assert(_data); + + const float high_offset = rect.top() + Margin; + const float low_offset = rect.bottom() - Margin; + + const queue< shared_ptr > &snapshots = + _data->get_snapshots(); + if(snapshots.empty()) + return; + + const shared_ptr &snapshot = snapshots.front(); + + const double pixels_offset = offset / scale; + const double samplerate = _data->get_samplerate(); + const double start_time = _data->get_start_time(); + const int64_t last_sample = (int64_t)snapshot->get_sample_count() - 1; + const double samples_per_pixel = samplerate * scale; + const int64_t start = min(max((int64_t)(samplerate * (offset - start_time)), + (int64_t)0), last_sample); + const int64_t end = min((int64_t)(start + samples_per_pixel * rect.width()), + last_sample); + const int64_t quantization_length = 1LL << (int64_t)floorf( + max(logf(samples_per_pixel / Log2), 0.0f)); + + snapshot->get_subsampled_edges(edges, start, end, + quantization_length, _probe_index); + + // Paint the edges + const unsigned int edge_point_count = (edges.size() - 2) * 2; + Point2F *const edge_points = new Point2F[edge_point_count]; + vertex = edge_points; + + for(vector::const_iterator i = edges.begin() + 1; + i != edges.end() - 1; i++) + { + const int x = (int)((*i).first / samples_per_pixel - pixels_offset) + + rect.left(); + + vertex->x = x, vertex->y = high_offset; + vertex++; + vertex->x = x, vertex->y = low_offset; + vertex++; + } + + glColor3fv(EdgeColour); + paint_lines(edge_points, edge_point_count); + delete[] edge_points; + + // Paint the caps + const unsigned int max_cap_point_count = (edges.size() - 1) * 2; + Point2F *const cap_points = new Point2F[max_cap_point_count]; + + glColor3fv(HighColour); + paint_caps(cap_points, edges, true, samples_per_pixel, + pixels_offset, rect.left(), high_offset); + glColor3fv(LowColour); + paint_caps(cap_points, edges, false, samples_per_pixel, + pixels_offset, rect.left(), low_offset); + + delete[] cap_points; +} + +int LogicSignal::paint_caps(Point2F *const cap_points, + vector< pair > &edges, bool level, + double samples_per_pixel, double pixels_offset, int x_offset, + int y_offset) +{ + Point2F *vertex = cap_points; + + for(vector::const_iterator i = edges.begin(); + i != (edges.end() - 1); i++) + if((*i).second == level) + { + vertex->x = (int)((*i).first / samples_per_pixel - + pixels_offset) + x_offset - 1; + vertex->y = y_offset; + vertex++; + + vertex->x = (int)((*(i+1)).first / samples_per_pixel - + pixels_offset) + x_offset; + vertex->y = y_offset; + vertex++; + } + + paint_lines(cap_points, vertex - cap_points); +} + +void LogicSignal::paint_lines(Point2F *points, int count) +{ + GLuint vbo_id; + + assert(points); + + glGenBuffers(1, &vbo_id); + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + + const unsigned int vbo_length = count * sizeof(Point2F); + glBufferData(GL_ARRAY_BUFFER, vbo_length, NULL, GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_length, points); + + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + + glVertexPointer(2, GL_FLOAT, sizeof(Point2F), 0); + + glEnableClientState(GL_VERTEX_ARRAY); + glDrawArrays(GL_LINES, 0, count); + glDisableClientState(GL_VERTEX_ARRAY); + + glDeleteBuffers(1, &vbo_id); +}