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