2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "anbox/graphics/opengles_message_processor.h"
19 #include "anbox/common/small_vector.h"
20 #include "anbox/graphics/buffered_io_stream.h"
21 #include "anbox/graphics/emugl/RenderThread.h"
22 #include "anbox/logger.h"
23 #include "anbox/network/connections.h"
24 #include "anbox/network/delegate_message_processor.h"
26 #include <condition_variable>
32 std::mutex OpenGlesMessageProcessor::global_lock{};
34 OpenGlesMessageProcessor::OpenGlesMessageProcessor(
35 const std::shared_ptr<Renderer> &renderer,
36 const std::shared_ptr<network::SocketMessenger> &messenger)
37 : messenger_(messenger),
38 stream_(std::make_shared<BufferedIOStream>(messenger_)) {
39 // We have to read the client flags first before we can continue
40 // processing the actual commands
41 unsigned int client_flags = 0;
42 auto err = messenger_->receive_msg(
43 boost::asio::buffer(&client_flags, sizeof(unsigned int)));
44 if (err) ERROR("%s", err.message());
46 render_thread_.reset(RenderThread::create(renderer, stream_.get(), std::ref(global_lock)));
47 if (!render_thread_->start())
48 BOOST_THROW_EXCEPTION(
49 std::runtime_error("Failed to start renderer thread"));
52 OpenGlesMessageProcessor::~OpenGlesMessageProcessor() {
53 render_thread_->forceStop();
54 render_thread_->wait(nullptr);
57 bool OpenGlesMessageProcessor::process_data(
58 const std::vector<std::uint8_t> &data) {
59 auto stream = std::static_pointer_cast<BufferedIOStream>(stream_);
60 Buffer buffer{data.data(), data.data() + data.size()};
61 stream->post_data(std::move(buffer));
64 } // namespace graphics