Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2010 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkRasterClip.h" |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | SkRasterClip::SkRasterClip() { |
michael@0 | 12 | fIsBW = true; |
michael@0 | 13 | fIsEmpty = true; |
michael@0 | 14 | fIsRect = false; |
michael@0 | 15 | SkDEBUGCODE(this->validate();) |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | SkRasterClip::SkRasterClip(const SkRasterClip& src) { |
michael@0 | 19 | AUTO_RASTERCLIP_VALIDATE(src); |
michael@0 | 20 | |
michael@0 | 21 | fIsBW = src.fIsBW; |
michael@0 | 22 | if (fIsBW) { |
michael@0 | 23 | fBW = src.fBW; |
michael@0 | 24 | } else { |
michael@0 | 25 | fAA = src.fAA; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | fIsEmpty = src.isEmpty(); |
michael@0 | 29 | fIsRect = src.isRect(); |
michael@0 | 30 | SkDEBUGCODE(this->validate();) |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | SkRasterClip::SkRasterClip(const SkIRect& bounds) : fBW(bounds) { |
michael@0 | 34 | fIsBW = true; |
michael@0 | 35 | fIsEmpty = this->computeIsEmpty(); // bounds might be empty, so compute |
michael@0 | 36 | fIsRect = !fIsEmpty; |
michael@0 | 37 | SkDEBUGCODE(this->validate();) |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | SkRasterClip::~SkRasterClip() { |
michael@0 | 41 | SkDEBUGCODE(this->validate();) |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | bool SkRasterClip::isComplex() const { |
michael@0 | 45 | return fIsBW ? fBW.isComplex() : !fAA.isEmpty(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | const SkIRect& SkRasterClip::getBounds() const { |
michael@0 | 49 | return fIsBW ? fBW.getBounds() : fAA.getBounds(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | bool SkRasterClip::setEmpty() { |
michael@0 | 53 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 54 | |
michael@0 | 55 | fIsBW = true; |
michael@0 | 56 | fBW.setEmpty(); |
michael@0 | 57 | fAA.setEmpty(); |
michael@0 | 58 | fIsEmpty = true; |
michael@0 | 59 | fIsRect = false; |
michael@0 | 60 | return false; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bool SkRasterClip::setRect(const SkIRect& rect) { |
michael@0 | 64 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 65 | |
michael@0 | 66 | fIsBW = true; |
michael@0 | 67 | fAA.setEmpty(); |
michael@0 | 68 | fIsRect = fBW.setRect(rect); |
michael@0 | 69 | fIsEmpty = !fIsRect; |
michael@0 | 70 | return fIsRect; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | bool SkRasterClip::setPath(const SkPath& path, const SkRegion& clip, bool doAA) { |
michael@0 | 74 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 75 | |
michael@0 | 76 | if (this->isBW() && !doAA) { |
michael@0 | 77 | (void)fBW.setPath(path, clip); |
michael@0 | 78 | } else { |
michael@0 | 79 | // TODO: since we are going to over-write fAA completely (aren't we?) |
michael@0 | 80 | // we should just clear our BW data (if any) and set fIsAA=true |
michael@0 | 81 | if (this->isBW()) { |
michael@0 | 82 | this->convertToAA(); |
michael@0 | 83 | } |
michael@0 | 84 | (void)fAA.setPath(path, &clip, doAA); |
michael@0 | 85 | } |
michael@0 | 86 | return this->updateCacheAndReturnNonEmpty(); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | bool SkRasterClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) { |
michael@0 | 90 | SkRegion tmp; |
michael@0 | 91 | tmp.setRect(clip); |
michael@0 | 92 | return this->setPath(path, tmp, doAA); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | bool SkRasterClip::op(const SkIRect& rect, SkRegion::Op op) { |
michael@0 | 96 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 97 | |
michael@0 | 98 | fIsBW ? fBW.op(rect, op) : fAA.op(rect, op); |
michael@0 | 99 | return this->updateCacheAndReturnNonEmpty(); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | bool SkRasterClip::op(const SkRegion& rgn, SkRegion::Op op) { |
michael@0 | 103 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 104 | |
michael@0 | 105 | if (fIsBW) { |
michael@0 | 106 | (void)fBW.op(rgn, op); |
michael@0 | 107 | } else { |
michael@0 | 108 | SkAAClip tmp; |
michael@0 | 109 | tmp.setRegion(rgn); |
michael@0 | 110 | (void)fAA.op(tmp, op); |
michael@0 | 111 | } |
michael@0 | 112 | return this->updateCacheAndReturnNonEmpty(); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | bool SkRasterClip::op(const SkRasterClip& clip, SkRegion::Op op) { |
michael@0 | 116 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 117 | clip.validate(); |
michael@0 | 118 | |
michael@0 | 119 | if (this->isBW() && clip.isBW()) { |
michael@0 | 120 | (void)fBW.op(clip.fBW, op); |
michael@0 | 121 | } else { |
michael@0 | 122 | SkAAClip tmp; |
michael@0 | 123 | const SkAAClip* other; |
michael@0 | 124 | |
michael@0 | 125 | if (this->isBW()) { |
michael@0 | 126 | this->convertToAA(); |
michael@0 | 127 | } |
michael@0 | 128 | if (clip.isBW()) { |
michael@0 | 129 | tmp.setRegion(clip.bwRgn()); |
michael@0 | 130 | other = &tmp; |
michael@0 | 131 | } else { |
michael@0 | 132 | other = &clip.aaRgn(); |
michael@0 | 133 | } |
michael@0 | 134 | (void)fAA.op(*other, op); |
michael@0 | 135 | } |
michael@0 | 136 | return this->updateCacheAndReturnNonEmpty(); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Our antialiasing currently has a granularity of 1/4 of a pixel along each |
michael@0 | 141 | * axis. Thus we can treat an axis coordinate as an integer if it differs |
michael@0 | 142 | * from its nearest int by < half of that value (1.8 in this case). |
michael@0 | 143 | */ |
michael@0 | 144 | static bool nearly_integral(SkScalar x) { |
michael@0 | 145 | static const SkScalar domain = SK_Scalar1 / 4; |
michael@0 | 146 | static const SkScalar halfDomain = domain / 2; |
michael@0 | 147 | |
michael@0 | 148 | x += halfDomain; |
michael@0 | 149 | return x - SkScalarFloorToScalar(x) < domain; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | bool SkRasterClip::op(const SkRect& r, SkRegion::Op op, bool doAA) { |
michael@0 | 153 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 154 | |
michael@0 | 155 | if (fIsBW && doAA) { |
michael@0 | 156 | // check that the rect really needs aa, or is it close enought to |
michael@0 | 157 | // integer boundaries that we can just treat it as a BW rect? |
michael@0 | 158 | if (nearly_integral(r.fLeft) && nearly_integral(r.fTop) && |
michael@0 | 159 | nearly_integral(r.fRight) && nearly_integral(r.fBottom)) { |
michael@0 | 160 | doAA = false; |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | if (fIsBW && !doAA) { |
michael@0 | 165 | SkIRect ir; |
michael@0 | 166 | r.round(&ir); |
michael@0 | 167 | (void)fBW.op(ir, op); |
michael@0 | 168 | } else { |
michael@0 | 169 | if (fIsBW) { |
michael@0 | 170 | this->convertToAA(); |
michael@0 | 171 | } |
michael@0 | 172 | (void)fAA.op(r, op, doAA); |
michael@0 | 173 | } |
michael@0 | 174 | return this->updateCacheAndReturnNonEmpty(); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void SkRasterClip::translate(int dx, int dy, SkRasterClip* dst) const { |
michael@0 | 178 | if (NULL == dst) { |
michael@0 | 179 | return; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 183 | |
michael@0 | 184 | if (this->isEmpty()) { |
michael@0 | 185 | dst->setEmpty(); |
michael@0 | 186 | return; |
michael@0 | 187 | } |
michael@0 | 188 | if (0 == (dx | dy)) { |
michael@0 | 189 | *dst = *this; |
michael@0 | 190 | return; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | dst->fIsBW = fIsBW; |
michael@0 | 194 | if (fIsBW) { |
michael@0 | 195 | fBW.translate(dx, dy, &dst->fBW); |
michael@0 | 196 | dst->fAA.setEmpty(); |
michael@0 | 197 | } else { |
michael@0 | 198 | fAA.translate(dx, dy, &dst->fAA); |
michael@0 | 199 | dst->fBW.setEmpty(); |
michael@0 | 200 | } |
michael@0 | 201 | dst->updateCacheAndReturnNonEmpty(); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | bool SkRasterClip::quickContains(const SkIRect& ir) const { |
michael@0 | 205 | return fIsBW ? fBW.quickContains(ir) : fAA.quickContains(ir); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 209 | |
michael@0 | 210 | const SkRegion& SkRasterClip::forceGetBW() { |
michael@0 | 211 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 212 | |
michael@0 | 213 | if (!fIsBW) { |
michael@0 | 214 | fBW.setRect(fAA.getBounds()); |
michael@0 | 215 | } |
michael@0 | 216 | return fBW; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | void SkRasterClip::convertToAA() { |
michael@0 | 220 | AUTO_RASTERCLIP_VALIDATE(*this); |
michael@0 | 221 | |
michael@0 | 222 | SkASSERT(fIsBW); |
michael@0 | 223 | fAA.setRegion(fBW); |
michael@0 | 224 | fIsBW = false; |
michael@0 | 225 | (void)this->updateCacheAndReturnNonEmpty(); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | #ifdef SK_DEBUG |
michael@0 | 229 | void SkRasterClip::validate() const { |
michael@0 | 230 | // can't ever assert that fBW is empty, since we may have called forceGetBW |
michael@0 | 231 | if (fIsBW) { |
michael@0 | 232 | SkASSERT(fAA.isEmpty()); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | fBW.validate(); |
michael@0 | 236 | fAA.validate(); |
michael@0 | 237 | |
michael@0 | 238 | SkASSERT(this->computeIsEmpty() == fIsEmpty); |
michael@0 | 239 | SkASSERT(this->computeIsRect() == fIsRect); |
michael@0 | 240 | } |
michael@0 | 241 | #endif |
michael@0 | 242 | |
michael@0 | 243 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 244 | |
michael@0 | 245 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper() { |
michael@0 | 246 | SkDEBUGCODE(fClipRgn = NULL;) |
michael@0 | 247 | SkDEBUGCODE(fBlitter = NULL;) |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkRasterClip& clip, |
michael@0 | 251 | SkBlitter* blitter) { |
michael@0 | 252 | this->init(clip, blitter); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkAAClip* aaclip, |
michael@0 | 256 | SkBlitter* blitter) { |
michael@0 | 257 | SkASSERT(blitter); |
michael@0 | 258 | SkASSERT(aaclip); |
michael@0 | 259 | fBWRgn.setRect(aaclip->getBounds()); |
michael@0 | 260 | fAABlitter.init(blitter, aaclip); |
michael@0 | 261 | // now our return values |
michael@0 | 262 | fClipRgn = &fBWRgn; |
michael@0 | 263 | fBlitter = &fAABlitter; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | void SkAAClipBlitterWrapper::init(const SkRasterClip& clip, SkBlitter* blitter) { |
michael@0 | 267 | SkASSERT(blitter); |
michael@0 | 268 | if (clip.isBW()) { |
michael@0 | 269 | fClipRgn = &clip.bwRgn(); |
michael@0 | 270 | fBlitter = blitter; |
michael@0 | 271 | } else { |
michael@0 | 272 | const SkAAClip& aaclip = clip.aaRgn(); |
michael@0 | 273 | fBWRgn.setRect(aaclip.getBounds()); |
michael@0 | 274 | fAABlitter.init(blitter, &aaclip); |
michael@0 | 275 | // now our return values |
michael@0 | 276 | fClipRgn = &fBWRgn; |
michael@0 | 277 | fBlitter = &fAABlitter; |
michael@0 | 278 | } |
michael@0 | 279 | } |