Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #ifndef SkRegionPriv_DEFINED |
michael@0 | 11 | #define SkRegionPriv_DEFINED |
michael@0 | 12 | |
michael@0 | 13 | #include "SkRegion.h" |
michael@0 | 14 | #include "SkThread.h" |
michael@0 | 15 | |
michael@0 | 16 | #define assert_sentinel(value, isSentinel) \ |
michael@0 | 17 | SkASSERT(((value) == SkRegion::kRunTypeSentinel) == isSentinel) |
michael@0 | 18 | |
michael@0 | 19 | //SkDEBUGCODE(extern int32_t gRgnAllocCounter;) |
michael@0 | 20 | |
michael@0 | 21 | #ifdef SK_DEBUG |
michael@0 | 22 | // Given the first interval (just past the interval-count), compute the |
michael@0 | 23 | // interval count, by search for the x-sentinel |
michael@0 | 24 | // |
michael@0 | 25 | static int compute_intervalcount(const SkRegion::RunType runs[]) { |
michael@0 | 26 | const SkRegion::RunType* curr = runs; |
michael@0 | 27 | while (*curr < SkRegion::kRunTypeSentinel) { |
michael@0 | 28 | SkASSERT(curr[0] < curr[1]); |
michael@0 | 29 | SkASSERT(curr[1] < SkRegion::kRunTypeSentinel); |
michael@0 | 30 | curr += 2; |
michael@0 | 31 | } |
michael@0 | 32 | return (curr - runs) >> 1; |
michael@0 | 33 | } |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | struct SkRegion::RunHead { |
michael@0 | 37 | private: |
michael@0 | 38 | |
michael@0 | 39 | public: |
michael@0 | 40 | int32_t fRefCnt; |
michael@0 | 41 | int32_t fRunCount; |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Number of spans with different Y values. This does not count the initial |
michael@0 | 45 | * Top value, nor does it count the final Y-Sentinel value. In the logical |
michael@0 | 46 | * case of a rectangle, this would return 1, and an empty region would |
michael@0 | 47 | * return 0. |
michael@0 | 48 | */ |
michael@0 | 49 | int getYSpanCount() const { |
michael@0 | 50 | return fYSpanCount; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Number of intervals in the entire region. This equals the number of |
michael@0 | 55 | * rects that would be returned by the Iterator. In the logical case of |
michael@0 | 56 | * a rect, this would return 1, and an empty region would return 0. |
michael@0 | 57 | */ |
michael@0 | 58 | int getIntervalCount() const { |
michael@0 | 59 | return fIntervalCount; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static RunHead* Alloc(int count) { |
michael@0 | 63 | //SkDEBUGCODE(sk_atomic_inc(&gRgnAllocCounter);) |
michael@0 | 64 | //SkDEBUGF(("************** gRgnAllocCounter::alloc %d\n", gRgnAllocCounter)); |
michael@0 | 65 | |
michael@0 | 66 | SkASSERT(count >= SkRegion::kRectRegionRuns); |
michael@0 | 67 | |
michael@0 | 68 | RunHead* head = (RunHead*)sk_malloc_throw(sizeof(RunHead) + count * sizeof(RunType)); |
michael@0 | 69 | head->fRefCnt = 1; |
michael@0 | 70 | head->fRunCount = count; |
michael@0 | 71 | // these must be filled in later, otherwise we will be invalid |
michael@0 | 72 | head->fYSpanCount = 0; |
michael@0 | 73 | head->fIntervalCount = 0; |
michael@0 | 74 | return head; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | static RunHead* Alloc(int count, int yspancount, int intervalCount) { |
michael@0 | 78 | SkASSERT(yspancount > 0); |
michael@0 | 79 | SkASSERT(intervalCount > 1); |
michael@0 | 80 | |
michael@0 | 81 | RunHead* head = Alloc(count); |
michael@0 | 82 | head->fYSpanCount = yspancount; |
michael@0 | 83 | head->fIntervalCount = intervalCount; |
michael@0 | 84 | return head; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | SkRegion::RunType* writable_runs() { |
michael@0 | 88 | SkASSERT(fRefCnt == 1); |
michael@0 | 89 | return (SkRegion::RunType*)(this + 1); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | const SkRegion::RunType* readonly_runs() const { |
michael@0 | 93 | return (const SkRegion::RunType*)(this + 1); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | RunHead* ensureWritable() { |
michael@0 | 97 | RunHead* writable = this; |
michael@0 | 98 | if (fRefCnt > 1) { |
michael@0 | 99 | // We need to alloc & copy the current region before we call |
michael@0 | 100 | // sk_atomic_dec because it could be freed in the meantime, |
michael@0 | 101 | // otherwise. |
michael@0 | 102 | writable = Alloc(fRunCount, fYSpanCount, fIntervalCount); |
michael@0 | 103 | memcpy(writable->writable_runs(), this->readonly_runs(), |
michael@0 | 104 | fRunCount * sizeof(RunType)); |
michael@0 | 105 | |
michael@0 | 106 | // fRefCount might have changed since we last checked. |
michael@0 | 107 | // If we own the last reference at this point, we need to |
michael@0 | 108 | // free the memory. |
michael@0 | 109 | if (sk_atomic_dec(&fRefCnt) == 1) { |
michael@0 | 110 | sk_free(this); |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | return writable; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Given a scanline (including its Bottom value at runs[0]), return the next |
michael@0 | 118 | * scanline. Asserts that there is one (i.e. runs[0] < Sentinel) |
michael@0 | 119 | */ |
michael@0 | 120 | static SkRegion::RunType* SkipEntireScanline(const SkRegion::RunType runs[]) { |
michael@0 | 121 | // we are not the Y Sentinel |
michael@0 | 122 | SkASSERT(runs[0] < SkRegion::kRunTypeSentinel); |
michael@0 | 123 | |
michael@0 | 124 | const int intervals = runs[1]; |
michael@0 | 125 | SkASSERT(runs[2 + intervals * 2] == SkRegion::kRunTypeSentinel); |
michael@0 | 126 | #ifdef SK_DEBUG |
michael@0 | 127 | { |
michael@0 | 128 | int n = compute_intervalcount(&runs[2]); |
michael@0 | 129 | SkASSERT(n == intervals); |
michael@0 | 130 | } |
michael@0 | 131 | #endif |
michael@0 | 132 | |
michael@0 | 133 | // skip the entire line [B N [L R] S] |
michael@0 | 134 | runs += 1 + 1 + intervals * 2 + 1; |
michael@0 | 135 | return const_cast<SkRegion::RunType*>(runs); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Return the scanline that contains the Y value. This requires that the Y |
michael@0 | 141 | * value is already known to be contained within the bounds of the region, |
michael@0 | 142 | * and so this routine never returns NULL. |
michael@0 | 143 | * |
michael@0 | 144 | * It returns the beginning of the scanline, starting with its Bottom value. |
michael@0 | 145 | */ |
michael@0 | 146 | SkRegion::RunType* findScanline(int y) const { |
michael@0 | 147 | const RunType* runs = this->readonly_runs(); |
michael@0 | 148 | |
michael@0 | 149 | // if the top-check fails, we didn't do a quick check on the bounds |
michael@0 | 150 | SkASSERT(y >= runs[0]); |
michael@0 | 151 | |
michael@0 | 152 | runs += 1; // skip top-Y |
michael@0 | 153 | for (;;) { |
michael@0 | 154 | int bottom = runs[0]; |
michael@0 | 155 | // If we hit this, we've walked off the region, and our bounds check |
michael@0 | 156 | // failed. |
michael@0 | 157 | SkASSERT(bottom < SkRegion::kRunTypeSentinel); |
michael@0 | 158 | if (y < bottom) { |
michael@0 | 159 | break; |
michael@0 | 160 | } |
michael@0 | 161 | runs = SkipEntireScanline(runs); |
michael@0 | 162 | } |
michael@0 | 163 | return const_cast<SkRegion::RunType*>(runs); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | // Copy src runs into us, computing interval counts and bounds along the way |
michael@0 | 167 | void computeRunBounds(SkIRect* bounds) { |
michael@0 | 168 | RunType* runs = this->writable_runs(); |
michael@0 | 169 | bounds->fTop = *runs++; |
michael@0 | 170 | |
michael@0 | 171 | int bot; |
michael@0 | 172 | int ySpanCount = 0; |
michael@0 | 173 | int intervalCount = 0; |
michael@0 | 174 | int left = SK_MaxS32; |
michael@0 | 175 | int rite = SK_MinS32; |
michael@0 | 176 | |
michael@0 | 177 | do { |
michael@0 | 178 | bot = *runs++; |
michael@0 | 179 | SkASSERT(bot < SkRegion::kRunTypeSentinel); |
michael@0 | 180 | ySpanCount += 1; |
michael@0 | 181 | |
michael@0 | 182 | const int intervals = *runs++; |
michael@0 | 183 | SkASSERT(intervals >= 0); |
michael@0 | 184 | SkASSERT(intervals < SkRegion::kRunTypeSentinel); |
michael@0 | 185 | |
michael@0 | 186 | if (intervals > 0) { |
michael@0 | 187 | #ifdef SK_DEBUG |
michael@0 | 188 | { |
michael@0 | 189 | int n = compute_intervalcount(runs); |
michael@0 | 190 | SkASSERT(n == intervals); |
michael@0 | 191 | } |
michael@0 | 192 | #endif |
michael@0 | 193 | RunType L = runs[0]; |
michael@0 | 194 | SkASSERT(L < SkRegion::kRunTypeSentinel); |
michael@0 | 195 | if (left > L) { |
michael@0 | 196 | left = L; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | runs += intervals * 2; |
michael@0 | 200 | RunType R = runs[-1]; |
michael@0 | 201 | SkASSERT(R < SkRegion::kRunTypeSentinel); |
michael@0 | 202 | if (rite < R) { |
michael@0 | 203 | rite = R; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | intervalCount += intervals; |
michael@0 | 207 | } |
michael@0 | 208 | SkASSERT(SkRegion::kRunTypeSentinel == *runs); |
michael@0 | 209 | runs += 1; // skip x-sentinel |
michael@0 | 210 | |
michael@0 | 211 | // test Y-sentinel |
michael@0 | 212 | } while (SkRegion::kRunTypeSentinel > *runs); |
michael@0 | 213 | |
michael@0 | 214 | #ifdef SK_DEBUG |
michael@0 | 215 | // +1 to skip the last Y-sentinel |
michael@0 | 216 | int runCount = runs - this->writable_runs() + 1; |
michael@0 | 217 | SkASSERT(runCount == fRunCount); |
michael@0 | 218 | #endif |
michael@0 | 219 | |
michael@0 | 220 | fYSpanCount = ySpanCount; |
michael@0 | 221 | fIntervalCount = intervalCount; |
michael@0 | 222 | |
michael@0 | 223 | bounds->fLeft = left; |
michael@0 | 224 | bounds->fRight = rite; |
michael@0 | 225 | bounds->fBottom = bot; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | private: |
michael@0 | 229 | int32_t fYSpanCount; |
michael@0 | 230 | int32_t fIntervalCount; |
michael@0 | 231 | }; |
michael@0 | 232 | |
michael@0 | 233 | #endif |