Rect.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * 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
  7. *
  8. * 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.
  15. */
  16. #include <system/graphics.h>
  17. #include <ui/Rect.h>
  18. namespace android {
  19. const Rect Rect::INVALID_RECT{0, 0, -1, -1};
  20. static inline int32_t min(int32_t a, int32_t b) {
  21. return (a < b) ? a : b;
  22. }
  23. static inline int32_t max(int32_t a, int32_t b) {
  24. return (a > b) ? a : b;
  25. }
  26. void Rect::makeInvalid() {
  27. left = 0;
  28. top = 0;
  29. right = -1;
  30. bottom = -1;
  31. }
  32. bool Rect::operator <(const Rect& rhs) const {
  33. if (top < rhs.top) {
  34. return true;
  35. } else if (top == rhs.top) {
  36. if (left < rhs.left) {
  37. return true;
  38. } else if (left == rhs.left) {
  39. if (bottom < rhs.bottom) {
  40. return true;
  41. } else if (bottom == rhs.bottom) {
  42. if (right < rhs.right) {
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. Rect& Rect::offsetTo(int32_t x, int32_t y) {
  51. right -= left - x;
  52. bottom -= top - y;
  53. left = x;
  54. top = y;
  55. return *this;
  56. }
  57. Rect& Rect::offsetBy(int32_t x, int32_t y) {
  58. left += x;
  59. top += y;
  60. right += x;
  61. bottom += y;
  62. return *this;
  63. }
  64. const Rect Rect::operator +(const Point& rhs) const {
  65. const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
  66. return result;
  67. }
  68. const Rect Rect::operator -(const Point& rhs) const {
  69. const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
  70. return result;
  71. }
  72. bool Rect::intersect(const Rect& with, Rect* result) const {
  73. result->left = max(left, with.left);
  74. result->top = max(top, with.top);
  75. result->right = min(right, with.right);
  76. result->bottom = min(bottom, with.bottom);
  77. return !(result->isEmpty());
  78. }
  79. Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
  80. Rect result(*this);
  81. if (xform & HAL_TRANSFORM_FLIP_H) {
  82. result = Rect(width - result.right, result.top, width - result.left,
  83. result.bottom);
  84. }
  85. if (xform & HAL_TRANSFORM_FLIP_V) {
  86. result = Rect(result.left, height - result.bottom, result.right,
  87. height - result.top);
  88. }
  89. if (xform & HAL_TRANSFORM_ROT_90) {
  90. int left = height - result.bottom;
  91. int top = result.left;
  92. int right = height - result.top;
  93. int bottom = result.right;
  94. result = Rect(left, top, right, bottom);
  95. }
  96. return result;
  97. }
  98. Rect Rect::reduce(const Rect& exclude) const {
  99. Rect result;
  100. uint32_t mask = 0;
  101. mask |= (exclude.left > left) ? 1 : 0;
  102. mask |= (exclude.top > top) ? 2 : 0;
  103. mask |= (exclude.right < right) ? 4 : 0;
  104. mask |= (exclude.bottom < bottom) ? 8 : 0;
  105. if (mask == 0) {
  106. // crop entirely covers us
  107. result.clear();
  108. } else {
  109. result = *this;
  110. if (!(mask & (mask - 1))) {
  111. // power-of-2, i.e.: just one bit is set
  112. if (mask & 1) {
  113. result.right = exclude.left;
  114. } else if (mask & 2) {
  115. result.bottom = exclude.top;
  116. } else if (mask & 4) {
  117. result.left = exclude.right;
  118. } else if (mask & 8) {
  119. result.top = exclude.bottom;
  120. }
  121. }
  122. }
  123. return result;
  124. }
  125. }; // namespace android