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" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 15 | #include "GrTextUtils.h" |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 16 | #include "SkAutoKern.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 17 | #include "SkDraw.h" |
| 18 | #include "SkDrawProcs.h" |
| 19 | #include "SkGlyphCache.h" |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 20 | #include "SkGrPriv.h" |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 21 | #include "SkDrawFilter.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 22 | #include "SkPath.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 23 | #include "SkTextBlobRunIterator.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 24 | #include "SkTextMapStateProc.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 25 | #include "SkTextFormatParams.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 26 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 27 | #include "batches/GrDrawPathBatch.h" |
| 28 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 29 | template<typename Key, typename Val> static void delete_hash_map_entry(const Key&, Val* val) { |
| 30 | SkASSERT(*val); |
| 31 | delete *val; |
| 32 | } |
| 33 | |
| 34 | template<typename T> static void delete_hash_table_entry(T* val) { |
| 35 | SkASSERT(*val); |
| 36 | delete *val; |
| 37 | } |
| 38 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 39 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext() |
| 40 | : fFallbackTextContext(nullptr) |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 41 | , fCacheSize(0) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 42 | } |
| 43 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 44 | GrStencilAndCoverTextContext* |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 45 | GrStencilAndCoverTextContext::Create() { |
| 46 | GrStencilAndCoverTextContext* textContext = new GrStencilAndCoverTextContext(); |
| 47 | textContext->fFallbackTextContext = GrAtlasTextContext::Create(); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 48 | |
| 49 | return textContext; |
| 50 | } |
| 51 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 52 | GrStencilAndCoverTextContext::~GrStencilAndCoverTextContext() { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 53 | delete fFallbackTextContext; |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 54 | fBlobIdCache.foreach(delete_hash_map_entry<uint32_t, TextBlob*>); |
| 55 | fBlobKeyCache.foreach(delete_hash_table_entry<TextBlob*>); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 56 | } |
| 57 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 58 | bool GrStencilAndCoverTextContext::internalCanDraw(const SkPaint& skPaint) { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 59 | if (skPaint.getRasterizer()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 62 | if (skPaint.getMaskFilter()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 65 | if (SkPathEffect* pe = skPaint.getPathEffect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 69 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 70 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 71 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 72 | } |
| 73 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 74 | void GrStencilAndCoverTextContext::drawText(GrContext* context, GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 75 | const GrClip& clip, const GrPaint& paint, |
| 76 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 77 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 78 | const char text[], size_t byteLength, |
| 79 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 80 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 81 | return; |
| 82 | } else if (this->canDraw(skPaint, viewMatrix)) { |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 83 | if (skPaint.getTextSize() > 0) { |
| 84 | TextRun run(skPaint); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 85 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget()); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 86 | run.setText(text, byteLength, x, y); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 87 | run.draw(context, dc, &pipelineBuilder, clip, paint.getColor(), 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); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 120 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget()); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 121 | run.setPosText(text, byteLength, pos, scalarsPerPosition, offset); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 122 | run.draw(context, dc, &pipelineBuilder, clip, paint.getColor(), viewMatrix, props, 0, 0, |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 123 | clipBounds, fFallbackTextContext, skPaint); |
| 124 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 125 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 126 | } else if (fFallbackTextContext->canDraw(skPaint, viewMatrix, props, |
| 127 | *context->caps()->shaderCaps())) { |
| 128 | fFallbackTextContext->drawPosText(context, dc, clip, paint, skPaint, viewMatrix, props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 129 | text, byteLength, pos, |
| 130 | scalarsPerPosition, offset, clipBounds); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // fall back to drawing as a path |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 135 | GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, skPaint, viewMatrix, text, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 136 | byteLength, pos, scalarsPerPosition, offset, clipBounds); |
| 137 | } |
| 138 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 139 | void GrStencilAndCoverTextContext::uncachedDrawTextBlob(GrContext* context, |
| 140 | GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 141 | const GrClip& clip, const SkPaint& skPaint, |
| 142 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 143 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 144 | const SkTextBlob* blob, |
| 145 | SkScalar x, SkScalar y, |
| 146 | SkDrawFilter* drawFilter, |
| 147 | const SkIRect& clipBounds) { |
| 148 | SkPaint runPaint = skPaint; |
| 149 | |
| 150 | SkTextBlobRunIterator it(blob); |
| 151 | for (;!it.done(); it.next()) { |
| 152 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 153 | const SkPoint& offset = it.offset(); |
| 154 | |
| 155 | // applyFontToPaint() always overwrites the exact same attributes, |
| 156 | // so it is safe to not re-seed the paint for this reason. |
| 157 | it.applyFontToPaint(&runPaint); |
| 158 | |
| 159 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 160 | // A false return from filter() means we should abort the current draw. |
| 161 | runPaint = skPaint; |
| 162 | continue; |
| 163 | } |
| 164 | |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 165 | runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint)); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 166 | |
| 167 | GrPaint grPaint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 168 | if (!SkPaintToGrPaint(context, runPaint, viewMatrix, dc->isGammaCorrect(), &grPaint)) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | switch (it.positioning()) { |
| 173 | case SkTextBlob::kDefault_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 174 | this->drawText(context, dc, clip, grPaint, runPaint, viewMatrix, props, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 175 | (const char *)it.glyphs(), |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 176 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 177 | break; |
| 178 | case SkTextBlob::kHorizontal_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 179 | this->drawPosText(context, dc, clip, grPaint, runPaint, viewMatrix, props, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 180 | (const char*)it.glyphs(), |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 181 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 182 | clipBounds); |
| 183 | break; |
| 184 | case SkTextBlob::kFull_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 185 | this->drawPosText(context, dc, clip, grPaint, runPaint, viewMatrix, props, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 186 | (const char*)it.glyphs(), |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 187 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | if (drawFilter) { |
| 192 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 193 | runPaint = skPaint; |
| 194 | } |
| 195 | } |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 196 | } |
| 197 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 198 | void GrStencilAndCoverTextContext::drawTextBlob(GrContext* context, GrDrawContext* dc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 199 | const GrClip& clip, const SkPaint& skPaint, |
| 200 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 201 | const SkSurfaceProps& props, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 202 | const SkTextBlob* skBlob, SkScalar x, SkScalar y, |
| 203 | SkDrawFilter* drawFilter, |
| 204 | const SkIRect& clipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 205 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 209 | if (!this->internalCanDraw(skPaint)) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 210 | fFallbackTextContext->drawTextBlob(context, dc, clip, skPaint, viewMatrix, props, skBlob, |
| 211 | x, y, drawFilter, clipBounds); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
| 215 | if (drawFilter || skPaint.getPathEffect()) { |
| 216 | // This draw can't be cached. |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 217 | this->uncachedDrawTextBlob(context, dc, clip, skPaint, viewMatrix, props, skBlob, x, y, |
| 218 | drawFilter, clipBounds); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | GrPaint paint; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 223 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, dc->isGammaCorrect(), &paint)) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
| 227 | const TextBlob& blob = this->findOrCreateTextBlob(skBlob, skPaint); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 228 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget()); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 229 | |
| 230 | TextBlob::Iter iter(blob); |
| 231 | for (TextRun* run = iter.get(); run; run = iter.next()) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 232 | run->draw(context, dc, &pipelineBuilder, clip, paint.getColor(), 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) |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 368 | : fStyle(fontAndStroke), |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 369 | fFont(fontAndStroke), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 370 | fTotalGlyphCount(0), |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 371 | fFallbackGlyphCount(0), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 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(); |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 475 | SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 476 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 477 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 478 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 479 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 480 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 481 | const char* stop = text + byteLength; |
| 482 | |
| 483 | // Measure first if needed. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 484 | if (fFont.getTextAlign() != SkPaint::kLeft_Align) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 485 | SkScalar stopX = 0; |
| 486 | SkScalar stopY = 0; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 487 | |
| 488 | const char* textPtr = text; |
| 489 | while (textPtr < stop) { |
| 490 | // We don't need x, y here, since all subpixel variants will have the |
| 491 | // same advance. |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 492 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 493 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 494 | stopX += SkFloatToScalar(glyph.fAdvanceX); |
| 495 | stopY += SkFloatToScalar(glyph.fAdvanceY); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 496 | } |
| 497 | SkASSERT(textPtr == stop); |
| 498 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 499 | SkScalar alignX = stopX * fTextRatio; |
| 500 | SkScalar alignY = stopY * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 501 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 502 | if (fFont.getTextAlign() == SkPaint::kCenter_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 503 | alignX = SkScalarHalf(alignX); |
| 504 | alignY = SkScalarHalf(alignY); |
| 505 | } |
| 506 | |
| 507 | x -= alignX; |
| 508 | y -= alignY; |
| 509 | } |
| 510 | |
| 511 | SkAutoKern autokern; |
| 512 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 513 | FallbackBlobBuilder fallback; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 514 | while (text < stop) { |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 515 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 516 | x += autokern.adjust(glyph) * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 517 | if (glyph.fWidth) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 518 | this->appendGlyph(glyph, SkPoint::Make(x, y), &fallback); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 519 | } |
| 520 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 521 | x += SkFloatToScalar(glyph.fAdvanceX) * fTextRatio; |
| 522 | y += SkFloatToScalar(glyph.fAdvanceY) * fTextRatio; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 523 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 524 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 525 | fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount)); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 526 | } |
| 527 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 528 | void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength, |
| 529 | const SkScalar pos[], int scalarsPerPosition, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 530 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 531 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 532 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 533 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 534 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 535 | SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 536 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 537 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 538 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 539 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 540 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 541 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 542 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 543 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 544 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 545 | FallbackBlobBuilder fallback; |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 546 | while (text < stop) { |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 547 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 548 | if (glyph.fWidth) { |
| 549 | SkPoint tmsLoc; |
| 550 | tmsProc(pos, &tmsLoc); |
| 551 | SkPoint loc; |
| 552 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 553 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 554 | this->appendGlyph(glyph, loc, &fallback); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 555 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 556 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 557 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 558 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 559 | fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 560 | } |
| 561 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 562 | GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx) const { |
| 563 | GrPathRange* glyphs = static_cast<GrPathRange*>( |
| 564 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(fGlyphPathsKey)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 565 | if (nullptr == glyphs) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 566 | if (fUsingRawGlyphPaths) { |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 567 | SkScalerContextEffects noeffects; |
| 568 | glyphs = ctx->resourceProvider()->createGlyphs(fFont.getTypeface(), noeffects, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 569 | nullptr, fStyle); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 570 | } else { |
| 571 | SkGlyphCache* cache = this->getGlyphCache(); |
| 572 | glyphs = ctx->resourceProvider()->createGlyphs(cache->getScalerContext()->getTypeface(), |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 573 | cache->getScalerContext()->getEffects(), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 574 | &cache->getDescriptor(), |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 575 | fStyle); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 576 | } |
| 577 | ctx->resourceProvider()->assignUniqueKeyToResource(fGlyphPathsKey, glyphs); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 578 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 579 | return glyphs; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 580 | } |
| 581 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 582 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 583 | const SkPoint& pos, |
| 584 | FallbackBlobBuilder* fallback) { |
| 585 | // Stick the glyphs we can't draw into the fallback text blob. |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 586 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 587 | if (!fallback->isInitialized()) { |
| 588 | fallback->init(fFont, fTextRatio); |
| 589 | } |
| 590 | fallback->appendGlyph(glyph.getGlyphID(), pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 591 | } else { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 592 | fInstanceData->append(glyph.getGlyphID(), fTextInverseRatio * pos.x(), |
| 593 | fTextInverseRatio * pos.y()); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 594 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 595 | } |
| 596 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 597 | void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx, |
| 598 | GrDrawContext* dc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 599 | GrPipelineBuilder* pipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 600 | const GrClip& clip, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 601 | GrColor color, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 602 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 603 | const SkSurfaceProps& props, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 604 | SkScalar x, SkScalar y, |
| 605 | const SkIRect& clipBounds, |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 606 | GrAtlasTextContext* fallbackTextContext, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 607 | const SkPaint& originalSkPaint) const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 608 | SkASSERT(fInstanceData); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 609 | SkASSERT(dc->accessRenderTarget()->isStencilBufferMultisampled() || !fFont.isAntiAlias()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 610 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 611 | if (fInstanceData->count()) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 612 | pipelineBuilder->setState(GrPipelineBuilder::kHWAntialias_Flag, fFont.isAntiAlias()); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 613 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 614 | static constexpr GrUserStencilSettings kCoverPass( |
| 615 | GrUserStencilSettings::StaticInit< |
| 616 | 0x0000, |
| 617 | GrUserStencilTest::kNotEqual, // Stencil pass accounts for clip. |
| 618 | 0xffff, |
| 619 | GrUserStencilOp::kZero, |
| 620 | GrUserStencilOp::kKeep, |
| 621 | 0xffff>() |
| 622 | ); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 623 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 624 | pipelineBuilder->setUserStencil(&kCoverPass); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 625 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 626 | SkAutoTUnref<GrPathRange> glyphs(this->createGlyphs(ctx)); |
| 627 | if (fLastDrawnGlyphsID != glyphs->getUniqueID()) { |
| 628 | // 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] | 629 | glyphs->loadPathsIfNeeded(fInstanceData->indices(), fInstanceData->count()); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 630 | fLastDrawnGlyphsID = glyphs->getUniqueID(); |
| 631 | } |
| 632 | |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame] | 633 | // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy |
| 634 | // the entire dst. Realistically this is a moot point, because any context that supports |
| 635 | // NV_path_rendering will also support NV_blend_equation_advanced. |
| 636 | // For clipping we'll just skip any optimizations based on the bounds. This does, however, |
| 637 | // hurt batching. |
| 638 | SkRect bounds = SkRect::MakeIWH(pipelineBuilder->getRenderTarget()->width(), |
| 639 | pipelineBuilder->getRenderTarget()->height()); |
| 640 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 641 | SkAutoTUnref<GrDrawBatch> batch( |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 642 | GrDrawPathRangeBatch::Create(viewMatrix, fTextRatio, fTextInverseRatio * x, |
| 643 | fTextInverseRatio * y, color, |
| 644 | GrPathRendering::kWinding_FillType, glyphs, fInstanceData, |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 645 | bounds)); |
| 646 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 647 | dc->drawBatch(pipelineBuilder, clip, batch); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 648 | } |
| 649 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 650 | if (fFallbackTextBlob) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 651 | SkPaint fallbackSkPaint(originalSkPaint); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 652 | fStyle.strokeRec().applyToPaint(&fallbackSkPaint); |
| 653 | if (!fStyle.isSimpleFill()) { |
| 654 | fallbackSkPaint.setStrokeWidth(fStyle.strokeRec().getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 655 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 656 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame^] | 657 | fallbackTextContext->drawTextBlob(ctx, dc, clip, fallbackSkPaint, viewMatrix, props, |
| 658 | fFallbackTextBlob, x, y, nullptr, clipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 659 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 660 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 661 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 662 | SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const { |
| 663 | if (!fDetachedGlyphCache) { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 664 | fDetachedGlyphCache = fFont.detachCache(nullptr, SkPaint::kNone_ScalerContextFlags, |
| 665 | nullptr); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 666 | } |
| 667 | return fDetachedGlyphCache; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const { |
| 672 | if (fDetachedGlyphCache) { |
| 673 | SkGlyphCache::AttachCache(fDetachedGlyphCache); |
| 674 | fDetachedGlyphCache = nullptr; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 679 | size_t size = sizeof(TextRun) + fGlyphPathsKey.size(); |
| 680 | // The instance data always reserves enough space for every glyph. |
| 681 | size += (fTotalGlyphCount + fFallbackGlyphCount) * (sizeof(uint16_t) + 2 * sizeof(float)); |
| 682 | if (fInstanceData) { |
| 683 | size += sizeof(InstanceData); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 684 | } |
| 685 | if (fFallbackTextBlob) { |
| 686 | size += sizeof(SkTextBlob); |
| 687 | } |
| 688 | return size; |
| 689 | } |
| 690 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 691 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 692 | |
| 693 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font, |
| 694 | SkScalar textRatio) { |
| 695 | SkASSERT(!this->isInitialized()); |
| 696 | fBuilder.reset(new SkTextBlobBuilder); |
| 697 | fFont = font; |
| 698 | fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align. |
| 699 | fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 700 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs |
| 701 | // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 702 | fFont.setSubpixelText(false); |
| 703 | fFont.setTextSize(fFont.getTextSize() * textRatio); |
| 704 | fBuffIdx = 0; |
| 705 | } |
| 706 | |
| 707 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId, |
| 708 | const SkPoint& pos) { |
| 709 | SkASSERT(this->isInitialized()); |
| 710 | if (fBuffIdx >= kWriteBufferSize) { |
| 711 | this->flush(); |
| 712 | } |
| 713 | fGlyphIds[fBuffIdx] = glyphId; |
| 714 | fPositions[fBuffIdx] = pos; |
| 715 | fBuffIdx++; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 716 | fCount++; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() { |
| 720 | SkASSERT(this->isInitialized()); |
| 721 | SkASSERT(fBuffIdx <= kWriteBufferSize); |
| 722 | if (!fBuffIdx) { |
| 723 | return; |
| 724 | } |
| 725 | // This will automatically merge with previous runs since we use the same font. |
| 726 | const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx); |
| 727 | memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t)); |
| 728 | memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar)); |
| 729 | fBuffIdx = 0; |
| 730 | } |
| 731 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 732 | const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfNeeded(int *count) { |
| 733 | *count = fCount; |
| 734 | if (fCount) { |
| 735 | this->flush(); |
| 736 | return fBuilder->build(); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 737 | } |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 738 | return nullptr; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 739 | } |