TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / gl_renderer_server.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
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"
27
28 #include <boost/throw_exception.hpp>
29 #include <boost/filesystem.hpp>
30 #include <cstdarg>
31 #include <stdexcept>
32
33 namespace fs = boost::filesystem;
34
35 namespace {
36 void logger_write(const emugl::LogLevel &level, const char *format, ...) {
37   (void)level;
38
39   char message[2048];
40   va_list args;
41
42   va_start(args, format);
43   vsnprintf(message, sizeof(message) - 1, format, args);
44   va_end(args);
45
46   switch (level) {
47   case emugl::LogLevel::WARNING:
48     WARNING("%s", message);
49     break;
50   case emugl::LogLevel::ERROR:
51     ERROR("%s", message);
52     break;
53   case emugl::LogLevel::FATAL:
54     FATAL("%s", message);
55     break;
56   case emugl::LogLevel::DEBUG:
57     DEBUG("%s", message);
58     break;
59   case emugl::LogLevel::TRACE:
60     TRACE("%s", message);
61     break;
62   default:
63     break;
64   }
65 }
66 }
67
68 namespace anbox {
69 namespace graphics {
70 GLRendererServer::GLRendererServer(const Config &config, const std::shared_ptr<wm::Manager> &wm)
71     : renderer_(std::make_shared<::Renderer>()) {
72
73   std::shared_ptr<LayerComposer::Strategy> composer_strategy;
74   if (config.single_window)
75     composer_strategy = std::make_shared<SingleWindowComposerStrategy>(wm);
76   else
77     composer_strategy = std::make_shared<MultiWindowComposerStrategy>(wm);
78
79   composer_ = std::make_shared<LayerComposer>(renderer_, composer_strategy);
80
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.");
89
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()},
94     };
95   }
96
97   emugl_logger_struct log_funcs;
98   log_funcs.coarse = logger_write;
99   log_funcs.fine = logger_write;
100
101   if (!emugl::initialize(gl_libs, &log_funcs, nullptr))
102     BOOST_THROW_EXCEPTION(std::runtime_error("Failed to initialize OpenGL renderer"));
103
104   renderer_->initialize(0);
105
106   registerRenderer(renderer_);
107   registerLayerComposer(composer_);
108 }
109
110 GLRendererServer::~GLRendererServer() { renderer_->finalize(); }
111 }  // namespace graphics
112 }  // namespace anbox