blob: bd3d76814fd60b9cbfb77ca23788204006a9d369 [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"
joshualitt2e2202e2015-12-10 11:22:08 -080017#include "SkTextBlobRunIterator.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050018#include "SkTextToPathIter.h"
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080020
Florin Malitac337c9e2017-03-10 18:02:29 +000021sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080022 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
23 // and size for the glyphIds array.
24 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
25 size_t size = sizeof(GrAtlasTextBlob) +
26 verticesCount +
27 glyphCount * sizeof(GrGlyph**) +
28 sizeof(GrAtlasTextBlob::Run) * runCount;
29
Robert Phillips303cd582018-02-14 18:54:01 -050030 void* allocation;
31 if (pool) {
32 allocation = pool->allocate(size);
33 } else {
34 allocation = ::operator new (size);
35 }
joshualitt92303772016-02-10 11:55:52 -080036 if (CACHE_SANITY_CHECK) {
37 sk_bzero(allocation, size);
38 }
39
Florin Malitac337c9e2017-03-10 18:02:29 +000040 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080041 cacheBlob->fSize = size;
42
43 // setup offsets for vertices / glyphs
Brian Salomon18923f92017-11-06 16:26:02 -050044 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080045 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
46 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
47
48 // Initialize runs
49 for (int i = 0; i < runCount; i++) {
50 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
51 }
52 cacheBlob->fRunCount = runCount;
53 cacheBlob->fPool = pool;
54 return cacheBlob;
55}
56
joshualitte76b4bb32015-12-28 07:23:58 -080057SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
58 const SkSurfaceProps& props,
Herb Derbyd8327a82018-01-22 14:39:27 -050059 SkScalerContextFlags scalerContextFlags,
joshualitte76b4bb32015-12-28 07:23:58 -080060 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080061 const SkMatrix* viewMatrix) {
joshualitte76b4bb32015-12-28 07:23:58 -080062 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
63
64 // if we have an override descriptor for the run, then we should use that
65 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
66 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070067 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050068 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
69 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
joshualitte76b4bb32015-12-28 07:23:58 -080070 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070071 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070072 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050073 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb32015-12-28 07:23:58 -080074}
75
joshualittf528e0d2015-12-09 06:42:52 -080076void GrAtlasTextBlob::appendGlyph(int runIndex,
77 const SkRect& positions,
78 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050079 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080080 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070081 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050082 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050083 if (positions.isEmpty()) {
84 return;
85 }
joshualitta06e6ab2015-12-10 08:54:41 -080086
87 // If the glyph is too large we fall back to paths
88 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050089 if (nullptr == glyph->fPath) {
90 const SkPath* glyphPath = cache->findPath(skGlyph);
91 if (!glyphPath) {
92 return;
93 }
94
95 glyph->fPath = new SkPath(*glyphPath);
96 }
97 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -080098 return;
99 }
100
joshualittf528e0d2015-12-09 06:42:52 -0800101 Run& run = fRuns[runIndex];
102 GrMaskFormat format = glyph->fMaskFormat;
103
104 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
105 if (run.fInitialized && subRun->maskFormat() != format) {
106 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500107 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800108 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500109 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800110 }
111
112 run.fInitialized = true;
113
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114 bool hasW = subRun->hasWCoord();
115 // DF glyphs drawn in perspective must always have a w coord.
116 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
117 // Non-DF glyphs should never have a w coord.
118 SkASSERT(!hasW || subRun->drawAsDistanceFields());
119
120 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800121
122 subRun->setMaskFormat(format);
123
joshualitt7481e752016-01-22 06:08:48 -0800124 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800125 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800126
127 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
128
Brian Salomon5c6ac642017-12-19 11:09:32 -0500129 // We always write the third position component used by SDFs. If it is unused it gets
130 // overwritten. Similarly, we always write the color and the blob will later overwrite it
131 // with texture coords if it is unused.
132 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
133 // V0
134 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 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 // V1
139 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 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 // V2
144 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
145 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
146 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800147
Brian Salomon5c6ac642017-12-19 11:09:32 -0500148 // V3
149 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
150 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800151
joshualitt18b072d2015-12-07 12:26:12 -0800152 subRun->appendVertices(vertexStride);
153 fGlyphs[subRun->glyphEndIndex()] = glyph;
154 subRun->glyphAppended();
Jim Van Verthcf838c72018-03-05 14:40:36 -0500155 subRun->setHasScaledGlyphs(SK_Scalar1 != scale);
joshualitt18b072d2015-12-07 12:26:12 -0800156}
157
Jim Van Verth54d9c882018-02-08 16:14:48 -0500158void GrAtlasTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
159 SkScalar scale, bool preTransformed) {
160 Run& run = fRuns[runIndex];
161 run.fPathGlyphs.push_back(GrAtlasTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800162}
163
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500164bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500165 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800166 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
167 // If we have LCD text then our canonical color will be set to transparent, in this case we have
168 // to regenerate the blob on any color change
169 // We use the grPaint to get any color filter effects
170 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400171 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800172 return true;
173 }
174
joshualitt8e0ef292016-02-19 14:13:03 -0800175 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800176 return true;
177 }
178
Brian Salomon5c6ac642017-12-19 11:09:32 -0500179 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800180 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800181 return true;
182 }
183
184 // We only cache one masked version
185 if (fKey.fHasBlur &&
186 (fBlurRec.fSigma != blurRec.fSigma ||
187 fBlurRec.fStyle != blurRec.fStyle ||
188 fBlurRec.fQuality != blurRec.fQuality)) {
189 return true;
190 }
191
192 // Similarly, we only cache one version for each style
193 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500194 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
195 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
196 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800197 return true;
198 }
199
200 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
201 // for mixed blobs if this becomes an issue.
202 if (this->hasBitmap() && this->hasDistanceField()) {
203 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800204 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800205 return false;
206 }
207 return true;
208 }
209
210 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800211 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
212 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
213 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
214 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800215 return true;
216 }
217
218 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
219 // but only for integer translations.
220 // This cool bit of math will determine the necessary translation to apply to the already
221 // generated vertex coordinates to move them to the correct position
222 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800223 viewMatrix.getScaleX() * (x - fInitialX) +
224 viewMatrix.getSkewX() * (y - fInitialY) -
225 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800226 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800227 viewMatrix.getSkewY() * (x - fInitialX) +
228 viewMatrix.getScaleY() * (y - fInitialY) -
229 fInitialViewMatrix.getTranslateY();
230 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800231 return true;
232 }
joshualittfd5f6c12015-12-10 07:44:50 -0800233 } else if (this->hasDistanceField()) {
234 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
235 // distance field being generated, so we have to regenerate in those cases
236 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800237 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800238 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
239 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
240 return true;
241 }
joshualittfd5f6c12015-12-10 07:44:50 -0800242 }
243
joshualittfd5f6c12015-12-10 07:44:50 -0800244 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
245 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
246 // the blob anyways at flush time, so no need to regenerate explicitly
247 return false;
248}
249
Brian Salomonf18b1d82017-10-27 11:30:49 -0400250inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400251 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400252 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
253 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500254 const GrDistanceFieldAdjustTable* distanceAdjustTable,
255 GrRestrictedAtlasManager* restrictedAtlasManager, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800256 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800257
Brian Salomon44acb5b2017-07-18 19:59:24 -0400258 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400259 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500260 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800261 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800262 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400263 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500264 std::move(grPaint), glyphCount, restrictedAtlasManager, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400265 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400266 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800267 } else {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500268 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount,
269 info.hasScaledGlyphs(), restrictedAtlasManager);
joshualitt2e2202e2015-12-10 11:22:08 -0800270 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500271 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800272 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400273 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800274 geometry.fBlob = SkRef(this);
275 geometry.fRun = run;
276 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500277 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400278 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800279 geometry.fX = x;
280 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500281 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400282 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800283}
284
joshualitt8e0ef292016-02-19 14:13:03 -0800285static void calculate_translation(bool applyVM,
286 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
287 const SkMatrix& currentViewMatrix, SkScalar currentX,
288 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
289 if (applyVM) {
290 *transX = newViewMatrix.getTranslateX() +
291 newViewMatrix.getScaleX() * (newX - currentX) +
292 newViewMatrix.getSkewX() * (newY - currentY) -
293 currentViewMatrix.getTranslateX();
294
295 *transY = newViewMatrix.getTranslateY() +
296 newViewMatrix.getSkewY() * (newX - currentX) +
297 newViewMatrix.getScaleY() * (newY - currentY) -
298 currentViewMatrix.getTranslateY();
299 } else {
300 *transX = newX - currentX;
301 *transY = newY - currentY;
302 }
303}
304
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500305void GrAtlasTextBlob::flush(GrRestrictedAtlasManager* restrictedAtlasManager,
306 GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500307 const GrDistanceFieldAdjustTable* distanceAdjustTable,
308 const GrTextUtils::Paint& paint, const GrClip& clip,
309 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
310 SkScalar x, SkScalar y) {
311
312 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
313 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
314 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
315 GrTextUtils::RunPaint runPaint(&paint, nullptr, props);
316 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
317 Run& run = fRuns[runIndex];
318
319 // first flush any path glyphs
320 if (run.fPathGlyphs.count()) {
321 SkScalar transX, transY;
322 uint16_t paintFlags = run.fPaintFlags;
323 if (!runPaint.modifyForRun(
324 [paintFlags](SkPaint* p) {
325 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
326 })) {
327 continue;
328 }
329 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
330 GrAtlasTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
331 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
332 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
333 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
334 SkMatrix pathMatrix;
335 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
336 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
337 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
338 }
joshualitt2e2202e2015-12-10 11:22:08 -0800339 }
joshualitt2e2202e2015-12-10 11:22:08 -0800340
Jim Van Verth54d9c882018-02-08 16:14:48 -0500341 // then flush each subrun, if any
342 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000343 continue;
344 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500345 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
346 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
347 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
348 int glyphCount = info.glyphCount();
349 if (0 == glyphCount) {
350 continue;
351 }
352
353 bool skipClip = false;
354 bool submitOp = true;
355 SkIRect clipRect = SkIRect::MakeEmpty();
356 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
357 SkRRect clipRRect;
358 GrAA aa;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500359 // We can clip geometrically if we're not using SDFs or scaled glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500360 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthcf838c72018-03-05 14:40:36 -0500361 if (!info.drawAsDistanceFields() && !info.hasScaledGlyphs() &&
362 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500363 clipRRect.isRect() && GrAA::kNo == aa) {
364 skipClip = true;
365 // We only need to do clipping work if the subrun isn't contained by the clip
366 SkRect subRunBounds;
367 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y);
368 if (!clipRRect.getBounds().contains(subRunBounds)) {
369 // If the subrun is completely outside, don't add an op for it
370 if (!clipRRect.getBounds().intersects(subRunBounds)) {
371 submitOp = false;
372 }
373 else {
374 clipRRect.getBounds().round(&clipRect);
375 }
376 }
377 }
378
379 if (submitOp) {
380 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
381 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500382 restrictedAtlasManager, target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500383 if (op) {
384 if (skipClip) {
385 target->addDrawOp(GrNoClip(), std::move(op));
386 }
387 else {
388 target->addDrawOp(clip, std::move(op));
389 }
390 }
391 }
392 }
393
Jim Van Verth89737de2018-02-06 21:30:20 +0000394 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000395}
396
Brian Salomon44acb5b2017-07-18 19:59:24 -0400397std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400398 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
399 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500400 const GrDistanceFieldAdjustTable* distanceAdjustTable,
401 GrRestrictedAtlasManager* restrictedAtlasManager, GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800402 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400403 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400404 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500405 distanceAdjustTable, restrictedAtlasManager, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800406}
joshualitt2e2202e2015-12-10 11:22:08 -0800407
joshualitt259fbf12015-07-21 11:39:34 -0700408void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800409 SkASSERT_RELEASE(l.fSize == r.fSize);
410 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700411
joshualitt2f2ee832016-02-10 08:52:24 -0800412 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
413 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
414 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700415
joshualitt2f2ee832016-02-10 08:52:24 -0800416 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
417 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
418 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700419
joshualitt2f2ee832016-02-10 08:52:24 -0800420 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800421 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
422 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
423 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
424 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700425
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700427 for (int i = 0; i < l.fRunCount; i++) {
428 const Run& lRun = l.fRuns[i];
429 const Run& rRun = r.fRuns[i];
430
joshualitt259fbf12015-07-21 11:39:34 -0700431 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800432 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500433 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700434 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800435 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700436 }
437
joshualitt259fbf12015-07-21 11:39:34 -0700438
joshualitt2f2ee832016-02-10 08:52:24 -0800439 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
440 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700441 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700442
443 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800444 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
445 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700446 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
447 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700448 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800449 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700450 }
451
452 // color can be changed
453 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800454 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700455
joshualitt2f2ee832016-02-10 08:52:24 -0800456 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700457 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
458 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
459 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
460
joshualitt2f2ee832016-02-10 08:52:24 -0800461 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
462 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700463
joshualitt2f2ee832016-02-10 08:52:24 -0800464 if (lSubRun.strike()) {
465 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500466 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
467 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800468
469 } else {
470 SkASSERT_RELEASE(!rSubRun.strike());
471 }
472
473 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
474 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
475 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
476 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
477 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
478 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
479 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700480 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500481
482 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
483 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
484 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
485 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
486
487 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
488 // We can't assert that these have the same translations
489 }
joshualitt259fbf12015-07-21 11:39:34 -0700490 }
491}
joshualitt8e0ef292016-02-19 14:13:03 -0800492
493void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
494 SkScalar x, SkScalar y, SkScalar* transX,
495 SkScalar* transY) {
496 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
497 fCurrentViewMatrix, fX, fY, transX, transY);
498 fCurrentViewMatrix = viewMatrix;
499 fX = x;
500 fY = y;
501}