2012-11-29 13:24:55 +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 SkRRect_DEFINED
|
|
|
|
#define SkRRect_DEFINED
|
|
|
|
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
|
2012-12-12 20:48:18 +00:00
|
|
|
class SkPath;
|
2013-11-05 15:54:42 +00:00
|
|
|
class SkMatrix;
|
2017-09-21 16:29:24 +00:00
|
|
|
class SkRBuffer;
|
|
|
|
class SkWBuffer;
|
2012-12-12 20:48:18 +00:00
|
|
|
|
2012-11-29 13:24:55 +00:00
|
|
|
// Path forward:
|
|
|
|
// core work
|
|
|
|
// add contains(SkRect&) - for clip stack
|
|
|
|
// add contains(SkRRect&) - for clip stack
|
|
|
|
// add heart rect computation (max rect inside RR)
|
|
|
|
// add 9patch rect computation
|
|
|
|
// add growToInclude(SkPath&)
|
|
|
|
// analysis
|
|
|
|
// use growToInclude to fit skp round rects & generate stats (RRs vs. real paths)
|
|
|
|
// check on # of rectorus's the RRs could handle
|
|
|
|
// rendering work
|
2014-02-16 23:35:31 +00:00
|
|
|
// update SkPath.addRRect() to only use quads
|
2012-11-29 13:24:55 +00:00
|
|
|
// add GM and bench
|
|
|
|
// further out
|
|
|
|
// detect and triangulate RRectorii rather than falling back to SW in Ganesh
|
2012-11-30 02:01:24 +00:00
|
|
|
//
|
2012-11-29 13:24:55 +00:00
|
|
|
|
|
|
|
/** \class SkRRect
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
The SkRRect class represents a rounded rect with a potentially different
|
|
|
|
radii for each corner. It does not have a constructor so must be
|
|
|
|
initialized with one of the initialization functions (e.g., setEmpty,
|
2012-11-29 13:24:55 +00:00
|
|
|
setRectRadii, etc.)
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
This class is intended to roughly match CSS' border-*-*-radius capabilities.
|
2012-11-29 13:24:55 +00:00
|
|
|
This means:
|
2012-11-30 02:01:24 +00:00
|
|
|
If either of a corner's radii are 0 the corner will be square.
|
2012-11-29 13:24:55 +00:00
|
|
|
Negative radii are not allowed (they are clamped to zero).
|
|
|
|
If the corner curves overlap they will be proportionally reduced to fit.
|
|
|
|
*/
|
|
|
|
class SK_API SkRRect {
|
|
|
|
public:
|
2017-09-20 15:05:49 +00:00
|
|
|
SkRRect() { this->setEmpty(); }
|
2016-08-15 21:49:10 +00:00
|
|
|
SkRRect(const SkRRect&) = default;
|
|
|
|
SkRRect& operator=(const SkRRect&) = default;
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Enum to capture the various possible subtypes of RR. Accessed
|
|
|
|
* by type(). The subtypes become progressively less restrictive.
|
|
|
|
*/
|
|
|
|
enum Type {
|
|
|
|
// !< The RR is empty
|
|
|
|
kEmpty_Type,
|
|
|
|
|
|
|
|
//!< The RR is actually a (non-empty) rect (i.e., at least one radius
|
|
|
|
//!< at each corner is zero)
|
2012-11-30 02:01:24 +00:00
|
|
|
kRect_Type,
|
2012-11-29 13:24:55 +00:00
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
//!< The RR is actually a (non-empty) oval (i.e., all x radii are equal
|
2012-11-29 13:24:55 +00:00
|
|
|
//!< and >= width/2 and all the y radii are equal and >= height/2
|
2012-11-30 02:01:24 +00:00
|
|
|
kOval_Type,
|
2012-11-29 13:24:55 +00:00
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
//!< The RR is non-empty and all the x radii are equal & all y radii
|
|
|
|
//!< are equal but it is not an oval (i.e., there are lines between
|
2012-11-29 13:24:55 +00:00
|
|
|
//!< the curves) nor a rect (i.e., both radii are non-zero)
|
2012-11-30 02:01:24 +00:00
|
|
|
kSimple_Type,
|
2012-11-29 13:24:55 +00:00
|
|
|
|
2014-03-17 21:17:30 +00:00
|
|
|
//!< The RR is non-empty and the two left x radii are equal, the two top
|
|
|
|
//!< y radii are equal, and the same for the right and bottom but it is
|
|
|
|
//!< neither an rect, oval, nor a simple RR. It is called "nine patch"
|
|
|
|
//!< because the centers of the corner ellipses form an axis aligned
|
|
|
|
//!< rect with edges that divide the RR into an 9 rectangular patches:
|
|
|
|
//!< an interior patch, four edge patches, and four corner patches.
|
|
|
|
kNinePatch_Type,
|
|
|
|
|
2012-11-29 13:24:55 +00:00
|
|
|
//!< A fully general (non-empty) RR. Some of the x and/or y radii are
|
|
|
|
//!< different from the others and there must be one corner where
|
|
|
|
//!< both radii are non-zero.
|
|
|
|
kComplex_Type,
|
2017-08-23 20:26:32 +00:00
|
|
|
|
|
|
|
kLastType = kComplex_Type,
|
2012-11-29 13:24:55 +00:00
|
|
|
};
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Returns the RR's sub type.
|
|
|
|
*/
|
2012-12-12 20:48:18 +00:00
|
|
|
Type getType() const {
|
2016-09-23 18:23:22 +00:00
|
|
|
SkASSERT(this->isValid());
|
2014-10-22 18:23:56 +00:00
|
|
|
return static_cast<Type>(fType);
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 20:48:18 +00:00
|
|
|
Type type() const { return this->getType(); }
|
|
|
|
|
|
|
|
inline bool isEmpty() const { return kEmpty_Type == this->getType(); }
|
|
|
|
inline bool isRect() const { return kRect_Type == this->getType(); }
|
|
|
|
inline bool isOval() const { return kOval_Type == this->getType(); }
|
|
|
|
inline bool isSimple() const { return kSimple_Type == this->getType(); }
|
2015-09-15 17:20:55 +00:00
|
|
|
// TODO: should isSimpleCircular & isCircle take a tolerance? This could help
|
|
|
|
// instances where the mapping to device space is noisy.
|
2014-02-19 15:18:05 +00:00
|
|
|
inline bool isSimpleCircular() const {
|
2015-09-15 17:20:55 +00:00
|
|
|
return this->isSimple() && SkScalarNearlyEqual(fRadii[0].fX, fRadii[0].fY);
|
|
|
|
}
|
|
|
|
inline bool isCircle() const {
|
|
|
|
return this->isOval() && SkScalarNearlyEqual(fRadii[0].fX, fRadii[0].fY);
|
2014-02-19 15:18:05 +00:00
|
|
|
}
|
2014-03-17 21:17:30 +00:00
|
|
|
inline bool isNinePatch() const { return kNinePatch_Type == this->getType(); }
|
2012-12-12 20:48:18 +00:00
|
|
|
inline bool isComplex() const { return kComplex_Type == this->getType(); }
|
|
|
|
|
2017-04-20 19:48:37 +00:00
|
|
|
bool allCornersCircular(SkScalar tolerance = SK_ScalarNearlyZero) const;
|
2014-03-10 22:53:20 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
SkScalar width() const { return fRect.width(); }
|
|
|
|
SkScalar height() const { return fRect.height(); }
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Set this RR to the empty rectangle (0,0,0,0) with 0 x & y radii.
|
|
|
|
*/
|
2012-11-30 02:01:24 +00:00
|
|
|
void setEmpty() {
|
2012-11-29 13:24:55 +00:00
|
|
|
fRect.setEmpty();
|
|
|
|
memset(fRadii, 0, sizeof(fRadii));
|
|
|
|
fType = kEmpty_Type;
|
|
|
|
|
2016-09-23 18:23:22 +00:00
|
|
|
SkASSERT(this->isValid());
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Set this RR to match the supplied rect. All radii will be 0.
|
|
|
|
*/
|
|
|
|
void setRect(const SkRect& rect) {
|
2015-09-29 18:24:07 +00:00
|
|
|
fRect = rect;
|
|
|
|
fRect.sort();
|
|
|
|
|
2017-09-20 15:05:49 +00:00
|
|
|
if (fRect.isEmpty() || !fRect.isFinite()) {
|
2012-11-29 13:24:55 +00:00
|
|
|
this->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(fRadii, 0, sizeof(fRadii));
|
|
|
|
fType = kRect_Type;
|
|
|
|
|
2016-09-23 18:23:22 +00:00
|
|
|
SkASSERT(this->isValid());
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 21:01:25 +00:00
|
|
|
static SkRRect MakeEmpty() {
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setEmpty();
|
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
2015-08-10 02:41:13 +00:00
|
|
|
static SkRRect MakeRect(const SkRect& r) {
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setRect(r);
|
|
|
|
return rr;
|
|
|
|
}
|
2016-06-06 21:01:25 +00:00
|
|
|
|
2015-09-15 17:20:55 +00:00
|
|
|
static SkRRect MakeOval(const SkRect& oval) {
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setOval(oval);
|
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:17:35 +00:00
|
|
|
static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setRectXY(rect, xRad, yRad);
|
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Set this RR to match the supplied oval. All x radii will equal half the
|
|
|
|
* width and all y radii will equal half the height.
|
|
|
|
*/
|
|
|
|
void setOval(const SkRect& oval) {
|
2015-09-29 18:24:07 +00:00
|
|
|
fRect = oval;
|
|
|
|
fRect.sort();
|
|
|
|
|
2017-09-20 15:05:49 +00:00
|
|
|
if (fRect.isEmpty() || !fRect.isFinite()) {
|
2012-11-29 13:24:55 +00:00
|
|
|
this->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-29 18:24:07 +00:00
|
|
|
SkScalar xRad = SkScalarHalf(fRect.width());
|
|
|
|
SkScalar yRad = SkScalarHalf(fRect.height());
|
2012-11-29 13:24:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
fRadii[i].set(xRad, yRad);
|
|
|
|
}
|
|
|
|
fType = kOval_Type;
|
|
|
|
|
2016-09-23 18:23:22 +00:00
|
|
|
SkASSERT(this->isValid());
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Initialize the RR with the same radii for all four corners.
|
|
|
|
*/
|
|
|
|
void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
|
|
|
|
|
2014-03-17 21:17:30 +00:00
|
|
|
/**
|
|
|
|
* Initialize the rr with one radius per-side.
|
|
|
|
*/
|
|
|
|
void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
|
|
|
|
SkScalar rightRad, SkScalar bottomRad);
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
/**
|
2012-11-29 13:24:55 +00:00
|
|
|
* Initialize the RR with potentially different radii for all four corners.
|
|
|
|
*/
|
|
|
|
void setRectRadii(const SkRect& rect, const SkVector radii[4]);
|
|
|
|
|
2012-11-30 02:01:24 +00:00
|
|
|
// The radii are stored in UL, UR, LR, LL order.
|
2012-11-29 13:24:55 +00:00
|
|
|
enum Corner {
|
|
|
|
kUpperLeft_Corner,
|
|
|
|
kUpperRight_Corner,
|
|
|
|
kLowerRight_Corner,
|
|
|
|
kLowerLeft_Corner
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkRect& rect() const { return fRect; }
|
|
|
|
const SkVector& radii(Corner corner) const { return fRadii[corner]; }
|
2012-12-12 20:48:18 +00:00
|
|
|
const SkRect& getBounds() const { return fRect; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a rrect is simple, all of its radii are equal. This returns one
|
|
|
|
* of those radii. This call requires the rrect to be non-complex.
|
|
|
|
*/
|
|
|
|
const SkVector& getSimpleRadii() const {
|
|
|
|
SkASSERT(!this->isComplex());
|
|
|
|
return fRadii[0];
|
|
|
|
}
|
2012-11-29 13:24:55 +00:00
|
|
|
|
|
|
|
friend bool operator==(const SkRRect& a, const SkRRect& b) {
|
|
|
|
return a.fRect == b.fRect &&
|
2013-03-08 18:35:13 +00:00
|
|
|
SkScalarsEqual(a.fRadii[0].asScalars(),
|
|
|
|
b.fRadii[0].asScalars(), 8);
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const SkRRect& a, const SkRRect& b) {
|
|
|
|
return a.fRect != b.fRect ||
|
2013-03-08 18:35:13 +00:00
|
|
|
!SkScalarsEqual(a.fRadii[0].asScalars(),
|
|
|
|
b.fRadii[0].asScalars(), 8);
|
2012-11-29 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-23 23:11:21 +00:00
|
|
|
/**
|
|
|
|
* Call inset on the bounds, and adjust the radii to reflect what happens
|
|
|
|
* in stroking: If the corner is sharp (no curvature), leave it alone,
|
|
|
|
* otherwise we grow/shrink the radii by the amount of the inset. If a
|
|
|
|
* given radius becomes negative, it is pinned to 0.
|
|
|
|
*
|
|
|
|
* It is valid for dst == this.
|
|
|
|
*/
|
2012-12-17 02:10:42 +00:00
|
|
|
void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
|
2012-12-23 23:11:21 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
void inset(SkScalar dx, SkScalar dy) {
|
|
|
|
this->inset(dx, dy, this);
|
|
|
|
}
|
2012-12-23 23:11:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call outset on the bounds, and adjust the radii to reflect what happens
|
|
|
|
* in stroking: If the corner is sharp (no curvature), leave it alone,
|
|
|
|
* otherwise we grow/shrink the radii by the amount of the inset. If a
|
|
|
|
* given radius becomes negative, it is pinned to 0.
|
|
|
|
*
|
|
|
|
* It is valid for dst == this.
|
|
|
|
*/
|
2012-12-17 02:10:42 +00:00
|
|
|
void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
|
|
|
|
this->inset(-dx, -dy, dst);
|
|
|
|
}
|
|
|
|
void outset(SkScalar dx, SkScalar dy) {
|
|
|
|
this->inset(-dx, -dy, this);
|
|
|
|
}
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2014-03-04 16:25:34 +00:00
|
|
|
/**
|
|
|
|
* Translate the rrect by (dx, dy).
|
|
|
|
*/
|
|
|
|
void offset(SkScalar dx, SkScalar dy) {
|
|
|
|
fRect.offset(dx, dy);
|
|
|
|
}
|
2012-12-17 02:10:42 +00:00
|
|
|
|
2016-08-15 21:49:10 +00:00
|
|
|
SkRRect SK_WARN_UNUSED_RESULT makeOffset(SkScalar dx, SkScalar dy) const {
|
|
|
|
return SkRRect(fRect.makeOffset(dx, dy), fRadii, fType);
|
|
|
|
}
|
|
|
|
|
2013-04-25 12:23:00 +00:00
|
|
|
/**
|
|
|
|
* Returns true if 'rect' is wholy inside the RR, and both
|
|
|
|
* are not empty.
|
|
|
|
*/
|
|
|
|
bool contains(const SkRect& rect) const;
|
|
|
|
|
2016-09-23 18:23:22 +00:00
|
|
|
bool isValid() const;
|
2017-09-20 15:05:49 +00:00
|
|
|
static bool AreRectAndRadiiValid(const SkRect&, const SkVector[4]);
|
2012-11-29 13:24:55 +00:00
|
|
|
|
2012-12-12 20:48:18 +00:00
|
|
|
enum {
|
|
|
|
kSizeInMemory = 12 * sizeof(SkScalar)
|
|
|
|
};
|
2012-12-13 02:01:33 +00:00
|
|
|
|
2012-12-12 20:48:18 +00:00
|
|
|
/**
|
|
|
|
* Write the rrect into the specified buffer. This is guaranteed to always
|
|
|
|
* write kSizeInMemory bytes, and that value is guaranteed to always be
|
|
|
|
* a multiple of 4. Return kSizeInMemory.
|
|
|
|
*/
|
2013-11-05 15:46:56 +00:00
|
|
|
size_t writeToMemory(void* buffer) const;
|
2017-09-21 16:29:24 +00:00
|
|
|
void writeToBuffer(SkWBuffer*) const;
|
2012-12-12 20:48:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-05 15:46:56 +00:00
|
|
|
* Reads the rrect from the specified buffer
|
|
|
|
*
|
|
|
|
* If the specified buffer is large enough, this will read kSizeInMemory bytes,
|
|
|
|
* and that value is guaranteed to always be a multiple of 4.
|
|
|
|
*
|
|
|
|
* @param buffer Memory to read from
|
|
|
|
* @param length Amount of memory available in the buffer
|
|
|
|
* @return number of bytes read (must be a multiple of 4) or
|
|
|
|
* 0 if there was not enough memory available
|
2012-12-12 20:48:18 +00:00
|
|
|
*/
|
2013-11-05 15:46:56 +00:00
|
|
|
size_t readFromMemory(const void* buffer, size_t length);
|
2017-09-21 16:29:24 +00:00
|
|
|
bool readFromBuffer(SkRBuffer*);
|
2012-12-12 20:48:18 +00:00
|
|
|
|
2013-11-05 15:54:42 +00:00
|
|
|
/**
|
|
|
|
* Transform by the specified matrix, and put the result in dst.
|
|
|
|
*
|
|
|
|
* @param matrix SkMatrix specifying the transform. Must only contain
|
|
|
|
* scale and/or translate, or this call will fail.
|
|
|
|
* @param dst SkRRect to store the result. It is an error to use this,
|
|
|
|
* which would make this function no longer const.
|
2017-10-11 21:13:28 +00:00
|
|
|
* @return true on success, false on failure.
|
2013-11-05 15:54:42 +00:00
|
|
|
*/
|
|
|
|
bool transform(const SkMatrix& matrix, SkRRect* dst) const;
|
|
|
|
|
2014-12-15 15:59:53 +00:00
|
|
|
void dump(bool asHex) const;
|
|
|
|
void dump() const { this->dump(false); }
|
|
|
|
void dumpHex() const { this->dump(true); }
|
2014-06-09 14:59:06 +00:00
|
|
|
|
2012-11-29 13:24:55 +00:00
|
|
|
private:
|
2016-08-15 21:49:10 +00:00
|
|
|
SkRRect(const SkRect& rect, const SkVector radii[4], int32_t type)
|
|
|
|
: fRect(rect)
|
|
|
|
, fRadii{radii[0], radii[1], radii[2], radii[3]}
|
|
|
|
, fType(type) {}
|
|
|
|
|
2012-11-29 13:24:55 +00:00
|
|
|
SkRect fRect;
|
|
|
|
// Radii order is UL, UR, LR, LL. Use Corner enum to index into fRadii[]
|
2012-11-30 02:01:24 +00:00
|
|
|
SkVector fRadii[4];
|
2014-10-22 18:23:56 +00:00
|
|
|
// use an explicitly sized type so we're sure the class is dense (no uninitialized bytes)
|
2014-12-18 21:29:54 +00:00
|
|
|
int32_t fType;
|
2012-11-29 13:24:55 +00:00
|
|
|
// TODO: add padding so we can use memcpy for flattening and not copy
|
|
|
|
// uninitialized data
|
|
|
|
|
2014-12-18 21:29:54 +00:00
|
|
|
void computeType();
|
2013-04-25 12:23:00 +00:00
|
|
|
bool checkCornerContainment(SkScalar x, SkScalar y) const;
|
2016-02-23 18:32:40 +00:00
|
|
|
void scaleRadii();
|
2012-12-12 20:48:18 +00:00
|
|
|
|
|
|
|
// to access fRadii directly
|
|
|
|
friend class SkPath;
|
2012-11-29 13:24:55 +00:00
|
|
|
};
|
|
|
|
|
2012-11-29 13:49:33 +00:00
|
|
|
#endif
|