joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkColorFilter.h" |
| 9 | #include "include/gpu/GrContext.h" |
| 10 | #include "src/core/SkMaskFilterBase.h" |
| 11 | #include "src/core/SkPaintPriv.h" |
| 12 | #include "src/gpu/GrBlurUtils.h" |
| 13 | #include "src/gpu/GrClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrStyle.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 15 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/ops/GrAtlasTextOp.h" |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 17 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/text/GrTextBlob.h" |
| 19 | #include "src/gpu/text/GrTextTarget.h" |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 20 | |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 21 | #include <new> |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 22 | |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 23 | static SkVector calculate_translation(bool applyVM, |
| 24 | const SkMatrix& drawMatrix, SkPoint drawOrigin, |
| 25 | const SkMatrix& currentViewMatrix, SkPoint currentOrigin) { |
| 26 | SkVector translate; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 27 | if (applyVM) { |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 28 | translate.fX = drawMatrix.getTranslateX() + |
| 29 | drawMatrix.getScaleX() * (drawOrigin.x() - currentOrigin.x()) + |
| 30 | drawMatrix.getSkewX() * (drawOrigin.y() - currentOrigin.y()) - |
| 31 | currentViewMatrix.getTranslateX(); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 32 | |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 33 | translate.fY = drawMatrix.getTranslateY() + |
| 34 | drawMatrix.getSkewY() * (drawOrigin.x() - currentOrigin.x()) + |
| 35 | drawMatrix.getScaleY() * (drawOrigin.y() - currentOrigin.y()) - |
| 36 | currentViewMatrix.getTranslateY(); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 37 | } else { |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 38 | translate = drawOrigin - currentOrigin; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 39 | } |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 40 | |
| 41 | return translate; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static SkMatrix make_inverse(const SkMatrix& matrix) { |
| 45 | SkMatrix inverseMatrix; |
| 46 | if (!matrix.invert(&inverseMatrix)) { |
| 47 | inverseMatrix = SkMatrix::I(); |
| 48 | } |
| 49 | return inverseMatrix; |
| 50 | } |
| 51 | |
| 52 | // -- GrTextBlob::Key ------------------------------------------------------------------------------ |
| 53 | GrTextBlob::Key::Key() { sk_bzero(this, sizeof(Key)); } |
| 54 | |
| 55 | bool GrTextBlob::Key::operator==(const GrTextBlob::Key& other) const { |
| 56 | return 0 == memcmp(this, &other, sizeof(Key)); |
| 57 | } |
| 58 | |
| 59 | // -- GrTextBlob::PathGlyph ------------------------------------------------------------------------ |
| 60 | GrTextBlob::PathGlyph::PathGlyph(const SkPath& path, SkPoint origin) |
| 61 | : fPath(path) |
| 62 | , fOrigin(origin) {} |
| 63 | |
| 64 | // -- GrTextBlob::SubRun --------------------------------------------------------------------------- |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 65 | // Hold data to draw the different types of sub run. SubRuns are produced knowing all the |
| 66 | // glyphs that are included in them. |
| 67 | class GrTextBlob::SubRun { |
| 68 | public: |
| 69 | // SubRun for masks |
| 70 | SubRun(SubRunType type, |
| 71 | GrTextBlob* textBlob, |
| 72 | const SkStrikeSpec& strikeSpec, |
| 73 | GrMaskFormat format, |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 74 | const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData, |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 75 | sk_sp<GrTextStrike>&& grStrike); |
| 76 | |
| 77 | // SubRun for paths |
| 78 | SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec); |
| 79 | |
| 80 | void appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables); |
| 81 | |
| 82 | // TODO when this object is more internal, drop the privacy |
| 83 | void resetBulkUseToken(); |
| 84 | GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken(); |
| 85 | void setStrike(sk_sp<GrTextStrike> strike); |
| 86 | GrTextStrike* strike() const; |
| 87 | sk_sp<GrTextStrike> refStrike() const; |
| 88 | |
| 89 | void setAtlasGeneration(uint64_t atlasGeneration); |
| 90 | uint64_t atlasGeneration() const; |
| 91 | |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 92 | void setColor(GrColor color); |
| 93 | GrColor color() const; |
| 94 | |
| 95 | GrMaskFormat maskFormat() const; |
| 96 | |
| 97 | const SkRect& vertexBounds() const; |
| 98 | void joinGlyphBounds(const SkRect& glyphBounds); |
| 99 | |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 100 | // This function assumes the translation will be applied before it is called again |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 101 | SkVector computeTranslation(const SkMatrix& drawMatrix, SkPoint drawOrigin); |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 102 | |
| 103 | bool drawAsDistanceFields() const; |
| 104 | bool drawAsPaths() const; |
| 105 | bool needsTransform() const; |
| 106 | bool hasW() const; |
| 107 | |
| 108 | // df properties |
| 109 | void setUseLCDText(bool useLCDText); |
| 110 | bool hasUseLCDText() const; |
| 111 | void setAntiAliased(bool antiAliased); |
| 112 | bool isAntiAliased() const; |
| 113 | |
| 114 | const SkStrikeSpec& strikeSpec() const; |
| 115 | |
| 116 | SubRun* fNextSubRun{nullptr}; |
| 117 | const SubRunType fType; |
| 118 | GrTextBlob* const fBlob; |
| 119 | const GrMaskFormat fMaskFormat; |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 120 | const SkSpan<GrGlyph*> fGlyphs; |
| 121 | const SkSpan<char> fVertexData; |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 122 | const SkStrikeSpec fStrikeSpec; |
| 123 | sk_sp<GrTextStrike> fStrike; |
| 124 | struct { |
| 125 | bool useLCDText:1; |
| 126 | bool antiAliased:1; |
| 127 | } fFlags{false, false}; |
| 128 | GrColor fColor; |
| 129 | GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken; |
| 130 | SkRect fVertexBounds = SkRectPriv::MakeLargestInverted(); |
| 131 | uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration}; |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 132 | SkPoint fCurrentOrigin; |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 133 | SkMatrix fCurrentMatrix; |
Herb Derby | e3c7ff4 | 2019-12-10 17:49:28 -0500 | [diff] [blame] | 134 | std::vector<PathGlyph> fPaths; |
| 135 | }; // SubRun |
| 136 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 137 | GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec, |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 138 | GrMaskFormat format, |
| 139 | const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 140 | sk_sp<GrTextStrike>&& grStrike) |
| 141 | : fType{type} |
| 142 | , fBlob{textBlob} |
| 143 | , fMaskFormat{format} |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 144 | , fGlyphs{glyphs} |
| 145 | , fVertexData{vertexData} |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 146 | , fStrikeSpec{strikeSpec} |
| 147 | , fStrike{grStrike} |
| 148 | , fColor{textBlob->fColor} |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 149 | , fCurrentOrigin{textBlob->fInitialOrigin} |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 150 | , fCurrentMatrix{textBlob->fInitialMatrix} { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 151 | SkASSERT(type != kTransformedPath); |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 152 | textBlob->insertSubRun(this); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec) |
| 156 | : fType{kTransformedPath} |
| 157 | , fBlob{textBlob} |
| 158 | , fMaskFormat{kA8_GrMaskFormat} |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 159 | , fGlyphs{SkSpan<GrGlyph*>{}} |
| 160 | , fVertexData{SkSpan<char>{}} |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 161 | , fStrikeSpec{strikeSpec} |
| 162 | , fStrike{nullptr} |
| 163 | , fColor{textBlob->fColor} |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 164 | , fPaths{} { |
| 165 | textBlob->insertSubRun(this); |
| 166 | } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 167 | |
| 168 | void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) { |
| 169 | GrTextStrike* grStrike = fStrike.get(); |
| 170 | SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio(); |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 171 | GrGlyph** glyphCursor = fGlyphs.data(); |
| 172 | char* vertexCursor = fVertexData.data(); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 173 | bool hasW = this->hasW(); |
| 174 | GrColor color = this->color(); |
| 175 | // glyphs drawn in perspective must always have a w coord. |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 176 | SkASSERT(hasW || !fBlob->fInitialMatrix.hasPerspective()); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 177 | size_t vertexStride = GetVertexStride(fMaskFormat, hasW); |
| 178 | // We always write the third position component used by SDFs. If it is unused it gets |
| 179 | // overwritten. Similarly, we always write the color and the blob will later overwrite it |
| 180 | // with texture coords if it is unused. |
| 181 | size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint); |
| 182 | for (auto [variant, pos] : drawables) { |
| 183 | SkGlyph* skGlyph = variant; |
| 184 | GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph); |
| 185 | // Only floor the device coordinates. |
| 186 | SkRect dstRect; |
| 187 | if (!this->needsTransform()) { |
| 188 | pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())}; |
| 189 | dstRect = grGlyph->destRect(pos); |
| 190 | } else { |
| 191 | dstRect = grGlyph->destRect(pos, strikeToSource); |
| 192 | } |
| 193 | |
| 194 | this->joinGlyphBounds(dstRect); |
| 195 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 196 | // V0 |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 197 | *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fTop, 1.f}; |
| 198 | *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color; |
| 199 | vertexCursor += vertexStride; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 200 | |
| 201 | // V1 |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 202 | *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fBottom, 1.f}; |
| 203 | *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color; |
| 204 | vertexCursor += vertexStride; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 205 | |
| 206 | // V2 |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 207 | *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fTop, 1.f}; |
| 208 | *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color; |
| 209 | vertexCursor += vertexStride; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 210 | |
| 211 | // V3 |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 212 | *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fBottom, 1.f}; |
| 213 | *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color; |
| 214 | vertexCursor += vertexStride; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 215 | |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 216 | *glyphCursor++ = grGlyph; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 217 | } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); } |
| 221 | |
| 222 | GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; } |
| 223 | void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); } |
| 224 | GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); } |
| 225 | sk_sp<GrTextStrike> GrTextBlob::SubRun::refStrike() const { return fStrike; } |
| 226 | void GrTextBlob::SubRun::setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;} |
| 227 | uint64_t GrTextBlob::SubRun::atlasGeneration() const { return fAtlasGeneration; } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 228 | void GrTextBlob::SubRun::setColor(GrColor color) { fColor = color; } |
| 229 | GrColor GrTextBlob::SubRun::color() const { return fColor; } |
| 230 | GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; } |
| 231 | const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; } |
| 232 | void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) { |
| 233 | fVertexBounds.joinNonEmptyArg(glyphBounds); |
| 234 | } |
| 235 | |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 236 | SkVector GrTextBlob::SubRun::computeTranslation( |
| 237 | const SkMatrix& drawMatrix, SkPoint drawOrigin){ |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 238 | // Don't use the matrix to translate on distance field for fallback subruns. |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 239 | |
| 240 | SkVector translate = calculate_translation( |
| 241 | !this->drawAsDistanceFields() && !this->needsTransform(), |
| 242 | drawMatrix, drawOrigin, fCurrentMatrix, fCurrentOrigin); |
| 243 | |
| 244 | // Update SubRun indicating that the vertices now correspond to the origin and matrix used in |
| 245 | // the draw. |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 246 | fCurrentMatrix = drawMatrix; |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 247 | fCurrentOrigin = drawOrigin; |
| 248 | return translate; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; } |
| 252 | |
| 253 | bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; } |
| 254 | |
| 255 | bool GrTextBlob::SubRun::needsTransform() const { |
| 256 | return fType == kTransformedPath |
| 257 | || fType == kTransformedMask |
| 258 | || fType == kTransformedSDFT; |
| 259 | } |
| 260 | |
| 261 | bool GrTextBlob::SubRun::hasW() const { |
| 262 | return fBlob->hasW(fType); |
| 263 | } |
| 264 | |
| 265 | void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; } |
| 266 | bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; } |
| 267 | void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; } |
| 268 | bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; } |
| 269 | const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; } |
| 270 | |
| 271 | // -- GrTextBlob ----------------------------------------------------------------------------------- |
| 272 | void GrTextBlob::operator delete(void* p) { ::operator delete(p); } |
| 273 | void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); } |
| 274 | void* GrTextBlob::operator new(size_t, void* p) { return p; } |
| 275 | |
| 276 | GrTextBlob::~GrTextBlob() = default; |
| 277 | |
Herb Derby | 659e409 | 2019-12-06 15:38:10 -0500 | [diff] [blame] | 278 | sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList, |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 279 | GrStrikeCache* strikeCache, |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 280 | const SkMatrix& drawMatrix, |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 281 | GrColor color, |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 282 | bool forceWForDistanceFields) { |
Herb Derby | cd498e1 | 2019-12-06 14:17:31 -0500 | [diff] [blame] | 283 | |
Herb Derby | 3d1a3bb | 2019-12-06 18:15:49 -0500 | [diff] [blame] | 284 | static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex)); |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 285 | static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex)); |
| 286 | size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph; |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 287 | if (drawMatrix.hasPerspective() || forceWForDistanceFields) { |
Herb Derby | 3d1a3bb | 2019-12-06 18:15:49 -0500 | [diff] [blame] | 288 | static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex)); |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 289 | static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex)); |
| 290 | quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph; |
Herb Derby | e74c776 | 2019-12-04 14:15:41 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 293 | // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments. |
| 294 | static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex)); |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 295 | // Assume there is no padding needed between glyph pointers and vertices. |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 296 | static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex)); |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 297 | |
| 298 | // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding |
| 299 | // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array |
| 300 | // and the SubRun. |
| 301 | size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun); |
| 302 | size_t arenaSize = |
| 303 | sizeof(GrGlyph*) * glyphRunList.totalGlyphCount() |
| 304 | + quadSize * glyphRunList.totalGlyphCount() |
| 305 | + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding); |
| 306 | |
| 307 | size_t allocationSize = sizeof(GrTextBlob) + arenaSize; |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 308 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 309 | void* allocation = ::operator new (allocationSize); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 310 | |
Herb Derby | aebc5f8 | 2019-12-10 14:07:10 -0500 | [diff] [blame] | 311 | SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint()); |
Herb Derby | 00ae959 | 2019-12-03 15:55:56 -0500 | [diff] [blame] | 312 | sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{ |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 313 | arenaSize, strikeCache, drawMatrix, glyphRunList.origin(), |
Herb Derby | aebc5f8 | 2019-12-10 14:07:10 -0500 | [diff] [blame] | 314 | color, initialLuminance, forceWForDistanceFields}}; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 315 | |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 316 | return blob; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 319 | void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec, |
| 320 | const SkPaint& paint) { |
| 321 | fKey = key; |
| 322 | if (key.fHasBlur) { |
| 323 | fBlurRec = blurRec; |
| 324 | } |
| 325 | if (key.fStyle != SkPaint::kFill_Style) { |
| 326 | fStrokeInfo.fFrameWidth = paint.getStrokeWidth(); |
| 327 | fStrokeInfo.fMiterLimit = paint.getStrokeMiter(); |
| 328 | fStrokeInfo.fJoin = paint.getStrokeJoin(); |
| 329 | } |
| 330 | } |
| 331 | const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; } |
| 332 | uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); } |
| 333 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 334 | bool GrTextBlob::hasDistanceField() const { |
| 335 | return SkToBool(fTextType & kHasDistanceField_TextType); |
| 336 | } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 337 | bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); } |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 338 | bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 339 | |
| 340 | void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; } |
| 341 | void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; } |
| 342 | void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) { |
| 343 | // we init fMaxMinScale and fMinMaxScale in the constructor |
| 344 | fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale); |
| 345 | fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale); |
| 346 | } |
| 347 | |
| 348 | size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) { |
| 349 | switch (maskFormat) { |
| 350 | case kA8_GrMaskFormat: |
Herb Derby | cd498e1 | 2019-12-06 14:17:31 -0500 | [diff] [blame] | 351 | return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 352 | case kARGB_GrMaskFormat: |
Herb Derby | cd498e1 | 2019-12-06 14:17:31 -0500 | [diff] [blame] | 353 | return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 354 | default: |
| 355 | SkASSERT(!hasWCoord); |
Herb Derby | cd498e1 | 2019-12-06 14:17:31 -0500 | [diff] [blame] | 356 | return sizeof(Mask2DVertex); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Herb Derby | 0edb214 | 2018-10-16 17:04:11 -0400 | [diff] [blame] | 360 | bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition, |
| 361 | const SkMaskFilterBase::BlurRec& blurRec, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 362 | const SkMatrix& drawMatrix, SkPoint drawOrigin) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 363 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 364 | // to regenerate the blob on any color change |
| 365 | // We use the grPaint to get any color filter effects |
| 366 | if (fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
Herb Derby | aebc5f8 | 2019-12-10 14:07:10 -0500 | [diff] [blame] | 367 | fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 371 | if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 375 | /** This could be relaxed for blobs with only distance field glyphs. */ |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 376 | if (fInitialMatrix.hasPerspective() && !fInitialMatrix.cheapEqualTo(drawMatrix)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
| 380 | // We only cache one masked version |
| 381 | if (fKey.fHasBlur && |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 382 | (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 383 | return true; |
| 384 | } |
| 385 | |
| 386 | // Similarly, we only cache one version for each style |
| 387 | if (fKey.fStyle != SkPaint::kFill_Style && |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 388 | (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 389 | fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 390 | fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 395 | // for mixed blobs if this becomes an issue. |
| 396 | if (this->hasBitmap() && this->hasDistanceField()) { |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 397 | // Identical view matrices and we can reuse in all cases |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 398 | return !(fInitialMatrix.cheapEqualTo(drawMatrix) && drawOrigin == fInitialOrigin); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (this->hasBitmap()) { |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 402 | if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() || |
| 403 | fInitialMatrix.getScaleY() != drawMatrix.getScaleY() || |
| 404 | fInitialMatrix.getSkewX() != drawMatrix.getSkewX() || |
| 405 | fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
Herb Derby | 9830fc4 | 2019-09-12 10:58:26 -0400 | [diff] [blame] | 409 | // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust |
| 410 | // the quads properly. Devise a system that regenerates the quads from original data |
| 411 | // using the transform to allow this to be used in general. |
| 412 | |
| 413 | // We can update the positions in the text blob without regenerating the whole |
| 414 | // blob, but only for integer translations. |
| 415 | // This cool bit of math will determine the necessary translation to apply to the |
| 416 | // already generated vertex coordinates to move them to the correct position. |
| 417 | // Figure out the translation in view space given a translation in source space. |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 418 | SkScalar transX = drawMatrix.getTranslateX() + |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 419 | drawMatrix.getScaleX() * (drawOrigin.x() - fInitialOrigin.x()) + |
| 420 | drawMatrix.getSkewX() * (drawOrigin.y() - fInitialOrigin.y()) - |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 421 | fInitialMatrix.getTranslateX(); |
| 422 | SkScalar transY = drawMatrix.getTranslateY() + |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 423 | drawMatrix.getSkewY() * (drawOrigin.x() - fInitialOrigin.x()) + |
| 424 | drawMatrix.getScaleY() * (drawOrigin.y() - fInitialOrigin.y()) - |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 425 | fInitialMatrix.getTranslateY(); |
Herb Derby | 9830fc4 | 2019-09-12 10:58:26 -0400 | [diff] [blame] | 426 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) { |
| 427 | return true; |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 428 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 429 | } else if (this->hasDistanceField()) { |
| 430 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 431 | // distance field being generated, so we have to regenerate in those cases |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 432 | SkScalar newMaxScale = drawMatrix.getMaxScale(); |
| 433 | SkScalar oldMaxScale = fInitialMatrix.getMaxScale(); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 434 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 435 | if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) { |
| 436 | return true; |
| 437 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 438 | } |
| 439 | |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 440 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 441 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 442 | // the blob anyways at flush time, so no need to regenerate explicitly |
| 443 | return false; |
| 444 | } |
| 445 | |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 446 | void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props, |
| 447 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 448 | const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 449 | const SkMatrix& drawMatrix, SkPoint drawOrigin) { |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 450 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 451 | for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) { |
| 452 | if (subRun->drawAsPaths()) { |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 453 | SkPaint runPaint{paint}; |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 454 | runPaint.setAntiAlias(subRun->isAntiAliased()); |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 455 | // If there are shaders, blurs or styles, the path must be scaled into source |
| 456 | // space independently of the CTM. This allows the CTM to be correct for the |
| 457 | // different effects. |
| 458 | GrStyle style(runPaint); |
Herb Derby | 9f49148 | 2018-08-08 11:53:00 -0400 | [diff] [blame] | 459 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 460 | bool scalePath = runPaint.getShader() |
| 461 | || style.applies() |
| 462 | || runPaint.getMaskFilter(); |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 463 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 464 | // The origin for the blob may have changed, so figure out the delta. |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 465 | SkVector originShift = drawOrigin - fInitialOrigin; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 466 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 467 | for (const auto& pathGlyph : subRun->fPaths) { |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 468 | SkMatrix ctm{drawMatrix}; |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 469 | SkMatrix pathMatrix = SkMatrix::MakeScale( |
| 470 | subRun->fStrikeSpec.strikeToSourceRatio()); |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 471 | // Shift the original glyph location in source space to the position of the new |
| 472 | // blob. |
| 473 | pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(), |
| 474 | originShift.y() + pathGlyph.fOrigin.y()); |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 475 | |
| 476 | // TmpPath must be in the same scope as GrShape shape below. |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 477 | SkTLazy<SkPath> tmpPath; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 478 | const SkPath* path = &pathGlyph.fPath; |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 479 | if (!scalePath) { |
| 480 | // Scale can be applied to CTM -- no effects. |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 481 | ctm.preConcat(pathMatrix); |
Robert Phillips | d20d261 | 2018-08-28 10:09:01 -0400 | [diff] [blame] | 482 | } else { |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 483 | // Scale the outline into source space. |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 484 | |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 485 | // Transform the path form the normalized outline to source space. This |
| 486 | // way the CTM will remain the same so it can be used by the effects. |
| 487 | SkPath* sourceOutline = tmpPath.init(); |
| 488 | path->transform(pathMatrix, sourceOutline); |
| 489 | sourceOutline->setIsVolatile(true); |
| 490 | path = sourceOutline; |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Robert Phillips | 46a1338 | 2018-08-23 13:53:01 -0400 | [diff] [blame] | 493 | // TODO: we are losing the mutability of the path here |
| 494 | GrShape shape(*path, paint); |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 495 | target->drawShape(clip, runPaint, ctm, shape); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 496 | } |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 497 | } else { |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 498 | int glyphCount = subRun->fGlyphs.size(); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 499 | if (0 == glyphCount) { |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | bool skipClip = false; |
| 504 | bool submitOp = true; |
| 505 | SkIRect clipRect = SkIRect::MakeEmpty(); |
| 506 | SkRect rtBounds = SkRect::MakeWH(target->width(), target->height()); |
| 507 | SkRRect clipRRect; |
| 508 | GrAA aa; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 509 | // We can clip geometrically if we're not using SDFs or transformed glyphs, |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 510 | // and we have an axis-aligned rectangular non-AA clip |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 511 | if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() && |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 512 | clip.isRRect(rtBounds, &clipRRect, &aa) && |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 513 | clipRRect.isRect() && GrAA::kNo == aa) { |
| 514 | skipClip = true; |
| 515 | // We only need to do clipping work if the subrun isn't contained by the clip |
| 516 | SkRect subRunBounds; |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 517 | this->computeSubRunBounds( |
| 518 | &subRunBounds, *subRun, drawMatrix, drawOrigin, false); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 519 | if (!clipRRect.getBounds().contains(subRunBounds)) { |
| 520 | // If the subrun is completely outside, don't add an op for it |
| 521 | if (!clipRRect.getBounds().intersects(subRunBounds)) { |
| 522 | submitOp = false; |
| 523 | } |
| 524 | else { |
| 525 | clipRRect.getBounds().round(&clipRect); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if (submitOp) { |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 531 | auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 532 | clipRect, paint, filteredColor, props, distanceAdjustTable, |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 533 | target); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 534 | if (op) { |
| 535 | if (skipClip) { |
| 536 | target->addDrawOp(GrNoClip(), std::move(op)); |
| 537 | } |
| 538 | else { |
| 539 | target->addDrawOp(clip, std::move(op)); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 544 | } |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 547 | void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 548 | const SkMatrix& drawMatrix, SkPoint drawOrigin, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 549 | bool needsGlyphTransform) { |
| 550 | // We don't yet position distance field text on the cpu, so we have to map the vertex bounds |
| 551 | // into device space. |
| 552 | // We handle vertex bounds differently for distance field text and bitmap text because |
| 553 | // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs |
| 554 | // from one blob then we are going to pay the price here of mapping the rect for each run. |
| 555 | *outBounds = subRun.vertexBounds(); |
| 556 | if (needsGlyphTransform) { |
| 557 | // Distance field text is positioned with the (X,Y) as part of the glyph position, |
| 558 | // and currently the view matrix is applied on the GPU |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 559 | outBounds->offset(drawOrigin - fInitialOrigin); |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 560 | drawMatrix.mapRect(outBounds); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 561 | } else { |
| 562 | // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in |
| 563 | // device space. |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 564 | SkMatrix boundsMatrix = fInitialMatrixInverse; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 565 | |
| 566 | boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y()); |
| 567 | |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 568 | boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y()); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 569 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 570 | boundsMatrix.postConcat(drawMatrix); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 571 | boundsMatrix.mapRect(outBounds); |
| 572 | |
| 573 | // Due to floating point numerical inaccuracies, we have to round out here |
| 574 | outBounds->roundOut(outBounds); |
| 575 | } |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 576 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 577 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 578 | const GrTextBlob::Key& GrTextBlob::key() const { return fKey; } |
| 579 | size_t GrTextBlob::size() const { return fSize; } |
| 580 | |
| 581 | std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp( |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 582 | int glyphCount, const SkMatrix& drawMatrix, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 583 | SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 584 | const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 585 | GrTextTarget* target) { |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 586 | SubRun* info = fFirstSubRun; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 587 | SkIRect emptyRect = SkIRect::MakeEmpty(); |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 588 | return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 589 | paint, filteredColor, props, distanceAdjustTable, target); |
| 590 | } |
| 591 | |
| 592 | bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const { |
| 593 | if (type == kTransformedSDFT) { |
| 594 | return this->hasPerspective() || fForceWForDistanceFields; |
| 595 | } else if (type == kTransformedMask || type == kTransformedPath) { |
| 596 | return this->hasPerspective(); |
Herb Derby | e9f691d | 2019-12-04 12:11:13 -0500 | [diff] [blame] | 597 | } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 598 | |
| 599 | // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not |
| 600 | // used. |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type, |
| 605 | const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 606 | const SkStrikeSpec& strikeSpec, |
| 607 | GrMaskFormat format) { |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 608 | SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()}; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 609 | bool hasW = this->hasW(type); |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 610 | size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph; |
| 611 | SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize}; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 612 | |
| 613 | sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache); |
| 614 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 615 | SubRun* subRun = fAlloc.make<SubRun>( |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 616 | type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike)); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 617 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 618 | subRun->appendGlyphs(drawables); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 619 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 620 | return subRun; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void GrTextBlob::addSingleMaskFormat( |
| 624 | SubRunType type, |
| 625 | const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 626 | const SkStrikeSpec& strikeSpec, |
| 627 | GrMaskFormat format) { |
| 628 | this->makeSubRun(type, drawables, strikeSpec, format); |
| 629 | } |
| 630 | |
| 631 | void GrTextBlob::addMultiMaskFormat( |
| 632 | SubRunType type, |
| 633 | const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 634 | const SkStrikeSpec& strikeSpec) { |
| 635 | this->setHasBitmap(); |
| 636 | if (drawables.empty()) { return; } |
| 637 | |
| 638 | auto glyphSpan = drawables.get<0>(); |
| 639 | SkGlyph* glyph = glyphSpan[0]; |
| 640 | GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat()); |
| 641 | size_t startIndex = 0; |
| 642 | for (size_t i = 1; i < drawables.size(); i++) { |
| 643 | glyph = glyphSpan[i]; |
| 644 | GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat()); |
| 645 | if (format != nextFormat) { |
| 646 | auto sameFormat = drawables.subspan(startIndex, i - startIndex); |
| 647 | this->addSingleMaskFormat(type, sameFormat, strikeSpec, format); |
| 648 | format = nextFormat; |
| 649 | startIndex = i; |
| 650 | } |
| 651 | } |
| 652 | auto sameFormat = drawables.last(drawables.size() - startIndex); |
| 653 | this->addSingleMaskFormat(type, sameFormat, strikeSpec, format); |
| 654 | } |
| 655 | |
| 656 | void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 657 | const SkStrikeSpec& strikeSpec, |
| 658 | const SkFont& runFont, |
| 659 | SkScalar minScale, |
| 660 | SkScalar maxScale) { |
| 661 | this->setHasDistanceField(); |
| 662 | this->setMinAndMaxScale(minScale, maxScale); |
| 663 | |
| 664 | SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat); |
| 665 | subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias); |
| 666 | subRun->setAntiAliased(runFont.hasSomeAntiAliasing()); |
Herb Derby | e9f691d | 2019-12-04 12:11:13 -0500 | [diff] [blame] | 667 | } |
| 668 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 669 | GrTextBlob::GrTextBlob(size_t allocSize, |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 670 | GrStrikeCache* strikeCache, |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 671 | const SkMatrix& drawMatrix, |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 672 | SkPoint origin, |
| 673 | GrColor color, |
Herb Derby | aebc5f8 | 2019-12-10 14:07:10 -0500 | [diff] [blame] | 674 | SkColor initialLuminance, |
Herb Derby | 00ae959 | 2019-12-03 15:55:56 -0500 | [diff] [blame] | 675 | bool forceWForDistanceFields) |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 676 | : fSize{allocSize} |
Herb Derby | 00ae959 | 2019-12-03 15:55:56 -0500 | [diff] [blame] | 677 | , fStrikeCache{strikeCache} |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 678 | , fInitialMatrix{drawMatrix} |
| 679 | , fInitialMatrixInverse{make_inverse(drawMatrix)} |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 680 | , fInitialOrigin{origin} |
Herb Derby | 00ae959 | 2019-12-03 15:55:56 -0500 | [diff] [blame] | 681 | , fForceWForDistanceFields{forceWForDistanceFields} |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 682 | , fColor{color} |
Herb Derby | aebc5f8 | 2019-12-10 14:07:10 -0500 | [diff] [blame] | 683 | , fInitialLuminance{initialLuminance} |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 684 | , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { } |
Herb Derby | 00ae959 | 2019-12-03 15:55:56 -0500 | [diff] [blame] | 685 | |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 686 | void GrTextBlob::insertSubRun(SubRun* subRun) { |
| 687 | if (fFirstSubRun == nullptr) { |
| 688 | fFirstSubRun = subRun; |
| 689 | fLastSubRun = subRun; |
| 690 | } else { |
| 691 | fLastSubRun->fNextSubRun = subRun; |
| 692 | fLastSubRun = subRun; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp( |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 697 | SubRun& info, int glyphCount, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 698 | const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 699 | const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props, |
| 700 | const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) { |
| 701 | GrMaskFormat format = info.maskFormat(); |
| 702 | |
| 703 | GrPaint grPaint; |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 704 | target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 705 | std::unique_ptr<GrAtlasTextOp> op; |
| 706 | if (info.drawAsDistanceFields()) { |
| 707 | // TODO: Can we be even smarter based on the dest transfer function? |
| 708 | op = GrAtlasTextOp::MakeDistanceField( |
| 709 | target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable, |
| 710 | target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint), |
| 711 | props, info.isAntiAliased(), info.hasUseLCDText()); |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 712 | } else { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 713 | op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount, |
| 714 | info.needsTransform()); |
| 715 | } |
| 716 | GrAtlasTextOp::Geometry& geometry = op->geometry(); |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 717 | geometry.fDrawMatrix = drawMatrix; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 718 | geometry.fClipRect = clipRect; |
| 719 | geometry.fBlob = SkRef(this); |
| 720 | geometry.fSubRunPtr = &info; |
| 721 | geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor; |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 722 | geometry.fDrawOrigin = drawOrigin; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 723 | op->init(); |
| 724 | return op; |
| 725 | } |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 726 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 727 | void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 728 | const SkStrikeSpec& strikeSpec) { |
| 729 | this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec); |
| 730 | } |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 731 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 732 | void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 733 | const SkFont& runFont, |
| 734 | const SkStrikeSpec& strikeSpec) { |
| 735 | this->setHasBitmap(); |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 736 | SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec); |
| 737 | subRun->setAntiAliased(runFont.hasSomeAntiAliasing()); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 738 | for (auto [variant, pos] : drawables) { |
Herb Derby | cb71889 | 2019-12-07 00:07:42 -0500 | [diff] [blame] | 739 | subRun->fPaths.emplace_back(*variant.path(), pos); |
Herb Derby | eba195f | 2019-12-03 16:44:47 -0500 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 743 | void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 744 | const SkStrikeSpec& strikeSpec, |
| 745 | const SkFont& runFont, |
| 746 | SkScalar minScale, |
| 747 | SkScalar maxScale) { |
| 748 | this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 749 | } |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 750 | |
| 751 | void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables, |
| 752 | const SkStrikeSpec& strikeSpec) { |
| 753 | this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec); |
| 754 | } |
| 755 | |
| 756 | // -- GrTextBlob::VertexRegenerator ---------------------------------------------------------------- |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 757 | static void regen_positions(char* vertex, size_t vertexStride, SkVector translation) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 758 | SkPoint* point = reinterpret_cast<SkPoint*>(vertex); |
| 759 | for (int i = 0; i < 4; ++i) { |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 760 | *point += translation; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 761 | point = SkTAddOffset<SkPoint>(point, vertexStride); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | static void regen_colors(char* vertex, size_t vertexStride, GrColor color) { |
| 766 | // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color |
| 767 | // vertices, hence vertexStride - sizeof(SkIPoint16) |
| 768 | size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor); |
| 769 | GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset); |
| 770 | for (int i = 0; i < 4; ++i) { |
| 771 | *vcolor = color; |
| 772 | vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph, |
| 777 | bool useDistanceFields) { |
| 778 | // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color |
| 779 | // vertices, hence vertexStride - sizeof(SkIPoint16) |
| 780 | size_t texCoordOffset = vertexStride - sizeof(SkIPoint16); |
| 781 | |
| 782 | uint16_t u0, v0, u1, v1; |
| 783 | SkASSERT(glyph); |
| 784 | int width = glyph->fBounds.width(); |
| 785 | int height = glyph->fBounds.height(); |
| 786 | |
| 787 | if (useDistanceFields) { |
| 788 | u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset; |
| 789 | v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset; |
| 790 | u1 = u0 + width - 2 * SK_DistanceFieldInset; |
| 791 | v1 = v0 + height - 2 * SK_DistanceFieldInset; |
| 792 | } else { |
| 793 | u0 = glyph->fAtlasLocation.fX; |
| 794 | v0 = glyph->fAtlasLocation.fY; |
| 795 | u1 = u0 + width; |
| 796 | v1 = v0 + height; |
| 797 | } |
| 798 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 799 | uint32_t pageIndex = glyph->pageIndex(); |
| 800 | SkASSERT(pageIndex < 4); |
| 801 | uint16_t uBit = (pageIndex >> 1) & 0x1; |
| 802 | uint16_t vBit = pageIndex & 0x1; |
| 803 | u0 <<= 1; |
| 804 | u0 |= uBit; |
| 805 | v0 <<= 1; |
| 806 | v0 |= vBit; |
| 807 | u1 <<= 1; |
| 808 | u1 |= uBit; |
| 809 | v1 <<= 1; |
| 810 | v1 |= vBit; |
| 811 | |
| 812 | uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset); |
| 813 | textureCoords[0] = u0; |
| 814 | textureCoords[1] = v0; |
| 815 | textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride); |
| 816 | textureCoords[0] = u0; |
| 817 | textureCoords[1] = v1; |
| 818 | textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride); |
| 819 | textureCoords[0] = u1; |
| 820 | textureCoords[1] = v0; |
| 821 | textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride); |
| 822 | textureCoords[0] = u1; |
| 823 | textureCoords[1] = v1; |
| 824 | |
| 825 | #ifdef DISPLAY_PAGE_INDEX |
| 826 | // Enable this to visualize the page from which each glyph is being drawn. |
| 827 | // Green Red Magenta Cyan -> 0 1 2 3; Black -> error |
| 828 | GrColor hackColor; |
| 829 | switch (pageIndex) { |
| 830 | case 0: |
| 831 | hackColor = GrColorPackRGBA(0, 255, 0, 255); |
| 832 | break; |
| 833 | case 1: |
| 834 | hackColor = GrColorPackRGBA(255, 0, 0, 255);; |
| 835 | break; |
| 836 | case 2: |
| 837 | hackColor = GrColorPackRGBA(255, 0, 255, 255); |
| 838 | break; |
| 839 | case 3: |
| 840 | hackColor = GrColorPackRGBA(0, 255, 255, 255); |
| 841 | break; |
| 842 | default: |
| 843 | hackColor = GrColorPackRGBA(0, 0, 0, 255); |
| 844 | break; |
| 845 | } |
| 846 | regen_colors(vertex, vertexStride, hackColor); |
| 847 | #endif |
| 848 | } |
| 849 | |
| 850 | GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 851 | GrTextBlob::SubRun* subRun, |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 852 | const SkMatrix& drawMatrix, |
| 853 | SkPoint drawOrigin, |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 854 | GrColor color, |
| 855 | GrDeferredUploadTarget* uploadTarget, |
| 856 | GrStrikeCache* grStrikeCache, |
| 857 | GrAtlasManager* fullAtlasManager) |
| 858 | : fResourceProvider(resourceProvider) |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 859 | , fDrawMatrix(drawMatrix) |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 860 | , fUploadTarget(uploadTarget) |
| 861 | , fGrStrikeCache(grStrikeCache) |
| 862 | , fFullAtlasManager(fullAtlasManager) |
| 863 | , fSubRun(subRun) |
| 864 | , fColor(color) { |
| 865 | // Compute translation if any |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 866 | fDrawTranslation = fSubRun->computeTranslation(fDrawMatrix, drawOrigin); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 867 | |
| 868 | // Because the GrStrikeCache may evict the strike a blob depends on using for |
| 869 | // generating its texture coords, we have to track whether or not the strike has |
| 870 | // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is |
| 871 | // otherwise we have to get the new strike, and use that to get the correct glyphs. |
| 872 | // Because we do not have the packed ids, and thus can't look up our glyphs in the |
| 873 | // new strike, we instead keep our ref to the old strike and use the packed ids from |
| 874 | // it. These ids will still be valid as long as we hold the ref. When we are done |
| 875 | // updating our cache of the GrGlyph*s, we drop our ref on the old strike |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 876 | fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned(); |
| 877 | fActions.regenStrike = fSubRun->strike()->isAbandoned(); |
| 878 | fActions.regenColor = kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color; |
| 879 | fActions.regenPositions = fDrawTranslation.x() != 0.f || fDrawTranslation.y() != 0.f; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 880 | } |
| 881 | |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 882 | bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result) { |
| 883 | SkASSERT(!fActions.regenStrike || fActions.regenTextureCoordinates); |
| 884 | if (fActions.regenTextureCoordinates) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 885 | fSubRun->resetBulkUseToken(); |
| 886 | |
| 887 | const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec(); |
| 888 | |
| 889 | if (!fMetricsAndImages.isValid() |
| 890 | || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) { |
| 891 | fMetricsAndImages.init(strikeSpec); |
| 892 | } |
| 893 | |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 894 | if (fActions.regenStrike) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 895 | // Take the glyphs from the old strike, and translate them a new strike. |
| 896 | sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache); |
| 897 | |
| 898 | // Start this batch at the start of the subRun plus any glyphs that were previously |
| 899 | // processed. |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 900 | SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - fCurrGlyph); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 901 | |
| 902 | // Convert old glyphs to newStrike. |
| 903 | for (auto& glyph : glyphs) { |
| 904 | SkPackedGlyphID id = glyph->fPackedID; |
| 905 | glyph = newStrike->getGlyph(id, fMetricsAndImages.get()); |
| 906 | SkASSERT(id == glyph->fPackedID); |
| 907 | } |
| 908 | |
| 909 | fSubRun->setStrike(newStrike); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | sk_sp<GrTextStrike> grStrike = fSubRun->refStrike(); |
| 914 | bool hasW = fSubRun->hasW(); |
| 915 | auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW); |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 916 | char* currVertex = fSubRun->fVertexData.data() + fCurrGlyph * kVerticesPerGlyph * vertexStride; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 917 | result->fFirstVertex = currVertex; |
| 918 | |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 919 | for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->fGlyphs.size(); glyphIdx++) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 920 | GrGlyph* glyph = nullptr; |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 921 | if (fActions.regenTextureCoordinates) { |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 922 | glyph = fSubRun->fGlyphs[glyphIdx]; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 923 | SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat()); |
| 924 | |
| 925 | if (!fFullAtlasManager->hasGlyph(glyph)) { |
| 926 | GrDrawOpAtlas::ErrorCode code; |
| 927 | code = grStrike->addGlyphToAtlas(fResourceProvider, fUploadTarget, fGrStrikeCache, |
| 928 | fFullAtlasManager, glyph, |
| 929 | fMetricsAndImages.get(), fSubRun->maskFormat(), |
| 930 | fSubRun->needsTransform()); |
| 931 | if (GrDrawOpAtlas::ErrorCode::kError == code) { |
| 932 | // Something horrible has happened - drop the op |
| 933 | return false; |
| 934 | } |
| 935 | else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) { |
| 936 | fBrokenRun = glyphIdx > 0; |
| 937 | result->fFinished = false; |
| 938 | return true; |
| 939 | } |
| 940 | } |
| 941 | auto tokenTracker = fUploadTarget->tokenTracker(); |
| 942 | fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph, |
| 943 | tokenTracker->nextDrawToken()); |
| 944 | } |
| 945 | |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 946 | if (fActions.regenPositions) { |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 947 | regen_positions(currVertex, vertexStride, fDrawTranslation); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 948 | } |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 949 | if (fActions.regenColor) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 950 | regen_colors(currVertex, vertexStride, fColor); |
| 951 | } |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 952 | if (fActions.regenTextureCoordinates) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 953 | regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields()); |
| 954 | } |
| 955 | |
| 956 | currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph; |
| 957 | ++result->fGlyphsRegenerated; |
| 958 | ++fCurrGlyph; |
| 959 | } |
| 960 | |
| 961 | // We may have changed the color so update it here |
| 962 | fSubRun->setColor(fColor); |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 963 | if (fActions.regenTextureCoordinates) { |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 964 | fSubRun->setAtlasGeneration(fBrokenRun |
| 965 | ? GrDrawOpAtlas::kInvalidAtlasGeneration |
| 966 | : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat())); |
| 967 | } else { |
| 968 | // For the non-texCoords case we need to ensure that we update the associated use tokens |
| 969 | fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(), |
| 970 | fUploadTarget->tokenTracker()->nextDrawToken(), |
| 971 | fSubRun->maskFormat()); |
| 972 | } |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) { |
| 977 | uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()); |
| 978 | // If regenerate() is called multiple times then the atlas gen may have changed. So we check |
| 979 | // this each time. |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 980 | fActions.regenTextureCoordinates |= fSubRun->atlasGeneration() != currentAtlasGen; |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 981 | |
Herb Derby | 7363021 | 2019-12-13 16:29:14 -0500 | [diff] [blame^] | 982 | if (fActions.regenStrike |
| 983 | |fActions.regenTextureCoordinates |
| 984 | |fActions.regenColor |
| 985 | |fActions.regenPositions) { |
| 986 | return this->doRegen(result); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 987 | } else { |
| 988 | bool hasW = fSubRun->hasW(); |
| 989 | auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW); |
| 990 | result->fFinished = true; |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 991 | result->fGlyphsRegenerated = fSubRun->fGlyphs.size() - fCurrGlyph; |
| 992 | result->fFirstVertex = fSubRun->fVertexData.data() + |
| 993 | fCurrGlyph * kVerticesPerGlyph * vertexStride; |
| 994 | fCurrGlyph = fSubRun->fGlyphs.size(); |
Herb Derby | a904764 | 2019-12-06 12:12:11 -0500 | [diff] [blame] | 995 | |
| 996 | // set use tokens for all of the glyphs in our subrun. This is only valid if we |
| 997 | // have a valid atlas generation |
| 998 | fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(), |
| 999 | fUploadTarget->tokenTracker()->nextDrawToken(), |
| 1000 | fSubRun->maskFormat()); |
| 1001 | return true; |
| 1002 | } |
| 1003 | SK_ABORT("Should not get here"); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |