File: wayland_display_observer.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (258 lines) | stat: -rw-r--r-- 8,511 bytes parent folder | download | duplicates (7)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/wayland_display_observer.h"

#include <chrome-color-management-server-protocol.h>
#include <wayland-server-core.h>
#include <wayland-server-protocol-core.h>
#include <xdg-output-unstable-v1-server-protocol.h>

#include "components/exo/wayland/output_metrics.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/wayland_display_output.h"
#include "components/exo/wayland/zaura_output_manager.h"
#include "components/exo/wayland/zcr_color_manager.h"
#include "ui/display/display_observer.h"
#include "ui/display/screen.h"

using DisplayMetric = display::DisplayObserver::DisplayMetric;

namespace exo {
namespace wayland {

WaylandDisplayObserver::WaylandDisplayObserver() = default;

WaylandDisplayObserver::~WaylandDisplayObserver() = default;

WaylandDisplayHandler::WaylandDisplayHandler(WaylandDisplayOutput* output,
                                             wl_resource* output_resource)
    : output_(output), output_resource_(output_resource) {}

WaylandDisplayHandler::~WaylandDisplayHandler() {
  for (auto& obs : observers_) {
    obs.OnOutputDestroyed();
  }
  if (xdg_output_resource_) {
    wl_resource_set_user_data(xdg_output_resource_, nullptr);
  }
  output_->UnregisterOutput(output_resource_);
}

void WaylandDisplayHandler::Initialize() {
  // Adding itself as an observer will send the initial display metrics.
  AddObserver(this);
  output_->RegisterOutput(output_resource_);
}

void WaylandDisplayHandler::AddObserver(WaylandDisplayObserver* observer) {
  observers_.AddObserver(observer);

  display::Display display;
  bool exists =
      display::Screen::GetScreen()->GetDisplayWithDisplayId(id(), &display);
  if (!exists) {
    // WaylandDisplayHandler is created asynchronously, and the
    // display can be deleted before created. This usually won't happen
    // in real environment, but can happen in test environment.
    return;
  }

  // Send the first round of changes to the observer.
  constexpr uint32_t kAllChanges = 0xFFFFFFFF;
  SendDisplayMetricsChanges(display, kAllChanges);
}

void WaylandDisplayHandler::RemoveObserver(WaylandDisplayObserver* observer) {
  observers_.RemoveObserver(observer);
}

int64_t WaylandDisplayHandler::id() const {
  DCHECK(output_);
  return output_->id();
}

void WaylandDisplayHandler::SendDisplayMetricsChanges(
    const display::Display& display,
    uint32_t changed_metrics) {
  CHECK(output_resource_);
  CHECK_EQ(id(), display.id());

  bool needs_done = false;

  // If supported, the aura_output_manager must have been bound by clients
  // before the wl_output associated with this WaylandDisplayHandler is bound.
  if (auto* output_manager = GetAuraOutputManager()) {
    // This sends all relevant output metrics to clients. These events are sent
    // immediately after the client binds an output and again every time display
    // metrics have changed.
    needs_done |= output_manager->SendOutputMetrics(output_resource_, display,
                                                    changed_metrics);
  }

  for (auto& observer : observers_) {
    needs_done |= observer.SendDisplayMetrics(display, changed_metrics);
  }

  if (needs_done) {
    if (wl_resource_get_version(output_resource_) >=
        WL_OUTPUT_DONE_SINCE_VERSION) {
      wl_output_send_done(output_resource_);
    }
    wl_client_flush(wl_resource_get_client(output_resource_));
  }
}

void WaylandDisplayHandler::SendDisplayActivated() {
  for (auto& observer : observers_) {
    observer.SendActiveDisplay();
  }
}

void WaylandDisplayHandler::OnXdgOutputCreated(
    wl_resource* xdg_output_resource) {
  DCHECK(!xdg_output_resource_);
  xdg_output_resource_ = xdg_output_resource;

  display::Display display;
  if (!display::Screen::GetScreen()->GetDisplayWithDisplayId(id(), &display)) {
    return;
  }

  if (SendXdgOutputMetrics(display, 0xFFFFFFFF)) {
    if (wl_resource_get_version(output_resource_) >=
        WL_OUTPUT_DONE_SINCE_VERSION) {
      wl_output_send_done(output_resource_);
    }
    wl_client_flush(wl_resource_get_client(output_resource_));
  }
}

void WaylandDisplayHandler::UnsetXdgOutputResource() {
  DCHECK(xdg_output_resource_);
  xdg_output_resource_ = nullptr;
}

void WaylandDisplayHandler::XdgOutputSendLogicalPosition(
    const gfx::Point& position) {
  zxdg_output_v1_send_logical_position(xdg_output_resource_, position.x(),
                                       position.y());
}

void WaylandDisplayHandler::XdgOutputSendLogicalSize(const gfx::Size& size) {
  zxdg_output_v1_send_logical_size(xdg_output_resource_, size.width(),
                                   size.height());
}

void WaylandDisplayHandler::XdgOutputSendDescription(const std::string& desc) {
  if (wl_resource_get_version(xdg_output_resource_) <
      ZXDG_OUTPUT_V1_DESCRIPTION_SINCE_VERSION) {
    return;
  }
  zxdg_output_v1_send_description(xdg_output_resource_, desc.c_str());
}

bool WaylandDisplayHandler::SendDisplayMetrics(const display::Display& display,
                                               uint32_t changed_metrics) {
  if (!output_resource_) {
    return false;
  }

  // There is no need to check DISPLAY_METRIC_PRIMARY because when primary
  // changes, bounds always changes. (new primary should have had non
  // 0,0 origin).
  // Only exception is when switching to newly connected primary with
  // the same bounds. This happens when you're in docked mode, suspend,
  // unplug the display, then resume to the internal display which has
  // the same resolution. Since metrics does not change, there is no need
  // to notify clients.

  const OutputMetrics output_metrics(display);
  bool result = false;

  if (changed_metrics & (DisplayMetric::DISPLAY_METRIC_BOUNDS |
                         DisplayMetric::DISPLAY_METRIC_ROTATION |
                         DisplayMetric::DISPLAY_METRIC_REFRESH_RATE)) {
    wl_output_send_geometry(
        output_resource_, output_metrics.origin.x(), output_metrics.origin.y(),
        output_metrics.physical_size_mm.width(),
        output_metrics.physical_size_mm.height(), output_metrics.subpixel,
        output_metrics.make.c_str(), output_metrics.model.c_str(),
        output_metrics.panel_transform);
    wl_output_send_mode(output_resource_, output_metrics.mode_flags,
                        output_metrics.physical_size_px.width(),
                        output_metrics.physical_size_px.height(),
                        output_metrics.refresh_mhz);
    result = true;
  }

  if (changed_metrics & DisplayMetric::DISPLAY_METRIC_DEVICE_SCALE_FACTOR) {
    if (wl_resource_get_version(output_resource_) >=
        WL_OUTPUT_SCALE_SINCE_VERSION) {
      wl_output_send_scale(output_resource_, output_metrics.scale);
      result = true;
    }
  }

  if (SendXdgOutputMetrics(display, changed_metrics)) {
    result = true;
  }

  return result;
}

bool WaylandDisplayHandler::SendXdgOutputMetrics(
    const display::Display& display,
    uint32_t changed_metrics) {
  if (!xdg_output_resource_) {
    return false;
  }

  const OutputMetrics output_metrics(display);
  bool result = false;

  if (changed_metrics & (DisplayMetric::DISPLAY_METRIC_BOUNDS |
                         DisplayMetric::DISPLAY_METRIC_ROTATION |
                         DisplayMetric::DISPLAY_METRIC_DEVICE_SCALE_FACTOR)) {
    XdgOutputSendLogicalPosition(output_metrics.logical_origin);
    XdgOutputSendLogicalSize(output_metrics.logical_size);
    XdgOutputSendDescription(output_metrics.description);
    if (wl_resource_get_version(xdg_output_resource_) < 3) {
      zxdg_output_v1_send_done(xdg_output_resource_);
    }
    result = true;
  }

  return result;
}

void WaylandDisplayHandler::SendActiveDisplay() {
  if (auto* output_manager = GetAuraOutputManager()) {
    output_manager->SendOutputActivated(output_resource_);
  }
}

void WaylandDisplayHandler::OnOutputDestroyed() {
  // destroying itself.
  RemoveObserver(this);
}

AuraOutputManager* WaylandDisplayHandler::GetAuraOutputManager() {
  wl_client* client = wl_resource_get_client(output_resource_);
  CHECK(client);
  return AuraOutputManager::Get(client);
}

size_t WaylandDisplayHandler::CountObserversForTesting() const {
  size_t count = 0;
  for (auto& obs : observers_) {
    if (&obs != this) {
      count++;
    }
  }
  return count;
}

}  // namespace wayland
}  // namespace exo