| 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: 31 окт. 2022 г. |
| 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/ws/factory.h> |
| 23 |
|
| 24 |
#include <lsp-plug.in/ipc/Thread.h> |
| 25 |
#include <lsp-plug.in/stdlib/math.h> |
| 26 |
#include <lsp-plug.in/test-fw/mtest.h> |
| 27 |
#include <lsp-plug.in/ws/IEventHandler.h> |
| 28 |
|
| 29 |
MTEST_BEGIN("ws.display", async_tasks) |
| 30 |
|
| 31 |
class Handler: public ws::IEventHandler |
| 32 |
{ |
| 33 |
private: |
| 34 |
static constexpr const size_t NUM_POINTS = 1000; |
| 35 |
|
| 36 |
private: |
| 37 |
test_type_t *pTest; |
| 38 |
ws::IWindow *pWnd; |
| 39 |
float fPhase; |
| 40 |
|
| 41 |
public: |
| 42 |
inline Handler(test_type_t *test, ws::IWindow *wnd) |
| 43 |
{ |
| 44 |
pTest = test; |
| 45 |
pWnd = wnd; |
| 46 |
fPhase = 0.0f; |
| 47 |
} |
| 48 |
|
| 49 |
void commit_phase(float phase) |
| 50 |
{ |
| 51 |
fPhase = phase; |
| 52 |
// Query area for redraw |
| 53 |
pWnd->invalidate(); |
| 54 |
} |
| 55 |
|
| 56 |
virtual status_t handle_event(const ws::event_t *ev) override |
| 57 |
{ |
| 58 |
switch (ev->nType) |
| 59 |
{ |
| 60 |
case ws::UIE_REDRAW: |
| 61 |
{ |
| 62 |
Color c(1.0f, 1.0f, 1.0f); |
| 63 |
ws::ISurface *s = pWnd->get_surface(); |
| 64 |
if (s == NULL) |
| 65 |
return STATUS_OK; |
| 66 |
|
| 67 |
// Perform drawing |
| 68 |
s->begin(); |
| 69 |
{ |
| 70 |
lsp_finally { s->end(); }; |
| 71 |
s->clear(c); |
| 72 |
|
| 73 |
float *x = new float[NUM_POINTS+1]; |
| 74 |
if (!x) |
| 75 |
return STATUS_NO_MEM; |
| 76 |
lsp_finally { delete[] x; }; |
| 77 |
float *y = new float[NUM_POINTS+1]; |
| 78 |
if (!y) |
| 79 |
return STATUS_NO_MEM; |
| 80 |
lsp_finally { delete[] y; }; |
| 81 |
|
| 82 |
// Draw lissajous figure |
| 83 |
for (size_t i=0; i <= NUM_POINTS; ++i) |
| 84 |
{ |
| 85 |
float a = (i * 2.0f * M_PI) / NUM_POINTS; |
| 86 |
x[i] = s->width() * (1 + cosf(4.0f * a + fPhase)) * 0.5f; |
| 87 |
y[i] = s->height() * (1 + sinf(3.0f * a)) * 0.5f; |
| 88 |
} |
| 89 |
|
| 90 |
c.set_rgb(0.0f, 0.0f, 1.0f); |
| 91 |
s->wire_poly(c, 3, x, y, NUM_POINTS+1); |
| 92 |
} |
| 93 |
|
| 94 |
return STATUS_OK; |
| 95 |
} |
| 96 |
|
| 97 |
case ws::UIE_CLOSE: |
| 98 |
{ |
| 99 |
pWnd->hide(); |
| 100 |
pWnd->display()->quit_main(); |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
default: |
| 105 |
return IEventHandler::handle_event(ev); |
| 106 |
} |
| 107 |
|
| 108 |
return STATUS_OK; |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
typedef struct async_task_t |
| 113 |
{ |
| 114 |
Handler *handler; |
| 115 |
float phase; |
| 116 |
} async_task_t; |
| 117 |
|
| 118 |
static status_t async_task_handler(ws::timestamp_t sched, ws::timestamp_t time, void *arg) |
| 119 |
{ |
| 120 |
async_task_t *task = static_cast<async_task_t *>(arg); |
| 121 |
if (task == NULL) |
| 122 |
return STATUS_BAD_ARGUMENTS; |
| 123 |
|
| 124 |
task->handler->commit_phase(task->phase); |
| 125 |
delete task; |
| 126 |
return STATUS_OK; |
| 127 |
} |
| 128 |
|
| 129 |
class Submitter: public ipc::Thread |
| 130 |
{ |
| 131 |
private: |
| 132 |
ws::IWindow *pWnd; |
| 133 |
Handler *pHandler; |
| 134 |
uint32_t nCounter; |
| 135 |
|
| 136 |
public: |
| 137 |
explicit Submitter(ws::IWindow *wnd, Handler *h) |
| 138 |
{ |
| 139 |
pWnd = wnd; |
| 140 |
pHandler = h; |
| 141 |
nCounter = 0; |
| 142 |
}; |
| 143 |
|
| 144 |
public: |
| 145 |
virtual status_t run() override |
| 146 |
{ |
| 147 |
while (!is_cancelled()) |
| 148 |
{ |
| 149 |
// Submit asynchronous task |
| 150 |
async_task_t *task = new async_task_t(); |
| 151 |
if (task == NULL) |
| 152 |
return STATUS_NO_MEM; |
| 153 |
|
| 154 |
task->handler = pHandler; |
| 155 |
task->phase = ((nCounter++) & 0x03ff) * 2.0f * M_PI / float(0x400); |
| 156 |
|
| 157 |
pWnd->display()->submit_task(0, async_task_handler, task); |
| 158 |
|
| 159 |
// Sleep for a while |
| 160 |
Thread::sleep(20); |
| 161 |
} |
| 162 |
|
| 163 |
return STATUS_OK; |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
MTEST_MAIN |
| 168 |
{ |
| 169 |
ws::IDisplay *dpy = ws::create_display(0, NULL); |
| 170 |
MTEST_ASSERT(dpy != NULL); |
| 171 |
lsp_finally { ws::free_display(dpy); }; |
| 172 |
|
| 173 |
ws::IWindow *wnd = dpy->create_window(); |
| 174 |
MTEST_ASSERT(wnd != NULL); |
| 175 |
lsp_finally { |
| 176 |
wnd->destroy(); |
| 177 |
delete wnd; |
| 178 |
}; |
| 179 |
|
| 180 |
MTEST_ASSERT(wnd->init() == STATUS_OK); |
| 181 |
MTEST_ASSERT(wnd->set_caption("Test async tasks") == STATUS_OK); |
| 182 |
MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE | ws::WA_RESIZE) == STATUS_OK); |
| 183 |
MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK); |
| 184 |
MTEST_ASSERT(wnd->set_size_constraints(320, 200, 640, 400) == STATUS_OK); |
| 185 |
|
| 186 |
Handler h(this, wnd); |
| 187 |
wnd->set_handler(&h); |
| 188 |
|
| 189 |
MTEST_ASSERT(wnd->show() == STATUS_OK); |
| 190 |
|
| 191 |
Submitter submitter(wnd, &h); |
| 192 |
MTEST_ASSERT(submitter.start() == STATUS_OK); |
| 193 |
MTEST_ASSERT(dpy->main() == STATUS_OK); |
| 194 |
MTEST_ASSERT(submitter.cancel() == STATUS_OK); |
| 195 |
MTEST_ASSERT(submitter.join() == STATUS_OK); |
| 196 |
} |
| 197 |
|
| 198 |
MTEST_END |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|