2012-10-03 13:46:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkPathRef_DEFINED
|
|
|
|
#define SkPathRef_DEFINED
|
|
|
|
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkAtomics.h"
|
2013-09-26 12:18:23 +00:00
|
|
|
#include "SkMatrix.h"
|
2018-09-17 21:29:39 +00:00
|
|
|
#include "SkMutex.h"
|
2013-09-26 12:18:23 +00:00
|
|
|
#include "SkPoint.h"
|
2015-11-19 22:47:43 +00:00
|
|
|
#include "SkRRect.h"
|
2013-09-26 12:18:23 +00:00
|
|
|
#include "SkRect.h"
|
2012-10-03 13:46:20 +00:00
|
|
|
#include "SkRefCnt.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTDArray.h"
|
2017-07-31 20:57:01 +00:00
|
|
|
#include "SkTemplates.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
|
|
|
|
2018-06-11 15:56:57 +00:00
|
|
|
#include <limits>
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2013-09-26 12:18:23 +00:00
|
|
|
class SkRBuffer;
|
|
|
|
class SkWBuffer;
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
/**
|
|
|
|
* Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
|
|
|
|
* modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
|
|
|
|
* SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
|
2013-12-03 16:43:54 +00:00
|
|
|
* copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
|
2016-10-27 16:30:08 +00:00
|
|
|
* constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef
|
|
|
|
* after the editor's constructor returns.
|
2012-10-03 13:46:20 +00:00
|
|
|
*
|
|
|
|
* The points and verbs are stored in a single allocation. The points are at the begining of the
|
|
|
|
* allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
|
|
|
|
* and verbs both grow into the middle of the allocation until the meet. To access verb i in the
|
|
|
|
* verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
|
|
|
|
* logical verb or the last verb in memory).
|
|
|
|
*/
|
2012-10-03 19:57:01 +00:00
|
|
|
|
2016-08-09 19:20:04 +00:00
|
|
|
class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
|
2012-10-03 13:46:20 +00:00
|
|
|
public:
|
|
|
|
class Editor {
|
|
|
|
public:
|
2016-10-27 16:30:08 +00:00
|
|
|
Editor(sk_sp<SkPathRef>* pathRef,
|
2012-10-03 13:46:20 +00:00
|
|
|
int incReserveVerbs = 0,
|
2013-09-27 17:48:49 +00:00
|
|
|
int incReservePoints = 0);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2013-06-27 18:39:39 +00:00
|
|
|
~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the array of points.
|
|
|
|
*/
|
2013-12-13 19:36:25 +00:00
|
|
|
SkPoint* points() { return fPathRef->getPoints(); }
|
|
|
|
const SkPoint* points() const { return fPathRef->points(); }
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the ith point. Shortcut for this->points() + i
|
|
|
|
*/
|
|
|
|
SkPoint* atPoint(int i) {
|
|
|
|
SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
|
|
|
|
return this->points() + i;
|
2016-09-27 13:34:10 +00:00
|
|
|
}
|
2013-12-13 19:36:25 +00:00
|
|
|
const SkPoint* atPoint(int i) const {
|
|
|
|
SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
|
|
|
|
return this->points() + i;
|
2016-09-27 13:34:10 +00:00
|
|
|
}
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the verb and allocates space for the number of points indicated by the verb. The
|
|
|
|
* return value is a pointer to where the points for the verb should be written.
|
2013-12-12 23:03:51 +00:00
|
|
|
* 'weight' is only used if 'verb' is kConic_Verb
|
2012-10-03 13:46:20 +00:00
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(fPathRef->validate();)
|
2013-12-12 23:03:51 +00:00
|
|
|
return fPathRef->growForVerb(verb, weight);
|
2013-09-27 17:48:49 +00:00
|
|
|
}
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-12 23:03:51 +00:00
|
|
|
* Allocates space for multiple instances of a particular verb and the
|
|
|
|
* requisite points & weights.
|
|
|
|
* The return pointer points at the first new point (indexed normally [<i>]).
|
|
|
|
* If 'verb' is kConic_Verb, 'weights' will return a pointer to the
|
2013-12-16 07:01:40 +00:00
|
|
|
* space for the conic weights (indexed normally).
|
2012-10-03 13:46:20 +00:00
|
|
|
*/
|
2013-12-16 07:01:40 +00:00
|
|
|
SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
|
|
|
|
int numVbs,
|
2017-08-28 14:34:05 +00:00
|
|
|
SkScalar** weights = nullptr) {
|
2013-12-16 07:01:40 +00:00
|
|
|
return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
|
2012-10-03 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the path ref to a new verb and point count. The new verbs and points are
|
|
|
|
* uninitialized.
|
|
|
|
*/
|
2013-05-31 15:17:50 +00:00
|
|
|
void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
|
|
|
|
fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
|
2012-10-03 13:46:20 +00:00
|
|
|
}
|
2013-12-13 19:36:25 +00:00
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
/**
|
|
|
|
* Gets the path ref that is wrapped in the Editor.
|
|
|
|
*/
|
|
|
|
SkPathRef* pathRef() { return fPathRef; }
|
|
|
|
|
2016-05-27 16:17:04 +00:00
|
|
|
void setIsOval(bool isOval, bool isCCW, unsigned start) {
|
|
|
|
fPathRef->setIsOval(isOval, isCCW, start);
|
|
|
|
}
|
2013-12-03 16:43:54 +00:00
|
|
|
|
2016-05-27 16:17:04 +00:00
|
|
|
void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
|
|
|
|
fPathRef->setIsRRect(isRRect, isCCW, start);
|
|
|
|
}
|
2015-11-19 22:47:43 +00:00
|
|
|
|
2013-12-13 19:36:25 +00:00
|
|
|
void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
private:
|
|
|
|
SkPathRef* fPathRef;
|
|
|
|
};
|
|
|
|
|
2015-11-19 22:47:43 +00:00
|
|
|
class SK_API Iter {
|
|
|
|
public:
|
|
|
|
Iter();
|
|
|
|
Iter(const SkPathRef&);
|
|
|
|
|
|
|
|
void setPathRef(const SkPathRef&);
|
|
|
|
|
|
|
|
/** Return the next verb in this iteration of the path. When all
|
|
|
|
segments have been visited, return kDone_Verb.
|
|
|
|
|
2017-09-08 19:00:25 +00:00
|
|
|
If any point in the path is non-finite, return kDone_Verb immediately.
|
|
|
|
|
2015-11-19 22:47:43 +00:00
|
|
|
@param pts The points representing the current verb and/or segment
|
|
|
|
This must not be NULL.
|
|
|
|
@return The verb for the current segment
|
|
|
|
*/
|
|
|
|
uint8_t next(SkPoint pts[4]);
|
2015-12-09 22:04:46 +00:00
|
|
|
uint8_t peek() const;
|
2015-11-19 22:47:43 +00:00
|
|
|
|
|
|
|
SkScalar conicWeight() const { return *fConicWeights; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkPoint* fPts;
|
|
|
|
const uint8_t* fVerbs;
|
|
|
|
const uint8_t* fVerbStop;
|
|
|
|
const SkScalar* fConicWeights;
|
|
|
|
};
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Gets a path ref with no verbs or points.
|
|
|
|
*/
|
2013-10-23 14:44:08 +00:00
|
|
|
static SkPathRef* CreateEmpty();
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2013-09-26 12:18:23 +00:00
|
|
|
/**
|
|
|
|
* Returns true if all of the points in this path are finite, meaning there
|
|
|
|
* are no infinities and no NaNs.
|
|
|
|
*/
|
|
|
|
bool isFinite() const {
|
|
|
|
if (fBoundsIsDirty) {
|
|
|
|
this->computeBounds();
|
|
|
|
}
|
|
|
|
return SkToBool(fIsFinite);
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:03:51 +00:00
|
|
|
/**
|
|
|
|
* Returns a mask, where each bit corresponding to a SegmentMask is
|
|
|
|
* set if the path contains 1 or more segments of that type.
|
|
|
|
* Returns 0 for an empty path (no segments).
|
|
|
|
*/
|
|
|
|
uint32_t getSegmentMasks() const { return fSegmentMask; }
|
|
|
|
|
2013-12-03 16:43:54 +00:00
|
|
|
/** Returns true if the path is an oval.
|
|
|
|
*
|
|
|
|
* @param rect returns the bounding rect of this oval. It's a circle
|
|
|
|
* if the height and width are the same.
|
2016-05-27 16:17:04 +00:00
|
|
|
* @param isCCW is the oval CCW (or CW if false).
|
|
|
|
* @param start indicates where the contour starts on the oval (see
|
|
|
|
* SkPath::addOval for intepretation of the index).
|
2013-12-03 16:43:54 +00:00
|
|
|
*
|
|
|
|
* @return true if this path is an oval.
|
|
|
|
* Tracking whether a path is an oval is considered an
|
|
|
|
* optimization for performance and so some paths that are in
|
|
|
|
* fact ovals can report false.
|
|
|
|
*/
|
2016-05-27 16:17:04 +00:00
|
|
|
bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const {
|
|
|
|
if (fIsOval) {
|
|
|
|
if (rect) {
|
|
|
|
*rect = this->getBounds();
|
|
|
|
}
|
|
|
|
if (isCCW) {
|
|
|
|
*isCCW = SkToBool(fRRectOrOvalIsCCW);
|
|
|
|
}
|
|
|
|
if (start) {
|
|
|
|
*start = fRRectOrOvalStartIdx;
|
|
|
|
}
|
2013-12-03 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SkToBool(fIsOval);
|
|
|
|
}
|
|
|
|
|
2016-05-27 16:17:04 +00:00
|
|
|
bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
|
|
|
|
if (fIsRRect) {
|
|
|
|
if (rrect) {
|
|
|
|
*rrect = this->getRRect();
|
|
|
|
}
|
|
|
|
if (isCCW) {
|
|
|
|
*isCCW = SkToBool(fRRectOrOvalIsCCW);
|
|
|
|
}
|
|
|
|
if (start) {
|
|
|
|
*start = fRRectOrOvalStartIdx;
|
|
|
|
}
|
2015-11-19 22:47:43 +00:00
|
|
|
}
|
|
|
|
return SkToBool(fIsRRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-26 12:18:23 +00:00
|
|
|
bool hasComputedBounds() const {
|
|
|
|
return !fBoundsIsDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the bounds of the path's points. If the path contains 0 or 1
|
|
|
|
points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
|
|
|
|
Note: this bounds may be larger than the actual shape, since curves
|
|
|
|
do not extend as far as their control points.
|
|
|
|
*/
|
2013-09-27 07:01:29 +00:00
|
|
|
const SkRect& getBounds() const {
|
2013-09-26 12:18:23 +00:00
|
|
|
if (fBoundsIsDirty) {
|
|
|
|
this->computeBounds();
|
|
|
|
}
|
|
|
|
return fBounds;
|
|
|
|
}
|
|
|
|
|
2015-11-19 22:47:43 +00:00
|
|
|
SkRRect getRRect() const;
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
/**
|
|
|
|
* Transforms a path ref by a matrix, allocating a new one only if necessary.
|
|
|
|
*/
|
2016-10-27 16:30:08 +00:00
|
|
|
static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
|
2012-10-03 13:46:20 +00:00
|
|
|
const SkPathRef& src,
|
2013-09-27 17:48:49 +00:00
|
|
|
const SkMatrix& matrix);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2014-01-23 15:16:05 +00:00
|
|
|
static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
|
|
|
|
* repopulated with approximately the same number of verbs and points. A new path ref is created
|
|
|
|
* only if necessary.
|
|
|
|
*/
|
2016-10-27 16:30:08 +00:00
|
|
|
static void Rewind(sk_sp<SkPathRef>* pathRef);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2016-08-09 19:20:04 +00:00
|
|
|
~SkPathRef();
|
Speed up convexpaths GM in debug builds
That GM has a path with 16k points, arranged in a rectangle. When we try
to draw it in Ganesh, we first check to see if it's a (closed) rectangle.
We eventually decide that it is a rectangle, but not closed, so it gets
passed along. Then we construct a GrShape, which tries to simplify the
shape, again asking if it's a rectangle...
Each isRect query was iterating over the 16k verbs, and for each one,
calling atVerb(i). That, internally, calls verbs(), which calls validate()
in debug builds. So we were walking all 16k points 16k times (to ensure
the bounds were correct). The end result is that the GM took over 11
seconds to draw, and now takes 3 ms.
Bug: skia:
Change-Id: If5f7a067b8c25f049dc64275d94a42ae4a50f6a9
Reviewed-on: https://skia-review.googlesource.com/34723
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2017-08-15 20:08:48 +00:00
|
|
|
int countPoints() const { return fPointCnt; }
|
|
|
|
int countVerbs() const { return fVerbCnt; }
|
|
|
|
int countWeights() const { return fConicWeights.count(); }
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pointer one beyond the first logical verb (last verb in memory order).
|
|
|
|
*/
|
Speed up convexpaths GM in debug builds
That GM has a path with 16k points, arranged in a rectangle. When we try
to draw it in Ganesh, we first check to see if it's a (closed) rectangle.
We eventually decide that it is a rectangle, but not closed, so it gets
passed along. Then we construct a GrShape, which tries to simplify the
shape, again asking if it's a rectangle...
Each isRect query was iterating over the 16k verbs, and for each one,
calling atVerb(i). That, internally, calls verbs(), which calls validate()
in debug builds. So we were walking all 16k points 16k times (to ensure
the bounds were correct). The end result is that the GM took over 11
seconds to draw, and now takes 3 ms.
Bug: skia:
Change-Id: If5f7a067b8c25f049dc64275d94a42ae4a50f6a9
Reviewed-on: https://skia-review.googlesource.com/34723
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2017-08-15 20:08:48 +00:00
|
|
|
const uint8_t* verbs() const { return fVerbs; }
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a const pointer to the first verb in memory (which is the last logical verb).
|
|
|
|
*/
|
|
|
|
const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a const pointer to the first point.
|
|
|
|
*/
|
Speed up convexpaths GM in debug builds
That GM has a path with 16k points, arranged in a rectangle. When we try
to draw it in Ganesh, we first check to see if it's a (closed) rectangle.
We eventually decide that it is a rectangle, but not closed, so it gets
passed along. Then we construct a GrShape, which tries to simplify the
shape, again asking if it's a rectangle...
Each isRect query was iterating over the 16k verbs, and for each one,
calling atVerb(i). That, internally, calls verbs(), which calls validate()
in debug builds. So we were walking all 16k points 16k times (to ensure
the bounds were correct). The end result is that the GM took over 11
seconds to draw, and now takes 3 ms.
Bug: skia:
Change-Id: If5f7a067b8c25f049dc64275d94a42ae4a50f6a9
Reviewed-on: https://skia-review.googlesource.com/34723
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2017-08-15 20:08:48 +00:00
|
|
|
const SkPoint* points() const { return fPoints; }
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for this->points() + this->countPoints()
|
|
|
|
*/
|
|
|
|
const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
|
|
|
|
|
Speed up convexpaths GM in debug builds
That GM has a path with 16k points, arranged in a rectangle. When we try
to draw it in Ganesh, we first check to see if it's a (closed) rectangle.
We eventually decide that it is a rectangle, but not closed, so it gets
passed along. Then we construct a GrShape, which tries to simplify the
shape, again asking if it's a rectangle...
Each isRect query was iterating over the 16k verbs, and for each one,
calling atVerb(i). That, internally, calls verbs(), which calls validate()
in debug builds. So we were walking all 16k points 16k times (to ensure
the bounds were correct). The end result is that the GM took over 11
seconds to draw, and now takes 3 ms.
Bug: skia:
Change-Id: If5f7a067b8c25f049dc64275d94a42ae4a50f6a9
Reviewed-on: https://skia-review.googlesource.com/34723
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2017-08-15 20:08:48 +00:00
|
|
|
const SkScalar* conicWeights() const { return fConicWeights.begin(); }
|
|
|
|
const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
|
2013-05-31 15:17:50 +00:00
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
/**
|
|
|
|
* Convenience methods for getting to a verb or point by index.
|
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
uint8_t atVerb(int index) const {
|
2012-10-03 13:46:20 +00:00
|
|
|
SkASSERT((unsigned) index < (unsigned) fVerbCnt);
|
|
|
|
return this->verbs()[~index];
|
|
|
|
}
|
|
|
|
const SkPoint& atPoint(int index) const {
|
|
|
|
SkASSERT((unsigned) index < (unsigned) fPointCnt);
|
|
|
|
return this->points()[index];
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:48:49 +00:00
|
|
|
bool operator== (const SkPathRef& ref) const;
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the path points and verbs to a buffer.
|
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
void writeToBuffer(SkWBuffer* buffer) const;
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of bytes that would be written in writeBuffer()
|
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
uint32_t writeSize() const;
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2016-02-18 12:11:48 +00:00
|
|
|
void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
|
|
|
|
|
2013-10-30 18:57:55 +00:00
|
|
|
/**
|
|
|
|
* Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
|
|
|
|
* same ID then they have the same verbs and points. However, two path refs may have the same
|
|
|
|
* contents but different genIDs.
|
|
|
|
*/
|
|
|
|
uint32_t genID() const;
|
|
|
|
|
2018-06-08 18:00:44 +00:00
|
|
|
struct GenIDChangeListener : SkRefCnt {
|
2015-08-04 17:01:58 +00:00
|
|
|
virtual ~GenIDChangeListener() {}
|
|
|
|
virtual void onChange() = 0;
|
|
|
|
};
|
|
|
|
|
2018-09-17 21:29:39 +00:00
|
|
|
void addGenIDChangeListener(sk_sp<GenIDChangeListener>); // Threadsafe.
|
2015-08-04 17:01:58 +00:00
|
|
|
|
2017-08-10 19:16:37 +00:00
|
|
|
bool isValid() const;
|
|
|
|
SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
|
2014-12-15 20:28:33 +00:00
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
private:
|
2013-09-26 12:18:23 +00:00
|
|
|
enum SerializationOffsets {
|
2017-09-21 20:59:17 +00:00
|
|
|
kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored.
|
|
|
|
kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored.
|
|
|
|
kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored.
|
|
|
|
kIsFinite_SerializationShift = 25, // requires 1 bit
|
|
|
|
kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored.
|
2018-02-20 14:48:13 +00:00
|
|
|
kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
|
2013-09-26 12:18:23 +00:00
|
|
|
};
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
SkPathRef() {
|
2013-09-26 12:18:23 +00:00
|
|
|
fBoundsIsDirty = true; // this also invalidates fIsFinite
|
2012-10-03 13:46:20 +00:00
|
|
|
fPointCnt = 0;
|
|
|
|
fVerbCnt = 0;
|
2017-08-28 14:34:05 +00:00
|
|
|
fVerbs = nullptr;
|
|
|
|
fPoints = nullptr;
|
2012-10-03 13:46:20 +00:00
|
|
|
fFreeSpace = 0;
|
|
|
|
fGenerationID = kEmptyGenID;
|
2013-12-12 23:03:51 +00:00
|
|
|
fSegmentMask = 0;
|
2013-12-03 16:43:54 +00:00
|
|
|
fIsOval = false;
|
2015-11-19 22:47:43 +00:00
|
|
|
fIsRRect = false;
|
2016-05-27 16:17:04 +00:00
|
|
|
// The next two values don't matter unless fIsOval or fIsRRect are true.
|
2016-07-22 20:51:42 +00:00
|
|
|
fRRectOrOvalIsCCW = false;
|
|
|
|
fRRectOrOvalStartIdx = 0xAC;
|
2013-06-27 18:39:39 +00:00
|
|
|
SkDEBUGCODE(fEditorsAttached = 0;)
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(this->validate();)
|
2012-10-03 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:48:49 +00:00
|
|
|
void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2018-02-20 14:48:13 +00:00
|
|
|
// Doesn't read fSegmentMask, but (re)computes it from the verbs array
|
|
|
|
unsigned computeSegmentMask() const;
|
|
|
|
|
2013-09-26 12:18:23 +00:00
|
|
|
// Return true if the computed bounds are finite.
|
|
|
|
static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
|
2015-07-28 13:00:50 +00:00
|
|
|
return bounds->setBoundsCheck(ref.points(), ref.countPoints());
|
2013-09-26 12:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called, if dirty, by getBounds()
|
|
|
|
void computeBounds() const {
|
|
|
|
SkDEBUGCODE(this->validate();)
|
2014-07-16 21:18:20 +00:00
|
|
|
// TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
|
|
|
|
// using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
|
2014-12-01 14:59:54 +00:00
|
|
|
SkASSERT(fBoundsIsDirty);
|
2013-09-26 12:18:23 +00:00
|
|
|
|
2014-12-01 14:59:54 +00:00
|
|
|
fIsFinite = ComputePtBounds(&fBounds, *this);
|
2013-09-26 12:18:23 +00:00
|
|
|
fBoundsIsDirty = false;
|
|
|
|
}
|
|
|
|
|
2013-12-13 19:36:25 +00:00
|
|
|
void setBounds(const SkRect& rect) {
|
|
|
|
SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
|
|
|
|
fBounds = rect;
|
|
|
|
fBoundsIsDirty = false;
|
2014-12-01 14:59:54 +00:00
|
|
|
fIsFinite = fBounds.isFinite();
|
2013-12-13 19:36:25 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
/** Makes additional room but does not change the counts or change the genID */
|
|
|
|
void incReserve(int additionalVerbs, int additionalPoints) {
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(this->validate();)
|
2012-10-03 13:46:20 +00:00
|
|
|
size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
|
|
|
|
this->makeSpace(space);
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(this->validate();)
|
2012-10-03 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 12:18:23 +00:00
|
|
|
/** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
|
2012-10-03 13:46:20 +00:00
|
|
|
* allocates space for reserveVerb additional verbs and reservePoints additional points.*/
|
2013-05-31 15:17:50 +00:00
|
|
|
void resetToSize(int verbCount, int pointCount, int conicCount,
|
2013-10-02 17:49:50 +00:00
|
|
|
int reserveVerbs = 0, int reservePoints = 0) {
|
|
|
|
SkDEBUGCODE(this->validate();)
|
|
|
|
fBoundsIsDirty = true; // this also invalidates fIsFinite
|
|
|
|
fGenerationID = 0;
|
|
|
|
|
2013-12-12 23:03:51 +00:00
|
|
|
fSegmentMask = 0;
|
2013-12-03 16:43:54 +00:00
|
|
|
fIsOval = false;
|
2015-11-19 22:47:43 +00:00
|
|
|
fIsRRect = false;
|
2013-12-03 16:43:54 +00:00
|
|
|
|
2013-10-02 17:49:50 +00:00
|
|
|
size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
|
|
|
|
size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
|
|
|
|
size_t minSize = newSize + newReserve;
|
|
|
|
|
|
|
|
ptrdiff_t sizeDelta = this->currSize() - minSize;
|
|
|
|
|
|
|
|
if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
|
|
|
|
sk_free(fPoints);
|
2017-08-28 14:34:05 +00:00
|
|
|
fPoints = nullptr;
|
|
|
|
fVerbs = nullptr;
|
2013-10-02 17:49:50 +00:00
|
|
|
fFreeSpace = 0;
|
|
|
|
fVerbCnt = 0;
|
|
|
|
fPointCnt = 0;
|
2018-09-11 18:01:42 +00:00
|
|
|
this->makeSpace(minSize, true);
|
2013-10-02 17:49:50 +00:00
|
|
|
fVerbCnt = verbCount;
|
|
|
|
fPointCnt = pointCount;
|
|
|
|
fFreeSpace -= newSize;
|
|
|
|
} else {
|
|
|
|
fPointCnt = pointCount;
|
|
|
|
fVerbCnt = verbCount;
|
|
|
|
fFreeSpace = this->currSize() - minSize;
|
|
|
|
}
|
|
|
|
fConicWeights.setCount(conicCount);
|
|
|
|
SkDEBUGCODE(this->validate();)
|
|
|
|
}
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-16 07:01:40 +00:00
|
|
|
* Increases the verb count by numVbs and point count by the required amount.
|
|
|
|
* The new points are uninitialized. All the new verbs are set to the specified
|
2013-12-12 23:03:51 +00:00
|
|
|
* verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
|
|
|
|
* uninitialized conic weights.
|
2012-10-03 13:46:20 +00:00
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increases the verb count 1, records the new verb, and creates room for the requisite number
|
|
|
|
* of additional points. A pointer to the first point is returned. Any new points are
|
|
|
|
* uninitialized.
|
|
|
|
*/
|
2013-12-12 23:03:51 +00:00
|
|
|
SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that the free space available in the path ref is >= size. The verb and point counts
|
2018-09-11 18:01:42 +00:00
|
|
|
* are not changed. May allocate extra capacity, unless |exact| is true.
|
2012-10-03 13:46:20 +00:00
|
|
|
*/
|
2018-09-11 18:01:42 +00:00
|
|
|
void makeSpace(size_t size, bool exact = false) {
|
2013-10-02 17:49:50 +00:00
|
|
|
SkDEBUGCODE(this->validate();)
|
2017-07-31 20:57:01 +00:00
|
|
|
if (size <= fFreeSpace) {
|
2013-10-02 17:49:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-31 20:57:01 +00:00
|
|
|
size_t growSize = size - fFreeSpace;
|
2013-10-02 17:49:50 +00:00
|
|
|
size_t oldSize = this->currSize();
|
2018-09-11 18:01:42 +00:00
|
|
|
|
|
|
|
if (!exact) {
|
|
|
|
// round to next multiple of 8 bytes
|
|
|
|
growSize = (growSize + 7) & ~static_cast<size_t>(7);
|
|
|
|
// we always at least double the allocation
|
|
|
|
if (growSize < oldSize) {
|
|
|
|
growSize = oldSize;
|
|
|
|
}
|
|
|
|
if (growSize < kMinSize) {
|
|
|
|
growSize = kMinSize;
|
|
|
|
}
|
2013-10-02 17:49:50 +00:00
|
|
|
}
|
2018-09-11 18:01:42 +00:00
|
|
|
|
2017-07-31 20:57:01 +00:00
|
|
|
constexpr size_t maxSize = std::numeric_limits<size_t>::max();
|
|
|
|
size_t newSize;
|
|
|
|
if (growSize <= maxSize - oldSize) {
|
|
|
|
newSize = oldSize + growSize;
|
|
|
|
} else {
|
|
|
|
SK_ABORT("Path too big.");
|
|
|
|
}
|
2013-10-02 17:49:50 +00:00
|
|
|
// Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
|
|
|
|
// encapsulate this.
|
|
|
|
fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
|
|
|
|
size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
|
2017-07-31 20:57:01 +00:00
|
|
|
void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
|
|
|
|
void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
|
2013-10-02 17:49:50 +00:00
|
|
|
memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
|
2017-07-31 20:57:01 +00:00
|
|
|
fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
|
2013-10-02 17:49:50 +00:00
|
|
|
fFreeSpace += growSize;
|
|
|
|
SkDEBUGCODE(this->validate();)
|
|
|
|
}
|
2012-10-03 13:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private, non-const-ptr version of the public function verbsMemBegin().
|
|
|
|
*/
|
|
|
|
uint8_t* verbsMemWritable() {
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(this->validate();)
|
2012-10-03 13:46:20 +00:00
|
|
|
return fVerbs - fVerbCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total amount of space allocated for verbs, points, and reserve.
|
|
|
|
*/
|
|
|
|
size_t currSize() const {
|
|
|
|
return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:44:08 +00:00
|
|
|
/**
|
|
|
|
* Called the first time someone calls CreateEmpty to actually create the singleton.
|
|
|
|
*/
|
2014-10-13 20:17:56 +00:00
|
|
|
friend SkPathRef* sk_create_empty_pathref();
|
2013-10-23 14:44:08 +00:00
|
|
|
|
2016-05-27 16:17:04 +00:00
|
|
|
void setIsOval(bool isOval, bool isCCW, unsigned start) {
|
|
|
|
fIsOval = isOval;
|
|
|
|
fRRectOrOvalIsCCW = isCCW;
|
2018-03-20 08:38:31 +00:00
|
|
|
fRRectOrOvalStartIdx = SkToU8(start);
|
2016-05-27 16:17:04 +00:00
|
|
|
}
|
2013-12-03 16:43:54 +00:00
|
|
|
|
2016-05-27 16:17:04 +00:00
|
|
|
void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
|
|
|
|
fIsRRect = isRRect;
|
|
|
|
fRRectOrOvalIsCCW = isCCW;
|
2018-03-20 08:38:31 +00:00
|
|
|
fRRectOrOvalStartIdx = SkToU8(start);
|
2016-05-27 16:17:04 +00:00
|
|
|
}
|
2015-11-19 22:47:43 +00:00
|
|
|
|
|
|
|
// called only by the editor. Note that this is not a const function.
|
2013-12-16 07:01:40 +00:00
|
|
|
SkPoint* getPoints() {
|
|
|
|
SkDEBUGCODE(this->validate();)
|
2013-12-13 19:36:25 +00:00
|
|
|
fIsOval = false;
|
2015-11-19 22:47:43 +00:00
|
|
|
fIsRRect = false;
|
|
|
|
return fPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkPoint* getPoints() const {
|
|
|
|
SkDEBUGCODE(this->validate();)
|
2013-12-16 07:01:40 +00:00
|
|
|
return fPoints;
|
2013-12-13 19:36:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 17:01:58 +00:00
|
|
|
void callGenIDChangeListeners();
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
enum {
|
|
|
|
kMinSize = 256,
|
|
|
|
};
|
|
|
|
|
2014-12-01 14:59:54 +00:00
|
|
|
mutable SkRect fBounds;
|
2013-09-26 12:18:23 +00:00
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
SkPoint* fPoints; // points to begining of the allocation
|
|
|
|
uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
|
|
|
|
int fVerbCnt;
|
|
|
|
int fPointCnt;
|
|
|
|
size_t fFreeSpace; // redundant but saves computation
|
2013-05-31 15:17:50 +00:00
|
|
|
SkTDArray<SkScalar> fConicWeights;
|
|
|
|
|
2012-10-03 13:46:20 +00:00
|
|
|
enum {
|
|
|
|
kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
|
|
|
|
};
|
2013-10-30 18:57:55 +00:00
|
|
|
mutable uint32_t fGenerationID;
|
2013-06-27 18:39:39 +00:00
|
|
|
SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
|
2012-10-03 13:46:20 +00:00
|
|
|
|
2018-09-17 21:29:39 +00:00
|
|
|
SkMutex fGenIDChangeListenersMutex;
|
|
|
|
SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are reffed
|
2015-08-04 17:01:58 +00:00
|
|
|
|
2015-11-19 22:47:43 +00:00
|
|
|
mutable uint8_t fBoundsIsDirty;
|
2018-06-12 20:30:29 +00:00
|
|
|
mutable bool fIsFinite; // only meaningful if bounds are valid
|
2015-11-19 22:47:43 +00:00
|
|
|
|
2018-06-12 20:30:29 +00:00
|
|
|
bool fIsOval;
|
|
|
|
bool fIsRRect;
|
2016-05-27 16:17:04 +00:00
|
|
|
// Both the circle and rrect special cases have a notion of direction and starting point
|
|
|
|
// The next two variables store that information for either.
|
2018-06-12 20:30:29 +00:00
|
|
|
bool fRRectOrOvalIsCCW;
|
2016-05-27 16:17:04 +00:00
|
|
|
uint8_t fRRectOrOvalStartIdx;
|
2015-11-19 22:47:43 +00:00
|
|
|
uint8_t fSegmentMask;
|
|
|
|
|
2013-12-13 19:36:25 +00:00
|
|
|
friend class PathRefTest_Private;
|
2015-11-19 22:47:43 +00:00
|
|
|
friend class ForceIsRRect_Private; // unit test isRRect
|
2018-09-11 18:01:42 +00:00
|
|
|
friend class SkPath;
|
2012-10-03 13:46:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|