2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
28 #include <boost/foreach.hpp>
30 #include "logicsnapshot.h"
32 using boost::lock_guard;
33 using boost::recursive_mutex;
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
46 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic) :
47 Snapshot(logic.unitsize),
48 _last_append_sample(0)
50 lock_guard<recursive_mutex> lock(_mutex);
51 memset(_mip_map, 0, sizeof(_mip_map));
52 append_payload(logic);
55 LogicSnapshot::~LogicSnapshot()
57 lock_guard<recursive_mutex> lock(_mutex);
58 BOOST_FOREACH(MipMapLevel &l, _mip_map)
62 void LogicSnapshot::append_payload(
63 const sr_datafeed_logic &logic)
65 assert(_unit_size == logic.unitsize);
66 assert((logic.length % _unit_size) == 0);
68 lock_guard<recursive_mutex> lock(_mutex);
70 append_data(logic.data, logic.length / _unit_size);
72 // Generate the first mip-map from the data
73 append_payload_to_mipmap();
76 void LogicSnapshot::get_samples(uint8_t *const data,
77 int64_t start_sample, int64_t end_sample) const
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);
86 lock_guard<recursive_mutex> lock(_mutex);
88 const size_t size = (end_sample - start_sample) * _unit_size;
89 memcpy(data, (const uint8_t*)_data + start_sample, size);
92 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
94 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
95 MipMapDataUnit) * MipMapDataUnit;
96 if (new_data_length > m.data_length)
98 m.data_length = new_data_length;
100 // Padding is added to allow for the uint64_t write word
101 m.data = realloc(m.data, new_data_length * _unit_size +
106 void LogicSnapshot::append_payload_to_mipmap()
108 MipMapLevel &m0 = _mip_map[0];
109 uint64_t prev_length;
110 const uint8_t *src_ptr;
112 uint64_t accumulator;
113 unsigned int diff_counter;
115 // Expand the data buffer to fit the new samples
116 prev_length = m0.length;
117 m0.length = _sample_count / MipMapScaleFactor;
119 // Break off if there are no new samples to compute
120 if (m0.length == prev_length)
123 reallocate_mipmap_level(m0);
125 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
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;)
134 // Accumulate transitions which have occurred in this sample
136 diff_counter = MipMapScaleFactor;
137 while (diff_counter-- > 0)
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;
145 *(uint64_t*)dest_ptr = accumulator;
146 dest_ptr += _unit_size;
149 // Compute higher level mipmaps
150 for (unsigned int level = 1; level < ScaleStepCount; level++)
152 MipMapLevel &m = _mip_map[level];
153 const MipMapLevel &ml = _mip_map[level-1];
155 // Expand the data buffer to fit the new samples
156 prev_length = m.length;
157 m.length = ml.length / MipMapScaleFactor;
159 // Break off if there are no more samples to computed
160 if (m.length == prev_length)
163 reallocate_mipmap_level(m);
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)
176 diff_counter = MipMapScaleFactor;
177 while (diff_counter-- > 0)
179 accumulator |= *(uint64_t*)src_ptr;
180 src_ptr += _unit_size;
183 *(uint64_t*)dest_ptr = accumulator;
188 uint64_t LogicSnapshot::get_sample(uint64_t index) const
191 assert(index < _sample_count);
193 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
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)
201 uint64_t index = start;
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);
212 lock_guard<recursive_mutex> lock(_mutex);
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;
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));
223 while (index + block_length <= end)
225 //----- Continue to search -----//
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);
232 if (min_length < MipMapScaleFactor)
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));
239 for (; index < final_index &&
240 (index & ~(~0 << MipMapScalePower)) != 0;
244 (get_sample(index) & sig_mask) != 0;
246 // If there was a change we cannot fast forward
247 if (sample != last_sample) {
248 fast_forward = false;
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);
264 // We can fast forward only if there was no change
266 (get_sample(index) & sig_mask) != 0;
267 if (last_sample != sample)
268 fast_forward = false;
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
278 // Slide right and zoom out at the beginnings of mip-map
279 // blocks until we encounter a change
281 const int level_scale_power =
282 (level + 1) * MipMapScalePower;
283 const uint64_t offset =
284 index >> level_scale_power;
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) &
293 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
294 // If we are now at the beginning of a
295 // higher level mip-map block ascend one
297 if (level + 1 >= ScaleStepCount ||
298 !_mip_map[level + 1].data)
303 // Slide right to the beginning of the
304 // next mip map block
305 index = pow2_ceil(index + 1,
310 // Zoom in, and slide right until we encounter a change,
311 // and repeat until we reach min_level
313 assert(_mip_map[level].data);
315 const int level_scale_power =
316 (level + 1) * MipMapScalePower;
317 const uint64_t offset =
318 index >> level_scale_power;
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) &
325 // Zoom in unless we reached the minimum
327 if (level == min_level)
332 // Slide right to the beginning of the
333 // next mip map block
334 index = pow2_ceil(index + 1,
339 // If individual samples within the limit of resolution,
340 // do a linear search for the next transition within the
342 if (min_length < MipMapScaleFactor) {
343 for (; index < end; index++) {
344 const bool sample = (get_sample(index) &
346 if (sample != last_sample)
352 //----- Store the edge -----//
354 // Take the last sample of the quanization block
355 const int64_t final_index = index + block_length;
356 if (index + block_length > end)
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));
365 last_sample = final_sample;
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));
375 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
378 assert(_mip_map[level].data);
379 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
380 _unit_size * offset);
383 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
385 const uint64_t p = 1 << power;
386 return (x + p - 1) / p * p;