blob: f839304c755eab09ed275cb4407c3980e72effc5 [file] [log] [blame]
Daniel Libbyadeca892020-06-05 20:03:401// Copyright 2016 The Chromium Authors. All rights reserved.
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
7namespace content {
8
9bool DisplayFeature::operator==(const DisplayFeature& other) const {
10 return orientation == other.orientation && offset == other.offset &&
11 mask_length == other.mask_length;
12}
13
14bool DisplayFeature::operator!=(const DisplayFeature& other) const {
15 return !(*this == other);
16}
17
Songtao Xia27891f212020-07-09 21:39:1718std::vector<gfx::Rect> DisplayFeature::ComputeWindowSegments(
19 const gfx::Size& visible_viewport_size) const {
20 std::vector<gfx::Rect> window_segments;
21
22 int display_feature_end = offset + mask_length;
23 if (orientation == DisplayFeature::Orientation::kVertical) {
24 // If the display feature is vertically oriented, it splits or masks
25 // the widget into two side-by-side segments. Note that in the masking
26 // scenario, there is an area of the widget that are not covered by the
27 // union of the window segments - this area's pixels will not be visible
28 // to the user.
29 window_segments.emplace_back(0, 0, offset, visible_viewport_size.height());
30 window_segments.emplace_back(
31 display_feature_end, 0,
32 visible_viewport_size.width() - display_feature_end,
33 visible_viewport_size.height());
34 } else {
35 // If the display feature is offset in the y direction, it splits or masks
36 // the widget into two stacked segments.
37 window_segments.emplace_back(0, 0, visible_viewport_size.width(), offset);
38 window_segments.emplace_back(
39 0, display_feature_end, visible_viewport_size.width(),
40 visible_viewport_size.height() - display_feature_end);
41 }
42
43 return window_segments;
44}
45
46// static
47base::Optional<DisplayFeature> DisplayFeature::Create(Orientation orientation,
48 int offset,
49 int mask_length,
50 int width,
51 int height,
52 ParamErrorEnum* error) {
53 if (!width && !height) {
54 *error = ParamErrorEnum::kDisplayFeatureWithZeroScreenSize;
55 return base::nullopt;
56 }
57
58 if (offset < 0 || mask_length < 0) {
59 *error = ParamErrorEnum::kNegativeDisplayFeatureParams;
60 return base::nullopt;
61 }
62
63 if (orientation == Orientation::kVertical && offset + mask_length > width) {
64 *error = ParamErrorEnum::kOutsideScreenWidth;
65 return base::nullopt;
66 }
67
68 if (orientation == Orientation::kHorizontal &&
69 offset + mask_length > height) {
70 *error = ParamErrorEnum::kOutsideScreenHeight;
71 return base::nullopt;
72 }
73
74 return DisplayFeature{orientation, offset, mask_length};
75}
76
Daniel Libbyadeca892020-06-05 20:03:4077} // namespace content