File: xdg_output.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 4,561 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/wayland/host/xdg_output.h"

#include <xdg-output-unstable-v1-client-protocol.h>

#include "ui/ozone/platform/wayland/common/wayland_util.h"
#include "ui/ozone/platform/wayland/host/wayland_output.h"

namespace ui {

XDGOutput::XDGOutput(zxdg_output_v1* xdg_output) : xdg_output_(xdg_output) {
  // Can be nullptr in tests.
  if (xdg_output_) {
    static constexpr zxdg_output_v1_listener kXdgOutputListener = {
        .logical_position = &OnLogicalPosition,
        .logical_size = &OnLogicalSize,
        .done = &OnDone,
        .name = &OnName,
        .description = &OnDescription,
    };
    zxdg_output_v1_add_listener(xdg_output_.get(), &kXdgOutputListener, this);
  }
}

XDGOutput::~XDGOutput() = default;

bool XDGOutput::IsReady() const {
  return is_ready_;
}

void XDGOutput::HandleDone() {
  // If `logical_size` has been set the server must have propagated all the
  // necessary state events for this xdg_output.
  is_ready_ = !logical_size_.IsEmpty();
}

void XDGOutput::UpdateMetrics(bool compute_scale_from_size,
                              WaylandOutput::Metrics& metrics) {
  if (!IsReady()) {
    return;
  }

  metrics.origin = logical_position_;
  metrics.logical_size = logical_size_;

  // Name is an optional xdg_output event.
  if (!name_.empty()) {
    metrics.name = name_;
  }

  // Description is an optional xdg_output event.
  if (!description_.empty()) {
    metrics.description = description_;
  }

  const gfx::Size logical_size = logical_size_;
  const gfx::Size physical_size = metrics.physical_size;
  DCHECK(!physical_size.IsEmpty());

  // As per xdg-ouput spec, compositors not scaling the monitor viewport in its
  // compositing space will advertise logical size equal to the physical size
  // (coming from current wl_output's mode info), in which case wl_output's
  // scale must be used. Mutter, for example, when running with its logical
  // monitor layout mode disabled, reports the same value for both logical and
  // physical size even for scales other than 1, which is considered
  // spec-compliant and has been similarly worked around in toolkits like GTK.
  // See https://gitlab.gnome.org/GNOME/mutter/-/issues/2631 for more details.
  //
  // TODO(crbug.com/336007385): Deriving display scale from xdg-ouput logical
  // size has been considered hacky and bug-prone, eg: rounding issues, as the
  // rounding algorithm used by compositors is unspecified. wp-fractional-scale
  // should be used instead as the long term solution, though it requires
  // broader refactor in how scales are assigned to browser windows.
  compute_scale_from_size &=
      (!logical_size.IsEmpty() && logical_size != physical_size);
  if (compute_scale_from_size) {
    const float max_physical_side =
        std::max(physical_size.width(), physical_size.height());
    const float max_logical_side =
        std::max(logical_size.width(), logical_size.height());
    // The scale needs to be clamped here in the same way as in other wayland
    // code, e.g. 'WaylandSurface::GetWaylandScale()'.
    metrics.scale_factor = wl::ClampScale(max_physical_side / max_logical_side);
  }
}

// static
void XDGOutput::OnLogicalPosition(void* data,
                                  zxdg_output_v1* output,
                                  int32_t x,
                                  int32_t y) {
  if (auto* self = static_cast<XDGOutput*>(data)) {
    self->logical_position_ = gfx::Point(x, y);
  }
}

// static
void XDGOutput::OnLogicalSize(void* data,
                              zxdg_output_v1* output,
                              int32_t width,
                              int32_t height) {
  if (auto* self = static_cast<XDGOutput*>(data)) {
    self->logical_size_ = gfx::Size(width, height);
  }
}

// static
void XDGOutput::OnDone(void* data, zxdg_output_v1* output) {
  // deprecated since version 3
}

// static
void XDGOutput::OnName(void* data, zxdg_output_v1* output, const char* name) {
  if (auto* self = static_cast<XDGOutput*>(data)) {
    self->name_ = name ? std::string(name) : std::string();
  }
}

// static
void XDGOutput::OnDescription(void* data,
                              zxdg_output_v1* output,
                              const char* description) {
  if (auto* self = static_cast<XDGOutput*>(data)) {
    self->description_ = description ? std::string(description) : std::string();
  }
}

}  // namespace ui