Skip to content

Commit

Permalink
Re-enable display scale observing for msd::BasicManager and
Browse files Browse the repository at this point in the history
`miral::DecorationBasicManager`.
  • Loading branch information
tarek-y-ismail committed Nov 20, 2024
1 parent d2f21f7 commit e70dc0b
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 56 deletions.
41 changes: 19 additions & 22 deletions examples/miral-shell/decoration/decoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,7 @@ auto UserDecoration::InputManager::buttons() const -> std::vector<std::shared_pt

void UserDecoration::render_titlebar(Renderer::Buffer const& titlebar_buffer)
{
fill_solid_color(titlebar_buffer, current_titlebar_color);

auto const draw_button =
[&]( geometry::Rectangle button_rect, Renderer::Pixel color)
{
auto start = button_rect.top_left;
auto end = button_rect.bottom_right();
for (auto y = start.y; y < end.y; y += geometry::DeltaY{1})
Renderer::render_row(titlebar_buffer, {start.x, y}, geometry::as_width(end.x - start.x), color);
};
Renderer::fill_solid_color(titlebar_buffer, current_titlebar_color);

auto buttons = input_manager.buttons();
for (auto const& button : buttons)
Expand All @@ -85,15 +76,11 @@ void UserDecoration::render_titlebar(Renderer::Buffer const& titlebar_buffer)
if (should_highlight)
color += 0x00444444;

draw_button(button_rect, color);
}
}

void UserDecoration::fill_solid_color(Renderer::Buffer const& left_border_buffer, Renderer::Pixel color)
{
for (geometry::Y y{0}; y < as_y(left_border_buffer.size().height); y += geometry::DeltaY{1})
{
Renderer::render_row(left_border_buffer, {0, y}, left_border_buffer.size().width, color);
// The buffers are automatically scaled with different display scales.
// `fill_solid_color` and `render_rect` use the scaled size to properly fill the buffer.
//
// Anything you draw yourself should be properly scaled with size.
Renderer::render_rect_scaled(titlebar_buffer, button_rect, scale, color);
}
}

Expand Down Expand Up @@ -370,6 +357,11 @@ void UserDecoration::focused()
current_titlebar_color = focused_titlebar_color;
}

void UserDecoration::scale_changed(float new_scale)
{
scale = new_scale;
}

auto UserDecoration::create_manager(mir::Server& server)
-> std::shared_ptr<miral::DecorationManagerAdapter>
{
Expand Down Expand Up @@ -398,15 +390,15 @@ auto UserDecoration::create_manager(mir::Server& server)
},
[decoration](auto const& left_border_buffer)
{
decoration->fill_solid_color(left_border_buffer, decoration->current_titlebar_color);
Renderer::fill_solid_color(left_border_buffer, decoration->current_titlebar_color);
},
[decoration](auto const& right_border_buffer)
{
decoration->fill_solid_color(right_border_buffer, decoration->current_titlebar_color);
Renderer::fill_solid_color(right_border_buffer, decoration->current_titlebar_color);
},
[decoration](auto const& bottom_border_buffer)
{
decoration->fill_solid_color(bottom_border_buffer, decoration->current_titlebar_color);
Renderer::fill_solid_color(bottom_border_buffer, decoration->current_titlebar_color);
},
[decoration](auto... args)
{
Expand Down Expand Up @@ -467,6 +459,11 @@ auto UserDecoration::create_manager(mir::Server& server)
{
decoration->redraw_notifier()->notify();
},
[decoration](auto new_scale)
{
decoration->scale_changed(new_scale);
decoration->redraw_notifier()->notify();
},
[decoration](auto window_state)
{
decoration->input_manager.update_window_state(*window_state);
Expand Down
6 changes: 2 additions & 4 deletions examples/miral-shell/decoration/decoration.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class UserDecoration : public miral::Decoration

using Renderer = miral::decoration::Renderer;
void render_titlebar(Renderer::Buffer const& titlebar_buffer);
void fill_solid_color(Renderer::Buffer const& left_border_buffer, Renderer::Pixel color);

static auto create_manager(mir::Server&)
-> std::shared_ptr<miral::DecorationManagerAdapter>;
Expand Down Expand Up @@ -129,12 +128,11 @@ class UserDecoration : public miral::Decoration
void focused();
void unfocused();

void minimized();
void maximized();
void normal();
void scale_changed(float new_scale);

InputManager input_manager;
static Renderer::Pixel const focused_titlebar_color = 0xFF323232;
static Renderer::Pixel const unfocused_titlebar_color = 0xFF525252;
Renderer::Pixel current_titlebar_color = focused_titlebar_color;
float scale = 1.0;
};
26 changes: 25 additions & 1 deletion include/miral/miral/decoration_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ using OnWindowResized =
std::function<void(ms::Surface const* window_surface, mir::geometry::Size const& /*window_size*/)>;
using OnWindowRenamed = std::function<void(ms::Surface const* window_surface, std::string const& name)>;
using OnWindowStateUpdated = std::function<void(std::shared_ptr<miral::decoration::WindowState>)>;
using OnScaleChanged = std::function<void(float)>;

struct InputContext
{
Expand Down Expand Up @@ -167,16 +168,26 @@ class Renderer
Renderer::RenderingCallback render_right_border,
Renderer::RenderingCallback render_bottom_border);

static void render_row(Buffer const& buffer, geometry::Point left, geometry::Width length, uint32_t color);
static void fill_solid_color(Renderer::Buffer const& left_border_buffer, Renderer::Pixel color);

// Renders in display-scale-space, takes into account the display scale
static void render_rect_scaled(Buffer const& buffer, geometry::Rectangle unscaled_rect, float scale, Pixel color);

void update_state(miral::decoration::WindowState const& window_state);
auto streams_to_spec(std::shared_ptr<miral::decoration::WindowState> const& window_state) const
-> msh::SurfaceSpecification;
void update_render_submit(std::shared_ptr<miral::decoration::WindowState> const& window_state);

void scale_changed(float new_scale);
auto scale() const -> float;

private:
auto make_graphics_buffer(Buffer const&) -> std::optional<std::shared_ptr<mg::Buffer>>;

// Renders in buffer pixel-scale-space, independent of the actual display scale
static void render_row_unscaled(Buffer const& buffer, geometry::Point left, geometry::Width length, uint32_t color);


std::shared_ptr<ms::Session> const session;
std::shared_ptr<mg::GraphicBufferAllocator> const buffer_allocator;

Expand All @@ -189,26 +200,39 @@ class Renderer
RenderingCallback const render_left_border;
RenderingCallback const render_right_border;
RenderingCallback const render_bottom_border;

float scale_ = 1.0;
};

struct DecorationAdapter
{
DecorationAdapter(
// Notifies Mir that decorations should be redrawn
std::shared_ptr<DecorationRedrawNotifier> const& redraw_notifier,

// Called by user code in response to input events
Renderer::RenderingCallback render_titlebar,
Renderer::RenderingCallback render_left_border,
Renderer::RenderingCallback render_right_border,
Renderer::RenderingCallback render_bottom_border,

// Called by Mir to inform user code of input events
OnProcessEnter process_enter,
OnProcessLeave process_leave,
OnProcessDown process_down,
OnProcessUp process_up,
OnProcessMove process_move,
OnProcessDrag process_drag,

// Called by Mir to inform user code of changes to the window being decorated
OnWindowAttribChanged attrib_changed,
OnWindowResized window_resized_to,
OnWindowRenamed window_renamed,

// Called by Mir to inform user code that the display scale changed
OnScaleChanged scale_changed,

// Called whenever the internal WindowState is updated
OnWindowStateUpdated update_decoration_window_state);

void set_custom_geometry(std::shared_ptr<miral::decoration::StaticGeometry> geometry);
Expand Down
2 changes: 1 addition & 1 deletion src/include/server/mir/shell/decoration/basic_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BasicManager :
std::shared_ptr<scene::Surface> const& surface)>;

BasicManager(
/* mir::ObserverRegistrar<mir::graphics::DisplayConfigurationObserver>& display_configuration_observers, */
mir::ObserverRegistrar<mir::graphics::DisplayConfigurationObserver>& display_configuration_observers,
DecorationBuilder&& decoration_builder);
~BasicManager();

Expand Down
57 changes: 51 additions & 6 deletions src/miral/decoration_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ auto md::Renderer::Buffer::stream() const -> std::shared_ptr<mc::BufferStream>
return stream_;
}

void md::Renderer::render_row(Buffer const& buffer, geometry::Point left, geometry::Width length, uint32_t color)
void md::Renderer::render_row_unscaled(Buffer const& buffer, geometry::Point left, geometry::Width length, uint32_t color)
{
auto buf_size = buffer.size();
if (left.y < geometry::Y{} || left.y >= as_y(buf_size.height))
Expand All @@ -246,6 +246,26 @@ void md::Renderer::render_row(Buffer const& buffer, geometry::Point left, geomet
*i = color;
}

void md::Renderer::fill_solid_color(Renderer::Buffer const& left_border_buffer, Renderer::Pixel color)
{
for (geometry::Y y{0}; y < as_y(left_border_buffer.size().height); y += geometry::DeltaY{1})
{
Renderer::render_row_unscaled(left_border_buffer, {0, y}, left_border_buffer.size().width, color);
}
}

void md::Renderer::render_rect_scaled(Buffer const& buffer, geometry::Rectangle unscaled_rect, float scale, Pixel color)
{
auto tl = unscaled_rect.top_left;
auto start = mir::geometry::Point{tl.x.as_value() * scale, tl.y.as_value() * scale};

auto br = unscaled_rect.bottom_right();
auto end = mir::geometry::Point{br.x.as_value() * scale, br.y.as_value() * scale};

for (auto y = start.y; y < end.y; y += geometry::DeltaY{1})
Renderer::render_row_unscaled(buffer, {start.x, y}, geometry::as_width(end.x - start.x), color);
}

auto md::Renderer::make_graphics_buffer(Buffer const& buffer)
-> std::optional<std::shared_ptr<mg::Buffer>>
{
Expand Down Expand Up @@ -274,10 +294,11 @@ auto md::Renderer::make_graphics_buffer(Buffer const& buffer)
void md::Renderer::update_state(md::WindowState const& window_state)
{
auto const conditional_resize =
[](auto& buffer, auto const& rect)
[scale = this->scale_](auto& buffer, auto const& rect)
{
if (rect.size != buffer.size())
buffer.resize(rect.size);
auto scaled_rect_size = rect.size * scale;
if (scaled_rect_size != buffer.size())
buffer.resize(scaled_rect_size);
};

conditional_resize(titlebar_buffer, window_state.titlebar_rect());
Expand Down Expand Up @@ -356,7 +377,7 @@ void md::Renderer::update_render_submit(std::shared_ptr<md::WindowState> const&
break;
}

float inv_scale = 1.0f; // 1.0f / window_state->scale();
float inv_scale = 1.0f / scale_;
for (auto const& [stream, buffer_opt] : new_buffers)
{
if (buffer_opt)
Expand All @@ -367,6 +388,16 @@ void md::Renderer::update_render_submit(std::shared_ptr<md::WindowState> const&
}
}

void md::Renderer::scale_changed(float new_scale)
{
scale_ = new_scale;
}

auto md::Renderer::scale() const -> float
{
return scale_;
}

class WindowSurfaceObserver : public ms::NullSurfaceObserver
{
public:
Expand Down Expand Up @@ -437,6 +468,7 @@ struct md::DecorationAdapter::Impl : public msd::Decoration
OnWindowAttribChanged attrib_changed,
OnWindowResized window_resized_to,
OnWindowRenamed window_renamed,
OnScaleChanged scale_changed,
OnWindowStateUpdated update_decoration_window_state) :
redraw_notifier_{redraw_notifier},
render_titlebar{render_titlebar},
Expand Down Expand Up @@ -468,6 +500,7 @@ struct md::DecorationAdapter::Impl : public msd::Decoration
window_renamed(args...);
on_update_decoration_window_state(window_state);
}},
on_scale_changed{scale_changed},
geometry{std::make_shared<md::StaticGeometry>(default_geometry)}
{
}
Expand All @@ -487,7 +520,7 @@ struct md::DecorationAdapter::Impl : public msd::Decoration

auto redraw_notifier() { return redraw_notifier_; }

void set_scale(float) override {}
void set_scale(float new_scale) override;

~Impl();

Expand Down Expand Up @@ -519,6 +552,7 @@ struct md::DecorationAdapter::Impl : public msd::Decoration
OnWindowAttribChanged on_attrib_changed;
OnWindowResized on_window_resized_to;
OnWindowRenamed on_window_renamed;
OnScaleChanged on_scale_changed;

std::shared_ptr<md::StaticGeometry> geometry;
};
Expand All @@ -538,6 +572,7 @@ md::DecorationAdapter::DecorationAdapter(
OnWindowAttribChanged attrib_changed,
OnWindowResized window_resized_to,
OnWindowRenamed window_renamed,
OnScaleChanged scale_changed,
OnWindowStateUpdated update_decoration_window_state) :
impl{std::make_shared<Impl>(
redraw_notifier,
Expand All @@ -554,6 +589,7 @@ md::DecorationAdapter::DecorationAdapter(
attrib_changed,
window_resized_to,
window_renamed,
scale_changed,
update_decoration_window_state)}
{
}
Expand Down Expand Up @@ -677,6 +713,15 @@ void md::DecorationAdapter::Impl::window_state_updated(std::shared_ptr<ms::Surfa
window_state = std::make_shared<md::WindowState>(geometry, window_surface.get());
}

void md::DecorationAdapter::Impl::set_scale(float new_scale)
{
if(new_scale == renderer->scale())
return;

renderer->scale_changed(new_scale);
on_scale_changed(new_scale);
}

md::DecorationAdapter::DecorationAdapter::Impl::~Impl()
{
window_surface->unregister_interest(*window_surface_observer);
Expand Down
3 changes: 2 additions & 1 deletion src/miral/decoration_basic_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ struct miral::DecorationBasicManager::Self : public mir::shell::decoration::Basi
{
Self(mir::Server& server, Decoration::DecorationBuilder&& decoration_builder) :
mir::shell::decoration::BasicManager(
[&server,decoration_builder](auto shell, auto window_surface)
*server.the_display_configuration_observer_registrar(),
[&server, decoration_builder](auto shell, auto window_surface)
{
auto session = window_surface->session().lock();
auto decoration_surface = create_surface(window_surface, shell);
Expand Down
6 changes: 6 additions & 0 deletions src/miral/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,12 @@ global:
miral::decoration::Renderer::area*;
miral::decoration::Renderer::buffer_format*;
miral::decoration::Renderer::bytes_per_pixel*;
miral::decoration::Renderer::fill_solid_color*;
miral::decoration::Renderer::render_rect_scaled*;
miral::decoration::Renderer::render_row*;
miral::decoration::Renderer::render_row_unscaled*;
miral::decoration::Renderer::scale*;
miral::decoration::Renderer::scale_changed*;
miral::decoration::Renderer::streams_to_spec*;
miral::decoration::Renderer::update_render_submit*;
miral::decoration::Renderer::update_state*;
Expand All @@ -590,6 +595,7 @@ global:
miral::decoration::WindowState::left_border_rect*;
miral::decoration::WindowState::resize_corner_input_size*;
miral::decoration::WindowState::right_border_rect*;
miral::decoration::WindowState::scale*;
miral::decoration::WindowState::side_border_height*;
miral::decoration::WindowState::side_border_width*;
miral::decoration::WindowState::titlebar_height*;
Expand Down
Loading

0 comments on commit e70dc0b

Please sign in to comment.