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