Skip to content

Commit

Permalink
Clean up in, about, and around GrTextBlob::SubRun
Browse files Browse the repository at this point in the history
- Simplify ctors
- Remove using for VertexRegenerator - helps navigation in IDE
- Move SubRun and Run appendGlyph into paint to co-locate them

etc.

Change-Id: I6ef75a18e41cff577b8d4e9bb41ab8e312da532a
Reviewed-on: https://skia-review.googlesource.com/c/170540
Auto-Submit: Herb Derby <[email protected]>
Commit-Queue: Mike Klein <[email protected]>
Reviewed-by: Mike Klein <[email protected]>
  • Loading branch information
herbderby authored and Skia Commit-Bot committed Nov 12, 2018
1 parent fd1fd27 commit 6dff60e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 159 deletions.
108 changes: 108 additions & 0 deletions src/core/SkGlyphRunPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,114 @@ void GrTextContext::drawGlyphRunList(
clip, viewMatrix, origin.x(), origin.y());
}

void GrTextBlob::SubRun::appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect) {

this->joinGlyphBounds(dstRect);

bool hasW = this->hasWCoord();
// glyphs drawn in perspective must always have a w coord.
SkASSERT(hasW || !blob->fInitialViewMatrix.hasPerspective());
auto maskFormat = this->maskFormat();
size_t vertexStride = GetVertexStride(maskFormat, hasW);

intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + fVertexEndIndex);

// We always write the third position component used by SDFs. If it is unused it gets
// overwritten. Similarly, we always write the color and the blob will later overwrite it
// with texture coords if it is unused.
size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
// V0
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fTop, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V1
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fBottom, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V2
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fTop, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V3
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fBottom, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;

fVertexEndIndex += vertexStride * kVerticesPerGlyph;
blob->fGlyphs[fGlyphEndIndex++] = glyph;
}

static SkRect rect_to_draw(
const SkGlyph& glyph, SkPoint origin, SkScalar textScale, bool isDFT) {

SkScalar dx = SkIntToScalar(glyph.fLeft);
SkScalar dy = SkIntToScalar(glyph.fTop);
SkScalar width = SkIntToScalar(glyph.fWidth);
SkScalar height = SkIntToScalar(glyph.fHeight);

if (isDFT) {
dx += SK_DistanceFieldInset;
dy += SK_DistanceFieldInset;
width -= 2 * SK_DistanceFieldInset;
height -= 2 * SK_DistanceFieldInset;
}

dx *= textScale;
dy *= textScale;
width *= textScale;
height *= textScale;

return SkRect::MakeXYWH(origin.x() + dx, origin.y() + dy, width, height);
}

void GrTextBlob::Run::appendGlyph(GrTextBlob* blob,
const sk_sp<GrTextStrike>& strike,
const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
SkPoint origin,
const SkPMColor4f& color4f, SkGlyphCache* skGlyphCache,
SkScalar textRatio, bool needsTransform) {

GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
skGlyph.getSubXFixed(),
skGlyph.getSubYFixed(),
maskStyle);
GrGlyph* glyph = strike->getGlyph(skGlyph, id, skGlyphCache);
if (!glyph) {
return;
}

SkASSERT(skGlyph.fWidth == glyph->width());
SkASSERT(skGlyph.fHeight == glyph->height());

bool isDFT = maskStyle == GrGlyph::kDistance_MaskStyle;

SkRect glyphRect = rect_to_draw(skGlyph, origin, textRatio, isDFT);
if (!glyphRect.isEmpty()) {
// TODO4F: Preserve float colors
GrColor color = color4f.toBytes_RGBA();

GrMaskFormat format = glyph->fMaskFormat;

SubRun* subRun = &fSubRunInfo.back();
if (fInitialized && subRun->maskFormat() != format) {
subRun = &pushBackSubRun();
subRun->setStrike(strike);
} else if (!fInitialized) {
subRun->setStrike(strike);
}

fInitialized = true;
subRun->setMaskFormat(format);
subRun->setColor(color);
subRun->setNeedsTransform(needsTransform);

subRun->appendGlyph(blob, glyph, glyphRect);
}
}


void GrTextBlob::generateFromGlyphRunList(GrGlyphCache* glyphCache,
const GrShaderCaps& shaderCaps,
const GrTextContext::Options& options,
Expand Down
2 changes: 1 addition & 1 deletion src/core/SkRectPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SkRectPriv {
return { SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax };
}

static SkRect MakeLargestInverted() {
static constexpr SkRect MakeLargestInverted() {
return { SK_ScalarMax, SK_ScalarMax, SK_ScalarMin, SK_ScalarMin };
}

Expand Down
68 changes: 0 additions & 68 deletions src/gpu/text/GrTextBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,74 +74,6 @@ SkExclusiveStrikePtr GrTextBlob::Run::setupCache(const SkPaint& skPaint,
return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *fTypeface);
}

static SkRect rect_to_draw(
const SkGlyph& glyph, SkPoint origin, SkScalar textScale, bool isDFT) {

SkScalar dx = SkIntToScalar(glyph.fLeft);
SkScalar dy = SkIntToScalar(glyph.fTop);
SkScalar width = SkIntToScalar(glyph.fWidth);
SkScalar height = SkIntToScalar(glyph.fHeight);

if (isDFT) {
dx += SK_DistanceFieldInset;
dy += SK_DistanceFieldInset;
width -= 2 * SK_DistanceFieldInset;
height -= 2 * SK_DistanceFieldInset;
}

dx *= textScale;
dy *= textScale;
width *= textScale;
height *= textScale;

return SkRect::MakeXYWH(origin.x() + dx, origin.y() + dy, width, height);
}

void GrTextBlob::Run::appendGlyph(GrTextBlob* blob,
const sk_sp<GrTextStrike>& strike,
const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
SkPoint origin,
const SkPMColor4f& color4f, SkGlyphCache* skGlyphCache,
SkScalar textRatio, bool needsTransform) {

GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
skGlyph.getSubXFixed(),
skGlyph.getSubYFixed(),
maskStyle);
GrGlyph* glyph = strike->getGlyph(skGlyph, id, skGlyphCache);
if (!glyph) {
return;
}

SkASSERT(skGlyph.fWidth == glyph->width());
SkASSERT(skGlyph.fHeight == glyph->height());

bool isDFT = maskStyle == GrGlyph::kDistance_MaskStyle;

SkRect glyphRect = rect_to_draw(skGlyph, origin, textRatio, isDFT);
if (!glyphRect.isEmpty()) {
// TODO4F: Preserve float colors
GrColor color = color4f.toBytes_RGBA();

GrMaskFormat format = glyph->fMaskFormat;

SubRun* subRun = &fSubRunInfo.back();
if (fInitialized && subRun->maskFormat() != format) {
subRun = &pushBackSubRun();
subRun->setStrike(strike);
} else if (!fInitialized) {
subRun->setStrike(strike);
}

fInitialized = true;
subRun->setMaskFormat(format);
subRun->setColor(color);
subRun->setNeedsTransform(needsTransform);

subRun->appendGlyph(blob, glyph, glyphRect);
}
}

void GrTextBlob::Run::appendPathGlyph(const SkPath& path, SkPoint position,
SkScalar scale, bool preTransformed) {
fPathGlyphs.push_back(PathGlyph(path, position.x(), position.y(), scale, preTransformed));
Expand Down
97 changes: 16 additions & 81 deletions src/gpu/text/GrTextBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {

Run* pushBackRun() {
SkASSERT(fRunCount < fRunCountLimit);

// If there is more run, then connect up the subruns.
if (fRunCount > 0) {
SubRun& newRun = fRuns[fRunCount].fSubRunInfo.back();
SubRun& lastRun = fRuns[fRunCount - 1].fSubRunInfo.back();
Expand Down Expand Up @@ -254,7 +256,6 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
, fMinMaxScale(SK_ScalarMax)
, fTextType(0) {}


// This function will only be called when we are generating a blob from scratch. We record the
// initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
// these numbers. When blobs are reused with new matrices, we need to return to model space so
Expand All @@ -275,74 +276,9 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {

class SubRun {
public:
SubRun()
: fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
, fVertexStartIndex(0)
, fVertexEndIndex(0)
, fGlyphStartIndex(0)
, fGlyphEndIndex(0)
, fColor(GrColor_ILLEGAL)
, fMaskFormat(kA8_GrMaskFormat)
, fFlags(0) {
fVertexBounds = SkRectPriv::MakeLargestInverted();
}
SubRun(const SubRun& that)
: fBulkUseToken(that.fBulkUseToken)
, fStrike(SkSafeRef(that.fStrike.get()))
, fCurrentViewMatrix(that.fCurrentViewMatrix)
, fVertexBounds(that.fVertexBounds)
, fAtlasGeneration(that.fAtlasGeneration)
, fVertexStartIndex(that.fVertexStartIndex)
, fVertexEndIndex(that.fVertexEndIndex)
, fGlyphStartIndex(that.fGlyphStartIndex)
, fGlyphEndIndex(that.fGlyphEndIndex)
, fX(that.fX)
, fY(that.fY)
, fColor(that.fColor)
, fMaskFormat(that.fMaskFormat)
, fFlags(that.fFlags) {
}

void appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect) {

this->joinGlyphBounds(dstRect);

bool hasW = this->hasWCoord();
// glyphs drawn in perspective must always have a w coord.
SkASSERT(hasW || !blob->fInitialViewMatrix.hasPerspective());
auto maskFormat = this->maskFormat();
size_t vertexStride = GetVertexStride(maskFormat, hasW);

intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + fVertexEndIndex);

// We always write the third position component used by SDFs. If it is unused it gets
// overwritten. Similarly, we always write the color and the blob will later overwrite it
// with texture coords if it is unused.
size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
// V0
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fTop, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V1
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fBottom, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V2
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fTop, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;
vertex += vertexStride;

// V3
*reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fBottom, 1.f};
*reinterpret_cast<GrColor*>(vertex + colorOffset) = fColor;

fVertexEndIndex += vertexStride * kVerticesPerGlyph;
blob->fGlyphs[fGlyphEndIndex++] = glyph;
}

SubRun() = default;

void appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect);

// TODO when this object is more internal, drop the privacy
void resetBulkUseToken() { fBulkUseToken.reset(); }
Expand All @@ -368,10 +304,10 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {

void setAsSuccessor(const SubRun& prev) {
fGlyphStartIndex = prev.glyphEndIndex();
fGlyphEndIndex = prev.glyphEndIndex();
fGlyphEndIndex = fGlyphStartIndex;

fVertexStartIndex = prev.vertexEndIndex();
fVertexEndIndex = prev.vertexEndIndex();
fVertexEndIndex = fVertexStartIndex;

// copy over viewmatrix settings
this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
Expand Down Expand Up @@ -425,17 +361,17 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
sk_sp<GrTextStrike> fStrike;
SkMatrix fCurrentViewMatrix;
SkRect fVertexBounds;
uint64_t fAtlasGeneration;
size_t fVertexStartIndex;
size_t fVertexEndIndex;
uint32_t fGlyphStartIndex;
uint32_t fGlyphEndIndex;
SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
size_t fVertexStartIndex{0};
size_t fVertexEndIndex{0};
uint32_t fGlyphStartIndex{0};
uint32_t fGlyphEndIndex{0};
SkScalar fX;
SkScalar fY;
GrColor fColor;
GrMaskFormat fMaskFormat;
uint32_t fFlags;
GrColor fColor{GrColor_ILLEGAL};
GrMaskFormat fMaskFormat{kA8_GrMaskFormat};
uint32_t fFlags{0};
}; // SubRunInfo


Expand Down Expand Up @@ -522,9 +458,8 @@ class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
newSubRun.setAsSuccessor(prevSubRun);
return newSubRun;
}
static const int kMinSubRuns = 1;
sk_sp<SkTypeface> fTypeface;
SkSTArray<kMinSubRuns, SubRun> fSubRunInfo;
SkSTArray<1, SubRun> fSubRunInfo;
SkAutoDescriptor fDescriptor;

// Effects from the paint that are used to build a SkScalerContext.
Expand Down
20 changes: 11 additions & 9 deletions src/gpu/text/GrTextBlobVertexRegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "SkGlyphCache.h"
#include "ops/GrAtlasTextOp.h"

using Regenerator = GrTextBlob::VertexRegenerator;

enum RegenMask {
kNoRegen = 0x0,
kRegenPos = 0x1,
Expand Down Expand Up @@ -191,11 +189,15 @@ inline void regen_vertices(char* vertex, const GrGlyph* glyph, size_t vertexStri
}
}

Regenerator::VertexRegenerator(GrResourceProvider* resourceProvider, GrTextBlob* blob,
int runIdx, int subRunIdx,
const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
GrDeferredUploadTarget* uploadTarget, GrGlyphCache* glyphCache,
GrAtlasManager* fullAtlasManager, SkExclusiveStrikePtr* lazyCache)
GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
GrTextBlob* blob,
int runIdx, int subRunIdx,
const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
GrColor color,
GrDeferredUploadTarget* uploadTarget,
GrGlyphCache* glyphCache,
GrAtlasManager* fullAtlasManager,
SkExclusiveStrikePtr* lazyCache)
: fResourceProvider(resourceProvider)
, fViewMatrix(viewMatrix)
, fBlob(blob)
Expand Down Expand Up @@ -230,7 +232,7 @@ Regenerator::VertexRegenerator(GrResourceProvider* resourceProvider, GrTextBlob*
}

template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
bool Regenerator::doRegen(Regenerator::Result* result) {
bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result) {
static_assert(!regenGlyphs || regenTexCoords, "must regenTexCoords along regenGlyphs");
sk_sp<GrTextStrike> strike;
if (regenTexCoords) {
Expand Down Expand Up @@ -319,7 +321,7 @@ bool Regenerator::doRegen(Regenerator::Result* result) {
return true;
}

bool Regenerator::regenerate(Regenerator::Result* result) {
bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
// If regenerate() is called multiple times then the atlas gen may have changed. So we check
// this each time.
Expand Down

0 comments on commit 6dff60e

Please sign in to comment.