OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "GrAtlasTextContext.h" | 7 #include "GrAtlasTextContext.h" |
8 | 8 |
9 #include "GrBatch.h" | 9 #include "GrBatch.h" |
10 #include "GrBatchFontCache.h" | 10 #include "GrBatchFontCache.h" |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 337 |
338 inline SkGlyphCache* GrAtlasTextContext::setupCache(BitmapTextBlob::Run* run, | 338 inline SkGlyphCache* GrAtlasTextContext::setupCache(BitmapTextBlob::Run* run, |
339 const SkPaint& skPaint, | 339 const SkPaint& skPaint, |
340 const SkMatrix* viewMatrix, | 340 const SkMatrix* viewMatrix, |
341 bool noGamma) { | 341 bool noGamma) { |
342 skPaint.getScalerContextDescriptor(&run->fDescriptor, &fDeviceProperties, vi
ewMatrix, noGamma); | 342 skPaint.getScalerContextDescriptor(&run->fDescriptor, &fDeviceProperties, vi
ewMatrix, noGamma); |
343 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); | 343 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
344 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc())
; | 344 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc())
; |
345 } | 345 } |
346 | 346 |
347 void GrAtlasTextContext::drawTextBlob(SkGpuDevice* gpuDevice, GrRenderTarget* rt
, | 347 void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt, |
348 const GrClip& clip, const SkPaint& skPaint
, | 348 const GrClip& clip, const SkPaint& skPaint
, |
349 const SkMatrix& viewMatrix, const SkTextBl
ob* blob, | 349 const SkMatrix& viewMatrix, const SkTextBl
ob* blob, |
350 SkScalar x, SkScalar y, | 350 SkScalar x, SkScalar y, |
351 SkDrawFilter* drawFilter, const SkIRect& c
lipBounds) { | 351 SkDrawFilter* drawFilter, const SkIRect& c
lipBounds) { |
352 // If we have been abandoned, then don't draw | 352 // If we have been abandoned, then don't draw |
353 if (fContext->abandoned()) { | 353 if (fContext->abandoned()) { |
354 return; | 354 return; |
355 } | 355 } |
356 | 356 |
357 GrDrawContext* drawContext = fContext->drawContext(); | 357 GrDrawContext* drawContext = fContext->drawContext(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 404 } |
405 | 405 |
406 if (cacheBlob) { | 406 if (cacheBlob) { |
407 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, blurRec, v
iewMatrix, x, y)) { | 407 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, blurRec, v
iewMatrix, x, y)) { |
408 // We have to remake the blob because changes may invalidate our mas
ks. | 408 // We have to remake the blob because changes may invalidate our mas
ks. |
409 // TODO we could probably get away reuse most of the time if the poi
nter is unique, | 409 // TODO we could probably get away reuse most of the time if the poi
nter is unique, |
410 // but we'd have to clear the subrun information | 410 // but we'd have to clear the subrun information |
411 fCache->remove(cacheBlob); | 411 fCache->remove(cacheBlob); |
412 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, s
kPaint, | 412 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, s
kPaint, |
413 kGrayTextVASize))); | 413 kGrayTextVASize))); |
414 this->regenerateTextBlob(gpuDevice, cacheBlob, skPaint, grPaint.getC
olor(), viewMatrix, | 414 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), vie
wMatrix, |
415 blob, x, y, drawFilter, clipRect, rt, clip,
grPaint); | 415 blob, x, y, drawFilter, clipRect, rt, clip,
grPaint); |
416 } else { | 416 } else { |
417 // If we can reuse the blob, then make sure we update the blob's vie
wmatrix, and x/y | 417 // If we can reuse the blob, then make sure we update the blob's vie
wmatrix, and x/y |
418 // offsets | 418 // offsets |
419 cacheBlob->fViewMatrix = viewMatrix; | 419 cacheBlob->fViewMatrix = viewMatrix; |
420 cacheBlob->fX = x; | 420 cacheBlob->fX = x; |
421 cacheBlob->fY = y; | 421 cacheBlob->fY = y; |
422 fCache->makeMRU(cacheBlob); | 422 fCache->makeMRU(cacheBlob); |
423 } | 423 } |
424 } else { | 424 } else { |
425 if (canCache) { | 425 if (canCache) { |
426 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, s
kPaint, | 426 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, s
kPaint, |
427 kGrayTextVASize))); | 427 kGrayTextVASize))); |
428 } else { | 428 } else { |
429 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize)); | 429 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize)); |
430 } | 430 } |
431 this->regenerateTextBlob(gpuDevice, cacheBlob, skPaint, grPaint.getColor
(), viewMatrix, | 431 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMat
rix, |
432 blob, x, y, drawFilter, clipRect, rt, clip, grP
aint); | 432 blob, x, y, drawFilter, clipRect, rt, clip, grP
aint); |
433 } | 433 } |
434 | 434 |
435 cacheBlob->fPaintColor = skPaint.getColor(); | 435 cacheBlob->fPaintColor = skPaint.getColor(); |
436 this->flush(gpuDevice, drawContext, blob, cacheBlob, rt, skPaint, grPaint, d
rawFilter, | 436 this->flush(drawContext, blob, cacheBlob, rt, skPaint, grPaint, drawFilter, |
437 clip, viewMatrix, clipBounds, x, y, transX, transY); | 437 clip, viewMatrix, clipBounds, x, y, transX, transY); |
438 } | 438 } |
439 | 439 |
440 inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, | 440 inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, |
441 const SkMatrix& viewMatr
ix) { | 441 const SkMatrix& viewMatr
ix) { |
442 // TODO: support perspective (need getMaxScale replacement) | 442 // TODO: support perspective (need getMaxScale replacement) |
443 if (viewMatrix.hasPerspective()) { | 443 if (viewMatrix.hasPerspective()) { |
444 return false; | 444 return false; |
445 } | 445 } |
446 | 446 |
(...skipping 18 matching lines...) Expand all Loading... |
465 } | 465 } |
466 | 466 |
467 // TODO: add some stroking support | 467 // TODO: add some stroking support |
468 if (skPaint.getStyle() != SkPaint::kFill_Style) { | 468 if (skPaint.getStyle() != SkPaint::kFill_Style) { |
469 return false; | 469 return false; |
470 } | 470 } |
471 | 471 |
472 return true; | 472 return true; |
473 } | 473 } |
474 | 474 |
475 void GrAtlasTextContext::regenerateTextBlob(SkGpuDevice* gpuDevice, BitmapTextBl
ob* cacheBlob, | 475 void GrAtlasTextContext::regenerateTextBlob(BitmapTextBlob* cacheBlob, |
476 const SkPaint& skPaint, GrColor colo
r, | 476 const SkPaint& skPaint, GrColor colo
r, |
477 const SkMatrix& viewMatrix, | 477 const SkMatrix& viewMatrix, |
478 const SkTextBlob* blob, SkScalar x,
SkScalar y, | 478 const SkTextBlob* blob, SkScalar x,
SkScalar y, |
479 SkDrawFilter* drawFilter, const SkIR
ect& clipRect, | 479 SkDrawFilter* drawFilter, const SkIR
ect& clipRect, |
480 GrRenderTarget* rt, const GrClip& cl
ip, | 480 GrRenderTarget* rt, const GrClip& cl
ip, |
481 const GrPaint& paint) { | 481 const GrPaint& paint) { |
482 cacheBlob->fViewMatrix = viewMatrix; | 482 cacheBlob->fViewMatrix = viewMatrix; |
483 cacheBlob->fX = x; | 483 cacheBlob->fX = x; |
484 cacheBlob->fY = y; | 484 cacheBlob->fY = y; |
485 | 485 |
486 // Regenerate textblob | 486 // Regenerate textblob |
487 SkPaint runPaint = skPaint; | 487 SkPaint runPaint = skPaint; |
488 SkTextBlob::RunIterator it(blob); | 488 SkTextBlob::RunIterator it(blob); |
489 for (int run = 0; !it.done(); it.next(), run++) { | 489 for (int run = 0; !it.done(); it.next(), run++) { |
490 int glyphCount = it.glyphCount(); | 490 int glyphCount = it.glyphCount(); |
491 size_t textLen = glyphCount * sizeof(uint16_t); | 491 size_t textLen = glyphCount * sizeof(uint16_t); |
492 const SkPoint& offset = it.offset(); | 492 const SkPoint& offset = it.offset(); |
493 // applyFontToPaint() always overwrites the exact same attributes, | 493 // applyFontToPaint() always overwrites the exact same attributes, |
494 // so it is safe to not re-seed the paint for this reason. | 494 // so it is safe to not re-seed the paint for this reason. |
495 it.applyFontToPaint(&runPaint); | 495 it.applyFontToPaint(&runPaint); |
496 | 496 |
497 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { | 497 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Typ
e)) { |
498 // A false return from filter() means we should abort the current dr
aw. | 498 // A false return from filter() means we should abort the current dr
aw. |
499 runPaint = skPaint; | 499 runPaint = skPaint; |
500 continue; | 500 continue; |
501 } | 501 } |
502 | 502 |
503 runPaint.setFlags(gpuDevice->filterTextFlags(runPaint)); | 503 runPaint.setFlags(FilterTextFlags(fDeviceProperties, runPaint)); |
504 | 504 |
505 // setup vertex / glyphIndex for the new run | 505 // setup vertex / glyphIndex for the new run |
506 if (run > 0) { | 506 if (run > 0) { |
507 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back(); | 507 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back(); |
508 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back(
); | 508 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back(
); |
509 | 509 |
510 newRun.fVertexStartIndex = lastRun.fVertexEndIndex; | 510 newRun.fVertexStartIndex = lastRun.fVertexEndIndex; |
511 newRun.fVertexEndIndex = lastRun.fVertexEndIndex; | 511 newRun.fVertexEndIndex = lastRun.fVertexEndIndex; |
512 | 512 |
513 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex; | 513 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex; |
(...skipping 1522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2036 | 2036 |
2037 // Distance field properties | 2037 // Distance field properties |
2038 SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable; | 2038 SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable; |
2039 SkColor fFilteredColor; | 2039 SkColor fFilteredColor; |
2040 bool fUseDistanceFields; | 2040 bool fUseDistanceFields; |
2041 bool fUseLCDText; | 2041 bool fUseLCDText; |
2042 bool fUseBGR; | 2042 bool fUseBGR; |
2043 float fGamma; | 2043 float fGamma; |
2044 }; | 2044 }; |
2045 | 2045 |
2046 void GrAtlasTextContext::flushRunAsPaths(SkGpuDevice* gpuDevice, GrDrawContext*
drawContext, | 2046 void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* drawContext, |
2047 GrRenderTarget* rt, const SkTextBlob::R
unIterator& it, | 2047 GrRenderTarget* rt, const SkTextBlob::R
unIterator& it, |
2048 const GrClip& clip, const SkPaint& skPa
int, | 2048 const GrClip& clip, const SkPaint& skPa
int, |
2049 SkDrawFilter* drawFilter, const SkMatri
x& viewMatrix, | 2049 SkDrawFilter* drawFilter, const SkMatri
x& viewMatrix, |
2050 const SkIRect& clipBounds, SkScalar x,
SkScalar y) { | 2050 const SkIRect& clipBounds, SkScalar x,
SkScalar y) { |
2051 SkPaint runPaint = skPaint; | 2051 SkPaint runPaint = skPaint; |
2052 | 2052 |
2053 size_t textLen = it.glyphCount() * sizeof(uint16_t); | 2053 size_t textLen = it.glyphCount() * sizeof(uint16_t); |
2054 const SkPoint& offset = it.offset(); | 2054 const SkPoint& offset = it.offset(); |
2055 | 2055 |
2056 it.applyFontToPaint(&runPaint); | 2056 it.applyFontToPaint(&runPaint); |
2057 | 2057 |
2058 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type))
{ | 2058 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type))
{ |
2059 return; | 2059 return; |
2060 } | 2060 } |
2061 | 2061 |
2062 runPaint.setFlags(gpuDevice->filterTextFlags(runPaint)); | 2062 runPaint.setFlags(FilterTextFlags(fDeviceProperties, runPaint)); |
2063 | 2063 |
2064 switch (it.positioning()) { | 2064 switch (it.positioning()) { |
2065 case SkTextBlob::kDefault_Positioning: | 2065 case SkTextBlob::kDefault_Positioning: |
2066 this->drawTextAsPath(drawContext, rt, clip, runPaint, viewMatrix, | 2066 this->drawTextAsPath(drawContext, rt, clip, runPaint, viewMatrix, |
2067 (const char *)it.glyphs(), | 2067 (const char *)it.glyphs(), |
2068 textLen, x + offset.x(), y + offset.y(), clipBo
unds); | 2068 textLen, x + offset.x(), y + offset.y(), clipBo
unds); |
2069 break; | 2069 break; |
2070 case SkTextBlob::kHorizontal_Positioning: | 2070 case SkTextBlob::kHorizontal_Positioning: |
2071 this->drawPosTextAsPath(drawContext, rt, clip, runPaint, viewMatrix, | 2071 this->drawPosTextAsPath(drawContext, rt, clip, runPaint, viewMatrix, |
2072 (const char*)it.glyphs(), | 2072 (const char*)it.glyphs(), |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2165 bigGlyph.fVx += transX; | 2165 bigGlyph.fVx += transX; |
2166 bigGlyph.fVy += transY; | 2166 bigGlyph.fVy += transY; |
2167 SkMatrix translate = cacheBlob->fViewMatrix; | 2167 SkMatrix translate = cacheBlob->fViewMatrix; |
2168 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy); | 2168 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy); |
2169 | 2169 |
2170 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, big
Glyph.fPath, | 2170 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, big
Glyph.fPath, |
2171 skPaint, translate, &pathMatrix, cli
pBounds, false); | 2171 skPaint, translate, &pathMatrix, cli
pBounds, false); |
2172 } | 2172 } |
2173 } | 2173 } |
2174 | 2174 |
2175 void GrAtlasTextContext::flush(SkGpuDevice* gpuDevice, | 2175 void GrAtlasTextContext::flush(GrDrawContext* drawContext, |
2176 GrDrawContext* drawContext, | |
2177 const SkTextBlob* blob, | 2176 const SkTextBlob* blob, |
2178 BitmapTextBlob* cacheBlob, | 2177 BitmapTextBlob* cacheBlob, |
2179 GrRenderTarget* rt, | 2178 GrRenderTarget* rt, |
2180 const SkPaint& skPaint, | 2179 const SkPaint& skPaint, |
2181 const GrPaint& grPaint, | 2180 const GrPaint& grPaint, |
2182 SkDrawFilter* drawFilter, | 2181 SkDrawFilter* drawFilter, |
2183 const GrClip& clip, | 2182 const GrClip& clip, |
2184 const SkMatrix& viewMatrix, | 2183 const SkMatrix& viewMatrix, |
2185 const SkIRect& clipBounds, | 2184 const SkIRect& clipBounds, |
2186 SkScalar x, SkScalar y, | 2185 SkScalar x, SkScalar y, |
2187 SkScalar transX, SkScalar transY) { | 2186 SkScalar transX, SkScalar transY) { |
2188 // We loop through the runs of the blob, flushing each. If any run is too l
arge, then we flush | 2187 // We loop through the runs of the blob, flushing each. If any run is too l
arge, then we flush |
2189 // it as paths | 2188 // it as paths |
2190 GrPipelineBuilder pipelineBuilder; | 2189 GrPipelineBuilder pipelineBuilder; |
2191 pipelineBuilder.setFromPaint(grPaint, rt, clip); | 2190 pipelineBuilder.setFromPaint(grPaint, rt, clip); |
2192 | 2191 |
2193 GrColor color = grPaint.getColor(); | 2192 GrColor color = grPaint.getColor(); |
2194 | 2193 |
2195 SkTextBlob::RunIterator it(blob); | 2194 SkTextBlob::RunIterator it(blob); |
2196 for (int run = 0; !it.done(); it.next(), run++) { | 2195 for (int run = 0; !it.done(); it.next(), run++) { |
2197 if (cacheBlob->fRuns[run].fDrawAsPaths) { | 2196 if (cacheBlob->fRuns[run].fDrawAsPaths) { |
2198 this->flushRunAsPaths(gpuDevice, drawContext, rt, it, clip, skPaint, | 2197 this->flushRunAsPaths(drawContext, rt, it, clip, skPaint, |
2199 drawFilter, viewMatrix, clipBounds, x, y); | 2198 drawFilter, viewMatrix, clipBounds, x, y); |
2200 continue; | 2199 continue; |
2201 } | 2200 } |
2202 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); | 2201 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); |
2203 this->flushRun(drawContext, &pipelineBuilder, cacheBlob, run, color, | 2202 this->flushRun(drawContext, &pipelineBuilder, cacheBlob, run, color, |
2204 transX, transY, skPaint); | 2203 transX, transY, skPaint); |
2205 } | 2204 } |
2206 | 2205 |
2207 // Now flush big glyphs | 2206 // Now flush big glyphs |
2208 this->flushBigGlyphs(cacheBlob, drawContext, rt, clip, skPaint, transX, tran
sY, clipBounds); | 2207 this->flushBigGlyphs(cacheBlob, drawContext, rt, clip, skPaint, transX, tran
sY, clipBounds); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2285 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMat
rix, text, | 2284 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMat
rix, text, |
2286 static_cast<size_t>(textLen), 0, 0,
noClip)); | 2285 static_cast<size_t>(textLen), 0, 0,
noClip)); |
2287 | 2286 |
2288 SkScalar transX = static_cast<SkScalar>(random->nextU()); | 2287 SkScalar transX = static_cast<SkScalar>(random->nextU()); |
2289 SkScalar transY = static_cast<SkScalar>(random->nextU()); | 2288 SkScalar transY = static_cast<SkScalar>(random->nextU()); |
2290 const GrAtlasTextContext::BitmapTextBlob::Run::SubRunInfo& info = blob->fRun
s[0].fSubRunInfo[0]; | 2289 const GrAtlasTextContext::BitmapTextBlob::Run::SubRunInfo& info = blob->fRun
s[0].fSubRunInfo[0]; |
2291 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, t
ransY, skPaint); | 2290 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, t
ransY, skPaint); |
2292 } | 2291 } |
2293 | 2292 |
2294 #endif | 2293 #endif |
OLD | NEW |