Beispiel #1
0
    ProcessorTaskImageResponse ImageTask::process(
        ProcessorTaskImageRequest& request,
        const std::string& hostId,
        zmq::socket_t* socket)
    {
        ProcessorTaskImageResponse response;
        response.set_taskid(request.taskid());

        auto error = CMP_InitializeBCLibrary();
        if (error != BC_ERROR_NONE)
        {
            switch (error)
            {
            case BC_ERROR_LIBRARY_NOT_INITIALIZED: LOG("BC_ERROR_LIBRARY_NOT_INITIALIZED"); break;
            case BC_ERROR_LIBRARY_ALREADY_INITIALIZED: LOG("BC_ERROR_LIBRARY_ALREADY_INITIALIZED"); break;
            case BC_ERROR_INVALID_PARAMETERS: LOG("BC_ERROR_INVALID_PARAMETERS"); break;
            case BC_ERROR_OUT_OF_MEMORY: LOG("BC_ERROR_OUT_OF_MEMORY"); break;
            }
        }
        //ASSERT(error == BC_ERROR_NONE, "Block compression library failed to initialize");
        auto destinationSizeBytes = static_cast<uint32_t>(engine::formatBytes(
            static_cast<Format>(request.enginepackedformat()),
            request.width(),
            request.height()));

        std::vector<char> dstBuffer(destinationSizeBytes);

        CMP_Texture dstTexture;
        dstTexture.dwSize = sizeof(CMP_Texture);
        dstTexture.dwWidth = request.width();
        dstTexture.dwHeight = request.height();
        dstTexture.dwPitch = 0;
        dstTexture.format = static_cast<CMP_FORMAT>(request.targetcmbcformat());
        dstTexture.nBlockHeight = 4;
        dstTexture.nBlockWidth = 4;
        dstTexture.nBlockDepth = 1;
        dstTexture.dwDataSize = static_cast<CMP_DWORD>(destinationSizeBytes);
        dstTexture.pData = reinterpret_cast<uint8_t*>(dstBuffer.data());// +currentDstBytes;

        CMP_Texture srcTexture;
        srcTexture.dwSize = sizeof(CMP_Texture);
        srcTexture.dwWidth = request.width();
        srcTexture.dwHeight = request.height();
        srcTexture.dwPitch = request.stride();
        srcTexture.format = static_cast<CMP_FORMAT>(request.sourcecmformat());
        srcTexture.dwDataSize = static_cast<CMP_DWORD>(request.data().size());// dibSize;
        srcTexture.pData = const_cast<CMP_BYTE*>(reinterpret_cast<const CMP_BYTE*>(request.data().data()));

        CMP_CompressOptions options = {};
        options.dwSize = sizeof(CMP_CompressOptions);
        options.bDisableMultiThreading = true;
        //options.fquality = 1.0;

        ImporterSurfaceWork work;
        work.request = &request;
        work.lastProgress = 0.0f;
        work.socket = socket;
        work.hostId = hostId;

        CMP_ConvertTexture(
            &srcTexture,
            &dstTexture,
            &options,
            feedback,
            reinterpret_cast<DWORD_PTR>(&work),
            reinterpret_cast<DWORD_PTR>(nullptr));

        response.set_width(request.width());
        response.set_height(request.height());
        response.set_format(request.enginepackedformat());
        response.set_data(dstBuffer.data(), dstBuffer.size());
        LOG("Task finished encoding: %s", request.taskid().c_str());
        return response;
    }
Beispiel #2
0
    int ResourceTask::join()
    {
        while (true)
        {
            poll(m_polls, 20);

            if (m_polls[0].revents & ZMQ_POLLIN)
            {
                m_state = TaskState::PerformingTask;

                MqMessage msg(m_socket);
                string id = string(msg.parts()[0].begin(), msg.parts()[0].end());

                ProcessorTaskMessageType type;
                type.ParseFromArray(msg.parts()[1].data(), msg.parts()[1].size());

                switch (type.type())
                {
                    case ProcessorTaskMessageType::TaskRequest:
                    {
                        ProcessorTaskRequest requesttype;
                        requesttype.ParseFromArray(msg.parts()[2].data(), msg.parts()[2].size());

                        vector<char> responseData;

                        switch (requesttype.type())
                        {
                            case ProcessorTaskRequest::Image:
                            {
                                ProcessorTaskImageRequest imageReq;
                                imageReq.ParseFromArray(msg.parts()[3].data(), msg.parts()[3].size());
                                LOG("Task was asked to perform Image encoding: %s", imageReq.taskid().c_str());
                                auto response = m_imageTask.process(imageReq, m_hostid, &m_socket);
                                responseData.resize(response.ByteSizeLong());
                                if(responseData.size() > 0)
                                    response.SerializeToArray(&responseData[0], responseData.size());
                                break;
                            }
                            case ProcessorTaskRequest::Model:
                            {
                                ProcessorTaskModelRequest modelReq;
                                modelReq.ParseFromArray(msg.parts()[3].data(), msg.parts()[3].size());
                                LOG("Task was asked to perform Model encoding: %i", modelReq.taskid().c_str());
                                auto response = m_modelTask.process(modelReq, m_hostid, &m_socket);

                                responseData.resize(response.ByteSizeLong());
                                if (responseData.size() > 0)
                                    response.SerializeToArray(&responseData[0], responseData.size());
                                break;
                            }
                        }

                        ProcessorTaskMessageType resulttype;
                        resulttype.set_type(ProcessorTaskMessageType::TaskResponse);
                        vector<char> type_message(resulttype.ByteSizeLong());
                        if (type_message.size() > 0)
                            resulttype.SerializeToArray(&type_message[0], type_message.size());

                        MqMessage responseMsg;
                        responseMsg.emplace_back(id);
                        responseMsg.emplace_back("");
                        responseMsg.emplace_back(std::move(type_message));
                        responseMsg.emplace_back(std::move(msg.parts()[2]));
                        responseMsg.emplace_back(std::move(responseData));
                        responseMsg.send(m_socket);

                        break;
                    }
                    case ProcessorTaskMessageType::DoneRequest:
                    {
                        ProcessorTaskMessageType resulttype;
                        resulttype.set_type(ProcessorTaskMessageType::DoneResponse);
                        vector<char> type_message(resulttype.ByteSizeLong());
                        if (type_message.size() > 0)
                            resulttype.SerializeToArray(&type_message[0], type_message.size());

                        MqMessage responseMsg;
                        responseMsg.emplace_back(id);
                        responseMsg.emplace_back("");
                        responseMsg.emplace_back(std::move(type_message));
                        responseMsg.send(m_socket);
                        return 0;
                    }
                }
                //EncodeTaskHeloAnswer
            }

            if (m_state == TaskState::WaitingForHelo)
            {
                auto now = std::chrono::high_resolution_clock::now();
                auto durationMs = static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(now - m_lastCheck).count()) / 1000000.0;

                if (durationMs > 300)
                {
                    m_lastCheck = now;
                    ProcessorTaskMessageType type;
                    type.set_type(ProcessorTaskMessageType::Helo);
                    vector<char> type_message(type.ByteSizeLong());
                    if (type_message.size() > 0)
                        type.SerializeToArray(&type_message[0], type_message.size());

                    MqMessage msg;
                    msg.emplace_back(m_hostid);
                    msg.emplace_back("");
                    msg.emplace_back(move(type_message));
                    msg.send(m_socket);

                    LOG("Sent TASK helo to ID: %s", m_hostid.c_str());
                }
            }
        }
        LOG("Task exiting");
        return 0;
    }