~aleteoryx/muditaos

muditaos/module-gui/test/test-catch/gui-context-drawer.cpp -rw-r--r-- 2.5 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

// This is minimalistic example how our GUI renderer works - this code won't compile right now
// there shouldn't be much work to do to make it compile again, there is no target like that in need so it's not added
// this could be potentially used instead running whole PurePhone simulator to test/create UI only code

#include <gui/widgets/Window.hpp>
#include <gui/core/Context.hpp>
#include <gui/core/Renderer.hpp>
#include <gui/core/DrawCommand.hpp>

static const int FrameBufferWidth  = 480;
static const int FrameBufferHeight = 600;
static const int FrameBufferSize   = FrameBufferWidth * FrameBufferHeight;

typedef struct
{
    uint32_t frameCount;
    uint32_t width;
    uint32_t height;
} shared_memory;

uint8_t *createSHMBuffer(std::string name)
{
    shared_memory *shared_mem_ptr;
    int fd_shm;

    // check if shared memory blok is already created
    if ((fd_shm = shm_open(name.c_str(), O_RDWR | O_CREAT, 0660)) == -1) {
        std::cerr << "shm is already created" << std::endl;
    }
    else {
        std::cout << "shm created" << std::endl;
        if (ftruncate(fd_shm, sizeof(shared_memory) + FrameBufferSize) == -1) {
            std::cerr << "shm is already created" << std::endl;
        }
    }
    if ((shared_mem_ptr = static_cast<shared_memory *>(
             mmap(NULL, sizeof(shared_memory) + FrameBufferSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm, 0))) ==
        MAP_FAILED) {
        std::cerr << "mmap failed" << std::endl;
    }

    return reinterpret_cast<uint8_t *>(shared_mem_ptr);
}

void drawWindow(auto *window, auto *frameBuffer)
{
    // create renderer object
    gui::Renderer renderer;
    // create canvas
    gui::Context *context = new gui::Context(static_cast<unsigned short>(480), static_cast<unsigned short>(600));
    context->fill(15);

    /// generate draw commands from window
    std::list<gui::DrawCommand *> commandsList = win->buildDrawList();
    std::vector<gui::DrawCommand *> commands{commandsList.begin(), commandsList.end()};

    // render commands
    renderer.render(context, commands);

    // copy context to shared memory (our framebuffer)
    memcpy(frameBuffer, context->getData(), FrameBufferSize);

    // cleanup context
    delete context;
}

int main(int argc, char **argv)
{
    gui::Window *win = new gui::AppWindow("MAIN");
    win->setSize(480, 600);
    win->buildInterface();
    win->setTitle("LOL");
    drawWindow(win, createSHMBuffer());
}