File: scaling_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (248 lines) | stat: -rw-r--r-- 9,369 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/win/scaling_util.h"

#include <algorithm>

#include "base/check.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/range/range.h"

namespace {

// Represents the amount of rotation an object has about a coordinate plane.
enum class CoordinateRotation {
  COORDINATE_ROTATE_0,
  COORDINATE_ROTATE_90,
  COORDINATE_ROTATE_180,
  COORDINATE_ROTATE_270,
};

// Returns the CoordinateRotation necessary for |ref| and |other| so that |ref|
// is positioned on top of |other|.
CoordinateRotation ComputeCoordinateRotationRefTop(const gfx::Rect& ref,
                                                   const gfx::Rect& other) {
  if (ref.bottom() <= other.y())
    return CoordinateRotation::COORDINATE_ROTATE_0;
  if (other.right() <= ref.x())
    return CoordinateRotation::COORDINATE_ROTATE_90;
  if (other.bottom() <= ref.y())
    return CoordinateRotation::COORDINATE_ROTATE_180;

  return CoordinateRotation::COORDINATE_ROTATE_270;
}

gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) {
  return gfx::Rect(rect.y(), -rect.x() - rect.width(),
                   rect.height(), rect.width());
}

gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) {
  return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(),
                   rect.width(), rect.height());
}

gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) {
  return gfx::Rect(-rect.y() - rect.height(), rect.x(),
                   rect.height(), rect.width());
}

gfx::Rect CoordinateRotateRect(const gfx::Rect& rect,
                               CoordinateRotation rotation) {
  switch (rotation) {
    case CoordinateRotation::COORDINATE_ROTATE_90:
      return CoordinateRotateRectangle90(rect);
    case CoordinateRotation::COORDINATE_ROTATE_180:
      return CoordinateRotateRectangle180(rect);
    case CoordinateRotation::COORDINATE_ROTATE_270:
      return CoordinateRotateRectangle270(rect);
    default:
      return rect;
  }
}

bool InRange(int target, int lower_bound, int upper_bound) {
  return lower_bound <= target && target <= upper_bound;
}

// Scaled |unscaled_offset| to the same relative position on |unscaled_length|
// based off of |unscaled_length|'s |scale_factor|.
int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) {
  float scaled_length = static_cast<float>(unscaled_length) / scale_factor;
  float percent =
      static_cast<float>(unscaled_offset) / static_cast<float>(unscaled_length);
  return base::ClampFloor(scaled_length * percent);
}

}  // namespace

namespace display {
namespace win {

bool DisplayInfosTouch(const internal::DisplayInfo& a,
                       const internal::DisplayInfo& b) {
  const gfx::Rect& a_rect = a.screen_rect();
  const gfx::Rect& b_rect = b.screen_rect();
  int max_left = std::max(a_rect.x(), b_rect.x());
  int max_top = std::max(a_rect.y(), b_rect.y());
  int min_right = std::min(a_rect.right(), b_rect.right());
  int min_bottom = std::min(a_rect.bottom(), b_rect.bottom());
  return (max_left == min_right &&
             a_rect.y() <= b_rect.bottom() &&
             b_rect.y() <= a_rect.bottom()) ||
         (max_top == min_bottom &&
             a_rect.x() <= b_rect.right() &&
             b_rect.x() <= a_rect.right());
}

DisplayPlacement::Position CalculateDisplayPosition(
    const internal::DisplayInfo& parent,
    const internal::DisplayInfo& current) {
  const gfx::Rect& parent_rect = parent.screen_rect();
  const gfx::Rect& current_rect = current.screen_rect();
  int max_left = std::max(parent_rect.x(), current_rect.x());
  int max_top = std::max(parent_rect.y(), current_rect.y());
  int min_right = std::min(parent_rect.right(), current_rect.right());
  int min_bottom = std::min(parent_rect.bottom(), current_rect.bottom());
  if (max_left == min_right && max_top == min_bottom) {
    // Corner touching.
    if (parent_rect.bottom() == max_top)
      return DisplayPlacement::Position::BOTTOM;
    if (parent_rect.x() == max_left)
      return DisplayPlacement::Position::LEFT;

    return DisplayPlacement::Position::TOP;
  }
  if (max_left == min_right &&
      parent_rect.y() <= current_rect.bottom() &&
      current_rect.y() <= parent_rect.bottom()) {
    // Vertical edge touching.
    return parent_rect.x() == max_left
        ? DisplayPlacement::Position::LEFT
        : DisplayPlacement::Position::RIGHT;
  }
  if (max_top == min_bottom &&
      parent_rect.x() <= current_rect.right() &&
      current_rect.x() <= parent_rect.right()) {
    // Horizontal edge touching.
    return parent_rect.y() == max_top
        ? DisplayPlacement::Position::TOP
        : DisplayPlacement::Position::BOTTOM;
  }
  NOTREACHED() << "CalculateDisplayPosition relies on touching DisplayInfos.";
}

DisplayPlacement CalculateDisplayPlacement(
    const internal::DisplayInfo& parent,
    const internal::DisplayInfo& current) {
  DCHECK(DisplayInfosTouch(parent, current)) << "DisplayInfos must touch.";

  DisplayPlacement placement;
  placement.parent_display_id = parent.id();
  placement.display_id = current.id();
  placement.position = CalculateDisplayPosition(parent, current);

  int parent_begin = 0;
  int parent_end = 0;
  int current_begin = 0;
  int current_end = 0;

  switch (placement.position) {
    case DisplayPlacement::Position::TOP:
    case DisplayPlacement::Position::BOTTOM:
      parent_begin = parent.screen_rect().x();
      parent_end = parent.screen_rect().right();
      current_begin = current.screen_rect().x();
      current_end = current.screen_rect().right();
      break;
    case DisplayPlacement::Position::LEFT:
    case DisplayPlacement::Position::RIGHT:
      parent_begin = parent.screen_rect().y();
      parent_end = parent.screen_rect().bottom();
      current_begin = current.screen_rect().y();
      current_end = current.screen_rect().bottom();
      break;
  }

  // Since we're talking offsets, make everything relative to parent_begin.
  parent_end -= parent_begin;
  current_begin -= parent_begin;
  current_end -= parent_begin;
  parent_begin = 0;

  // There are a few ways lines can intersect:
  // End Aligned
  // CURRENT's offset is relative to the end (in our world, BOTTOM_RIGHT).
  //                 +-PARENT----------------+
  //                    +-CURRENT------------+
  //
  // Positioning based off of |current_begin|.
  // CURRENT's offset is simply a percentage of its position on PARENT.
  //                 +-PARENT----------------+
  //                        +-CURRENT------------+
  //
  // Positioning based off of |current_end|.
  // CURRENT's offset is dependent on the percentage of its end position on
  // PARENT.
  //                 +-PARENT----------------+
  //           +-CURRENT------------+
  //
  // Positioning based off of |parent_begin| on current.
  // CURRENT's offset is dependent on the percentage of its end position on
  // PARENT.
  //                 +-PARENT----------------+
  //           +-CURRENT--------------------------+
  if (parent_end == current_end) {
    // End aligned.
    placement.offset_reference =
        DisplayPlacement::OffsetReference::BOTTOM_RIGHT;
    placement.offset = 0;
  } else if (InRange(current_begin, parent_begin, parent_end)) {
    placement.offset = ScaleOffset(parent_end,
                                   parent.device_scale_factor(),
                                   current_begin);
  } else if (InRange(current_end, parent_begin, parent_end)) {
    placement.offset_reference =
        DisplayPlacement::OffsetReference::BOTTOM_RIGHT;
    placement.offset = ScaleOffset(parent_end,
                                   parent.device_scale_factor(),
                                   parent_end - current_end);
  } else {
    DCHECK(InRange(parent_begin, current_begin, current_end));
    placement.offset = ScaleOffset(current_end - current_begin,
                                   current.device_scale_factor(),
                                   current_begin);
  }

  return placement;
}

// This function rotates the rectangles so that |ref| is always on top of
// |rect|, allowing the function to concentrate on comparing |ref|'s bottom
// corners and |rect|'s top corners when the rects don't overlap vertically.
int64_t SquaredDistanceBetweenRects(const gfx::Rect& ref,
                                    const gfx::Rect& rect) {
  gfx::Rect intersection_rect = gfx::IntersectRects(ref, rect);
  if (!intersection_rect.IsEmpty())
    return -(intersection_rect.width() * intersection_rect.height());

  CoordinateRotation degrees = ComputeCoordinateRotationRefTop(ref, rect);
  gfx::Rect top_rect(CoordinateRotateRect(ref, degrees));
  gfx::Rect bottom_rect(CoordinateRotateRect(rect, degrees));
  if (bottom_rect.right() < top_rect.x())
    return (bottom_rect.top_right() - top_rect.bottom_left()).LengthSquared();
  else if (top_rect.right() < bottom_rect.x())
    return (bottom_rect.origin() - top_rect.bottom_right()).LengthSquared();

  int distance = bottom_rect.y() - top_rect.bottom();
  return distance * distance;
}

}  // namespace win
}  // namespace display