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 namespace boost;
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
43 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic) :
44 Snapshot(logic.unitsize),
45 _last_append_sample(0)
47 lock_guard<recursive_mutex> lock(_mutex);
48 memset(_mip_map, 0, sizeof(_mip_map));
49 append_payload(logic);
52 LogicSnapshot::~LogicSnapshot()
54 lock_guard<recursive_mutex> lock(_mutex);
55 BOOST_FOREACH(MipMapLevel &l, _mip_map)
59 void LogicSnapshot::append_payload(
60 const sr_datafeed_logic &logic)
62 assert(_unit_size == logic.unitsize);
63 assert((logic.length % _unit_size) == 0);
65 lock_guard<recursive_mutex> lock(_mutex);
67 append_data(logic.data, logic.length / _unit_size);
69 // Generate the first mip-map from the data
70 append_payload_to_mipmap();
73 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
75 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
76 MipMapDataUnit) * MipMapDataUnit;
77 if (new_data_length > m.data_length)
79 m.data_length = new_data_length;
81 // Padding is added to allow for the uint64_t write word
82 m.data = realloc(m.data, new_data_length * _unit_size +
87 void LogicSnapshot::append_payload_to_mipmap()
89 MipMapLevel &m0 = _mip_map[0];
91 const uint8_t *src_ptr;
94 unsigned int diff_counter;
96 // Expand the data buffer to fit the new samples
97 prev_length = m0.length;
98 m0.length = _sample_count / MipMapScaleFactor;
100 // Break off if there are no new samples to compute
101 if (m0.length == prev_length)
104 reallocate_mipmap_level(m0);
106 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
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;)
115 // Accumulate transitions which have occurred in this sample
117 diff_counter = MipMapScaleFactor;
118 while (diff_counter-- > 0)
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;
126 *(uint64_t*)dest_ptr = accumulator;
127 dest_ptr += _unit_size;
130 // Compute higher level mipmaps
131 for (unsigned int level = 1; level < ScaleStepCount; level++)
133 MipMapLevel &m = _mip_map[level];
134 const MipMapLevel &ml = _mip_map[level-1];
136 // Expand the data buffer to fit the new samples
137 prev_length = m.length;
138 m.length = ml.length / MipMapScaleFactor;
140 // Break off if there are no more samples to computed
141 if (m.length == prev_length)
144 reallocate_mipmap_level(m);
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)
157 diff_counter = MipMapScaleFactor;
158 while (diff_counter-- > 0)
160 accumulator |= *(uint64_t*)src_ptr;
161 src_ptr += _unit_size;
164 *(uint64_t*)dest_ptr = accumulator;
169 uint64_t LogicSnapshot::get_sample(uint64_t index) const
172 assert(index < _sample_count);
174 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
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)
182 uint64_t index = start;
187 assert(end <= get_sample_count());
188 assert(start <= end);
189 assert(min_length > 0);
190 assert(sig_index >= 0);
191 assert(sig_index < 64);
193 lock_guard<recursive_mutex> lock(_mutex);
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;
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));
204 while (index + block_length <= end)
206 //----- Continue to search -----//
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);
213 if (min_length < MipMapScaleFactor)
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));
220 for (; index < final_index &&
221 (index & ~(~0 << MipMapScalePower)) != 0;
225 (get_sample(index) & sig_mask) != 0;
227 // If there was a change we cannot fast forward
228 if (sample != last_sample) {
229 fast_forward = false;
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);
245 // We can fast forward only if there was no change
247 (get_sample(index) & sig_mask) != 0;
248 if (last_sample != sample)
249 fast_forward = false;
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
259 // Slide right and zoom out at the beginnings of mip-map
260 // blocks until we encounter a change
262 const int level_scale_power =
263 (level + 1) * MipMapScalePower;
264 const uint64_t offset =
265 index >> level_scale_power;
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) &
274 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
275 // If we are now at the beginning of a
276 // higher level mip-map block ascend one
278 if (level + 1 >= ScaleStepCount ||
279 !_mip_map[level + 1].data)
284 // Slide right to the beginning of the
285 // next mip map block
286 index = pow2_ceil(index + 1,
291 // Zoom in, and slide right until we encounter a change,
292 // and repeat until we reach min_level
294 assert(_mip_map[level].data);
296 const int level_scale_power =
297 (level + 1) * MipMapScalePower;
298 const uint64_t offset =
299 index >> level_scale_power;
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) &
306 // Zoom in unless we reached the minimum
308 if (level == min_level)
313 // Slide right to the beginning of the
314 // next mip map block
315 index = pow2_ceil(index + 1,
320 // If individual samples within the limit of resolution,
321 // do a linear search for the next transition within the
323 if (min_length < MipMapScaleFactor) {
324 for (; index < end; index++) {
325 const bool sample = (get_sample(index) &
327 if (sample != last_sample)
333 //----- Store the edge -----//
335 // Take the last sample of the quanization block
336 const int64_t final_index = index + block_length;
337 if (index + block_length > end)
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));
346 last_sample = final_sample;
349 // Add the final state
350 edges.push_back(pair<int64_t, bool>(end,
351 get_sample(end) & sig_mask));
354 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
357 assert(_mip_map[level].data);
358 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
359 _unit_size * offset);
362 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
364 const uint64_t p = 1 << power;
365 return (x + p - 1) / p * p;