~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/module-services/service-gui/WorkerGUI.cpp -rw-r--r-- 2.4 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 months 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

#include "WorkerGUI.hpp"

#include <DrawCommand.hpp>
#include <log/log.hpp>
#include <Service/Worker.hpp>
#include <service-gui/ServiceGUI.hpp>

#include <memory>
#include <sstream>
#include "messages/RenderingFinished.hpp"

namespace service::gui
{
    WorkerGUI::WorkerGUI(ServiceGUI *service) : Worker(service), guiService{service}
    {}

    void WorkerGUI::notify(Signal command)
    {
        if (auto queue = getQueueByName(SignallingQueueName); !queue->Overwrite(&command)) {
            LOG_ERROR("Unable to overwrite the command in the commands queue.");
        }
    }

    bool WorkerGUI::handleMessage(uint32_t queueID)
    {
        if (const auto queue = queues[queueID]; queue->GetQueueName() == SignallingQueueName) {
            if (sys::WorkerCommand command; queue->Dequeue(&command, 0)) {
                handleCommand(static_cast<Signal>(command.command));
            }
        }
        return true;
    }

    void WorkerGUI::handleCommand(Signal command)
    {
        switch (command) {
        case Signal::Render: {
            auto item = guiService->commandsQueue->dequeue();
            if (item.has_value()) {
                render(item->commands, item->refreshMode);
            }
            break;
        }
        case Signal::ChangeColorScheme: {
            changeColorScheme(guiService->colorSchemeUpdate);
            break;
        }
        default:
            LOG_ERROR("Command not valid.");
        }
    }

    void WorkerGUI::render(DrawCommandsQueue::CommandList &commands, ::gui::RefreshModes refreshMode)
    {
        const auto [contextId, context] = guiService->contextPool->borrowContext(); // Waits for the context.
        renderer.render(context, commands);
#if DEBUG_EINK_REFRESH == 1
        LOG_INFO("Render ContextId: %d\n%s", contextId, context->toAsciiScaled().c_str());
#endif
        onRenderingFinished(contextId, refreshMode);
    }

    void WorkerGUI::changeColorScheme(const std::unique_ptr<::gui::ColorScheme> &scheme)
    {
        renderer.changeColorScheme(scheme);
    }

    void WorkerGUI::onRenderingFinished(int contextId, ::gui::RefreshModes refreshMode)
    {
        auto msg = std::make_shared<service::gui::RenderingFinished>(contextId, refreshMode);
        guiService->bus.sendUnicast(std::move(msg), guiService->GetName());
    }

} // namespace service::gui