gfx/skia/trunk/src/core/SkRegionPriv.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkRegionPriv.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkRegionPriv_DEFINED
    1.14 +#define SkRegionPriv_DEFINED
    1.15 +
    1.16 +#include "SkRegion.h"
    1.17 +#include "SkThread.h"
    1.18 +
    1.19 +#define assert_sentinel(value, isSentinel) \
    1.20 +    SkASSERT(((value) == SkRegion::kRunTypeSentinel) == isSentinel)
    1.21 +
    1.22 +//SkDEBUGCODE(extern int32_t gRgnAllocCounter;)
    1.23 +
    1.24 +#ifdef SK_DEBUG
    1.25 +// Given the first interval (just past the interval-count), compute the
    1.26 +// interval count, by search for the x-sentinel
    1.27 +//
    1.28 +static int compute_intervalcount(const SkRegion::RunType runs[]) {
    1.29 +    const SkRegion::RunType* curr = runs;
    1.30 +    while (*curr < SkRegion::kRunTypeSentinel) {
    1.31 +        SkASSERT(curr[0] < curr[1]);
    1.32 +        SkASSERT(curr[1] < SkRegion::kRunTypeSentinel);
    1.33 +        curr += 2;
    1.34 +    }
    1.35 +    return (curr - runs) >> 1;
    1.36 +}
    1.37 +#endif
    1.38 +
    1.39 +struct SkRegion::RunHead {
    1.40 +private:
    1.41 +
    1.42 +public:
    1.43 +    int32_t fRefCnt;
    1.44 +    int32_t fRunCount;
    1.45 +
    1.46 +    /**
    1.47 +     *  Number of spans with different Y values. This does not count the initial
    1.48 +     *  Top value, nor does it count the final Y-Sentinel value. In the logical
    1.49 +     *  case of a rectangle, this would return 1, and an empty region would
    1.50 +     *  return 0.
    1.51 +     */
    1.52 +    int getYSpanCount() const {
    1.53 +        return fYSpanCount;
    1.54 +    }
    1.55 +
    1.56 +    /**
    1.57 +     *  Number of intervals in the entire region. This equals the number of
    1.58 +     *  rects that would be returned by the Iterator. In the logical case of
    1.59 +     *  a rect, this would return 1, and an empty region would return 0.
    1.60 +     */
    1.61 +    int getIntervalCount() const {
    1.62 +        return fIntervalCount;
    1.63 +    }
    1.64 +
    1.65 +    static RunHead* Alloc(int count) {
    1.66 +        //SkDEBUGCODE(sk_atomic_inc(&gRgnAllocCounter);)
    1.67 +        //SkDEBUGF(("************** gRgnAllocCounter::alloc %d\n", gRgnAllocCounter));
    1.68 +
    1.69 +        SkASSERT(count >= SkRegion::kRectRegionRuns);
    1.70 +
    1.71 +        RunHead* head = (RunHead*)sk_malloc_throw(sizeof(RunHead) + count * sizeof(RunType));
    1.72 +        head->fRefCnt = 1;
    1.73 +        head->fRunCount = count;
    1.74 +        // these must be filled in later, otherwise we will be invalid
    1.75 +        head->fYSpanCount = 0;
    1.76 +        head->fIntervalCount = 0;
    1.77 +        return head;
    1.78 +    }
    1.79 +
    1.80 +    static RunHead* Alloc(int count, int yspancount, int intervalCount) {
    1.81 +        SkASSERT(yspancount > 0);
    1.82 +        SkASSERT(intervalCount > 1);
    1.83 +
    1.84 +        RunHead* head = Alloc(count);
    1.85 +        head->fYSpanCount = yspancount;
    1.86 +        head->fIntervalCount = intervalCount;
    1.87 +        return head;
    1.88 +    }
    1.89 +
    1.90 +    SkRegion::RunType* writable_runs() {
    1.91 +        SkASSERT(fRefCnt == 1);
    1.92 +        return (SkRegion::RunType*)(this + 1);
    1.93 +    }
    1.94 +
    1.95 +    const SkRegion::RunType* readonly_runs() const {
    1.96 +        return (const SkRegion::RunType*)(this + 1);
    1.97 +    }
    1.98 +
    1.99 +    RunHead* ensureWritable() {
   1.100 +        RunHead* writable = this;
   1.101 +        if (fRefCnt > 1) {
   1.102 +            // We need to alloc & copy the current region before we call
   1.103 +            // sk_atomic_dec because it could be freed in the meantime,
   1.104 +            // otherwise.
   1.105 +            writable = Alloc(fRunCount, fYSpanCount, fIntervalCount);
   1.106 +            memcpy(writable->writable_runs(), this->readonly_runs(),
   1.107 +                   fRunCount * sizeof(RunType));
   1.108 +
   1.109 +            // fRefCount might have changed since we last checked.
   1.110 +            // If we own the last reference at this point, we need to
   1.111 +            // free the memory.
   1.112 +            if (sk_atomic_dec(&fRefCnt) == 1) {
   1.113 +                sk_free(this);
   1.114 +            }
   1.115 +        }
   1.116 +        return writable;
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     *  Given a scanline (including its Bottom value at runs[0]), return the next
   1.121 +     *  scanline. Asserts that there is one (i.e. runs[0] < Sentinel)
   1.122 +     */
   1.123 +    static SkRegion::RunType* SkipEntireScanline(const SkRegion::RunType runs[]) {
   1.124 +        // we are not the Y Sentinel
   1.125 +        SkASSERT(runs[0] < SkRegion::kRunTypeSentinel);
   1.126 +
   1.127 +        const int intervals = runs[1];
   1.128 +        SkASSERT(runs[2 + intervals * 2] == SkRegion::kRunTypeSentinel);
   1.129 +#ifdef SK_DEBUG
   1.130 +        {
   1.131 +            int n = compute_intervalcount(&runs[2]);
   1.132 +            SkASSERT(n == intervals);
   1.133 +        }
   1.134 +#endif
   1.135 +
   1.136 +        // skip the entire line [B N [L R] S]
   1.137 +        runs += 1 + 1 + intervals * 2 + 1;
   1.138 +        return const_cast<SkRegion::RunType*>(runs);
   1.139 +    }
   1.140 +
   1.141 +
   1.142 +    /**
   1.143 +     *  Return the scanline that contains the Y value. This requires that the Y
   1.144 +     *  value is already known to be contained within the bounds of the region,
   1.145 +     *  and so this routine never returns NULL.
   1.146 +     *
   1.147 +     *  It returns the beginning of the scanline, starting with its Bottom value.
   1.148 +     */
   1.149 +    SkRegion::RunType* findScanline(int y) const {
   1.150 +        const RunType* runs = this->readonly_runs();
   1.151 +
   1.152 +        // if the top-check fails, we didn't do a quick check on the bounds
   1.153 +        SkASSERT(y >= runs[0]);
   1.154 +
   1.155 +        runs += 1;  // skip top-Y
   1.156 +        for (;;) {
   1.157 +            int bottom = runs[0];
   1.158 +            // If we hit this, we've walked off the region, and our bounds check
   1.159 +            // failed.
   1.160 +            SkASSERT(bottom < SkRegion::kRunTypeSentinel);
   1.161 +            if (y < bottom) {
   1.162 +                break;
   1.163 +            }
   1.164 +            runs = SkipEntireScanline(runs);
   1.165 +        }
   1.166 +        return const_cast<SkRegion::RunType*>(runs);
   1.167 +    }
   1.168 +
   1.169 +    // Copy src runs into us, computing interval counts and bounds along the way
   1.170 +    void computeRunBounds(SkIRect* bounds) {
   1.171 +        RunType* runs = this->writable_runs();
   1.172 +        bounds->fTop = *runs++;
   1.173 +
   1.174 +        int bot;
   1.175 +        int ySpanCount = 0;
   1.176 +        int intervalCount = 0;
   1.177 +        int left = SK_MaxS32;
   1.178 +        int rite = SK_MinS32;
   1.179 +
   1.180 +        do {
   1.181 +            bot = *runs++;
   1.182 +            SkASSERT(bot < SkRegion::kRunTypeSentinel);
   1.183 +            ySpanCount += 1;
   1.184 +
   1.185 +            const int intervals = *runs++;
   1.186 +            SkASSERT(intervals >= 0);
   1.187 +            SkASSERT(intervals < SkRegion::kRunTypeSentinel);
   1.188 +
   1.189 +            if (intervals > 0) {
   1.190 +#ifdef SK_DEBUG
   1.191 +                {
   1.192 +                    int n = compute_intervalcount(runs);
   1.193 +                    SkASSERT(n == intervals);
   1.194 +                }
   1.195 +#endif
   1.196 +                RunType L = runs[0];
   1.197 +                SkASSERT(L < SkRegion::kRunTypeSentinel);
   1.198 +                if (left > L) {
   1.199 +                    left = L;
   1.200 +                }
   1.201 +
   1.202 +                runs += intervals * 2;
   1.203 +                RunType R = runs[-1];
   1.204 +                SkASSERT(R < SkRegion::kRunTypeSentinel);
   1.205 +                if (rite < R) {
   1.206 +                    rite = R;
   1.207 +                }
   1.208 +
   1.209 +                intervalCount += intervals;
   1.210 +            }
   1.211 +            SkASSERT(SkRegion::kRunTypeSentinel == *runs);
   1.212 +            runs += 1;  // skip x-sentinel
   1.213 +
   1.214 +            // test Y-sentinel
   1.215 +        } while (SkRegion::kRunTypeSentinel > *runs);
   1.216 +
   1.217 +#ifdef SK_DEBUG
   1.218 +        // +1 to skip the last Y-sentinel
   1.219 +        int runCount = runs - this->writable_runs() + 1;
   1.220 +        SkASSERT(runCount == fRunCount);
   1.221 +#endif
   1.222 +
   1.223 +        fYSpanCount = ySpanCount;
   1.224 +        fIntervalCount = intervalCount;
   1.225 +
   1.226 +        bounds->fLeft = left;
   1.227 +        bounds->fRight = rite;
   1.228 +        bounds->fBottom = bot;
   1.229 +    }
   1.230 +
   1.231 +private:
   1.232 +    int32_t fYSpanCount;
   1.233 +    int32_t fIntervalCount;
   1.234 +};
   1.235 +
   1.236 +#endif

mercurial