Region.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2007 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. #ifndef ANDROID_UI_REGION_H
  17. #define ANDROID_UI_REGION_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Vector.h>
  21. #include <ui/Rect.h>
  22. #include <utils/Flattenable.h>
  23. namespace android {
  24. // ---------------------------------------------------------------------------
  25. class SharedBuffer;
  26. class String8;
  27. // ---------------------------------------------------------------------------
  28. class Region : public LightFlattenable<Region>
  29. {
  30. public:
  31. static const Region INVALID_REGION;
  32. Region();
  33. Region(const Region& rhs);
  34. explicit Region(const Rect& rhs);
  35. ~Region();
  36. static Region createTJunctionFreeRegion(const Region& r);
  37. Region& operator = (const Region& rhs);
  38. inline bool isEmpty() const { return getBounds().isEmpty(); }
  39. inline bool isRect() const { return mStorage.size() == 1; }
  40. inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
  41. inline Rect bounds() const { return getBounds(); }
  42. bool contains(const Point& point) const;
  43. bool contains(int x, int y) const;
  44. // the region becomes its bounds
  45. Region& makeBoundsSelf();
  46. void clear();
  47. void set(const Rect& r);
  48. void set(int32_t w, int32_t h);
  49. void set(uint32_t w, uint32_t h);
  50. Region& orSelf(const Rect& rhs);
  51. Region& xorSelf(const Rect& rhs);
  52. Region& andSelf(const Rect& rhs);
  53. Region& subtractSelf(const Rect& rhs);
  54. // boolean operators, applied on this
  55. Region& orSelf(const Region& rhs);
  56. Region& xorSelf(const Region& rhs);
  57. Region& andSelf(const Region& rhs);
  58. Region& subtractSelf(const Region& rhs);
  59. // boolean operators
  60. const Region merge(const Rect& rhs) const;
  61. const Region mergeExclusive(const Rect& rhs) const;
  62. const Region intersect(const Rect& rhs) const;
  63. const Region subtract(const Rect& rhs) const;
  64. // boolean operators
  65. const Region merge(const Region& rhs) const;
  66. const Region mergeExclusive(const Region& rhs) const;
  67. const Region intersect(const Region& rhs) const;
  68. const Region subtract(const Region& rhs) const;
  69. // these translate rhs first
  70. Region& translateSelf(int dx, int dy);
  71. Region& orSelf(const Region& rhs, int dx, int dy);
  72. Region& xorSelf(const Region& rhs, int dx, int dy);
  73. Region& andSelf(const Region& rhs, int dx, int dy);
  74. Region& subtractSelf(const Region& rhs, int dx, int dy);
  75. // these translate rhs first
  76. const Region translate(int dx, int dy) const;
  77. const Region merge(const Region& rhs, int dx, int dy) const;
  78. const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
  79. const Region intersect(const Region& rhs, int dx, int dy) const;
  80. const Region subtract(const Region& rhs, int dx, int dy) const;
  81. // convenience operators overloads
  82. inline const Region operator | (const Region& rhs) const;
  83. inline const Region operator ^ (const Region& rhs) const;
  84. inline const Region operator & (const Region& rhs) const;
  85. inline const Region operator - (const Region& rhs) const;
  86. inline const Region operator + (const Point& pt) const;
  87. inline Region& operator |= (const Region& rhs);
  88. inline Region& operator ^= (const Region& rhs);
  89. inline Region& operator &= (const Region& rhs);
  90. inline Region& operator -= (const Region& rhs);
  91. inline Region& operator += (const Point& pt);
  92. // returns true if the regions share the same underlying storage
  93. bool isTriviallyEqual(const Region& region) const;
  94. /* various ways to access the rectangle list */
  95. // STL-like iterators
  96. typedef Rect const* const_iterator;
  97. const_iterator begin() const;
  98. const_iterator end() const;
  99. // returns an array of rect which has the same life-time has this
  100. // Region object.
  101. Rect const* getArray(size_t* count) const;
  102. // returns a SharedBuffer as well as the number of rects.
  103. // ownership is transfered to the caller.
  104. // the caller must call SharedBuffer::release() to free the memory.
  105. SharedBuffer const* getSharedBuffer(size_t* count) const;
  106. /* no user serviceable parts here... */
  107. // add a rectangle to the internal list. This rectangle must
  108. // be sorted in Y and X and must not make the region invalid.
  109. void addRectUnchecked(int l, int t, int r, int b);
  110. inline bool isFixedSize() const { return false; }
  111. size_t getFlattenedSize() const;
  112. status_t flatten(void* buffer, size_t size) const;
  113. status_t unflatten(void const* buffer, size_t size);
  114. void dump(String8& out, const char* what, uint32_t flags=0) const;
  115. void dump(const char* what, uint32_t flags=0) const;
  116. private:
  117. class rasterizer;
  118. friend class rasterizer;
  119. Region& operationSelf(const Rect& r, int op);
  120. Region& operationSelf(const Region& r, int op);
  121. Region& operationSelf(const Region& r, int dx, int dy, int op);
  122. const Region operation(const Rect& rhs, int op) const;
  123. const Region operation(const Region& rhs, int op) const;
  124. const Region operation(const Region& rhs, int dx, int dy, int op) const;
  125. static void boolean_operation(int op, Region& dst,
  126. const Region& lhs, const Region& rhs, int dx, int dy);
  127. static void boolean_operation(int op, Region& dst,
  128. const Region& lhs, const Rect& rhs, int dx, int dy);
  129. static void boolean_operation(int op, Region& dst,
  130. const Region& lhs, const Region& rhs);
  131. static void boolean_operation(int op, Region& dst,
  132. const Region& lhs, const Rect& rhs);
  133. static void translate(Region& reg, int dx, int dy);
  134. static void translate(Region& dst, const Region& reg, int dx, int dy);
  135. static bool validate(const Region& reg,
  136. const char* name, bool silent = false);
  137. // mStorage is a (manually) sorted array of Rects describing the region
  138. // with an extra Rect as the last element which is set to the
  139. // bounds of the region. However, if the region is
  140. // a simple Rect then mStorage contains only that rect.
  141. Vector<Rect> mStorage;
  142. };
  143. const Region Region::operator | (const Region& rhs) const {
  144. return merge(rhs);
  145. }
  146. const Region Region::operator ^ (const Region& rhs) const {
  147. return mergeExclusive(rhs);
  148. }
  149. const Region Region::operator & (const Region& rhs) const {
  150. return intersect(rhs);
  151. }
  152. const Region Region::operator - (const Region& rhs) const {
  153. return subtract(rhs);
  154. }
  155. const Region Region::operator + (const Point& pt) const {
  156. return translate(pt.x, pt.y);
  157. }
  158. Region& Region::operator |= (const Region& rhs) {
  159. return orSelf(rhs);
  160. }
  161. Region& Region::operator ^= (const Region& rhs) {
  162. return xorSelf(rhs);
  163. }
  164. Region& Region::operator &= (const Region& rhs) {
  165. return andSelf(rhs);
  166. }
  167. Region& Region::operator -= (const Region& rhs) {
  168. return subtractSelf(rhs);
  169. }
  170. Region& Region::operator += (const Point& pt) {
  171. return translateSelf(pt.x, pt.y);
  172. }
  173. // ---------------------------------------------------------------------------
  174. }; // namespace android
  175. #endif // ANDROID_UI_REGION_H