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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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.

     1 /*
     2  * Copyright 2013 Google Inc.
     3  *
     4  * Use of this source code is governed by a BSD-style license that can be
     5  * found in the LICENSE file.
     6  */
     8 #include "SkBuffer.h"
     9 #include "SkOnce.h"
    10 #include "SkPath.h"
    11 #include "SkPathRef.h"
    13 //////////////////////////////////////////////////////////////////////////////
    14 SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
    15                           int incReserveVerbs,
    16                           int incReservePoints)
    17 {
    18     if ((*pathRef)->unique()) {
    19         (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
    20     } else {
    21         SkPathRef* copy = SkNEW(SkPathRef);
    22         copy->copy(**pathRef, incReserveVerbs, incReservePoints);
    23         pathRef->reset(copy);
    24     }
    25     fPathRef = *pathRef;
    26     fPathRef->fGenerationID = 0;
    27     SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
    28 }
    30 //////////////////////////////////////////////////////////////////////////////
    31 static SkPathRef* gEmptyPathRef = NULL;
    32 static void cleanup_gEmptyPathRef() { gEmptyPathRef->unref(); }
    34 void SkPathRef::CreateEmptyImpl(int) {
    35     gEmptyPathRef = SkNEW(SkPathRef);
    36     gEmptyPathRef->computeBounds();  // Preemptively avoid a race to clear fBoundsIsDirty.
    37 }
    39 SkPathRef* SkPathRef::CreateEmpty() {
    40     SK_DECLARE_STATIC_ONCE(once);
    41     SkOnce(&once, SkPathRef::CreateEmptyImpl, 0, cleanup_gEmptyPathRef);
    42     return SkRef(gEmptyPathRef);
    43 }
    45 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
    46                                       const SkPathRef& src,
    47                                       const SkMatrix& matrix) {
    48     SkDEBUGCODE(src.validate();)
    49     if (matrix.isIdentity()) {
    50         if (*dst != &src) {
    51             src.ref();
    52             dst->reset(const_cast<SkPathRef*>(&src));
    53             SkDEBUGCODE((*dst)->validate();)
    54         }
    55         return;
    56     }
    58     if (!(*dst)->unique()) {
    59         dst->reset(SkNEW(SkPathRef));
    60     }
    62     if (*dst != &src) {
    63         (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
    64         memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
    65         (*dst)->fConicWeights = src.fConicWeights;
    66     }
    68     SkASSERT((*dst)->countPoints() == src.countPoints());
    69     SkASSERT((*dst)->countVerbs() == src.countVerbs());
    70     SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
    72     // Need to check this here in case (&src == dst)
    73     bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
    75     matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
    77     /*
    78         *  Here we optimize the bounds computation, by noting if the bounds are
    79         *  already known, and if so, we just transform those as well and mark
    80         *  them as "known", rather than force the transformed path to have to
    81         *  recompute them.
    82         *
    83         *  Special gotchas if the path is effectively empty (<= 1 point) or
    84         *  if it is non-finite. In those cases bounds need to stay empty,
    85         *  regardless of the matrix.
    86         */
    87     if (canXformBounds) {
    88         (*dst)->fBoundsIsDirty = false;
    89         if (src.fIsFinite) {
    90             matrix.mapRect(&(*dst)->fBounds, src.fBounds);
    91             if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
    92                 (*dst)->fBounds.setEmpty();
    93             }
    94         } else {
    95             (*dst)->fIsFinite = false;
    96             (*dst)->fBounds.setEmpty();
    97         }
    98     } else {
    99         (*dst)->fBoundsIsDirty = true;
   100     }
   102     (*dst)->fSegmentMask = src.fSegmentMask;
   104     // It's an oval only if it stays a rect.
   105     (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
   107     SkDEBUGCODE((*dst)->validate();)
   108 }
   110 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
   111     SkPathRef* ref = SkNEW(SkPathRef);
   112     bool isOval;
   113     uint8_t segmentMask;
   115     int32_t packed;
   116     if (!buffer->readS32(&packed)) {
   117         SkDELETE(ref);
   118         return NULL;
   119     }
   121     ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
   122     segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
   123     isOval  = (packed >> kIsOval_SerializationShift) & 1;
   125     int32_t verbCount, pointCount, conicCount;
   126     if (!buffer->readU32(&(ref->fGenerationID)) ||
   127         !buffer->readS32(&verbCount) ||
   128         !buffer->readS32(&pointCount) ||
   129         !buffer->readS32(&conicCount)) {
   130         SkDELETE(ref);
   131         return NULL;
   132     }
   134     ref->resetToSize(verbCount, pointCount, conicCount);
   135     SkASSERT(verbCount == ref->countVerbs());
   136     SkASSERT(pointCount == ref->countPoints());
   137     SkASSERT(conicCount == ref->fConicWeights.count());
   139     if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
   140         !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
   141         !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
   142         !buffer->read(&ref->fBounds, sizeof(SkRect))) {
   143         SkDELETE(ref);
   144         return NULL;
   145     }
   146     ref->fBoundsIsDirty = false;
   148     // resetToSize clears fSegmentMask and fIsOval
   149     ref->fSegmentMask = segmentMask;
   150     ref->fIsOval = isOval;
   151     return ref;
   152 }
   154 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
   155     if ((*pathRef)->unique()) {
   156         SkDEBUGCODE((*pathRef)->validate();)
   157         (*pathRef)->fBoundsIsDirty = true;  // this also invalidates fIsFinite
   158         (*pathRef)->fVerbCnt = 0;
   159         (*pathRef)->fPointCnt = 0;
   160         (*pathRef)->fFreeSpace = (*pathRef)->currSize();
   161         (*pathRef)->fGenerationID = 0;
   162         (*pathRef)->fConicWeights.rewind();
   163         (*pathRef)->fSegmentMask = 0;
   164         (*pathRef)->fIsOval = false;
   165         SkDEBUGCODE((*pathRef)->validate();)
   166     } else {
   167         int oldVCnt = (*pathRef)->countVerbs();
   168         int oldPCnt = (*pathRef)->countPoints();
   169         pathRef->reset(SkNEW(SkPathRef));
   170         (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
   171     }
   172 }
   174 bool SkPathRef::operator== (const SkPathRef& ref) const {
   175     SkDEBUGCODE(this->validate();)
   176     SkDEBUGCODE(ref.validate();)
   178     // We explicitly check fSegmentMask as a quick-reject. We could skip it,
   179     // since it is only a cache of info in the fVerbs, but its a fast way to
   180     // notice a difference
   181     if (fSegmentMask != ref.fSegmentMask) {
   182         return false;
   183     }
   185     bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
   186 #ifdef SK_RELEASE
   187     if (genIDMatch) {
   188         return true;
   189     }
   190 #endif
   191     if (fPointCnt != ref.fPointCnt ||
   192         fVerbCnt != ref.fVerbCnt) {
   193         SkASSERT(!genIDMatch);
   194         return false;
   195     }
   196     if (0 != memcmp(this->verbsMemBegin(),
   197                     ref.verbsMemBegin(),
   198                     ref.fVerbCnt * sizeof(uint8_t))) {
   199         SkASSERT(!genIDMatch);
   200         return false;
   201     }
   202     if (0 != memcmp(this->points(),
   203                     ref.points(),
   204                     ref.fPointCnt * sizeof(SkPoint))) {
   205         SkASSERT(!genIDMatch);
   206         return false;
   207     }
   208     if (fConicWeights != ref.fConicWeights) {
   209         SkASSERT(!genIDMatch);
   210         return false;
   211     }
   212     // We've done the work to determine that these are equal. If either has a zero genID, copy
   213     // the other's. If both are 0 then genID() will compute the next ID.
   214     if (0 == fGenerationID) {
   215         fGenerationID = ref.genID();
   216     } else if (0 == ref.fGenerationID) {
   217         ref.fGenerationID = this->genID();
   218     }
   219     return true;
   220 }
   222 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
   223     SkDEBUGCODE(this->validate();)
   224     SkDEBUGCODE(size_t beforePos = buffer->pos();)
   226     // Call getBounds() to ensure (as a side-effect) that fBounds
   227     // and fIsFinite are computed.
   228     const SkRect& bounds = this->getBounds();
   230     int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
   231                      ((fIsOval & 1) << kIsOval_SerializationShift) |
   232                      (fSegmentMask << kSegmentMask_SerializationShift);
   233     buffer->write32(packed);
   235     // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
   236     // SkWBuffer. Until this is fixed we write 0.
   237     buffer->write32(0);
   238     buffer->write32(fVerbCnt);
   239     buffer->write32(fPointCnt);
   240     buffer->write32(fConicWeights.count());
   241     buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
   242     buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
   243     buffer->write(fConicWeights.begin(), fConicWeights.bytes());
   244     buffer->write(&bounds, sizeof(bounds));
   246     SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
   247 }
   249 uint32_t SkPathRef::writeSize() const {
   250     return uint32_t(5 * sizeof(uint32_t) +
   251                     fVerbCnt * sizeof(uint8_t) +
   252                     fPointCnt * sizeof(SkPoint) +
   253                     fConicWeights.bytes() +
   254                     sizeof(SkRect));
   255 }
   257 void SkPathRef::copy(const SkPathRef& ref,
   258                      int additionalReserveVerbs,
   259                      int additionalReservePoints) {
   260     SkDEBUGCODE(this->validate();)
   261     this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
   262                         additionalReserveVerbs, additionalReservePoints);
   263     memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
   264     memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
   265     fConicWeights = ref.fConicWeights;
   266     // We could call genID() here to force a real ID (instead of 0). However, if we're making
   267     // a copy then presumably we intend to make a modification immediately afterwards.
   268     fGenerationID = ref.fGenerationID;
   269     fBoundsIsDirty = ref.fBoundsIsDirty;
   270     if (!fBoundsIsDirty) {
   271         fBounds = ref.fBounds;
   272         fIsFinite = ref.fIsFinite;
   273     }
   274     fSegmentMask = ref.fSegmentMask;
   275     fIsOval = ref.fIsOval;
   276     SkDEBUGCODE(this->validate();)
   277 }
   279 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
   280                                         int numVbs,
   281                                         SkScalar** weights) {
   282     // This value is just made-up for now. When count is 4, calling memset was much
   283     // slower than just writing the loop. This seems odd, and hopefully in the
   284     // future this will appear to have been a fluke...
   285     static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
   287     SkDEBUGCODE(this->validate();)
   288     int pCnt;
   289     bool dirtyAfterEdit = true;
   290     switch (verb) {
   291         case SkPath::kMove_Verb:
   292             pCnt = numVbs;
   293             dirtyAfterEdit = false;
   294             break;
   295         case SkPath::kLine_Verb:
   296             fSegmentMask |= SkPath::kLine_SegmentMask;
   297             pCnt = numVbs;
   298             break;
   299         case SkPath::kQuad_Verb:
   300             fSegmentMask |= SkPath::kQuad_SegmentMask;
   301             pCnt = 2 * numVbs;
   302             break;
   303         case SkPath::kConic_Verb:
   304             fSegmentMask |= SkPath::kConic_SegmentMask;
   305             pCnt = 2 * numVbs;
   306             break;
   307         case SkPath::kCubic_Verb:
   308             fSegmentMask |= SkPath::kCubic_SegmentMask;
   309             pCnt = 3 * numVbs;
   310             break;
   311         case SkPath::kClose_Verb:
   312             SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
   313             pCnt = 0;
   314             dirtyAfterEdit = false;
   315             break;
   316         case SkPath::kDone_Verb:
   317             SkDEBUGFAIL("growForRepeatedVerb called for kDone");
   318             // fall through
   319         default:
   320             SkDEBUGFAIL("default should not be reached");
   321             pCnt = 0;
   322             dirtyAfterEdit = false;
   323     }
   325     size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
   326     this->makeSpace(space);
   328     SkPoint* ret = fPoints + fPointCnt;
   329     uint8_t* vb = fVerbs - fVerbCnt;
   331     // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
   332     // be 0, the compiler will remove the test/branch entirely.
   333     if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
   334         memset(vb - numVbs, verb, numVbs);
   335     } else {
   336         for (int i = 0; i < numVbs; ++i) {
   337             vb[~i] = verb;
   338         }
   339     }
   341     fVerbCnt += numVbs;
   342     fPointCnt += pCnt;
   343     fFreeSpace -= space;
   344     fBoundsIsDirty = true;  // this also invalidates fIsFinite
   345     if (dirtyAfterEdit) {
   346         fIsOval = false;
   347     }
   349     if (SkPath::kConic_Verb == verb) {
   350         SkASSERT(NULL != weights);
   351         *weights = fConicWeights.append(numVbs);
   352     }
   354     SkDEBUGCODE(this->validate();)
   355     return ret;
   356 }
   358 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
   359     SkDEBUGCODE(this->validate();)
   360     int pCnt;
   361     bool dirtyAfterEdit = true;
   362     switch (verb) {
   363         case SkPath::kMove_Verb:
   364             pCnt = 1;
   365             dirtyAfterEdit = false;
   366             break;
   367         case SkPath::kLine_Verb:
   368             fSegmentMask |= SkPath::kLine_SegmentMask;
   369             pCnt = 1;
   370             break;
   371         case SkPath::kQuad_Verb:
   372             fSegmentMask |= SkPath::kQuad_SegmentMask;
   373             pCnt = 2;
   374             break;
   375         case SkPath::kConic_Verb:
   376             fSegmentMask |= SkPath::kConic_SegmentMask;
   377             pCnt = 2;
   378             break;
   379         case SkPath::kCubic_Verb:
   380             fSegmentMask |= SkPath::kCubic_SegmentMask;
   381             pCnt = 3;
   382             break;
   383         case SkPath::kClose_Verb:
   384             pCnt = 0;
   385             dirtyAfterEdit = false;
   386             break;
   387         case SkPath::kDone_Verb:
   388             SkDEBUGFAIL("growForVerb called for kDone");
   389             // fall through
   390         default:
   391             SkDEBUGFAIL("default is not reached");
   392             dirtyAfterEdit = false;
   393             pCnt = 0;
   394     }
   395     size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
   396     this->makeSpace(space);
   397     this->fVerbs[~fVerbCnt] = verb;
   398     SkPoint* ret = fPoints + fPointCnt;
   399     fVerbCnt += 1;
   400     fPointCnt += pCnt;
   401     fFreeSpace -= space;
   402     fBoundsIsDirty = true;  // this also invalidates fIsFinite
   403     if (dirtyAfterEdit) {
   404         fIsOval = false;
   405     }
   407     if (SkPath::kConic_Verb == verb) {
   408         *fConicWeights.append() = weight;
   409     }
   411     SkDEBUGCODE(this->validate();)
   412     return ret;
   413 }
   415 uint32_t SkPathRef::genID() const {
   416     SkASSERT(!fEditorsAttached);
   417     static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
   418     if (!fGenerationID) {
   419         if (0 == fPointCnt && 0 == fVerbCnt) {
   420             fGenerationID = kEmptyGenID;
   421         } else {
   422             static int32_t  gPathRefGenerationID;
   423             // do a loop in case our global wraps around, as we never want to return a 0 or the
   424             // empty ID
   425             do {
   426                 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
   427             } while (fGenerationID <= kEmptyGenID);
   428         }
   429     }
   430     return fGenerationID;
   431 }
   433 #ifdef SK_DEBUG
   434 void SkPathRef::validate() const {
   435     this->INHERITED::validate();
   436     SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
   437     SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
   438     SkASSERT((NULL == fPoints) == (NULL == fVerbs));
   439     SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
   440     SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
   441     SkASSERT(!(NULL == fPoints && fPointCnt));
   442     SkASSERT(!(NULL == fVerbs && fVerbCnt));
   443     SkASSERT(this->currSize() ==
   444                 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
   446     if (!fBoundsIsDirty && !fBounds.isEmpty()) {
   447         bool isFinite = true;
   448         for (int i = 0; i < fPointCnt; ++i) {
   449             SkASSERT(!fPoints[i].isFinite() || (
   450                      fBounds.fLeft - fPoints[i].fX   < SK_ScalarNearlyZero &&
   451                      fPoints[i].fX - fBounds.fRight  < SK_ScalarNearlyZero &&
   452                      fBounds.fTop  - fPoints[i].fY   < SK_ScalarNearlyZero &&
   453                      fPoints[i].fY - fBounds.fBottom < SK_ScalarNearlyZero));
   454             if (!fPoints[i].isFinite()) {
   455                 isFinite = false;
   456             }
   457         }
   458         SkASSERT(SkToBool(fIsFinite) == isFinite);
   459     }
   461 #ifdef SK_DEBUG_PATH
   462     uint32_t mask = 0;
   463     for (int i = 0; i < fVerbCnt; ++i) {
   464         switch (fVerbs[~i]) {
   465             case SkPath::kMove_Verb:
   466                 break;
   467             case SkPath::kLine_Verb:
   468                 mask |= SkPath::kLine_SegmentMask;
   469                 break;
   470             case SkPath::kQuad_Verb:
   471                 mask |= SkPath::kQuad_SegmentMask;
   472                 break;
   473             case SkPath::kConic_Verb:
   474                 mask |= SkPath::kConic_SegmentMask;
   475                 break;
   476             case SkPath::kCubic_Verb:
   477                 mask |= SkPath::kCubic_SegmentMask;
   478                 break;
   479             case SkPath::kClose_Verb:
   480                 break;
   481             case SkPath::kDone_Verb:
   482                 SkDEBUGFAIL("Done verb shouldn't be recorded.");
   483                 break;
   484             default:
   485                 SkDEBUGFAIL("Unknown Verb");
   486                 break;
   487         }
   488     }
   489     SkASSERT(mask == fSegmentMask);
   490 #endif // SK_DEBUG_PATH
   491 }
   492 #endif

mercurial