]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/logicsnapshot.cpp
Preallocate memory before samples are received.
[pulseview.git] / pv / data / logicsnapshot.cpp
... / ...
CommitLineData
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
32using boost::lock_guard;
33using boost::recursive_mutex;
34using std::max;
35using std::min;
36using std::pair;
37
38namespace pv {
39namespace data {
40
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
45
46LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
47 const uint64_t expected_num_samples) :
48 Snapshot(logic.unitsize),
49 _last_append_sample(0)
50{
51 set_capacity(expected_num_samples);
52
53 lock_guard<recursive_mutex> lock(_mutex);
54 memset(_mip_map, 0, sizeof(_mip_map));
55 append_payload(logic);
56}
57
58LogicSnapshot::~LogicSnapshot()
59{
60 lock_guard<recursive_mutex> lock(_mutex);
61 BOOST_FOREACH(MipMapLevel &l, _mip_map)
62 free(l.data);
63}
64
65void LogicSnapshot::append_payload(
66 const sr_datafeed_logic &logic)
67{
68 assert(_unit_size == logic.unitsize);
69 assert((logic.length % _unit_size) == 0);
70
71 lock_guard<recursive_mutex> lock(_mutex);
72
73 append_data(logic.data, logic.length / _unit_size);
74
75 // Generate the first mip-map from the data
76 append_payload_to_mipmap();
77}
78
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);
84 assert(start_sample <= (int64_t)_sample_count);
85 assert(end_sample >= 0);
86 assert(end_sample <= (int64_t)_sample_count);
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
95void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
96{
97 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
98 MipMapDataUnit) * MipMapDataUnit;
99 if (new_data_length > m.data_length)
100 {
101 m.data_length = new_data_length;
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));
106 }
107}
108
109void LogicSnapshot::append_payload_to_mipmap()
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;
120 m0.length = _sample_count / MipMapScaleFactor;
121
122 // Break off if there are no new samples to compute
123 if (m0.length == prev_length)
124 return;
125
126 reallocate_mipmap_level(m0);
127
128 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
129
130 // Iterate through the samples to populate the first level mipmap
131 const uint8_t *const end_src_ptr = (uint8_t*)_data +
132 m0.length * _unit_size * MipMapScaleFactor;
133 for (src_ptr = (uint8_t*)_data +
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;
140 while (diff_counter-- > 0)
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
153 for (unsigned int level = 1; level < ScaleStepCount; level++)
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
163 if (m.length == prev_length)
164 break;
165
166 reallocate_mipmap_level(m);
167
168 // Subsample the level lower level
169 src_ptr = (uint8_t*)ml.data +
170 _unit_size * prev_length * MipMapScaleFactor;
171 const uint8_t *const end_dest_ptr =
172 (uint8_t*)m.data + _unit_size * m.length;
173 for (dest_ptr = (uint8_t*)m.data +
174 _unit_size * prev_length;
175 dest_ptr < end_dest_ptr;
176 dest_ptr += _unit_size)
177 {
178 accumulator = 0;
179 diff_counter = MipMapScaleFactor;
180 while (diff_counter-- > 0)
181 {
182 accumulator |= *(uint64_t*)src_ptr;
183 src_ptr += _unit_size;
184 }
185
186 *(uint64_t*)dest_ptr = accumulator;
187 }
188 }
189}
190
191uint64_t LogicSnapshot::get_sample(uint64_t index) const
192{
193 assert(_data);
194 assert(index < _sample_count);
195
196 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
197}
198
199void LogicSnapshot::get_subsampled_edges(
200 std::vector<EdgePair> &edges,
201 uint64_t start, uint64_t end,
202 float min_length, int sig_index)
203{
204 uint64_t index = start;
205 unsigned int level;
206 bool last_sample;
207 bool fast_forward;
208
209 assert(end <= get_sample_count());
210 assert(start <= end);
211 assert(min_length > 0);
212 assert(sig_index >= 0);
213 assert(sig_index < 64);
214
215 lock_guard<recursive_mutex> lock(_mutex);
216
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) /
219 LogMipMapScaleFactor) - 1, 0);
220 const uint64_t sig_mask = 1ULL << sig_index;
221
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));
225
226 while (index + block_length <= end)
227 {
228 //----- Continue to search -----//
229 level = min_level;
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);
234
235 if (min_length < MipMapScaleFactor)
236 {
237 // Search individual samples up to the beginning of
238 // the next first level mip map block
239 const uint64_t final_index = min(end,
240 pow2_ceil(index, MipMapScalePower));
241
242 for (; index < final_index &&
243 (index & ~(~0 << MipMapScalePower)) != 0;
244 index++)
245 {
246 const bool sample =
247 (get_sample(index) & sig_mask) != 0;
248
249 // If there was a change we cannot fast forward
250 if (sample != last_sample) {
251 fast_forward = false;
252 break;
253 }
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);
264 if (index >= end)
265 break;
266
267 // We can fast forward only if there was no change
268 const bool sample =
269 (get_sample(index) & sig_mask) != 0;
270 if (last_sample != sample)
271 fast_forward = false;
272 }
273
274 if (fast_forward) {
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
283 while (1) {
284 const int level_scale_power =
285 (level + 1) * MipMapScalePower;
286 const uint64_t offset =
287 index >> level_scale_power;
288
289 // Check if we reached the last block at this
290 // level, or if there was a change in this block
291 if (offset >= _mip_map[level].length ||
292 (get_subsample(level, offset) &
293 sig_mask))
294 break;
295
296 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
297 // If we are now at the beginning of a
298 // higher level mip-map block ascend one
299 // level
300 if (level + 1 >= ScaleStepCount ||
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 }
311 }
312
313 // Zoom in, and slide right until we encounter a change,
314 // and repeat until we reach min_level
315 while (1) {
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;
322
323 // Check if we reached the last block at this
324 // level, or if there was a change in this block
325 if (offset >= _mip_map[level].length ||
326 (get_subsample(level, offset) &
327 sig_mask)) {
328 // Zoom in unless we reached the minimum
329 // zoom
330 if (level == min_level)
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 }
340 }
341
342 // If individual samples within the limit of resolution,
343 // do a linear search for the next transition within the
344 // block
345 if (min_length < MipMapScaleFactor) {
346 for (; index < end; index++) {
347 const bool sample = (get_sample(index) &
348 sig_mask) != 0;
349 if (sample != last_sample)
350 break;
351 }
352 }
353 }
354
355 //----- Store the edge -----//
356
357 // Take the last sample of the quanization block
358 const int64_t final_index = index + block_length;
359 if (index + block_length > end)
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;
369 }
370
371 // Add the final state
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));
376}
377
378uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
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
386uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
387{
388 const uint64_t p = 1 << power;
389 return (x + p - 1) / p * p;
390}
391
392} // namespace data
393} // namespace pv