From: Joel Holdsworth Date: Sun, 2 Feb 2014 17:48:47 +0000 (+0000) Subject: Split annotation data set into rows, and improved painting. X-Git-Tag: pulseview-0.2.0~93 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=f9101a91fc942a28515872ae6c7285973bd54b91 Split annotation data set into rows, and improved painting. This has allowed the removal of Annotation::_row and Annotation::_pd_index --- diff --git a/CMakeLists.txt b/CMakeLists.txt index acec8976..d98ff54c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,8 @@ if(ENABLE_DECODE) pv/data/decoderstack.cpp pv/data/decode/annotation.cpp pv/data/decode/decoder.cpp + pv/data/decode/row.cpp + pv/data/decode/rowdata.cpp pv/prop/binding/decoderoptions.cpp pv/view/decodetrace.cpp pv/widgets/decodergroupbox.cpp diff --git a/pv/data/decode/annotation.cpp b/pv/data/decode/annotation.cpp index 4a6890b5..b0517ae6 100644 --- a/pv/data/decode/annotation.cpp +++ b/pv/data/decode/annotation.cpp @@ -40,7 +40,6 @@ Annotation::Annotation(const srd_proto_data *const pdata) : assert(pda); _format = pda->ann_format; - _row = 0; const char *const *annotations = (char**)pda->ann_text; while(*annotations) { @@ -64,31 +63,11 @@ int Annotation::format() const return _format; } -int Annotation::row() const -{ - return _row; -} - -int Annotation::pd_index() const -{ - return _pd_index; -} - const std::vector& Annotation::annotations() const { return _annotations; } -void Annotation::set_row(int row) -{ - _row = row; -} - -void Annotation::set_pd_index(int pd_index) -{ - _pd_index = pd_index; -} - } // namespace decode } // namespace data } // namespace pv diff --git a/pv/data/decode/annotation.h b/pv/data/decode/annotation.h index 64e6db57..6d0a19ae 100644 --- a/pv/data/decode/annotation.h +++ b/pv/data/decode/annotation.h @@ -39,8 +39,6 @@ public: uint64_t start_sample() const; uint64_t end_sample() const; int format() const; - int row() const; - int pd_index() const; const std::vector& annotations() const; void set_row(int row); @@ -50,8 +48,6 @@ private: uint64_t _start_sample; uint64_t _end_sample; int _format; - int _row; - int _pd_index; std::vector _annotations; }; diff --git a/pv/data/decode/row.cpp b/pv/data/decode/row.cpp new file mode 100644 index 00000000..0eee547c --- /dev/null +++ b/pv/data/decode/row.cpp @@ -0,0 +1,57 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "row.h" + +namespace pv { +namespace data { +namespace decode { + +Row::Row() : + _decoder(NULL), + _row(NULL) +{ +} + +Row::Row(const srd_decoder *decoder, const srd_decoder_annotation_row *row) : + _decoder(decoder), + _row(row) +{ +} + +const srd_decoder* Row::decoder() const +{ + return _decoder; +} + +const srd_decoder_annotation_row* Row::row() const +{ + return _row; +} + +bool Row::operator<(const Row &other) const +{ + return (_decoder < other._decoder) || + (_decoder == other._decoder && _row < other._row); +} + +} // decode +} // data +} // pv diff --git a/pv/data/decode/row.h b/pv/data/decode/row.h new file mode 100644 index 00000000..4ee05f6e --- /dev/null +++ b/pv/data/decode/row.h @@ -0,0 +1,57 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_DATA_DECODE_ROW_H +#define PULSEVIEW_PV_DATA_DECODE_ROW_H + +#include + +#include "annotation.h" + +struct srd_decoder; +struct srd_decoder_annotation_row; + +namespace pv { +namespace data { +namespace decode { + +class Row +{ +public: + Row(); + + Row(const srd_decoder *decoder, + const srd_decoder_annotation_row *row = NULL); + + const srd_decoder* decoder() const; + const srd_decoder_annotation_row* row() const; + + bool operator<(const Row &other) const; + +private: + const srd_decoder *_decoder; + const srd_decoder_annotation_row *_row; +}; + +} // decode +} // data +} // pv + +#endif // PULSEVIEW_PV_DATA_DECODE_ROW_H diff --git a/pv/data/decode/rowdata.cpp b/pv/data/decode/rowdata.cpp new file mode 100644 index 00000000..a06af616 --- /dev/null +++ b/pv/data/decode/rowdata.cpp @@ -0,0 +1,118 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include + +#include "rowdata.h" + +using std::vector; + +namespace pv { +namespace data { +namespace decode { + +RowData::RowData() +{ +} + +uint64_t RowData::get_max_sample() const +{ + if (_annotations.empty()) + return 0; + return _annotations.back().end_sample(); +} + +void RowData::get_annotation_subset( + std::vector &dest, + uint64_t start_sample, uint64_t end_sample) const +{ + const vector::const_iterator start_iter = + lower_bound(_ann_end_index.begin(), + _ann_end_index.end(), start_sample, + bind(&RowData::index_entry_end_sample_lt, + this, _1, _2)); + + const vector::const_iterator end_iter = + upper_bound(_ann_start_index.begin(), + _ann_start_index.end(), end_sample, + bind(&RowData::index_entry_start_sample_gt, + this, _1, _2)); + + for (vector::const_iterator i = start_iter; + i != _ann_end_index.end() && *i != *end_iter; i++) + dest.push_back(_annotations[*i]); +} + +void RowData::push_annotation(const Annotation &a) +{ + const size_t offset = _annotations.size(); + _annotations.push_back(a); + + // Insert the annotation into the start index + vector::iterator i = _ann_start_index.end(); + if (!_ann_start_index.empty() && + _annotations[_ann_start_index.back()].start_sample() > + a.start_sample()) + i = upper_bound(_ann_start_index.begin(), + _ann_start_index.end(), a.start_sample(), + bind(&RowData::index_entry_start_sample_gt, + this, _1, _2)); + + _ann_start_index.insert(i, offset); + + // Insert the annotation into the end index + vector::iterator j = _ann_end_index.end(); + if (!_ann_end_index.empty() && + _annotations[_ann_end_index.back()].end_sample() < + a.end_sample()) + j = upper_bound(_ann_end_index.begin(), + _ann_end_index.end(), a.end_sample(), + bind(&RowData::index_entry_end_sample_gt, + this, _1, _2)); + + _ann_end_index.insert(j, offset); +} + +bool RowData::index_entry_start_sample_gt( + const uint64_t sample, const size_t index) const +{ + assert(index < _annotations.size()); + return _annotations[index].start_sample() > sample; +} + +bool RowData::index_entry_end_sample_lt( + const size_t index, const uint64_t sample) const +{ + assert(index < _annotations.size()); + return _annotations[index].end_sample() < sample; +} + +bool RowData::index_entry_end_sample_gt( + const uint64_t sample, const size_t index) const +{ + assert(index < _annotations.size()); + return _annotations[index].end_sample() > sample; +} + +} // decode +} // data +} // pv diff --git a/pv/data/decode/rowdata.h b/pv/data/decode/rowdata.h new file mode 100644 index 00000000..3cd454e3 --- /dev/null +++ b/pv/data/decode/rowdata.h @@ -0,0 +1,72 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_DATA_DECODE_ROWDATA_H +#define PULSEVIEW_PV_DATA_DECODE_ROWDATA_H + +#include + +#include "annotation.h" + +namespace pv { +namespace data { +namespace decode { + +class RowData +{ +public: + RowData(); + +public: + uint64_t get_max_sample() const; + + /** + * Extracts sorted annotations between two period into a vector. + */ + void get_annotation_subset( + std::vector &dest, + uint64_t start_sample, uint64_t end_sample) const; + + void push_annotation(const Annotation& a); + +private: + bool index_entry_start_sample_gt( + const uint64_t sample, const size_t index) const; + bool index_entry_end_sample_lt( + const size_t index, const uint64_t sample) const; + bool index_entry_end_sample_gt( + const uint64_t sample, const size_t index) const; + +private: + std::vector _annotations; + + /** + * _ann_start_index and _ann_end_index contain lists of annotions + * (represented by offsets in the _annotations vector), sorted in + * ascending ordered by the start_sample and end_sample respectively. + */ + std::vector _ann_start_index, _ann_end_index; +}; + +} +} // data +} // pv + +#endif // PULSEVIEW_PV_DATA_DECODE_ROWDATA_H diff --git a/pv/data/decoderstack.cpp b/pv/data/decoderstack.cpp index 1c6b3c6b..323f5cc2 100644 --- a/pv/data/decoderstack.cpp +++ b/pv/data/decoderstack.cpp @@ -20,7 +20,6 @@ #include -#include #include #include @@ -40,10 +39,16 @@ using boost::lock_guard; using boost::mutex; using boost::shared_ptr; using std::deque; +using std::make_pair; +using std::max; using std::min; using std::list; +using std::map; +using std::pair; using std::vector; +using namespace pv::data::decode; + namespace pv { namespace data { @@ -80,8 +85,6 @@ void DecoderStack::push(boost::shared_ptr decoder) void DecoderStack::remove(int index) { - using pv::data::decode::Decoder; - assert(index >= 0); assert(index < (int)_stack.size()); @@ -100,27 +103,47 @@ int64_t DecoderStack::samples_decoded() const return _samples_decoded; } +std::vector DecoderStack::get_rows() const +{ + lock_guard lock(_mutex); + + vector rows; + + BOOST_FOREACH (const shared_ptr &dec, _stack) + { + assert(dec); + const srd_decoder *const decc = dec->decoder(); + assert(dec->decoder()); + + // Add a row for the decoder if it doesn't have a row list + if (!decc->annotation_rows) + rows.push_back(Row(decc)); + + // Add the decoder rows + for (const GSList *l = decc->annotation_rows; l; l = l->next) + { + const srd_decoder_annotation_row *const ann_row = + (srd_decoder_annotation_row *)l->data; + assert(ann_row); + rows.push_back(Row(decc, ann_row)); + } + } + + return rows; +} + void DecoderStack::get_annotation_subset( std::vector &dest, - uint64_t start_sample, uint64_t end_sample) const + const Row &row, uint64_t start_sample, + uint64_t end_sample) const { lock_guard lock(_mutex); - const vector::const_iterator start_iter = - lower_bound(_ann_end_index.begin(), - _ann_end_index.end(), start_sample, - bind(&DecoderStack::index_entry_end_sample_lt, - this, _1, _2)); - - const vector::const_iterator end_iter = - upper_bound(_ann_start_index.begin(), - _ann_start_index.end(), end_sample, - bind(&DecoderStack::index_entry_start_sample_gt, - this, _1, _2)); - - for (vector::const_iterator i = start_iter; - i != _ann_end_index.end() && *i != *end_iter; i++) - dest.push_back(_annotations[*i]); + std::map::const_iterator iter = + _rows.find(row); + if (iter != _rows.end()) + (*iter).second.get_annotation_subset(dest, + start_sample, end_sample); } QString DecoderStack::error_message() @@ -129,6 +152,14 @@ QString DecoderStack::error_message() return _error_message; } +void DecoderStack::clear() +{ + _samples_decoded = 0; + _error_message = QString(); + _rows.clear(); + _class_rows.clear(); +} + void DecoderStack::begin_decode() { shared_ptr logic_signal; @@ -137,10 +168,39 @@ void DecoderStack::begin_decode() _decode_thread.interrupt(); _decode_thread.join(); - _samples_decoded = 0; - clear(); + // Add classes + BOOST_FOREACH (const shared_ptr &dec, _stack) + { + assert(dec); + const srd_decoder *const decc = dec->decoder(); + assert(dec->decoder()); + + // Add a row for the decoder if it doesn't have a row list + if (!decc->annotation_rows) + _rows[Row(decc)] = decode::RowData(); + + // Add the decoder rows + for (const GSList *l = decc->annotation_rows; l; l = l->next) + { + const srd_decoder_annotation_row *const ann_row = + (srd_decoder_annotation_row *)l->data; + assert(ann_row); + + const Row row(decc, ann_row); + + // Add a new empty row data object + _rows[row] = decode::RowData(); + + // Map out all the classes + for (const GSList *ll = ann_row->ann_classes; + ll; ll = ll->next) + _class_rows[make_pair(decc, + GPOINTER_TO_INT(ll->data))] = row; + } + } + // We get the logic data of the first probe in the list. // This works because we are currently assuming all // LogicSignals have the same data/snapshot @@ -163,18 +223,16 @@ void DecoderStack::begin_decode() data); } -void DecoderStack::clear() -{ - _annotations.clear(); - _ann_start_index.clear(); - _ann_end_index.clear(); -} - uint64_t DecoderStack::get_max_sample_count() const { - if (_annotations.empty()) - return 0; - return _annotations.back().end_sample(); + uint64_t max_sample_count = 0; + + for (map::const_iterator i = _rows.begin(); + i != _rows.end(); i++) + max_sample_count = max(max_sample_count, + (*i).second.get_max_sample()); + + return max_sample_count; } void DecoderStack::decode_proc(shared_ptr data) @@ -196,9 +254,6 @@ void DecoderStack::decode_proc(shared_ptr data) const unsigned int chunk_sample_count = DecodeChunkLength / snapshot->unit_size(); - // Clear error message upon every new session run - _error_message = QString(); - // Create the session srd_session_new(&session); assert(session); @@ -257,100 +312,47 @@ void DecoderStack::decode_proc(shared_ptr data) srd_session_destroy(session); } -bool DecoderStack::index_entry_start_sample_gt( - const uint64_t sample, const size_t index) const -{ - assert(index < _annotations.size()); - return _annotations[index].start_sample() > sample; -} - -bool DecoderStack::index_entry_end_sample_lt( - const size_t index, const uint64_t sample) const -{ - assert(index < _annotations.size()); - return _annotations[index].end_sample() < sample; -} - -bool DecoderStack::index_entry_end_sample_gt( - const uint64_t sample, const size_t index) const -{ - assert(index < _annotations.size()); - return _annotations[index].end_sample() > sample; -} - -void DecoderStack::insert_annotation_into_start_index( - const pv::data::decode::Annotation &a, const size_t storage_offset) -{ - vector::iterator i = _ann_start_index.end(); - if (!_ann_start_index.empty() && - _annotations[_ann_start_index.back()].start_sample() > - a.start_sample()) - i = upper_bound(_ann_start_index.begin(), - _ann_start_index.end(), a.start_sample(), - bind(&DecoderStack::index_entry_start_sample_gt, - this, _1, _2)); - - _ann_start_index.insert(i, storage_offset); -} - -void DecoderStack::insert_annotation_into_end_index( - const pv::data::decode::Annotation &a, const size_t storage_offset) -{ - vector::iterator i = _ann_end_index.end(); - if (!_ann_end_index.empty() && - _annotations[_ann_end_index.back()].end_sample() < - a.end_sample()) - i = upper_bound(_ann_end_index.begin(), - _ann_end_index.end(), a.end_sample(), - bind(&DecoderStack::index_entry_end_sample_gt, - this, _1, _2)); - - _ann_end_index.insert(i, storage_offset); -} - void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) { - using pv::data::decode::Annotation; - - GSList *l, *ll; - int row, ann_class, idx = 0; - struct srd_decoder_annotation_row *ann_row; - struct srd_decoder *decc; - assert(pdata); assert(decoder); DecoderStack *const d = (DecoderStack*)decoder; + assert(d); lock_guard lock(d->_mutex); - Annotation a = Annotation(pdata); - - decc = pdata->pdo->di->decoder; - BOOST_FOREACH(const shared_ptr &dec, d->stack()) { - if (dec->decoder() == decc) - break; - idx++; - } - a.set_pd_index(idx); - - for (l = decc->annotation_rows, row = 0; l; l = l->next, row++) + const Annotation a(pdata); + + // Find the row + assert(pdata->pdo); + assert(pdata->pdo->di); + const srd_decoder *const decc = pdata->pdo->di->decoder; + assert(decc); + + map::iterator row_iter = d->_rows.end(); + + // Try looking up the sub-row of this class + const map, Row>::const_iterator r = + d->_class_rows.find(make_pair(decc, a.format())); + if (r != d->_class_rows.end()) + row_iter = d->_rows.find((*r).second); + else { - ann_row = (struct srd_decoder_annotation_row *)l->data; - - for (ll = ann_row->ann_classes, ann_class = 0; ll; - ll = ll->next, ann_class++) - { - if (GPOINTER_TO_INT(ll->data) == a.format()) - a.set_row(row); - } + // Failing that, use the decoder as a key + row_iter = d->_rows.find(Row(decc)); } - const size_t offset = d->_annotations.size(); - d->_annotations.push_back(a); + assert(row_iter != d->_rows.end()); + if (row_iter == d->_rows.end()) { + qDebug() << "Unexpected annotation: decoder = " << decc << + ", format = " << a.format(); + assert(0); + return; + } - d->insert_annotation_into_start_index(a, offset); - d->insert_annotation_into_end_index(a, offset); + // Add the annotation + (*row_iter).second.push_annotation(a); d->new_decode_data(); } diff --git a/pv/data/decoderstack.h b/pv/data/decoderstack.h index 9d032ad9..80ad64cd 100644 --- a/pv/data/decoderstack.h +++ b/pv/data/decoderstack.h @@ -31,7 +31,11 @@ #include #include +#include +#include + struct srd_decoder; +struct srd_decoder_annotation_row; struct srd_probe; struct srd_proto_data; @@ -74,12 +78,15 @@ public: int64_t samples_decoded() const; + std::vector get_rows() const; + /** * Extracts sorted annotations between two period into a vector. */ void get_annotation_subset( std::vector &dest, - uint64_t start_sample, uint64_t end_sample) const; + const decode::Row &row, uint64_t start_sample, + uint64_t end_sample) const; QString error_message(); @@ -92,20 +99,6 @@ public: private: void decode_proc(boost::shared_ptr data); - bool index_entry_start_sample_gt( - const uint64_t sample, const size_t index) const; - bool index_entry_end_sample_lt( - const size_t index, const uint64_t sample) const; - bool index_entry_end_sample_gt( - const uint64_t sample, const size_t index) const; - - void insert_annotation_into_start_index( - const pv::data::decode::Annotation &a, - const size_t storage_offset); - void insert_annotation_into_end_index( - const pv::data::decode::Annotation &a, - const size_t storage_offset); - static void annotation_callback(srd_proto_data *pdata, void *decoder); @@ -126,14 +119,10 @@ private: mutable boost::mutex _mutex; int64_t _samples_decoded; - std::vector _annotations; - /** - * _ann_start_index and _ann_end_index contain lists of annotions - * (represented by offsets in the _annotations vector), sorted in - * ascending ordered by the start_sample and end_sample respectively. - */ - std::vector _ann_start_index, _ann_end_index; + std::map _rows; + + std::map, decode::Row> _class_rows; QString _error_message; diff --git a/pv/view/decodetrace.cpp b/pv/view/decodetrace.cpp index ce62ab6f..161eb966 100644 --- a/pv/view/decodetrace.cpp +++ b/pv/view/decodetrace.cpp @@ -122,7 +122,7 @@ void DecodeTrace::paint_back(QPainter &p, int left, int right) void DecodeTrace::paint_mid(QPainter &p, int left, int right) { - using pv::data::decode::Annotation; + using namespace pv::data::decode; const double scale = _view->scale(); assert(scale > 0); @@ -143,40 +143,43 @@ void DecodeTrace::paint_mid(QPainter &p, int left, int right) samples_per_pixel, 0.0); QFontMetrics m(QApplication::font()); - const int h = (m.boundingRect(QRect(), 0, "Tg").height() * 5) / 4; + const int text_height = m.boundingRect(QRect(), 0, "Tg").height(); + const int annotation_height = (text_height * 5) / 4; + const int row_height = (text_height * 6) / 4; assert(_decoder_stack); const QString err = _decoder_stack->error_message(); if (!err.isEmpty()) { draw_error(p, err, left, right); - draw_unresolved_period(p, h, left, right, samples_per_pixel, - pixels_offset); + draw_unresolved_period(p, annotation_height, left, right, + samples_per_pixel, pixels_offset); return; } + // Iterate through the rows assert(_view); - const int y = get_y(); + int y = get_y(); assert(_decoder_stack); - vector annotations; - _decoder_stack->get_annotation_subset(annotations, - start_sample, end_sample); - BOOST_FOREACH(const Annotation &a, annotations) + const vector rows(_decoder_stack->get_rows()); + BOOST_FOREACH (const Row &row, rows) { - // Every stacked PD is 60 pixels further down. - int y_stack_offset = a.pd_index() * 60; - - // Every annotation row is 20 pixels further down. - int y_ann_row_offset = a.row() * 20; - - draw_annotation(a, p, get_text_colour(), h, left, right, - samples_per_pixel, pixels_offset, - y + y_stack_offset + y_ann_row_offset); + vector annotations; + _decoder_stack->get_annotation_subset(annotations, row, + start_sample, end_sample); + if (!annotations.empty()) { + BOOST_FOREACH(const Annotation &a, annotations) + draw_annotation(a, p, get_text_colour(), + annotation_height, left, right, + samples_per_pixel, pixels_offset, y); + y += row_height; + } } - draw_unresolved_period(p, h, left, right, + // Draw the hatching + draw_unresolved_period(p, annotation_height, left, right, samples_per_pixel, pixels_offset); }