You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_
  11. #define MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_
  12. #include <stdint.h>
  13. #include "rtc_base/system/rtc_export.h"
  14. namespace webrtc {
  15. // A vector in the 2D integer space. E.g. can be used to represent screen DPI.
  16. class DesktopVector {
  17. public:
  18. DesktopVector() : x_(0), y_(0) {}
  19. DesktopVector(int32_t x, int32_t y) : x_(x), y_(y) {}
  20. int32_t x() const { return x_; }
  21. int32_t y() const { return y_; }
  22. bool is_zero() const { return x_ == 0 && y_ == 0; }
  23. bool equals(const DesktopVector& other) const {
  24. return x_ == other.x_ && y_ == other.y_;
  25. }
  26. void set(int32_t x, int32_t y) {
  27. x_ = x;
  28. y_ = y;
  29. }
  30. DesktopVector add(const DesktopVector& other) const {
  31. return DesktopVector(x() + other.x(), y() + other.y());
  32. }
  33. DesktopVector subtract(const DesktopVector& other) const {
  34. return DesktopVector(x() - other.x(), y() - other.y());
  35. }
  36. private:
  37. int32_t x_;
  38. int32_t y_;
  39. };
  40. // Type used to represent screen/window size.
  41. class DesktopSize {
  42. public:
  43. DesktopSize() : width_(0), height_(0) {}
  44. DesktopSize(int32_t width, int32_t height) : width_(width), height_(height) {}
  45. int32_t width() const { return width_; }
  46. int32_t height() const { return height_; }
  47. bool is_empty() const { return width_ <= 0 || height_ <= 0; }
  48. bool equals(const DesktopSize& other) const {
  49. return width_ == other.width_ && height_ == other.height_;
  50. }
  51. void set(int32_t width, int32_t height) {
  52. width_ = width;
  53. height_ = height;
  54. }
  55. private:
  56. int32_t width_;
  57. int32_t height_;
  58. };
  59. // Represents a rectangle on the screen.
  60. class RTC_EXPORT DesktopRect {
  61. public:
  62. static DesktopRect MakeSize(const DesktopSize& size) {
  63. return DesktopRect(0, 0, size.width(), size.height());
  64. }
  65. static DesktopRect MakeWH(int32_t width, int32_t height) {
  66. return DesktopRect(0, 0, width, height);
  67. }
  68. static DesktopRect MakeXYWH(int32_t x,
  69. int32_t y,
  70. int32_t width,
  71. int32_t height) {
  72. return DesktopRect(x, y, x + width, y + height);
  73. }
  74. static DesktopRect MakeLTRB(int32_t left,
  75. int32_t top,
  76. int32_t right,
  77. int32_t bottom) {
  78. return DesktopRect(left, top, right, bottom);
  79. }
  80. static DesktopRect MakeOriginSize(const DesktopVector& origin,
  81. const DesktopSize& size) {
  82. return MakeXYWH(origin.x(), origin.y(), size.width(), size.height());
  83. }
  84. DesktopRect() : left_(0), top_(0), right_(0), bottom_(0) {}
  85. int32_t left() const { return left_; }
  86. int32_t top() const { return top_; }
  87. int32_t right() const { return right_; }
  88. int32_t bottom() const { return bottom_; }
  89. int32_t width() const { return right_ - left_; }
  90. int32_t height() const { return bottom_ - top_; }
  91. void set_width(int32_t width) { right_ = left_ + width; }
  92. void set_height(int32_t height) { bottom_ = top_ + height; }
  93. DesktopVector top_left() const { return DesktopVector(left_, top_); }
  94. DesktopSize size() const { return DesktopSize(width(), height()); }
  95. bool is_empty() const { return left_ >= right_ || top_ >= bottom_; }
  96. bool equals(const DesktopRect& other) const {
  97. return left_ == other.left_ && top_ == other.top_ &&
  98. right_ == other.right_ && bottom_ == other.bottom_;
  99. }
  100. // Returns true if |point| lies within the rectangle boundaries.
  101. bool Contains(const DesktopVector& point) const;
  102. // Returns true if |rect| lies within the boundaries of this rectangle.
  103. bool ContainsRect(const DesktopRect& rect) const;
  104. // Finds intersection with |rect|.
  105. void IntersectWith(const DesktopRect& rect);
  106. // Extends the rectangle to cover |rect|. If |this| is empty, replaces |this|
  107. // with |rect|; if |rect| is empty, this function takes no effect.
  108. void UnionWith(const DesktopRect& rect);
  109. // Adds (dx, dy) to the position of the rectangle.
  110. void Translate(int32_t dx, int32_t dy);
  111. void Translate(DesktopVector d) { Translate(d.x(), d.y()); }
  112. // Enlarges current DesktopRect by subtracting |left_offset| and |top_offset|
  113. // from |left_| and |top_|, and adding |right_offset| and |bottom_offset| to
  114. // |right_| and |bottom_|. This function does not normalize the result, so
  115. // |left_| and |top_| may be less than zero or larger than |right_| and
  116. // |bottom_|.
  117. void Extend(int32_t left_offset,
  118. int32_t top_offset,
  119. int32_t right_offset,
  120. int32_t bottom_offset);
  121. // Scales current DesktopRect. This function does not impact the |top_| and
  122. // |left_|.
  123. void Scale(double horizontal, double vertical);
  124. private:
  125. DesktopRect(int32_t left, int32_t top, int32_t right, int32_t bottom)
  126. : left_(left), top_(top), right_(right), bottom_(bottom) {}
  127. int32_t left_;
  128. int32_t top_;
  129. int32_t right_;
  130. int32_t bottom_;
  131. };
  132. } // namespace webrtc
  133. #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_