From 53b8dabe7322d61e03a4b7f9710c79664493e31a Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Thu, 25 Jun 2015 00:04:27 +0100 Subject: [PATCH] Cache current view for reuse on next paint --- inputsource.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ inputsource.h | 5 +++++ 2 files changed, 52 insertions(+) diff --git a/inputsource.cpp b/inputsource.cpp index d68c2e0..826e45b 100644 --- a/inputsource.cpp +++ b/inputsource.cpp @@ -45,6 +45,48 @@ InputSource::~InputSource() { } void InputSource::GetViewport(float *dest, int x, int y, int width, int height, int zoom) { + int crossover_start_lines = 0; + int crossover_end_lines = 0; + if (m_output_cache != nullptr && m_cache_valid) { + // Scroll down, with at least some of previous cache still in view + if (m_prev_y <= y && y < m_prev_y + m_prev_height) { + float *crossover_start = m_output_cache + (y - m_prev_y) * m_fft_size; + int crossover_lines; + // This covers the case of a window size reduction + if (y + height < m_prev_y + m_prev_height) { + crossover_lines = (m_prev_y + height - y); + } else { + crossover_lines = (m_prev_y + m_prev_height - y); + } + int crossover_length = crossover_lines * m_fft_size * sizeof(float); + memcpy(dest, crossover_start, crossover_length); + + crossover_start_lines = crossover_lines; + // Scroll up + } else if (y <= m_prev_y && m_prev_y < y + height) { + float *crossover_start = dest + (m_prev_y - y) * m_fft_size; + int crossover_lines = y + height - m_prev_y; + int crossover_length = crossover_lines * m_fft_size * sizeof(float); + memcpy(crossover_start, m_output_cache, crossover_length); + + crossover_end_lines = crossover_lines; + } + } + + if (m_output_cache == nullptr || height != m_prev_height) { + m_output_cache = (float*)realloc(m_output_cache, m_fft_size * height * sizeof(float)); + } + + m_prev_y = y; + m_prev_height = height; + + float *cache_ptr = m_output_cache; + y += crossover_start_lines; + height -= crossover_start_lines; + height -= crossover_end_lines; + dest += crossover_start_lines * m_fft_size; + cache_ptr += crossover_start_lines * m_fft_size; + fftwf_complex *sample_ptr = &m_data[y * GetFFTStride()]; for (int i = 0; i < height; i++) { @@ -66,9 +108,12 @@ void InputSource::GetViewport(float *dest, int x, int y, int width, int height, float magdb = 10 * log2(mag) / log2(10); *dest = magdb; dest++; + *cache_ptr = magdb; + cache_ptr++; } sample_ptr += GetFFTStride(); } + m_cache_valid = true; } int InputSource::GetHeight() { @@ -87,6 +132,7 @@ bool InputSource::ZoomIn() { m_zoom = m_max_zoom; return false; } + m_cache_valid = false; return true; } @@ -96,6 +142,7 @@ bool InputSource::ZoomOut() { m_zoom = 0; return false; } + m_cache_valid = false; return true; } diff --git a/inputsource.h b/inputsource.h index 3986c2f..86e052e 100644 --- a/inputsource.h +++ b/inputsource.h @@ -10,6 +10,11 @@ private: fftwf_complex *m_data; int m_fft_size; + float *m_output_cache = nullptr; + bool m_cache_valid = false; + int m_prev_y = -1; + int m_prev_height = -1; + fftwf_complex *m_fftw_in; fftwf_complex *m_fftw_out; fftwf_plan m_fftw_plan;