Jonathan Ross | c1d5cc7 | 2022-11-03 22:09:39 | [diff] [blame] | 1 | // 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 | |
| 7 | namespace content { |
| 8 | |
| 9 | ScreenState::ScreenState() = default; |
| 10 | |
| 11 | void 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 | |
| 33 | bool 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 | |
| 40 | bool 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 | |
| 47 | bool 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 | |
| 53 | bool 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 |
| 62 | bool 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 |
| 77 | bool 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 |
| 91 | bool 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 |