desktop_geometry.h (5734B)
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 11 #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_ 12 #define MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_ 13 14 #include <stdint.h> 15 16 #include "rtc_base/strings/string_builder.h" 17 #include "rtc_base/system/rtc_export.h" 18 19 namespace webrtc { 20 21 // A vector in the 2D integer space. E.g. can be used to represent screen DPI. 22 class DesktopVector { 23 public: 24 DesktopVector() : x_(0), y_(0) {} 25 DesktopVector(int32_t x, int32_t y) : x_(x), y_(y) {} 26 27 int32_t x() const { return x_; } 28 int32_t y() const { return y_; } 29 bool is_zero() const { return x_ == 0 && y_ == 0; } 30 31 bool equals(const DesktopVector& other) const { 32 return x_ == other.x_ && y_ == other.y_; 33 } 34 35 void set(int32_t x, int32_t y) { 36 x_ = x; 37 y_ = y; 38 } 39 40 DesktopVector add(const DesktopVector& other) const { 41 return DesktopVector(x() + other.x(), y() + other.y()); 42 } 43 DesktopVector subtract(const DesktopVector& other) const { 44 return DesktopVector(x() - other.x(), y() - other.y()); 45 } 46 47 DesktopVector operator-() const { return DesktopVector(-x_, -y_); } 48 49 template <typename Sink> 50 friend void AbslStringify(Sink& sink, const DesktopVector& self) { 51 StringBuilder sb; 52 sink.Append((sb << "x: " << self.x() << ", y: " << self.y()).str()); 53 } 54 55 private: 56 int32_t x_; 57 int32_t y_; 58 }; 59 60 // Type used to represent screen/window size. 61 class DesktopSize { 62 public: 63 DesktopSize() : width_(0), height_(0) {} 64 DesktopSize(int32_t width, int32_t height) : width_(width), height_(height) {} 65 66 int32_t width() const { return width_; } 67 int32_t height() const { return height_; } 68 69 bool is_empty() const { return width_ <= 0 || height_ <= 0; } 70 71 bool equals(const DesktopSize& other) const { 72 return width_ == other.width_ && height_ == other.height_; 73 } 74 75 void set(int32_t width, int32_t height) { 76 width_ = width; 77 height_ = height; 78 } 79 80 private: 81 int32_t width_; 82 int32_t height_; 83 }; 84 85 // Represents a rectangle on the screen. 86 class RTC_EXPORT DesktopRect { 87 public: 88 static DesktopRect MakeSize(const DesktopSize& size) { 89 return DesktopRect(0, 0, size.width(), size.height()); 90 } 91 static DesktopRect MakeWH(int32_t width, int32_t height) { 92 return DesktopRect(0, 0, width, height); 93 } 94 static DesktopRect MakeXYWH(int32_t x, 95 int32_t y, 96 int32_t width, 97 int32_t height) { 98 return DesktopRect(x, y, x + width, y + height); 99 } 100 static DesktopRect MakeLTRB(int32_t left, 101 int32_t top, 102 int32_t right, 103 int32_t bottom) { 104 return DesktopRect(left, top, right, bottom); 105 } 106 static DesktopRect MakeOriginSize(const DesktopVector& origin, 107 const DesktopSize& size) { 108 return MakeXYWH(origin.x(), origin.y(), size.width(), size.height()); 109 } 110 111 DesktopRect() : left_(0), top_(0), right_(0), bottom_(0) {} 112 113 int32_t left() const { return left_; } 114 int32_t top() const { return top_; } 115 int32_t right() const { return right_; } 116 int32_t bottom() const { return bottom_; } 117 int32_t width() const { return right_ - left_; } 118 int32_t height() const { return bottom_ - top_; } 119 120 void set_width(int32_t width) { right_ = left_ + width; } 121 void set_height(int32_t height) { bottom_ = top_ + height; } 122 123 DesktopVector top_left() const { return DesktopVector(left_, top_); } 124 DesktopSize size() const { return DesktopSize(width(), height()); } 125 126 bool is_empty() const { return left_ >= right_ || top_ >= bottom_; } 127 128 bool equals(const DesktopRect& other) const { 129 return left_ == other.left_ && top_ == other.top_ && 130 right_ == other.right_ && bottom_ == other.bottom_; 131 } 132 133 // Returns true if `point` lies within the rectangle boundaries. 134 bool Contains(const DesktopVector& point) const; 135 136 // Returns true if `rect` lies within the boundaries of this rectangle. 137 bool ContainsRect(const DesktopRect& rect) const; 138 139 // Finds intersection with `rect`. 140 void IntersectWith(const DesktopRect& rect); 141 142 // Extends the rectangle to cover `rect`. If `this` is empty, replaces `this` 143 // with `rect`; if `rect` is empty, this function takes no effect. 144 void UnionWith(const DesktopRect& rect); 145 146 // Adds (dx, dy) to the position of the rectangle. 147 void Translate(int32_t dx, int32_t dy); 148 void Translate(DesktopVector d) { Translate(d.x(), d.y()); } 149 150 // Enlarges current DesktopRect by subtracting `left_offset` and `top_offset` 151 // from `left_` and `top_`, and adding `right_offset` and `bottom_offset` to 152 // `right_` and `bottom_`. This function does not normalize the result, so 153 // `left_` and `top_` may be less than zero or larger than `right_` and 154 // `bottom_`. 155 void Extend(int32_t left_offset, 156 int32_t top_offset, 157 int32_t right_offset, 158 int32_t bottom_offset); 159 160 // Scales current DesktopRect. This function does not impact the `top_` and 161 // `left_`. 162 void Scale(double horizontal, double vertical); 163 164 private: 165 DesktopRect(int32_t left, int32_t top, int32_t right, int32_t bottom) 166 : left_(left), top_(top), right_(right), bottom_(bottom) {} 167 168 int32_t left_; 169 int32_t top_; 170 int32_t right_; 171 int32_t bottom_; 172 }; 173 174 } // namespace webrtc 175 176 #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_