gfx/skia/trunk/src/core/SkRasterClip.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkRasterClip.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,279 @@
     1.4 +/*
     1.5 + * Copyright 2010 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkRasterClip.h"
    1.12 +
    1.13 +
    1.14 +SkRasterClip::SkRasterClip() {
    1.15 +    fIsBW = true;
    1.16 +    fIsEmpty = true;
    1.17 +    fIsRect = false;
    1.18 +    SkDEBUGCODE(this->validate();)
    1.19 +}
    1.20 +
    1.21 +SkRasterClip::SkRasterClip(const SkRasterClip& src) {
    1.22 +    AUTO_RASTERCLIP_VALIDATE(src);
    1.23 +
    1.24 +    fIsBW = src.fIsBW;
    1.25 +    if (fIsBW) {
    1.26 +        fBW = src.fBW;
    1.27 +    } else {
    1.28 +        fAA = src.fAA;
    1.29 +    }
    1.30 +
    1.31 +    fIsEmpty = src.isEmpty();
    1.32 +    fIsRect = src.isRect();
    1.33 +    SkDEBUGCODE(this->validate();)
    1.34 +}
    1.35 +
    1.36 +SkRasterClip::SkRasterClip(const SkIRect& bounds) : fBW(bounds) {
    1.37 +    fIsBW = true;
    1.38 +    fIsEmpty = this->computeIsEmpty();  // bounds might be empty, so compute
    1.39 +    fIsRect = !fIsEmpty;
    1.40 +    SkDEBUGCODE(this->validate();)
    1.41 +}
    1.42 +
    1.43 +SkRasterClip::~SkRasterClip() {
    1.44 +    SkDEBUGCODE(this->validate();)
    1.45 +}
    1.46 +
    1.47 +bool SkRasterClip::isComplex() const {
    1.48 +    return fIsBW ? fBW.isComplex() : !fAA.isEmpty();
    1.49 +}
    1.50 +
    1.51 +const SkIRect& SkRasterClip::getBounds() const {
    1.52 +    return fIsBW ? fBW.getBounds() : fAA.getBounds();
    1.53 +}
    1.54 +
    1.55 +bool SkRasterClip::setEmpty() {
    1.56 +    AUTO_RASTERCLIP_VALIDATE(*this);
    1.57 +
    1.58 +    fIsBW = true;
    1.59 +    fBW.setEmpty();
    1.60 +    fAA.setEmpty();
    1.61 +    fIsEmpty = true;
    1.62 +    fIsRect = false;
    1.63 +    return false;
    1.64 +}
    1.65 +
    1.66 +bool SkRasterClip::setRect(const SkIRect& rect) {
    1.67 +    AUTO_RASTERCLIP_VALIDATE(*this);
    1.68 +
    1.69 +    fIsBW = true;
    1.70 +    fAA.setEmpty();
    1.71 +    fIsRect = fBW.setRect(rect);
    1.72 +    fIsEmpty = !fIsRect;
    1.73 +    return fIsRect;
    1.74 +}
    1.75 +
    1.76 +bool SkRasterClip::setPath(const SkPath& path, const SkRegion& clip, bool doAA) {
    1.77 +    AUTO_RASTERCLIP_VALIDATE(*this);
    1.78 +
    1.79 +    if (this->isBW() && !doAA) {
    1.80 +        (void)fBW.setPath(path, clip);
    1.81 +    } else {
    1.82 +        // TODO: since we are going to over-write fAA completely (aren't we?)
    1.83 +        // we should just clear our BW data (if any) and set fIsAA=true
    1.84 +        if (this->isBW()) {
    1.85 +            this->convertToAA();
    1.86 +        }
    1.87 +        (void)fAA.setPath(path, &clip, doAA);
    1.88 +    }
    1.89 +    return this->updateCacheAndReturnNonEmpty();
    1.90 +}
    1.91 +
    1.92 +bool SkRasterClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) {
    1.93 +    SkRegion tmp;
    1.94 +    tmp.setRect(clip);
    1.95 +    return this->setPath(path, tmp, doAA);
    1.96 +}
    1.97 +
    1.98 +bool SkRasterClip::op(const SkIRect& rect, SkRegion::Op op) {
    1.99 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.100 +
   1.101 +    fIsBW ? fBW.op(rect, op) : fAA.op(rect, op);
   1.102 +    return this->updateCacheAndReturnNonEmpty();
   1.103 +}
   1.104 +
   1.105 +bool SkRasterClip::op(const SkRegion& rgn, SkRegion::Op op) {
   1.106 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.107 +
   1.108 +    if (fIsBW) {
   1.109 +        (void)fBW.op(rgn, op);
   1.110 +    } else {
   1.111 +        SkAAClip tmp;
   1.112 +        tmp.setRegion(rgn);
   1.113 +        (void)fAA.op(tmp, op);
   1.114 +    }
   1.115 +    return this->updateCacheAndReturnNonEmpty();
   1.116 +}
   1.117 +
   1.118 +bool SkRasterClip::op(const SkRasterClip& clip, SkRegion::Op op) {
   1.119 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.120 +    clip.validate();
   1.121 +
   1.122 +    if (this->isBW() && clip.isBW()) {
   1.123 +        (void)fBW.op(clip.fBW, op);
   1.124 +    } else {
   1.125 +        SkAAClip tmp;
   1.126 +        const SkAAClip* other;
   1.127 +
   1.128 +        if (this->isBW()) {
   1.129 +            this->convertToAA();
   1.130 +        }
   1.131 +        if (clip.isBW()) {
   1.132 +            tmp.setRegion(clip.bwRgn());
   1.133 +            other = &tmp;
   1.134 +        } else {
   1.135 +            other = &clip.aaRgn();
   1.136 +        }
   1.137 +        (void)fAA.op(*other, op);
   1.138 +    }
   1.139 +    return this->updateCacheAndReturnNonEmpty();
   1.140 +}
   1.141 +
   1.142 +/**
   1.143 + *  Our antialiasing currently has a granularity of 1/4 of a pixel along each
   1.144 + *  axis. Thus we can treat an axis coordinate as an integer if it differs
   1.145 + *  from its nearest int by < half of that value (1.8 in this case).
   1.146 + */
   1.147 +static bool nearly_integral(SkScalar x) {
   1.148 +    static const SkScalar domain = SK_Scalar1 / 4;
   1.149 +    static const SkScalar halfDomain = domain / 2;
   1.150 +
   1.151 +    x += halfDomain;
   1.152 +    return x - SkScalarFloorToScalar(x) < domain;
   1.153 +}
   1.154 +
   1.155 +bool SkRasterClip::op(const SkRect& r, SkRegion::Op op, bool doAA) {
   1.156 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.157 +
   1.158 +    if (fIsBW && doAA) {
   1.159 +        // check that the rect really needs aa, or is it close enought to
   1.160 +        // integer boundaries that we can just treat it as a BW rect?
   1.161 +        if (nearly_integral(r.fLeft) && nearly_integral(r.fTop) &&
   1.162 +            nearly_integral(r.fRight) && nearly_integral(r.fBottom)) {
   1.163 +            doAA = false;
   1.164 +        }
   1.165 +    }
   1.166 +
   1.167 +    if (fIsBW && !doAA) {
   1.168 +        SkIRect ir;
   1.169 +        r.round(&ir);
   1.170 +        (void)fBW.op(ir, op);
   1.171 +    } else {
   1.172 +        if (fIsBW) {
   1.173 +            this->convertToAA();
   1.174 +        }
   1.175 +        (void)fAA.op(r, op, doAA);
   1.176 +    }
   1.177 +    return this->updateCacheAndReturnNonEmpty();
   1.178 +}
   1.179 +
   1.180 +void SkRasterClip::translate(int dx, int dy, SkRasterClip* dst) const {
   1.181 +    if (NULL == dst) {
   1.182 +        return;
   1.183 +    }
   1.184 +
   1.185 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.186 +
   1.187 +    if (this->isEmpty()) {
   1.188 +        dst->setEmpty();
   1.189 +        return;
   1.190 +    }
   1.191 +    if (0 == (dx | dy)) {
   1.192 +        *dst = *this;
   1.193 +        return;
   1.194 +    }
   1.195 +
   1.196 +    dst->fIsBW = fIsBW;
   1.197 +    if (fIsBW) {
   1.198 +        fBW.translate(dx, dy, &dst->fBW);
   1.199 +        dst->fAA.setEmpty();
   1.200 +    } else {
   1.201 +        fAA.translate(dx, dy, &dst->fAA);
   1.202 +        dst->fBW.setEmpty();
   1.203 +    }
   1.204 +    dst->updateCacheAndReturnNonEmpty();
   1.205 +}
   1.206 +
   1.207 +bool SkRasterClip::quickContains(const SkIRect& ir) const {
   1.208 +    return fIsBW ? fBW.quickContains(ir) : fAA.quickContains(ir);
   1.209 +}
   1.210 +
   1.211 +///////////////////////////////////////////////////////////////////////////////
   1.212 +
   1.213 +const SkRegion& SkRasterClip::forceGetBW() {
   1.214 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.215 +
   1.216 +    if (!fIsBW) {
   1.217 +        fBW.setRect(fAA.getBounds());
   1.218 +    }
   1.219 +    return fBW;
   1.220 +}
   1.221 +
   1.222 +void SkRasterClip::convertToAA() {
   1.223 +    AUTO_RASTERCLIP_VALIDATE(*this);
   1.224 +
   1.225 +    SkASSERT(fIsBW);
   1.226 +    fAA.setRegion(fBW);
   1.227 +    fIsBW = false;
   1.228 +    (void)this->updateCacheAndReturnNonEmpty();
   1.229 +}
   1.230 +
   1.231 +#ifdef SK_DEBUG
   1.232 +void SkRasterClip::validate() const {
   1.233 +    // can't ever assert that fBW is empty, since we may have called forceGetBW
   1.234 +    if (fIsBW) {
   1.235 +        SkASSERT(fAA.isEmpty());
   1.236 +    }
   1.237 +
   1.238 +    fBW.validate();
   1.239 +    fAA.validate();
   1.240 +
   1.241 +    SkASSERT(this->computeIsEmpty() == fIsEmpty);
   1.242 +    SkASSERT(this->computeIsRect() == fIsRect);
   1.243 +}
   1.244 +#endif
   1.245 +
   1.246 +///////////////////////////////////////////////////////////////////////////////
   1.247 +
   1.248 +SkAAClipBlitterWrapper::SkAAClipBlitterWrapper() {
   1.249 +    SkDEBUGCODE(fClipRgn = NULL;)
   1.250 +    SkDEBUGCODE(fBlitter = NULL;)
   1.251 +}
   1.252 +
   1.253 +SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkRasterClip& clip,
   1.254 +                                               SkBlitter* blitter) {
   1.255 +    this->init(clip, blitter);
   1.256 +}
   1.257 +
   1.258 +SkAAClipBlitterWrapper::SkAAClipBlitterWrapper(const SkAAClip* aaclip,
   1.259 +                                               SkBlitter* blitter) {
   1.260 +    SkASSERT(blitter);
   1.261 +    SkASSERT(aaclip);
   1.262 +    fBWRgn.setRect(aaclip->getBounds());
   1.263 +    fAABlitter.init(blitter, aaclip);
   1.264 +    // now our return values
   1.265 +    fClipRgn = &fBWRgn;
   1.266 +    fBlitter = &fAABlitter;
   1.267 +}
   1.268 +
   1.269 +void SkAAClipBlitterWrapper::init(const SkRasterClip& clip, SkBlitter* blitter) {
   1.270 +    SkASSERT(blitter);
   1.271 +    if (clip.isBW()) {
   1.272 +        fClipRgn = &clip.bwRgn();
   1.273 +        fBlitter = blitter;
   1.274 +    } else {
   1.275 +        const SkAAClip& aaclip = clip.aaRgn();
   1.276 +        fBWRgn.setRect(aaclip.getBounds());
   1.277 +        fAABlitter.init(blitter, &aaclip);
   1.278 +        // now our return values
   1.279 +        fClipRgn = &fBWRgn;
   1.280 +        fBlitter = &fAABlitter;
   1.281 +    }
   1.282 +}

mercurial