| OLD | NEW |
| (Empty) | |
| 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 |
| 17 #ifndef ANDROID_UI_PRIVATE_REGION_HELPER_H |
| 18 #define ANDROID_UI_PRIVATE_REGION_HELPER_H |
| 19 |
| 20 #include <stdint.h> |
| 21 #include <sys/types.h> |
| 22 |
| 23 namespace android { |
| 24 // ---------------------------------------------------------------------------- |
| 25 |
| 26 template<typename RECT, typename TYPE> |
| 27 class region_operator |
| 28 { |
| 29 static const TYPE max_value = 0x7FFFFFF; |
| 30 |
| 31 public: |
| 32 /* |
| 33 * Common boolean operations: |
| 34 * value is computed as 0b101 op 0b110 |
| 35 * other boolean operation are possible, simply compute |
| 36 * their corresponding value with the above formulae and use |
| 37 * it when instantiating a region_operator. |
| 38 */ |
| 39 static const uint32_t LHS = 0x5; // 0b101 |
| 40 static const uint32_t RHS = 0x6; // 0b110 |
| 41 enum { |
| 42 op_nand = LHS & ~RHS, |
| 43 op_and = LHS & RHS, |
| 44 op_or = LHS | RHS, |
| 45 op_xor = LHS ^ RHS |
| 46 }; |
| 47 |
| 48 struct region { |
| 49 RECT const* rects; |
| 50 size_t count; |
| 51 TYPE dx; |
| 52 TYPE dy; |
| 53 inline region(const region& rhs) |
| 54 : rects(rhs.rects), count(rhs.count), dx(rhs.dx), dy(rhs.dy) { } |
| 55 inline region(RECT const* r, size_t c) |
| 56 : rects(r), count(c), dx(), dy() { } |
| 57 inline region(RECT const* r, size_t c, TYPE dx, TYPE dy) |
| 58 : rects(r), count(c), dx(dx), dy(dy) { } |
| 59 }; |
| 60 |
| 61 class region_rasterizer { |
| 62 friend class region_operator; |
| 63 virtual void operator()(const RECT& rect) = 0; |
| 64 public: |
| 65 virtual ~region_rasterizer() { }; |
| 66 }; |
| 67 |
| 68 inline region_operator(int op, const region& lhs, const region& rhs) |
| 69 : op_mask(op), spanner(lhs, rhs) |
| 70 { |
| 71 } |
| 72 |
| 73 void operator()(region_rasterizer& rasterizer) { |
| 74 RECT current; |
| 75 do { |
| 76 SpannerInner spannerInner(spanner.lhs, spanner.rhs); |
| 77 int inside = spanner.next(current.fTop, current.fBottom); |
| 78 spannerInner.prepare(inside); |
| 79 do { |
| 80 TYPE left, right; |
| 81 int inside = spannerInner.next(current.fLeft, current.fRight); |
| 82 if ((op_mask >> inside) & 1) { |
| 83 if (current.fLeft < current.fRight && |
| 84 current.fTop < current.fBottom) { |
| 85 rasterizer(current); |
| 86 } |
| 87 } |
| 88 } while(!spannerInner.isDone()); |
| 89 } while(!spanner.isDone()); |
| 90 } |
| 91 |
| 92 private: |
| 93 uint32_t op_mask; |
| 94 |
| 95 class SpannerBase |
| 96 { |
| 97 public: |
| 98 SpannerBase() |
| 99 : lhs_head(max_value), lhs_tail(max_value), |
| 100 rhs_head(max_value), rhs_tail(max_value) { |
| 101 } |
| 102 |
| 103 enum { |
| 104 lhs_before_rhs = 0, |
| 105 lhs_after_rhs = 1, |
| 106 lhs_coincide_rhs = 2 |
| 107 }; |
| 108 |
| 109 protected: |
| 110 TYPE lhs_head; |
| 111 TYPE lhs_tail; |
| 112 TYPE rhs_head; |
| 113 TYPE rhs_tail; |
| 114 |
| 115 inline int next(TYPE& head, TYPE& tail, |
| 116 bool& more_lhs, bool& more_rhs) |
| 117 { |
| 118 int inside; |
| 119 more_lhs = false; |
| 120 more_rhs = false; |
| 121 if (lhs_head < rhs_head) { |
| 122 inside = lhs_before_rhs; |
| 123 head = lhs_head; |
| 124 if (lhs_tail <= rhs_head) { |
| 125 tail = lhs_tail; |
| 126 more_lhs = true; |
| 127 } else { |
| 128 lhs_head = rhs_head; |
| 129 tail = rhs_head; |
| 130 } |
| 131 } else if (rhs_head < lhs_head) { |
| 132 inside = lhs_after_rhs; |
| 133 head = rhs_head; |
| 134 if (rhs_tail <= lhs_head) { |
| 135 tail = rhs_tail; |
| 136 more_rhs = true; |
| 137 } else { |
| 138 rhs_head = lhs_head; |
| 139 tail = lhs_head; |
| 140 } |
| 141 } else { |
| 142 inside = lhs_coincide_rhs; |
| 143 head = lhs_head; |
| 144 if (lhs_tail <= rhs_tail) { |
| 145 tail = rhs_head = lhs_tail; |
| 146 more_lhs = true; |
| 147 } |
| 148 if (rhs_tail <= lhs_tail) { |
| 149 tail = lhs_head = rhs_tail; |
| 150 more_rhs = true; |
| 151 } |
| 152 } |
| 153 return inside; |
| 154 } |
| 155 }; |
| 156 |
| 157 class Spanner : protected SpannerBase |
| 158 { |
| 159 friend class region_operator; |
| 160 region lhs; |
| 161 region rhs; |
| 162 |
| 163 public: |
| 164 inline Spanner(const region& lhs, const region& rhs) |
| 165 : lhs(lhs), rhs(rhs) |
| 166 { |
| 167 if (lhs.count) { |
| 168 SpannerBase::lhs_head = lhs.rects->fTop + lhs.dy; |
| 169 SpannerBase::lhs_tail = lhs.rects->fBottom + lhs.dy; |
| 170 } |
| 171 if (rhs.count) { |
| 172 SpannerBase::rhs_head = rhs.rects->fTop + rhs.dy; |
| 173 SpannerBase::rhs_tail = rhs.rects->fBottom + rhs.dy; |
| 174 } |
| 175 } |
| 176 |
| 177 inline bool isDone() const { |
| 178 return !rhs.count && !lhs.count; |
| 179 } |
| 180 |
| 181 inline int next(TYPE& top, TYPE& bottom) |
| 182 { |
| 183 bool more_lhs = false; |
| 184 bool more_rhs = false; |
| 185 int inside = SpannerBase::next(top, bottom, more_lhs, more_rhs); |
| 186 if (more_lhs) { |
| 187 advance(lhs, SpannerBase::lhs_head, SpannerBase::lhs_tail); |
| 188 } |
| 189 if (more_rhs) { |
| 190 advance(rhs, SpannerBase::rhs_head, SpannerBase::rhs_tail); |
| 191 } |
| 192 return inside; |
| 193 } |
| 194 |
| 195 private: |
| 196 static inline |
| 197 void advance(region& reg, TYPE& aTop, TYPE& aBottom) { |
| 198 // got to next span |
| 199 size_t count = reg.count; |
| 200 RECT const * rects = reg.rects; |
| 201 RECT const * const end = rects + count; |
| 202 const int top = rects->fTop; |
| 203 while (rects != end && rects->fTop == top) { |
| 204 rects++; |
| 205 count--; |
| 206 } |
| 207 if (rects != end) { |
| 208 aTop = rects->fTop + reg.dy; |
| 209 aBottom = rects->fBottom + reg.dy; |
| 210 } else { |
| 211 aTop = max_value; |
| 212 aBottom = max_value; |
| 213 } |
| 214 reg.rects = rects; |
| 215 reg.count = count; |
| 216 } |
| 217 }; |
| 218 |
| 219 class SpannerInner : protected SpannerBase |
| 220 { |
| 221 region lhs; |
| 222 region rhs; |
| 223 |
| 224 public: |
| 225 inline SpannerInner(const region& lhs, const region& rhs) |
| 226 : lhs(lhs), rhs(rhs) |
| 227 { |
| 228 } |
| 229 |
| 230 inline void prepare(int inside) { |
| 231 if (inside == SpannerBase::lhs_before_rhs) { |
| 232 if (lhs.count) { |
| 233 SpannerBase::lhs_head = lhs.rects->fLeft + lhs.dx; |
| 234 SpannerBase::lhs_tail = lhs.rects->fRight + lhs.dx; |
| 235 } |
| 236 SpannerBase::rhs_head = max_value; |
| 237 SpannerBase::rhs_tail = max_value; |
| 238 } else if (inside == SpannerBase::lhs_after_rhs) { |
| 239 SpannerBase::lhs_head = max_value; |
| 240 SpannerBase::lhs_tail = max_value; |
| 241 if (rhs.count) { |
| 242 SpannerBase::rhs_head = rhs.rects->fLeft + rhs.dx; |
| 243 SpannerBase::rhs_tail = rhs.rects->fRight + rhs.dx; |
| 244 } |
| 245 } else { |
| 246 if (lhs.count) { |
| 247 SpannerBase::lhs_head = lhs.rects->fLeft + lhs.dx; |
| 248 SpannerBase::lhs_tail = lhs.rects->fRight + lhs.dx; |
| 249 } |
| 250 if (rhs.count) { |
| 251 SpannerBase::rhs_head = rhs.rects->fLeft + rhs.dx; |
| 252 SpannerBase::rhs_tail = rhs.rects->fRight + rhs.dx; |
| 253 } |
| 254 } |
| 255 } |
| 256 |
| 257 inline bool isDone() const { |
| 258 return SpannerBase::lhs_head == max_value && |
| 259 SpannerBase::rhs_head == max_value; |
| 260 } |
| 261 |
| 262 inline int next(TYPE& left, TYPE& right) |
| 263 { |
| 264 bool more_lhs = false; |
| 265 bool more_rhs = false; |
| 266 int inside = SpannerBase::next(left, right, more_lhs, more_rhs); |
| 267 if (more_lhs) { |
| 268 advance(lhs, SpannerBase::lhs_head, SpannerBase::lhs_tail); |
| 269 } |
| 270 if (more_rhs) { |
| 271 advance(rhs, SpannerBase::rhs_head, SpannerBase::rhs_tail); |
| 272 } |
| 273 return inside; |
| 274 } |
| 275 |
| 276 private: |
| 277 static inline |
| 278 void advance(region& reg, TYPE& left, TYPE& right) { |
| 279 if (reg.rects && reg.count) { |
| 280 const int cur_span_top = reg.rects->fTop; |
| 281 reg.rects++; |
| 282 reg.count--; |
| 283 if (!reg.count || reg.rects->fTop != cur_span_top) { |
| 284 left = max_value; |
| 285 right = max_value; |
| 286 } else { |
| 287 left = reg.rects->fLeft + reg.dx; |
| 288 right = reg.rects->fRight + reg.dx; |
| 289 } |
| 290 } |
| 291 } |
| 292 }; |
| 293 |
| 294 Spanner spanner; |
| 295 }; |
| 296 |
| 297 // ---------------------------------------------------------------------------- |
| 298 }; |
| 299 |
| 300 #endif /* ANDROID_UI_PRIVATE_REGION_HELPER_H */ |
| OLD | NEW |