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