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