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::get_samples(uint8_t *const data,
74 int64_t start_sample, int64_t end_sample) const
77 assert(start_sample >= 0);
78 assert(start_sample < (int64_t)_sample_count);
79 assert(end_sample >= 0);
80 assert(end_sample < (int64_t)_sample_count);
81 assert(start_sample <= end_sample);
83 lock_guard<recursive_mutex> lock(_mutex);
85 const size_t size = (end_sample - start_sample) * _unit_size;
86 memcpy(data, (const uint8_t*)_data + start_sample, size);
89 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
91 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
92 MipMapDataUnit) * MipMapDataUnit;
93 if (new_data_length > m.data_length)
95 m.data_length = new_data_length;
97 // Padding is added to allow for the uint64_t write word
98 m.data = realloc(m.data, new_data_length * _unit_size +
103 void LogicSnapshot::append_payload_to_mipmap()
105 MipMapLevel &m0 = _mip_map[0];
106 uint64_t prev_length;
107 const uint8_t *src_ptr;
109 uint64_t accumulator;
110 unsigned int diff_counter;
112 // Expand the data buffer to fit the new samples
113 prev_length = m0.length;
114 m0.length = _sample_count / MipMapScaleFactor;
116 // Break off if there are no new samples to compute
117 if (m0.length == prev_length)
120 reallocate_mipmap_level(m0);
122 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
124 // Iterate through the samples to populate the first level mipmap
125 const uint8_t *const end_src_ptr = (uint8_t*)_data +
126 m0.length * _unit_size * MipMapScaleFactor;
127 for (src_ptr = (uint8_t*)_data +
128 prev_length * _unit_size * MipMapScaleFactor;
129 src_ptr < end_src_ptr;)
131 // Accumulate transitions which have occurred in this sample
133 diff_counter = MipMapScaleFactor;
134 while (diff_counter-- > 0)
136 const uint64_t sample = *(uint64_t*)src_ptr;
137 accumulator |= _last_append_sample ^ sample;
138 _last_append_sample = sample;
139 src_ptr += _unit_size;
142 *(uint64_t*)dest_ptr = accumulator;
143 dest_ptr += _unit_size;
146 // Compute higher level mipmaps
147 for (unsigned int level = 1; level < ScaleStepCount; level++)
149 MipMapLevel &m = _mip_map[level];
150 const MipMapLevel &ml = _mip_map[level-1];
152 // Expand the data buffer to fit the new samples
153 prev_length = m.length;
154 m.length = ml.length / MipMapScaleFactor;
156 // Break off if there are no more samples to computed
157 if (m.length == prev_length)
160 reallocate_mipmap_level(m);
162 // Subsample the level lower level
163 src_ptr = (uint8_t*)ml.data +
164 _unit_size * prev_length * MipMapScaleFactor;
165 const uint8_t *const end_dest_ptr =
166 (uint8_t*)m.data + _unit_size * m.length;
167 for (dest_ptr = (uint8_t*)m.data +
168 _unit_size * prev_length;
169 dest_ptr < end_dest_ptr;
170 dest_ptr += _unit_size)
173 diff_counter = MipMapScaleFactor;
174 while (diff_counter-- > 0)
176 accumulator |= *(uint64_t*)src_ptr;
177 src_ptr += _unit_size;
180 *(uint64_t*)dest_ptr = accumulator;
185 uint64_t LogicSnapshot::get_sample(uint64_t index) const
188 assert(index < _sample_count);
190 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
193 void LogicSnapshot::get_subsampled_edges(
194 std::vector<EdgePair> &edges,
195 uint64_t start, uint64_t end,
196 float min_length, int sig_index)
198 uint64_t index = start;
203 assert(end <= get_sample_count());
204 assert(start <= end);
205 assert(min_length > 0);
206 assert(sig_index >= 0);
207 assert(sig_index < 64);
209 lock_guard<recursive_mutex> lock(_mutex);
211 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
212 const unsigned int min_level = max((int)floorf(logf(min_length) /
213 LogMipMapScaleFactor) - 1, 0);
214 const uint64_t sig_mask = 1ULL << sig_index;
216 // Store the initial state
217 last_sample = (get_sample(start) & sig_mask) != 0;
218 edges.push_back(pair<int64_t, bool>(index++, last_sample));
220 while (index + block_length <= end)
222 //----- Continue to search -----//
225 // We cannot fast-forward if there is no mip-map data at
226 // at the minimum level.
227 fast_forward = (_mip_map[level].data != NULL);
229 if (min_length < MipMapScaleFactor)
231 // Search individual samples up to the beginning of
232 // the next first level mip map block
233 const uint64_t final_index = min(end,
234 pow2_ceil(index, MipMapScalePower));
236 for (; index < final_index &&
237 (index & ~(~0 << MipMapScalePower)) != 0;
241 (get_sample(index) & sig_mask) != 0;
243 // If there was a change we cannot fast forward
244 if (sample != last_sample) {
245 fast_forward = false;
252 // If resolution is less than a mip map block,
253 // round up to the beginning of the mip-map block
254 // for this level of detail
255 const int min_level_scale_power =
256 (level + 1) * MipMapScalePower;
257 index = pow2_ceil(index, min_level_scale_power);
261 // We can fast forward only if there was no change
263 (get_sample(index) & sig_mask) != 0;
264 if (last_sample != sample)
265 fast_forward = false;
270 // Fast forward: This involves zooming out to higher
271 // levels of the mip map searching for changes, then
272 // zooming in on them to find the point where the edge
275 // Slide right and zoom out at the beginnings of mip-map
276 // blocks until we encounter a change
278 const int level_scale_power =
279 (level + 1) * MipMapScalePower;
280 const uint64_t offset =
281 index >> level_scale_power;
283 // Check if we reached the last block at this
284 // level, or if there was a change in this block
285 if (offset >= _mip_map[level].length ||
286 (get_subsample(level, offset) &
290 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
291 // If we are now at the beginning of a
292 // higher level mip-map block ascend one
294 if (level + 1 >= ScaleStepCount ||
295 !_mip_map[level + 1].data)
300 // Slide right to the beginning of the
301 // next mip map block
302 index = pow2_ceil(index + 1,
307 // Zoom in, and slide right until we encounter a change,
308 // and repeat until we reach min_level
310 assert(_mip_map[level].data);
312 const int level_scale_power =
313 (level + 1) * MipMapScalePower;
314 const uint64_t offset =
315 index >> level_scale_power;
317 // Check if we reached the last block at this
318 // level, or if there was a change in this block
319 if (offset >= _mip_map[level].length ||
320 (get_subsample(level, offset) &
322 // Zoom in unless we reached the minimum
324 if (level == min_level)
329 // Slide right to the beginning of the
330 // next mip map block
331 index = pow2_ceil(index + 1,
336 // If individual samples within the limit of resolution,
337 // do a linear search for the next transition within the
339 if (min_length < MipMapScaleFactor) {
340 for (; index < end; index++) {
341 const bool sample = (get_sample(index) &
343 if (sample != last_sample)
349 //----- Store the edge -----//
351 // Take the last sample of the quanization block
352 const int64_t final_index = index + block_length;
353 if (index + block_length > end)
356 // Store the final state
357 const bool final_sample =
358 (get_sample(final_index - 1) & sig_mask) != 0;
359 edges.push_back(pair<int64_t, bool>(index, final_sample));
362 last_sample = final_sample;
365 // Add the final state
366 edges.push_back(pair<int64_t, bool>(end,
367 get_sample(end) & sig_mask));
370 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
373 assert(_mip_map[level].data);
374 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
375 _unit_size * offset);
378 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
380 const uint64_t p = 1 << power;
381 return (x + p - 1) / p * p;