Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/flame.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 29 янв. 2025 г.
7 *
8 * lsp-ws-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-ws-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/dsp/dsp.h>
23 #include <lsp-plug.in/ws/factory.h>
24 #include <lsp-plug.in/ws/IEventHandler.h>
25 #include <lsp-plug.in/stdlib/math.h>
26 #include <lsp-plug.in/test-fw/mtest.h>
27
28 #define FRAME_PERIOD (1000 / 25) /* Launch at 25 Hz rate */
29 #define FRAME_BUFFER_WIDTH 256
30 #define FRAME_BUFFER_HEIGHT 160
31
32 MTEST_BEGIN("ws.display", flame)
33
34 typedef struct osc_t
35 {
36 float A0; // Initial amplitude
37 float X0; // Initial location
38 float W0; // Frequency
39 float P0; // Initial phase
40 float R0; // Reduction/Decay
41 } osc_t;
42
43 class Handler: public ws::IEventHandler
44 {
45 private:
46 test_type_t *pTest;
47 ws::IWindow *pWnd;
48 ws::taskid_t nTaskId;
49 size_t nPhase;
50 osc_t vOsc[3];
51 float *vRow;
52 float *vRGBA;
53 uint8_t *vBuffer;
54
55 protected:
56 static status_t execute_timer(ws::timestamp_t sched, ws::timestamp_t time, void *arg)
57 {
58 Handler *pthis = static_cast<Handler *>(arg);
59 if (pthis == NULL)
60 return STATUS_OK;
61
62 pthis->nTaskId = -1;
63 pthis->on_timer();
64 pthis->launch(time + FRAME_PERIOD);
65
66 return STATUS_OK;
67 }
68
69 void oscillate(float *dst, const osc_t *osc, float t, ssize_t n)
70 {
71 float P = 2.0f * M_PI * osc->W0 * t + osc->P0;
72
73 for (ssize_t x=0; x < n; ++x)
74 {
75 float dx = -0.05f * fabs(osc->X0 - x);
76 dst[x] += osc->A0 * cosf(P + dx) * expf(osc->R0 * dx);
77 }
78 }
79
80 public:
81 inline Handler(test_type_t *test, ws::IWindow *wnd)
82 {
83 pTest = test;
84 pWnd = wnd;
85 nTaskId = -1;
86
87 nPhase = 0;
88
89 vOsc[0].A0 = 0.25f;
90 vOsc[0].X0 = 64;
91 vOsc[0].W0 = 2.0f;
92 vOsc[0].P0 = 0.0f;
93 vOsc[0].R0 = 0.01f;
94
95 vOsc[1].A0 = 0.25f;
96 vOsc[1].X0 = 128;
97 vOsc[1].W0 = 6.5f;
98 vOsc[1].P0 = 1.0f;
99 vOsc[1].R0 = 0.1f;
100
101 vOsc[2].A0 = 0.15f;
102 vOsc[2].X0 = 192;
103 vOsc[2].W0 = 1.33f;
104 vOsc[2].P0 = 0.5f;
105 vOsc[2].R0 = 0.05f;
106
107 vRow = new float[FRAME_BUFFER_WIDTH];
108 vRGBA = new float[FRAME_BUFFER_WIDTH * 4];
109 vBuffer = new uint8_t[FRAME_BUFFER_WIDTH * FRAME_BUFFER_HEIGHT * 4];
110
111 bzero(vBuffer, FRAME_BUFFER_WIDTH * FRAME_BUFFER_HEIGHT * 4);
112 }
113
114 ~Handler()
115 {
116 delete [] vBuffer;
117 delete [] vRow;
118 delete [] vRGBA;
119 }
120
121 void on_timer()
122 {
123 memmove(
124 &vBuffer[FRAME_BUFFER_WIDTH * 4 * 8],
125 vBuffer,
126 FRAME_BUFFER_WIDTH * (FRAME_BUFFER_HEIGHT - 8) * 4);
127
128 dsp::hsla_hue_eff_t eff;
129 eff.h = 0.0f;
130 eff.s = 1.0f;
131 eff.l = 0.5f;
132 eff.a = 0.0f;
133 eff.thresh = 1.0f / 3.0f;
134
135 for (size_t n=0; n<8; ++n)
136 {
137 float t = float(nPhase) / 0x800;
138 dsp::fill(vRow, 0.5f, FRAME_BUFFER_WIDTH);
139
140 for (size_t i=0; i<3; ++i)
141 oscillate(vRow, &vOsc[i], t, FRAME_BUFFER_WIDTH);
142
143 dsp::eff_hsla_hue(vRGBA, vRow, &eff, FRAME_BUFFER_WIDTH);
144 dsp::hsla_to_rgba(vRGBA, vRGBA, FRAME_BUFFER_WIDTH);
145 dsp::rgba_to_bgra32(&vBuffer[(7 - n) * FRAME_BUFFER_WIDTH * 4], vRGBA, FRAME_BUFFER_WIDTH);
146
147 ++nPhase;
148 }
149
150 // Query area for redraw
151 pWnd->invalidate();
152 }
153
154 void launch(ws::timestamp_t deadline = 0)
155 {
156 nTaskId = pWnd->display()->submit_task(deadline, execute_timer, this);
157 }
158
159 void stop()
160 {
161 if (nTaskId >= 0)
162 pWnd->display()->cancel_task(nTaskId);
163 }
164
165 virtual status_t handle_event(const ws::event_t *ev)
166 {
167 switch (ev->nType)
168 {
169 case ws::UIE_REDRAW:
170 {
171 Color c(0.0f, 0.0f, 0.0f);
172 ws::ISurface *s = pWnd->get_surface();
173 if (s == NULL)
174 return STATUS_OK;
175
176 float HW = pWnd->width() * 0.5f;
177 float HH = pWnd->height() * 0.5f;
178 #define X(x) (((x) + 1.0f) * HW)
179 #define Y(y) ((1.0f - (y)) * HH)
180
181 // Perform drawing
182 s->begin();
183 s->clear(c);
184
185 // Thin markers
186 c.set_rgb24(0xcccccc);
187 s->line(c, X(-1.0f), Y(-0.5f), X(1.0f), Y(-0.5f), 1.0f);
188 s->line(c, X(-1.0f), Y(0.5f), X(1.0f), Y(0.5f), 1.0f);
189 s->line(c, X(-0.5f), Y(-1.0f), X(-0.5f), Y(1.0f), 1.0f);
190 s->line(c, X(0.5f), Y(-1.0f), X(0.5f), Y(1.0f), 1.0f);
191
192 // Axes
193 {
194 ws::IGradient *g = s->linear_gradient(X(0.0f), Y(-1.0f), X(0.0f), Y(1.0f));
195 if (g != NULL)
196 {
197 lsp_finally { delete g; };
198 c.set_rgb24(0x0000ff);
199 g->set_start(c, 0.5f);
200 g->set_stop(c, 0.0f);
201
202 s->line(g, X(0.0f), Y(-1.0f), X(0.0f), Y(1.0f), 4.0f);
203 s->line(g, X(0.0f), Y(1.0f), X(-0.0625f), Y(1.0f - 0.0625f), 2.0f);
204 s->line(g, X(0.0f), Y(1.0f), X(0.0625f), Y(1.0f - 0.0625f), 2.0f);
205 }
206 }
207 {
208 ws::IGradient *g = s->linear_gradient(X(-1.0f), Y(0.0f), X(1.0f), Y(0.0f));
209 if (g != NULL)
210 {
211 lsp_finally { delete g; };
212 c.set_rgb24(0xff0000);
213 g->set_start(c, 0.5f);
214 g->set_stop(c, 0.0f);
215
216 s->line(g, X(-1.0f), Y(0.0f), X(1.0f), Y(0.0f), 4.0f);
217 s->line(g, X(1.0f), Y(0.0f), X(1.0f - 0.0625f), Y(0.0625f), 2.0f);
218 s->line(g, X(1.0f), Y(0.0f), X(1.0f - 0.0625f), Y(-0.0625f), 2.0f);
219 }
220 }
221
222 s->draw_raw(vBuffer,
223 FRAME_BUFFER_WIDTH, FRAME_BUFFER_HEIGHT, FRAME_BUFFER_WIDTH*4,
224 pWnd->width() * 0.25f, pWnd->height() * 0.25f,
225 pWnd->width() * 0.5f / FRAME_BUFFER_WIDTH, pWnd->height() * 0.5f / FRAME_BUFFER_HEIGHT, 0.0f);
226
227 s->end();
228
229 return STATUS_OK;
230 }
231
232 case ws::UIE_CLOSE:
233 {
234 pWnd->hide();
235 pWnd->display()->quit_main();
236 break;
237 }
238
239 default:
240 return IEventHandler::handle_event(ev);
241 }
242
243 return STATUS_OK;
244 }
245 };
246
247 MTEST_MAIN
248 {
249 ws::IDisplay *dpy = ws::create_display(0, NULL);
250 MTEST_ASSERT(dpy != NULL);
251 lsp_finally { ws::free_display(dpy); };
252
253 ws::IWindow *wnd = dpy->create_window();
254 MTEST_ASSERT(wnd != NULL);
255 lsp_finally {
256 wnd->destroy();
257 delete wnd;
258 };
259
260 MTEST_ASSERT(wnd->init() == STATUS_OK);
261 MTEST_ASSERT(wnd->set_caption("Test flame") == STATUS_OK);
262 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
263 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
264 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
265
266 Handler h(this, wnd);
267 h.launch();
268 lsp_finally{ h.stop(); };
269
270 wnd->set_handler(&h);
271
272 MTEST_ASSERT(wnd->show() == STATUS_OK);
273
274 MTEST_ASSERT(dpy->main() == STATUS_OK);
275 }
276
277 MTEST_END
278
279
280