blob: 50ce0f8f8375deda2f78774baa893601a480ca75 [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"
9
joshualitt2e2202e2015-12-10 11:22:08 -080010#include "GrBlurUtils.h"
11#include "GrContext.h"
12#include "GrDrawContext.h"
13#include "SkColorFilter.h"
14#include "SkDrawFilter.h"
15#include "SkTextBlobRunIterator.h"
16#include "batches/GrAtlasTextBatch.h"
17
joshualittf528e0d2015-12-09 06:42:52 -080018void GrAtlasTextBlob::appendGlyph(int runIndex,
19 const SkRect& positions,
20 GrColor color,
21 GrBatchTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080022 GrGlyph* glyph,
23 GrFontScaler* scaler, const SkGlyph& skGlyph,
24 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
25
26 // If the glyph is too large we fall back to paths
27 if (glyph->fTooLargeForAtlas) {
28 this->appendLargeGlyph(glyph, scaler, skGlyph, x, y, scale, applyVM);
29 return;
30 }
31
joshualittf528e0d2015-12-09 06:42:52 -080032 Run& run = fRuns[runIndex];
33 GrMaskFormat format = glyph->fMaskFormat;
34
35 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
36 if (run.fInitialized && subRun->maskFormat() != format) {
37 subRun = &run.push_back();
38 subRun->setStrike(strike);
39 } else if (!run.fInitialized) {
40 subRun->setStrike(strike);
41 }
42
43 run.fInitialized = true;
44
45 size_t vertexStride = GetVertexStride(format);
46
47 subRun->setMaskFormat(format);
48
49 run.fVertexBounds.joinNonEmptyArg(positions);
joshualittf9e658b2015-12-09 09:26:44 -080050 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -080051
52 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
53
joshualittf528e0d2015-12-09 06:42:52 -080054 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -080055 // V0
56 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
57 position->set(positions.fLeft, positions.fTop);
58 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
59 *colorPtr = color;
60 vertex += vertexStride;
61
62 // V1
63 position = reinterpret_cast<SkPoint*>(vertex);
64 position->set(positions.fLeft, positions.fBottom);
65 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
66 *colorPtr = color;
67 vertex += vertexStride;
68
69 // V2
70 position = reinterpret_cast<SkPoint*>(vertex);
71 position->set(positions.fRight, positions.fBottom);
72 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
73 *colorPtr = color;
74 vertex += vertexStride;
75
76 // V3
77 position = reinterpret_cast<SkPoint*>(vertex);
78 position->set(positions.fRight, positions.fTop);
79 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
80 *colorPtr = color;
81 } else {
82 // V0
83 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
84 position->set(positions.fLeft, positions.fTop);
85 vertex += vertexStride;
86
87 // V1
88 position = reinterpret_cast<SkPoint*>(vertex);
89 position->set(positions.fLeft, positions.fBottom);
90 vertex += vertexStride;
91
92 // V2
93 position = reinterpret_cast<SkPoint*>(vertex);
94 position->set(positions.fRight, positions.fBottom);
95 vertex += vertexStride;
96
97 // V3
98 position = reinterpret_cast<SkPoint*>(vertex);
99 position->set(positions.fRight, positions.fTop);
100 }
101 subRun->appendVertices(vertexStride);
102 fGlyphs[subRun->glyphEndIndex()] = glyph;
103 subRun->glyphAppended();
104}
105
joshualitta06e6ab2015-12-10 08:54:41 -0800106void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, GrFontScaler* scaler, const SkGlyph& skGlyph,
107 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
108 if (nullptr == glyph->fPath) {
109 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
110 if (!glyphPath) {
111 return;
112 }
113
114 glyph->fPath = new SkPath(*glyphPath);
115 }
116 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
117}
118
joshualittfd5f6c12015-12-10 07:44:50 -0800119bool GrAtlasTextBlob::mustRegenerate(SkScalar* outTransX, SkScalar* outTransY,
120 const SkPaint& paint,
121 GrColor color, const SkMaskFilter::BlurRec& blurRec,
122 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
123 // If we have LCD text then our canonical color will be set to transparent, in this case we have
124 // to regenerate the blob on any color change
125 // We use the grPaint to get any color filter effects
126 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
127 fPaintColor != color) {
128 return true;
129 }
130
131 if (fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
132 return true;
133 }
134
135 if (fViewMatrix.hasPerspective() && !fViewMatrix.cheapEqualTo(viewMatrix)) {
136 return true;
137 }
138
139 // We only cache one masked version
140 if (fKey.fHasBlur &&
141 (fBlurRec.fSigma != blurRec.fSigma ||
142 fBlurRec.fStyle != blurRec.fStyle ||
143 fBlurRec.fQuality != blurRec.fQuality)) {
144 return true;
145 }
146
147 // Similarly, we only cache one version for each style
148 if (fKey.fStyle != SkPaint::kFill_Style &&
149 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
150 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
151 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
152 return true;
153 }
154
155 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
156 // for mixed blobs if this becomes an issue.
157 if (this->hasBitmap() && this->hasDistanceField()) {
158 // Identical viewmatrices and we can reuse in all cases
159 if (fViewMatrix.cheapEqualTo(viewMatrix) && x == fX && y == fY) {
160 return false;
161 }
162 return true;
163 }
164
165 if (this->hasBitmap()) {
166 if (fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
167 fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
168 fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
169 fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
170 return true;
171 }
172
173 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
174 // but only for integer translations.
175 // This cool bit of math will determine the necessary translation to apply to the already
176 // generated vertex coordinates to move them to the correct position
177 SkScalar transX = viewMatrix.getTranslateX() +
178 viewMatrix.getScaleX() * (x - fX) +
179 viewMatrix.getSkewX() * (y - fY) -
180 fViewMatrix.getTranslateX();
181 SkScalar transY = viewMatrix.getTranslateY() +
182 viewMatrix.getSkewY() * (x - fX) +
183 viewMatrix.getScaleY() * (y - fY) -
184 fViewMatrix.getTranslateY();
185 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
186 return true;
187 }
188
189 (*outTransX) = transX;
190 (*outTransY) = transY;
191 } else if (this->hasDistanceField()) {
192 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
193 // distance field being generated, so we have to regenerate in those cases
194 SkScalar newMaxScale = viewMatrix.getMaxScale();
195 SkScalar oldMaxScale = fViewMatrix.getMaxScale();
196 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
197 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
198 return true;
199 }
200
201 (*outTransX) = x - fX;
202 (*outTransY) = y - fY;
203 }
204
205
206 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
207 // offsets. Note, we offset the vertex bounds right before flushing
208 fViewMatrix = viewMatrix;
209 fX = x;
210 fY = y;
211
212 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
213 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
214 // the blob anyways at flush time, so no need to regenerate explicitly
215 return false;
216}
217
joshualitt2e2202e2015-12-10 11:22:08 -0800218GrDrawBatch* GrAtlasTextBlob::createBatch(const Run::SubRunInfo& info,
219 int glyphCount, int run, int subRun,
220 GrColor color, SkScalar transX, SkScalar transY,
221 const SkPaint& skPaint, const SkSurfaceProps& props,
222 const GrDistanceFieldAdjustTable* distanceAdjustTable,
223 GrBatchFontCache* cache) {
224 GrMaskFormat format = info.maskFormat();
225 GrColor subRunColor;
226 if (kARGB_GrMaskFormat == format) {
227 uint8_t paintAlpha = skPaint.getAlpha();
228 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
229 } else {
230 subRunColor = color;
231 }
232
233 GrAtlasTextBatch* batch;
234 if (info.drawAsDistanceFields()) {
235 SkColor filteredColor;
236 SkColorFilter* colorFilter = skPaint.getColorFilter();
237 if (colorFilter) {
238 filteredColor = colorFilter->filterColor(skPaint.getColor());
239 } else {
240 filteredColor = skPaint.getColor();
241 }
242 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
243 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, cache,
244 distanceAdjustTable, filteredColor,
245 info.hasUseLCDText(), useBGR);
246 } else {
247 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
248 }
249 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
250 geometry.fBlob = SkRef(this);
251 geometry.fRun = run;
252 geometry.fSubRun = subRun;
253 geometry.fColor = subRunColor;
254 geometry.fTransX = transX;
255 geometry.fTransY = transY;
256 batch->init();
257
258 return batch;
259}
260
261inline
262void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
263 int run, GrColor color,
264 SkScalar transX, SkScalar transY,
265 const SkPaint& skPaint, const SkSurfaceProps& props,
266 const GrDistanceFieldAdjustTable* distanceAdjustTable,
267 GrBatchFontCache* cache) {
268 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
269 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
270 int glyphCount = info.glyphCount();
271 if (0 == glyphCount) {
272 continue;
273 }
274
275 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
276 subRun, color, transX, transY,
277 skPaint, props,
278 distanceAdjustTable, cache));
279 dc->drawBatch(pipelineBuilder, batch);
280 }
281}
282
283void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
284 const GrClip& clip, const SkPaint& skPaint,
285 SkScalar transX, SkScalar transY,
286 const SkIRect& clipBounds) {
287 for (int i = 0; i < fBigGlyphs.count(); i++) {
288 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
289 bigGlyph.fVx += transX;
290 bigGlyph.fVy += transY;
291 SkMatrix ctm;
292 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
293 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
294 if (bigGlyph.fApplyVM) {
295 ctm.postConcat(fViewMatrix);
296 }
297
298 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
299 skPaint, ctm, nullptr, clipBounds, false);
300 }
301}
302
303void GrAtlasTextBlob::flushRunAsPaths(GrDrawContext* dc,
304 GrTextContext* textContext,
305 const SkSurfaceProps& props,
306 const SkTextBlobRunIterator& it,
307 const GrClip& clip, const SkPaint& skPaint,
308 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
309 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
310 SkPaint runPaint = skPaint;
311
312 size_t textLen = it.glyphCount() * sizeof(uint16_t);
313 const SkPoint& offset = it.offset();
314
315 it.applyFontToPaint(&runPaint);
316
317 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
318 return;
319 }
320
321 runPaint.setFlags(GrTextContext::FilterTextFlags(props, runPaint));
322
323 switch (it.positioning()) {
324 case SkTextBlob::kDefault_Positioning:
325 textContext->drawTextAsPath(dc, clip, runPaint, viewMatrix,
326 (const char *)it.glyphs(),
327 textLen, x + offset.x(), y + offset.y(), clipBounds);
328 break;
329 case SkTextBlob::kHorizontal_Positioning:
330 textContext->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
331 (const char*)it.glyphs(),
332 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
333 clipBounds);
334 break;
335 case SkTextBlob::kFull_Positioning:
336 textContext->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
337 (const char*)it.glyphs(),
338 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
339 break;
340 }
341}
342
343void GrAtlasTextBlob::flushCached(const SkTextBlob* blob,
344 GrContext* context,
345 GrDrawContext* dc,
346 GrTextContext* textContext,
347 const SkSurfaceProps& props,
348 const GrDistanceFieldAdjustTable* distanceAdjustTable,
349 const SkPaint& skPaint,
350 const GrPaint& grPaint,
351 SkDrawFilter* drawFilter,
352 const GrClip& clip,
353 const SkMatrix& viewMatrix,
354 const SkIRect& clipBounds,
355 SkScalar x, SkScalar y,
356 SkScalar transX, SkScalar transY) {
357 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
358 // it as paths
359 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
360
361 GrColor color = grPaint.getColor();
362
363 SkTextBlobRunIterator it(blob);
364 for (int run = 0; !it.done(); it.next(), run++) {
365 if (fRuns[run].fDrawAsPaths) {
366 this->flushRunAsPaths(dc, textContext, props, it, clip, skPaint,
367 drawFilter, viewMatrix, clipBounds, x, y);
368 continue;
369 }
370 fRuns[run].fVertexBounds.offset(transX, transY);
371 this->flushRun(dc, &pipelineBuilder, run, color,
372 transX, transY, skPaint, props,
373 distanceAdjustTable, context->getBatchFontCache());
374 }
375
376 // Now flush big glyphs
377 this->flushBigGlyphs(context, dc, clip, skPaint, transX, transY, clipBounds);
378}
379
380void GrAtlasTextBlob::flushThrowaway(GrContext* context,
381 GrDrawContext* dc,
382 const SkSurfaceProps& props,
383 const GrDistanceFieldAdjustTable* distanceAdjustTable,
384 const SkPaint& skPaint,
385 const GrPaint& grPaint,
386 const GrClip& clip,
387 const SkIRect& clipBounds) {
388 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
389
390 GrColor color = grPaint.getColor();
391 for (int run = 0; run < fRunCount; run++) {
392 this->flushRun(dc, &pipelineBuilder, run, color, 0, 0, skPaint, props,
393 distanceAdjustTable, context->getBatchFontCache());
394 }
395
396 // Now flush big glyphs
397 this->flushBigGlyphs(context, dc, clip, skPaint, 0, 0, clipBounds);
398}
399
400
joshualitt18b072d2015-12-07 12:26:12 -0800401// TODO get this code building again
joshualitt259fbf12015-07-21 11:39:34 -0700402#ifdef CACHE_SANITY_CHECK
403void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
404 SkASSERT(l.fSize == r.fSize);
405 SkASSERT(l.fPool == r.fPool);
406
407 SkASSERT(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
408 SkASSERT(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
409 SkASSERT(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
410
411 SkASSERT(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
412 SkASSERT(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
413 SkASSERT(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
414
415 SkASSERT(l.fBigGlyphs.count() == r.fBigGlyphs.count());
416 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
417 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
418 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
419
420 SkASSERT(lBigGlyph.fPath == rBigGlyph.fPath);
421 // We can't assert that these have the same translations
422 }
423
424 SkASSERT(l.fKey == r.fKey);
425 SkASSERT(l.fViewMatrix.cheapEqualTo(r.fViewMatrix));
426 SkASSERT(l.fPaintColor == r.fPaintColor);
427 SkASSERT(l.fMaxMinScale == r.fMaxMinScale);
428 SkASSERT(l.fMinMaxScale == r.fMinMaxScale);
429 SkASSERT(l.fTextType == r.fTextType);
430
431 SkASSERT(l.fRunCount == r.fRunCount);
432 for (int i = 0; i < l.fRunCount; i++) {
433 const Run& lRun = l.fRuns[i];
434 const Run& rRun = r.fRuns[i];
435
436 if (lRun.fStrike.get()) {
437 SkASSERT(rRun.fStrike.get());
438 SkASSERT(GrBatchTextStrike::GetKey(*lRun.fStrike) ==
439 GrBatchTextStrike::GetKey(*rRun.fStrike));
440
441 } else {
442 SkASSERT(!rRun.fStrike.get());
443 }
444
445 if (lRun.fTypeface.get()) {
446 SkASSERT(rRun.fTypeface.get());
447 SkASSERT(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
448 } else {
449 SkASSERT(!rRun.fTypeface.get());
450 }
451
joshualitt7e7b5c52015-07-21 12:56:56 -0700452 // We offset bounds right before flush time so they will not be correct here
joshualitt259fbf12015-07-21 11:39:34 -0700453 //SkASSERT(lRun.fVertexBounds == rRun.fVertexBounds);
454
455 SkASSERT(lRun.fDescriptor.getDesc());
456 SkASSERT(rRun.fDescriptor.getDesc());
457 SkASSERT(lRun.fDescriptor.getDesc()->equals(*rRun.fDescriptor.getDesc()));
458
459 if (lRun.fOverrideDescriptor.get()) {
460 SkASSERT(lRun.fOverrideDescriptor->getDesc());
461 SkASSERT(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());;
462 SkASSERT(lRun.fOverrideDescriptor->getDesc()->equals(
463 *rRun.fOverrideDescriptor->getDesc()));
464 } else {
465 SkASSERT(!rRun.fOverrideDescriptor.get());
466 }
467
468 // color can be changed
469 //SkASSERT(lRun.fColor == rRun.fColor);
470 SkASSERT(lRun.fInitialized == rRun.fInitialized);
471 SkASSERT(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
472
473 SkASSERT(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
474 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
475 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
476 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
477
478 SkASSERT(lSubRun.fVertexStartIndex == rSubRun.fVertexStartIndex);
479 SkASSERT(lSubRun.fVertexEndIndex == rSubRun.fVertexEndIndex);
480 SkASSERT(lSubRun.fGlyphStartIndex == rSubRun.fGlyphStartIndex);
481 SkASSERT(lSubRun.fGlyphEndIndex == rSubRun.fGlyphEndIndex);
482 SkASSERT(lSubRun.fTextRatio == rSubRun.fTextRatio);
483 SkASSERT(lSubRun.fMaskFormat == rSubRun.fMaskFormat);
484 SkASSERT(lSubRun.fDrawAsDistanceFields == rSubRun.fDrawAsDistanceFields);
485 SkASSERT(lSubRun.fUseLCDText == rSubRun.fUseLCDText);
486
487 //We can't compare the bulk use tokens with this method
488 /*
489 SkASSERT(lSubRun.fBulkUseToken.fPlotsToUpdate.count() ==
490 rSubRun.fBulkUseToken.fPlotsToUpdate.count());
491 SkASSERT(lSubRun.fBulkUseToken.fPlotAlreadyUpdated ==
492 rSubRun.fBulkUseToken.fPlotAlreadyUpdated);
493 for (int k = 0; k < lSubRun.fBulkUseToken.fPlotsToUpdate.count(); k++) {
494 SkASSERT(lSubRun.fBulkUseToken.fPlotsToUpdate[k] ==
495 rSubRun.fBulkUseToken.fPlotsToUpdate[k]);
496 }*/
497 }
498 }
499}
500
501#endif