blob: 6e1d0b1bded07bb9c1c8b923b03926eab4f3a006 [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
8#ifndef GrAtlasTextBlob_DEFINED
9#define GrAtlasTextBlob_DEFINED
10
Brian Salomonf856fd12016-12-16 14:24:34 -050011#include "GrAtlasGlyphCache.h"
joshualitt259fbf12015-07-21 11:39:34 -070012#include "GrColor.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050013#include "GrDrawOpAtlas.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "GrMemoryPool.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050015#include "GrTextUtils.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"
joshualitt259fbf12015-07-21 11:39:34 -070022#include "SkSurfaceProps.h"
joshualitt374b2f72015-07-21 08:05:03 -070023#include "SkTInternalLList.h"
24
joshualitt2e2202e2015-12-10 11:22:08 -080025struct GrDistanceFieldAdjustTable;
joshualitt92303772016-02-10 11:55:52 -080026class GrMemoryPool;
joshualitt2e2202e2015-12-10 11:22:08 -080027class SkDrawFilter;
28class SkTextBlob;
29class SkTextBlobRunIterator;
30
joshualitt259fbf12015-07-21 11:39:34 -070031// With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob
32// that comes in to verify the integrity of its cache
joshualitt2f2ee832016-02-10 08:52:24 -080033#define CACHE_SANITY_CHECK 0
joshualitt259fbf12015-07-21 11:39:34 -070034
joshualitt374b2f72015-07-21 08:05:03 -070035/*
36 * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing
37 * on the GPU. These are initially created with valid positions and colors, but invalid
38 * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also
39 * consists of a number of runs. Runs inside a blob are flushed individually so they can be
40 * reordered.
41 *
42 * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that
43 * the GrAtlas will not evict anything the Blob needs.
44 *
45 * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose.
joshualitt259fbf12015-07-21 11:39:34 -070046 *
47 * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual
joshualitt374b2f72015-07-21 08:05:03 -070048 */
joshualitt2e2202e2015-12-10 11:22:08 -080049class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> {
50public:
joshualitt374b2f72015-07-21 08:05:03 -070051 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob);
52
Brian Salomon18923f92017-11-06 16:26:02 -050053 class VertexRegenerator;
54
Robert Phillips303cd582018-02-14 18:54:01 -050055 static sk_sp<GrAtlasTextBlob> Make(GrMemoryPool*, int glyphCount, int runCount);
joshualitt323c2eb2016-01-20 06:48:47 -080056
Brian Salomon5c6ac642017-12-19 11:09:32 -050057 /**
58 * We currently force regeneration of a blob if old or new matrix differ in having perspective.
59 * If we ever change that then the key must contain the perspectiveness when there are distance
60 * fields as perspective distance field use 3 component vertex positions and non-perspective
61 * uses 2.
62 */
joshualitt323c2eb2016-01-20 06:48:47 -080063 struct Key {
64 Key() {
65 sk_bzero(this, sizeof(Key));
66 }
67 uint32_t fUniqueID;
68 // Color may affect the gamma of the mask we generate, but in a fairly limited way.
69 // Each color is assigned to on of a fixed number of buckets based on its
70 // luminance. For each luminance bucket there is a "canonical color" that
71 // represents the bucket. This functionality is currently only supported for A8
72 SkColor fCanonicalColor;
73 SkPaint::Style fStyle;
74 SkPixelGeometry fPixelGeometry;
75 bool fHasBlur;
brianosman8d7ffce2016-04-21 08:29:06 -070076 uint32_t fScalerContextFlags;
joshualitt323c2eb2016-01-20 06:48:47 -080077
78 bool operator==(const Key& other) const {
79 return 0 == memcmp(this, &other, sizeof(Key));
80 }
81 };
82
joshualitt92303772016-02-10 11:55:52 -080083 void setupKey(const GrAtlasTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050084 const SkMaskFilterBase::BlurRec& blurRec,
joshualitt92303772016-02-10 11:55:52 -080085 const SkPaint& paint) {
86 fKey = key;
87 if (key.fHasBlur) {
88 fBlurRec = blurRec;
89 }
90 if (key.fStyle != SkPaint::kFill_Style) {
91 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
92 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
93 fStrokeInfo.fJoin = paint.getStrokeJoin();
94 }
95 }
96
joshualitt323c2eb2016-01-20 06:48:47 -080097 static const Key& GetKey(const GrAtlasTextBlob& blob) {
98 return blob.fKey;
99 }
100
101 static uint32_t Hash(const Key& key) {
mtklein4e976072016-08-08 09:06:27 -0700102 return SkOpts::hash(&key, sizeof(Key));
joshualitt323c2eb2016-01-20 06:48:47 -0800103 }
104
105 void operator delete(void* p) {
106 GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p);
Robert Phillips303cd582018-02-14 18:54:01 -0500107 if (blob->fPool) {
108 blob->fPool->release(p);
109 } else {
110 ::operator delete(p);
111 }
joshualitt323c2eb2016-01-20 06:48:47 -0800112 }
113 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400114 SK_ABORT("All blobs are created by placement new.");
joshualitt323c2eb2016-01-20 06:48:47 -0800115 return sk_malloc_throw(0);
116 }
117
118 void* operator new(size_t, void* p) { return p; }
119 void operator delete(void* target, void* placement) {
120 ::operator delete(target, placement);
121 }
122
123 bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
124 bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
125 void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
126 void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
127
joshualittddd22d82016-02-16 06:47:52 -0800128 int runCount() const { return fRunCount; }
129
joshualitt323c2eb2016-01-20 06:48:47 -0800130 void push_back_run(int currRun) {
131 SkASSERT(currRun < fRunCount);
132 if (currRun > 0) {
133 Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
134 Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
135 newRun.setAsSuccessor(lastRun);
136 }
137 }
138
139 // sets the last subrun of runIndex to use distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -0500140 void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias, bool hasWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800141 Run& run = fRuns[runIndex];
142 Run::SubRunInfo& subRun = run.fSubRunInfo.back();
143 subRun.setUseLCDText(hasLCD);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400144 subRun.setAntiAliased(isAntiAlias);
joshualitt323c2eb2016-01-20 06:48:47 -0800145 subRun.setDrawAsDistanceFields();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500146 subRun.setHasWCoord(hasWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800147 }
148
Jim Van Verth54d9c882018-02-08 16:14:48 -0500149 void setRunPaintFlags(int runIndex, uint16_t paintFlags) {
150 fRuns[runIndex].fPaintFlags = paintFlags & Run::kPaintFlagsMask;
Jim Van Verth89737de2018-02-06 21:30:20 +0000151 }
152
joshualitt323c2eb2016-01-20 06:48:47 -0800153 void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) {
154 // we init fMaxMinScale and fMinMaxScale in the constructor
155 fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale);
156 fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale);
157 }
158
159 // inits the override descriptor on the current run. All following subruns must use this
160 // descriptor
161 void initOverride(int runIndex) {
162 Run& run = fRuns[runIndex];
163 // Push back a new subrun to fill and set the override descriptor
164 run.push_back();
165 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
166 }
167
168 SkGlyphCache* setupCache(int runIndex,
169 const SkSurfaceProps& props,
Herb Derbyd8327a82018-01-22 14:39:27 -0500170 SkScalerContextFlags scalerContextFlags,
joshualitt323c2eb2016-01-20 06:48:47 -0800171 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -0800172 const SkMatrix* viewMatrix);
joshualitt323c2eb2016-01-20 06:48:47 -0800173
174 // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended
175 // as a path.
176 void appendGlyph(int runIndex,
177 const SkRect& positions,
178 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -0500179 GrAtlasTextStrike* strike,
joshualitt323c2eb2016-01-20 06:48:47 -0800180 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -0700181 SkGlyphCache*, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500182 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
183
184 // Appends a glyph to the blob as a path only.
185 void appendPathGlyph(int runIndex, const SkPath& path,
186 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
joshualitt323c2eb2016-01-20 06:48:47 -0800187
Brian Salomon5c6ac642017-12-19 11:09:32 -0500188 static size_t GetVertexStride(GrMaskFormat maskFormat, bool isDistanceFieldWithWCoord) {
joshualitt323c2eb2016-01-20 06:48:47 -0800189 switch (maskFormat) {
190 case kA8_GrMaskFormat:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500191 return isDistanceFieldWithWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800192 case kARGB_GrMaskFormat:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500193 SkASSERT(!isDistanceFieldWithWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800194 return kColorTextVASize;
195 default:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500196 SkASSERT(!isDistanceFieldWithWCoord);
joshualitt323c2eb2016-01-20 06:48:47 -0800197 return kLCDTextVASize;
198 }
199 }
200
Mike Reed80747ef2018-01-23 15:29:32 -0500201 bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilterBase::BlurRec& blurRec,
joshualitt323c2eb2016-01-20 06:48:47 -0800202 const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
203
Jim Van Verth54d9c882018-02-08 16:14:48 -0500204 void flush(GrAtlasGlyphCache*, GrTextUtils::Target*, const SkSurfaceProps& props,
205 const GrDistanceFieldAdjustTable* distanceAdjustTable,
206 const GrTextUtils::Paint& paint, const GrClip& clip,
207 const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x,
208 SkScalar y);
joshualitt323c2eb2016-01-20 06:48:47 -0800209
joshualitt8e0ef292016-02-19 14:13:03 -0800210 void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
211 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittbc811112016-02-11 12:42:02 -0800212 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
213 // into device space.
214 // We handle vertex bounds differently for distance field text and bitmap text because
215 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
216 // from one blob then we are going to pay the price here of mapping the rect for each run.
217 const Run& run = fRuns[runIndex];
218 const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex];
219 *outBounds = subRun.vertexBounds();
220 if (subRun.drawAsDistanceFields()) {
221 // Distance field text is positioned with the (X,Y) as part of the glyph position,
222 // and currently the view matrix is applied on the GPU
joshualitt8e0ef292016-02-19 14:13:03 -0800223 outBounds->offset(x - fInitialX, y - fInitialY);
224 viewMatrix.mapRect(outBounds);
joshualittbc811112016-02-11 12:42:02 -0800225 } else {
226 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
227 // device space.
228 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
229
230 boundsMatrix.postTranslate(-fInitialX, -fInitialY);
231
joshualitt8e0ef292016-02-19 14:13:03 -0800232 boundsMatrix.postTranslate(x, y);
joshualittbc811112016-02-11 12:42:02 -0800233
joshualitt8e0ef292016-02-19 14:13:03 -0800234 boundsMatrix.postConcat(viewMatrix);
joshualittbc811112016-02-11 12:42:02 -0800235 boundsMatrix.mapRect(outBounds);
236
237 // Due to floating point numerical inaccuracies, we have to round out here
238 outBounds->roundOut(outBounds);
239 }
240 }
241
joshualitt323c2eb2016-01-20 06:48:47 -0800242 // position + local coord
243 static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
244 static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500245 static const size_t kGrayTextDFPerspectiveVASize =
246 sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16);
joshualitt323c2eb2016-01-20 06:48:47 -0800247 static const size_t kLCDTextVASize = kGrayTextVASize;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500248 static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize;
joshualitt323c2eb2016-01-20 06:48:47 -0800249 static const int kVerticesPerGlyph = 4;
250
joshualitt323c2eb2016-01-20 06:48:47 -0800251 static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&);
joshualitt323c2eb2016-01-20 06:48:47 -0800252
253 // The color here is the GrPaint color, and it is used to determine whether we
254 // have to regenerate LCD text blobs.
255 // We use this color vs the SkPaint color because it has the colorfilter applied.
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400256 void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix,
257 SkScalar x, SkScalar y) {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400258 fLuminanceColor = luminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800259 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800260 }
261
joshualitt7481e752016-01-22 06:08:48 -0800262 void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
263 this->setupViewMatrix(viewMatrix, x, y);
joshualitt323c2eb2016-01-20 06:48:47 -0800264 }
265
joshualitt92303772016-02-10 11:55:52 -0800266 const Key& key() const { return fKey; }
267
268 ~GrAtlasTextBlob() {
269 for (int i = 0; i < fRunCount; i++) {
270 fRuns[i].~Run();
271 }
272 }
273
joshualittbc811112016-02-11 12:42:02 -0800274 ////////////////////////////////////////////////////////////////////////////////////////////////
275 // Internal test methods
Jim Van Verth56c37142017-10-31 14:44:25 -0400276 std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400277 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
278 const GrTextUtils::Paint&, const SkSurfaceProps&,
279 const GrDistanceFieldAdjustTable*, GrAtlasGlyphCache*,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400280 GrTextUtils::Target*);
joshualittbc811112016-02-11 12:42:02 -0800281
joshualitt323c2eb2016-01-20 06:48:47 -0800282private:
joshualitt92303772016-02-10 11:55:52 -0800283 GrAtlasTextBlob()
284 : fMaxMinScale(-SK_ScalarMax)
285 , fMinMaxScale(SK_ScalarMax)
286 , fTextType(0) {}
287
joshualitt323c2eb2016-01-20 06:48:47 -0800288
joshualitt8e0ef292016-02-19 14:13:03 -0800289 // This function will only be called when we are generating a blob from scratch. We record the
joshualitt7481e752016-01-22 06:08:48 -0800290 // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
291 // these numbers. When blobs are reused with new matrices, we need to return to model space so
292 // we can update the vertex bounds appropriately.
293 void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt8e0ef292016-02-19 14:13:03 -0800294 fInitialViewMatrix = viewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800295 if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
296 fInitialViewMatrixInverse = SkMatrix::I();
297 SkDebugf("Could not invert viewmatrix\n");
298 }
joshualitt8e0ef292016-02-19 14:13:03 -0800299 fInitialX = x;
300 fInitialY = y;
301
302 // make sure all initial subruns have the correct VM and X/Y applied
303 for (int i = 0; i < fRunCount; i++) {
304 fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
305 }
joshualitt7481e752016-01-22 06:08:48 -0800306 }
307
joshualitt374b2f72015-07-21 08:05:03 -0700308 /*
309 * Each Run inside of the blob can have its texture coordinates regenerated if required.
310 * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been
311 * any evictions inside of the atlas, then we will simply regenerate Runs. We could track
312 * this at a more fine grained level, but its not clear if this is worth it, as evictions
313 * should be fairly rare.
314 *
315 * One additional point, each run can contain glyphs with any of the three mask formats.
316 * We call these SubRuns. Because a subrun must be a contiguous range, we have to create
317 * a new subrun each time the mask format changes in a run. In theory, a run can have as
318 * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In
319 * practice, the vast majority of runs have only a single subrun.
320 *
321 * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to
Jim Van Verthf4c13162018-01-11 16:40:24 -0500322 * handle, we have a bit to mark the run as flushable via rendering as paths or as scaled
323 * glyphs. It would be a bit expensive to figure out ahead of time whether or not a run
joshualitt374b2f72015-07-21 08:05:03 -0700324 * can flush in this manner, so we always allocate vertices for the run, regardless of
325 * whether or not it is too large. The benefit of this strategy is that we can always reuse
326 * a blob allocation regardless of viewmatrix changes. We could store positions for these
Jim Van Verthf4c13162018-01-11 16:40:24 -0500327 * 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 -0700328 * glyph cache to get the path at flush time, or hold onto the path in the cache, which
329 * would greatly increase the memory of these cached items.
330 */
331 struct Run {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500332 Run() : fPaintFlags(0)
333 , fInitialized(false) {
joshualitt374b2f72015-07-21 08:05:03 -0700334 // To ensure we always have one subrun, we push back a fresh run here
335 fSubRunInfo.push_back();
336 }
337 struct SubRunInfo {
338 SubRunInfo()
Brian Salomon2ee084e2016-12-16 18:59:19 -0500339 : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration)
340 , fVertexStartIndex(0)
341 , fVertexEndIndex(0)
342 , fGlyphStartIndex(0)
343 , fGlyphEndIndex(0)
344 , fColor(GrColor_ILLEGAL)
345 , fMaskFormat(kA8_GrMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400346 , fFlags(0) {
Mike Reed274218e2018-01-08 15:05:02 -0500347 fVertexBounds = SkRectPriv::MakeLargestInverted();
joshualitt7481e752016-01-22 06:08:48 -0800348 }
joshualitt7e97b0b2015-07-31 15:18:08 -0700349 SubRunInfo(const SubRunInfo& that)
350 : fBulkUseToken(that.fBulkUseToken)
351 , fStrike(SkSafeRef(that.fStrike.get()))
joshualitt8e0ef292016-02-19 14:13:03 -0800352 , fCurrentViewMatrix(that.fCurrentViewMatrix)
joshualitt7481e752016-01-22 06:08:48 -0800353 , fVertexBounds(that.fVertexBounds)
joshualitt7e97b0b2015-07-31 15:18:08 -0700354 , fAtlasGeneration(that.fAtlasGeneration)
355 , fVertexStartIndex(that.fVertexStartIndex)
356 , fVertexEndIndex(that.fVertexEndIndex)
357 , fGlyphStartIndex(that.fGlyphStartIndex)
358 , fGlyphEndIndex(that.fGlyphEndIndex)
joshualitt8e0ef292016-02-19 14:13:03 -0800359 , fX(that.fX)
360 , fY(that.fY)
joshualittf9e658b2015-12-09 09:26:44 -0800361 , fColor(that.fColor)
joshualitt7e97b0b2015-07-31 15:18:08 -0700362 , fMaskFormat(that.fMaskFormat)
Jim Van Verth90e89b32017-07-06 16:36:55 -0400363 , fFlags(that.fFlags) {
joshualitt7e97b0b2015-07-31 15:18:08 -0700364 }
joshualitt3660d532015-12-07 11:32:50 -0800365
joshualitt18b072d2015-12-07 12:26:12 -0800366 // TODO when this object is more internal, drop the privacy
joshualitt3660d532015-12-07 11:32:50 -0800367 void resetBulkUseToken() { fBulkUseToken.reset(); }
Brian Salomon2ee084e2016-12-16 18:59:19 -0500368 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500369 void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); }
370 GrAtlasTextStrike* strike() const { return fStrike.get(); }
joshualitt3660d532015-12-07 11:32:50 -0800371
372 void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
373 uint64_t atlasGeneration() const { return fAtlasGeneration; }
374
375 size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800376 size_t vertexStartIndex() const { return fVertexStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800377 size_t vertexEndIndex() const { return fVertexEndIndex; }
378 void appendVertices(size_t vertexStride) {
379 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
380 }
381
382 uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800383 uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
joshualitt3660d532015-12-07 11:32:50 -0800384 uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
385 void glyphAppended() { fGlyphEndIndex++; }
joshualittf9e658b2015-12-09 09:26:44 -0800386 void setColor(GrColor color) { fColor = color; }
387 GrColor color() const { return fColor; }
joshualitt3660d532015-12-07 11:32:50 -0800388 void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
389 GrMaskFormat maskFormat() const { return fMaskFormat; }
390
joshualitt18b072d2015-12-07 12:26:12 -0800391 void setAsSuccessor(const SubRunInfo& prev) {
392 fGlyphStartIndex = prev.glyphEndIndex();
393 fGlyphEndIndex = prev.glyphEndIndex();
394
395 fVertexStartIndex = prev.vertexEndIndex();
396 fVertexEndIndex = prev.vertexEndIndex();
joshualitt8e0ef292016-02-19 14:13:03 -0800397
398 // copy over viewmatrix settings
399 this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
joshualitt18b072d2015-12-07 12:26:12 -0800400 }
401
joshualitt7481e752016-01-22 06:08:48 -0800402 const SkRect& vertexBounds() const { return fVertexBounds; }
403 void joinGlyphBounds(const SkRect& glyphBounds) {
404 fVertexBounds.joinNonEmptyArg(glyphBounds);
405 }
406
joshualitt8e0ef292016-02-19 14:13:03 -0800407 void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
408 fCurrentViewMatrix = viewMatrix;
409 fX = x;
410 fY = y;
411 }
412
413 // This function assumes the translation will be applied before it is called again
414 void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500415 SkScalar* transX, SkScalar* transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800416
joshualitt3660d532015-12-07 11:32:50 -0800417 // df properties
Jim Van Verth90e89b32017-07-06 16:36:55 -0400418 void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
419 bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
420 void setUseLCDText(bool useLCDText) {
421 fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
422 }
423 bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
424 void setAntiAliased(bool antiAliased) {
425 fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
426 }
427 bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500428 void setHasWCoord(bool hasW) {
429 fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag;
430 }
431 bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); }
joshualitt3660d532015-12-07 11:32:50 -0800432
433 private:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400434 enum Flag {
435 kDrawAsSDF_Flag = 0x1,
436 kUseLCDText_Flag = 0x2,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500437 kAntiAliased_Flag = 0x4,
438 kHasWCoord_Flag = 0x8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400439 };
440
Brian Salomon2ee084e2016-12-16 18:59:19 -0500441 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
Brian Salomonf856fd12016-12-16 14:24:34 -0500442 sk_sp<GrAtlasTextStrike> fStrike;
joshualitt8e0ef292016-02-19 14:13:03 -0800443 SkMatrix fCurrentViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800444 SkRect fVertexBounds;
joshualitt374b2f72015-07-21 08:05:03 -0700445 uint64_t fAtlasGeneration;
446 size_t fVertexStartIndex;
447 size_t fVertexEndIndex;
448 uint32_t fGlyphStartIndex;
449 uint32_t fGlyphEndIndex;
joshualitt8e0ef292016-02-19 14:13:03 -0800450 SkScalar fX;
451 SkScalar fY;
joshualittf9e658b2015-12-09 09:26:44 -0800452 GrColor fColor;
joshualitt374b2f72015-07-21 08:05:03 -0700453 GrMaskFormat fMaskFormat;
Jim Van Verth90e89b32017-07-06 16:36:55 -0400454 uint32_t fFlags;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400455 }; // SubRunInfo
joshualitt374b2f72015-07-21 08:05:03 -0700456
457 SubRunInfo& push_back() {
458 // Forward glyph / vertex information to seed the new sub run
joshualitt374b2f72015-07-21 08:05:03 -0700459 SubRunInfo& newSubRun = fSubRunInfo.push_back();
joshualitt18b072d2015-12-07 12:26:12 -0800460 const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
joshualitte43e3bd2015-07-29 11:10:38 -0700461
joshualitt18b072d2015-12-07 12:26:12 -0800462 newSubRun.setAsSuccessor(prevSubRun);
joshualitt374b2f72015-07-21 08:05:03 -0700463 return newSubRun;
464 }
465 static const int kMinSubRuns = 1;
Hal Canary144caf52016-11-07 17:57:18 -0500466 sk_sp<SkTypeface> fTypeface;
joshualitt374b2f72015-07-21 08:05:03 -0700467 SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo;
468 SkAutoDescriptor fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700469
470 // Effects from the paint that are used to build a SkScalerContext.
471 sk_sp<SkPathEffect> fPathEffect;
bsalomon8b6fa5e2016-05-19 16:23:47 -0700472 sk_sp<SkMaskFilter> fMaskFilter;
joshualitt3660d532015-12-07 11:32:50 -0800473
474 // Distance field text cannot draw coloremoji, and so has to fall back. However,
475 // though the distance field text and the coloremoji may share the same run, they
476 // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it
477 // will be used in place of the run's descriptor to regen texture coords
Ben Wagner145dbcd2016-11-03 14:40:50 -0400478 std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
Jim Van Verth54d9c882018-02-08 16:14:48 -0500479
480 // Any glyphs that can't be rendered with the base or override descriptor
481 // are rendered as paths
482 struct PathGlyph {
483 PathGlyph(const SkPath& path, SkScalar x, SkScalar y, SkScalar scale, bool preXformed)
484 : fPath(path)
485 , fX(x)
486 , fY(y)
487 , fScale(scale)
488 , fPreTransformed(preXformed) {}
489 SkPath fPath;
490 SkScalar fX;
491 SkScalar fY;
492 SkScalar fScale;
493 bool fPreTransformed;
494 };
495
496 SkTArray<PathGlyph> fPathGlyphs;
497
498 struct {
499 unsigned fPaintFlags : 16; // needed mainly for rendering paths
500 bool fInitialized : 1;
501 };
502 // the only flags we need to set
503 static constexpr auto kPaintFlagsMask = SkPaint::kAntiAlias_Flag;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400504 }; // Run
joshualitt374b2f72015-07-21 08:05:03 -0700505
Brian Salomonf18b1d82017-10-27 11:30:49 -0400506 inline std::unique_ptr<GrAtlasTextOp> makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400507 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400508 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
509 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
510 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
511 GrTextUtils::Target*);
joshualitt323c2eb2016-01-20 06:48:47 -0800512
joshualitt374b2f72015-07-21 08:05:03 -0700513 struct StrokeInfo {
514 SkScalar fFrameWidth;
515 SkScalar fMiterLimit;
516 SkPaint::Join fJoin;
517 };
518
519 enum TextType {
520 kHasDistanceField_TextType = 0x1,
521 kHasBitmap_TextType = 0x2,
522 };
523
524 // all glyph / vertex offsets are into these pools.
Brian Salomon18923f92017-11-06 16:26:02 -0500525 char* fVertices;
joshualitt374b2f72015-07-21 08:05:03 -0700526 GrGlyph** fGlyphs;
527 Run* fRuns;
Robert Phillips303cd582018-02-14 18:54:01 -0500528 GrMemoryPool* fPool; // this will be null when DDLs are being recorded
Mike Reed80747ef2018-01-23 15:29:32 -0500529 SkMaskFilterBase::BlurRec fBlurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700530 StrokeInfo fStrokeInfo;
joshualitt374b2f72015-07-21 08:05:03 -0700531 Key fKey;
joshualitt8e0ef292016-02-19 14:13:03 -0800532 SkMatrix fInitialViewMatrix;
joshualitt7481e752016-01-22 06:08:48 -0800533 SkMatrix fInitialViewMatrixInverse;
joshualitt2f2ee832016-02-10 08:52:24 -0800534 size_t fSize;
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400535 SkColor fLuminanceColor;
joshualitt7481e752016-01-22 06:08:48 -0800536 SkScalar fInitialX;
537 SkScalar fInitialY;
joshualitt374b2f72015-07-21 08:05:03 -0700538
539 // We can reuse distance field text, but only if the new viewmatrix would not result in
540 // a mip change. Because there can be multiple runs in a blob, we track the overall
541 // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
542 SkScalar fMaxMinScale;
543 SkScalar fMinMaxScale;
544 int fRunCount;
545 uint8_t fTextType;
joshualitt374b2f72015-07-21 08:05:03 -0700546};
547
Brian Salomon18923f92017-11-06 16:26:02 -0500548/**
549 * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself.
550 * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either
551 * because of changes to the atlas or because of different draw parameters (e.g. color change). In
552 * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to
553 * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the
554 * entire sub run has been completed.
555 */
556class GrAtlasTextBlob::VertexRegenerator {
557public:
558 /**
559 * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of
560 * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of
561 * SkGlyphCache.
562 */
Robert Phillips59ddc6d2018-02-21 21:56:35 +0000563 VertexRegenerator(GrAtlasTextBlob* blob, int runIdx, int subRunIdx, const SkMatrix& viewMatrix,
564 SkScalar x, SkScalar y, GrColor color, GrDeferredUploadTarget*,
565 GrAtlasGlyphCache*, SkAutoGlyphCache*);
Brian Salomon18923f92017-11-06 16:26:02 -0500566
567 struct Result {
568 /**
569 * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph
570 * draws and call regenerate() again.
571 */
572 bool fFinished = true;
573
574 /**
575 * How many glyphs were regenerated. Will be equal to the sub run's glyph count if
576 * fType is kFinished.
577 */
578 int fGlyphsRegenerated = 0;
579
580 /**
581 * Pointer where the caller finds the first regenerated vertex.
582 */
583 const char* fFirstVertex;
584 };
585
586 Result regenerate();
587
588private:
589 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
590 Result doRegen();
591
592 const SkMatrix& fViewMatrix;
593 GrAtlasTextBlob* fBlob;
594 GrDeferredUploadTarget* fUploadTarget;
595 GrAtlasGlyphCache* fGlyphCache;
596 SkAutoGlyphCache* fLazyCache;
597 Run* fRun;
598 Run::SubRunInfo* 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
joshualitt374b2f72015-07-21 08:05:03 -0700608#endif