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