reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 8 | #include "GrTextContext.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 10 | #include "GrFontScaler.h" |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame^] | 11 | #include "GrTextUtils.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 12 | |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 13 | #include "SkDrawFilter.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 14 | #include "SkGlyphCache.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 15 | #include "SkGrPriv.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 16 | #include "SkTextBlobRunIterator.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 18 | GrTextContext::GrTextContext(GrContext* context, const SkSurfaceProps& surfaceProps) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | : fFallbackTextContext(nullptr) |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 20 | , fContext(context) |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 21 | , fSurfaceProps(surfaceProps) { |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 22 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 23 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 24 | GrTextContext::~GrTextContext() { |
| 25 | delete fFallbackTextContext; |
| 26 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 27 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 28 | void GrTextContext::drawText(GrDrawContext* dc, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 29 | const GrClip& clip, const GrPaint& paint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 30 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 31 | const char text[], size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 32 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 33 | if (fContext->abandoned()) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 37 | GrTextContext* textContext = this; |
| 38 | do { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 39 | if (textContext->canDraw(skPaint, viewMatrix)) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 40 | textContext->onDrawText(dc, clip, paint, skPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 41 | text, byteLength, x, y, clipBounds); |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 42 | return; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 43 | } |
| 44 | textContext = textContext->fFallbackTextContext; |
| 45 | } while (textContext); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 46 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 47 | // fall back to drawing as a path |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame^] | 48 | GrTextUtils::DrawTextAsPath(fContext, dc, clip, skPaint, viewMatrix, text, byteLength, x, y, |
| 49 | clipBounds); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 50 | } |
| 51 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 52 | void GrTextContext::drawPosText(GrDrawContext* dc, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 53 | const GrClip& clip, const GrPaint& paint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 54 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 55 | const char text[], size_t byteLength, |
| 56 | const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 57 | const SkPoint& offset, const SkIRect& clipBounds) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 58 | if (fContext->abandoned()) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 62 | GrTextContext* textContext = this; |
| 63 | do { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 64 | if (textContext->canDraw(skPaint, viewMatrix)) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 65 | textContext->onDrawPosText(dc, clip, paint, skPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 66 | text, byteLength, pos, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 67 | scalarsPerPosition, offset, clipBounds); |
| 68 | return; |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 69 | } |
| 70 | textContext = textContext->fFallbackTextContext; |
| 71 | } while (textContext); |
| 72 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 73 | // fall back to drawing as a path |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame^] | 74 | GrTextUtils::DrawPosTextAsPath(fContext, dc, fSurfaceProps, clip, skPaint, viewMatrix, text, |
| 75 | byteLength, pos, scalarsPerPosition, offset, clipBounds); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 78 | bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) { |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 79 | if (!SkXfermode::AsMode(paint.getXfermode(), nullptr) || |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 80 | paint.getMaskFilter() || |
| 81 | paint.getRasterizer() || |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 82 | paint.getPathEffect() || |
| 83 | paint.isFakeBoldText() || |
| 84 | paint.getStyle() != SkPaint::kFill_Style) |
| 85 | { |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 91 | uint32_t GrTextContext::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) { |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 92 | uint32_t flags = paint.getFlags(); |
| 93 | |
| 94 | if (!paint.isLCDRenderText() || !paint.isAntiAlias()) { |
| 95 | return flags; |
| 96 | } |
| 97 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 98 | if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) { |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 99 | flags &= ~SkPaint::kLCDRenderText_Flag; |
| 100 | flags |= SkPaint::kGenA8FromLCD_Flag; |
| 101 | } |
| 102 | |
| 103 | return flags; |
| 104 | } |
| 105 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 106 | void GrTextContext::drawTextBlob(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 107 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 108 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 109 | SkScalar x, SkScalar y, |
| 110 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 111 | SkPaint runPaint = skPaint; |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 112 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 113 | SkTextBlobRunIterator it(blob); |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 114 | for (;!it.done(); it.next()) { |
| 115 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 116 | const SkPoint& offset = it.offset(); |
| 117 | // applyFontToPaint() always overwrites the exact same attributes, |
| 118 | // so it is safe to not re-seed the paint for this reason. |
| 119 | it.applyFontToPaint(&runPaint); |
| 120 | |
| 121 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 122 | // A false return from filter() means we should abort the current draw. |
| 123 | runPaint = skPaint; |
| 124 | continue; |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 125 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 126 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 127 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 128 | |
| 129 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 130 | if (!SkPaintToGrPaint(fContext, runPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 133 | |
| 134 | switch (it.positioning()) { |
| 135 | case SkTextBlob::kDefault_Positioning: |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 136 | this->drawText(dc, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(), |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 137 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 138 | break; |
| 139 | case SkTextBlob::kHorizontal_Positioning: |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 140 | this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(), |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 141 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds); |
| 142 | break; |
| 143 | case SkTextBlob::kFull_Positioning: |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 144 | this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(), |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 145 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 146 | break; |
| 147 | default: |
| 148 | SkFAIL("unhandled positioning mode"); |
| 149 | } |
| 150 | |
| 151 | if (drawFilter) { |
| 152 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 153 | runPaint = skPaint; |
| 154 | } |
| 155 | } |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 156 | } |
| 157 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 158 | static void GlyphCacheAuxProc(void* data) { |
| 159 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 160 | SkSafeUnref(scaler); |
| 161 | } |
| 162 | |
| 163 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 164 | void* auxData; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | GrFontScaler* scaler = nullptr; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 167 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 168 | scaler = (GrFontScaler*)auxData; |
| 169 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | if (nullptr == scaler) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 171 | scaler = new GrFontScaler(cache); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 172 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 173 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 174 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 175 | return scaler; |
| 176 | } |