kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrStencilAndCoverTextContext.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 9 | #include "GrAtlasTextContext.h" |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 11 | #include "GrPath.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 12 | #include "GrPathRange.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 13 | #include "GrRenderTargetContext.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 15 | #include "GrTextUtils.h" |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 16 | #include "SkAutoKern.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 17 | #include "SkDraw.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 18 | #include "SkDrawFilter.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 19 | #include "SkDrawProcs.h" |
| 20 | #include "SkGlyphCache.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 21 | #include "SkGr.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 22 | #include "SkPath.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 23 | #include "SkTextBlobRunIterator.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 24 | #include "SkTextFormatParams.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 25 | #include "SkTextMapStateProc.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 26 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 27 | #include "ops/GrDrawPathOp.h" |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 28 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 29 | template<typename Key, typename Val> static void delete_hash_map_entry(const Key&, Val* val) { |
| 30 | SkASSERT(*val); |
| 31 | delete *val; |
| 32 | } |
| 33 | |
| 34 | template<typename T> static void delete_hash_table_entry(T* val) { |
| 35 | SkASSERT(*val); |
| 36 | delete *val; |
| 37 | } |
| 38 | |
brianosman | 86e7626 | 2016-08-11 12:17:31 -0700 | [diff] [blame] | 39 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrAtlasTextContext* fallbackTextContext) |
| 40 | : fFallbackTextContext(fallbackTextContext) |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 41 | , fCacheSize(0) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 42 | } |
| 43 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 44 | GrStencilAndCoverTextContext* |
brianosman | 86e7626 | 2016-08-11 12:17:31 -0700 | [diff] [blame] | 45 | GrStencilAndCoverTextContext::Create(GrAtlasTextContext* fallbackTextContext) { |
| 46 | return new GrStencilAndCoverTextContext(fallbackTextContext);; |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 47 | } |
| 48 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 49 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 50 | fBlobIdCache.foreach(delete_hash_map_entry<uint32_t, TextBlob*>); |
| 51 | fBlobKeyCache.foreach(delete_hash_table_entry<TextBlob*>); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 52 | } |
| 53 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 54 | bool GrStencilAndCoverTextContext::internalCanDraw(const SkPaint& skPaint) { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 55 | if (skPaint.getRasterizer()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 58 | if (skPaint.getMaskFilter()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 61 | if (SkPathEffect* pe = skPaint.getPathEffect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 65 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 66 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 67 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 70 | void GrStencilAndCoverTextContext::drawText(GrContext* context, GrRenderTargetContext* rtc, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 71 | const GrClip& clip, const SkPaint& skPaint, |
| 72 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
| 73 | const char text[], size_t byteLength, SkScalar x, |
| 74 | SkScalar y, const SkIRect& clipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 75 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 76 | return; |
| 77 | } else if (this->canDraw(skPaint, viewMatrix)) { |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 78 | if (skPaint.getTextSize() > 0) { |
| 79 | TextRun run(skPaint); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 80 | run.setText(text, byteLength, x, y); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 81 | run.draw(context, rtc, clip, viewMatrix, props, 0, 0, clipBounds, fFallbackTextContext, |
| 82 | skPaint); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 83 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 84 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 85 | } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix, props, |
| 86 | *context->caps()->shaderCaps())) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 87 | fFallbackTextContext->drawText(context, rtc, clip, skPaint, viewMatrix, props, text, |
| 88 | byteLength, x, y, clipBounds); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
| 92 | // fall back to drawing as a path |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 93 | GrTextUtils::DrawTextAsPath(context, rtc, clip, skPaint, viewMatrix, text, byteLength, x, y, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 94 | clipBounds); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 95 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 96 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 97 | void GrStencilAndCoverTextContext::drawPosText(GrContext* context, GrRenderTargetContext* rtc, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 98 | const GrClip& clip, const SkPaint& skPaint, |
| 99 | const SkMatrix& viewMatrix, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 100 | const SkSurfaceProps& props, const char text[], |
| 101 | size_t byteLength, const SkScalar pos[], |
| 102 | int scalarsPerPosition, const SkPoint& offset, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 103 | const SkIRect& clipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 104 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 105 | return; |
| 106 | } else if (this->canDraw(skPaint, viewMatrix)) { |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 107 | if (skPaint.getTextSize() > 0) { |
| 108 | TextRun run(skPaint); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 109 | run.setPosText(text, byteLength, pos, scalarsPerPosition, offset); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 110 | run.draw(context, rtc, clip, viewMatrix, props, 0, 0, clipBounds, fFallbackTextContext, |
| 111 | skPaint); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 112 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 113 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 114 | } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix, props, |
| 115 | *context->caps()->shaderCaps())) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 116 | fFallbackTextContext->drawPosText(context, rtc, clip, skPaint, viewMatrix, props, text, |
| 117 | byteLength, pos, scalarsPerPosition, offset, clipBounds); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | // fall back to drawing as a path |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 122 | GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, skPaint, viewMatrix, text, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 123 | byteLength, pos, scalarsPerPosition, offset, clipBounds); |
| 124 | } |
| 125 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 126 | void GrStencilAndCoverTextContext::uncachedDrawTextBlob(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 127 | GrRenderTargetContext* rtc, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 128 | const GrClip& clip, |
| 129 | const SkPaint& skPaint, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 130 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 131 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 132 | const SkTextBlob* blob, |
| 133 | SkScalar x, SkScalar y, |
| 134 | SkDrawFilter* drawFilter, |
| 135 | const SkIRect& clipBounds) { |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame^] | 136 | GrTextUtils::Paint paint(&skPaint, rtc->colorSpaceInfo()); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 137 | GrTextUtils::RunPaint runPaint(&paint, drawFilter, props); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 138 | SkTextBlobRunIterator it(blob); |
| 139 | for (;!it.done(); it.next()) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 140 | if (!runPaint.modifyForRun(it)) { |
| 141 | continue; |
| 142 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 143 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 144 | const SkPoint& offset = it.offset(); |
| 145 | |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 146 | switch (it.positioning()) { |
| 147 | case SkTextBlob::kDefault_Positioning: |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 148 | this->drawText(context, rtc, clip, runPaint, viewMatrix, props, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 149 | (const char*)it.glyphs(), textLen, x + offset.x(), y + offset.y(), |
| 150 | clipBounds); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 151 | break; |
| 152 | case SkTextBlob::kHorizontal_Positioning: |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 153 | this->drawPosText(context, rtc, clip, runPaint, viewMatrix, props, |
| 154 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 155 | SkPoint::Make(x, y + offset.y()), clipBounds); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 156 | break; |
| 157 | case SkTextBlob::kFull_Positioning: |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 158 | this->drawPosText(context, rtc, clip, runPaint, viewMatrix, props, |
| 159 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 160 | SkPoint::Make(x, y), clipBounds); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 161 | break; |
| 162 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 163 | } |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 166 | void GrStencilAndCoverTextContext::drawTextBlob(GrContext* context, GrRenderTargetContext* rtc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 167 | const GrClip& clip, const SkPaint& skPaint, |
| 168 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 169 | const SkSurfaceProps& props, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 170 | const SkTextBlob* skBlob, SkScalar x, SkScalar y, |
| 171 | SkDrawFilter* drawFilter, |
| 172 | const SkIRect& clipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 173 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 177 | if (!this->internalCanDraw(skPaint)) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 178 | fFallbackTextContext->drawTextBlob(context, rtc, clip, skPaint, viewMatrix, props, skBlob, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 179 | x, y, drawFilter, clipBounds); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (drawFilter || skPaint.getPathEffect()) { |
| 184 | // This draw can't be cached. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 185 | this->uncachedDrawTextBlob(context, rtc, clip, skPaint, viewMatrix, props, skBlob, x, y, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 186 | drawFilter, clipBounds); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 190 | const TextBlob& blob = this->findOrCreateTextBlob(skBlob, skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 191 | |
| 192 | TextBlob::Iter iter(blob); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 193 | for (TextRun *run = iter.get(), *nextRun; run; run = nextRun) { |
| 194 | nextRun = iter.next(); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 195 | run->draw(context, rtc, clip, viewMatrix, props, x, y, clipBounds, fFallbackTextContext, |
| 196 | skPaint); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 197 | run->releaseGlyphCache(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 201 | static inline int style_key_cnt(const GrStyle& style) { |
| 202 | int cnt = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); |
| 203 | // We should be able to make a key because we filtered out arbitrary path effects. |
| 204 | SkASSERT(cnt > 0); |
| 205 | return cnt; |
| 206 | } |
| 207 | |
| 208 | static inline void write_style_key(uint32_t* dst, const GrStyle& style) { |
| 209 | // Pass 1 for the scale since the GPU will apply the style not GrStyle::applyToPath(). |
| 210 | GrStyle::WriteKey(dst, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Scalar1); |
| 211 | } |
| 212 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 213 | const GrStencilAndCoverTextContext::TextBlob& |
| 214 | GrStencilAndCoverTextContext::findOrCreateTextBlob(const SkTextBlob* skBlob, |
| 215 | const SkPaint& skPaint) { |
| 216 | // The font-related parameters are baked into the text blob and will override this skPaint, so |
| 217 | // the only remaining properties that can affect a TextBlob are the ones related to stroke. |
| 218 | if (SkPaint::kFill_Style == skPaint.getStyle()) { // Fast path. |
| 219 | if (TextBlob** found = fBlobIdCache.find(skBlob->uniqueID())) { |
| 220 | fLRUList.remove(*found); |
| 221 | fLRUList.addToTail(*found); |
| 222 | return **found; |
| 223 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 224 | TextBlob* blob = new TextBlob(skBlob->uniqueID(), skBlob, skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 225 | this->purgeToFit(*blob); |
| 226 | fBlobIdCache.set(skBlob->uniqueID(), blob); |
| 227 | fLRUList.addToTail(blob); |
| 228 | fCacheSize += blob->cpuMemorySize(); |
| 229 | return *blob; |
| 230 | } else { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 231 | GrStyle style(skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 232 | SkSTArray<4, uint32_t, true> key; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 233 | key.reset(1 + style_key_cnt(style)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 234 | key[0] = skBlob->uniqueID(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 235 | write_style_key(&key[1], style); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 236 | if (TextBlob** found = fBlobKeyCache.find(key)) { |
| 237 | fLRUList.remove(*found); |
| 238 | fLRUList.addToTail(*found); |
| 239 | return **found; |
| 240 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 241 | TextBlob* blob = new TextBlob(key, skBlob, skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 242 | this->purgeToFit(*blob); |
| 243 | fBlobKeyCache.set(blob); |
| 244 | fLRUList.addToTail(blob); |
| 245 | fCacheSize += blob->cpuMemorySize(); |
| 246 | return *blob; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void GrStencilAndCoverTextContext::purgeToFit(const TextBlob& blob) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 251 | static const size_t maxCacheSize = 4 * 1024 * 1024; // Allow up to 4 MB for caching text blobs. |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 252 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 253 | size_t maxSizeForNewBlob = maxCacheSize - blob.cpuMemorySize(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 254 | while (fCacheSize && fCacheSize > maxSizeForNewBlob) { |
| 255 | TextBlob* lru = fLRUList.head(); |
| 256 | if (1 == lru->key().count()) { |
| 257 | // 1-length keys are unterstood to be the blob id. |
| 258 | fBlobIdCache.remove(lru->key()[0]); |
| 259 | } else { |
| 260 | fBlobKeyCache.remove(lru->key()); |
| 261 | } |
| 262 | fLRUList.remove(lru); |
| 263 | fCacheSize -= lru->cpuMemorySize(); |
| 264 | delete lru; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 269 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 270 | void GrStencilAndCoverTextContext::TextBlob::init(const SkTextBlob* skBlob, |
| 271 | const SkPaint& skPaint) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 272 | fCpuMemorySize = sizeof(TextBlob); |
| 273 | SkPaint runPaint(skPaint); |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 274 | for (SkTextBlobRunIterator iter(skBlob); !iter.done(); iter.next()) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 275 | iter.applyFontToPaint(&runPaint); // No need to re-seed the paint. |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 276 | if (runPaint.getTextSize() <= 0) { |
| 277 | continue; |
| 278 | } |
bsalomon | 5aaef1f | 2015-11-18 14:11:08 -0800 | [diff] [blame] | 279 | TextRun* run = this->addToTail(runPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 280 | |
| 281 | const char* text = reinterpret_cast<const char*>(iter.glyphs()); |
| 282 | size_t byteLength = sizeof(uint16_t) * iter.glyphCount(); |
| 283 | const SkPoint& runOffset = iter.offset(); |
| 284 | |
| 285 | switch (iter.positioning()) { |
| 286 | case SkTextBlob::kDefault_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 287 | run->setText(text, byteLength, runOffset.fX, runOffset.fY); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 288 | break; |
| 289 | case SkTextBlob::kHorizontal_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 290 | run->setPosText(text, byteLength, iter.pos(), 1, SkPoint::Make(0, runOffset.fY)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 291 | break; |
| 292 | case SkTextBlob::kFull_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 293 | run->setPosText(text, byteLength, iter.pos(), 2, SkPoint::Make(0, 0)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 297 | fCpuMemorySize += run->computeSizeInCache(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 298 | } |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 302 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 303 | class GrStencilAndCoverTextContext::FallbackBlobBuilder { |
| 304 | public: |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 305 | FallbackBlobBuilder() : fBuffIdx(0), fCount(0) {} |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 306 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 307 | bool isInitialized() const { return fBuilder != nullptr; } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 308 | |
| 309 | void init(const SkPaint& font, SkScalar textRatio); |
| 310 | |
| 311 | void appendGlyph(uint16_t glyphId, const SkPoint& pos); |
| 312 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 313 | sk_sp<SkTextBlob> makeIfNeeded(int* count); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 314 | |
| 315 | private: |
| 316 | enum { kWriteBufferSize = 1024 }; |
| 317 | |
| 318 | void flush(); |
| 319 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 320 | std::unique_ptr<SkTextBlobBuilder> fBuilder; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 321 | SkPaint fFont; |
| 322 | int fBuffIdx; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 323 | int fCount; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 324 | uint16_t fGlyphIds[kWriteBufferSize]; |
| 325 | SkPoint fPositions[kWriteBufferSize]; |
| 326 | }; |
| 327 | |
| 328 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 329 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 330 | GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke) |
robertphillips | ecbc15d | 2016-07-19 14:04:39 -0700 | [diff] [blame] | 331 | : fStyle(fontAndStroke) |
| 332 | , fFont(fontAndStroke) |
| 333 | , fTotalGlyphCount(0) |
| 334 | , fFallbackGlyphCount(0) |
| 335 | , fDetachedGlyphCache(nullptr) |
| 336 | , fLastDrawnGlyphsID(SK_InvalidUniqueID) { |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 337 | SkASSERT(fFont.getTextSize() > 0); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 338 | SkASSERT(!fStyle.hasNonDashPathEffect()); // Arbitrary path effects not supported. |
| 339 | SkASSERT(!fStyle.isSimpleHairline()); // Hairlines are not supported. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 340 | |
| 341 | // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path |
| 342 | // rendering API for stroking). |
| 343 | fFont.setStyle(SkPaint::kFill_Style); |
| 344 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 345 | if (fFont.isFakeBoldText() && fStyle.isSimpleFill()) { |
| 346 | const SkStrokeRec& stroke = fStyle.strokeRec(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 347 | // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke. |
| 348 | SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(), |
| 349 | kStdFakeBoldInterpKeys, |
| 350 | kStdFakeBoldInterpValues, |
| 351 | kStdFakeBoldInterpLength); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 352 | SkScalar extra = fFont.getTextSize() * fakeBoldScale; |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 353 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 354 | SkStrokeRec strokeRec(SkStrokeRec::kFill_InitStyle); |
| 355 | strokeRec.setStrokeStyle(stroke.needToApply() ? stroke.getWidth() + extra : extra, |
| 356 | true /*strokeAndFill*/); |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 357 | fStyle = GrStyle(strokeRec, fStyle.refPathEffect()); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 358 | fFont.setFakeBoldText(false); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 359 | } |
| 360 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 361 | if (!fFont.getPathEffect() && !fStyle.isDashed()) { |
| 362 | const SkStrokeRec& stroke = fStyle.strokeRec(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 363 | // We can draw the glyphs from canonically sized paths. |
| 364 | fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 365 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 366 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 367 | // Compensate for the glyphs being scaled by fTextRatio. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 368 | if (!fStyle.isSimpleFill()) { |
| 369 | SkStrokeRec strokeRec(SkStrokeRec::kFill_InitStyle); |
| 370 | strokeRec.setStrokeStyle(stroke.getWidth() / fTextRatio, |
| 371 | SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()); |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 372 | fStyle = GrStyle(strokeRec, fStyle.refPathEffect()); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | fFont.setLinearText(true); |
| 376 | fFont.setLCDRenderText(false); |
| 377 | fFont.setAutohinted(false); |
| 378 | fFont.setHinting(SkPaint::kNo_Hinting); |
| 379 | fFont.setSubpixelText(true); |
| 380 | fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 381 | |
| 382 | fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() && |
| 383 | 0 == fFont.getTextSkewX() && |
| 384 | !fFont.isFakeBoldText() && |
| 385 | !fFont.isVerticalText(); |
| 386 | } else { |
| 387 | fTextRatio = fTextInverseRatio = 1.0f; |
| 388 | fUsingRawGlyphPaths = false; |
| 389 | } |
| 390 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 391 | // Generate the key that will be used to cache the GPU glyph path objects. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 392 | if (fUsingRawGlyphPaths && fStyle.isSimpleFill()) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 393 | static const GrUniqueKey::Domain kRawFillPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 394 | |
| 395 | const SkTypeface* typeface = fFont.getTypeface(); |
| 396 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kRawFillPathGlyphDomain, 1); |
| 397 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 398 | } else { |
| 399 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 400 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 401 | int styleDataCount = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec); |
| 402 | // Key should be valid since we opted out of drawing arbitrary path effects. |
| 403 | SkASSERT(styleDataCount >= 0); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 404 | if (fUsingRawGlyphPaths) { |
| 405 | const SkTypeface* typeface = fFont.getTypeface(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 406 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, 2 + styleDataCount); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 407 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 408 | reinterpret_cast<uint32_t&>(builder[1]) = styleDataCount; |
| 409 | if (styleDataCount) { |
| 410 | write_style_key(&builder[2], fStyle); |
| 411 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 412 | } else { |
| 413 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
| 414 | const SkTypeface* typeface = glyphCache->getScalerContext()->getTypeface(); |
| 415 | const SkDescriptor* desc = &glyphCache->getDescriptor(); |
| 416 | int descDataCount = (desc->getLength() + 3) / 4; |
| 417 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 418 | 2 + styleDataCount + descDataCount); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 419 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 420 | reinterpret_cast<uint32_t&>(builder[1]) = styleDataCount | (descDataCount << 16); |
| 421 | if (styleDataCount) { |
| 422 | write_style_key(&builder[2], fStyle); |
| 423 | } |
| 424 | memcpy(&builder[2 + styleDataCount], desc, desc->getLength()); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | GrStencilAndCoverTextContext::TextRun::~TextRun() { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 430 | this->releaseGlyphCache(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 434 | SkScalar x, SkScalar y) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 435 | SkASSERT(byteLength == 0 || text != nullptr); |
| 436 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 437 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
robertphillips | e34f17d | 2016-07-19 07:59:22 -0700 | [diff] [blame] | 438 | SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(), |
| 439 | fFont.isDevKernText(), |
| 440 | true); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 441 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 442 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 443 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 444 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 445 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 446 | const char* stop = text + byteLength; |
| 447 | |
| 448 | // Measure first if needed. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 449 | if (fFont.getTextAlign() != SkPaint::kLeft_Align) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 450 | SkScalar stopX = 0; |
| 451 | SkScalar stopY = 0; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 452 | |
| 453 | const char* textPtr = text; |
| 454 | while (textPtr < stop) { |
| 455 | // We don't need x, y here, since all subpixel variants will have the |
| 456 | // same advance. |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 457 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 458 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 459 | stopX += SkFloatToScalar(glyph.fAdvanceX); |
| 460 | stopY += SkFloatToScalar(glyph.fAdvanceY); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 461 | } |
| 462 | SkASSERT(textPtr == stop); |
| 463 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 464 | SkScalar alignX = stopX * fTextRatio; |
| 465 | SkScalar alignY = stopY * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 466 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 467 | if (fFont.getTextAlign() == SkPaint::kCenter_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 468 | alignX = SkScalarHalf(alignX); |
| 469 | alignY = SkScalarHalf(alignY); |
| 470 | } |
| 471 | |
| 472 | x -= alignX; |
| 473 | y -= alignY; |
| 474 | } |
| 475 | |
| 476 | SkAutoKern autokern; |
| 477 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 478 | FallbackBlobBuilder fallback; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 479 | while (text < stop) { |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 480 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 481 | x += autokern.adjust(glyph) * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 482 | if (glyph.fWidth) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 483 | this->appendGlyph(glyph, SkPoint::Make(x, y), &fallback); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 484 | } |
| 485 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 486 | x += SkFloatToScalar(glyph.fAdvanceX) * fTextRatio; |
| 487 | y += SkFloatToScalar(glyph.fAdvanceY) * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 488 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 489 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 490 | fFallbackTextBlob = fallback.makeIfNeeded(&fFallbackGlyphCount); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 491 | } |
| 492 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 493 | void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength, |
| 494 | const SkScalar pos[], int scalarsPerPosition, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 495 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 496 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 497 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 498 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 499 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
robertphillips | e34f17d | 2016-07-19 07:59:22 -0700 | [diff] [blame] | 500 | SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(), |
| 501 | fFont.isDevKernText(), |
| 502 | true); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 503 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 504 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 505 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 506 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 507 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 508 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 509 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 510 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 511 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 512 | FallbackBlobBuilder fallback; |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 513 | while (text < stop) { |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 514 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 515 | if (glyph.fWidth) { |
| 516 | SkPoint tmsLoc; |
| 517 | tmsProc(pos, &tmsLoc); |
| 518 | SkPoint loc; |
| 519 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 520 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 521 | this->appendGlyph(glyph, loc, &fallback); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 522 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 523 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 524 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 525 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 526 | fFallbackTextBlob = fallback.makeIfNeeded(&fFallbackGlyphCount); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 529 | sk_sp<GrPathRange> GrStencilAndCoverTextContext::TextRun::createGlyphs( |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 530 | GrResourceProvider* resourceProvider) const { |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 531 | sk_sp<GrPathRange> glyphs = |
| 532 | resourceProvider->findByUniqueKey<GrPathRange>(fGlyphPathsKey); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 533 | if (!glyphs) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 534 | if (fUsingRawGlyphPaths) { |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 535 | SkScalerContextEffects noeffects; |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 536 | glyphs = resourceProvider->createGlyphs(fFont.getTypeface(), noeffects, |
| 537 | nullptr, fStyle); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 538 | } else { |
| 539 | SkGlyphCache* cache = this->getGlyphCache(); |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 540 | glyphs = resourceProvider->createGlyphs(cache->getScalerContext()->getTypeface(), |
| 541 | cache->getScalerContext()->getEffects(), |
| 542 | &cache->getDescriptor(), |
| 543 | fStyle); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 544 | } |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 545 | resourceProvider->assignUniqueKeyToResource(fGlyphPathsKey, glyphs.get()); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 546 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 547 | return glyphs; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 548 | } |
| 549 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 550 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 551 | const SkPoint& pos, |
| 552 | FallbackBlobBuilder* fallback) { |
| 553 | // Stick the glyphs we can't draw into the fallback text blob. |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 554 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 555 | if (!fallback->isInitialized()) { |
| 556 | fallback->init(fFont, fTextRatio); |
| 557 | } |
| 558 | fallback->appendGlyph(glyph.getGlyphID(), pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 559 | } else { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 560 | fInstanceData->append(glyph.getGlyphID(), fTextInverseRatio * pos.x(), |
| 561 | fTextInverseRatio * pos.y()); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 562 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 565 | void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx, |
| 566 | GrRenderTargetContext* renderTargetContext, |
| 567 | const GrClip& clip, const SkMatrix& viewMatrix, |
| 568 | const SkSurfaceProps& props, SkScalar x, |
| 569 | SkScalar y, const SkIRect& clipBounds, |
| 570 | GrAtlasTextContext* fallbackTextContext, |
| 571 | const SkPaint& originalSkPaint) const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 572 | SkASSERT(fInstanceData); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 573 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 574 | if (fInstanceData->count()) { |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 575 | sk_sp<GrPathRange> glyphs(this->createGlyphs(ctx->resourceProvider())); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 576 | if (fLastDrawnGlyphsID != glyphs->uniqueID()) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 577 | // Either this is the first draw or the glyphs object was purged since last draw. |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 578 | glyphs->loadPathsIfNeeded(fInstanceData->indices(), fInstanceData->count()); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 579 | fLastDrawnGlyphsID = glyphs->uniqueID(); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 582 | GrPaint grPaint; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame^] | 583 | if (!SkPaintToGrPaint(ctx, renderTargetContext->colorSpaceInfo(), originalSkPaint, |
| 584 | viewMatrix, &grPaint)) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame] | 588 | // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy |
| 589 | // the entire dst. Realistically this is a moot point, because any context that supports |
| 590 | // NV_path_rendering will also support NV_blend_equation_advanced. |
| 591 | // For clipping we'll just skip any optimizations based on the bounds. This does, however, |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 592 | // hurt GrOp combining. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 593 | const SkRect bounds = SkRect::MakeIWH(renderTargetContext->width(), |
| 594 | renderTargetContext->height()); |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame] | 595 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 596 | // The run's "font" overrides the anti-aliasing of the passed in SkPaint! |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 597 | GrAAType aaType = GrChooseAAType(this->aa(), renderTargetContext->fsaaType(), |
Brian Salomon | e225b56 | 2017-06-14 13:00:03 -0400 | [diff] [blame] | 598 | GrAllowMixedSamples::kYes, *renderTargetContext->caps()); |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 599 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 600 | std::unique_ptr<GrDrawOp> op = GrDrawPathRangeOp::Make( |
| 601 | viewMatrix, fTextRatio, fTextInverseRatio * x, fTextInverseRatio * y, |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 602 | std::move(grPaint), GrPathRendering::kWinding_FillType, aaType, glyphs.get(), |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 603 | fInstanceData.get(), bounds); |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 604 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 605 | renderTargetContext->addDrawOp(clip, std::move(op)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 606 | } |
| 607 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 608 | if (fFallbackTextBlob) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 609 | SkPaint fallbackSkPaint(originalSkPaint); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 610 | fStyle.strokeRec().applyToPaint(&fallbackSkPaint); |
| 611 | if (!fStyle.isSimpleFill()) { |
| 612 | fallbackSkPaint.setStrokeWidth(fStyle.strokeRec().getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 613 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 614 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 615 | fallbackTextContext->drawTextBlob(ctx, renderTargetContext, clip, fallbackSkPaint, |
| 616 | viewMatrix, props, fFallbackTextBlob.get(), x, y, nullptr, |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 617 | clipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 618 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 619 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 620 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 621 | SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const { |
| 622 | if (!fDetachedGlyphCache) { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 623 | fDetachedGlyphCache = fFont.detachCache(nullptr, SkPaint::kNone_ScalerContextFlags, |
| 624 | nullptr); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 625 | } |
| 626 | return fDetachedGlyphCache; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const { |
| 631 | if (fDetachedGlyphCache) { |
| 632 | SkGlyphCache::AttachCache(fDetachedGlyphCache); |
| 633 | fDetachedGlyphCache = nullptr; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 638 | size_t size = sizeof(TextRun) + fGlyphPathsKey.size(); |
| 639 | // The instance data always reserves enough space for every glyph. |
| 640 | size += (fTotalGlyphCount + fFallbackGlyphCount) * (sizeof(uint16_t) + 2 * sizeof(float)); |
| 641 | if (fInstanceData) { |
| 642 | size += sizeof(InstanceData); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 643 | } |
| 644 | if (fFallbackTextBlob) { |
| 645 | size += sizeof(SkTextBlob); |
| 646 | } |
| 647 | return size; |
| 648 | } |
| 649 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 650 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 651 | |
| 652 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font, |
| 653 | SkScalar textRatio) { |
| 654 | SkASSERT(!this->isInitialized()); |
| 655 | fBuilder.reset(new SkTextBlobBuilder); |
| 656 | fFont = font; |
| 657 | fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align. |
| 658 | fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 659 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs |
| 660 | // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 661 | fFont.setSubpixelText(false); |
| 662 | fFont.setTextSize(fFont.getTextSize() * textRatio); |
| 663 | fBuffIdx = 0; |
| 664 | } |
| 665 | |
| 666 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId, |
| 667 | const SkPoint& pos) { |
| 668 | SkASSERT(this->isInitialized()); |
| 669 | if (fBuffIdx >= kWriteBufferSize) { |
| 670 | this->flush(); |
| 671 | } |
| 672 | fGlyphIds[fBuffIdx] = glyphId; |
| 673 | fPositions[fBuffIdx] = pos; |
| 674 | fBuffIdx++; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 675 | fCount++; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() { |
| 679 | SkASSERT(this->isInitialized()); |
| 680 | SkASSERT(fBuffIdx <= kWriteBufferSize); |
| 681 | if (!fBuffIdx) { |
| 682 | return; |
| 683 | } |
| 684 | // This will automatically merge with previous runs since we use the same font. |
| 685 | const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx); |
| 686 | memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t)); |
| 687 | memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar)); |
| 688 | fBuffIdx = 0; |
| 689 | } |
| 690 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 691 | sk_sp<SkTextBlob> GrStencilAndCoverTextContext::FallbackBlobBuilder::makeIfNeeded(int *count) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 692 | *count = fCount; |
| 693 | if (fCount) { |
| 694 | this->flush(); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 695 | return fBuilder->make(); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 696 | } |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 697 | return nullptr; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 698 | } |