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