提交 3e126fb2 编写于 作者: S Simon Fels

Integrate RenderThread directly into our message processor

上级 9d7c5652
......@@ -109,9 +109,7 @@ anbox::cmds::Run::Run(const BusFactory& bus_factory)
auto qemu_pipe_connector = std::make_shared<network::PublishedSocketConnector>(
utils::string_format("%s/qemu_pipe", config::socket_path()),
rt,
std::make_shared<qemu::PipeConnectionCreator>(rt,
renderer->socket_path(),
icon_));
std::make_shared<qemu::PipeConnectionCreator>(rt));
auto android_api_stub = std::make_shared<bridge::AndroidApiStub>();
......
......@@ -18,6 +18,7 @@
#include "anbox/logger.h"
#include "anbox/graphics/gl_renderer_server.h"
#include "anbox/graphics/window_creator.h"
#include "anbox/graphics/emugl/FrameBuffer.h"
#include "OpenglRender/render_api.h"
......@@ -50,8 +51,8 @@ GLRendererServer::GLRendererServer(const std::shared_ptr<WindowCreator> &window_
}
GLRendererServer::~GLRendererServer() {
// destroyOpenGLSubwindow();
stopOpenGLRenderer();
if (const auto fb = FrameBuffer::getFB())
fb->finalize();
}
void logger_write(const char *format, ...) {
......@@ -70,22 +71,7 @@ void GLRendererServer::start() {
log_funcs.coarse = logger_write;
log_funcs.fine = logger_write;
auto display_info = window_creator_->display_info();
const auto width = display_info.horizontal_resolution;
const auto height = display_info.vertical_resolution;
char server_addr[256] = { 0 };
// The width & height we supply here are the dimensions the internal framebuffer
// will use. Making this static prevents us for now to resize the window we create
// later for the actual display.
if (!initOpenGLRenderer(window_creator_->native_display(), server_addr, sizeof(server_addr), log_funcs, logger_write))
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to setup OpenGL renderer"));
socket_path_ = server_addr;
}
std::string GLRendererServer::socket_path() const {
return socket_path_;
FrameBuffer::initialize(window_creator_->native_display());
}
} // namespace graphics
} // namespace anbox
......@@ -34,10 +34,7 @@ public:
void start();
std::string socket_path() const;
private:
std::string socket_path_;
std::shared_ptr<WindowCreator> window_creator_;
};
......
......@@ -20,39 +20,139 @@
#include "anbox/network/local_socket_messenger.h"
#include "anbox/network/connections.h"
#include "anbox/network/delegate_message_processor.h"
#include "anbox/graphics/emugl/RenderThread.h"
namespace anbox {
namespace graphics {
OpenGlesMessageProcessor::OpenGlesMessageProcessor(const std::string &renderer_socket_path,
const std::shared_ptr<Runtime> &rt,
const std::shared_ptr<network::SocketMessenger> &messenger) :
client_messenger_(messenger) {
#include "external/android-emugl/host/include/libOpenglRender/IOStream.h"
connect_and_attach(renderer_socket_path, rt);
}
#include <condition_variable>
#include <queue>
OpenGlesMessageProcessor::~OpenGlesMessageProcessor() {
}
namespace {
class DirectIOStream : public IOStream {
public:
explicit DirectIOStream(const std::shared_ptr<anbox::network::SocketMessenger> &messenger,
const size_t &buffer_size = 10000) :
IOStream(buffer_size),
messenger_(messenger) {
}
virtual ~DirectIOStream() {
if (send_buffer_ != nullptr) {
free(send_buffer_);
send_buffer_ = nullptr;
}
}
void* allocBuffer(size_t min_size) override {
size_t size = (send_buffer_size_ < min_size ? min_size : send_buffer_size_);
if (!send_buffer_)
send_buffer_ = (unsigned char *) malloc(size);
else if (send_buffer_size_ < size) {
unsigned char *p = (unsigned char *)realloc(send_buffer_, size);
if (p != NULL) {
send_buffer_ = p;
send_buffer_size_ = size;
} else {
free(send_buffer_);
send_buffer_ = NULL;
send_buffer_size_ = 0;
}
}
return send_buffer_;
}
int commitBuffer(size_t size) override {
messenger_->send(reinterpret_cast<const char*>(send_buffer_), size);
return size;
}
const unsigned char* readFully(void*, size_t) override {
ERROR("Not implemented");
return nullptr;
}
const unsigned char* read(void *data, size_t *size) override {
if (!wait_for_data() || buffer_.size() == 0) {
*size = 0;
return nullptr;
}
auto bytes_to_read = *size;
if (bytes_to_read > buffer_.size())
bytes_to_read = buffer_.size();
::memcpy(data, buffer_.data(), bytes_to_read);
buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_read);
*size = bytes_to_read;
return static_cast<const unsigned char*>(data);
}
void OpenGlesMessageProcessor::connect_and_attach(const std::string &socket_path,
const std::shared_ptr<Runtime> &rt) {
int writeFully(const void*, size_t) override {
ERROR("Not implemented");
return 0;
}
auto socket = std::make_shared<boost::asio::local::stream_protocol::socket>(rt->service());
socket->connect(boost::asio::local::stream_protocol::endpoint(socket_path));
void forceStop() override {
std::unique_lock<std::mutex> l(mutex_);
buffer_.clear();
}
messenger_ = std::make_shared<network::LocalSocketMessenger>(socket);
renderer_ = std::make_shared<network::SocketConnection>(
messenger_, messenger_, 0, nullptr,
std::make_shared<network::DelegateMessageProcessor>([&](const std::vector<std::uint8_t> &data) {
client_messenger_->send(reinterpret_cast<const char*>(data.data()), data.size());
void submitData(const std::vector<std::uint8_t> &data) {
std::unique_lock<std::mutex> l(mutex_);
for (const auto &byte : data)
buffer_.push_back(byte);
// buffer_.insert(buffer_.end(), data.begin(), data.end());
lock_.notify_one();
}
private:
bool wait_for_data() {
std::unique_lock<std::mutex> l(mutex_);
if (!l.owns_lock())
return false;
lock_.wait(l, [&]() { return !buffer_.empty(); });
return true;
}));
renderer_->set_name("opengles-renderer");
renderer_->read_next_message();
}
std::shared_ptr<anbox::network::SocketMessenger> messenger_;
std::mutex mutex_;
std::condition_variable lock_;
std::vector<std::uint8_t> buffer_;
unsigned char *send_buffer_ = nullptr;
size_t send_buffer_size_ = 0;
};
}
namespace anbox {
namespace graphics {
emugl::Mutex OpenGlesMessageProcessor::global_lock{};
OpenGlesMessageProcessor::OpenGlesMessageProcessor(const std::shared_ptr<network::SocketMessenger> &messenger) :
messenger_(messenger),
stream_(std::make_shared<DirectIOStream>(messenger_)),
renderer_(RenderThread::create(stream_.get(), &global_lock)) {
// We have to read the client flags first before we can continue
// processing the actual commands
std::array<std::uint8_t, sizeof(unsigned int)> buffer;
messenger_->receive_msg(boost::asio::buffer(buffer));
renderer_->start();
}
OpenGlesMessageProcessor::~OpenGlesMessageProcessor() {
DEBUG("");
renderer_->forceStop();
renderer_->wait(nullptr);
}
bool OpenGlesMessageProcessor::process_data(const std::vector<std::uint8_t> &data) {
messenger_->send(reinterpret_cast<const char*>(data.data()), data.size());
auto stream = std::static_pointer_cast<DirectIOStream>(stream_);
stream->submitData(data);
return true;
}
} // namespace graphics
......
......@@ -27,24 +27,26 @@
#include "anbox/network/socket_messenger.h"
#include "anbox/network/socket_connection.h"
#include "external/android-emugl/shared/emugl/common/mutex.h"
class IOStream;
class RenderThread;
namespace anbox {
namespace graphics {
class OpenGlesMessageProcessor : public network::MessageProcessor {
public:
OpenGlesMessageProcessor(const std::string &renderer_socket_path,
const std::shared_ptr<Runtime> &rt,
const std::shared_ptr<network::SocketMessenger> &messenger);
OpenGlesMessageProcessor(const std::shared_ptr<network::SocketMessenger> &messenger);
~OpenGlesMessageProcessor();
bool process_data(const std::vector<std::uint8_t> &data) override;
private:
void connect_and_attach(const std::string &socket_path,
const std::shared_ptr<Runtime> &rt);
static emugl::Mutex global_lock;
std::shared_ptr<network::SocketMessenger> client_messenger_;
std::shared_ptr<network::SocketMessenger> messenger_;
std::shared_ptr<network::SocketConnection> renderer_;
std::shared_ptr<IOStream> stream_;
std::shared_ptr<RenderThread> renderer_;
};
} // namespace graphics
} // namespace anbox
......
......@@ -62,14 +62,10 @@ std::string client_type_to_string(const anbox::qemu::PipeConnectionCreator::clie
}
namespace anbox {
namespace qemu {
PipeConnectionCreator::PipeConnectionCreator(const std::shared_ptr<Runtime> &rt,
const std::string &renderer_socket_path,
const std::string &boot_animation_icon_path) :
PipeConnectionCreator::PipeConnectionCreator(const std::shared_ptr<Runtime> &rt) :
runtime_(rt),
next_connection_id_(0),
connections_(std::make_shared<network::Connections<network::SocketConnection>>()),
renderer_socket_path_(renderer_socket_path),
boot_animation_icon_path_(boot_animation_icon_path) {
connections_(std::make_shared<network::Connections<network::SocketConnection>>()) {
}
PipeConnectionCreator::~PipeConnectionCreator() {
......@@ -137,7 +133,7 @@ PipeConnectionCreator::client_type PipeConnectionCreator::identify_client(
std::shared_ptr<network::MessageProcessor> PipeConnectionCreator::create_processor(const client_type &type, const std::shared_ptr<network::SocketMessenger> &messenger) {
if (type == client_type::opengles)
return std::make_shared<graphics::OpenGlesMessageProcessor>(renderer_socket_path_, runtime_, messenger);
return std::make_shared<graphics::OpenGlesMessageProcessor>(messenger);
else if (type == client_type::qemud_boot_properties)
return std::make_shared<qemu::BootPropertiesMessageProcessor>(messenger);
else if (type == client_type::qemud_hw_control)
......@@ -150,8 +146,6 @@ std::shared_ptr<network::MessageProcessor> PipeConnectionCreator::create_process
return std::make_shared<qemu::FingerprintMessageProcessor>(messenger);
else if (type == client_type::qemud_gsm)
return std::make_shared<qemu::GsmMessageProcessor>(messenger);
else if (type == client_type::bootanimation)
return std::make_shared<qemu::BootAnimationMessageProcessor>(messenger, boot_animation_icon_path_);
else if (type == client_type::qemud_adb)
return std::make_shared<qemu::AdbMessageProcessor>(runtime_, messenger);
......
......@@ -33,10 +33,7 @@ namespace anbox {
namespace qemu {
class PipeConnectionCreator : public network::ConnectionCreator<boost::asio::local::stream_protocol> {
public:
PipeConnectionCreator(
const std::shared_ptr<Runtime> &rt,
const std::string &renderer_socket_path,
const std::string &boot_animation_icon_path);
PipeConnectionCreator(const std::shared_ptr<Runtime> &rt);
~PipeConnectionCreator() noexcept;
void create_connection_for(
......@@ -65,9 +62,6 @@ private:
std::shared_ptr<Runtime> runtime_;
std::atomic<int> next_connection_id_;
std::shared_ptr<network::Connections<network::SocketConnection>> const connections_;
std::string renderer_socket_path_;
std::string boot_animation_icon_path_;
};
} // namespace qemu
} // namespace anbox
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册