blob: 73c87215a9b0327f1ec770e64a4f4e6676c97d41 [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
8#include "GrAtlasTextBlob.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
Florin Malitac337c9e2017-03-10 18:02:29 +000022sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080023 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
24 // and size for the glyphIds array.
25 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
26 size_t size = sizeof(GrAtlasTextBlob) +
27 verticesCount +
28 glyphCount * sizeof(GrGlyph**) +
29 sizeof(GrAtlasTextBlob::Run) * runCount;
30
Robert Phillips303cd582018-02-14 18:54:01 -050031 void* allocation;
32 if (pool) {
33 allocation = pool->allocate(size);
34 } else {
35 allocation = ::operator new (size);
36 }
joshualitt92303772016-02-10 11:55:52 -080037 if (CACHE_SANITY_CHECK) {
38 sk_bzero(allocation, size);
39 }
40
Florin Malitac337c9e2017-03-10 18:02:29 +000041 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080042 cacheBlob->fSize = size;
43
44 // setup offsets for vertices / glyphs
Brian Salomon18923f92017-11-06 16:26:02 -050045 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080046 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
47 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
48
49 // Initialize runs
50 for (int i = 0; i < runCount; i++) {
51 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
52 }
53 cacheBlob->fRunCount = runCount;
54 cacheBlob->fPool = pool;
55 return cacheBlob;
56}
57
Herb Derby526819d2018-03-09 12:51:12 -050058SkExclusiveStrikePtr GrAtlasTextBlob::setupCache(int runIndex,
59 const SkSurfaceProps& props,
60 SkScalerContextFlags scalerContextFlags,
61 const SkPaint& skPaint,
62 const SkMatrix* viewMatrix) {
joshualitte76b4bb32015-12-28 07:23:58 -080063 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
64
65 // if we have an override descriptor for the run, then we should use that
66 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
67 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050069 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
70 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050071 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070072 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070073 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derby526819d2018-03-09 12:51:12 -050074 return SkGlyphCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *run->fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080075}
76
joshualittf528e0d2015-12-09 06:42:52 -080077void GrAtlasTextBlob::appendGlyph(int runIndex,
78 const SkRect& positions,
79 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050080 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080081 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070082 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050083 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050084 if (positions.isEmpty()) {
85 return;
86 }
joshualitta06e6ab2015-12-10 08:54:41 -080087
88 // If the glyph is too large we fall back to paths
89 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050090 if (nullptr == glyph->fPath) {
91 const SkPath* glyphPath = cache->findPath(skGlyph);
92 if (!glyphPath) {
93 return;
94 }
95
96 glyph->fPath = new SkPath(*glyphPath);
97 }
98 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -080099 return;
100 }
101
joshualittf528e0d2015-12-09 06:42:52 -0800102 Run& run = fRuns[runIndex];
103 GrMaskFormat format = glyph->fMaskFormat;
104
105 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
106 if (run.fInitialized && subRun->maskFormat() != format) {
107 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500108 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800109 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500110 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800111 }
112
113 run.fInitialized = true;
114
Brian Salomon5c6ac642017-12-19 11:09:32 -0500115 bool hasW = subRun->hasWCoord();
116 // DF glyphs drawn in perspective must always have a w coord.
117 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
118 // Non-DF glyphs should never have a w coord.
119 SkASSERT(!hasW || subRun->drawAsDistanceFields());
120
121 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800122
123 subRun->setMaskFormat(format);
124
joshualitt7481e752016-01-22 06:08:48 -0800125 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800126 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800127
128 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
129
Brian Salomon5c6ac642017-12-19 11:09:32 -0500130 // We always write the third position component used by SDFs. If it is unused it gets
131 // overwritten. Similarly, we always write the color and the blob will later overwrite it
132 // with texture coords if it is unused.
133 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
134 // V0
135 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
136 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
137 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800138
Brian Salomon5c6ac642017-12-19 11:09:32 -0500139 // V1
140 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
141 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
142 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800143
Brian Salomon5c6ac642017-12-19 11:09:32 -0500144 // V2
145 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
146 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
147 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800148
Brian Salomon5c6ac642017-12-19 11:09:32 -0500149 // V3
150 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
151 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800152
joshualitt18b072d2015-12-07 12:26:12 -0800153 subRun->appendVertices(vertexStride);
154 fGlyphs[subRun->glyphEndIndex()] = glyph;
155 subRun->glyphAppended();
Jim Van Verthcf838c72018-03-05 14:40:36 -0500156 subRun->setHasScaledGlyphs(SK_Scalar1 != scale);
joshualitt18b072d2015-12-07 12:26:12 -0800157}
158
Jim Van Verth54d9c882018-02-08 16:14:48 -0500159void GrAtlasTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
160 SkScalar scale, bool preTransformed) {
161 Run& run = fRuns[runIndex];
162 run.fPathGlyphs.push_back(GrAtlasTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800163}
164
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500165bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500166 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800167 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
168 // If we have LCD text then our canonical color will be set to transparent, in this case we have
169 // to regenerate the blob on any color change
170 // We use the grPaint to get any color filter effects
171 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400172 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800173 return true;
174 }
175
joshualitt8e0ef292016-02-19 14:13:03 -0800176 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800177 return true;
178 }
179
Brian Salomon5c6ac642017-12-19 11:09:32 -0500180 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800181 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800182 return true;
183 }
184
185 // We only cache one masked version
186 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400187 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800188 return true;
189 }
190
191 // Similarly, we only cache one version for each style
192 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500193 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
194 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
195 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800196 return true;
197 }
198
199 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
200 // for mixed blobs if this becomes an issue.
201 if (this->hasBitmap() && this->hasDistanceField()) {
202 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800203 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800204 return false;
205 }
206 return true;
207 }
208
209 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800210 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
211 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
212 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
213 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800214 return true;
215 }
216
217 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
218 // but only for integer translations.
219 // This cool bit of math will determine the necessary translation to apply to the already
220 // generated vertex coordinates to move them to the correct position
221 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800222 viewMatrix.getScaleX() * (x - fInitialX) +
223 viewMatrix.getSkewX() * (y - fInitialY) -
224 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800225 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800226 viewMatrix.getSkewY() * (x - fInitialX) +
227 viewMatrix.getScaleY() * (y - fInitialY) -
228 fInitialViewMatrix.getTranslateY();
229 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800230 return true;
231 }
joshualittfd5f6c12015-12-10 07:44:50 -0800232 } else if (this->hasDistanceField()) {
233 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
234 // distance field being generated, so we have to regenerate in those cases
235 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800236 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800237 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
238 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
239 return true;
240 }
joshualittfd5f6c12015-12-10 07:44:50 -0800241 }
242
joshualittfd5f6c12015-12-10 07:44:50 -0800243 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
244 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
245 // the blob anyways at flush time, so no need to regenerate explicitly
246 return false;
247}
248
Brian Salomonf18b1d82017-10-27 11:30:49 -0400249inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400250 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400251 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
252 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500253 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800254 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800255
Brian Salomon44acb5b2017-07-18 19:59:24 -0400256 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400257 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500258 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800259 if (info.drawAsDistanceFields()) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400260 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips5a66efb2018-03-07 15:13:18 -0500261 std::move(grPaint), glyphCount, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400262 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400263 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800264 } else {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500265 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500266 info.hasScaledGlyphs());
joshualitt2e2202e2015-12-10 11:22:08 -0800267 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500268 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800269 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400270 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800271 geometry.fBlob = SkRef(this);
272 geometry.fRun = run;
273 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500274 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400275 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800276 geometry.fX = x;
277 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500278 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400279 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800280}
281
joshualitt8e0ef292016-02-19 14:13:03 -0800282static void calculate_translation(bool applyVM,
283 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
284 const SkMatrix& currentViewMatrix, SkScalar currentX,
285 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
286 if (applyVM) {
287 *transX = newViewMatrix.getTranslateX() +
288 newViewMatrix.getScaleX() * (newX - currentX) +
289 newViewMatrix.getSkewX() * (newY - currentY) -
290 currentViewMatrix.getTranslateX();
291
292 *transY = newViewMatrix.getTranslateY() +
293 newViewMatrix.getSkewY() * (newX - currentX) +
294 newViewMatrix.getScaleY() * (newY - currentY) -
295 currentViewMatrix.getTranslateY();
296 } else {
297 *transX = newX - currentX;
298 *transY = newY - currentY;
299 }
300}
301
Robert Phillips5a66efb2018-03-07 15:13:18 -0500302void GrAtlasTextBlob::flush(GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500303 const GrDistanceFieldAdjustTable* distanceAdjustTable,
304 const GrTextUtils::Paint& paint, const GrClip& clip,
305 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
306 SkScalar x, SkScalar y) {
307
308 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
309 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
310 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
Ben Wagnerd234afd2018-04-13 15:50:01 -0400311 GrTextUtils::RunPaint runPaint(&paint, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500312 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
313 Run& run = fRuns[runIndex];
314
315 // first flush any path glyphs
316 if (run.fPathGlyphs.count()) {
317 SkScalar transX, transY;
318 uint16_t paintFlags = run.fPaintFlags;
319 if (!runPaint.modifyForRun(
320 [paintFlags](SkPaint* p) {
321 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
322 })) {
323 continue;
324 }
325 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
326 GrAtlasTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
327 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
328 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
329 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
330 SkMatrix pathMatrix;
331 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
332 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
333 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
334 }
joshualitt2e2202e2015-12-10 11:22:08 -0800335 }
joshualitt2e2202e2015-12-10 11:22:08 -0800336
Jim Van Verth54d9c882018-02-08 16:14:48 -0500337 // then flush each subrun, if any
338 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000339 continue;
340 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500341 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
342 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
343 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
344 int glyphCount = info.glyphCount();
345 if (0 == glyphCount) {
346 continue;
347 }
348
349 bool skipClip = false;
350 bool submitOp = true;
351 SkIRect clipRect = SkIRect::MakeEmpty();
352 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
353 SkRRect clipRRect;
354 GrAA aa;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500355 // We can clip geometrically if we're not using SDFs or scaled glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500356 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthcf838c72018-03-05 14:40:36 -0500357 if (!info.drawAsDistanceFields() && !info.hasScaledGlyphs() &&
358 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500359 clipRRect.isRect() && GrAA::kNo == aa) {
360 skipClip = true;
361 // We only need to do clipping work if the subrun isn't contained by the clip
362 SkRect subRunBounds;
363 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y);
364 if (!clipRRect.getBounds().contains(subRunBounds)) {
365 // If the subrun is completely outside, don't add an op for it
366 if (!clipRRect.getBounds().intersects(subRunBounds)) {
367 submitOp = false;
368 }
369 else {
370 clipRRect.getBounds().round(&clipRect);
371 }
372 }
373 }
374
375 if (submitOp) {
376 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
377 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500378 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500379 if (op) {
380 if (skipClip) {
381 target->addDrawOp(GrNoClip(), std::move(op));
382 }
383 else {
384 target->addDrawOp(clip, std::move(op));
385 }
386 }
387 }
388 }
389
Jim Van Verth89737de2018-02-06 21:30:20 +0000390 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000391}
392
Brian Salomon44acb5b2017-07-18 19:59:24 -0400393std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400394 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
395 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500396 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800397 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400398 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400399 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500400 distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800401}
joshualitt2e2202e2015-12-10 11:22:08 -0800402
joshualitt259fbf12015-07-21 11:39:34 -0700403void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800404 SkASSERT_RELEASE(l.fSize == r.fSize);
405 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700406
joshualitt2f2ee832016-02-10 08:52:24 -0800407 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
408 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700409
joshualitt2f2ee832016-02-10 08:52:24 -0800410 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
411 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
412 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700413
joshualitt2f2ee832016-02-10 08:52:24 -0800414 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800415 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
416 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
417 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
418 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700419
joshualitt2f2ee832016-02-10 08:52:24 -0800420 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700421 for (int i = 0; i < l.fRunCount; i++) {
422 const Run& lRun = l.fRuns[i];
423 const Run& rRun = r.fRuns[i];
424
joshualitt259fbf12015-07-21 11:39:34 -0700425 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500427 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700428 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800429 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700430 }
431
joshualitt259fbf12015-07-21 11:39:34 -0700432
joshualitt2f2ee832016-02-10 08:52:24 -0800433 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
434 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700435 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700436
437 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800438 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
439 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700440 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
441 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700442 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800443 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700444 }
445
446 // color can be changed
447 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800448 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700449
joshualitt2f2ee832016-02-10 08:52:24 -0800450 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700451 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
452 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
453 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
454
joshualitt2f2ee832016-02-10 08:52:24 -0800455 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
456 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700457
joshualitt2f2ee832016-02-10 08:52:24 -0800458 if (lSubRun.strike()) {
459 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500460 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
461 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800462
463 } else {
464 SkASSERT_RELEASE(!rSubRun.strike());
465 }
466
467 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
468 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
469 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
470 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
471 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
472 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
473 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700474 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500475
476 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
477 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
478 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
479 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
480
481 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
482 // We can't assert that these have the same translations
483 }
joshualitt259fbf12015-07-21 11:39:34 -0700484 }
485}
joshualitt8e0ef292016-02-19 14:13:03 -0800486
487void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
488 SkScalar x, SkScalar y, SkScalar* transX,
489 SkScalar* transY) {
490 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
491 fCurrentViewMatrix, fX, fY, transX, transY);
492 fCurrentViewMatrix = viewMatrix;
493 fX = x;
494 fY = y;
495}