desktop_geometry.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. DesktopVector operator-() const { return DesktopVector(-x_, -y_); }
  37. private:
  38. int32_t x_;
  39. int32_t y_;
  40. };
  41. // Type used to represent screen/window size.
  42. class DesktopSize {
  43. public:
  44. DesktopSize() : width_(0), height_(0) {}
  45. DesktopSize(int32_t width, int32_t height) : width_(width), height_(height) {}
  46. int32_t width() const { return width_; }
  47. int32_t height() const { return height_; }
  48. bool is_empty() const { return width_ <= 0 || height_ <= 0; }
  49. bool equals(const DesktopSize& other) const {
  50. return width_ == other.width_ && height_ == other.height_;
  51. }
  52. void set(int32_t width, int32_t height) {
  53. width_ = width;
  54. height_ = height;
  55. }
  56. private:
  57. int32_t width_;
  58. int32_t height_;
  59. };
  60. // Represents a rectangle on the screen.
  61. class RTC_EXPORT DesktopRect {
  62. public:
  63. static DesktopRect MakeSize(const DesktopSize& size) {
  64. return DesktopRect(0, 0, size.width(), size.height());
  65. }
  66. static DesktopRect MakeWH(int32_t width, int32_t height) {
  67. return DesktopRect(0, 0, width, height);
  68. }
  69. static DesktopRect MakeXYWH(int32_t x,
  70. int32_t y,
  71. int32_t width,
  72. int32_t height) {
  73. return DesktopRect(x, y, x + width, y + height);
  74. }
  75. static DesktopRect MakeLTRB(int32_t left,
  76. int32_t top,
  77. int32_t right,
  78. int32_t bottom) {
  79. return DesktopRect(left, top, right, bottom);
  80. }
  81. static DesktopRect MakeOriginSize(const DesktopVector& origin,
  82. const DesktopSize& size) {
  83. return MakeXYWH(origin.x(), origin.y(), size.width(), size.height());
  84. }
  85. DesktopRect() : left_(0), top_(0), right_(0), bottom_(0) {}
  86. int32_t left() const { return left_; }
  87. int32_t top() const { return top_; }
  88. int32_t right() const { return right_; }
  89. int32_t bottom() const { return bottom_; }
  90. int32_t width() const { return right_ - left_; }
  91. int32_t height() const { return bottom_ - top_; }
  92. void set_width(int32_t width) { right_ = left_ + width; }
  93. void set_height(int32_t height) { bottom_ = top_ + height; }
  94. DesktopVector top_left() const { return DesktopVector(left_, top_); }
  95. DesktopSize size() const { return DesktopSize(width(), height()); }
  96. bool is_empty() const { return left_ >= right_ || top_ >= bottom_; }
  97. bool equals(const DesktopRect& other) const {
  98. return left_ == other.left_ && top_ == other.top_ &&
  99. right_ == other.right_ && bottom_ == other.bottom_;
  100. }
  101. // Returns true if |point| lies within the rectangle boundaries.
  102. bool Contains(const DesktopVector& point) const;
  103. // Returns true if |rect| lies within the boundaries of this rectangle.
  104. bool ContainsRect(const DesktopRect& rect) const;
  105. // Finds intersection with |rect|.
  106. void IntersectWith(const DesktopRect& rect);
  107. // Extends the rectangle to cover |rect|. If |this| is empty, replaces |this|
  108. // with |rect|; if |rect| is empty, this function takes no effect.
  109. void UnionWith(const DesktopRect& rect);
  110. // Adds (dx, dy) to the position of the rectangle.
  111. void Translate(int32_t dx, int32_t dy);
  112. void Translate(DesktopVector d) { Translate(d.x(), d.y()); }
  113. // Enlarges current DesktopRect by subtracting |left_offset| and |top_offset|
  114. // from |left_| and |top_|, and adding |right_offset| and |bottom_offset| to
  115. // |right_| and |bottom_|. This function does not normalize the result, so
  116. // |left_| and |top_| may be less than zero or larger than |right_| and
  117. // |bottom_|.
  118. void Extend(int32_t left_offset,
  119. int32_t top_offset,
  120. int32_t right_offset,
  121. int32_t bottom_offset);
  122. // Scales current DesktopRect. This function does not impact the |top_| and
  123. // |left_|.
  124. void Scale(double horizontal, double vertical);
  125. private:
  126. DesktopRect(int32_t left, int32_t top, int32_t right, int32_t bottom)
  127. : left_(left), top_(top), right_(right), bottom_(bottom) {}
  128. int32_t left_;
  129. int32_t top_;
  130. int32_t right_;
  131. int32_t bottom_;
  132. };
  133. } // namespace webrtc
  134. #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_