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