MCPcopy Create free account
hub / github.com/diyorbek/canvas-native / ffi_thread_func

Function ffi_thread_func

native/debug/test_multithread.cpp:26–70  ·  view source on GitHub ↗

--- THE FFI / WORKER THREAD --- This thread simulates your library receiving FFI calls and rendering them offscreen.

Source from the content-addressed store, hash-verified

24// This thread simulates your library receiving FFI calls and rendering them
25// offscreen.
26void ffi_thread_func() {
27 // 1. Claim the background context for this thread
28 SDL_GL_MakeCurrent(window, ffiContext);
29
30 // 2. Create a thread-local NanoVG context
31 NVGcontext* ffi_vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
32
33 float rotation = 0;
34
35 while (running) {
36 // In a real library, this is where you'd wait for FFI commands
37 SDL_Delay(16); // Aim for ~60fps updates
38
39 // 3. Render into the shared framebuffer
40 nvgluBindFramebuffer(uiLayer);
41 glViewport(0, 0, 1024, 768);
42 glClearColor(0, 0, 0, 0); // Transparent base
43 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
44
45 nvgBeginFrame(ffi_vg, 1024, 768, 1.0f);
46
47 // Rotating shape to prove the thread is alive
48 rotation += 0.02f;
49 nvgSave(ffi_vg);
50 nvgTranslate(ffi_vg, 500, 350);
51 nvgRotate(ffi_vg, rotation);
52 nvgBeginPath(ffi_vg);
53 nvgRect(ffi_vg, -50, -50, 100, 100);
54 nvgStrokeColor(ffi_vg, nvgRGBA(255, 165, 0, 255));
55 nvgStrokeWidth(ffi_vg, 10);
56 nvgStroke(ffi_vg);
57 nvgRestore(ffi_vg);
58
59 nvgFontSize(ffi_vg, 24.0f);
60 nvgFillColor(ffi_vg, nvgRGBA(255, 255, 255, 255));
61 nvgTextAlign(ffi_vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
62 nvgText(ffi_vg, 500, 450, "RENDERED BY FFI THREAD", NULL);
63 nvgEndFrame(ffi_vg);
64
65 // 4. CRITICAL: Flush commands so Main Thread sees the data
66 glFlush();
67 }
68
69 nvgDeleteGL3(ffi_vg);
70}
71
72// --- THE MAIN THREAD (CONSUMER) ---
73int main(int argc, char* argv[]) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected