blob: 965acbd90bc433bbd5192d0cca585b6d891cfffe [file] [log] [blame]
joshualitt374b2f72015-07-21 08:05:03 -07001/*
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
Herb Derby86240592018-05-24 16:12:31 -04008#ifndef GrTextBlob_DEFINED
9#define GrTextBlob_DEFINED
joshualitt374b2f72015-07-21 08:05:03 -070010
joshualitt259fbf12015-07-21 11:39:34 -070011#include "GrColor.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050012#include "GrDrawOpAtlas.h"
Herb Derby081e6f32019-01-16 13:46:02 -050013#include "GrStrikeCache.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040014#include "GrTextTarget.h"
Herb Derbydc214c22018-11-08 13:31:39 -050015#include "text/GrTextContext.h"
joshualitt374b2f72015-07-21 08:05:03 -070016#include "SkDescriptor.h"
Mike Reed80747ef2018-01-23 15:29:32 -050017#include "SkMaskFilterBase.h"
mtklein4e976072016-08-08 09:06:27 -070018#include "SkOpts.h"
bsalomon8b6fa5e2016-05-19 16:23:47 -070019#include "SkPathEffect.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050020#include "SkPoint3.h"
Mike Reed274218e2018-01-08 15:05:02 -050021#include "SkRectPriv.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040022#include "SkStrikeCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070023#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070024#include "SkTInternalLList.h"
25
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026class GrAtlasManager;
joshualitt2e2202e2015-12-10 11:22:08 -080027struct GrDistanceFieldAdjustTable;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050028struct GrGlyph;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050029
joshualitt2e2202e2015-12-10 11:22:08 -080030class SkTextBlob;
31class SkTextBlobRunIterator;
32
Herb Derby26cbe512018-05-24 14:39:01 -040033// With this flag enabled, the GrTextContext will, as a sanity check, regenerate every blob
joshualitt259fbf12015-07-21 11:39:34 -070034// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080035#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070036
joshualitt374b2f72015-07-21 08:05:03 -070037/*
Herb Derby86240592018-05-24 16:12:31 -040038 * A GrTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
joshualitt374b2f72015-07-21 08:05:03 -070039 * on the GPU. These are initially created with valid positions and colors, but invalid
Herb Derby86240592018-05-24 16:12:31 -040040 * texture coordinates. The GrTextBlob itself has a few Blob-wide properties, and also
joshualitt374b2f72015-07-21 08:05:03 -070041 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
42 * reordered.
43 *
Herb Derby86240592018-05-24 16:12:31 -040044 * The only thing(aside from a memcopy) required to flush a GrTextBlob is to ensure that
joshualitt374b2f72015-07-21 08:05:03 -070045 * the GrAtlas will not evict anything the Blob needs.
46 *
47 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070048 *
49 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070050 */
Herb Derbyac962c92019-03-08 12:31:47 -050051class GrTextBlob : public SkNVRefCnt<GrTextBlob>, public SkGlyphRunPainterInterface {
Herb Derbydc214c22018-11-08 13:31:39 -050052 struct Run;
joshualitt2e2202e2015-12-10 11:22:08 -080053public:
Herb Derby86240592018-05-24 16:12:31 -040054 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrTextBlob);
joshualitt374b2f72015-07-21 08:05:03 -070055
Brian Salomon18923f92017-11-06 16:26:02 -050056 class VertexRegenerator;
57
Herb Derbya00da612019-03-04 17:10:01 -050058 void generateFromGlyphRunList(const GrShaderCaps& shaderCaps,
Herb Derbydc214c22018-11-08 13:31:39 -050059 const GrTextContext::Options& options,
60 const SkPaint& paint,
Herb Derbydc214c22018-11-08 13:31:39 -050061 SkScalerContextFlags scalerContextFlags,
62 const SkMatrix& viewMatrix,
63 const SkSurfaceProps& props,
64 const SkGlyphRunList& glyphRunList,
65 SkGlyphRunListPainter* glyphPainter);
66
Herb Derbya00da612019-03-04 17:10:01 -050067 static sk_sp<GrTextBlob> Make(
68 int glyphCount,
69 int runCount,
70 GrColor color,
71 GrStrikeCache* strikeCache);
joshualitt323c2eb2016-01-20 06:48:47 -080072
Brian Salomon5c6ac642017-12-19 11:09:32 -050073 /**
74 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
75 * If we ever change that then the key must contain the perspectiveness when there are distance
76 * fields as perspective distance field use 3 component vertex positions and non-perspective
77 * uses 2.
78 */
joshualitt323c2eb2016-01-20 06:48:47 -080079 struct Key {
80 Key() {
81 sk_bzero(this, sizeof(Key));
82 }
83 uint32_t fUniqueID;
84 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
85 // Each color is assigned to on of a fixed number of buckets based on its
86 // luminance. For each luminance bucket there is a "canonical color" that
87 // represents the bucket. This functionality is currently only supported for A8
88 SkColor fCanonicalColor;
89 SkPaint::Style fStyle;
90 SkPixelGeometry fPixelGeometry;
91 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070092 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080093
94 bool operator==(const Key& other) const {
95 return 0 == memcmp(this, &other, sizeof(Key));
96 }
97 };
98
Herb Derby86240592018-05-24 16:12:31 -040099 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -0500100 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -0800101 const SkPaint& paint) {
102 fKey = key;
103 if (key.fHasBlur) {
104 fBlurRec = blurRec;
105 }
106 if (key.fStyle != SkPaint::kFill_Style) {
107 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
108 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
109 fStrokeInfo.fJoin = paint.getStrokeJoin();
110 }
111 }
112
Herb Derby86240592018-05-24 16:12:31 -0400113 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -0800114 return blob.fKey;
115 }
116
117 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700118 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800119 }
120
121 void operator delete(void* p) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400122 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800123 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400124
joshualitt323c2eb2016-01-20 06:48:47 -0800125 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400126 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800127 return sk_malloc_throw(0);
128 }
129
130 void* operator new(size_t, void* p) { return p; }
joshualitt323c2eb2016-01-20 06:48:47 -0800131
132 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
133 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
134 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
135 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
136
Herb Derby2bb343c2018-11-08 15:03:48 -0500137 int runCountLimit() const { return fRunCountLimit; }
joshualittddd22d82016-02-16 06:47:52 -0800138
Herb Derby2bb343c2018-11-08 15:03:48 -0500139 Run* pushBackRun() {
140 SkASSERT(fRunCount < fRunCountLimit);
Herb Derby6dff60e2018-11-12 15:45:49 -0500141
142 // If there is more run, then connect up the subruns.
Herb Derby2bb343c2018-11-08 15:03:48 -0500143 if (fRunCount > 0) {
Herb Derby69ff8952018-11-12 11:39:12 -0500144 SubRun& newRun = fRuns[fRunCount].fSubRunInfo.back();
145 SubRun& lastRun = fRuns[fRunCount - 1].fSubRunInfo.back();
joshualitt323c2eb2016-01-20 06:48:47 -0800146 newRun.setAsSuccessor(lastRun);
147 }
joshualitt323c2eb2016-01-20 06:48:47 -0800148
Herb Derby2bb343c2018-11-08 15:03:48 -0500149 fRunCount++;
Herb Derbyc72594a2019-03-05 17:39:38 -0500150 return this->currentRun();
Jim Van Verth89737de2018-02-06 21:30:20 +0000151 }
152
Herb Derby78540b92019-02-19 14:42:24 -0500153 void setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
joshualitt323c2eb2016-01-20 06:48:47 -0800154 // we init fMaxMinScale and fMinMaxScale in the constructor
Herb Derby78540b92019-02-19 14:42:24 -0500155 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
156 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
joshualitt323c2eb2016-01-20 06:48:47 -0800157 }
158
Jim Van Verthb515ae72018-05-23 16:44:55 -0400159 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800160 switch (maskFormat) {
161 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400162 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800163 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400164 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800165 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400166 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800167 return kLCDTextVASize;
168 }
169 }
170
Herb Derby0edb2142018-10-16 17:04:11 -0400171 bool mustRegenerate(const SkPaint&, bool, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800172 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
173
Herb Derbyc1b482c2018-08-09 15:02:27 -0400174 void flush(GrTextTarget*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500175 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400176 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400177 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800178
joshualitt8e0ef292016-02-19 14:13:03 -0800179 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400180 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
181 bool needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800182 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
183 // into device space.
184 // We handle vertex bounds differently for distance field text and bitmap text because
185 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
186 // from one blob then we are going to pay the price here of mapping the rect for each run.
187 const Run& run = fRuns[runIndex];
Herb Derby69ff8952018-11-12 11:39:12 -0500188 const SubRun& subRun = run.fSubRunInfo[subRunIndex];
joshualittbc811112016-02-11 12:42:02 -0800189 *outBounds = subRun.vertexBounds();
Jim Van Verth70276912018-06-01 13:46:46 -0400190 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800191 // Distance field text is positioned with the (X,Y) as part of the glyph position,
192 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800193 outBounds->offset(x - fInitialX, y - fInitialY);
194 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800195 } else {
196 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
197 // device space.
198 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
199
200 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
201
joshualitt8e0ef292016-02-19 14:13:03 -0800202 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800203
joshualitt8e0ef292016-02-19 14:13:03 -0800204 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800205 boundsMatrix.mapRect(outBounds);
206
207 // Due to floating point numerical inaccuracies, we have to round out here
208 outBounds->roundOut(outBounds);
209 }
210 }
211
joshualitt323c2eb2016-01-20 06:48:47 -0800212 // position + local coord
213 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400214 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800215 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500216 static const size_t kGrayTextDFPerspectiveVASize =
217 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800218 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500219 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800220 static const int kVerticesPerGlyph = 4;
221
Herb Derby86240592018-05-24 16:12:31 -0400222 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800223
224 // The color here is the GrPaint color, and it is used to determine whether we
225 // have to regenerate LCD text blobs.
226 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400227 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
228 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400229 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800230 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800231 }
232
joshualitt7481e752016-01-22 06:08:48 -0800233 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
234 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800235 }
236
joshualitt92303772016-02-10 11:55:52 -0800237 const Key& key() const { return fKey; }
238
Herb Derbyb12175f2018-05-23 16:38:09 -0400239 size_t size() const { return fSize; }
240
Herb Derbyac962c92019-03-08 12:31:47 -0500241 ~GrTextBlob() override {
Herb Derby2bb343c2018-11-08 15:03:48 -0500242 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt92303772016-02-10 11:55:52 -0800243 fRuns[i].~Run();
244 }
245 }
246
joshualittbc811112016-02-11 12:42:02 -0800247 ////////////////////////////////////////////////////////////////////////////////////////////////
248 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400249 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400250 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Osmancf860852018-10-31 14:04:39 -0400251 const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400252 const SkSurfaceProps&, const GrDistanceFieldAdjustTable*,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400253 GrTextTarget*);
joshualittbc811112016-02-11 12:42:02 -0800254
joshualitt323c2eb2016-01-20 06:48:47 -0800255private:
Herb Derbya00da612019-03-04 17:10:01 -0500256 GrTextBlob(GrStrikeCache* strikeCache) : fStrikeCache{strikeCache} { }
joshualitt92303772016-02-10 11:55:52 -0800257
joshualitt8e0ef292016-02-19 14:13:03 -0800258 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800259 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
260 // these numbers. When blobs are reused with new matrices, we need to return to model space so
261 // we can update the vertex bounds appropriately.
262 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800263 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800264 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
265 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800266 }
joshualitt8e0ef292016-02-19 14:13:03 -0800267 fInitialX = x;
268 fInitialY = y;
269
270 // make sure all initial subruns have the correct VM and X/Y applied
Herb Derby2bb343c2018-11-08 15:03:48 -0500271 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800272 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
273 }
joshualitt7481e752016-01-22 06:08:48 -0800274 }
275
Herb Derby69ff8952018-11-12 11:39:12 -0500276 class SubRun {
277 public:
Herb Derby5424a5e2018-11-14 12:04:38 -0500278 SubRun(Run* run, const SkAutoDescriptor& desc, GrColor color)
279 : fColor{color}
280 , fRun{run}
Herb Derby317adf72018-11-16 17:29:29 -0500281 , fDesc{desc} {}
Herb Derby69ff8952018-11-12 11:39:12 -0500282
Herb Derby5424a5e2018-11-14 12:04:38 -0500283 // When used with emplace_back, this constructs a SubRun from the last SubRun in an array.
284 //SubRun(SkSTArray<1, SubRun>* subRunList)
285 // : fColor{subRunList->fromBack(1).fColor} { }
286
Herb Derby438ea542018-12-19 18:25:11 -0500287 void appendGlyph(GrGlyph* glyph, SkRect dstRect);
Herb Derby96a17092018-11-12 12:45:21 -0500288
Herb Derby69ff8952018-11-12 11:39:12 -0500289 // TODO when this object is more internal, drop the privacy
290 void resetBulkUseToken() { fBulkUseToken.reset(); }
291 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
292 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
293 GrTextStrike* strike() const { return fStrike.get(); }
294 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
295
296 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
297 uint64_t atlasGeneration() const { return fAtlasGeneration; }
298
299 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
300 size_t vertexStartIndex() const { return fVertexStartIndex; }
301 size_t vertexEndIndex() const { return fVertexEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500302
303 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
304 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
305 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500306 void setColor(GrColor color) { fColor = color; }
307 GrColor color() const { return fColor; }
308 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
309 GrMaskFormat maskFormat() const { return fMaskFormat; }
310
311 void setAsSuccessor(const SubRun& prev) {
312 fGlyphStartIndex = prev.glyphEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500313 fGlyphEndIndex = fGlyphStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500314
315 fVertexStartIndex = prev.vertexEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500316 fVertexEndIndex = fVertexStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500317
318 // copy over viewmatrix settings
319 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
320 }
321
322 const SkRect& vertexBounds() const { return fVertexBounds; }
323 void joinGlyphBounds(const SkRect& glyphBounds) {
324 fVertexBounds.joinNonEmptyArg(glyphBounds);
325 }
326
327 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
328 fCurrentViewMatrix = viewMatrix;
329 fX = x;
330 fY = y;
331 }
332
333 // This function assumes the translation will be applied before it is called again
334 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
335 SkScalar* transX, SkScalar* transY);
336
337 // df properties
herbc5df7cb2018-11-15 17:08:26 -0500338 void setDrawAsDistanceFields() { fFlags.drawAsSdf = true; }
339 bool drawAsDistanceFields() const { return fFlags.drawAsSdf; }
340 void setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
341 bool hasUseLCDText() const { return fFlags.useLCDText; }
342 void setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
343 bool isAntiAliased() const { return fFlags.antiAliased; }
344 void setHasWCoord(bool hasW) { fFlags.hasWCoord = hasW; }
345 bool hasWCoord() const { return fFlags.hasWCoord; }
346 void setNeedsTransform(bool needsTransform) { fFlags.needsTransform = needsTransform; }
347 bool needsTransform() const { return fFlags.needsTransform; }
Herb Derby5f7b0142018-12-14 16:47:45 -0500348 void setFallback() { fFlags.argbFallback = true; }
349 bool isFallback() { return fFlags.argbFallback; }
Herb Derby317adf72018-11-16 17:29:29 -0500350
351 const SkDescriptor* desc() const { return fDesc.getDesc(); }
Herb Derby69ff8952018-11-12 11:39:12 -0500352
353 private:
Herb Derby69ff8952018-11-12 11:39:12 -0500354 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
355 sk_sp<GrTextStrike> fStrike;
356 SkMatrix fCurrentViewMatrix;
Herb Derby6dff60e2018-11-12 15:45:49 -0500357 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
358 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
359 size_t fVertexStartIndex{0};
360 size_t fVertexEndIndex{0};
361 uint32_t fGlyphStartIndex{0};
362 uint32_t fGlyphEndIndex{0};
Herb Derby69ff8952018-11-12 11:39:12 -0500363 SkScalar fX;
364 SkScalar fY;
Herb Derby6dff60e2018-11-12 15:45:49 -0500365 GrColor fColor{GrColor_ILLEGAL};
366 GrMaskFormat fMaskFormat{kA8_GrMaskFormat};
herbc5df7cb2018-11-15 17:08:26 -0500367 struct {
368 bool drawAsSdf:1;
369 bool useLCDText:1;
370 bool antiAliased:1;
371 bool hasWCoord:1;
372 bool needsTransform:1;
Herb Derby317adf72018-11-16 17:29:29 -0500373 bool argbFallback:1;
374 } fFlags{false, false, false, false, false, false};
Herb Derbyf7d5d742018-11-16 13:24:32 -0500375 Run* const fRun;
Herb Derby317adf72018-11-16 17:29:29 -0500376 const SkAutoDescriptor& fDesc;
Herb Derby69ff8952018-11-12 11:39:12 -0500377 }; // SubRunInfo
378
joshualitt374b2f72015-07-21 08:05:03 -0700379 /*
380 * Each Run inside of the blob can have its texture coordinates regenerated if required.
381 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
382 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
383 * this at a more fine grained level, but its not clear if this is worth it, as evictions
384 * should be fairly rare.
385 *
386 * One additional point, each run can contain glyphs with any of the three mask formats.
387 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
388 * a new subrun each time the mask format changes in a run. In theory, a run can have as
389 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
390 * practice, the vast majority of runs have only a single subrun.
391 *
Herb Derby26cbe512018-05-24 14:39:01 -0400392 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500393 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
394 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700395 * can flush in this manner, so we always allocate vertices for the run, regardless of
396 * whether or not it is too large. The benefit of this strategy is that we can always reuse
397 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500398 * glyphs, however, it's not clear if this is a win because we'd still have to either go to the
joshualitt374b2f72015-07-21 08:05:03 -0700399 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
400 * would greatly increase the memory of these cached items.
401 */
402 struct Run {
Herb Derby5424a5e2018-11-14 12:04:38 -0500403 explicit Run(GrTextBlob* blob, GrColor color)
404 : fBlob{blob}, fColor{color} {
joshualitt374b2f72015-07-21 08:05:03 -0700405 // To ensure we always have one subrun, we push back a fresh run here
Herb Derby5424a5e2018-11-14 12:04:38 -0500406 fSubRunInfo.emplace_back(this, fDescriptor, color);
joshualitt374b2f72015-07-21 08:05:03 -0700407 }
joshualitt374b2f72015-07-21 08:05:03 -0700408
Herb Derby2bb343c2018-11-08 15:03:48 -0500409 // sets the last subrun of runIndex to use w values
410 void setSubRunHasW(bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500411 SubRun& subRun = this->fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500412 subRun.setHasWCoord(hasWCoord);
413 }
414
415 // inits the override descriptor on the current run. All following subruns must use this
416 // descriptor
Herb Derby317adf72018-11-16 17:29:29 -0500417 SubRun* initARGBFallback() {
418 fARGBFallbackDescriptor.reset(new SkAutoDescriptor{});
Herb Derby2bb343c2018-11-08 15:03:48 -0500419 // Push back a new subrun to fill and set the override descriptor
Herb Derby5424a5e2018-11-14 12:04:38 -0500420 SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor, fColor);
Herb Derby317adf72018-11-16 17:29:29 -0500421 subRun->setMaskFormat(kARGB_GrMaskFormat);
422 subRun->setFallback();
423 return subRun;
Herb Derby2bb343c2018-11-08 15:03:48 -0500424 }
425
426 // Appends a glyph to the blob as a path only.
427 void appendPathGlyph(
428 const SkPath& path, SkPoint position, SkScalar scale, bool preTransformed);
429
Herb Derby438ea542018-12-19 18:25:11 -0500430 // Append a glyph to the sub run taking care to switch the glyph if needed.
431 void switchSubRunIfNeededAndAppendGlyph(GrGlyph* glyph,
432 const sk_sp<GrTextStrike>& strike,
433 const SkRect& destRect,
434 bool needsTransform);
435
436 // Used when the glyph in the cache has the CTM already applied, therefore no transform
437 // is needed during rendering.
438 void appendDeviceSpaceGlyph(const sk_sp<GrTextStrike>& strike,
439 const SkGlyph& skGlyph,
440 SkPoint origin);
441
442 // The glyph is oriented upright in the cache and needs to be transformed onto the screen.
443 void appendSourceSpaceGlyph(const sk_sp<GrTextStrike>& strike,
444 const SkGlyph& skGlyph,
445 SkPoint origin,
446 SkScalar textScale);
Herb Derby2bb343c2018-11-08 15:03:48 -0500447
Herb Derbya4c64872019-02-13 11:46:19 -0500448 void setupFont(const SkStrikeSpec& strikeSpec);
Herb Derby2bb343c2018-11-08 15:03:48 -0500449
Mike Reed48b958b2018-12-03 13:09:02 -0500450 void setRunFontAntiAlias(bool aa) {
451 fAntiAlias = aa;
Herb Derby2bb343c2018-11-08 15:03:48 -0500452 }
453
454 // sets the last subrun of runIndex to use distance field text
455 void setSubRunHasDistanceFields(bool hasLCD, bool isAntiAlias, bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500456 SubRun& subRun = fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500457 subRun.setUseLCDText(hasLCD);
458 subRun.setAntiAliased(isAntiAlias);
459 subRun.setDrawAsDistanceFields();
460 subRun.setHasWCoord(hasWCoord);
461 }
462
Herb Derby5424a5e2018-11-14 12:04:38 -0500463 SubRun* pushBackSubRun(const SkAutoDescriptor& desc, GrColor color) {
joshualitt374b2f72015-07-21 08:05:03 -0700464 // Forward glyph / vertex information to seed the new sub run
Herb Derby5424a5e2018-11-14 12:04:38 -0500465 SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc, color);
466
Herb Derby69ff8952018-11-12 11:39:12 -0500467 const SubRun& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700468
Herb Derby5424a5e2018-11-14 12:04:38 -0500469 // Forward glyph / vertex information to seed the new sub run
joshualitt18b072d2015-12-07 12:26:12 -0800470 newSubRun.setAsSuccessor(prevSubRun);
Herb Derby317adf72018-11-16 17:29:29 -0500471 return &newSubRun;
joshualitt374b2f72015-07-21 08:05:03 -0700472 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500473
474 // Any glyphs that can't be rendered with the base or override descriptor
475 // are rendered as paths
476 struct PathGlyph {
477 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
478 : fPath(path)
479 , fX(x)
480 , fY(y)
481 , fScale(scale)
482 , fPreTransformed(preXformed) {}
483 SkPath fPath;
484 SkScalar fX;
485 SkScalar fY;
486 SkScalar fScale;
487 bool fPreTransformed;
488 };
489
Herb Derbyf7d5d742018-11-16 13:24:32 -0500490
491 sk_sp<SkTypeface> fTypeface;
492 SkSTArray<1, SubRun> fSubRunInfo;
493 SkAutoDescriptor fDescriptor;
494
495 // Effects from the paint that are used to build a SkScalerContext.
496 sk_sp<SkPathEffect> fPathEffect;
497 sk_sp<SkMaskFilter> fMaskFilter;
498
499 // Distance field text cannot draw coloremoji, and so has to fall back. However,
500 // though the distance field text and the coloremoji may share the same run, they
Herb Derby317adf72018-11-16 17:29:29 -0500501 // will have different descriptors. If fARGBFallbackDescriptor is non-nullptr, then it
Herb Derbyf7d5d742018-11-16 13:24:32 -0500502 // will be used in place of the run's descriptor to regen texture coords
Herb Derby317adf72018-11-16 17:29:29 -0500503 std::unique_ptr<SkAutoDescriptor> fARGBFallbackDescriptor;
Herb Derbyf7d5d742018-11-16 13:24:32 -0500504
Jim Van Verth54d9c882018-02-08 16:14:48 -0500505 SkTArray<PathGlyph> fPathGlyphs;
506
Mike Reed48b958b2018-12-03 13:09:02 -0500507 bool fAntiAlias{false}; // needed mainly for rendering paths
Herb Derbyf7d5d742018-11-16 13:24:32 -0500508 bool fInitialized{false};
509
510 GrTextBlob* const fBlob;
Herb Derby5424a5e2018-11-14 12:04:38 -0500511 GrColor fColor;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400512 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700513
Herb Derbyc72594a2019-03-05 17:39:38 -0500514 std::unique_ptr<GrAtlasTextOp> makeOp(
Herb Derby69ff8952018-11-12 11:39:12 -0500515 const SubRun& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400516 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400517 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps&,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400518 const GrDistanceFieldAdjustTable*, GrTextTarget*);
joshualitt323c2eb2016-01-20 06:48:47 -0800519
Herb Derbyc72594a2019-03-05 17:39:38 -0500520 // currentRun, startRun, and the process* calls are all used by the SkGlyphRunPainter, and
521 // live in SkGlyphRunPainter.cpp file.
522 Run* currentRun();
Herb Derbyc72594a2019-03-05 17:39:38 -0500523
Herb Derbyac962c92019-03-08 12:31:47 -0500524 void startRun(const SkGlyphRun& glyphRun, bool useSDFT) override;
Herb Derbyc72594a2019-03-05 17:39:38 -0500525
Herb Derbyac962c92019-03-08 12:31:47 -0500526 void processDeviceMasks(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
527 SkStrikeInterface* strike) override;
Herb Derbyc72594a2019-03-05 17:39:38 -0500528
Herb Derbyac962c92019-03-08 12:31:47 -0500529 void processSourcePaths(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> paths,
530 SkStrikeInterface* strike, SkScalar cacheToSourceScale) override;
531
532 void processDevicePaths(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> paths) override;
533
534 void processSourceSDFT(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
Herb Derbyc72594a2019-03-05 17:39:38 -0500535 SkStrikeInterface* strike,
536 const SkFont& runFont,
Herb Derbyac962c92019-03-08 12:31:47 -0500537 SkScalar cacheToSourceScale,
Herb Derbyc72594a2019-03-05 17:39:38 -0500538 SkScalar minScale,
539 SkScalar maxScale,
Herb Derbyac962c92019-03-08 12:31:47 -0500540 bool hasWCoord) override;
Herb Derbyc72594a2019-03-05 17:39:38 -0500541
Herb Derbyac962c92019-03-08 12:31:47 -0500542 void processSourceFallback(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
Herb Derbyc72594a2019-03-05 17:39:38 -0500543 SkStrikeInterface* strike,
Herb Derbyac962c92019-03-08 12:31:47 -0500544 SkScalar cacheToSourceScale,
545 bool hasW) override;
Herb Derbyc72594a2019-03-05 17:39:38 -0500546
Herb Derbyac962c92019-03-08 12:31:47 -0500547 void processDeviceFallback(SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
548 SkStrikeInterface* strike) override;
Herb Derbyc72594a2019-03-05 17:39:38 -0500549
joshualitt374b2f72015-07-21 08:05:03 -0700550 struct StrokeInfo {
551 SkScalar fFrameWidth;
552 SkScalar fMiterLimit;
553 SkPaint::Join fJoin;
554 };
555
556 enum TextType {
557 kHasDistanceField_TextType = 0x1,
558 kHasBitmap_TextType = 0x2,
559 };
560
561 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500562 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700563 GrGlyph** fGlyphs;
564 Run* fRuns;
Herb Derbya00da612019-03-04 17:10:01 -0500565
566 // Lifetime: The GrStrikeCache is owned by and has the same lifetime as the GrRecordingContext.
567 // The GrRecordingContext also owns the GrTextBlob cache which owns this GrTextBlob.
568 GrStrikeCache* const fStrikeCache;
Mike Reed80747ef2018-01-23 15:29:32 -0500569 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700570 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700571 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800572 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800573 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800574 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400575 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800576 SkScalar fInitialX;
577 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700578
579 // We can reuse distance field text, but only if the new viewmatrix would not result in
580 // a mip change. Because there can be multiple runs in a blob, we track the overall
581 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
Herb Derbya00da612019-03-04 17:10:01 -0500582 SkScalar fMaxMinScale{-SK_ScalarMax};
583 SkScalar fMinMaxScale{SK_ScalarMax};
Herb Derby2bb343c2018-11-08 15:03:48 -0500584 int fRunCount{0};
585 int fRunCountLimit;
Herb Derbya00da612019-03-04 17:10:01 -0500586 uint8_t fTextType{0};
joshualitt374b2f72015-07-21 08:05:03 -0700587};
588
Brian Salomon18923f92017-11-06 16:26:02 -0500589/**
590 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
591 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
592 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
593 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
594 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
595 * entire sub run has been completed.
596 */
Herb Derby86240592018-05-24 16:12:31 -0400597class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500598public:
599 /**
600 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
601 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
602 * SkGlyphCache.
603 */
Herb Derby86240592018-05-24 16:12:31 -0400604 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500605 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Herb Derby081e6f32019-01-16 13:46:02 -0500606 GrDeferredUploadTarget*, GrStrikeCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400607 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500608
609 struct Result {
610 /**
611 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
612 * draws and call regenerate() again.
613 */
614 bool fFinished = true;
615
616 /**
617 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
618 * fType is kFinished.
619 */
620 int fGlyphsRegenerated = 0;
621
622 /**
623 * Pointer where the caller finds the first regenerated vertex.
624 */
625 const char* fFirstVertex;
626 };
627
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500628 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500629
630private:
Brian Osman5d6be8f2019-01-08 12:02:51 -0500631 bool doRegen(Result*, bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs);
Brian Salomon18923f92017-11-06 16:26:02 -0500632
Robert Phillips4bc70112018-03-01 10:24:02 -0500633 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500634 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400635 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500636 GrDeferredUploadTarget* fUploadTarget;
Herb Derby081e6f32019-01-16 13:46:02 -0500637 GrStrikeCache* fGlyphCache;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500638 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500639 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500640 Run* fRun;
Herb Derby69ff8952018-11-12 11:39:12 -0500641 SubRun* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500642 GrColor fColor;
643 SkScalar fTransX;
644 SkScalar fTransY;
645
646 uint32_t fRegenFlags = 0;
647 int fCurrGlyph = 0;
648 bool fBrokenRun = false;
649};
650
Herb Derby86240592018-05-24 16:12:31 -0400651#endif // GrTextBlob_DEFINED