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