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/gl_renderer_server.h"
19 #include "anbox/graphics/emugl/RenderApi.h"
20 #include "anbox/graphics/emugl/RenderControl.h"
21 #include "anbox/graphics/emugl/Renderer.h"
22 #include "anbox/graphics/layer_composer.h"
23 #include "anbox/graphics/multi_window_composer_strategy.h"
24 #include "anbox/graphics/single_window_composer_strategy.h"
25 #include "anbox/logger.h"
26 #include "anbox/wm/manager.h"
28 #include <boost/throw_exception.hpp>
29 #include <boost/filesystem.hpp>
33 namespace fs = boost::filesystem;
36 void logger_write(const emugl::LogLevel &level, const char *format, ...) {
42 va_start(args, format);
43 vsnprintf(message, sizeof(message) - 1, format, args);
47 case emugl::LogLevel::WARNING:
48 WARNING("%s", message);
50 case emugl::LogLevel::ERROR:
53 case emugl::LogLevel::FATAL:
56 case emugl::LogLevel::DEBUG:
59 case emugl::LogLevel::TRACE:
70 GLRendererServer::GLRendererServer(const Config &config, const std::shared_ptr<wm::Manager> &wm)
71 : renderer_(std::make_shared<::Renderer>()) {
73 std::shared_ptr<LayerComposer::Strategy> composer_strategy;
74 if (config.single_window)
75 composer_strategy = std::make_shared<SingleWindowComposerStrategy>(wm);
77 composer_strategy = std::make_shared<MultiWindowComposerStrategy>(wm);
79 composer_ = std::make_shared<LayerComposer>(renderer_, composer_strategy);
81 auto gl_libs = emugl::default_gl_libraries();
82 if (config.driver == Config::Driver::Software) {
83 auto swiftshader_path = fs::path(utils::get_env_value("SWIFTSHADER_PATH"));
84 const auto snap_path = utils::get_env_value("SNAP");
85 if (!snap_path.empty())
86 swiftshader_path = fs::path(snap_path) / "lib" / "anbox" / "swiftshader";
87 if (!fs::exists(swiftshader_path))
88 throw std::runtime_error("Software rendering is enabled, but SwiftShader library directory is not found.");
90 gl_libs = std::vector<emugl::GLLibrary>{
91 {emugl::GLLibrary::Type::EGL, (swiftshader_path / "libEGL.so").string()},
92 {emugl::GLLibrary::Type::GLESv1, (swiftshader_path / "libGLES_CM.so").string()},
93 {emugl::GLLibrary::Type::GLESv2, (swiftshader_path / "libGLESv2.so").string()},
97 emugl_logger_struct log_funcs;
98 log_funcs.coarse = logger_write;
99 log_funcs.fine = logger_write;
101 if (!emugl::initialize(gl_libs, &log_funcs, nullptr))
102 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to initialize OpenGL renderer"));
104 renderer_->initialize(0);
106 registerRenderer(renderer_);
107 registerLayerComposer(composer_);
110 GLRendererServer::~GLRendererServer() { renderer_->finalize(); }
111 } // namespace graphics