SignalBase: Make sure an invalid color isn't shown as "white"
[pulseview.git] / pv / data / logicsegment.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
21
22 #include <extdef.h>
23
24 #include <cassert>
25 #include <cmath>
26 #include <cstdlib>
27 #include <cstring>
28 #include <cstdint>
29
30 #include "logic.hpp"
31 #include "logicsegment.hpp"
32
33 #include <libsigrokcxx/libsigrokcxx.hpp>
34
35 using std::lock_guard;
36 using std::recursive_mutex;
37 using std::max;
38 using std::min;
39 using std::shared_ptr;
40 using std::vector;
41
42 using sigrok::Logic;
43
44 namespace pv {
45 namespace data {
46
47 const int LogicSegment::MipMapScalePower = 4;
48 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
49 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
50 const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes
51
52 LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
53         unsigned int unit_size, uint64_t samplerate) :
54         Segment(segment_id, samplerate, unit_size),
55         owner_(owner),
56         last_append_sample_(0),
57         last_append_accumulator_(0),
58         last_append_extra_(0)
59 {
60         memset(mip_map_, 0, sizeof(mip_map_));
61 }
62
63 LogicSegment::~LogicSegment()
64 {
65         lock_guard<recursive_mutex> lock(mutex_);
66         for (MipMapLevel &l : mip_map_)
67                 free(l.data);
68 }
69
70 template <class T>
71 void LogicSegment::downsampleTmain(const T*&in, T &acc, T &prev)
72 {
73         // Accumulate one sample at a time
74         for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
75                 T sample = *in++;
76                 acc |= prev ^ sample;
77                 prev = sample;
78         }
79 }
80
81 template <>
82 void LogicSegment::downsampleTmain<uint8_t>(const uint8_t*&in, uint8_t &acc, uint8_t &prev)
83 {
84         // Handle 8 bit samples in 32 bit steps
85         uint32_t prev32 = prev | prev << 8 | prev << 16 | prev << 24;
86         uint32_t acc32 = acc;
87         const uint32_t *in32 = (const uint32_t*)in;
88         for (uint64_t i = 0; i < MipMapScaleFactor; i += 4) {
89                 uint32_t sample32 = *in32++;
90                 acc32 |= prev32 ^ sample32;
91                 prev32 = sample32;
92         }
93         // Reduce result back to uint8_t
94 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
95         prev = (prev32 >> 24) & 0xff; // MSB is last
96 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
97         prev = prev32 & 0xff; // LSB is last
98 #else
99 #error Endianness unknown
100 #endif
101         acc |= acc32 & 0xff;
102         acc |= (acc32 >> 8) & 0xff;
103         acc |= (acc32 >> 16) & 0xff;
104         acc |= (acc32 >> 24) & 0xff;
105         in = (const uint8_t*)in32;
106 }
107
108 template <>
109 void LogicSegment::downsampleTmain<uint16_t>(const uint16_t*&in, uint16_t &acc, uint16_t &prev)
110 {
111         // Handle 16 bit samples in 32 bit steps
112         uint32_t prev32 = prev | prev << 16;
113         uint32_t acc32 = acc;
114         const uint32_t *in32 = (const uint32_t*)in;
115         for (uint64_t i = 0; i < MipMapScaleFactor; i += 2) {
116                 uint32_t sample32 = *in32++;
117                 acc32 |= prev32 ^ sample32;
118                 prev32 = sample32;
119         }
120         // Reduce result back to uint16_t
121 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
122         prev = (prev32 >> 16) & 0xffff; // MSB is last
123 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
124         prev = prev32 & 0xffff; // LSB is last
125 #else
126 #error Endian unknown
127 #endif
128         acc |= acc32 & 0xffff;
129         acc |= (acc32 >> 16) & 0xffff;
130         in = (const uint16_t*)in32;
131 }
132
133 template <class T>
134 void LogicSegment::downsampleT(const uint8_t *in_, uint8_t *&out_, uint64_t len)
135 {
136         const T *in = (const T*)in_;
137         T *out = (T*)out_;
138         T prev = last_append_sample_;
139         T acc = last_append_accumulator_;
140
141         // Try to complete the previous downsample
142         if (last_append_extra_) {
143                 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
144                         T sample = *in++;
145                         acc |= prev ^ sample;
146                         prev = sample;
147                         last_append_extra_++;
148                         len--;
149                 }
150                 if (!len) {
151                         // Not enough samples available to complete downsample
152                         last_append_sample_ = prev;
153                         last_append_accumulator_ = acc;
154                         return;
155                 }
156                 // We have a complete downsample
157                 *out++ = acc;
158                 acc = 0;
159                 last_append_extra_ = 0;
160         }
161
162         // Handle complete blocks of MipMapScaleFactor samples
163         while (len >= MipMapScaleFactor) {
164                 downsampleTmain<T>(in, acc, prev);
165                 len -= MipMapScaleFactor;
166                 // Output downsample
167                 *out++ = acc;
168                 acc = 0;
169         }
170
171         // Process remainder, not enough for a complete sample
172         while (len > 0) {
173                 T sample = *in++;
174                 acc |= prev ^ sample;
175                 prev = sample;
176                 last_append_extra_++;
177                 len--;
178         }
179
180         // Update context
181         last_append_sample_ = prev;
182         last_append_accumulator_ = acc;
183         out_ = (uint8_t *)out;
184 }
185
186 void LogicSegment::downsampleGeneric(const uint8_t *in, uint8_t *&out, uint64_t len)
187 {
188         // Downsample using the generic unpack_sample()
189         // which can handle any width between 1 and 8 bytes
190         uint64_t prev = last_append_sample_;
191         uint64_t acc = last_append_accumulator_;
192
193         // Try to complete the previous downsample
194         if (last_append_extra_) {
195                 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
196                         const uint64_t sample = unpack_sample(in);
197                         in += unit_size_;
198                         acc |= prev ^ sample;
199                         prev = sample;
200                         last_append_extra_++;
201                         len--;
202                 }
203                 if (!len) {
204                         // Not enough samples available to complete downsample
205                         last_append_sample_ = prev;
206                         last_append_accumulator_ = acc;
207                         return;
208                 }
209                 // We have a complete downsample
210                 pack_sample(out, acc);
211                 out += unit_size_;
212                 acc = 0;
213                 last_append_extra_ = 0;
214         }
215
216         // Handle complete blocks of MipMapScaleFactor samples
217         while (len >= MipMapScaleFactor) {
218                 // Accumulate one sample at a time
219                 for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
220                         const uint64_t sample = unpack_sample(in);
221                         in += unit_size_;
222                         acc |= prev ^ sample;
223                         prev = sample;
224                 }
225                 len -= MipMapScaleFactor;
226                 // Output downsample
227                 pack_sample(out, acc);
228                 out += unit_size_;
229                 acc = 0;
230         }
231
232         // Process remainder, not enough for a complete sample
233         while (len > 0) {
234                 const uint64_t sample = unpack_sample(in);
235                 in += unit_size_;
236                 acc |= prev ^ sample;
237                 prev = sample;
238                 last_append_extra_++;
239                 len--;
240         }
241
242         // Update context
243         last_append_sample_ = prev;
244         last_append_accumulator_ = acc;
245 }
246
247 inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
248 {
249 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
250         return *(uint64_t*)ptr;
251 #else
252         uint64_t value = 0;
253         switch (unit_size_) {
254         default:
255                 value |= ((uint64_t)ptr[7]) << 56;
256                 /* FALLTHRU */
257         case 7:
258                 value |= ((uint64_t)ptr[6]) << 48;
259                 /* FALLTHRU */
260         case 6:
261                 value |= ((uint64_t)ptr[5]) << 40;
262                 /* FALLTHRU */
263         case 5:
264                 value |= ((uint64_t)ptr[4]) << 32;
265                 /* FALLTHRU */
266         case 4:
267                 value |= ((uint32_t)ptr[3]) << 24;
268                 /* FALLTHRU */
269         case 3:
270                 value |= ((uint32_t)ptr[2]) << 16;
271                 /* FALLTHRU */
272         case 2:
273                 value |= ptr[1] << 8;
274                 /* FALLTHRU */
275         case 1:
276                 value |= ptr[0];
277                 /* FALLTHRU */
278         case 0:
279                 break;
280         }
281         return value;
282 #endif
283 }
284
285 inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
286 {
287 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
288         *(uint64_t*)ptr = value;
289 #else
290         switch (unit_size_) {
291         default:
292                 ptr[7] = value >> 56;
293                 /* FALLTHRU */
294         case 7:
295                 ptr[6] = value >> 48;
296                 /* FALLTHRU */
297         case 6:
298                 ptr[5] = value >> 40;
299                 /* FALLTHRU */
300         case 5:
301                 ptr[4] = value >> 32;
302                 /* FALLTHRU */
303         case 4:
304                 ptr[3] = value >> 24;
305                 /* FALLTHRU */
306         case 3:
307                 ptr[2] = value >> 16;
308                 /* FALLTHRU */
309         case 2:
310                 ptr[1] = value >> 8;
311                 /* FALLTHRU */
312         case 1:
313                 ptr[0] = value;
314                 /* FALLTHRU */
315         case 0:
316                 break;
317         }
318 #endif
319 }
320
321 void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
322 {
323         assert(unit_size_ == logic->unit_size());
324         assert((logic->data_length() % unit_size_) == 0);
325
326         append_payload(logic->data_pointer(), logic->data_length());
327 }
328
329 void LogicSegment::append_payload(void *data, uint64_t data_size)
330 {
331         assert((data_size % unit_size_) == 0);
332
333         lock_guard<recursive_mutex> lock(mutex_);
334
335         const uint64_t prev_sample_count = sample_count_;
336         const uint64_t sample_count = data_size / unit_size_;
337
338         append_samples(data, sample_count);
339
340         // Generate the first mip-map from the data
341         append_payload_to_mipmap();
342
343         if (sample_count > 1)
344                 owner_.notify_samples_added(this, prev_sample_count + 1,
345                         prev_sample_count + 1 + sample_count);
346         else
347                 owner_.notify_samples_added(this, prev_sample_count + 1,
348                         prev_sample_count + 1);
349 }
350
351 void LogicSegment::get_samples(int64_t start_sample,
352         int64_t end_sample,     uint8_t* dest) const
353 {
354         assert(start_sample >= 0);
355         assert(start_sample <= (int64_t)sample_count_);
356         assert(end_sample >= 0);
357         assert(end_sample <= (int64_t)sample_count_);
358         assert(start_sample <= end_sample);
359         assert(dest != nullptr);
360
361         lock_guard<recursive_mutex> lock(mutex_);
362
363         get_raw_samples(start_sample, (end_sample - start_sample), dest);
364 }
365
366 void LogicSegment::get_subsampled_edges(
367         vector<EdgePair> &edges,
368         uint64_t start, uint64_t end,
369         float min_length, int sig_index, bool first_change_only)
370 {
371         uint64_t index = start;
372         unsigned int level;
373         bool last_sample;
374         bool fast_forward;
375
376         assert(start <= end);
377         assert(min_length > 0);
378         assert(sig_index >= 0);
379         assert(sig_index < 64);
380
381         lock_guard<recursive_mutex> lock(mutex_);
382
383         // Make sure we only process as many samples as we have
384         if (end > get_sample_count())
385                 end = get_sample_count();
386
387         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
388         const unsigned int min_level = max((int)floorf(logf(min_length) /
389                 LogMipMapScaleFactor) - 1, 0);
390         const uint64_t sig_mask = 1ULL << sig_index;
391
392         // Store the initial state
393         last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
394         if (!first_change_only)
395                 edges.emplace_back(index++, last_sample);
396
397         while (index + block_length <= end) {
398                 //----- Continue to search -----//
399                 level = min_level;
400
401                 // We cannot fast-forward if there is no mip-map data at
402                 // the minimum level.
403                 fast_forward = (mip_map_[level].data != nullptr);
404
405                 if (min_length < MipMapScaleFactor) {
406                         // Search individual samples up to the beginning of
407                         // the next first level mip map block
408                         const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
409
410                         for (; index < final_index &&
411                                         (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
412                                         index++) {
413
414                                 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
415
416                                 // If there was a change we cannot fast forward
417                                 if (sample != last_sample) {
418                                         fast_forward = false;
419                                         break;
420                                 }
421                         }
422                 } else {
423                         // If resolution is less than a mip map block,
424                         // round up to the beginning of the mip-map block
425                         // for this level of detail
426                         const int min_level_scale_power = (level + 1) * MipMapScalePower;
427                         index = pow2_ceil(index, min_level_scale_power);
428                         if (index >= end)
429                                 break;
430
431                         // We can fast forward only if there was no change
432                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
433                         if (last_sample != sample)
434                                 fast_forward = false;
435                 }
436
437                 if (fast_forward) {
438
439                         // Fast forward: This involves zooming out to higher
440                         // levels of the mip map searching for changes, then
441                         // zooming in on them to find the point where the edge
442                         // begins.
443
444                         // Slide right and zoom out at the beginnings of mip-map
445                         // blocks until we encounter a change
446                         while (true) {
447                                 const int level_scale_power = (level + 1) * MipMapScalePower;
448                                 const uint64_t offset = index >> level_scale_power;
449
450                                 // Check if we reached the last block at this
451                                 // level, or if there was a change in this block
452                                 if (offset >= mip_map_[level].length ||
453                                         (get_subsample(level, offset) & sig_mask))
454                                         break;
455
456                                 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
457                                         // If we are now at the beginning of a
458                                         // higher level mip-map block ascend one
459                                         // level
460                                         if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
461                                                 break;
462
463                                         level++;
464                                 } else {
465                                         // Slide right to the beginning of the
466                                         // next mip map block
467                                         index = pow2_ceil(index + 1, level_scale_power);
468                                 }
469                         }
470
471                         // Zoom in, and slide right until we encounter a change,
472                         // and repeat until we reach min_level
473                         while (true) {
474                                 assert(mip_map_[level].data);
475
476                                 const int level_scale_power = (level + 1) * MipMapScalePower;
477                                 const uint64_t offset = index >> level_scale_power;
478
479                                 // Check if we reached the last block at this
480                                 // level, or if there was a change in this block
481                                 if (offset >= mip_map_[level].length ||
482                                                 (get_subsample(level, offset) & sig_mask)) {
483                                         // Zoom in unless we reached the minimum
484                                         // zoom
485                                         if (level == min_level)
486                                                 break;
487
488                                         level--;
489                                 } else {
490                                         // Slide right to the beginning of the
491                                         // next mip map block
492                                         index = pow2_ceil(index + 1, level_scale_power);
493                                 }
494                         }
495
496                         // If individual samples within the limit of resolution,
497                         // do a linear search for the next transition within the
498                         // block
499                         if (min_length < MipMapScaleFactor) {
500                                 for (; index < end; index++) {
501                                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
502                                         if (sample != last_sample)
503                                                 break;
504                                 }
505                         }
506                 }
507
508                 //----- Store the edge -----//
509
510                 // Take the last sample of the quanization block
511                 const int64_t final_index = index + block_length;
512                 if (index + block_length > end)
513                         break;
514
515                 // Store the final state
516                 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
517                 edges.emplace_back(index, final_sample);
518
519                 index = final_index;
520                 last_sample = final_sample;
521
522                 if (first_change_only)
523                         break;
524         }
525
526         // Add the final state
527         if (!first_change_only) {
528                 const bool end_sample = get_unpacked_sample(end) & sig_mask;
529                 if (last_sample != end_sample)
530                         edges.emplace_back(end, end_sample);
531                 edges.emplace_back(end + 1, end_sample);
532         }
533 }
534
535 void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
536         uint64_t origin_sample, float min_length, int sig_index)
537 {
538         if (origin_sample >= sample_count_)
539                 return;
540
541         // Put the edges vector on the heap, it can become quite big until we can
542         // use a get_subsampled_edges() implementation that searches backwards
543         vector<EdgePair>* edges = new vector<EdgePair>;
544
545         // Get all edges to the left of origin_sample
546         get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
547
548         // If we don't specify "first only", the first and last edge are the states
549         // at samples 0 and origin_sample. If only those exist, there are no edges
550         if (edges->size() == 2) {
551                 delete edges;
552                 return;
553         }
554
555         // Dismiss the entry for origin_sample so that back() gives us the
556         // real last entry
557         edges->pop_back();
558         dest.push_back(edges->back());
559         edges->clear();
560
561         // Get first edge to the right of origin_sample
562         get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
563
564         // "first only" is specified, so nothing needs to be dismissed
565         if (edges->size() == 0) {
566                 delete edges;
567                 return;
568         }
569
570         dest.push_back(edges->front());
571
572         delete edges;
573 }
574
575 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
576 {
577         lock_guard<recursive_mutex> lock(mutex_);
578
579         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
580                 MipMapDataUnit) * MipMapDataUnit;
581
582         if (new_data_length > m.data_length) {
583                 m.data_length = new_data_length;
584
585                 // Padding is added to allow for the uint64_t write word
586                 m.data = realloc(m.data, new_data_length * unit_size_ +
587                         sizeof(uint64_t));
588         }
589 }
590
591 void LogicSegment::append_payload_to_mipmap()
592 {
593         MipMapLevel &m0 = mip_map_[0];
594         uint64_t prev_length;
595         uint8_t *dest_ptr;
596         SegmentDataIterator* it;
597         uint64_t accumulator;
598         unsigned int diff_counter;
599
600         // Expand the data buffer to fit the new samples
601         prev_length = m0.length;
602         m0.length = sample_count_ / MipMapScaleFactor;
603
604         // Break off if there are no new samples to compute
605         if (m0.length == prev_length)
606                 return;
607
608         reallocate_mipmap_level(m0);
609
610         dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
611
612         // Iterate through the samples to populate the first level mipmap
613         const uint64_t start_sample = prev_length * MipMapScaleFactor;
614         const uint64_t end_sample = m0.length * MipMapScaleFactor;
615         uint64_t len_sample = end_sample - start_sample;
616         it = begin_sample_iteration(start_sample);
617         while (len_sample > 0) {
618                 // Number of samples available in this chunk
619                 uint64_t count = get_iterator_valid_length(it);
620                 // Reduce if less than asked for
621                 count = std::min(count, len_sample);
622                 uint8_t *src_ptr = get_iterator_value(it);
623                 // Submit these contiguous samples to downsampling in bulk
624                 if (unit_size_ == 1)
625                         downsampleT<uint8_t>(src_ptr, dest_ptr, count);
626                 else if (unit_size_ == 2)
627                         downsampleT<uint16_t>(src_ptr, dest_ptr, count);
628                 else if (unit_size_ == 4)
629                         downsampleT<uint32_t>(src_ptr, dest_ptr, count);
630                 else if (unit_size_ == 8)
631                         downsampleT<uint8_t>(src_ptr, dest_ptr, count);
632                 else
633                         downsampleGeneric(src_ptr, dest_ptr, count);
634                 len_sample -= count;
635                 // Advance iterator, should move to start of next chunk
636                 continue_sample_iteration(it, count);
637         }
638         end_sample_iteration(it);
639
640         // Compute higher level mipmaps
641         for (unsigned int level = 1; level < ScaleStepCount; level++) {
642                 MipMapLevel &m = mip_map_[level];
643                 const MipMapLevel &ml = mip_map_[level - 1];
644
645                 // Expand the data buffer to fit the new samples
646                 prev_length = m.length;
647                 m.length = ml.length / MipMapScaleFactor;
648
649                 // Break off if there are no more samples to be computed
650                 if (m.length == prev_length)
651                         break;
652
653                 reallocate_mipmap_level(m);
654
655                 // Subsample the lower level
656                 const uint8_t* src_ptr = (uint8_t*)ml.data +
657                         unit_size_ * prev_length * MipMapScaleFactor;
658                 const uint8_t *const end_dest_ptr =
659                         (uint8_t*)m.data + unit_size_ * m.length;
660
661                 for (dest_ptr = (uint8_t*)m.data +
662                                 unit_size_ * prev_length;
663                                 dest_ptr < end_dest_ptr;
664                                 dest_ptr += unit_size_) {
665                         accumulator = 0;
666                         diff_counter = MipMapScaleFactor;
667                         while (diff_counter-- > 0) {
668                                 accumulator |= unpack_sample(src_ptr);
669                                 src_ptr += unit_size_;
670                         }
671
672                         pack_sample(dest_ptr, accumulator);
673                 }
674         }
675 }
676
677 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
678 {
679         assert(index < sample_count_);
680
681         assert(unit_size_ <= 8);  // 8 * 8 = 64 channels
682         uint8_t data[8];
683
684         get_raw_samples(index, 1, data);
685
686         return unpack_sample(data);
687 }
688
689 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
690 {
691         assert(level >= 0);
692         assert(mip_map_[level].data);
693         return unpack_sample((uint8_t*)mip_map_[level].data +
694                 unit_size_ * offset);
695 }
696
697 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
698 {
699         const uint64_t p = UINT64_C(1) << power;
700         return (x + p - 1) / p * p;
701 }
702
703 } // namespace data
704 } // namespace pv