Region.cpp revision 3e010f3138593cc6953039ee0e3db8ee31881296
1cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten/* 2cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Copyright (C) 2007 The Android Open Source Project 3cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 4cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License"); 5cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * you may not use this file except in compliance with the License. 6cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * You may obtain a copy of the License at 7cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 8cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * http://www.apache.org/licenses/LICENSE-2.0 9cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 10cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Unless required by applicable law or agreed to in writing, software 11cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS, 12cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * See the License for the specific language governing permissions and 14cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * limitations under the License. 15cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten */ 16cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 1763c002ab68761be0eace98f28320d8eb2f3f7695Jean-Michel Trivi#define LOG_TAG "Region" 1847550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten 196cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi#include <limits.h> 2063c002ab68761be0eace98f28320d8eb2f3f7695Jean-Michel Trivi 2163c002ab68761be0eace98f28320d8eb2f3f7695Jean-Michel Trivi#include <utils/Log.h> 2263c002ab68761be0eace98f28320d8eb2f3f7695Jean-Michel Trivi#include <utils/String8.h> 2363c002ab68761be0eace98f28320d8eb2f3f7695Jean-Michel Trivi#include <utils/CallStack.h> 24cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 25cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#include <ui/Rect.h> 26cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#include <ui/Region.h> 27cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#include <ui/Point.h> 28cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 29cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#include <private/ui/RegionHelper.h> 30cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 31cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 32cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#define VALIDATE_REGIONS (false) 33cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#define VALIDATE_WITH_CORECG (false) 34cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 35cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 36cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#if VALIDATE_WITH_CORECG 37cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#include <core/SkRegion.h> 38cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#endif 39cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 40cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastennamespace android { 41cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 42cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 43cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenenum { 44cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten op_nand = region_operator<Rect>::op_nand, 45cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten op_and = region_operator<Rect>::op_and, 46cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten op_or = region_operator<Rect>::op_or, 47cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten op_xor = region_operator<Rect>::op_xor 48cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten}; 49cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 50cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenenum { 51cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten direction_LTR, 52cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten direction_RTL 53cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten}; 54cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 55cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 56cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 57cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion::Region() { 58cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(Rect(0,0)); 59cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 60cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 61cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion::Region(const Region& rhs) 62cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten : mStorage(rhs.mStorage) 63cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 64cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#if VALIDATE_REGIONS 65cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten validate(rhs, "rhs copy-ctor"); 66cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#endif 67d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi} 68cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 69cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion::Region(const Rect& rhs) { 70cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(rhs); 71cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 72cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 73cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion::~Region() 741a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten{ 7547550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten} 7699b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten 771a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten/** 78cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way 79cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 80cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst 81cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be 82cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * compared with the span directly below, and subdivided as needed to resolve T-junctions. 83cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 84cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * The resulting temporary vector will be a completely reversed copy of the original, without any 85cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * bottom-up T-junctions. 86cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 87cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Second pass through, divideSpanRTL will be false since the previous span will index into the 88cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * final, correctly ordered region buffer. Each rectangle will be compared with the span directly 89cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * above it, and subdivided to resolve any remaining T-junctions. 90cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten */ 91cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenstatic void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end, 92cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten Vector<Rect>& dst, int spanDirection) { 93cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.clear(); 94b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi 95f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi const Rect* current = end - 1; 96b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi int lastTop = current->top; 97cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 98cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // add first span immediately 99cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten do { 100cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.add(*current); 10147550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten current--; 102cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } while (current->top == lastTop && current >= begin); 10347550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten 1046cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi unsigned int beginLastSpan = -1; 105d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi unsigned int endLastSpan = -1; 106cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten int top = -1; 107cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten int bottom = -1; 108b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi 109cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // for all other spans, split if a t-junction exists in the span directly above 110cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten while (current >= begin) { 111cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (current->top != (current + 1)->top) { 112cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // new span 113cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) || 114cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) { 11547550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten // previous span not directly adjacent, don't check for T junctions 11647550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten beginLastSpan = INT_MAX; 117cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } else { 118cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten beginLastSpan = endLastSpan + 1; 119cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 120cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten endLastSpan = dst.size() - 1; 121cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 122cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten top = current->top; 123cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten bottom = current->bottom; 124cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 125cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten int left = current->left; 126cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten int right = current->right; 127cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 128cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) { 129cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten const Rect* prev = &dst[prevIndex]; 130cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (spanDirection == direction_RTL) { 131cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // iterating over previous span RTL, quit if it's too far left 132cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->right <= left) break; 133cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 134cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->right > left && prev->right < right) { 135cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.add(Rect(prev->right, top, right, bottom)); 136cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten right = prev->right; 137cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 138cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 139cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->left > left && prev->left < right) { 140cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.add(Rect(prev->left, top, right, bottom)); 141cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten right = prev->left; 142cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 143cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 144cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // if an entry in the previous span is too far right, nothing further left in the 145cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // current span will need it 146cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->left >= right) { 1471a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten beginLastSpan = prevIndex; 14847550bf6cf5cf08a402a54b1589f4b64582a5120Glenn Kasten } 1491a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten } else { 150cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // iterating over previous span LTR, quit if it's too far right 151cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->left >= right) break; 152b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi 153b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi if (prev->left > left && prev->left < right) { 154b2aeb0f1009555181dabb944fe05901cb6e6f632Jean-Michel Trivi dst.add(Rect(left, top, prev->left, bottom)); 155cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten left = prev->left; 156cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 157cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 158cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->right > left && prev->right < right) { 159cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.add(Rect(left, top, prev->right, bottom)); 160cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten left = prev->right; 161cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 162cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten // if an entry in the previous span is too far left, nothing further right in the 1636e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi // current span will need it 164cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (prev->right <= left) { 1656e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi beginLastSpan = prevIndex; 166cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 167cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 168cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 169cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 170cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (left < right) { 171cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten dst.add(Rect(left, top, right, bottom)); 172cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 173cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 174cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten current--; 175cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 176cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 177cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 178cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten/** 179cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Creates a new region with the same data as the argument, but divides rectangles as necessary to 180cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * remove T-Junctions 181cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * 182cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * Note: the output will not necessarily be a very efficient representation of the region, since it 183cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten * may be that a triangle-based approach would generate significantly simpler geometry 1846e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi */ 1856e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviRegion Region::createTJunctionFreeRegion(const Region& r) { 186cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (r.isEmpty()) return r; 187cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (r.isRect()) return r; 188cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 189faf90312d2156acbf27c62e114fd180708aa7654Glenn Kasten Vector<Rect> reversed; 190cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL); 191cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 192cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten Region outputRegion; 193cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten reverseRectsResolvingJunctions(reversed.begin(), reversed.end(), 194cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten outputRegion.mStorage, direction_LTR); 195cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds 196cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 197cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#if VALIDATE_REGIONS 198cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten validate(outputRegion, "T-Junction free region"); 199cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#endif 200cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 201cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return outputRegion; 202cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 203cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 204cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::operator = (const Region& rhs) 205cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 206cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#if VALIDATE_REGIONS 207cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten validate(*this, "this->operator="); 208cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten validate(rhs, "rhs.operator="); 209cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten#endif 210cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage = rhs.mStorage; 211cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return *this; 212cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 213cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 214cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::makeBoundsSelf() 215cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 216cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (mStorage.size() >= 2) { 217cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten const Rect bounds(getBounds()); 218cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.clear(); 219cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(bounds); 220cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten } 221cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return *this; 222cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 223cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 224cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenvoid Region::clear() 225cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 226cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.clear(); 227cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(Rect(0,0)); 228cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 229cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 230cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenvoid Region::set(const Rect& r) 231cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 232cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.clear(); 233cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(r); 234cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 235cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 236cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenvoid Region::set(uint32_t w, uint32_t h) 237cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 238cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.clear(); 239cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.add(Rect(w,h)); 240cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 241cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 242cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 243cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 244cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenvoid Region::addRectUnchecked(int l, int t, int r, int b) 245cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten{ 246cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten Rect rect(l,t,r,b); 247cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten size_t where = mStorage.size() - 1; 248cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten mStorage.insertAt(rect, where, 1); 249cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 250cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 251cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 252cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 253cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::orSelf(const Rect& r) { 254cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(r, op_or); 255cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 256cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::xorSelf(const Rect& r) { 257cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(r, op_xor); 258cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 259cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::andSelf(const Rect& r) { 260cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(r, op_and); 261cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 262cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::subtractSelf(const Rect& r) { 263cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(r, op_nand); 264cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 265cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::operationSelf(const Rect& r, int op) { 266cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten Region lhs(*this); 267cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten boolean_operation(op, *this, lhs, r); 268cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return *this; 269cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 270cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 271cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 272cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 273cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::orSelf(const Region& rhs) { 274cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(rhs, op_or); 275cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 276cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::xorSelf(const Region& rhs) { 277cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(rhs, op_xor); 278cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 279cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::andSelf(const Region& rhs) { 280cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(rhs, op_and); 281cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 282cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::subtractSelf(const Region& rhs) { 283cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operationSelf(rhs, op_nand); 284cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 285cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::operationSelf(const Region& rhs, int op) { 286cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten Region lhs(*this); 287cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten boolean_operation(op, *this, lhs, rhs); 288cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return *this; 289cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 290cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 291cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn KastenRegion& Region::translateSelf(int x, int y) { 292cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten if (x|y) translate(*this, x, y); 293cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return *this; 294cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 295cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 296cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten// ---------------------------------------------------------------------------- 297cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten 298cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenconst Region Region::merge(const Rect& rhs) const { 299cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operation(rhs, op_or); 300cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 301cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenconst Region Region::mergeExclusive(const Rect& rhs) const { 302cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operation(rhs, op_xor); 303cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 304cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kastenconst Region Region::intersect(const Rect& rhs) const { 305cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten return operation(rhs, op_and); 306cafa51fdd8b3f29ebaa0682070100825a9cce2a8Glenn Kasten} 3078b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kastenconst Region Region::subtract(const Rect& rhs) const { 3088b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten return operation(rhs, op_nand); 3098b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten} 3108b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kastenconst Region Region::operation(const Rect& rhs, int op) const { 31192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi Region result; 31292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi boolean_operation(op, result, *this, rhs); 31392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi return result; 31437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi} 31592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi 3168b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten// ---------------------------------------------------------------------------- 3178b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten 318d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Triviconst Region Region::merge(const Region& rhs) const { 31994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten return operation(rhs, op_or); 320321f2cffd7dd560bf2e5c898be6953e19bed8496Jean-Michel Trivi} 32137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Triviconst Region Region::mergeExclusive(const Region& rhs) const { 32292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi return operation(rhs, op_xor); 323eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi} 324eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Triviconst Region Region::intersect(const Region& rhs) const { 325eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi return operation(rhs, op_and); 326d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi} 3278b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kastenconst Region Region::subtract(const Region& rhs) const { 3288b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten return operation(rhs, op_nand); 3298b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten} 3308b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kastenconst Region Region::operation(const Region& rhs, int op) const { 3318b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten Region result; 3328b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten boolean_operation(op, result, *this, rhs); 33399b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten return result; 33499b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten} 33594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten 33668d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Triviconst Region Region::translate(int x, int y) const { 337b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi Region result; 338f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi translate(result, *this, x, y); 339b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi return result; 340f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi} 341f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi 342f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi// ---------------------------------------------------------------------------- 343f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi 34494a37e8117fb72790882dfb815f99e2365754c74Glenn KastenRegion& Region::orSelf(const Region& rhs, int dx, int dy) { 3458b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten return operationSelf(rhs, dx, dy, op_or); 346} 347Region& Region::xorSelf(const Region& rhs, int dx, int dy) { 348 return operationSelf(rhs, dx, dy, op_xor); 349} 350Region& Region::andSelf(const Region& rhs, int dx, int dy) { 351 return operationSelf(rhs, dx, dy, op_and); 352} 353Region& Region::subtractSelf(const Region& rhs, int dx, int dy) { 354 return operationSelf(rhs, dx, dy, op_nand); 355} 356Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) { 357 Region lhs(*this); 358 boolean_operation(op, *this, lhs, rhs, dx, dy); 359 return *this; 360} 361 362// ---------------------------------------------------------------------------- 363 364const Region Region::merge(const Region& rhs, int dx, int dy) const { 365 return operation(rhs, dx, dy, op_or); 366} 367const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const { 368 return operation(rhs, dx, dy, op_xor); 369} 370const Region Region::intersect(const Region& rhs, int dx, int dy) const { 371 return operation(rhs, dx, dy, op_and); 372} 373const Region Region::subtract(const Region& rhs, int dx, int dy) const { 374 return operation(rhs, dx, dy, op_nand); 375} 376const Region Region::operation(const Region& rhs, int dx, int dy, int op) const { 377 Region result; 378 boolean_operation(op, result, *this, rhs, dx, dy); 379 return result; 380} 381 382// ---------------------------------------------------------------------------- 383 384// This is our region rasterizer, which merges rects and spans together 385// to obtain an optimal region. 386class Region::rasterizer : public region_operator<Rect>::region_rasterizer 387{ 388 Rect bounds; 389 Vector<Rect>& storage; 390 Rect* head; 391 Rect* tail; 392 Vector<Rect> span; 393 Rect* cur; 394public: 395 rasterizer(Region& reg) 396 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() { 397 storage.clear(); 398 } 399 400 ~rasterizer() { 401 if (span.size()) { 402 flushSpan(); 403 } 404 if (storage.size()) { 405 bounds.top = storage.itemAt(0).top; 406 bounds.bottom = storage.top().bottom; 407 if (storage.size() == 1) { 408 storage.clear(); 409 } 410 } else { 411 bounds.left = 0; 412 bounds.right = 0; 413 } 414 storage.add(bounds); 415 } 416 417 virtual void operator()(const Rect& rect) { 418 //ALOGD(">>> %3d, %3d, %3d, %3d", 419 // rect.left, rect.top, rect.right, rect.bottom); 420 if (span.size()) { 421 if (cur->top != rect.top) { 422 flushSpan(); 423 } else if (cur->right == rect.left) { 424 cur->right = rect.right; 425 return; 426 } 427 } 428 span.add(rect); 429 cur = span.editArray() + (span.size() - 1); 430 } 431private: 432 template<typename T> 433 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; } 434 template<typename T> 435 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; } 436 void flushSpan() { 437 bool merge = false; 438 if (tail-head == ssize_t(span.size())) { 439 Rect const* p = span.editArray(); 440 Rect const* q = head; 441 if (p->top == q->bottom) { 442 merge = true; 443 while (q != tail) { 444 if ((p->left != q->left) || (p->right != q->right)) { 445 merge = false; 446 break; 447 } 448 p++, q++; 449 } 450 } 451 } 452 if (merge) { 453 const int bottom = span[0].bottom; 454 Rect* r = head; 455 while (r != tail) { 456 r->bottom = bottom; 457 r++; 458 } 459 } else { 460 bounds.left = min(span.itemAt(0).left, bounds.left); 461 bounds.right = max(span.top().right, bounds.right); 462 storage.appendVector(span); 463 tail = storage.editArray() + storage.size(); 464 head = tail - span.size(); 465 } 466 span.clear(); 467 } 468}; 469 470bool Region::validate(const Region& reg, const char* name, bool silent) 471{ 472 bool result = true; 473 const_iterator cur = reg.begin(); 474 const_iterator const tail = reg.end(); 475 const_iterator prev = cur; 476 Rect b(*prev); 477 while (cur != tail) { 478 if (cur->isValid() == false) { 479 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name); 480 result = false; 481 } 482 if (cur->right > region_operator<Rect>::max_value) { 483 ALOGE_IF(!silent, "%s: rect->right > max_value", name); 484 result = false; 485 } 486 if (cur->bottom > region_operator<Rect>::max_value) { 487 ALOGE_IF(!silent, "%s: rect->right > max_value", name); 488 result = false; 489 } 490 if (prev != cur) { 491 b.left = b.left < cur->left ? b.left : cur->left; 492 b.top = b.top < cur->top ? b.top : cur->top; 493 b.right = b.right > cur->right ? b.right : cur->right; 494 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom; 495 if ((*prev < *cur) == false) { 496 ALOGE_IF(!silent, "%s: region's Rects not sorted", name); 497 result = false; 498 } 499 if (cur->top == prev->top) { 500 if (cur->bottom != prev->bottom) { 501 ALOGE_IF(!silent, "%s: invalid span %p", name, cur); 502 result = false; 503 } else if (cur->left < prev->right) { 504 ALOGE_IF(!silent, 505 "%s: spans overlap horizontally prev=%p, cur=%p", 506 name, prev, cur); 507 result = false; 508 } 509 } else if (cur->top < prev->bottom) { 510 ALOGE_IF(!silent, 511 "%s: spans overlap vertically prev=%p, cur=%p", 512 name, prev, cur); 513 result = false; 514 } 515 prev = cur; 516 } 517 cur++; 518 } 519 if (b != reg.getBounds()) { 520 result = false; 521 ALOGE_IF(!silent, 522 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name, 523 b.left, b.top, b.right, b.bottom, 524 reg.getBounds().left, reg.getBounds().top, 525 reg.getBounds().right, reg.getBounds().bottom); 526 } 527 if (reg.mStorage.size() == 2) { 528 result = false; 529 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name); 530 } 531 if (result == false && !silent) { 532 reg.dump(name); 533 CallStack stack; 534 stack.update(); 535 stack.dump(""); 536 } 537 return result; 538} 539 540void Region::boolean_operation(int op, Region& dst, 541 const Region& lhs, 542 const Region& rhs, int dx, int dy) 543{ 544#if VALIDATE_REGIONS 545 validate(lhs, "boolean_operation (before): lhs"); 546 validate(rhs, "boolean_operation (before): rhs"); 547 validate(dst, "boolean_operation (before): dst"); 548#endif 549 550 size_t lhs_count; 551 Rect const * const lhs_rects = lhs.getArray(&lhs_count); 552 553 size_t rhs_count; 554 Rect const * const rhs_rects = rhs.getArray(&rhs_count); 555 556 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); 557 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy); 558 region_operator<Rect> operation(op, lhs_region, rhs_region); 559 { // scope for rasterizer (dtor has side effects) 560 rasterizer r(dst); 561 operation(r); 562 } 563 564#if VALIDATE_REGIONS 565 validate(lhs, "boolean_operation: lhs"); 566 validate(rhs, "boolean_operation: rhs"); 567 validate(dst, "boolean_operation: dst"); 568#endif 569 570#if VALIDATE_WITH_CORECG 571 SkRegion sk_lhs; 572 SkRegion sk_rhs; 573 SkRegion sk_dst; 574 575 for (size_t i=0 ; i<lhs_count ; i++) 576 sk_lhs.op( 577 lhs_rects[i].left + dx, 578 lhs_rects[i].top + dy, 579 lhs_rects[i].right + dx, 580 lhs_rects[i].bottom + dy, 581 SkRegion::kUnion_Op); 582 583 for (size_t i=0 ; i<rhs_count ; i++) 584 sk_rhs.op( 585 rhs_rects[i].left + dx, 586 rhs_rects[i].top + dy, 587 rhs_rects[i].right + dx, 588 rhs_rects[i].bottom + dy, 589 SkRegion::kUnion_Op); 590 591 const char* name = "---"; 592 SkRegion::Op sk_op; 593 switch (op) { 594 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break; 595 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break; 596 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break; 597 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break; 598 } 599 sk_dst.op(sk_lhs, sk_rhs, sk_op); 600 601 if (sk_dst.isEmpty() && dst.isEmpty()) 602 return; 603 604 bool same = true; 605 Region::const_iterator head = dst.begin(); 606 Region::const_iterator const tail = dst.end(); 607 SkRegion::Iterator it(sk_dst); 608 while (!it.done()) { 609 if (head != tail) { 610 if ( 611 head->left != it.rect().fLeft || 612 head->top != it.rect().fTop || 613 head->right != it.rect().fRight || 614 head->bottom != it.rect().fBottom 615 ) { 616 same = false; 617 break; 618 } 619 } else { 620 same = false; 621 break; 622 } 623 head++; 624 it.next(); 625 } 626 627 if (head != tail) { 628 same = false; 629 } 630 631 if(!same) { 632 ALOGD("---\nregion boolean %s failed", name); 633 lhs.dump("lhs"); 634 rhs.dump("rhs"); 635 dst.dump("dst"); 636 ALOGD("should be"); 637 SkRegion::Iterator it(sk_dst); 638 while (!it.done()) { 639 ALOGD(" [%3d, %3d, %3d, %3d]", 640 it.rect().fLeft, 641 it.rect().fTop, 642 it.rect().fRight, 643 it.rect().fBottom); 644 it.next(); 645 } 646 } 647#endif 648} 649 650void Region::boolean_operation(int op, Region& dst, 651 const Region& lhs, 652 const Rect& rhs, int dx, int dy) 653{ 654 if (!rhs.isValid()) { 655 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}", 656 op, rhs.left, rhs.top, rhs.right, rhs.bottom); 657 return; 658 } 659 660#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS 661 boolean_operation(op, dst, lhs, Region(rhs), dx, dy); 662#else 663 size_t lhs_count; 664 Rect const * const lhs_rects = lhs.getArray(&lhs_count); 665 666 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); 667 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy); 668 region_operator<Rect> operation(op, lhs_region, rhs_region); 669 { // scope for rasterizer (dtor has side effects) 670 rasterizer r(dst); 671 operation(r); 672 } 673 674#endif 675} 676 677void Region::boolean_operation(int op, Region& dst, 678 const Region& lhs, const Region& rhs) 679{ 680 boolean_operation(op, dst, lhs, rhs, 0, 0); 681} 682 683void Region::boolean_operation(int op, Region& dst, 684 const Region& lhs, const Rect& rhs) 685{ 686 boolean_operation(op, dst, lhs, rhs, 0, 0); 687} 688 689void Region::translate(Region& reg, int dx, int dy) 690{ 691 if ((dx || dy) && !reg.isEmpty()) { 692#if VALIDATE_REGIONS 693 validate(reg, "translate (before)"); 694#endif 695 size_t count = reg.mStorage.size(); 696 Rect* rects = reg.mStorage.editArray(); 697 while (count) { 698 rects->translate(dx, dy); 699 rects++; 700 count--; 701 } 702#if VALIDATE_REGIONS 703 validate(reg, "translate (after)"); 704#endif 705 } 706} 707 708void Region::translate(Region& dst, const Region& reg, int dx, int dy) 709{ 710 dst = reg; 711 translate(dst, dx, dy); 712} 713 714// ---------------------------------------------------------------------------- 715 716size_t Region::getSize() const { 717 return mStorage.size() * sizeof(Rect); 718} 719 720status_t Region::flatten(void* buffer) const { 721#if VALIDATE_REGIONS 722 validate(*this, "Region::flatten"); 723#endif 724 Rect* rects = reinterpret_cast<Rect*>(buffer); 725 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect)); 726 return NO_ERROR; 727} 728 729status_t Region::unflatten(void const* buffer, size_t size) { 730 Region result; 731 if (size >= sizeof(Rect)) { 732 Rect const* rects = reinterpret_cast<Rect const*>(buffer); 733 size_t count = size / sizeof(Rect); 734 if (count > 0) { 735 result.mStorage.clear(); 736 ssize_t err = result.mStorage.insertAt(0, count); 737 if (err < 0) { 738 return status_t(err); 739 } 740 memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect)); 741 } 742 } 743#if VALIDATE_REGIONS 744 validate(result, "Region::unflatten"); 745#endif 746 747 if (!result.validate(result, "Region::unflatten", true)) { 748 ALOGE("Region::unflatten() failed, invalid region"); 749 return BAD_VALUE; 750 } 751 mStorage = result.mStorage; 752 return NO_ERROR; 753} 754 755// ---------------------------------------------------------------------------- 756 757Region::const_iterator Region::begin() const { 758 return mStorage.array(); 759} 760 761Region::const_iterator Region::end() const { 762 size_t numRects = isRect() ? 1 : mStorage.size() - 1; 763 return mStorage.array() + numRects; 764} 765 766Rect const* Region::getArray(size_t* count) const { 767 const_iterator const b(begin()); 768 const_iterator const e(end()); 769 if (count) *count = e-b; 770 return b; 771} 772 773SharedBuffer const* Region::getSharedBuffer(size_t* count) const { 774 // We can get to the SharedBuffer of a Vector<Rect> because Rect has 775 // a trivial destructor. 776 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array()); 777 if (count) { 778 size_t numRects = isRect() ? 1 : mStorage.size() - 1; 779 count[0] = numRects; 780 } 781 sb->acquire(); 782 return sb; 783} 784 785// ---------------------------------------------------------------------------- 786 787void Region::dump(String8& out, const char* what, uint32_t flags) const 788{ 789 (void)flags; 790 const_iterator head = begin(); 791 const_iterator const tail = end(); 792 793 size_t SIZE = 256; 794 char buffer[SIZE]; 795 796 snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n", 797 what, this, tail-head); 798 out.append(buffer); 799 while (head != tail) { 800 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n", 801 head->left, head->top, head->right, head->bottom); 802 out.append(buffer); 803 head++; 804 } 805} 806 807void Region::dump(const char* what, uint32_t flags) const 808{ 809 (void)flags; 810 const_iterator head = begin(); 811 const_iterator const tail = end(); 812 ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head); 813 while (head != tail) { 814 ALOGD(" [%3d, %3d, %3d, %3d]\n", 815 head->left, head->top, head->right, head->bottom); 816 head++; 817 } 818} 819 820// ---------------------------------------------------------------------------- 821 822}; // namespace android 823