~aleteoryx/muditaos

ref: d5de12f7cea071555de4058ebf62b160a86a36af muditaos/module-services/service-gui/WorkerGUI.cpp -rw-r--r-- 3.0 KiB
d5de12f7 — Radosław Wicik [EGD-3852] clean include in service (#928) 5 years 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
78
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

extern "C"
{
}

#include <Service/Bus.hpp> // for Bus
#include <memory>          // for unique_ptr, make_shared
#include <utility>         // for move
#include <vector>          // for vector

#include "Service/Service.hpp" // for Service
#include "Service/Message.hpp" // for DataMessage
#include "Service/Worker.hpp"  // for Worker, WorkerCommand
#include "MessageType.hpp"     // for MessageType, MessageType::GUIRenderingFinished
#include "WorkerGUI.hpp"
// module-utils
#include "log/log.hpp"                // for LOG_ERROR
#include "DrawCommand.hpp"            // for DrawCommand
#include "Renderer.hpp"               // for Renderer
#include "projdefs.h"                 // for pdMS_TO_TICKS, pdTRUE
#include "queue.h"                    // for xQueueReceive, QueueDefinition, QueueHandle_t
#include "semphr.h"                   // for xSemaphoreGive, xSemaphoreTake
#include "service-gui/ServiceGUI.hpp" // for ServiceGUI

namespace sgui
{

    WorkerGUI::WorkerGUI(ServiceGUI *service) : Worker(service)
    {}

    bool WorkerGUI::handleMessage(uint32_t queueID)
    {
        QueueHandle_t queue = queues[queueID];

        sgui::ServiceGUI *serviceGUI = reinterpret_cast<sgui::ServiceGUI *>(service);

        // queue for receiving rendering commands
        if (queueID == 0) {
            //		LOG_INFO("Received rendering commands");

            sys::WorkerCommand received;
            xQueueReceive(queue, &received, 0);

            // take all unique pointers
            std::vector<std::unique_ptr<gui::DrawCommand>> uniqueCommands;

            if (xSemaphoreTake(serviceGUI->semCommands, pdMS_TO_TICKS(1000)) == pdTRUE) {
                for (auto it = serviceGUI->commands.begin(); it != serviceGUI->commands.end(); it++)
                    uniqueCommands.push_back(std::move(*it));
                serviceGUI->commands.clear();
                xSemaphoreGive(serviceGUI->semCommands);
            }
            else {
                LOG_ERROR("Failed to acquire semaphore");
            }

            // create temporary vector of pointers to draw commands to avoid polluting renderer with smart pointers.
            std::vector<gui::DrawCommand *> commands;
            for (auto it = uniqueCommands.begin(); it != uniqueCommands.end(); it++)
                commands.push_back((*it).get());

            //		uint32_t start_tick = xTaskGetTickCount();
            serviceGUI->renderer.render(serviceGUI->renderContext, commands);
            //		uint32_t end_tick = xTaskGetTickCount();
            //		LOG_INFO("[WorkerGUI] RenderingTime: %d", end_tick - start_tick);

            //			delete received;

            // notify gui service that rendering is complete
            auto message = std::make_shared<sys::DataMessage>(MessageType::GUIRenderingFinished);
            sys::Bus::SendUnicast(message, this->service->GetName(), this->service);
        }
        return true;
    }

} /* namespace sgui */