Fix some disappearing annotations
[pulseview.git] / pv / data / logicsnapshot.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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <extdef.h>
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 #include <boost/foreach.hpp>
29
30 #include "logicsnapshot.h"
31
32 using boost::lock_guard;
33 using boost::recursive_mutex;
34 using std::max;
35 using std::min;
36 using std::pair;
37
38 namespace pv {
39 namespace data {
40
41 const int LogicSnapshot::MipMapScalePower = 4;
42 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
43 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
44 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
45
46 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic) :
47         Snapshot(logic.unitsize),
48         _last_append_sample(0)
49 {
50         lock_guard<recursive_mutex> lock(_mutex);
51         memset(_mip_map, 0, sizeof(_mip_map));
52         append_payload(logic);
53 }
54
55 LogicSnapshot::~LogicSnapshot()
56 {
57         lock_guard<recursive_mutex> lock(_mutex);
58         BOOST_FOREACH(MipMapLevel &l, _mip_map)
59                 free(l.data);
60 }
61
62 void LogicSnapshot::append_payload(
63         const sr_datafeed_logic &logic)
64 {
65         assert(_unit_size == logic.unitsize);
66         assert((logic.length % _unit_size) == 0);
67
68         lock_guard<recursive_mutex> lock(_mutex);
69
70         append_data(logic.data, logic.length / _unit_size);
71
72         // Generate the first mip-map from the data
73         append_payload_to_mipmap();
74 }
75
76 void LogicSnapshot::get_samples(uint8_t *const data,
77         int64_t start_sample, int64_t end_sample) const
78 {
79         assert(data);
80         assert(start_sample >= 0);
81         assert(start_sample <= (int64_t)_sample_count);
82         assert(end_sample >= 0);
83         assert(end_sample <= (int64_t)_sample_count);
84         assert(start_sample <= end_sample);
85
86         lock_guard<recursive_mutex> lock(_mutex);
87
88         const size_t size = (end_sample - start_sample) * _unit_size;
89         memcpy(data, (const uint8_t*)_data + start_sample, size);
90 }
91
92 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
93 {
94         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
95                 MipMapDataUnit) * MipMapDataUnit;
96         if (new_data_length > m.data_length)
97         {
98                 m.data_length = new_data_length;
99
100                 // Padding is added to allow for the uint64_t write word
101                 m.data = realloc(m.data, new_data_length * _unit_size +
102                         sizeof(uint64_t));
103         }
104 }
105
106 void LogicSnapshot::append_payload_to_mipmap()
107 {
108         MipMapLevel &m0 = _mip_map[0];
109         uint64_t prev_length;
110         const uint8_t *src_ptr;
111         uint8_t *dest_ptr;
112         uint64_t accumulator;
113         unsigned int diff_counter;
114
115         // Expand the data buffer to fit the new samples
116         prev_length = m0.length;
117         m0.length = _sample_count / MipMapScaleFactor;
118
119         // Break off if there are no new samples to compute
120         if (m0.length == prev_length)
121                 return;
122
123         reallocate_mipmap_level(m0);
124
125         dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
126
127         // Iterate through the samples to populate the first level mipmap
128         const uint8_t *const end_src_ptr = (uint8_t*)_data +
129                 m0.length * _unit_size * MipMapScaleFactor;
130         for (src_ptr = (uint8_t*)_data +
131                 prev_length * _unit_size * MipMapScaleFactor;
132                 src_ptr < end_src_ptr;)
133         {
134                 // Accumulate transitions which have occurred in this sample
135                 accumulator = 0;
136                 diff_counter = MipMapScaleFactor;
137                 while (diff_counter-- > 0)
138                 {
139                         const uint64_t sample = *(uint64_t*)src_ptr;
140                         accumulator |= _last_append_sample ^ sample;
141                         _last_append_sample = sample;
142                         src_ptr += _unit_size;
143                 }
144
145                 *(uint64_t*)dest_ptr = accumulator;
146                 dest_ptr += _unit_size;
147         }
148
149         // Compute higher level mipmaps
150         for (unsigned int level = 1; level < ScaleStepCount; level++)
151         {
152                 MipMapLevel &m = _mip_map[level];
153                 const MipMapLevel &ml = _mip_map[level-1];
154
155                 // Expand the data buffer to fit the new samples
156                 prev_length = m.length;
157                 m.length = ml.length / MipMapScaleFactor;
158
159                 // Break off if there are no more samples to computed
160                 if (m.length == prev_length)
161                         break;
162
163                 reallocate_mipmap_level(m);
164
165                 // Subsample the level lower level
166                 src_ptr = (uint8_t*)ml.data +
167                         _unit_size * prev_length * MipMapScaleFactor;
168                 const uint8_t *const end_dest_ptr =
169                         (uint8_t*)m.data + _unit_size * m.length;
170                 for (dest_ptr = (uint8_t*)m.data +
171                         _unit_size * prev_length;
172                         dest_ptr < end_dest_ptr;
173                         dest_ptr += _unit_size)
174                 {
175                         accumulator = 0;
176                         diff_counter = MipMapScaleFactor;
177                         while (diff_counter-- > 0)
178                         {
179                                 accumulator |= *(uint64_t*)src_ptr;
180                                 src_ptr += _unit_size;
181                         }
182
183                         *(uint64_t*)dest_ptr = accumulator;
184                 }
185         }
186 }
187
188 uint64_t LogicSnapshot::get_sample(uint64_t index) const
189 {
190         assert(_data);
191         assert(index < _sample_count);
192
193         return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
194 }
195
196 void LogicSnapshot::get_subsampled_edges(
197         std::vector<EdgePair> &edges,
198         uint64_t start, uint64_t end,
199         float min_length, int sig_index)
200 {
201         uint64_t index = start;
202         unsigned int level;
203         bool last_sample;
204         bool fast_forward;
205
206         assert(end <= get_sample_count());
207         assert(start <= end);
208         assert(min_length > 0);
209         assert(sig_index >= 0);
210         assert(sig_index < 64);
211
212         lock_guard<recursive_mutex> lock(_mutex);
213
214         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
215         const unsigned int min_level = max((int)floorf(logf(min_length) /
216                 LogMipMapScaleFactor) - 1, 0);
217         const uint64_t sig_mask = 1ULL << sig_index;
218
219         // Store the initial state
220         last_sample = (get_sample(start) & sig_mask) != 0;
221         edges.push_back(pair<int64_t, bool>(index++, last_sample));
222
223         while (index + block_length <= end)
224         {
225                 //----- Continue to search -----//
226                 level = min_level;
227
228                 // We cannot fast-forward if there is no mip-map data at
229                 // at the minimum level.
230                 fast_forward = (_mip_map[level].data != NULL);
231
232                 if (min_length < MipMapScaleFactor)
233                 {
234                         // Search individual samples up to the beginning of
235                         // the next first level mip map block
236                         const uint64_t final_index = min(end,
237                                 pow2_ceil(index, MipMapScalePower));
238
239                         for (; index < final_index &&
240                                 (index & ~(~0 << MipMapScalePower)) != 0;
241                                 index++)
242                         {
243                                 const bool sample =
244                                         (get_sample(index) & sig_mask) != 0;
245
246                                 // If there was a change we cannot fast forward
247                                 if (sample != last_sample) {
248                                         fast_forward = false;
249                                         break;
250                                 }
251                         }
252                 }
253                 else
254                 {
255                         // If resolution is less than a mip map block,
256                         // round up to the beginning of the mip-map block
257                         // for this level of detail
258                         const int min_level_scale_power =
259                                 (level + 1) * MipMapScalePower;
260                         index = pow2_ceil(index, min_level_scale_power);
261                         if (index >= end)
262                                 break;
263
264                         // We can fast forward only if there was no change
265                         const bool sample =
266                                 (get_sample(index) & sig_mask) != 0;
267                         if (last_sample != sample)
268                                 fast_forward = false;
269                 }
270
271                 if (fast_forward) {
272
273                         // Fast forward: This involves zooming out to higher
274                         // levels of the mip map searching for changes, then
275                         // zooming in on them to find the point where the edge
276                         // begins.
277
278                         // Slide right and zoom out at the beginnings of mip-map
279                         // blocks until we encounter a change
280                         while (1) {
281                                 const int level_scale_power =
282                                         (level + 1) * MipMapScalePower;
283                                 const uint64_t offset =
284                                         index >> level_scale_power;
285
286                                 // Check if we reached the last block at this
287                                 // level, or if there was a change in this block
288                                 if (offset >= _mip_map[level].length ||
289                                         (get_subsample(level, offset) &
290                                                 sig_mask))
291                                         break;
292
293                                 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
294                                         // If we are now at the beginning of a
295                                         // higher level mip-map block ascend one
296                                         // level
297                                         if (level + 1 >= ScaleStepCount ||
298                                                 !_mip_map[level + 1].data)
299                                                 break;
300
301                                         level++;
302                                 } else {
303                                         // Slide right to the beginning of the
304                                         // next mip map block
305                                         index = pow2_ceil(index + 1,
306                                                 level_scale_power);
307                                 }
308                         }
309
310                         // Zoom in, and slide right until we encounter a change,
311                         // and repeat until we reach min_level
312                         while (1) {
313                                 assert(_mip_map[level].data);
314
315                                 const int level_scale_power =
316                                         (level + 1) * MipMapScalePower;
317                                 const uint64_t offset =
318                                         index >> level_scale_power;
319
320                                 // Check if we reached the last block at this
321                                 // level, or if there was a change in this block
322                                 if (offset >= _mip_map[level].length ||
323                                         (get_subsample(level, offset) &
324                                                 sig_mask)) {
325                                         // Zoom in unless we reached the minimum
326                                         // zoom
327                                         if (level == min_level)
328                                                 break;
329
330                                         level--;
331                                 } else {
332                                         // Slide right to the beginning of the
333                                         // next mip map block
334                                         index = pow2_ceil(index + 1,
335                                                 level_scale_power);
336                                 }
337                         }
338
339                         // If individual samples within the limit of resolution,
340                         // do a linear search for the next transition within the
341                         // block
342                         if (min_length < MipMapScaleFactor) {
343                                 for (; index < end; index++) {
344                                         const bool sample = (get_sample(index) &
345                                                 sig_mask) != 0;
346                                         if (sample != last_sample)
347                                                 break;
348                                 }
349                         }
350                 }
351
352                 //----- Store the edge -----//
353
354                 // Take the last sample of the quanization block
355                 const int64_t final_index = index + block_length;
356                 if (index + block_length > end)
357                         break;
358
359                 // Store the final state
360                 const bool final_sample =
361                         (get_sample(final_index - 1) & sig_mask) != 0;
362                 edges.push_back(pair<int64_t, bool>(index, final_sample));
363
364                 index = final_index;
365                 last_sample = final_sample;
366         }
367
368         // Add the final state
369         const bool end_sample = get_sample(end) & sig_mask;
370         if (last_sample != end_sample)
371                 edges.push_back(pair<int64_t, bool>(end, end_sample));
372         edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
373 }
374
375 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
376 {
377         assert(level >= 0);
378         assert(_mip_map[level].data);
379         return *(uint64_t*)((uint8_t*)_mip_map[level].data +
380                 _unit_size * offset);
381 }
382
383 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
384 {
385         const uint64_t p = 1 << power;
386         return (x + p - 1) / p * p;
387 }
388
389 } // namespace data
390 } // namespace pv