blob: 365ea13f8a2a4da1a191f52584d99e7f58f01feb [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian35801ce2009-05-26 17:44:57 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080015 */
16
Jamie Gennis59332802012-05-07 13:49:17 -070017#include <system/graphics.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#include <ui/Rect.h>
19
20namespace android {
21
Dianne Hackborn9147d112010-07-09 11:44:11 -070022static inline int32_t min(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070023 return (a < b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024}
25
Dianne Hackborn9147d112010-07-09 11:44:11 -070026static inline int32_t max(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070027 return (a > b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028}
29
30void Rect::makeInvalid() {
31 left = 0;
32 top = 0;
33 right = -1;
34 bottom = -1;
35}
36
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070037bool Rect::operator <(const Rect& rhs) const {
38 if (top < rhs.top) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 return true;
40 } else if (top == rhs.top) {
41 if (left < rhs.left) {
42 return true;
43 } else if (left == rhs.left) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070044 if (bottom < rhs.bottom) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 return true;
46 } else if (bottom == rhs.bottom) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070047 if (right < rhs.right) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 return true;
49 }
50 }
51 }
52 }
53 return false;
54}
55
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070056Rect& Rect::offsetTo(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 right -= left - x;
58 bottom -= top - y;
59 left = x;
60 top = y;
61 return *this;
62}
63
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070064Rect& Rect::offsetBy(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 left += x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066 top += y;
67 right += x;
68 bottom += y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 return *this;
70}
71
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070072const Rect Rect::operator +(const Point& rhs) const {
73 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070074 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075}
76
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070077const Rect Rect::operator -(const Point& rhs) const {
78 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070079 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080}
81
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070082bool Rect::intersect(const Rect& with, Rect* result) const {
83 result->left = max(left, with.left);
84 result->top = max(top, with.top);
85 result->right = min(right, with.right);
86 result->bottom = min(bottom, with.bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 return !(result->isEmpty());
88}
89
Jamie Gennisf15a83f2012-05-10 20:43:55 -070090Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -070091 Rect result(*this);
92 if (xform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070093 result = Rect(width - result.right, result.top, width - result.left,
94 result.bottom);
Jamie Gennis59332802012-05-07 13:49:17 -070095 }
96 if (xform & HAL_TRANSFORM_FLIP_V) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070097 result = Rect(result.left, height - result.bottom, result.right,
98 height - result.top);
Jamie Gennis59332802012-05-07 13:49:17 -070099 }
100 if (xform & HAL_TRANSFORM_ROT_90) {
101 int left = height - result.bottom;
102 int top = result.left;
103 int right = height - result.top;
104 int bottom = result.right;
105 result = Rect(left, top, right, bottom);
106 }
107 return result;
108}
109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110}; // namespace android