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