Debian Codegraph @

libraries/lib-wave-track-paint/waveform/WaveBitmapCache.cpp

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 WaveBitmapCache.h
7
8 Dmitry Vedenko
9
10 **********************************************************************/
11 #include "WaveBitmapCache.h"
12
13 #include <cassert>
14 #include <cmath>
15 #include <cstring>
16
17 #include "ZoomInfo.h"
18
19 #include "waveform/WaveDataCache.h"
20
21 #include "Envelope.h"
22 #include "FrameStatistics.h"
23
24 #include "WaveClip.h"
25
26 // The worst case scenario is:
27 // blank > background > envelope top + envelope bottom > sample > rms > zero line
28 // So we have 7 bands
29
30 constexpr size_t ColorFunctionBands = 7;
31
32 struct Triplet final
33 {
34 Triplet() = default;
35
36 explicit Triplet(graphics::Color color)
37 : r(color.GetRed())
38 , g(color.GetGreen())
39 , b(color.GetBlue())
40 {
41 }
42
43 void SetColor(graphics::Color color)
44 {
45 r = color.GetRed();
46 g = color.GetGreen();
47 b = color.GetBlue();
48 }
49
50 Triplet(const Triplet&) = default;
51 Triplet(Triplet&&) = default;
52 Triplet& operator=(const Triplet&) = default;
53 Triplet& operator=(Triplet&&) = default;
54
55 uint8_t r { 0 };
56 uint8_t g { 0 };
57 uint8_t b { 0 };
58
59 uint8_t unused { 0 };
60 };
61
62 struct Band final
63 {
64 Triplet color;
65 int from;
66 int to;
67 };
68
69 struct ColorFunction final
70 {
71 std::array<Band, ColorFunctionBands> bands;
72 size_t bandsCount { 0 };
73
74 Triplet GetColor(int row, Triplet defaultColor) const noexcept
75 {
76 // Later bands are the topmost
77 for (size_t i = bandsCount; i > 0; --i)
78 {
79 const Band& band = bands[i - 1];
80 if (band.from <= row && row < band.to)
81 return band.color;
82 }
83 return defaultColor;
84 }
85
86 void AddBand(graphics::Color color, int from, int to)
87 {
88 assert(bandsCount < bands.size());
89 assert(from <= to);
90
91 bands[bandsCount++] = { Triplet(color), from, to };
92 }
93
94 void Reset()
95 {
96 bandsCount = 0;
97 }
98 };
99 struct WaveBitmapCache::LookupHelper final
100 {
101 explicit LookupHelper(std::shared_ptr <WaveDataCache> dataCache)
102 : DataCache(std::move(dataCache))
103 {
104 }
105
106 bool PerformLookup(WaveBitmapCache* cache, GraphicsDataCacheKey key)
107 {
108 DataCache->UpdateViewportWidth(cache->GetMaxViewportWidth());
109
110 auto result = DataCache->PerformLookup(key);
111
112 if (result == nullptr)
113 return false;
114
115 auto sw = FrameStatistics::CreateStopwatch(
116 FrameStatistics::SectionID::WaveBitmapCachePreprocess);
117
118 const auto columnsCount = result->AvailableColumns;
119
120 if (cache->mPaintParameters.DBScale)
121 {
122 auto GetDBValue =
123 [dbRange = cache->mPaintParameters.DBRange](float value)
124 {
125 float sign = (value >= 0 ? 1 : -1);
126
127 if (value != 0.)
128 {
129 float db = LINEAR_TO_DB(fabs(value));
130 value = (db + dbRange) / dbRange;
131
132 if (value < 0.0)
133 value = 0.0;
134
135 value *= sign;
136 }
137
138 return value;
139 };
140
141 for (size_t column = 0; column < columnsCount; ++column)
142 {
143 auto oldColumn = result->Data[column];
144
145 DBRemappedColumns[column] = { GetDBValue(oldColumn.min),
146 GetDBValue(oldColumn.max),
147 GetDBValue(oldColumn.rms) };
148 }
149 }
150
151 auto GetRowFromValue =
152 [min = cache->mPaintParameters.Min, max = cache->mPaintParameters.Max,
153 height = cache->mPaintParameters.Height](float value)
154 {
155 value = (max - value) / (max - min);
156 return static_cast<int>(value * (height - 1) + 0.5);
157 };
158
159 const auto height = cache->mPaintParameters.Height;
160
161 const auto zeroLineY = GetRowFromValue(.0f);
162
163 auto inputData = cache->mPaintParameters.DBScale ?
164 DBRemappedColumns.data() :
165 result->Data.data();
166
167 auto envelope = cache->mEnvelope;
168
169 if (
170 envelope != nullptr && (envelope->GetNumberOfPoints() > 0 ||
171 envelope->GetDefaultValue() != 1.0))
172 {
173 envelope->GetValues(
174 EnvelopeValues.data(), static_cast<int>(EnvelopeValues.size()),
175 key.FirstSample / cache->GetScaledSampleRate() +
176 envelope->GetOffset(),
177 1.0 / key.PixelsPerSecond);
178
179 for (size_t column = 0; column < columnsCount; ++column)
180 {
181 const auto columnData = inputData[column];
182 const float envelopeValue = EnvelopeValues[column];
183
184 EnvRemappedColumns[column] = {
185 columnData.min * envelopeValue,
186 columnData.max * envelopeValue,
187 columnData.rms * envelopeValue
188 };
189 }
190
191 inputData = EnvRemappedColumns.data();
192 }
193
194 const auto globalMaxRow = GetRowFromValue(cache->mPaintParameters.Max);
195 const auto globalMinRow = GetRowFromValue(cache->mPaintParameters.Min) + 1;
196
197 const auto blankColor = cache->mPaintParameters.BlankColor;
198 const auto zeroLineColor = cache->mPaintParameters.ZeroLineColor;
199
200 const auto backgroundColors = cache->mPaintParameters.BackgroundColors;
201 const auto envelopeColors = cache->mPaintParameters.EnvelopeColors;
202 const auto sampleColors = cache->mPaintParameters.SampleColors;
203 const auto rmsColors = cache->mPaintParameters.RMSColors;
204 const auto clipColors = cache->mPaintParameters.ClippingColors;
205 const auto showRMS = cache->mPaintParameters.ShowRMS;
206 const auto drawEnvelope = cache->mPaintParameters.DrawEnvelope;
207
208 auto firstPixel = int64_t(key.FirstSample / cache->GetScaledSampleRate() * key.PixelsPerSecond + 0.5);
209
210 const auto selFirst = cache->mSelection.FirstPixel;
211 const auto selLast = cache->mSelection.LastPixel;
212
213 const bool showClipping = cache->mPaintParameters.ShowClipping;
214
215 for (size_t column = 0; column < columnsCount; ++column)
216 {
217 const bool selected = firstPixel >= selFirst && firstPixel < selLast;
218 ++firstPixel;
219
220 const auto columnData = inputData[column];
221 auto& function = ColorFunctions[column];
222 function.Reset();
223
224 if (showClipping && (columnData.min <= -MAX_AUDIO || columnData.max >= MAX_AUDIO))
225 {
226 function.AddBand(selected ? clipColors.Selected : clipColors.Normal, 0, height);
227 continue;
228 }
229
230 const auto maxSampleRow = GetRowFromValue(columnData.max);
231 const auto minSampleRow = GetRowFromValue(columnData.min);
232
233 function.AddBand(selected ? backgroundColors.Selected : backgroundColors.Normal, globalMaxRow, globalMinRow);
234
235 if (envelope) {
236 const auto envelopeValue = envelope->GetNumberOfPoints() ? EnvelopeValues[column] : envelope->GetDefaultValue();
237 const auto envelopePositiveRow = std::clamp(GetRowFromValue(envelopeValue), globalMaxRow, globalMinRow);
238 const auto envelopeNegativeRow = std::clamp(GetRowFromValue(-envelopeValue) + 1, globalMaxRow, globalMinRow);
239
240 function.AddBand(envelopeColors.Normal, globalMaxRow, envelopePositiveRow);
241
242 if (drawEnvelope) {
243 const auto envelopeEndPositiveRow = std::clamp(GetRowFromValue(envelopeValue - 0.5), globalMaxRow, zeroLineY);
244 const auto envelopeEndNegativeRow = std::clamp(GetRowFromValue(-envelopeValue + 0.5) + 1, zeroLineY, globalMinRow);
245
246 if (envelopeEndNegativeRow != envelopeEndPositiveRow)
247 function.AddBand(envelopeColors.Normal, envelopeEndPositiveRow, envelopeEndNegativeRow);
248 }
249
250 function.AddBand(envelopeColors.Normal, envelopeNegativeRow, globalMinRow);
251 }
252
253 function.AddBand(zeroLineColor, zeroLineY, zeroLineY+1);
254
255 function.AddBand(selected ? sampleColors.Selected : sampleColors.Normal, maxSampleRow, minSampleRow == maxSampleRow ? minSampleRow + 1 : minSampleRow);
256
257 if (showRMS)
258 {
259 const auto rmsPositiveRow = GetRowFromValue(std::min(columnData.rms, columnData.max));
260 const auto rmsNegativeRow = GetRowFromValue(std::max(-columnData.rms, columnData.min));
261
262 if (rmsNegativeRow >= rmsPositiveRow)
263 function.AddBand(selected ? rmsColors.Selected : rmsColors.Normal, rmsPositiveRow, rmsNegativeRow);
264 }
265 }
266
267 AvailableColumns = columnsCount;
268 IsComplete = result->IsComplete;
269
270 return true;
271 }
272
273 std::shared_ptr<WaveDataCache> DataCache;
274
275 std::array<ColorFunction, GraphicsDataCacheBase::CacheElementWidth>
276 ColorFunctions {};
277
278 WaveCacheElement::Columns DBRemappedColumns {};
279
280 std::array<double, GraphicsDataCacheBase::CacheElementWidth>
281 EnvelopeValues {};
282 WaveCacheElement::Columns EnvRemappedColumns {};
283
284 size_t AvailableColumns { 0 };
285 bool IsComplete { 0 };
286 };
287
288 WaveBitmapCache::WaveBitmapCache(
289 const WaveClip& waveClip, std::shared_ptr<WaveDataCache> dataCache,
290 ElementFactory elementFactory)
291 : GraphicsDataCache { waveClip.GetRate() / waveClip.GetStretchRatio(),
292 std::move(elementFactory) }
293 , mLookupHelper { std::make_unique<LookupHelper>(std::move(dataCache)) }
294 , mWaveClip { waveClip }
295 , mStretchChangedSubscription {
296 const_cast<WaveClip&>(waveClip)
297 .Observer::Publisher<StretchRatioChange>::Subscribe(
298 [this](const StretchRatioChange&) {
299 SetScaledSampleRate(
300 mWaveClip.GetRate() / mWaveClip.GetStretchRatio());
301 })
302 }
303 {
304 }
305
306 WaveBitmapCache::~WaveBitmapCache() = default;
307
308 WaveBitmapCacheElement::~WaveBitmapCacheElement() = default;
309
310
311 WaveBitmapCache&
312 WaveBitmapCache::SetPaintParameters(const WavePaintParameters& params)
313 {
314 if (mPaintParameters != params)
315 {
316 mPaintParameters = params;
317 mEnvelope = params.AttachedEnvelope;
318 mEnvelopeVersion = mEnvelope != nullptr ? mEnvelope->GetVersion() : 0;
319
320 Invalidate();
321 }
322
323 return *this;
324 }
325
326 WaveBitmapCache& WaveBitmapCache::SetSelection(
327 const ZoomInfo& zoomInfo, double t0, double t1, bool selected)
328 {
329 const auto empty = !selected || t0 > t1 ||
330 ((t1 - t0) < std::numeric_limits<double>::epsilon());
331
332 const auto first = empty ? int64_t(-1) : zoomInfo.TimeToPosition(t0);
333 const auto last =
334 empty ? int64_t(-1) : std::max(zoomInfo.TimeToPosition(t1), first + 1);
335
336 if (mSelection.FirstPixel != first || mSelection.LastPixel != last)
337 {
338 mSelection.FirstPixel = first;
339 mSelection.LastPixel = last;
340
341 Invalidate();
342 }
343
344 return *this;
345 }
346
347 void WaveBitmapCache::CheckCache(const ZoomInfo&, double, double)
348 {
349 if (mEnvelope != nullptr && mEnvelopeVersion != mEnvelope->GetVersion())
350 {
351 mEnvelopeVersion = mEnvelope->GetVersion();
352 Invalidate();
353 }
354 }
355
356 bool WaveBitmapCache::InitializeElement(
357 const GraphicsDataCacheKey& key, WaveBitmapCacheElement& element)
358 {
359 if (mPaintParameters.Height == 0)
360 return false;
361
362 if (!mLookupHelper->PerformLookup(this, key))
363 {
364 const auto width = 1;
365 const auto height = mPaintParameters.Height;
366 const auto bytes = element.Allocate(width, height);
367 std::memset(bytes, 0, width * height * 3);
368 return true;
369 }
370
371 auto sw = FrameStatistics::CreateStopwatch(
372 FrameStatistics::SectionID::WaveBitmapCache);
373
374 const auto columnsCount = mLookupHelper->AvailableColumns;
375
376 const auto defaultColor = Triplet(mPaintParameters.BlankColor);
377
378 const auto height = static_cast<int>(mPaintParameters.Height);
379
380 auto rowData = element.Allocate(columnsCount, height);
381
382 for (int row = 0; row < height; ++row)
383 {
384 auto colorFunction = mLookupHelper->ColorFunctions.data();
385
386 for (size_t pixel = 0; pixel < columnsCount; ++pixel)
387 {
388 const auto color = colorFunction->GetColor(row, defaultColor);
389
390 *rowData++ = color.r;
391 *rowData++ = color.g;
392 *rowData++ = color.b;
393
394 ++colorFunction;
395 }
396 }
397
398 element.AvailableColumns = columnsCount;
399 element.IsComplete = mLookupHelper->IsComplete;
400
401 return true;
402 }