blob: 00995d02390d4208a185b8b56227288746b8c7b2 [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"
Mike Klein79aea6a2018-06-11 10:45:26 -040021#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080022
Herb Derby86240592018-05-24 16:12:31 -040023sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
24 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080025 // and size for the glyphIds array.
26 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Herb Derby86240592018-05-24 16:12:31 -040027 size_t size = sizeof(GrTextBlob) +
joshualitt92303772016-02-10 11:55:52 -080028 verticesCount +
29 glyphCount * sizeof(GrGlyph**) +
Herb Derby86240592018-05-24 16:12:31 -040030 sizeof(GrTextBlob::Run) * runCount;
joshualitt92303772016-02-10 11:55:52 -080031
Herb Derbyb12175f2018-05-23 16:38:09 -040032 void* allocation = ::operator new (size);
33
joshualitt92303772016-02-10 11:55:52 -080034 if (CACHE_SANITY_CHECK) {
35 sk_bzero(allocation, size);
36 }
37
Herb Derby86240592018-05-24 16:12:31 -040038 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080039 cacheBlob->fSize = size;
40
41 // setup offsets for vertices / glyphs
Herb Derby86240592018-05-24 16:12:31 -040042 cacheBlob->fVertices = sizeof(GrTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080043 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
Herb Derby86240592018-05-24 16:12:31 -040044 cacheBlob->fRuns = reinterpret_cast<GrTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualitt92303772016-02-10 11:55:52 -080045
46 // Initialize runs
47 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040048 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080049 }
50 cacheBlob->fRunCount = runCount;
joshualitt92303772016-02-10 11:55:52 -080051 return cacheBlob;
52}
53
Herb Derby86240592018-05-24 16:12:31 -040054SkExclusiveStrikePtr GrTextBlob::setupCache(int runIndex,
Herb Derby526819d2018-03-09 12:51:12 -050055 const SkSurfaceProps& props,
56 SkScalerContextFlags scalerContextFlags,
57 const SkPaint& skPaint,
58 const SkMatrix* viewMatrix) {
Herb Derby86240592018-05-24 16:12:31 -040059 GrTextBlob::Run* run = &fRuns[runIndex];
joshualitte76b4bb32015-12-28 07:23:58 -080060
61 // if we have an override descriptor for the run, then we should use that
62 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
63 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050065 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
66 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050067 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070069 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derbyfa996902018-04-18 11:36:12 -040070 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080071}
72
Herb Derby86240592018-05-24 16:12:31 -040073void GrTextBlob::appendGlyph(int runIndex,
joshualittf528e0d2015-12-09 06:42:52 -080074 const SkRect& positions,
75 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050076 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080077 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070078 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050079 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050080 if (positions.isEmpty()) {
81 return;
82 }
joshualitta06e6ab2015-12-10 08:54:41 -080083
84 // If the glyph is too large we fall back to paths
85 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050086 if (nullptr == glyph->fPath) {
87 const SkPath* glyphPath = cache->findPath(skGlyph);
88 if (!glyphPath) {
89 return;
90 }
91
92 glyph->fPath = new SkPath(*glyphPath);
93 }
94 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -080095 return;
96 }
97
joshualittf528e0d2015-12-09 06:42:52 -080098 Run& run = fRuns[runIndex];
99 GrMaskFormat format = glyph->fMaskFormat;
100
101 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
102 if (run.fInitialized && subRun->maskFormat() != format) {
103 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500104 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800105 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500106 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800107 }
108
109 run.fInitialized = true;
110
Brian Salomon5c6ac642017-12-19 11:09:32 -0500111 bool hasW = subRun->hasWCoord();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400112 // glyphs drawn in perspective must always have a w coord.
113 SkASSERT(hasW || !fInitialViewMatrix.hasPerspective());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114
115 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800116
117 subRun->setMaskFormat(format);
118
joshualitt7481e752016-01-22 06:08:48 -0800119 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800120 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800121
122 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
123
Brian Salomon5c6ac642017-12-19 11:09:32 -0500124 // We always write the third position component used by SDFs. If it is unused it gets
125 // overwritten. Similarly, we always write the color and the blob will later overwrite it
126 // with texture coords if it is unused.
127 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
128 // V0
129 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
130 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
131 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800132
Brian Salomon5c6ac642017-12-19 11:09:32 -0500133 // V1
134 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
135 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
136 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800137
Brian Salomon5c6ac642017-12-19 11:09:32 -0500138 // V2
139 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
140 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
141 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800142
Brian Salomon5c6ac642017-12-19 11:09:32 -0500143 // V3
144 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
145 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800146
joshualitt18b072d2015-12-07 12:26:12 -0800147 subRun->appendVertices(vertexStride);
148 fGlyphs[subRun->glyphEndIndex()] = glyph;
149 subRun->glyphAppended();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400150 subRun->setNeedsTransform(!preTransformed);
joshualitt18b072d2015-12-07 12:26:12 -0800151}
152
Herb Derby86240592018-05-24 16:12:31 -0400153void GrTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500154 SkScalar scale, bool preTransformed) {
155 Run& run = fRuns[runIndex];
Herb Derby86240592018-05-24 16:12:31 -0400156 run.fPathGlyphs.push_back(GrTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800157}
158
Herb Derby86240592018-05-24 16:12:31 -0400159bool GrTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500160 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800161 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
162 // If we have LCD text then our canonical color will be set to transparent, in this case we have
163 // to regenerate the blob on any color change
164 // We use the grPaint to get any color filter effects
165 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400166 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800167 return true;
168 }
169
joshualitt8e0ef292016-02-19 14:13:03 -0800170 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800171 return true;
172 }
173
Brian Salomon5c6ac642017-12-19 11:09:32 -0500174 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800175 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800176 return true;
177 }
178
179 // We only cache one masked version
180 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400181 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800182 return true;
183 }
184
185 // Similarly, we only cache one version for each style
186 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500187 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
188 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
189 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800190 return true;
191 }
192
193 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
194 // for mixed blobs if this becomes an issue.
195 if (this->hasBitmap() && this->hasDistanceField()) {
196 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800197 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800198 return false;
199 }
200 return true;
201 }
202
203 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800204 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
205 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
206 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
207 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800208 return true;
209 }
210
211 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
212 // but only for integer translations.
213 // This cool bit of math will determine the necessary translation to apply to the already
214 // generated vertex coordinates to move them to the correct position
215 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800216 viewMatrix.getScaleX() * (x - fInitialX) +
217 viewMatrix.getSkewX() * (y - fInitialY) -
218 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800219 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800220 viewMatrix.getSkewY() * (x - fInitialX) +
221 viewMatrix.getScaleY() * (y - fInitialY) -
222 fInitialViewMatrix.getTranslateY();
223 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800224 return true;
225 }
joshualittfd5f6c12015-12-10 07:44:50 -0800226 } else if (this->hasDistanceField()) {
227 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
228 // distance field being generated, so we have to regenerate in those cases
229 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800230 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800231 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
232 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
233 return true;
234 }
joshualittfd5f6c12015-12-10 07:44:50 -0800235 }
236
joshualittfd5f6c12015-12-10 07:44:50 -0800237 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
238 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
239 // the blob anyways at flush time, so no need to regenerate explicitly
240 return false;
241}
242
Herb Derby86240592018-05-24 16:12:31 -0400243inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400244 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400245 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
246 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500247 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800248 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800249
Brian Salomon44acb5b2017-07-18 19:59:24 -0400250 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400251 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500252 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800253 if (info.drawAsDistanceFields()) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400254 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips5a66efb2018-03-07 15:13:18 -0500255 std::move(grPaint), glyphCount, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400256 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400257 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800258 } else {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500259 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400260 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800261 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500262 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800263 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400264 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800265 geometry.fBlob = SkRef(this);
266 geometry.fRun = run;
267 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500268 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400269 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800270 geometry.fX = x;
271 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500272 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400273 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800274}
275
joshualitt8e0ef292016-02-19 14:13:03 -0800276static void calculate_translation(bool applyVM,
277 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
278 const SkMatrix& currentViewMatrix, SkScalar currentX,
279 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
280 if (applyVM) {
281 *transX = newViewMatrix.getTranslateX() +
282 newViewMatrix.getScaleX() * (newX - currentX) +
283 newViewMatrix.getSkewX() * (newY - currentY) -
284 currentViewMatrix.getTranslateX();
285
286 *transY = newViewMatrix.getTranslateY() +
287 newViewMatrix.getSkewY() * (newX - currentX) +
288 newViewMatrix.getScaleY() * (newY - currentY) -
289 currentViewMatrix.getTranslateY();
290 } else {
291 *transX = newX - currentX;
292 *transY = newY - currentY;
293 }
294}
295
Herb Derby86240592018-05-24 16:12:31 -0400296void GrTextBlob::flush(GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500297 const GrDistanceFieldAdjustTable* distanceAdjustTable,
298 const GrTextUtils::Paint& paint, const GrClip& clip,
299 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
300 SkScalar x, SkScalar y) {
301
Herb Derby86240592018-05-24 16:12:31 -0400302 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500303 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
304 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Ben Wagnerd234afd2018-04-13 15:50:01 -0400305 GrTextUtils::RunPaint runPaint(&paint, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500306 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
307 Run& run = fRuns[runIndex];
308
309 // first flush any path glyphs
310 if (run.fPathGlyphs.count()) {
311 SkScalar transX, transY;
312 uint16_t paintFlags = run.fPaintFlags;
313 if (!runPaint.modifyForRun(
314 [paintFlags](SkPaint* p) {
315 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
316 })) {
317 continue;
318 }
319 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400320 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500321 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
322 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
323 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
324 SkMatrix pathMatrix;
325 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
326 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
327 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
328 }
joshualitt2e2202e2015-12-10 11:22:08 -0800329 }
joshualitt2e2202e2015-12-10 11:22:08 -0800330
Jim Van Verth54d9c882018-02-08 16:14:48 -0500331 // then flush each subrun, if any
332 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000333 continue;
334 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500335 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
336 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
337 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
338 int glyphCount = info.glyphCount();
339 if (0 == glyphCount) {
340 continue;
341 }
342
343 bool skipClip = false;
344 bool submitOp = true;
345 SkIRect clipRect = SkIRect::MakeEmpty();
346 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
347 SkRRect clipRRect;
348 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400349 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500350 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400351 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500352 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500353 clipRRect.isRect() && GrAA::kNo == aa) {
354 skipClip = true;
355 // We only need to do clipping work if the subrun isn't contained by the clip
356 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400357 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
358 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500359 if (!clipRRect.getBounds().contains(subRunBounds)) {
360 // If the subrun is completely outside, don't add an op for it
361 if (!clipRRect.getBounds().intersects(subRunBounds)) {
362 submitOp = false;
363 }
364 else {
365 clipRRect.getBounds().round(&clipRect);
366 }
367 }
368 }
369
370 if (submitOp) {
371 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
372 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500373 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500374 if (op) {
375 if (skipClip) {
376 target->addDrawOp(GrNoClip(), std::move(op));
377 }
378 else {
379 target->addDrawOp(clip, std::move(op));
380 }
381 }
382 }
383 }
384
Jim Van Verth89737de2018-02-06 21:30:20 +0000385 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000386}
387
Herb Derby86240592018-05-24 16:12:31 -0400388std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400389 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
390 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500391 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
Herb Derby86240592018-05-24 16:12:31 -0400392 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400393 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400394 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500395 distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800396}
joshualitt2e2202e2015-12-10 11:22:08 -0800397
Herb Derby86240592018-05-24 16:12:31 -0400398void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800399 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700400
joshualitt2f2ee832016-02-10 08:52:24 -0800401 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
402 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700403
joshualitt2f2ee832016-02-10 08:52:24 -0800404 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
405 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
406 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700407
joshualitt2f2ee832016-02-10 08:52:24 -0800408 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800409 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
410 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
411 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
412 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700413
joshualitt2f2ee832016-02-10 08:52:24 -0800414 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700415 for (int i = 0; i < l.fRunCount; i++) {
416 const Run& lRun = l.fRuns[i];
417 const Run& rRun = r.fRuns[i];
418
joshualitt259fbf12015-07-21 11:39:34 -0700419 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800420 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500421 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700422 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800423 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700424 }
425
joshualitt259fbf12015-07-21 11:39:34 -0700426
joshualitt2f2ee832016-02-10 08:52:24 -0800427 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
428 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700429 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700430
431 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800432 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
433 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700434 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
435 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700436 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800437 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700438 }
439
440 // color can be changed
441 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800442 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700443
joshualitt2f2ee832016-02-10 08:52:24 -0800444 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700445 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
446 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
447 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
448
joshualitt2f2ee832016-02-10 08:52:24 -0800449 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
450 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700451
joshualitt2f2ee832016-02-10 08:52:24 -0800452 if (lSubRun.strike()) {
453 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500454 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
455 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800456
457 } else {
458 SkASSERT_RELEASE(!rSubRun.strike());
459 }
460
461 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
462 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
463 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
464 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
465 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
466 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
467 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700468 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500469
470 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
471 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
472 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
473 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
474
475 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
476 // We can't assert that these have the same translations
477 }
joshualitt259fbf12015-07-21 11:39:34 -0700478 }
479}
joshualitt8e0ef292016-02-19 14:13:03 -0800480
Herb Derby86240592018-05-24 16:12:31 -0400481void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800482 SkScalar x, SkScalar y, SkScalar* transX,
483 SkScalar* transY) {
484 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
485 fCurrentViewMatrix, fX, fY, transX, transY);
486 fCurrentViewMatrix = viewMatrix;
487 fX = x;
488 fY = y;
489}