diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gpu/GrShape.cpp | 254 | ||||
-rw-r--r-- | src/gpu/GrShape.h | 160 | ||||
-rw-r--r-- | src/gpu/GrStyle.cpp | 33 | ||||
-rw-r--r-- | src/gpu/GrStyle.h | 92 |
4 files changed, 0 insertions, 539 deletions
diff --git a/src/gpu/GrShape.cpp b/src/gpu/GrShape.cpp deleted file mode 100644 index 27450cb695..0000000000 --- a/src/gpu/GrShape.cpp +++ /dev/null @@ -1,254 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "GrShape.h" - -GrShape& GrShape::operator=(const GrShape& that) { - bool wasPath = Type::kPath == fType; - fStyle = that.fStyle; - fType = that.fType; - switch (fType) { - case Type::kEmpty: - if (wasPath) { - fPath.reset(); - } - break; - case Type::kRRect: - if (wasPath) { - fPath.reset(); - } - fRRect = that.fRRect; - break; - case Type::kPath: - if (wasPath) { - *fPath.get() = *that.fPath.get(); - } else { - fPath.set(*that.fPath.get()); - } - break; - } - fInheritedKey.reset(that.fInheritedKey.count()); - memcpy(fInheritedKey.get(), that.fInheritedKey.get(), - sizeof(uint32_t) * fInheritedKey.count()); - return *this; -} - -int GrShape::unstyledKeySize() const { - if (fInheritedKey.count()) { - return fInheritedKey.count(); - } - switch (fType) { - case Type::kEmpty: - return 1; - case Type::kRRect: - SkASSERT(!fInheritedKey.count()); - SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t)); - return SkRRect::kSizeInMemory / sizeof(uint32_t); - case Type::kPath: - if (fPath.get()->isVolatile()) { - return -1; - } else { - return 1; - } - } - SkFAIL("Should never get here."); - return 0; -} - -void GrShape::writeUnstyledKey(uint32_t* key) const { - SkASSERT(this->unstyledKeySize()); - SkDEBUGCODE(uint32_t* origKey = key;) - if (fInheritedKey.count()) { - memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count()); - SkDEBUGCODE(key += fInheritedKey.count();) - } else { - switch (fType) { - case Type::kEmpty: - *key++ = 1; - break; - case Type::kRRect: - fRRect.writeToMemory(key); - key += SkRRect::kSizeInMemory / sizeof(uint32_t); - break; - case Type::kPath: - SkASSERT(!fPath.get()->isVolatile()); - *key++ = fPath.get()->getGenerationID(); - break; - } - } - SkASSERT(key - origKey == this->unstyledKeySize()); -} - -int GrShape::StyleKeySize(const GrStyle& style, bool stopAfterPE) { - GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); - int size = 0; - if (style.isDashed()) { - // One scalar for dash phase and one for each dash value. - size += 1 + style.dashIntervalCnt(); - } else if (style.pathEffect()) { - // No key for a generic path effect. - return -1; - } - - if (stopAfterPE) { - return size; - } - - if (style.strokeRec().needToApply()) { - // One for style/cap/join, 2 for miter and width. - size += 3; - } - return size; -} - -void GrShape::StyleKey(uint32_t* key, const GrStyle& style, bool stopAfterPE) { - SkASSERT(key); - SkASSERT(StyleKeySize(style, stopAfterPE) >= 0); - GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); - - int i = 0; - if (style.isDashed()) { - GR_STATIC_ASSERT(sizeof(style.dashPhase()) == sizeof(uint32_t)); - SkScalar phase = style.dashPhase(); - memcpy(&key[i++], &phase, sizeof(SkScalar)); - - int32_t count = style.dashIntervalCnt(); - // Dash count should always be even. - SkASSERT(0 == (count & 0x1)); - const SkScalar* intervals = style.dashIntervals(); - int intervalByteCnt = count * sizeof(SkScalar); - memcpy(&key[i], intervals, intervalByteCnt); - SkDEBUGCODE(i += count); - } else { - SkASSERT(!style.pathEffect()); - } - - if (!stopAfterPE && style.strokeRec().needToApply()) { - enum { - kStyleBits = 2, - kJoinBits = 2, - kCapBits = 32 - kStyleBits - kJoinBits, - - kJoinShift = kStyleBits, - kCapShift = kJoinShift + kJoinBits, - }; - GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); - GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits)); - GR_STATIC_ASSERT(SkPaint::kCapCount <= (1 << kCapBits)); - key[i++] = style.strokeRec().getStyle() | - style.strokeRec().getJoin() << kJoinShift | - style.strokeRec().getCap() << kCapShift; - - SkScalar scalar; - // Miter limit only affects miter joins - scalar = SkPaint::kMiter_Join == style.strokeRec().getJoin() - ? style.strokeRec().getMiter() - : -1.f; - memcpy(&key[i++], &scalar, sizeof(scalar)); - - scalar = style.strokeRec().getWidth(); - memcpy(&key[i++], &scalar, sizeof(scalar)); - } - SkASSERT(StyleKeySize(style, stopAfterPE) == i); -} - -void GrShape::setInheritedKey(const GrShape &parent, bool stopAfterPE){ - SkASSERT(!fInheritedKey.count()); - // If the output shape turns out to be simple, then we will just use its geometric key - if (Type::kPath == fType) { - // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as - // ApplyFullStyle(shape). - // The full key is structured as (geo,path_effect,stroke). - // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then - // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key - // and then append the style key (which should now be stroke only) at the end. - int parentCnt = parent.fInheritedKey.count(); - bool useParentGeoKey = !parentCnt; - if (useParentGeoKey) { - parentCnt = parent.unstyledKeySize(); - } - int styleCnt = StyleKeySize(parent.fStyle, stopAfterPE); - if (styleCnt < 0) { - // The style doesn't allow a key, set the path to volatile so that we fail when - // we try to get a key for the shape. - fPath.get()->setIsVolatile(true); - } else { - fInheritedKey.reset(parentCnt + styleCnt); - if (useParentGeoKey) { - // This will be the geo key. - parent.writeUnstyledKey(fInheritedKey.get()); - } else { - // This should be geo,path_effect - memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), - parentCnt * sizeof(uint32_t)); - } - // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) - StyleKey(fInheritedKey.get() + parentCnt, parent.fStyle, stopAfterPE); - } - } -} - -GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) { - switch (fType) { - case Type::kEmpty: - return; - case Type::kRRect: - fRRect = that.fRRect; - return; - case Type::kPath: - fPath.set(*that.fPath.get()); - return; - } - fInheritedKey.reset(that.fInheritedKey.count()); - memcpy(fInheritedKey.get(), that.fInheritedKey.get(), - sizeof(uint32_t) * fInheritedKey.count()); -} - -GrShape::GrShape(const GrShape& parent, bool stopAfterPE) { - fType = Type::kEmpty; - SkPathEffect* pe = parent.fStyle.pathEffect(); - const SkPath* inPath; - SkStrokeRec strokeRec = parent.fStyle.strokeRec(); - if (pe) { - fType = Type::kPath; - fPath.init(); - if (parent.fType == Type::kPath) { - inPath = parent.fPath.get(); - } else { - inPath = fPath.get(); - parent.asPath(fPath.get()); - } - // Should we consider bounds? Would have to include in key, but it'd be nice to know - // if the bounds actually modified anything before including in key. - if (!pe->filterPath(fPath.get(), *inPath, &strokeRec, nullptr)) { - // Make an empty unstyled shape if filtering fails. - fType = Type::kEmpty; - fStyle = GrStyle(); - fPath.reset(); - return; - } - inPath = fPath.get(); - } else if (stopAfterPE || !strokeRec.needToApply()) { - *this = parent; - return; - } else { - fType = Type::kPath; - fPath.init(); - if (parent.fType == Type::kPath) { - inPath = parent.fPath.get(); - } else { - inPath = fPath.get(); - parent.asPath(fPath.get()); - } - } - if (!stopAfterPE) { - strokeRec.applyToPath(fPath.get(), *inPath); - } else { - fStyle = GrStyle(strokeRec, nullptr); - } - this->setInheritedKey(parent, stopAfterPE); -} diff --git a/src/gpu/GrShape.h b/src/gpu/GrShape.h deleted file mode 100644 index 3c3f9ecec7..0000000000 --- a/src/gpu/GrShape.h +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrShape_DEFINED -#define GrShape_DEFINED - -#include "GrStyle.h" -#include "SkPath.h" -#include "SkRRect.h" -#include "SkTemplates.h" -#include "SkTLazy.h" - -/** - * Represents a geometric shape (rrect or path) and the GrStyle that it should be rendered with. - * It is possible to apply the style to the GrShape to produce a new GrShape where the geometry - * reflects the styling information (e.g. is stroked). It is also possible to apply just the - * path effect from the style. In this case the resulting shape will include any remaining - * stroking information that is to be applied after the path effect. - * - * Shapes can produce keys that represent only the geometry information, not the style. Note that - * when styling information is applied to produce a new shape then the style has been converted - * to geometric information and is included in the new shape's key. When the same style is applied - * to two shapes that reflect the same underlying geometry the computed keys of the stylized shapes - * will be the same. - * - * Currently this can only be constructed from a rrect, though it can become a path by applying - * style to the geometry. The idea is to expand this to cover most or all of the geometries that - * have SkCanvas::draw APIs. - */ -class GrShape { -public: - GrShape() : fType(Type::kEmpty) {} - - explicit GrShape(const SkRRect& rrect) : fType(Type::kRRect), fRRect(rrect) {} - explicit GrShape(const SkRect& rect) : fType(Type::kRRect), fRRect(SkRRect::MakeRect(rect)) {} - - GrShape(const SkRRect& rrect, const GrStyle& style) - : fType(Type::kRRect) - , fRRect(rrect) - , fStyle(style) {} - - GrShape(const SkRect& rect, const GrStyle& style) - : fType(Type::kRRect) - , fRRect(SkRRect::MakeRect(rect)) - , fStyle(style) {} - - GrShape(const SkRRect& rrect, const SkPaint& paint) - : fType(Type::kRRect) - , fRRect(rrect) - , fStyle(paint) {} - - GrShape(const SkRect& rect, const SkPaint& paint) - : fType(Type::kRRect) - , fRRect(SkRRect::MakeRect(rect)) - , fStyle(paint) {} - - GrShape(const GrShape&); - GrShape& operator=(const GrShape& that); - - ~GrShape() { - if (Type::kPath == fType) { - fPath.reset(); - } - } - - const GrStyle& style() const { return fStyle; } - - /** - * Returns a GrShape where the shape's geometry fully reflects the original shape's GrStyle. - * The GrStyle of the returned shape will either be fill or hairline. - */ - GrShape applyFullStyle() { return GrShape(*this, false); } - - /** - * Similar to above but applies only the path effect. Path effects take the original geometry - * and fill/stroking information and compute a new geometry and residual fill/stroking - * information to be applied. The path effect's output geometry and stroking will be captured - * in the returned GrShape. - */ - GrShape applyPathEffect() { return GrShape(*this, true); } - - bool asRRect(SkRRect* rrect) const { - if (Type::kRRect != fType) { - return false; - } - if (rrect) { - *rrect = fRRect; - } - return true; - } - - void asPath(SkPath* out) const { - switch (fType) { - case Type::kRRect: - out->reset(); - out->addRRect(fRRect); - break; - case Type::kPath: - *out = *fPath.get(); - break; - case Type::kEmpty: - out->reset(); - break; - } - } - - /** - * Gets the size of the key for the shape represented by this GrShape (ignoring its styling). - * A negative value is returned if the shape has no key (shouldn't be cached). - */ - int unstyledKeySize() const; - - /** - * Writes unstyledKeySize() bytes into the provided pointer. Assumes that there is enough - * space allocated for the key and that unstyledKeySize() does not return a negative value - * for this shape. - */ - void writeUnstyledKey(uint32_t* key) const; - -private: - /** - * Computes the key length for a GrStyle. The return will be negative if it cannot be turned - * into a key. - */ - static int StyleKeySize(const GrStyle& , bool stopAfterPE); - - /** - * Writes a unique key for the style into the provided buffer. This function assumes the buffer - * has room for at least StyleKeySize() values. It assumes that StyleKeySize() returns a - * positive value for the style and stopAfterPE param. This is written so that the key for just - * dash application followed by the key for the remaining SkStrokeRec is the same as the - * key for applying dashing and SkStrokeRec all at once. - */ - static void StyleKey(uint32_t*, const GrStyle&, bool stopAfterPE); - - /** Constructor used by Apply* functions */ - GrShape(const GrShape& parentShape, bool stopAfterPE); - - /** - * Determines the key we should inherit from the input shape's geometry and style when - * we are applying the style to create a new shape. - */ - void setInheritedKey(const GrShape& parentShape, bool stopAfterPE); - - enum class Type { - kEmpty, - kRRect, - kPath, - } fType; - - SkRRect fRRect; - SkTLazy<SkPath> fPath; - GrStyle fStyle; - SkAutoSTArray<8, uint32_t> fInheritedKey; -}; -#endif diff --git a/src/gpu/GrStyle.cpp b/src/gpu/GrStyle.cpp deleted file mode 100644 index 40a148bb4a..0000000000 --- a/src/gpu/GrStyle.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "GrStyle.h" - -void GrStyle::initPathEffect(SkPathEffect* pe) { - if (!pe) { - fDashInfo.fType = SkPathEffect::kNone_DashType; - return; - } - SkPathEffect::DashInfo info; - if (SkPathEffect::kDash_DashType == pe->asADash(&info)) { - if (fStrokeRec.getStyle() == SkStrokeRec::kFill_Style) { - fPathEffect.reset(nullptr); - } else { - fPathEffect.reset(SkSafeRef(pe)); - fDashInfo.fType = SkPathEffect::kDash_DashType; - fDashInfo.fIntervals.reset(info.fCount); - fDashInfo.fPhase = info.fPhase; - info.fIntervals = fDashInfo.fIntervals.get(); - pe->asADash(&info); - return; - } - } else { - fPathEffect.reset(SkSafeRef(pe)); - } - fDashInfo.fType = SkPathEffect::kNone_DashType; - fDashInfo.fIntervals.reset(0); -} diff --git a/src/gpu/GrStyle.h b/src/gpu/GrStyle.h deleted file mode 100644 index 4eef252de4..0000000000 --- a/src/gpu/GrStyle.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrStyle_DEFINED -#define GrStyle_DEFINED - -#include "GrTypes.h" -#include "SkPathEffect.h" -#include "SkStrokeRec.h" -#include "SkTemplates.h" - -/** - * Represents the various ways that a GrShape can be styled. It has fill/stroking information - * as well as an optional path effect. If the path effect represents dashing, the dashing - * information is extracted from the path effect and stored explicitly. - * - * This object does not support stroke-and-fill styling. It is expected that stroking and filling - * is handled by drawing a stroke and a fill separately. - * - * This will replace GrStrokeInfo as GrShape is deployed. - */ -class GrStyle { -public: - GrStyle() : fStrokeRec(SkStrokeRec::kFill_InitStyle) { - fDashInfo.fType = SkPathEffect::kNone_DashType; - } - - GrStyle(const SkStrokeRec& strokeRec, SkPathEffect* pe) : fStrokeRec(strokeRec) { - SkASSERT(SkStrokeRec::kStrokeAndFill_Style != strokeRec.getStyle()); - this->initPathEffect(pe); - } - - GrStyle(const GrStyle& that) : fStrokeRec(SkStrokeRec::kFill_InitStyle) { - *this = that; - } - - explicit GrStyle(const SkPaint& paint) : fStrokeRec(paint) { - SkASSERT(SkStrokeRec::kStrokeAndFill_Style != fStrokeRec.getStyle()); - this->initPathEffect(paint.getPathEffect()); - } - - GrStyle& operator=(const GrStyle& that) { - fPathEffect = that.fPathEffect; - fDashInfo = that.fDashInfo; - fStrokeRec = that.fStrokeRec; - return *this; - } - SkPathEffect* pathEffect() const { return fPathEffect.get(); } - - bool isDashed() const { return SkPathEffect::kDash_DashType == fDashInfo.fType; } - SkScalar dashPhase() const { - SkASSERT(this->isDashed()); - return fDashInfo.fPhase; - } - int dashIntervalCnt() const { - SkASSERT(this->isDashed()); - return fDashInfo.fIntervals.count(); - } - const SkScalar* dashIntervals() const { - SkASSERT(this->isDashed()); - return fDashInfo.fIntervals.get(); - } - - const SkStrokeRec& strokeRec() const { return fStrokeRec; } - -private: - void initPathEffect(SkPathEffect* pe); - - struct DashInfo { - DashInfo& operator=(const DashInfo& that) { - fType = that.fType; - fPhase = that.fPhase; - fIntervals.reset(that.fIntervals.count()); - memcpy(fIntervals.get(), that.fIntervals.get(), - sizeof(SkScalar) * that.fIntervals.count()); - return *this; - } - SkPathEffect::DashType fType; - SkScalar fPhase; - SkAutoSTArray<4, SkScalar> fIntervals; - }; - - SkStrokeRec fStrokeRec; - sk_sp<SkPathEffect> fPathEffect; - DashInfo fDashInfo; -}; - -#endif |