Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: providing a gateway for writing your own renderer through MirAL #3338

Merged
merged 9 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions debian/libmiral7.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ libmiral.so.7 libmiral7 #MINVER#
(c++)"miral::CursorTheme::CursorTheme(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)@MIRAL_5.0" 5.0.0
(c++)"miral::CursorTheme::operator()(mir::Server&) const@MIRAL_5.0" 5.0.0
(c++)"miral::CursorTheme::~CursorTheme()@MIRAL_5.0" 5.0.0
(c++)"miral::CustomRenderer::CustomRenderer(std::function<std::unique_ptr<mir::renderer::Renderer, std::default_delete<mir::renderer::Renderer> > (std::unique_ptr<mir::graphics::gl::OutputSurface, std::default_delete<mir::graphics::gl::OutputSurface> >, std::shared_ptr<mir::graphics::GLRenderingProvider>)>&&)@MIRAL_5.0" 5.0.0
(c++)"miral::CustomRenderer::operator()(mir::Server&) const@MIRAL_5.0" 5.0.0
(c++)"miral::DisplayConfiguration::DisplayConfiguration(miral::DisplayConfiguration const&)@MIRAL_5.0" 5.0.0
(c++)"miral::DisplayConfiguration::DisplayConfiguration(miral::MirRunner const&)@MIRAL_5.0" 5.0.0
(c++)"miral::DisplayConfiguration::add_output_attribute(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)@MIRAL_5.0" 5.0.0
Expand Down
59 changes: 59 additions & 0 deletions include/miral/miral/custom_renderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIRAL_CUSTOM_RENDERER_H
#define MIRAL_CUSTOM_RENDERER_H

#include <memory>
#include <functional>

namespace mir
{
class Server;
namespace graphics
{
class GLRenderingProvider;
namespace gl
{
class OutputSurface;
}
}
namespace renderer
{
class Renderer;
}
}

namespace miral
{

class CustomRenderer
{
public:
using Builder = std::function<
std::unique_ptr<mir::renderer::Renderer>(
std::unique_ptr<mir::graphics::gl::OutputSurface>, std::shared_ptr<mir::graphics::GLRenderingProvider>)
>;

explicit CustomRenderer(Builder&& renderer);
void operator()(mir::Server& server) const;
private:
struct Self;
std::shared_ptr<Self> self;
};
}

#endif //MIRAL_CUSTOM_RENDERER_H
10 changes: 10 additions & 0 deletions src/include/server/mir/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ namespace input
{
class SeatObserver;
}
namespace renderer
{
class RendererFactory;
}

class Fd;
class MainLoop;
Expand Down Expand Up @@ -294,6 +298,9 @@ class Server
/// Sets an override functor for creating the persistent_surface_store
void override_the_persistent_surface_store(Builder<shell::PersistentSurfaceStore> const& persistent_surface_store);

/// Sets an override functor for the renderer_factory
void override_the_renderer_factory(Builder<renderer::RendererFactory> const& renderer_factory_builder);

/// Each of the wrap functions takes a wrapper functor of the same form
template<typename T> using Wrapper = std::function<std::shared_ptr<T>(std::shared_ptr<T> const&)>;

Expand Down Expand Up @@ -421,6 +428,9 @@ class Server
auto the_session_lock() const ->
std::shared_ptr<scene::SessionLock>;

auto the_renderer_factory() const ->
std::shared_ptr<renderer::RendererFactory>;

/** @} */

/** @name Client side support
Expand Down
2 changes: 2 additions & 0 deletions src/miral/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include_directories(
${PROJECT_SOURCE_DIR}/src/include/server
${PROJECT_SOURCE_DIR}/include/renderer
)

set(MIRAL_ABI 7)
Expand Down Expand Up @@ -54,6 +55,7 @@ add_library(miral SHARED
configuration_option.cpp ${miral_include}/miral/configuration_option.h
${miral_include}/miral/command_line_option.h
cursor_theme.cpp ${miral_include}/miral/cursor_theme.h
custom_renderer.cpp ${miral_include}/miral/custom_renderer.h
display_configuration.cpp ${miral_include}/miral/display_configuration.h
external_client.cpp ${miral_include}/miral/external_client.h
keymap.cpp ${miral_include}/miral/keymap.h
Expand Down
49 changes: 49 additions & 0 deletions src/miral/custom_renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "miral/custom_renderer.h"
#include <mir/server.h>
#include <mir/renderer/renderer_factory.h>
#include <mir/renderer/renderer.h>
#include <mir/renderer/gl/gl_surface.h>

struct miral::CustomRenderer::Self : public mir::renderer::RendererFactory
{
explicit Self(Builder&& renderer)
: renderer(std::move(renderer))
{
}
[[nodiscard]] auto create_renderer_for(
std::unique_ptr<mir::graphics::gl::OutputSurface> output_surface,
std::shared_ptr<mir::graphics::GLRenderingProvider> gl_provider) const
-> std::unique_ptr<mir::renderer::Renderer> override
{
return renderer(std::move(output_surface), std::move(gl_provider));
}
private:
miral::CustomRenderer::Builder const renderer;
};

miral::CustomRenderer::CustomRenderer(Builder&& renderer)
: self{std::make_shared<Self>(std::move(renderer))}
{
}

void miral::CustomRenderer::operator()(mir::Server& server) const
{
std::function<std::shared_ptr<mir::renderer::RendererFactory>()> builder = [self=self] { return self; };
server.override_the_renderer_factory(builder);
}
4 changes: 4 additions & 0 deletions src/miral/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ global:
miral::CursorTheme::?CursorTheme*;
miral::CursorTheme::CursorTheme*;
miral::CursorTheme::operator*;
miral::CustomRenderer::?CustomRenderer*;
miral::CustomRenderer::CustomRenderer*;
miral::CustomRenderer::operator*;
miral::DisplayConfiguration::?DisplayConfiguration*;
miral::DisplayConfiguration::DisplayConfiguration*;
miral::DisplayConfiguration::add_output_attribute*;
Expand Down Expand Up @@ -414,6 +417,7 @@ global:
typeinfo?for?miral::CanonicalWindowManagerPolicy;
typeinfo?for?miral::ConfigurationOption;
typeinfo?for?miral::CursorTheme;
typeinfo?for?miral::CustomRenderer;
typeinfo?for?miral::DisplayConfiguration;
typeinfo?for?miral::ExternalClientLauncher;
typeinfo?for?miral::FdHandle;
Expand Down
6 changes: 4 additions & 2 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ struct TemporaryCompositeEventFilter : public mi::CompositeEventFilter
MACRO(session_listener)\
MACRO(shell)\
MACRO(application_not_responding_detector)\
MACRO(persistent_surface_store)
MACRO(persistent_surface_store)\
MACRO(renderer_factory)

#define FOREACH_ACCESSOR(MACRO)\
MACRO(the_buffer_stream_factory)\
Expand Down Expand Up @@ -131,7 +132,8 @@ struct TemporaryCompositeEventFilter : public mi::CompositeEventFilter
MACRO(the_persistent_surface_store)\
MACRO(the_display_configuration_observer_registrar)\
MACRO(the_seat_observer_registrar)\
MACRO(the_session_lock)
MACRO(the_session_lock)\
MACRO(the_renderer_factory)

#define MIR_SERVER_BUILDER(name)\
std::function<std::invoke_result_t<decltype(&mir::DefaultServerConfiguration::the_##name),mir::DefaultServerConfiguration*>()> name##_builder;
Expand Down
2 changes: 2 additions & 0 deletions src/server/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ MIR_SERVER_2.17 {
mir::Server::override_the_session_listener*;
mir::Server::override_the_shell*;
mir::Server::override_the_window_manager_builder*;
mir::Server::the_renderer_factory*;
mir::Server::override_the_renderer_factory*;
mir::Server::run*;
mir::Server::set_command_line*;
mir::Server::set_command_line_handler*;
Expand Down
Loading