blob: 8510ebdecd57713c343f5ac0a68f12757dd9bfff [file] [log] [blame]
Jonathan Rossc1d5cc72022-11-03 22:09:391// Copyright 2022 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/renderer_host/screen_state.h"
6
7namespace content {
8
9ScreenState::ScreenState() = default;
10
11void ScreenState::CopyDefinedAttributes(const ScreenState& other) {
12 if (!other.visible_viewport_size.IsEmpty())
13 visible_viewport_size = other.visible_viewport_size;
14 if (!other.physical_backing_size.IsEmpty())
15 physical_backing_size = other.physical_backing_size;
16 if (!other.screen_info_size.IsEmpty())
17 screen_info_size = other.screen_info_size;
18 if (other.orientation_type != display::mojom::ScreenOrientation::kUndefined)
19 orientation_type = other.orientation_type;
20 has_unlocked_orientation_lock = other.has_unlocked_orientation_lock;
21 is_expecting_fullscreen_rotation = other.is_expecting_fullscreen_rotation;
22 is_fullscreen = other.is_fullscreen;
23 is_picture_in_picture = other.is_picture_in_picture;
24 on_physical_backing_changed_received =
25 other.on_physical_backing_changed_received;
26 on_sync_display_properties_changed_received =
27 other.on_sync_display_properties_changed_received;
28 any_non_rotation_size_changed = other.any_non_rotation_size_changed;
29 if (other.local_surface_id.is_valid())
30 local_surface_id = other.local_surface_id;
31}
32
33bool ScreenState::EqualOrientations(const ScreenState& other) {
34 return !IsRotation(visible_viewport_size, other.visible_viewport_size) &&
35 !IsRotation(physical_backing_size, other.physical_backing_size) &&
36 !ExpectsResizeForOrientationChange(orientation_type,
37 other.orientation_type);
38}
39
40bool ScreenState::IsRotated(const ScreenState& other) {
41 return IsRotation(visible_viewport_size, other.visible_viewport_size) &&
42 IsRotation(physical_backing_size, other.physical_backing_size) &&
43 ExpectsResizeForOrientationChange(orientation_type,
44 other.orientation_type);
45}
46
47bool ScreenState::IsValid() {
48 return !visible_viewport_size.IsEmpty() && !physical_backing_size.IsEmpty() &&
49 !screen_info_size.IsEmpty() &&
50 orientation_type != display::mojom::ScreenOrientation::kUndefined;
51}
52
53bool ScreenState::EqualVisualProperties(const ScreenState& other) const {
54 return visible_viewport_size == other.visible_viewport_size &&
55 physical_backing_size == other.physical_backing_size &&
56 screen_info_size == other.screen_info_size &&
57 orientation_type == other.orientation_type &&
58 is_fullscreen == other.is_fullscreen;
59}
60
61// static
62bool ScreenState::IsRotation(const gfx::Size& old_size,
63 const gfx::Size& new_size) {
64 // The size change can sometimes include both the rotation and top-controls
65 // adjustments at the same time. So we can't rely on it being a direct swap.
66 if (old_size.width() > old_size.height() &&
67 new_size.width() < new_size.height()) {
68 return true;
69 } else if (old_size.width() < old_size.height() &&
70 new_size.width() > new_size.height()) {
71 return true;
72 }
73 return false;
74}
75
76// static
77bool ScreenState::IsSingleAxisResize(const gfx::Size& old_size,
78 const gfx::Size& new_size) {
79 if (old_size.width() == new_size.width() &&
80 old_size.height() != new_size.height()) {
81 return true;
82 }
83 if (old_size.height() == new_size.height() &&
84 old_size.width() != new_size.width()) {
85 return true;
86 }
87 return false;
88}
89
90// static
91bool ScreenState::ExpectsResizeForOrientationChange(
92 display::mojom::ScreenOrientation current,
93 display::mojom::ScreenOrientation pending) {
94 switch (current) {
95 case display::mojom::ScreenOrientation::kUndefined:
96 return false;
97 case display::mojom::ScreenOrientation::kPortraitPrimary:
98 case display::mojom::ScreenOrientation::kPortraitSecondary:
99 return pending == display::mojom::ScreenOrientation::kLandscapePrimary ||
100 pending == display::mojom::ScreenOrientation::kLandscapeSecondary;
101 case display::mojom::ScreenOrientation::kLandscapePrimary:
102 case display::mojom::ScreenOrientation::kLandscapeSecondary:
103 return pending == display::mojom::ScreenOrientation::kPortraitPrimary ||
104 pending == display::mojom::ScreenOrientation::kPortraitSecondary;
105 }
106 return false;
107}
108
109} // namespace content