Skip to content

Add initial support for HighDPI on Wayland #314

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

Merged
merged 1 commit into from
Dec 30, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ enum FlutterDesktopViewRotation {

// Properties for configuring a Flutter view instance.
typedef struct {
// View width.
// View width in logical pixels.
int width;

// View height.
// View height in logical pixels.
int height;

// View rotation setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class ELinuxWindow {
virtual bool IsValid() const = 0;

// Get current window width in physical pixels.
uint32_t GetCurrentWidth() const { return view_properties_.width; }
uint32_t GetCurrentWidth() const {
return view_properties_.width * current_scale_;
}

// Get current window height in physical pixels.
uint32_t GetCurrentHeight() const { return view_properties_.height; }
uint32_t GetCurrentHeight() const {
return view_properties_.height * current_scale_;
}

void SetRotation(FlutterDesktopViewRotation rotation) {
if (rotation == FlutterDesktopViewRotation::kRotation_90) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ const xdg_toplevel_listener ELinuxWindowWayland::kXdgToplevelListener = {
self->view_properties_.width = next_width;
self->view_properties_.height = next_height;
if (self->window_decorations_) {
self->window_decorations_->Resize(next_width, next_height);
self->window_decorations_->Resize(next_width, next_height,
self->current_scale_);
}
if (self->binding_handler_delegate_) {
self->binding_handler_delegate_->OnWindowSizeChanged(
next_width,
next_height - self->WindowDecorationsPhysicalHeight());
next_width * self->current_scale_,
next_height * self->current_scale_ -
self->WindowDecorationsPhysicalHeight());
}
},
.close =
Expand Down Expand Up @@ -313,11 +315,11 @@ const wl_pointer_listener ELinuxWindowWayland::kWlPointerListener = {
}

if (self->binding_handler_delegate_) {
double x = wl_fixed_to_double(surface_x);
double y = wl_fixed_to_double(surface_y);
self->binding_handler_delegate_->OnPointerMove(x, y);
self->pointer_x_ = x;
self->pointer_y_ = y;
double x_px = wl_fixed_to_double(surface_x) * self->current_scale_;
double y_px = wl_fixed_to_double(surface_y) * self->current_scale_;
self->binding_handler_delegate_->OnPointerMove(x_px, y_px);
self->pointer_x_ = x_px;
self->pointer_y_ = y_px;
}
},
.leave = [](void* data,
Expand Down Expand Up @@ -347,11 +349,11 @@ const wl_pointer_listener ELinuxWindowWayland::kWlPointerListener = {
wl_fixed_t surface_y) -> void {
auto self = reinterpret_cast<ELinuxWindowWayland*>(data);
if (self->binding_handler_delegate_) {
double x = wl_fixed_to_double(surface_x);
double y = wl_fixed_to_double(surface_y);
self->binding_handler_delegate_->OnPointerMove(x, y);
self->pointer_x_ = x;
self->pointer_y_ = y;
double x_px = wl_fixed_to_double(surface_x) * self->current_scale_;
double y_px = wl_fixed_to_double(surface_y) * self->current_scale_;
self->binding_handler_delegate_->OnPointerMove(x_px, y_px);
self->pointer_x_ = x_px;
self->pointer_y_ = y_px;
}
},
.button = [](void* data,
Expand Down Expand Up @@ -604,7 +606,10 @@ const wl_output_listener ELinuxWindowWayland::kWlOutputListener = {
self->view_properties_.height = height;

if (self->window_decorations_) {
self->window_decorations_->Resize(width, height);
int32_t width_dip = width / self->current_scale_;
int32_t height_dip = height / self->current_scale_;
self->window_decorations_->Resize(width_dip, height_dip,
self->current_scale_);
}

if (self->binding_handler_delegate_) {
Expand Down Expand Up @@ -1158,8 +1163,8 @@ bool ELinuxWindowWayland::CreateRenderSurface(int32_t width_px,
}

if (view_properties_.view_mode == FlutterDesktopViewMode::kFullscreen) {
width_px = view_properties_.width;
height_px = view_properties_.height;
width_px = view_properties_.width * current_scale_;
height_px = view_properties_.height * current_scale_;
}

ELINUX_LOG(TRACE) << "Created the Wayland surface: " << width_px << "x"
Expand Down Expand Up @@ -1192,6 +1197,7 @@ bool ELinuxWindowWayland::CreateRenderSurface(int32_t width_px,
xdg_toplevel_ = xdg_surface_get_toplevel(xdg_surface_);
xdg_toplevel_set_title(xdg_toplevel_, "Flutter");
xdg_toplevel_add_listener(xdg_toplevel_, &kXdgToplevelListener, this);
wl_surface_set_buffer_scale(native_window_->Surface(), current_scale_);
wl_surface_commit(native_window_->Surface());

{
Expand All @@ -1211,9 +1217,11 @@ bool ELinuxWindowWayland::CreateRenderSurface(int32_t width_px,
render_surface_->SetNativeWindow(native_window_.get());

if (view_properties_.use_window_decoration) {
int32_t width_dip = width_px / current_scale_;
int32_t height_dip = height_px / current_scale_;
window_decorations_ = std::make_unique<WindowDecorationsWayland>(
wl_display_, wl_compositor_, wl_subcompositor_,
native_window_->Surface(), width_px, height_px);
native_window_->Surface(), width_dip, height_dip, current_scale_);
}

return true;
Expand Down Expand Up @@ -1604,10 +1612,18 @@ void ELinuxWindowWayland::UpdateWindowScale() {
ELINUX_LOG(TRACE) << "Window scale has changed: " << scale_factor;
this->current_scale_ = scale_factor;

wl_surface_set_buffer_scale(native_window_->Surface(), current_scale_);

if (this->window_decorations_) {
this->window_decorations_->Resize(this->view_properties_.width,
this->view_properties_.height,
this->current_scale_);
}

if (this->binding_handler_delegate_) {
this->binding_handler_delegate_->OnWindowSizeChanged(
this->view_properties_.width,
this->view_properties_.height -
this->view_properties_.width * this->current_scale_,
this->view_properties_.height * this->current_scale_ -
this->WindowDecorationsPhysicalHeight());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ void NativeWindowWaylandDecoration::SetPosition(const int32_t x_dip,
wl_subsurface_set_position(subsurface_, x_dip, y_dip);
}

void NativeWindowWaylandDecoration::SetScaleFactor(float scale_factor) {
if (!valid_) {
ELINUX_LOG(ERROR) << "Failed to set the scale factor of the window.";
return;
}

wl_surface_set_buffer_scale(surface_, scale_factor);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class NativeWindowWaylandDecoration : public NativeWindow {
// |NativeWindow|
void SetPosition(const int32_t x_dip, const int32_t y_dip) override;

// Sets the scale factor for the next commit. Scale factor persists until a
// new one is set.
void SetScaleFactor(float scale_factor);

wl_surface* Surface() const { return surface_; }

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class WindowDecoration {
// @param[in] height_px Physical height of the window.
virtual void Resize(const size_t width_px, const size_t height_px) = 0;

// Sets the scale factor for the next commit. Scale factor persists until a
// new one is set.
virtual void SetScaleFactor(float scale_factor) = 0;

void DestroyContext() const { render_surface_->DestroyContext(); };

wl_surface* Surface() const { return native_window_->Surface(); };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ void WindowDecorationButton::Resize(const size_t width_px,
render_surface_->Resize(width_px, height_px);
}

void WindowDecorationButton::SetScaleFactor(float scale_factor) {
native_window_->SetScaleFactor(scale_factor);
}

void WindowDecorationButton::LoadShader() {
if (shader_) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class WindowDecorationButton : public WindowDecoration {
// |WindowDecoration|
void Resize(const size_t width_px, const size_t height_px) override;

// |WindowDecoration|
void SetScaleFactor(float scale_factor) override;

private:
void LoadShader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ void WindowDecorationTitlebar::Resize(const size_t width_px,
render_surface_->Resize(width_px, height_px);
}

void WindowDecorationTitlebar::SetScaleFactor(float scale_factor) {
native_window_->SetScaleFactor(scale_factor);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class WindowDecorationTitlebar : public WindowDecoration {

// |WindowDecoration|
void Resize(const size_t width_px, const size_t height_px) override;

// |WindowDecoration|
void SetScaleFactor(float scale_factor) override;
};

} // namespace flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ WindowDecorationsWayland::WindowDecorationsWayland(
wl_subcompositor* subcompositor,
wl_surface* root_surface,
int32_t width_dip,
int32_t height_dip) {
int32_t height_dip,
double pixel_ratio) {
constexpr bool sub_egl_display = true;

// title-bar.
titlebar_ = std::make_unique<WindowDecorationTitlebar>(
std::make_unique<NativeWindowWaylandDecoration>(compositor, subcompositor,
root_surface, width_dip,
kTitleBarHeightDIP),
std::make_unique<NativeWindowWaylandDecoration>(
compositor, subcompositor, root_surface, width_dip * pixel_ratio,
kTitleBarHeightDIP * pixel_ratio),
std::make_unique<SurfaceDecoration>(std::make_unique<ContextEgl>(
std::make_unique<EnvironmentEgl>(display, sub_egl_display))));
titlebar_->SetPosition(0, -kTitleBarHeightDIP);
Expand All @@ -40,8 +41,8 @@ WindowDecorationsWayland::WindowDecorationsWayland(
buttons_.push_back(std::make_unique<WindowDecorationButton>(
type,
std::make_unique<NativeWindowWaylandDecoration>(
compositor, subcompositor, root_surface, kButtonWidthDIP,
kButtonHeightDIP),
compositor, subcompositor, root_surface,
kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio),
std::make_unique<SurfaceDecoration>(std::make_unique<ContextEgl>(
std::make_unique<EnvironmentEgl>(display, sub_egl_display)))));
buttons_[type]->SetPosition(
Expand All @@ -53,8 +54,8 @@ WindowDecorationsWayland::WindowDecorationsWayland(
buttons_.push_back(std::make_unique<WindowDecorationButton>(
type,
std::make_unique<NativeWindowWaylandDecoration>(
compositor, subcompositor, root_surface, kButtonWidthDIP,
kButtonHeightDIP),
compositor, subcompositor, root_surface,
kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio),
std::make_unique<SurfaceDecoration>(std::make_unique<ContextEgl>(
std::make_unique<EnvironmentEgl>(display, sub_egl_display)))));
buttons_[type]->SetPosition(
Expand All @@ -66,8 +67,8 @@ WindowDecorationsWayland::WindowDecorationsWayland(
buttons_.push_back(std::make_unique<WindowDecorationButton>(
type,
std::make_unique<NativeWindowWaylandDecoration>(
compositor, subcompositor, root_surface, kButtonWidthDIP,
kButtonHeightDIP),
compositor, subcompositor, root_surface,
kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio),
std::make_unique<SurfaceDecoration>(std::make_unique<ContextEgl>(
std::make_unique<EnvironmentEgl>(display, sub_egl_display)))));
buttons_[type]->SetPosition(
Expand All @@ -91,15 +92,19 @@ void WindowDecorationsWayland::Draw() {
}

void WindowDecorationsWayland::Resize(const int32_t width_dip,
const int32_t height_dip) {
const int32_t height_dip,
double pixel_ratio) {
titlebar_->SetScaleFactor(pixel_ratio);
titlebar_->SetPosition(0, -kTitleBarHeightDIP);
titlebar_->Resize(width_dip, kTitleBarHeightDIP);
titlebar_->Resize(width_dip * pixel_ratio, kTitleBarHeightDIP * pixel_ratio);

for (auto i = 0; i < buttons_.size(); i++) {
buttons_[i]->SetScaleFactor(pixel_ratio);
buttons_[i]->SetPosition(
width_dip - kButtonWidthDIP * (i + 1) - kButtonMarginDIP * (i + 1),
-(kButtonHeightDIP + (kTitleBarHeightDIP - kButtonHeightDIP) / 2));
buttons_[i]->Resize(kButtonWidthDIP, kButtonHeightDIP);
buttons_[i]->Resize(kButtonWidthDIP * pixel_ratio,
kButtonHeightDIP * pixel_ratio);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ class WindowDecorationsWayland {
public:
// @param[in] width_dip Logical width of the window (i.e. surface width).
// @param[in] height_dip Logical height of the window (i.e. surface height).
// @param[in] pixel_ratio Physical / logical pixels ratio.
WindowDecorationsWayland(wl_display* display,
wl_compositor* compositor,
wl_subcompositor* subcompositor,
wl_surface* root_surface,
int32_t width_dip,
int32_t height_dip);
int32_t height_dip,
double pixel_ratio);
~WindowDecorationsWayland();

void Draw();

// @param[in] width_dip Logical width of the window (i.e. surface width).
// @param[in] height_dip Logical height of the window (i.e. surface height).
void Resize(const int32_t width_dip, const int32_t height_dip);
// @param[in] pixel_ratio Physical / logical pixels ratio.
void Resize(const int32_t width_dip,
const int32_t height_dip,
double pixel_ratio);

bool IsMatched(wl_surface* surface,
WindowDecoration::DecorationType decoration_type) const;
Expand Down