Skip to content

Commit

Permalink
Add Set|GetBoundsIndip to PlatformWindow
Browse files Browse the repository at this point in the history
and use it where makes sense. ozone-wayland still uses pixel
coordinates internally, and that'll be addressed in next CL.

A few notable changes.
1) Added virtual WindowTreeHost::GetRootWindowBounds() method
 This bounds should come from platform window on ozone platform.
2) Added virtual WindowTreeHost::GetBoundsInDIP()
  to unify the way to obtain the DP coordinates of the WTH.
  (see DefaultScreenPositionClient::GetRootWindowOriginInScreen)
3) Split the DesktopWindowTreeHost::GetBoundsInDIP() to
  win and ozone impl.
4) Changed DesktopWindowTreeHostPlatform::GetRootTransform to
 use WTH's device_scale_factor() which is already available. This also
 caused circular dependency when I unified conversion because GetDisplayNearestRootWindow needs to compute bounds on X11.

This also removes unused WTH::IntializeDeviceScaleFactor

Bug: 1306688
Test: There should be no functional change. All test should pass.

Change-Id: I8b6f69a033bd2dcbfaea5106404547df870d4756
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3628272
Reviewed-by: Scott Violet <sky@chromium.org>
Commit-Queue: Mitsuru Oshima <oshima@chromium.org>
Reviewed-by: Maksim Sisov <msisov@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1002577}
  • Loading branch information
mitoshima authored and Chromium LUCI CQ committed May 12, 2022
1 parent a416be1 commit 960b728
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 63 deletions.
21 changes: 13 additions & 8 deletions ui/aura/window_tree_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ void WindowTreeHost::Hide() {
OnAcceleratedWidgetMadeVisible(false);
}

gfx::Rect WindowTreeHost::GetBoundsInDIP() const {
aura::Window* root_window = const_cast<aura::Window*>(window());
display::Screen* screen = display::Screen::GetScreen();
gfx::Rect screen_bounds = GetBoundsInPixels();
return screen->ScreenToDIPRectInWindow(root_window, screen_bounds);
}

gfx::Rect WindowTreeHost::GetBoundsInAcceleratedWidgetPixelCoordinates() {
return gfx::Rect(GetBoundsInPixels().size());
}
Expand Down Expand Up @@ -458,9 +465,12 @@ void WindowTreeHost::UpdateRootWindowSize() {
bool compositor_inited = !!compositor()->root_layer();
ScopedLocalSurfaceIdValidator lsi_validator(compositor_inited ? window()
: nullptr);
gfx::Rect transformed_bounds_in_dp =
GetTransformedRootWindowBoundsFromPixelSize(GetBoundsInPixels().size());
window()->SetBounds(transformed_bounds_in_dp);
window()->SetBounds(CalculateRootWindowBounds());
}

gfx::Rect WindowTreeHost::CalculateRootWindowBounds() const {
return GetTransformedRootWindowBoundsFromPixelSize(
GetBoundsInPixels().size());
}

std::unique_ptr<ScopedEnableUnadjustedMouseEvents>
Expand Down Expand Up @@ -532,11 +542,6 @@ WindowTreeHost::WindowTreeHost(std::unique_ptr<Window> window)
#endif
}

void WindowTreeHost::IntializeDeviceScaleFactor(float device_scale_factor) {
DCHECK(!compositor_->root_layer()) << "Only call this before InitHost()";
device_scale_factor_ = device_scale_factor;
}

void WindowTreeHost::UpdateCompositorVisibility(bool visible) {
if (!compositor())
return;
Expand Down
10 changes: 6 additions & 4 deletions ui/aura/window_tree_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class AURA_EXPORT WindowTreeHost : public ui::internal::InputMethodDelegate,
virtual void SetBoundsInPixels(const gfx::Rect& bounds_in_pixels) = 0;
virtual gfx::Rect GetBoundsInPixels() const = 0;

// Gets the bounds in DIP.
virtual gfx::Rect GetBoundsInDIP() const;

// Returns the bounds relative to the accelerated widget. In the typical case,
// the origin is 0,0 and the size is the same as the pixel-bounds. On some
// OSs the bounds may be inset (on Windows, this is referred to as the client
Expand Down Expand Up @@ -312,10 +315,6 @@ class AURA_EXPORT WindowTreeHost : public ui::internal::InputMethodDelegate,

explicit WindowTreeHost(std::unique_ptr<Window> window = nullptr);

// Set the cached display device scale factor. This should only be called
// during subclass initialization, when the value is needed before InitHost().
void IntializeDeviceScaleFactor(float device_scale_factor);

// All calls to changing the visibility of the Compositor funnel into this.
// In addition to changing the visibility this may also evict the root frame.
void UpdateCompositorVisibility(bool visible);
Expand Down Expand Up @@ -395,6 +394,9 @@ class AURA_EXPORT WindowTreeHost : public ui::internal::InputMethodDelegate,
// Updates the root window's size after WindowTreeHost's property changed.
void UpdateRootWindowSize();

// Calculates the root window bounds to be used by UpdateRootwindowSize().
virtual gfx::Rect CalculateRootWindowBounds() const;

private:
class HideHelper;

Expand Down
11 changes: 11 additions & 0 deletions ui/ozone/platform/drm/host/drm_window_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ gfx::Rect DrmWindowHost::GetBounds() const {
return bounds_;
}

void DrmWindowHost::SetBoundsInDIP(const gfx::Rect& bounds) {
NOTREACHED();
// No scaling at DRM level and should always use pixel bounds.
}

gfx::Rect DrmWindowHost::GetBoundsInDIP() const {
// No scaling at DRM level and should always use pixel bounds.
NOTREACHED();
return bounds_;
}

void DrmWindowHost::SetTitle(const std::u16string& title) {}

void DrmWindowHost::SetCapture() {
Expand Down
2 changes: 2 additions & 0 deletions ui/ozone/platform/drm/host/drm_window_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class DrmWindowHost : public PlatformWindow,
void PrepareForShutdown() override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetTitle(const std::u16string& title) override;
void SetCapture() override;
void ReleaseCapture() override;
Expand Down
9 changes: 9 additions & 0 deletions ui/ozone/platform/flatland/flatland_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ void FlatlandWindow::SetBounds(const gfx::Rect& bounds) {
bounds_ = bounds;
}

gfx::Rect FlatlandWindow::GetBoundsInDIP() const {
return window_delegate_->ConvertRectToDIP(bounds_);
}

void FlatlandWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
// This path should only be reached in tests.
bounds_ = window_delegate_->ConvertRectToPixels(bounds);
}

void FlatlandWindow::SetTitle(const std::u16string& title) {
NOTIMPLEMENTED();
}
Expand Down
2 changes: 2 additions & 0 deletions ui/ozone/platform/flatland/flatland_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class COMPONENT_EXPORT(OZONE) FlatlandWindow : public PlatformWindow,
// PlatformWindow implementation.
gfx::Rect GetBounds() const override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
void SetTitle(const std::u16string& title) override;
void Show(bool inactive) override;
void Hide() override;
Expand Down
9 changes: 9 additions & 0 deletions ui/ozone/platform/scenic/scenic_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ void ScenicWindow::SetBounds(const gfx::Rect& bounds) {
bounds_ = bounds;
}

gfx::Rect ScenicWindow::GetBoundsInDIP() const {
return delegate_->ConvertRectToDIP(bounds_);
}

void ScenicWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
// This path should only be reached in tests.
bounds_ = delegate_->ConvertRectToPixels(bounds);
}

void ScenicWindow::SetTitle(const std::u16string& title) {
NOTIMPLEMENTED_LOG_ONCE();
}
Expand Down
2 changes: 2 additions & 0 deletions ui/ozone/platform/scenic/scenic_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class COMPONENT_EXPORT(OZONE) ScenicWindow final : public PlatformWindow,
// ui::PlatformWindow implementation.
gfx::Rect GetBounds() const override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
void SetTitle(const std::u16string& title) override;
void Show(bool inactive) override;
void Hide() override;
Expand Down
13 changes: 8 additions & 5 deletions ui/ozone/platform/wayland/host/wayland_screen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,25 @@ gfx::Point WaylandScreen::GetCursorScreenPoint() const {
// wl_shell/xdg-shell do not provide either location of surfaces in global
// space coordinate system or location of a pointer. Instead, only locations
// of mouse/touch events are known. Given that Chromium assumes top-level
// windows are located at origin, always provide a cursor point in regards
// to surfaces' location.
// windows are located at origin when screen coordinates is not available,
// always provide a cursor point in regards to surfaces' location.
//
// If a pointer is located in any of the existing wayland windows, return
// the last known cursor position. Otherwise, return such a point, which is
// not contained by any of the windows.
// the last known cursor position.
auto* cursor_position = connection_->wayland_cursor_position();
if (connection_->wayland_window_manager()
->GetCurrentPointerOrTouchFocusedWindow() &&
cursor_position)
return cursor_position->GetCursorSurfacePoint();

// Make sure the cursor position does not overlap with any window by using the
// outside of largest window bounds.
// TODO(oshima): Change this for the case that screen coordinates is
// available.
auto* window =
connection_->wayland_window_manager()->GetWindowWithLargestBounds();
DCHECK(window);
const gfx::Rect bounds = window->GetBounds();
const gfx::Rect bounds = window->GetBoundsInDIP();
return gfx::Point(bounds.width() + 10, bounds.height() + 10);
}

Expand Down
30 changes: 29 additions & 1 deletion ui/ozone/platform/wayland/host/wayland_screen_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ class TestDisplayObserver : public display::DisplayObserver {
display::Display removed_display_;
};

class MockWaylandPlatformWindowDelegate : public MockPlatformWindowDelegate {
public:
MockWaylandPlatformWindowDelegate() = default;
MockWaylandPlatformWindowDelegate(const MockWaylandPlatformWindowDelegate&) =
delete;
MockWaylandPlatformWindowDelegate operator=(
const MockWaylandPlatformWindowDelegate&) = delete;
~MockWaylandPlatformWindowDelegate() override = default;

gfx::Rect ConvertRectToPixels(const gfx::Rect& rect_in_dp) const override {
float scale = wayland_window_ ? wayland_window_->window_scale() : 1.0f;
return gfx::ScaleToEnclosingRect(rect_in_dp, scale);
}

gfx::Rect ConvertRectToDIP(const gfx::Rect& rect_in_pixels) const override {
float scale = wayland_window_ ? wayland_window_->window_scale() : 1.0f;
return gfx::ScaleToEnclosedRect(rect_in_pixels, 1.0f / scale);
}

void set_wayland_window(WaylandWindow* wayland_window) {
wayland_window_ = wayland_window;
}

private:
WaylandWindow* wayland_window_ = nullptr;
};

} // namespace

class WaylandScreenTest : public WaylandTest {
Expand Down Expand Up @@ -352,14 +379,15 @@ TEST_P(WaylandScreenTest, GetAcceleratedWidgetAtScreenPoint) {
gfx::Point(window_bounds.width() + 1, window_bounds.height() + 1));
EXPECT_EQ(widget_at_screen_point, gfx::kNullAcceleratedWidget);

MockPlatformWindowDelegate delegate;
MockWaylandPlatformWindowDelegate delegate;
auto menu_window_bounds =
gfx::Rect(window_->GetBounds().width() - 10,
window_->GetBounds().height() - 10, 100, 100);
std::unique_ptr<WaylandWindow> menu_window =
CreateWaylandWindowWithProperties(menu_window_bounds,
PlatformWindowType::kMenu,
window_->GetWidget(), &delegate);
delegate.set_wayland_window(menu_window.get());

Sync();

Expand Down
6 changes: 5 additions & 1 deletion ui/ozone/platform/wayland/host/wayland_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ gfx::Rect WaylandWindow::GetBounds() const {
}

gfx::Rect WaylandWindow::GetBoundsInDIP() const {
return gfx::ScaleToRoundedRect(bounds_px_, 1.0f / window_scale());
return delegate_->ConvertRectToDIP(bounds_px_);
}

void WaylandWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
SetBounds(delegate_->ConvertRectToPixels(bounds));
}

void WaylandWindow::OnSurfaceConfigureEvent() {
Expand Down
7 changes: 3 additions & 4 deletions ui/ozone/platform/wayland/host/wayland_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class WaylandWindow : public PlatformWindow,
bool IsVisible() const override;
void PrepareForShutdown() override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() const override;
void SetTitle(const std::u16string& title) override;
void SetCapture() override;
Expand Down Expand Up @@ -308,9 +310,6 @@ class WaylandWindow : public PlatformWindow,
return ui_task_runner_;
}

// Returns bounds in DIP.
gfx::Rect GetBoundsInDIP() const;

base::WeakPtr<WaylandWindow> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
Expand All @@ -326,7 +325,7 @@ class WaylandWindow : public PlatformWindow,
const WaylandConnection* connection() const { return connection_; }
PlatformWindowDelegate* delegate() { return delegate_; }

// Sets bounds in dip.
// [Deprecatd] Sets bounds in dip. This will be replaced with SetBoundsInDIP.
void SetBoundsDip(const gfx::Rect& bounds_dip);

void set_ui_scale(float ui_scale) { ui_scale_ = ui_scale; }
Expand Down
2 changes: 1 addition & 1 deletion ui/ozone/platform/wayland/host/wayland_window_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ WaylandWindow* WaylandWindowManager::GetWindowWithLargestBounds() const {
continue;
}
WaylandWindow* window = entry.second;
if (window_with_largest_bounds->GetBounds() < window->GetBounds())
if (window_with_largest_bounds->GetBoundsInDIP() < window->GetBoundsInDIP())
window_with_largest_bounds = window;
}
return window_with_largest_bounds;
Expand Down
7 changes: 2 additions & 5 deletions ui/ozone/platform/x11/x11_screen_ozone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ display::Display X11ScreenOzone::GetDisplayForAcceleratedWidget(
return GetPrimaryDisplay();

X11Window* window = window_manager_->GetWindow(widget);
if (window) {
gfx::Rect bounds_dip = gfx::ToEnclosingRect(
gfx::ConvertRectToDips(window->GetBounds(), GetXDisplayScaleFactor()));
return GetDisplayMatching(bounds_dip);
}
if (window)
return GetDisplayMatching(window->GetBoundsInDIP());
return GetPrimaryDisplay();
}

Expand Down
9 changes: 9 additions & 0 deletions ui/ozone/platform/x11/x11_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ui/ozone/platform/x11/x11_window.h"

#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
Expand Down Expand Up @@ -536,6 +537,14 @@ gfx::Rect X11Window::GetBounds() const {
return bounds_in_pixels_;
}

void X11Window::SetBoundsInDIP(const gfx::Rect& bounds_in_dip) {
SetBounds(platform_window_delegate_->ConvertRectToPixels(bounds_in_dip));
}

gfx::Rect X11Window::GetBoundsInDIP() const {
return platform_window_delegate_->ConvertRectToDIP(bounds_in_pixels_);
}

void X11Window::SetTitle(const std::u16string& title) {
if (window_title_ == title)
return;
Expand Down
2 changes: 2 additions & 0 deletions ui/ozone/platform/x11/x11_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class X11Window : public PlatformWindow,
void PrepareForShutdown() override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetTitle(const std::u16string& title) override;
void SetCapture() override;
void ReleaseCapture() override;
Expand Down
11 changes: 10 additions & 1 deletion ui/platform_window/platform_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@ class COMPONENT_EXPORT(PLATFORM_WINDOW) PlatformWindow
virtual void PrepareForShutdown() = 0;

// Sets and gets the bounds of the platform-window. Note that the bounds is in
// physical pixel coordinates.
// physical pixel coordinates. The implementation should use
// `PlatformWindowDelegate::ConvertRectToPixels|DIP` if conversion is
// necessary.
virtual void SetBounds(const gfx::Rect& bounds) = 0;
virtual gfx::Rect GetBounds() const = 0;

// Sets and gets the bounds of the platform-window. Note that the bounds is in
// device-independent-pixel (dip) coordinates. The implementation should use
// `PlatformWindowDelegate::ConvertRectToPixels|DIP` if conversion is
// necessary.
virtual void SetBoundsInDIP(const gfx::Rect& bounds) = 0;
virtual gfx::Rect GetBoundsInDIP() const = 0;

virtual void SetTitle(const std::u16string& title) = 0;

virtual void SetCapture() = 0;
Expand Down
8 changes: 8 additions & 0 deletions ui/platform_window/stub/stub_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ gfx::Rect StubWindow::GetBounds() const {
return bounds_;
}

void StubWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
SetBounds(delegate_->ConvertRectToPixels(bounds));
}

gfx::Rect StubWindow::GetBoundsInDIP() const {
return delegate_->ConvertRectToDIP(bounds_);
}

void StubWindow::SetTitle(const std::u16string& title) {}

void StubWindow::SetCapture() {}
Expand Down
2 changes: 2 additions & 0 deletions ui/platform_window/stub/stub_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class STUB_WINDOW_EXPORT StubWindow : public PlatformWindow {
void PrepareForShutdown() override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetTitle(const std::u16string& title) override;
void SetCapture() override;
void ReleaseCapture() override;
Expand Down
10 changes: 10 additions & 0 deletions ui/platform_window/win/win_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ gfx::Rect WinWindow::GetBounds() const {
return gfx::Rect(cr);
}

void WinWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
// SetBounds should not be used on Windows tests.
NOTREACHED();
}
gfx::Rect WinWindow::GetBoundsInDIP() const {
// GetBounds should not be used on Windows tests.
NOTREACHED();
return GetBounds();
}

void WinWindow::SetTitle(const std::u16string& title) {
SetWindowText(hwnd(), base::as_wcstr(title));
}
Expand Down
2 changes: 2 additions & 0 deletions ui/platform_window/win/win_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class WIN_WINDOW_EXPORT WinWindow : public PlatformWindow,
void PrepareForShutdown() override;
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() const override;
void SetBoundsInDIP(const gfx::Rect& bounds) override;
gfx::Rect GetBoundsInDIP() const override;
void SetTitle(const std::u16string& title) override;
void SetCapture() override;
void ReleaseCapture() override;
Expand Down

0 comments on commit 960b728

Please sign in to comment.