blob: 321c4a6750ff976b464fa40f9e46e06c1b167775 [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"
10#include "GrContext.h"
bsalomonbb243832016-07-22 07:10:19 -070011#include "GrPipelineBuilder.h"
Brian Salomon344ec422016-12-15 10:58:41 -050012#include "GrRenderTargetContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080013#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "SkColorFilter.h"
15#include "SkDrawFilter.h"
joshualitte76b4bb2015-12-28 07:23:58 -080016#include "SkGlyphCache.h"
joshualitt2e2202e2015-12-10 11:22:08 -080017#include "SkTextBlobRunIterator.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080019
joshualitt92303772016-02-10 11:55:52 -080020GrAtlasTextBlob* GrAtlasTextBlob::Create(GrMemoryPool* pool, int glyphCount, int runCount) {
21 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
22 // and size for the glyphIds array.
23 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
24 size_t size = sizeof(GrAtlasTextBlob) +
25 verticesCount +
26 glyphCount * sizeof(GrGlyph**) +
27 sizeof(GrAtlasTextBlob::Run) * runCount;
28
29 void* allocation = pool->allocate(size);
30 if (CACHE_SANITY_CHECK) {
31 sk_bzero(allocation, size);
32 }
33
34 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob;
35 cacheBlob->fSize = size;
36
37 // setup offsets for vertices / glyphs
38 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
39 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
40 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
41
42 // Initialize runs
43 for (int i = 0; i < runCount; i++) {
44 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
45 }
46 cacheBlob->fRunCount = runCount;
47 cacheBlob->fPool = pool;
48 return cacheBlob;
49}
50
joshualitte76b4bb2015-12-28 07:23:58 -080051SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
52 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070053 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080054 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080055 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080056 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
57
58 // if we have an override descriptor for the run, then we should use that
59 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
60 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070061 SkScalerContextEffects effects;
62 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
65 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
66 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050067 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080068}
69
joshualittf528e0d2015-12-09 06:42:52 -080070void GrAtlasTextBlob::appendGlyph(int runIndex,
71 const SkRect& positions,
72 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050073 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080074 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070075 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050076 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -080077
78 // If the glyph is too large we fall back to paths
79 if (glyph->fTooLargeForAtlas) {
Jim Van Verth08576e62016-11-16 10:15:23 -050080 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, treatAsBMP);
joshualitta06e6ab2015-12-10 08:54:41 -080081 return;
82 }
83
joshualittf528e0d2015-12-09 06:42:52 -080084 Run& run = fRuns[runIndex];
85 GrMaskFormat format = glyph->fMaskFormat;
86
87 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
88 if (run.fInitialized && subRun->maskFormat() != format) {
89 subRun = &run.push_back();
90 subRun->setStrike(strike);
91 } else if (!run.fInitialized) {
92 subRun->setStrike(strike);
93 }
94
95 run.fInitialized = true;
96
97 size_t vertexStride = GetVertexStride(format);
98
99 subRun->setMaskFormat(format);
100
joshualitt7481e752016-01-22 06:08:48 -0800101 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800102 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800103
104 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
105
joshualittf528e0d2015-12-09 06:42:52 -0800106 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800107 // V0
108 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
109 position->set(positions.fLeft, positions.fTop);
110 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
111 *colorPtr = color;
112 vertex += vertexStride;
113
114 // V1
115 position = reinterpret_cast<SkPoint*>(vertex);
116 position->set(positions.fLeft, positions.fBottom);
117 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
118 *colorPtr = color;
119 vertex += vertexStride;
120
121 // V2
122 position = reinterpret_cast<SkPoint*>(vertex);
123 position->set(positions.fRight, positions.fBottom);
124 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
125 *colorPtr = color;
126 vertex += vertexStride;
127
128 // V3
129 position = reinterpret_cast<SkPoint*>(vertex);
130 position->set(positions.fRight, positions.fTop);
131 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
132 *colorPtr = color;
133 } else {
134 // V0
135 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
136 position->set(positions.fLeft, positions.fTop);
137 vertex += vertexStride;
138
139 // V1
140 position = reinterpret_cast<SkPoint*>(vertex);
141 position->set(positions.fLeft, positions.fBottom);
142 vertex += vertexStride;
143
144 // V2
145 position = reinterpret_cast<SkPoint*>(vertex);
146 position->set(positions.fRight, positions.fBottom);
147 vertex += vertexStride;
148
149 // V3
150 position = reinterpret_cast<SkPoint*>(vertex);
151 position->set(positions.fRight, positions.fTop);
152 }
153 subRun->appendVertices(vertexStride);
154 fGlyphs[subRun->glyphEndIndex()] = glyph;
155 subRun->glyphAppended();
156}
157
bsalomonc2878e22016-05-17 13:18:03 -0700158void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500159 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800160 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700161 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800162 if (!glyphPath) {
163 return;
164 }
165
166 glyph->fPath = new SkPath(*glyphPath);
167 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500168 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800169}
170
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500171bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
172 const SkMaskFilter::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800173 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
174 // If we have LCD text then our canonical color will be set to transparent, in this case we have
175 // to regenerate the blob on any color change
176 // We use the grPaint to get any color filter effects
177 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500178 fFilteredPaintColor != paint.filteredSkColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800179 return true;
180 }
181
joshualitt8e0ef292016-02-19 14:13:03 -0800182 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800183 return true;
184 }
185
joshualitt8e0ef292016-02-19 14:13:03 -0800186 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800187 return true;
188 }
189
190 // We only cache one masked version
191 if (fKey.fHasBlur &&
192 (fBlurRec.fSigma != blurRec.fSigma ||
193 fBlurRec.fStyle != blurRec.fStyle ||
194 fBlurRec.fQuality != blurRec.fQuality)) {
195 return true;
196 }
197
198 // Similarly, we only cache one version for each style
199 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500200 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
201 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
202 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800203 return true;
204 }
205
206 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
207 // for mixed blobs if this becomes an issue.
208 if (this->hasBitmap() && this->hasDistanceField()) {
209 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800210 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800211 return false;
212 }
213 return true;
214 }
215
216 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800217 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
218 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
219 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
220 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800221 return true;
222 }
223
224 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
225 // but only for integer translations.
226 // This cool bit of math will determine the necessary translation to apply to the already
227 // generated vertex coordinates to move them to the correct position
228 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800229 viewMatrix.getScaleX() * (x - fInitialX) +
230 viewMatrix.getSkewX() * (y - fInitialY) -
231 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800232 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800233 viewMatrix.getSkewY() * (x - fInitialX) +
234 viewMatrix.getScaleY() * (y - fInitialY) -
235 fInitialViewMatrix.getTranslateY();
236 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800237 return true;
238 }
joshualittfd5f6c12015-12-10 07:44:50 -0800239 } else if (this->hasDistanceField()) {
240 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
241 // distance field being generated, so we have to regenerate in those cases
242 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800243 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800244 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
245 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
246 return true;
247 }
joshualittfd5f6c12015-12-10 07:44:50 -0800248 }
249
joshualittfd5f6c12015-12-10 07:44:50 -0800250 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
251 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
252 // the blob anyways at flush time, so no need to regenerate explicitly
253 return false;
254}
255
Brian Salomonf8334782017-01-03 09:42:58 -0500256inline std::unique_ptr<GrDrawOp> GrAtlasTextBlob::makeOp(
Brian Salomon344ec422016-12-15 10:58:41 -0500257 const Run::SubRunInfo& info, int glyphCount, int run, int subRun,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500258 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const GrTextUtils::Paint& paint,
Brian Salomon344ec422016-12-15 10:58:41 -0500259 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500260 bool useGammaCorrectDistanceTable, GrAtlasGlyphCache* cache) {
joshualitt2e2202e2015-12-10 11:22:08 -0800261 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800262
Brian Salomonf8334782017-01-03 09:42:58 -0500263 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800264 if (info.drawAsDistanceFields()) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500265 SkColor filteredColor = paint.filteredSkColor();
joshualitt2e2202e2015-12-10 11:22:08 -0800266 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon09d994e2016-12-21 11:14:46 -0500267 op = GrAtlasTextOp::MakeDistanceField(glyphCount, cache, distanceAdjustTable,
268 useGammaCorrectDistanceTable, filteredColor,
269 info.hasUseLCDText(), useBGR);
joshualitt2e2202e2015-12-10 11:22:08 -0800270 } else {
Brian Salomon09d994e2016-12-21 11:14:46 -0500271 op = GrAtlasTextOp::MakeBitmap(format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800272 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500273 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800274 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800275 geometry.fBlob = SkRef(this);
276 geometry.fRun = run;
277 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500278 geometry.fColor =
279 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulGrColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800280 geometry.fX = x;
281 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500282 op->init();
joshualitt2e2202e2015-12-10 11:22:08 -0800283
Brian Salomon09d994e2016-12-21 11:14:46 -0500284 return std::move(op);
joshualitt2e2202e2015-12-10 11:22:08 -0800285}
286
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500287inline void GrAtlasTextBlob::flushRun(GrRenderTargetContext* rtc, const GrClip& clip, int run,
288 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
289 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500290 const GrDistanceFieldAdjustTable* distanceAdjustTable,
291 GrAtlasGlyphCache* cache) {
292 int lastRun = fRuns[run].fSubRunInfo.count() - 1;
293 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800294 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500295 GrPaint grPaint;
296 if (!paint.toGrPaint(info.maskFormat(), rtc, viewMatrix, &grPaint)) {
297 continue;
298 }
joshualitt2e2202e2015-12-10 11:22:08 -0800299 int glyphCount = info.glyphCount();
300 if (0 == glyphCount) {
301 continue;
302 }
303
Brian Salomonf8334782017-01-03 09:42:58 -0500304 std::unique_ptr<GrDrawOp> op(this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500305 paint, props, distanceAdjustTable,
Brian Salomonf8334782017-01-03 09:42:58 -0500306 rtc->isGammaCorrect(), cache));
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500307 GrPipelineBuilder pipelineBuilder(std::move(grPaint), GrAAType::kNone);
bsalomonbb243832016-07-22 07:10:19 -0700308
Brian Salomon24f19782016-12-13 15:10:11 -0500309 rtc->addDrawOp(pipelineBuilder, clip, std::move(op));
joshualitt2e2202e2015-12-10 11:22:08 -0800310 }
311}
312
joshualitt8e0ef292016-02-19 14:13:03 -0800313static void calculate_translation(bool applyVM,
314 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
315 const SkMatrix& currentViewMatrix, SkScalar currentX,
316 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
317 if (applyVM) {
318 *transX = newViewMatrix.getTranslateX() +
319 newViewMatrix.getScaleX() * (newX - currentX) +
320 newViewMatrix.getSkewX() * (newY - currentY) -
321 currentViewMatrix.getTranslateX();
322
323 *transY = newViewMatrix.getTranslateY() +
324 newViewMatrix.getSkewY() * (newX - currentX) +
325 newViewMatrix.getScaleY() * (newY - currentY) -
326 currentViewMatrix.getTranslateY();
327 } else {
328 *transX = newX - currentX;
329 *transY = newY - currentY;
330 }
331}
332
Brian Osman11052242016-10-27 14:47:55 -0400333void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrRenderTargetContext* rtc,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500334 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800335 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800336 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800337 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800338 for (int i = 0; i < fBigGlyphs.count(); i++) {
339 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500340 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800341 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800342 SkMatrix ctm;
343 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800344 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500345 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800346 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800347 }
348
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500349 GrBlurUtils::drawPathWithMaskFilter(context, rtc, clip, bigGlyph.fPath, paint, ctm, nullptr,
350 clipBounds, false);
joshualitt2e2202e2015-12-10 11:22:08 -0800351 }
352}
353
Brian Osman11052242016-10-27 14:47:55 -0400354void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrRenderTargetContext* rtc,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500355 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
356 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt2e2202e2015-12-10 11:22:08 -0800357 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
358 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800359 size_t textLen = it.glyphCount() * sizeof(uint16_t);
360 const SkPoint& offset = it.offset();
361
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500362 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
363 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800364 return;
365 }
366
joshualitt2e2202e2015-12-10 11:22:08 -0800367 switch (it.positioning()) {
368 case SkTextBlob::kDefault_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400369 GrTextUtils::DrawTextAsPath(context, rtc, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500370 (const char*)it.glyphs(), textLen, x + offset.x(),
371 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800372 break;
373 case SkTextBlob::kHorizontal_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400374 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500375 (const char*)it.glyphs(), textLen, it.pos(), 1,
376 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800377 break;
378 case SkTextBlob::kFull_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400379 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500380 (const char*)it.glyphs(), textLen, it.pos(), 2,
381 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800382 break;
383 }
384}
385
Brian Salomon82f44312017-01-11 13:42:54 -0500386void GrAtlasTextBlob::flushCached(GrContext* context, GrRenderTargetContext* rtc,
387 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800388 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500389 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
390 const GrClip& clip, const SkMatrix& viewMatrix,
391 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800392 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
393 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800394 SkTextBlobRunIterator it(blob);
395 for (int run = 0; !it.done(); it.next(), run++) {
396 if (fRuns[run].fDrawAsPaths) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500397 this->flushRunAsPaths(context, rtc, props, it, clip, paint, drawFilter, viewMatrix,
398 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800399 continue;
400 }
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500401 this->flushRun(rtc, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
402 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800403 }
404
405 // Now flush big glyphs
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500406 this->flushBigGlyphs(context, rtc, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800407}
408
Brian Salomon82f44312017-01-11 13:42:54 -0500409void GrAtlasTextBlob::flushThrowaway(GrContext* context, GrRenderTargetContext* rtc,
joshualitt2e2202e2015-12-10 11:22:08 -0800410 const SkSurfaceProps& props,
411 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500412 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500413 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800414 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800415 for (int run = 0; run < fRunCount; run++) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500416 this->flushRun(rtc, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon82f44312017-01-11 13:42:54 -0500417 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800418 }
419
420 // Now flush big glyphs
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500421 this->flushBigGlyphs(context, rtc, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800422}
423
Brian Salomonf8334782017-01-03 09:42:58 -0500424std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
425 int glyphCount, int run, int subRun, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500426 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf8334782017-01-03 09:42:58 -0500427 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800428 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500429 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, paint, props,
Brian Salomon344ec422016-12-15 10:58:41 -0500430 distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800431}
joshualitt2e2202e2015-12-10 11:22:08 -0800432
joshualitt259fbf12015-07-21 11:39:34 -0700433void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800434 SkASSERT_RELEASE(l.fSize == r.fSize);
435 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700436
joshualitt2f2ee832016-02-10 08:52:24 -0800437 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
438 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
439 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700440
joshualitt2f2ee832016-02-10 08:52:24 -0800441 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
442 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
443 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700444
joshualitt2f2ee832016-02-10 08:52:24 -0800445 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700446 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
447 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
448 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
449
joshualitt2f2ee832016-02-10 08:52:24 -0800450 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700451 // We can't assert that these have the same translations
452 }
453
joshualitt2f2ee832016-02-10 08:52:24 -0800454 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800455 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
456 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
457 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
458 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700459
joshualitt2f2ee832016-02-10 08:52:24 -0800460 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700461 for (int i = 0; i < l.fRunCount; i++) {
462 const Run& lRun = l.fRuns[i];
463 const Run& rRun = r.fRuns[i];
464
joshualitt259fbf12015-07-21 11:39:34 -0700465 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800466 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500467 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700468 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800469 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700470 }
471
joshualitt259fbf12015-07-21 11:39:34 -0700472
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
474 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700475 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700476
477 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800478 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
479 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700480 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
481 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700482 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800483 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700484 }
485
486 // color can be changed
487 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800488 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
489 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700490
joshualitt2f2ee832016-02-10 08:52:24 -0800491 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700492 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
493 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
494 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
495
joshualitt2f2ee832016-02-10 08:52:24 -0800496 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
497 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700498
joshualitt2f2ee832016-02-10 08:52:24 -0800499 if (lSubRun.strike()) {
500 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500501 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
502 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800503
504 } else {
505 SkASSERT_RELEASE(!rSubRun.strike());
506 }
507
508 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
509 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
510 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
511 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
512 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
513 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
514 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700515 }
516 }
517}
joshualitt8e0ef292016-02-19 14:13:03 -0800518
519void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
520 SkScalar x, SkScalar y, SkScalar* transX,
521 SkScalar* transY) {
522 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
523 fCurrentViewMatrix, fX, fY, transX, transY);
524 fCurrentViewMatrix = viewMatrix;
525 fX = x;
526 fY = y;
527}