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