blob: b6fc3ce170248ce41aa9c0dc715ffc01550dd1ea [file] [log] [blame]
joshualitt259fbf12015-07-21 11:39:34 -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#include "GrTextBlob.h"
joshualitt2e2202e2015-12-10 11:22:08 -08009#include "GrBlurUtils.h"
Jim Van Verth58c3cce2017-10-19 15:50:24 -040010#include "GrClip.h"
joshualitt2e2202e2015-12-10 11:22:08 -080011#include "GrContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080012#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080013#include "SkColorFilter.h"
14#include "SkDrawFilter.h"
joshualitte76b4bb32015-12-28 07:23:58 -080015#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050017#include "SkPaintPriv.h"
joshualitt2e2202e2015-12-10 11:22:08 -080018#include "SkTextBlobRunIterator.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050019#include "SkTextToPathIter.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080021
Herb Derby86240592018-05-24 16:12:31 -040022sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
23 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080024 // and size for the glyphIds array.
25 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Herb Derby86240592018-05-24 16:12:31 -040026 size_t size = sizeof(GrTextBlob) +
joshualitt92303772016-02-10 11:55:52 -080027 verticesCount +
28 glyphCount * sizeof(GrGlyph**) +
Herb Derby86240592018-05-24 16:12:31 -040029 sizeof(GrTextBlob::Run) * runCount;
joshualitt92303772016-02-10 11:55:52 -080030
Herb Derbyb12175f2018-05-23 16:38:09 -040031 void* allocation = ::operator new (size);
32
joshualitt92303772016-02-10 11:55:52 -080033 if (CACHE_SANITY_CHECK) {
34 sk_bzero(allocation, size);
35 }
36
Herb Derby86240592018-05-24 16:12:31 -040037 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080038 cacheBlob->fSize = size;
39
40 // setup offsets for vertices / glyphs
Herb Derby86240592018-05-24 16:12:31 -040041 cacheBlob->fVertices = sizeof(GrTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080042 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
Herb Derby86240592018-05-24 16:12:31 -040043 cacheBlob->fRuns = reinterpret_cast<GrTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualitt92303772016-02-10 11:55:52 -080044
45 // Initialize runs
46 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040047 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080048 }
49 cacheBlob->fRunCount = runCount;
joshualitt92303772016-02-10 11:55:52 -080050 return cacheBlob;
51}
52
Herb Derby86240592018-05-24 16:12:31 -040053SkExclusiveStrikePtr GrTextBlob::setupCache(int runIndex,
Herb Derby526819d2018-03-09 12:51:12 -050054 const SkSurfaceProps& props,
55 SkScalerContextFlags scalerContextFlags,
56 const SkPaint& skPaint,
57 const SkMatrix* viewMatrix) {
Herb Derby86240592018-05-24 16:12:31 -040058 GrTextBlob::Run* run = &fRuns[runIndex];
joshualitte76b4bb32015-12-28 07:23:58 -080059
60 // if we have an override descriptor for the run, then we should use that
61 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
62 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070063 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050064 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
65 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050066 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070067 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derbyfa996902018-04-18 11:36:12 -040069 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080070}
71
Herb Derby86240592018-05-24 16:12:31 -040072void GrTextBlob::appendGlyph(int runIndex,
joshualittf528e0d2015-12-09 06:42:52 -080073 const SkRect& positions,
74 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050075 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080076 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070077 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050078 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050079 if (positions.isEmpty()) {
80 return;
81 }
joshualitta06e6ab2015-12-10 08:54:41 -080082
83 // If the glyph is too large we fall back to paths
84 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050085 if (nullptr == glyph->fPath) {
86 const SkPath* glyphPath = cache->findPath(skGlyph);
87 if (!glyphPath) {
88 return;
89 }
90
91 glyph->fPath = new SkPath(*glyphPath);
92 }
93 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -080094 return;
95 }
96
joshualittf528e0d2015-12-09 06:42:52 -080097 Run& run = fRuns[runIndex];
98 GrMaskFormat format = glyph->fMaskFormat;
99
100 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
101 if (run.fInitialized && subRun->maskFormat() != format) {
102 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500103 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800104 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500105 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800106 }
107
108 run.fInitialized = true;
109
Brian Salomon5c6ac642017-12-19 11:09:32 -0500110 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400111 // glyphs drawn in perspective must always have a w coord.
112 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500113
114 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800115
116 subRun->setMaskFormat(format);
117
joshualitt7481e752016-01-22 06:08:48 -0800118 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800119 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800120
121 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
122
Brian Salomon5c6ac642017-12-19 11:09:32 -0500123 // We always write the third position component used by SDFs. If it is unused it gets
124 // overwritten. Similarly, we always write the color and the blob will later overwrite it
125 // with texture coords if it is unused.
126 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
127 // V0
128 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
129 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
130 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800131
Brian Salomon5c6ac642017-12-19 11:09:32 -0500132 // V1
133 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
134 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
135 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800136
Brian Salomon5c6ac642017-12-19 11:09:32 -0500137 // V2
138 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
139 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
140 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800141
Brian Salomon5c6ac642017-12-19 11:09:32 -0500142 // V3
143 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
144 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800145
joshualitt18b072d2015-12-07 12:26:12 -0800146 subRun->appendVertices(vertexStride);
147 fGlyphs[subRun->glyphEndIndex()] = glyph;
148 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400149 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800150}
151
Herb Derby86240592018-05-24 16:12:31 -0400152void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500153 SkScalar scale, bool preTransformed) {
154 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400155 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800156}
157
Herb Derby86240592018-05-24 16:12:31 -0400158bool GrTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500159 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800160 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
161 // If we have LCD text then our canonical color will be set to transparent, in this case we have
162 // to regenerate the blob on any color change
163 // We use the grPaint to get any color filter effects
164 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400165 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800166 return true;
167 }
168
joshualitt8e0ef292016-02-19 14:13:03 -0800169 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800170 return true;
171 }
172
Brian Salomon5c6ac642017-12-19 11:09:32 -0500173 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800174 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800175 return true;
176 }
177
178 // We only cache one masked version
179 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400180 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800181 return true;
182 }
183
184 // Similarly, we only cache one version for each style
185 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500186 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
187 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
188 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800189 return true;
190 }
191
192 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
193 // for mixed blobs if this becomes an issue.
194 if (this->hasBitmap() && this->hasDistanceField()) {
195 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800196 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800197 return false;
198 }
199 return true;
200 }
201
202 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800203 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
204 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
205 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
206 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800207 return true;
208 }
209
210 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
211 // but only for integer translations.
212 // This cool bit of math will determine the necessary translation to apply to the already
213 // generated vertex coordinates to move them to the correct position
214 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800215 viewMatrix.getScaleX() * (x - fInitialX) +
216 viewMatrix.getSkewX() * (y - fInitialY) -
217 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800218 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800219 viewMatrix.getSkewY() * (x - fInitialX) +
220 viewMatrix.getScaleY() * (y - fInitialY) -
221 fInitialViewMatrix.getTranslateY();
222 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800223 return true;
224 }
joshualittfd5f6c12015-12-10 07:44:50 -0800225 } else if (this->hasDistanceField()) {
226 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
227 // distance field being generated, so we have to regenerate in those cases
228 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800229 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800230 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
231 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
232 return true;
233 }
joshualittfd5f6c12015-12-10 07:44:50 -0800234 }
235
joshualittfd5f6c12015-12-10 07:44:50 -0800236 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
237 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
238 // the blob anyways at flush time, so no need to regenerate explicitly
239 return false;
240}
241
Herb Derby86240592018-05-24 16:12:31 -0400242inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400243 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400244 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
245 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500246 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800247 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800248
Brian Salomon44acb5b2017-07-18 19:59:24 -0400249 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400250 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500251 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800252 if (info.drawAsDistanceFields()) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400253 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips5a66efb2018-03-07 15:13:18 -0500254 std::move(grPaint), glyphCount, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400255 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400256 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800257 } else {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500258 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400259 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800260 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500261 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800262 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400263 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800264 geometry.fBlob = SkRef(this);
265 geometry.fRun = run;
266 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500267 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400268 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800269 geometry.fX = x;
270 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500271 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400272 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800273}
274
joshualitt8e0ef292016-02-19 14:13:03 -0800275static void calculate_translation(bool applyVM,
276 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
277 const SkMatrix& currentViewMatrix, SkScalar currentX,
278 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
279 if (applyVM) {
280 *transX = newViewMatrix.getTranslateX() +
281 newViewMatrix.getScaleX() * (newX - currentX) +
282 newViewMatrix.getSkewX() * (newY - currentY) -
283 currentViewMatrix.getTranslateX();
284
285 *transY = newViewMatrix.getTranslateY() +
286 newViewMatrix.getSkewY() * (newX - currentX) +
287 newViewMatrix.getScaleY() * (newY - currentY) -
288 currentViewMatrix.getTranslateY();
289 } else {
290 *transX = newX - currentX;
291 *transY = newY - currentY;
292 }
293}
294
Herb Derby86240592018-05-24 16:12:31 -0400295void GrTextBlob::flush(GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500296 const GrDistanceFieldAdjustTable* distanceAdjustTable,
297 const GrTextUtils::Paint& paint, const GrClip& clip,
298 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
299 SkScalar x, SkScalar y) {
300
Herb Derby86240592018-05-24 16:12:31 -0400301 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500302 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
303 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Ben Wagnerd234afd2018-04-13 15:50:01 -0400304 GrTextUtils::RunPaint runPaint(&paint, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500305 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
306 Run& run = fRuns[runIndex];
307
308 // first flush any path glyphs
309 if (run.fPathGlyphs.count()) {
310 SkScalar transX, transY;
311 uint16_t paintFlags = run.fPaintFlags;
312 if (!runPaint.modifyForRun(
313 [paintFlags](SkPaint* p) {
314 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
315 })) {
316 continue;
317 }
318 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400319 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500320 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
321 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
322 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
323 SkMatrix pathMatrix;
324 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
325 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
326 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
327 }
joshualitt2e2202e2015-12-10 11:22:08 -0800328 }
joshualitt2e2202e2015-12-10 11:22:08 -0800329
Jim Van Verth54d9c882018-02-08 16:14:48 -0500330 // then flush each subrun, if any
331 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000332 continue;
333 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500334 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
335 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
336 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
337 int glyphCount = info.glyphCount();
338 if (0 == glyphCount) {
339 continue;
340 }
341
342 bool skipClip = false;
343 bool submitOp = true;
344 SkIRect clipRect = SkIRect::MakeEmpty();
345 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
346 SkRRect clipRRect;
347 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400348 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500349 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400350 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500351 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500352 clipRRect.isRect() && GrAA::kNo == aa) {
353 skipClip = true;
354 // We only need to do clipping work if the subrun isn't contained by the clip
355 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400356 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
357 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500358 if (!clipRRect.getBounds().contains(subRunBounds)) {
359 // If the subrun is completely outside, don't add an op for it
360 if (!clipRRect.getBounds().intersects(subRunBounds)) {
361 submitOp = false;
362 }
363 else {
364 clipRRect.getBounds().round(&clipRect);
365 }
366 }
367 }
368
369 if (submitOp) {
370 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
371 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500372 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500373 if (op) {
374 if (skipClip) {
375 target->addDrawOp(GrNoClip(), std::move(op));
376 }
377 else {
378 target->addDrawOp(clip, std::move(op));
379 }
380 }
381 }
382 }
383
Jim Van Verth89737de2018-02-06 21:30:20 +0000384 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000385}
386
Herb Derby86240592018-05-24 16:12:31 -0400387std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400388 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
389 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500390 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
Herb Derby86240592018-05-24 16:12:31 -0400391 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400392 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400393 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500394 distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800395}
joshualitt2e2202e2015-12-10 11:22:08 -0800396
Herb Derby86240592018-05-24 16:12:31 -0400397void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800398 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700399
joshualitt2f2ee832016-02-10 08:52:24 -0800400 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
401 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700402
joshualitt2f2ee832016-02-10 08:52:24 -0800403 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
404 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
405 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700406
joshualitt2f2ee832016-02-10 08:52:24 -0800407 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800408 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
409 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
410 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
411 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700412
joshualitt2f2ee832016-02-10 08:52:24 -0800413 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700414 for (int i = 0; i < l.fRunCount; i++) {
415 const Run& lRun = l.fRuns[i];
416 const Run& rRun = r.fRuns[i];
417
joshualitt259fbf12015-07-21 11:39:34 -0700418 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800419 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500420 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700421 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800422 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700423 }
424
joshualitt259fbf12015-07-21 11:39:34 -0700425
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
427 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700428 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700429
430 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800431 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
432 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700433 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
434 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700435 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800436 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700437 }
438
439 // color can be changed
440 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800441 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700442
joshualitt2f2ee832016-02-10 08:52:24 -0800443 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700444 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
445 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
446 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
447
joshualitt2f2ee832016-02-10 08:52:24 -0800448 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
449 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700450
joshualitt2f2ee832016-02-10 08:52:24 -0800451 if (lSubRun.strike()) {
452 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500453 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
454 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800455
456 } else {
457 SkASSERT_RELEASE(!rSubRun.strike());
458 }
459
460 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
461 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
462 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
463 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
464 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
465 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
466 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700467 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500468
469 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
470 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
471 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
472 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
473
474 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
475 // We can't assert that these have the same translations
476 }
joshualitt259fbf12015-07-21 11:39:34 -0700477 }
478}
joshualitt8e0ef292016-02-19 14:13:03 -0800479
Herb Derby86240592018-05-24 16:12:31 -0400480void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800481 SkScalar x, SkScalar y, SkScalar* transX,
482 SkScalar* transY) {
483 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
484 fCurrentViewMatrix, fX, fY, transX, transY);
485 fCurrentViewMatrix = viewMatrix;
486 fX = x;
487 fY = y;
488}