Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include "Screen.h"
9#include "Compositor.h"
10#include "Event.h"
11#include "EventLoop.h"
12#include "ScreenBackend.h"
13#include "VirtualScreenBackend.h"
14#include "WindowManager.h"
15#include <AK/Debug.h>
16#include <AK/Format.h>
17#include <Kernel/API/Graphics.h>
18#include <Kernel/API/MousePacket.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <sys/mman.h>
22#include <unistd.h>
23
24namespace WindowServer {
25
26Vector<NonnullRefPtr<Screen>, default_screen_count> Screen::s_screens;
27Screen* Screen::s_main_screen { nullptr };
28Gfx::IntRect Screen::s_bounding_screens_rect {};
29ScreenLayout Screen::s_layout;
30Vector<int, default_scale_factors_in_use_count> Screen::s_scale_factors_in_use;
31
32struct FlushRectData {
33 Vector<FBRect, 32> pending_flush_rects;
34 bool too_many_pending_flush_rects { false };
35};
36
37ScreenInput& ScreenInput::the()
38{
39 static ScreenInput s_the;
40 return s_the;
41}
42
43Screen& ScreenInput::cursor_location_screen()
44{
45 auto* screen = Screen::find_by_location(m_cursor_location);
46 VERIFY(screen);
47 return *screen;
48}
49
50Screen const& ScreenInput::cursor_location_screen() const
51{
52 auto* screen = Screen::find_by_location(m_cursor_location);
53 VERIFY(screen);
54 return *screen;
55}
56
57bool Screen::apply_layout(ScreenLayout&& screen_layout, DeprecatedString& error_msg)
58{
59 if (!screen_layout.is_valid(&error_msg))
60 return false;
61
62 if (screen_layout == s_layout)
63 return true;
64
65 bool place_cursor_on_main_screen = find_by_location(ScreenInput::the().cursor_location()) == nullptr;
66
67 HashMap<size_t, size_t> current_to_new_indices_map;
68 HashMap<size_t, size_t> new_to_current_indices_map;
69 HashMap<size_t, NonnullRefPtr<Screen>> devices_no_longer_used;
70 for (size_t i = 0; i < s_layout.screens.size(); i++) {
71 auto& screen = s_layout.screens[i];
72 bool found = false;
73 for (size_t j = 0; j < screen_layout.screens.size(); j++) {
74 auto& new_screen = screen_layout.screens[j];
75 if (new_screen.device == screen.device) {
76 current_to_new_indices_map.set(i, j);
77 new_to_current_indices_map.set(j, i);
78 found = true;
79 break;
80 }
81 }
82
83 if (!found)
84 devices_no_longer_used.set(i, s_screens[i]);
85 }
86 HashMap<Screen*, size_t> screens_with_resolution_change;
87 HashMap<Screen*, size_t> screens_with_scale_change;
88 for (auto& it : current_to_new_indices_map) {
89 auto& screen = s_layout.screens[it.key];
90 auto& new_screen = screen_layout.screens[it.value];
91 if (screen.resolution != new_screen.resolution)
92 screens_with_resolution_change.set(s_screens[it.key], it.value);
93 if (screen.scale_factor != new_screen.scale_factor)
94 screens_with_scale_change.set(s_screens[it.key], it.value);
95 }
96
97 auto screens_backup = move(s_screens);
98 auto layout_backup = move(s_layout);
99
100 for (auto& it : screens_with_resolution_change) {
101 auto& existing_screen = *it.key;
102 dbgln("Closing device {} in preparation for resolution change", layout_backup.screens[existing_screen.index()].device.value_or("<virtual screen>"));
103 existing_screen.close_device();
104 }
105
106 AK::ArmedScopeGuard rollback([&] {
107 for (auto& screen : s_screens)
108 screen->close_device();
109 s_screens = move(screens_backup);
110 s_layout = move(layout_backup);
111 for (size_t i = 0; i < s_screens.size(); i++) {
112 auto& old_screen = s_screens[i];
113 // Restore the original screen index in case it changed
114 old_screen->set_index(i);
115 if (i == s_layout.main_screen_index)
116 old_screen->make_main_screen();
117 bool changed_scale = screens_with_scale_change.contains(old_screen);
118 if (screens_with_resolution_change.contains(old_screen)) {
119 if (old_screen->open_device()) {
120 // The resolution was changed, so we also implicitly applied the new scale factor
121 changed_scale = false;
122 } else {
123 // Don't set error_msg here, it should already be set
124 dbgln("Rolling back screen layout failed: could not open device");
125 }
126 }
127
128 old_screen->update_virtual_and_physical_rects();
129 if (changed_scale)
130 old_screen->scale_factor_changed();
131 }
132 update_bounding_rect();
133 });
134 s_layout = move(screen_layout);
135 for (size_t index = 0; index < s_layout.screens.size(); index++) {
136 Screen* screen;
137 bool need_to_open_device;
138 if (auto it = new_to_current_indices_map.find(index); it != new_to_current_indices_map.end()) {
139 // Re-use the existing screen instance
140 screen = screens_backup[it->value];
141 s_screens.append(*screen);
142 screen->set_index(index);
143
144 need_to_open_device = screens_with_resolution_change.contains(screen);
145 } else {
146 screen = WindowServer::Screen::create(index);
147 if (!screen) {
148 error_msg = DeprecatedString::formatted("Error creating screen #{}", index);
149 return false;
150 }
151
152 need_to_open_device = false;
153 }
154
155 if (need_to_open_device && !screen->open_device()) {
156 error_msg = DeprecatedString::formatted("Error opening device for screen #{}", index);
157 return false;
158 }
159
160 screen->update_virtual_and_physical_rects();
161 if (!need_to_open_device && screens_with_scale_change.contains(screen))
162 screen->scale_factor_changed();
163
164 VERIFY(screen);
165 VERIFY(index == screen->index());
166
167 if (s_layout.main_screen_index == index)
168 screen->make_main_screen();
169 }
170
171 rollback.disarm();
172
173 if (place_cursor_on_main_screen) {
174 ScreenInput::the().set_cursor_location(Screen::main().rect().center());
175 } else {
176 auto cursor_location = ScreenInput::the().cursor_location();
177 if (!find_by_location(cursor_location)) {
178 // Cursor is off screen, try to find the closest location on another screen
179 float closest_distance = 0;
180 Optional<Gfx::IntPoint> closest_point;
181 for (auto& screen : s_screens) {
182 auto closest_point_on_screen_rect = screen->rect().closest_to(cursor_location);
183 auto distance = closest_point_on_screen_rect.distance_from(cursor_location);
184 if (!closest_point.has_value() || distance < closest_distance) {
185 closest_distance = distance;
186 closest_point = closest_point_on_screen_rect;
187 }
188 }
189 ScreenInput::the().set_cursor_location(closest_point.value()); // We should always have one
190 }
191 }
192
193 update_bounding_rect();
194 update_scale_factors_in_use();
195 return true;
196}
197
198void Screen::update_scale_factors_in_use()
199{
200 s_scale_factors_in_use.clear();
201 for_each([&](auto& screen) {
202 auto scale_factor = screen.scale_factor();
203 // The This doesn't have to be extremely efficient as this
204 // code is only run when we start up or the screen configuration
205 // changes. But using a vector allows for efficient iteration,
206 // which is the most common use case.
207 if (!s_scale_factors_in_use.contains_slow(scale_factor))
208 s_scale_factors_in_use.append(scale_factor);
209 return IterationDecision::Continue;
210 });
211}
212
213Screen::Screen(size_t screen_index)
214 : m_index(screen_index)
215 , m_flush_rects(adopt_own(*new FlushRectData()))
216 , m_compositor_screen_data(Compositor::create_screen_data({}))
217{
218 update_virtual_and_physical_rects();
219 open_device();
220}
221
222Screen::~Screen()
223{
224 close_device();
225}
226
227bool Screen::open_device()
228{
229 close_device();
230 auto& info = screen_layout_info();
231
232 switch (info.mode) {
233 case ScreenLayout::Screen::Mode::Device: {
234 m_backend = make<HardwareScreenBackend>(info.device.value());
235 auto return_value = m_backend->open();
236 if (return_value.is_error()) {
237 dbgln("Screen #{}: Failed to open backend: {}", index(), return_value.error());
238 return false;
239 }
240
241 set_resolution(true);
242 return true;
243 }
244 case ScreenLayout::Screen::Mode::Virtual: {
245 m_backend = make<VirtualScreenBackend>();
246 // Virtual device open should never fail.
247 MUST(m_backend->open());
248
249 set_resolution(true);
250 return true;
251 }
252 default:
253 dbgln("Unsupported screen type {}", ScreenLayout::Screen::mode_to_string(info.mode));
254 return false;
255 }
256}
257
258void Screen::close_device()
259{
260 m_backend = nullptr;
261}
262
263void Screen::update_virtual_and_physical_rects()
264{
265 auto& screen_info = screen_layout_info();
266 m_virtual_rect = { screen_info.location, { screen_info.resolution.width() / screen_info.scale_factor, screen_info.resolution.height() / screen_info.scale_factor } };
267 m_physical_rect = { Gfx::IntPoint { 0, 0 }, { screen_info.resolution.width(), screen_info.resolution.height() } };
268 dbgln("update_virtual_and_physical_rects for screen #{}: {}", index(), m_virtual_rect);
269}
270
271void Screen::scale_factor_changed()
272{
273 // Flush rects are affected by the screen factor
274 constrain_pending_flush_rects();
275}
276
277Screen& Screen::closest_to_rect(Gfx::IntRect const& rect)
278{
279 Screen* best_screen = nullptr;
280 int best_area = 0;
281 for (auto& screen : s_screens) {
282 auto r = screen->rect().intersected(rect);
283 int area = r.width() * r.height();
284 if (!best_screen || area > best_area) {
285 best_screen = screen;
286 best_area = area;
287 }
288 }
289 if (!best_screen) {
290 // TODO: try to find the best screen in close proximity
291 best_screen = &Screen::main();
292 }
293 return *best_screen;
294}
295
296Screen& Screen::closest_to_location(Gfx::IntPoint point)
297{
298 for (auto& screen : s_screens) {
299 if (screen->rect().contains(point))
300 return screen;
301 }
302 // TODO: guess based on how close the point is to the next screen rectangle
303 return Screen::main();
304}
305
306void Screen::update_bounding_rect()
307{
308 if (!s_screens.is_empty()) {
309 s_bounding_screens_rect = s_screens[0]->rect();
310 for (size_t i = 1; i < s_screens.size(); i++)
311 s_bounding_screens_rect = s_bounding_screens_rect.united(s_screens[i]->rect());
312 } else {
313 s_bounding_screens_rect = {};
314 }
315}
316
317bool Screen::set_resolution(bool initial)
318{
319 // Remember the screen that the cursor is on. Make sure it stays on the same screen if we change its resolution...
320 Screen* screen_with_cursor = nullptr;
321 if (!initial)
322 screen_with_cursor = &ScreenInput::the().cursor_location_screen();
323
324 auto& info = screen_layout_info();
325
326 ErrorOr<void> return_value = Error::from_errno(EINVAL);
327 {
328 GraphicsHeadModeSetting requested_mode_setting;
329 memset(&requested_mode_setting, 0, sizeof(GraphicsHeadModeSetting));
330 requested_mode_setting.horizontal_stride = info.resolution.width() * 4;
331 requested_mode_setting.pixel_clock_in_khz = 0;
332 requested_mode_setting.horizontal_active = info.resolution.width();
333 requested_mode_setting.horizontal_front_porch_pixels = 0;
334 requested_mode_setting.horizontal_sync_time_pixels = 0;
335 requested_mode_setting.horizontal_blank_pixels = 0;
336 requested_mode_setting.vertical_active = info.resolution.height();
337 requested_mode_setting.vertical_front_porch_lines = 0;
338 requested_mode_setting.vertical_sync_time_lines = 0;
339 requested_mode_setting.vertical_blank_lines = 0;
340 requested_mode_setting.horizontal_offset = 0;
341 requested_mode_setting.vertical_offset = 0;
342 return_value = m_backend->set_head_mode_setting(requested_mode_setting);
343 }
344
345 dbgln_if(WSSCREEN_DEBUG, "Screen #{}: fb_set_resolution() - success", index());
346
347 auto on_change_resolution = [&]() -> ErrorOr<void> {
348 if (initial) {
349 TRY(m_backend->unmap_framebuffer());
350 TRY(m_backend->map_framebuffer());
351 }
352 auto mode_setting = TRY(m_backend->get_head_mode_setting());
353 info.resolution = { mode_setting.horizontal_active, mode_setting.vertical_active };
354
355 update_virtual_and_physical_rects();
356
357 // Since pending flush rects are affected by the scale factor
358 // update even if only the scale factor changed
359 constrain_pending_flush_rects();
360
361 if (this == screen_with_cursor) {
362 auto& screen_input = ScreenInput::the();
363 screen_input.set_cursor_location(screen_input.cursor_location().constrained(rect()));
364 }
365 return {};
366 };
367
368 if (!return_value.is_error()) {
369 return_value = on_change_resolution();
370 if (!return_value.is_error())
371 return true;
372 }
373 if (return_value.is_error() && return_value.error() != Error::from_errno(EOVERFLOW)) {
374 dbgln("Screen #{}: Failed to set resolution {}: {}", index(), info.resolution, return_value.error());
375 MUST(on_change_resolution());
376 return false;
377 }
378 dbgln("Screen #{}: Failed to set resolution {}: {}, falling back to safe resolution", index(), info.resolution, return_value.error());
379 MUST(m_backend->set_safe_head_mode_setting());
380 MUST(on_change_resolution());
381 return false;
382}
383
384void Screen::set_buffer(int index)
385{
386 m_backend->set_head_buffer(index);
387}
388
389size_t Screen::buffer_offset(int index) const
390{
391 if (index == 0)
392 return 0;
393 if (index == 1)
394 return m_backend->m_back_buffer_offset;
395 VERIFY_NOT_REACHED();
396}
397
398void ScreenInput::set_acceleration_factor(double factor)
399{
400 VERIFY(factor >= mouse_accel_min && factor <= mouse_accel_max);
401 m_acceleration_factor = factor;
402}
403
404void ScreenInput::set_scroll_step_size(unsigned step_size)
405{
406 VERIFY(step_size >= scroll_step_size_min);
407 m_scroll_step_size = step_size;
408}
409
410void ScreenInput::on_receive_mouse_data(MousePacket const& packet)
411{
412 auto& current_screen = cursor_location_screen();
413 auto prev_location = m_cursor_location;
414 if (packet.is_relative) {
415 m_cursor_location.translate_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
416 dbgln_if(WSSCREEN_DEBUG, "Screen: New Relative mouse point @ {}", m_cursor_location);
417 } else {
418 m_cursor_location = { packet.x * current_screen.width() / 0xffff, packet.y * current_screen.height() / 0xffff };
419 dbgln_if(WSSCREEN_DEBUG, "Screen: New Absolute mouse point @ {}", m_cursor_location);
420 }
421
422 auto* moved_to_screen = Screen::find_by_location(m_cursor_location);
423 if (!moved_to_screen) {
424 m_cursor_location = m_cursor_location.constrained(current_screen.rect());
425 moved_to_screen = ¤t_screen;
426 }
427
428 unsigned buttons = packet.buttons;
429 unsigned prev_buttons = m_mouse_button_state;
430 m_mouse_button_state = buttons;
431 unsigned changed_buttons = prev_buttons ^ buttons;
432 auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
433 if (!(changed_buttons & (unsigned)button))
434 return;
435 auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, m_cursor_location, buttons, button, m_modifiers);
436 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
437 };
438 post_mousedown_or_mouseup_if_needed(MouseButton::Primary);
439 post_mousedown_or_mouseup_if_needed(MouseButton::Secondary);
440 post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
441 post_mousedown_or_mouseup_if_needed(MouseButton::Backward);
442 post_mousedown_or_mouseup_if_needed(MouseButton::Forward);
443 if (m_cursor_location != prev_location) {
444 auto message = make<MouseEvent>(Event::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
445 if (WindowManager::the().dnd_client())
446 message->set_mime_data(WindowManager::the().dnd_mime_data());
447 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
448 }
449
450 if (packet.z || packet.w) {
451 auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.w * m_scroll_step_size, packet.z * m_scroll_step_size, packet.w, packet.z);
452 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
453 }
454
455 if (m_cursor_location != prev_location)
456 Compositor::the().invalidate_cursor();
457}
458
459void ScreenInput::on_receive_keyboard_data(::KeyEvent kernel_event)
460{
461 m_modifiers = kernel_event.modifiers();
462 auto message = make<KeyEvent>(kernel_event.is_press() ? Event::KeyDown : Event::KeyUp, kernel_event.key, kernel_event.code_point, kernel_event.modifiers(), kernel_event.scancode);
463 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
464}
465
466void Screen::constrain_pending_flush_rects()
467{
468 auto& flush_rects = *m_flush_rects;
469 if (flush_rects.pending_flush_rects.is_empty())
470 return;
471 Gfx::IntRect screen_rect({}, rect().size());
472 Gfx::DisjointIntRectSet rects;
473 for (auto& fb_rect : flush_rects.pending_flush_rects) {
474 Gfx::IntRect rect { (int)fb_rect.x, (int)fb_rect.y, (int)fb_rect.width, (int)fb_rect.height };
475 auto intersected_rect = rect.intersected(screen_rect);
476 if (!intersected_rect.is_empty())
477 rects.add(intersected_rect);
478 }
479 flush_rects.pending_flush_rects.clear_with_capacity();
480 for (auto const& rect : rects.rects()) {
481 flush_rects.pending_flush_rects.append({
482 .head_index = 0,
483 .x = (unsigned)rect.x(),
484 .y = (unsigned)rect.y(),
485 .width = (unsigned)rect.width(),
486 .height = (unsigned)rect.height(),
487 });
488 }
489}
490
491void Screen::queue_flush_display_rect(Gfx::IntRect const& flush_region)
492{
493 // NOTE: we don't scale until in Screen::flush_display so that when
494 // there are too many rectangles that we end up throwing away, we didn't
495 // waste accounting for scale factor!
496 auto& flush_rects = *m_flush_rects;
497 if (flush_rects.too_many_pending_flush_rects) {
498 // We already have too many, just make sure we extend it if needed
499 VERIFY(!flush_rects.pending_flush_rects.is_empty());
500 if (flush_rects.pending_flush_rects.size() == 1) {
501 auto& union_rect = flush_rects.pending_flush_rects[0];
502 auto new_union = flush_region.united(Gfx::IntRect((int)union_rect.x, (int)union_rect.y, (int)union_rect.width, (int)union_rect.height));
503 union_rect.x = new_union.left();
504 union_rect.y = new_union.top();
505 union_rect.width = new_union.width();
506 union_rect.height = new_union.height();
507 } else {
508 // Convert all the rectangles into one union
509 auto new_union = flush_region;
510 for (auto& flush_rect : flush_rects.pending_flush_rects)
511 new_union = new_union.united(Gfx::IntRect((int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height));
512 flush_rects.pending_flush_rects.resize(1, true);
513 auto& union_rect = flush_rects.pending_flush_rects[0];
514 union_rect.x = new_union.left();
515 union_rect.y = new_union.top();
516 union_rect.width = new_union.width();
517 union_rect.height = new_union.height();
518 }
519 return;
520 }
521 VERIFY(flush_rects.pending_flush_rects.size() < flush_rects.pending_flush_rects.capacity());
522 flush_rects.pending_flush_rects.append({ 0,
523 (unsigned)flush_region.left(),
524 (unsigned)flush_region.top(),
525 (unsigned)flush_region.width(),
526 (unsigned)flush_region.height() });
527 if (flush_rects.pending_flush_rects.size() == flush_rects.pending_flush_rects.capacity()) {
528 // If we get one more rectangle then we need to convert it to a single union rectangle
529 flush_rects.too_many_pending_flush_rects = true;
530 }
531}
532
533void Screen::flush_display(int buffer_index)
534{
535 VERIFY(m_backend->m_can_device_flush_buffers || m_backend->m_can_device_flush_entire_framebuffer);
536 auto& flush_rects = *m_flush_rects;
537 if (flush_rects.pending_flush_rects.is_empty())
538 return;
539
540 // Now that we have a final set of rects, apply the scale factor
541 auto scale_factor = this->scale_factor();
542 for (auto& flush_rect : flush_rects.pending_flush_rects) {
543 VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains({ (int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height }));
544 flush_rect.x *= scale_factor;
545 flush_rect.y *= scale_factor;
546 flush_rect.width *= scale_factor;
547 flush_rect.height *= scale_factor;
548 }
549
550 if (m_backend->m_can_device_flush_entire_framebuffer) {
551 auto return_value = m_backend->flush_framebuffer();
552 if (return_value.is_error())
553 dbgln("Screen #{}: Error flushing display: {}", index(), return_value.error());
554 } else {
555 auto return_value = m_backend->flush_framebuffer_rects(buffer_index, flush_rects.pending_flush_rects.span());
556 if (return_value.is_error())
557 dbgln("Screen #{}: Error flushing display: {}", index(), return_value.error());
558 }
559
560 flush_rects.too_many_pending_flush_rects = false;
561 flush_rects.pending_flush_rects.clear_with_capacity();
562}
563
564void Screen::flush_display_entire_framebuffer()
565{
566 VERIFY(m_backend->m_can_device_flush_entire_framebuffer);
567 auto return_value = m_backend->flush_framebuffer();
568 if (return_value.is_error())
569 dbgln("Screen #{}: Error flushing display front buffer: {}", index(), return_value.error());
570}
571
572void Screen::flush_display_front_buffer(int front_buffer_index, Gfx::IntRect& rect)
573{
574 VERIFY(m_backend->m_can_device_flush_buffers);
575 auto scale_factor = this->scale_factor();
576 FBRect flush_rect {
577 .head_index = 0,
578 .x = (unsigned)(rect.x() * scale_factor),
579 .y = (unsigned)(rect.y() * scale_factor),
580 .width = (unsigned)(rect.width() * scale_factor),
581 .height = (unsigned)(rect.height() * scale_factor)
582 };
583
584 VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains(rect));
585
586 auto return_value = m_backend->flush_framebuffer_rects(front_buffer_index, { &flush_rect, 1 });
587 if (return_value.is_error())
588 dbgln("Screen #{}: Error flushing display front buffer: {}", index(), return_value.error());
589}
590
591}