Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
Daniel Libby | adeca89 | 2020-06-05 20:03:40 | [diff] [blame] | 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/display_feature.h" |
| 6 | |
| 7 | namespace content { |
| 8 | |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 9 | std::vector<gfx::Rect> DisplayFeature::ComputeViewportSegments( |
Menard, Alexis | 91b2a32 | 2024-02-23 21:41:11 | [diff] [blame] | 10 | const gfx::Size& visible_viewport_size, |
| 11 | int root_view_offset_from_origin) const { |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 12 | std::vector<gfx::Rect> viewport_segments; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 13 | |
| 14 | int display_feature_end = offset + mask_length; |
| 15 | if (orientation == DisplayFeature::Orientation::kVertical) { |
| 16 | // If the display feature is vertically oriented, it splits or masks |
| 17 | // the widget into two side-by-side segments. Note that in the masking |
| 18 | // scenario, there is an area of the widget that are not covered by the |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 19 | // union of the viewport segments - this area's pixels will not be visible |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 20 | // to the user. |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 21 | viewport_segments.emplace_back(0, 0, offset, |
| 22 | visible_viewport_size.height()); |
| 23 | viewport_segments.emplace_back( |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 24 | display_feature_end, 0, |
| 25 | visible_viewport_size.width() - display_feature_end, |
| 26 | visible_viewport_size.height()); |
| 27 | } else { |
| 28 | // If the display feature is offset in the y direction, it splits or masks |
| 29 | // the widget into two stacked segments. |
Menard, Alexis | 91b2a32 | 2024-02-23 21:41:11 | [diff] [blame] | 30 | // We need to offset the display feature by the browser controls top height. |
| 31 | display_feature_end = display_feature_end - root_view_offset_from_origin; |
| 32 | int final_offset = offset - root_view_offset_from_origin; |
| 33 | if (final_offset < 0 || display_feature_end < 0) { |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 34 | return viewport_segments; |
Menard, Alexis | 91b2a32 | 2024-02-23 21:41:11 | [diff] [blame] | 35 | } |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 36 | viewport_segments.emplace_back(0, 0, visible_viewport_size.width(), |
| 37 | final_offset); |
| 38 | viewport_segments.emplace_back( |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 39 | 0, display_feature_end, visible_viewport_size.width(), |
| 40 | visible_viewport_size.height() - display_feature_end); |
| 41 | } |
| 42 | |
Alexis Menard | fb62ecc | 2024-02-26 21:25:19 | [diff] [blame] | 43 | return viewport_segments; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // static |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 47 | std::optional<DisplayFeature> DisplayFeature::Create(Orientation orientation, |
| 48 | int offset, |
| 49 | int mask_length, |
| 50 | int width, |
| 51 | int height, |
| 52 | ParamErrorEnum* error) { |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 53 | if (!width && !height) { |
| 54 | *error = ParamErrorEnum::kDisplayFeatureWithZeroScreenSize; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 55 | return std::nullopt; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (offset < 0 || mask_length < 0) { |
| 59 | *error = ParamErrorEnum::kNegativeDisplayFeatureParams; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 60 | return std::nullopt; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (orientation == Orientation::kVertical && offset + mask_length > width) { |
| 64 | *error = ParamErrorEnum::kOutsideScreenWidth; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 65 | return std::nullopt; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | if (orientation == Orientation::kHorizontal && |
| 69 | offset + mask_length > height) { |
| 70 | *error = ParamErrorEnum::kOutsideScreenHeight; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 71 | return std::nullopt; |
Songtao Xia | 27891f21 | 2020-07-09 21:39:17 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return DisplayFeature{orientation, offset, mask_length}; |
| 75 | } |
| 76 | |
Daniel Libby | adeca89 | 2020-06-05 20:03:40 | [diff] [blame] | 77 | } // namespace content |