blob: 739a07919523d4eb6f3130fb0f19a00682a5036c [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"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050013#include "GrGlyphCache.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 */
Mike Klein408ef212018-10-30 15:23:00 +000051class GrTextBlob : public SkNVRefCnt<GrTextBlob> {
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 Derbydc214c22018-11-08 13:31:39 -050058 void generateFromGlyphRunList(GrGlyphCache* glyphCache,
59 const GrShaderCaps& shaderCaps,
60 const GrTextContext::Options& options,
61 const SkPaint& paint,
62 const SkPMColor4f& filteredColor,
63 SkScalerContextFlags scalerContextFlags,
64 const SkMatrix& viewMatrix,
65 const SkSurfaceProps& props,
66 const SkGlyphRunList& glyphRunList,
67 SkGlyphRunListPainter* glyphPainter);
68
Herb Derby86240592018-05-24 16:12:31 -040069 static sk_sp<GrTextBlob> Make(int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080070
Brian Salomon5c6ac642017-12-19 11:09:32 -050071 /**
72 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
73 * If we ever change that then the key must contain the perspectiveness when there are distance
74 * fields as perspective distance field use 3 component vertex positions and non-perspective
75 * uses 2.
76 */
joshualitt323c2eb2016-01-20 06:48:47 -080077 struct Key {
78 Key() {
79 sk_bzero(this, sizeof(Key));
80 }
81 uint32_t fUniqueID;
82 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
83 // Each color is assigned to on of a fixed number of buckets based on its
84 // luminance. For each luminance bucket there is a "canonical color" that
85 // represents the bucket. This functionality is currently only supported for A8
86 SkColor fCanonicalColor;
87 SkPaint::Style fStyle;
88 SkPixelGeometry fPixelGeometry;
89 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070090 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080091
92 bool operator==(const Key& other) const {
93 return 0 == memcmp(this, &other, sizeof(Key));
94 }
95 };
96
Herb Derby86240592018-05-24 16:12:31 -040097 void setupKey(const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050098 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080099 const SkPaint& paint) {
100 fKey = key;
101 if (key.fHasBlur) {
102 fBlurRec = blurRec;
103 }
104 if (key.fStyle != SkPaint::kFill_Style) {
105 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
106 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
107 fStrokeInfo.fJoin = paint.getStrokeJoin();
108 }
109 }
110
Herb Derby86240592018-05-24 16:12:31 -0400111 static const Key& GetKey(const GrTextBlob& blob) {
joshualitt323c2eb2016-01-20 06:48:47 -0800112 return blob.fKey;
113 }
114
115 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700116 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800117 }
118
119 void operator delete(void* p) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400120 ::operator delete(p);
joshualitt323c2eb2016-01-20 06:48:47 -0800121 }
Herb Derbyb12175f2018-05-23 16:38:09 -0400122
joshualitt323c2eb2016-01-20 06:48:47 -0800123 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400124 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800125 return sk_malloc_throw(0);
126 }
127
128 void* operator new(size_t, void* p) { return p; }
joshualitt323c2eb2016-01-20 06:48:47 -0800129
130 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
131 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
132 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
133 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
134
Herb Derby2bb343c2018-11-08 15:03:48 -0500135 int runCountLimit() const { return fRunCountLimit; }
joshualittddd22d82016-02-16 06:47:52 -0800136
Herb Derby2bb343c2018-11-08 15:03:48 -0500137 Run* pushBackRun() {
138 SkASSERT(fRunCount < fRunCountLimit);
Herb Derby6dff60e2018-11-12 15:45:49 -0500139
140 // If there is more run, then connect up the subruns.
Herb Derby2bb343c2018-11-08 15:03:48 -0500141 if (fRunCount > 0) {
Herb Derby69ff8952018-11-12 11:39:12 -0500142 SubRun& newRun = fRuns[fRunCount].fSubRunInfo.back();
143 SubRun& lastRun = fRuns[fRunCount - 1].fSubRunInfo.back();
joshualitt323c2eb2016-01-20 06:48:47 -0800144 newRun.setAsSuccessor(lastRun);
145 }
joshualitt323c2eb2016-01-20 06:48:47 -0800146
Herb Derby2bb343c2018-11-08 15:03:48 -0500147 fRunCount++;
148 return &fRuns[fRunCount - 1];
Jim Van Verth89737de2018-02-06 21:30:20 +0000149 }
150
joshualitt323c2eb2016-01-20 06:48:47 -0800151 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
152 // we init fMaxMinScale and fMinMaxScale in the constructor
153 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
154 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
155 }
156
Jim Van Verthb515ae72018-05-23 16:44:55 -0400157 static size_t GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800158 switch (maskFormat) {
159 case kA8_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400160 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800161 case kARGB_GrMaskFormat:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400162 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800163 default:
Jim Van Verthb515ae72018-05-23 16:44:55 -0400164 SkASSERT(!hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800165 return kLCDTextVASize;
166 }
167 }
168
Herb Derby0edb2142018-10-16 17:04:11 -0400169 bool mustRegenerate(const SkPaint&, bool, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800170 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
171
Herb Derbyc1b482c2018-08-09 15:02:27 -0400172 void flush(GrTextTarget*, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500173 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400174 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400175 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800176
joshualitt8e0ef292016-02-19 14:13:03 -0800177 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
Jim Van Verth70276912018-06-01 13:46:46 -0400178 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
179 bool needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800180 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
181 // into device space.
182 // We handle vertex bounds differently for distance field text and bitmap text because
183 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
184 // from one blob then we are going to pay the price here of mapping the rect for each run.
185 const Run& run = fRuns[runIndex];
Herb Derby69ff8952018-11-12 11:39:12 -0500186 const SubRun& subRun = run.fSubRunInfo[subRunIndex];
joshualittbc811112016-02-11 12:42:02 -0800187 *outBounds = subRun.vertexBounds();
Jim Van Verth70276912018-06-01 13:46:46 -0400188 if (needsGlyphTransform) {
joshualittbc811112016-02-11 12:42:02 -0800189 // Distance field text is positioned with the (X,Y) as part of the glyph position,
190 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800191 outBounds->offset(x - fInitialX, y - fInitialY);
192 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800193 } else {
194 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
195 // device space.
196 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
197
198 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
199
joshualitt8e0ef292016-02-19 14:13:03 -0800200 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800201
joshualitt8e0ef292016-02-19 14:13:03 -0800202 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800203 boundsMatrix.mapRect(outBounds);
204
205 // Due to floating point numerical inaccuracies, we have to round out here
206 outBounds->roundOut(outBounds);
207 }
208 }
209
joshualitt323c2eb2016-01-20 06:48:47 -0800210 // position + local coord
211 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400212 static const size_t kColorTextPerspectiveVASize = sizeof(SkPoint3) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800213 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500214 static const size_t kGrayTextDFPerspectiveVASize =
215 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800216 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500217 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800218 static const int kVerticesPerGlyph = 4;
219
Herb Derby86240592018-05-24 16:12:31 -0400220 static void AssertEqual(const GrTextBlob&, const GrTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800221
222 // The color here is the GrPaint color, and it is used to determine whether we
223 // have to regenerate LCD text blobs.
224 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400225 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
226 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400227 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800228 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800229 }
230
joshualitt7481e752016-01-22 06:08:48 -0800231 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
232 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800233 }
234
joshualitt92303772016-02-10 11:55:52 -0800235 const Key& key() const { return fKey; }
236
Herb Derbyb12175f2018-05-23 16:38:09 -0400237 size_t size() const { return fSize; }
238
Herb Derby86240592018-05-24 16:12:31 -0400239 ~GrTextBlob() {
Herb Derby2bb343c2018-11-08 15:03:48 -0500240 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt92303772016-02-10 11:55:52 -0800241 fRuns[i].~Run();
242 }
243 }
244
joshualittbc811112016-02-11 12:42:02 -0800245 ////////////////////////////////////////////////////////////////////////////////////////////////
246 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400247 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400248 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Osmancf860852018-10-31 14:04:39 -0400249 const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400250 const SkSurfaceProps&, const GrDistanceFieldAdjustTable*,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400251 GrTextTarget*);
joshualittbc811112016-02-11 12:42:02 -0800252
joshualitt323c2eb2016-01-20 06:48:47 -0800253private:
Herb Derby86240592018-05-24 16:12:31 -0400254 GrTextBlob()
joshualitt92303772016-02-10 11:55:52 -0800255 : fMaxMinScale(-SK_ScalarMax)
256 , fMinMaxScale(SK_ScalarMax)
257 , fTextType(0) {}
258
joshualitt8e0ef292016-02-19 14:13:03 -0800259 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800260 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
261 // these numbers. When blobs are reused with new matrices, we need to return to model space so
262 // we can update the vertex bounds appropriately.
263 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800264 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800265 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
266 fInitialViewMatrixInverse = SkMatrix::I();
joshualitt7481e752016-01-22 06:08:48 -0800267 }
joshualitt8e0ef292016-02-19 14:13:03 -0800268 fInitialX = x;
269 fInitialY = y;
270
271 // make sure all initial subruns have the correct VM and X/Y applied
Herb Derby2bb343c2018-11-08 15:03:48 -0500272 for (int i = 0; i < fRunCountLimit; i++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800273 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
274 }
joshualitt7481e752016-01-22 06:08:48 -0800275 }
276
Herb Derby69ff8952018-11-12 11:39:12 -0500277 class SubRun {
278 public:
Herb Derby317adf72018-11-16 17:29:29 -0500279 SubRun(Run* run, const SkAutoDescriptor& desc)
280 : fRun{run}
281 , fDesc{desc} {}
Herb Derby69ff8952018-11-12 11:39:12 -0500282
Herb Derby6dff60e2018-11-12 15:45:49 -0500283 void appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect);
Herb Derby96a17092018-11-12 12:45:21 -0500284
Herb Derby69ff8952018-11-12 11:39:12 -0500285 // TODO when this object is more internal, drop the privacy
286 void resetBulkUseToken() { fBulkUseToken.reset(); }
287 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
288 void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
289 GrTextStrike* strike() const { return fStrike.get(); }
290 sk_sp<GrTextStrike> refStrike() const { return fStrike; }
291
292 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
293 uint64_t atlasGeneration() const { return fAtlasGeneration; }
294
295 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
296 size_t vertexStartIndex() const { return fVertexStartIndex; }
297 size_t vertexEndIndex() const { return fVertexEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500298
299 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
300 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
301 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
Herb Derby69ff8952018-11-12 11:39:12 -0500302 void setColor(GrColor color) { fColor = color; }
303 GrColor color() const { return fColor; }
304 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
305 GrMaskFormat maskFormat() const { return fMaskFormat; }
306
307 void setAsSuccessor(const SubRun& prev) {
308 fGlyphStartIndex = prev.glyphEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500309 fGlyphEndIndex = fGlyphStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500310
311 fVertexStartIndex = prev.vertexEndIndex();
Herb Derby6dff60e2018-11-12 15:45:49 -0500312 fVertexEndIndex = fVertexStartIndex;
Herb Derby69ff8952018-11-12 11:39:12 -0500313
314 // copy over viewmatrix settings
315 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
316 }
317
318 const SkRect& vertexBounds() const { return fVertexBounds; }
319 void joinGlyphBounds(const SkRect& glyphBounds) {
320 fVertexBounds.joinNonEmptyArg(glyphBounds);
321 }
322
323 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
324 fCurrentViewMatrix = viewMatrix;
325 fX = x;
326 fY = y;
327 }
328
329 // This function assumes the translation will be applied before it is called again
330 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
331 SkScalar* transX, SkScalar* transY);
332
333 // df properties
herbc5df7cb2018-11-15 17:08:26 -0500334 void setDrawAsDistanceFields() { fFlags.drawAsSdf = true; }
335 bool drawAsDistanceFields() const { return fFlags.drawAsSdf; }
336 void setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
337 bool hasUseLCDText() const { return fFlags.useLCDText; }
338 void setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
339 bool isAntiAliased() const { return fFlags.antiAliased; }
340 void setHasWCoord(bool hasW) { fFlags.hasWCoord = hasW; }
341 bool hasWCoord() const { return fFlags.hasWCoord; }
342 void setNeedsTransform(bool needsTransform) { fFlags.needsTransform = needsTransform; }
343 bool needsTransform() const { return fFlags.needsTransform; }
Herb Derby317adf72018-11-16 17:29:29 -0500344 void setFallback() {fFlags.argbFallback = true;}
345
346 const SkDescriptor* desc() const { return fDesc.getDesc(); }
Herb Derby69ff8952018-11-12 11:39:12 -0500347
348 private:
Herb Derby69ff8952018-11-12 11:39:12 -0500349 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
350 sk_sp<GrTextStrike> fStrike;
351 SkMatrix fCurrentViewMatrix;
Herb Derby6dff60e2018-11-12 15:45:49 -0500352 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
353 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
354 size_t fVertexStartIndex{0};
355 size_t fVertexEndIndex{0};
356 uint32_t fGlyphStartIndex{0};
357 uint32_t fGlyphEndIndex{0};
Herb Derby69ff8952018-11-12 11:39:12 -0500358 SkScalar fX;
359 SkScalar fY;
Herb Derby6dff60e2018-11-12 15:45:49 -0500360 GrColor fColor{GrColor_ILLEGAL};
361 GrMaskFormat fMaskFormat{kA8_GrMaskFormat};
herbc5df7cb2018-11-15 17:08:26 -0500362 struct {
363 bool drawAsSdf:1;
364 bool useLCDText:1;
365 bool antiAliased:1;
366 bool hasWCoord:1;
367 bool needsTransform:1;
Herb Derby317adf72018-11-16 17:29:29 -0500368 bool argbFallback:1;
369 } fFlags{false, false, false, false, false, false};
Herb Derbyf7d5d742018-11-16 13:24:32 -0500370 Run* const fRun;
Herb Derby317adf72018-11-16 17:29:29 -0500371 const SkAutoDescriptor& fDesc;
Herb Derby69ff8952018-11-12 11:39:12 -0500372 }; // SubRunInfo
373
374
joshualitt374b2f72015-07-21 08:05:03 -0700375 /*
376 * Each Run inside of the blob can have its texture coordinates regenerated if required.
377 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
378 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
379 * this at a more fine grained level, but its not clear if this is worth it, as evictions
380 * should be fairly rare.
381 *
382 * One additional point, each run can contain glyphs with any of the three mask formats.
383 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
384 * a new subrun each time the mask format changes in a run. In theory, a run can have as
385 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
386 * practice, the vast majority of runs have only a single subrun.
387 *
Herb Derby26cbe512018-05-24 14:39:01 -0400388 * Finally, for runs where the entire thing is too large for the GrTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500389 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
390 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700391 * can flush in this manner, so we always allocate vertices for the run, regardless of
392 * whether or not it is too large. The benefit of this strategy is that we can always reuse
393 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500394 * 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 -0700395 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
396 * would greatly increase the memory of these cached items.
397 */
398 struct Run {
Herb Derbyf7d5d742018-11-16 13:24:32 -0500399 // the only flags we need to set
400 static constexpr auto kPaintFlagsMask = SkPaint::kAntiAlias_Flag;
401
402 explicit Run(GrTextBlob* blob)
403 : fBlob{blob} {
joshualitt374b2f72015-07-21 08:05:03 -0700404 // To ensure we always have one subrun, we push back a fresh run here
Herb Derby317adf72018-11-16 17:29:29 -0500405 fSubRunInfo.emplace_back(this, fDescriptor);
joshualitt374b2f72015-07-21 08:05:03 -0700406 }
joshualitt374b2f72015-07-21 08:05:03 -0700407
Herb Derby2bb343c2018-11-08 15:03:48 -0500408 // sets the last subrun of runIndex to use w values
409 void setSubRunHasW(bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500410 SubRun& subRun = this->fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500411 subRun.setHasWCoord(hasWCoord);
412 }
413
414 // inits the override descriptor on the current run. All following subruns must use this
415 // descriptor
Herb Derby317adf72018-11-16 17:29:29 -0500416 SubRun* initARGBFallback() {
417 fARGBFallbackDescriptor.reset(new SkAutoDescriptor{});
Herb Derby2bb343c2018-11-08 15:03:48 -0500418 // Push back a new subrun to fill and set the override descriptor
Herb Derby317adf72018-11-16 17:29:29 -0500419 SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor);
420 subRun->setMaskFormat(kARGB_GrMaskFormat);
421 subRun->setFallback();
422 return subRun;
Herb Derby2bb343c2018-11-08 15:03:48 -0500423 }
424
425 // Appends a glyph to the blob as a path only.
426 void appendPathGlyph(
427 const SkPath& path, SkPoint position, SkScalar scale, bool preTransformed);
428
429 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
430 // as a path.
431 void appendGlyph(GrTextBlob* blob,
432 const sk_sp<GrTextStrike>& strike,
433 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
434 SkPoint origin,
435 const SkPMColor4f& color, SkGlyphCache* skGlyphCache,
436 SkScalar textRatio, bool needsTransform);
437
438 SkExclusiveStrikePtr setupCache(const SkPaint& skPaint,
439 const SkSurfaceProps& props,
440 SkScalerContextFlags scalerContextFlags,
441 const SkMatrix& viewMatrix);
442
443 void setRunPaintFlags(uint16_t paintFlags) {
444 fPaintFlags = paintFlags & Run::kPaintFlagsMask;
445 }
446
447 // sets the last subrun of runIndex to use distance field text
448 void setSubRunHasDistanceFields(bool hasLCD, bool isAntiAlias, bool hasWCoord) {
Herb Derby69ff8952018-11-12 11:39:12 -0500449 SubRun& subRun = fSubRunInfo.back();
Herb Derby2bb343c2018-11-08 15:03:48 -0500450 subRun.setUseLCDText(hasLCD);
451 subRun.setAntiAliased(isAntiAlias);
452 subRun.setDrawAsDistanceFields();
453 subRun.setHasWCoord(hasWCoord);
454 }
455
Herb Derby317adf72018-11-16 17:29:29 -0500456 SubRun* pushBackSubRun(const SkAutoDescriptor& desc) {
joshualitt374b2f72015-07-21 08:05:03 -0700457 // Forward glyph / vertex information to seed the new sub run
Herb Derby317adf72018-11-16 17:29:29 -0500458 SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc);
Herb Derby69ff8952018-11-12 11:39:12 -0500459 const SubRun& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700460
joshualitt18b072d2015-12-07 12:26:12 -0800461 newSubRun.setAsSuccessor(prevSubRun);
Herb Derby317adf72018-11-16 17:29:29 -0500462 return &newSubRun;
joshualitt374b2f72015-07-21 08:05:03 -0700463 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500464
465 // Any glyphs that can't be rendered with the base or override descriptor
466 // are rendered as paths
467 struct PathGlyph {
468 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
469 : fPath(path)
470 , fX(x)
471 , fY(y)
472 , fScale(scale)
473 , fPreTransformed(preXformed) {}
474 SkPath fPath;
475 SkScalar fX;
476 SkScalar fY;
477 SkScalar fScale;
478 bool fPreTransformed;
479 };
480
Herb Derbyf7d5d742018-11-16 13:24:32 -0500481
482 sk_sp<SkTypeface> fTypeface;
483 SkSTArray<1, SubRun> fSubRunInfo;
484 SkAutoDescriptor fDescriptor;
485
486 // Effects from the paint that are used to build a SkScalerContext.
487 sk_sp<SkPathEffect> fPathEffect;
488 sk_sp<SkMaskFilter> fMaskFilter;
489
490 // Distance field text cannot draw coloremoji, and so has to fall back. However,
491 // though the distance field text and the coloremoji may share the same run, they
Herb Derby317adf72018-11-16 17:29:29 -0500492 // will have different descriptors. If fARGBFallbackDescriptor is non-nullptr, then it
Herb Derbyf7d5d742018-11-16 13:24:32 -0500493 // will be used in place of the run's descriptor to regen texture coords
Herb Derby317adf72018-11-16 17:29:29 -0500494 std::unique_ptr<SkAutoDescriptor> fARGBFallbackDescriptor;
Herb Derbyf7d5d742018-11-16 13:24:32 -0500495
Jim Van Verth54d9c882018-02-08 16:14:48 -0500496 SkTArray<PathGlyph> fPathGlyphs;
497
Herb Derbyf7d5d742018-11-16 13:24:32 -0500498 uint16_t fPaintFlags{0}; // needed mainly for rendering paths
499 bool fInitialized{false};
500
501 GrTextBlob* const fBlob;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400502 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700503
Brian Salomonf18b1d82017-10-27 11:30:49 -0400504 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Herb Derby69ff8952018-11-12 11:39:12 -0500505 const SubRun& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400506 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400507 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps&,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400508 const GrDistanceFieldAdjustTable*, GrTextTarget*);
joshualitt323c2eb2016-01-20 06:48:47 -0800509
joshualitt374b2f72015-07-21 08:05:03 -0700510 struct StrokeInfo {
511 SkScalar fFrameWidth;
512 SkScalar fMiterLimit;
513 SkPaint::Join fJoin;
514 };
515
516 enum TextType {
517 kHasDistanceField_TextType = 0x1,
518 kHasBitmap_TextType = 0x2,
519 };
520
521 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500522 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700523 GrGlyph** fGlyphs;
524 Run* fRuns;
Mike Reed80747ef2018-01-23 15:29:32 -0500525 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700526 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700527 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800528 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800529 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800530 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400531 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800532 SkScalar fInitialX;
533 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700534
535 // We can reuse distance field text, but only if the new viewmatrix would not result in
536 // a mip change. Because there can be multiple runs in a blob, we track the overall
537 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
538 SkScalar fMaxMinScale;
539 SkScalar fMinMaxScale;
Herb Derby2bb343c2018-11-08 15:03:48 -0500540 int fRunCount{0};
541 int fRunCountLimit;
joshualitt374b2f72015-07-21 08:05:03 -0700542 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700543};
544
Brian Salomon18923f92017-11-06 16:26:02 -0500545/**
546 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
547 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
548 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
549 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
550 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
551 * entire sub run has been completed.
552 */
Herb Derby86240592018-05-24 16:12:31 -0400553class GrTextBlob::VertexRegenerator {
Brian Salomon18923f92017-11-06 16:26:02 -0500554public:
555 /**
556 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
557 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
558 * SkGlyphCache.
559 */
Herb Derby86240592018-05-24 16:12:31 -0400560 VertexRegenerator(GrResourceProvider*, GrTextBlob*, int runIdx, int subRunIdx,
Robert Phillips4bc70112018-03-01 10:24:02 -0500561 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500562 GrDeferredUploadTarget*, GrGlyphCache*, GrAtlasManager*,
Herb Derby7956b592018-03-22 16:10:30 -0400563 SkExclusiveStrikePtr*);
Brian Salomon18923f92017-11-06 16:26:02 -0500564
565 struct Result {
566 /**
567 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
568 * draws and call regenerate() again.
569 */
570 bool fFinished = true;
571
572 /**
573 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
574 * fType is kFinished.
575 */
576 int fGlyphsRegenerated = 0;
577
578 /**
579 * Pointer where the caller finds the first regenerated vertex.
580 */
581 const char* fFirstVertex;
582 };
583
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500584 bool regenerate(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500585
586private:
587 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500588 bool doRegen(Result*);
Brian Salomon18923f92017-11-06 16:26:02 -0500589
Robert Phillips4bc70112018-03-01 10:24:02 -0500590 GrResourceProvider* fResourceProvider;
Brian Salomon18923f92017-11-06 16:26:02 -0500591 const SkMatrix& fViewMatrix;
Herb Derby86240592018-05-24 16:12:31 -0400592 GrTextBlob* fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500593 GrDeferredUploadTarget* fUploadTarget;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500594 GrGlyphCache* fGlyphCache;
595 GrAtlasManager* fFullAtlasManager;
Herb Derby8c4cbf42018-03-09 15:28:04 -0500596 SkExclusiveStrikePtr* fLazyCache;
Brian Salomon18923f92017-11-06 16:26:02 -0500597 Run* fRun;
Herb Derby69ff8952018-11-12 11:39:12 -0500598 SubRun* fSubRun;
Brian Salomon18923f92017-11-06 16:26:02 -0500599 GrColor fColor;
600 SkScalar fTransX;
601 SkScalar fTransY;
602
603 uint32_t fRegenFlags = 0;
604 int fCurrGlyph = 0;
605 bool fBrokenRun = false;
606};
607
Herb Derby86240592018-05-24 16:12:31 -0400608#endif // GrTextBlob_DEFINED