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" |
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" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 21 | #include "SkPath.h" |
| 22 | #include "SkTextMapStateProc.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 23 | #include "SkTextFormatParams.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 24 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 25 | #include "batches/GrDrawPathBatch.h" |
| 26 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 27 | template<typename Key, typename Val> static void delete_hash_map_entry(const Key&, Val* val) { |
| 28 | SkASSERT(*val); |
| 29 | delete *val; |
| 30 | } |
| 31 | |
| 32 | template<typename T> static void delete_hash_table_entry(T* val) { |
| 33 | SkASSERT(*val); |
| 34 | delete *val; |
| 35 | } |
| 36 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 37 | GrStencilAndCoverTextContext::GrStencilAndCoverTextContext(GrContext* context, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 38 | const SkSurfaceProps& surfaceProps) |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 39 | : INHERITED(context, surfaceProps), |
| 40 | fCacheSize(0) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 41 | } |
| 42 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 43 | GrStencilAndCoverTextContext* |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 44 | GrStencilAndCoverTextContext::Create(GrContext* context, const SkSurfaceProps& surfaceProps) { |
| 45 | GrStencilAndCoverTextContext* textContext = |
| 46 | new GrStencilAndCoverTextContext(context, surfaceProps); |
| 47 | textContext->fFallbackTextContext = GrAtlasTextContext::Create(context, surfaceProps); |
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() { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 53 | fBlobIdCache.foreach(delete_hash_map_entry<uint32_t, TextBlob*>); |
| 54 | fBlobKeyCache.foreach(delete_hash_table_entry<TextBlob*>); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 55 | } |
| 56 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 57 | bool GrStencilAndCoverTextContext::internalCanDraw(const SkPaint& skPaint) { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 58 | if (skPaint.getRasterizer()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 61 | if (skPaint.getMaskFilter()) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 64 | if (SkPathEffect* pe = skPaint.getPathEffect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | if (pe->asADash(nullptr) != SkPathEffect::kDash_DashType) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 68 | } |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 69 | // No hairlines. They would require new paths with customized strokes for every new draw matrix. |
| 70 | return SkPaint::kStroke_Style != skPaint.getStyle() || 0 != skPaint.getStrokeWidth(); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 71 | } |
| 72 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 73 | void GrStencilAndCoverTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 74 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 75 | const GrPaint& paint, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 76 | const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 77 | const SkMatrix& viewMatrix, |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 78 | const char text[], |
| 79 | size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 80 | SkScalar x, SkScalar y, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 81 | const SkIRect& clipBounds) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 82 | TextRun run(skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 83 | GrPipelineBuilder pipelineBuilder(paint, rt, clip); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 84 | run.setText(text, byteLength, x, y); |
| 85 | run.draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, 0, 0, clipBounds, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 86 | fFallbackTextContext, skPaint); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 87 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 88 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 89 | void GrStencilAndCoverTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
| 90 | const GrClip& clip, |
| 91 | const GrPaint& paint, |
| 92 | const SkPaint& skPaint, |
| 93 | const SkMatrix& viewMatrix, |
| 94 | const char text[], |
| 95 | size_t byteLength, |
| 96 | const SkScalar pos[], |
| 97 | int scalarsPerPosition, |
| 98 | const SkPoint& offset, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 99 | const SkIRect& clipBounds) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 100 | TextRun run(skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 101 | GrPipelineBuilder pipelineBuilder(paint, rt, clip); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 102 | run.setPosText(text, byteLength, pos, scalarsPerPosition, offset); |
| 103 | run.draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, 0, 0, clipBounds, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 104 | fFallbackTextContext, skPaint); |
| 105 | } |
| 106 | |
| 107 | void GrStencilAndCoverTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt, |
| 108 | const GrClip& clip, const SkPaint& skPaint, |
| 109 | const SkMatrix& viewMatrix, |
| 110 | const SkTextBlob* skBlob, SkScalar x, SkScalar y, |
| 111 | SkDrawFilter* drawFilter, |
| 112 | const SkIRect& clipBounds) { |
| 113 | if (!this->internalCanDraw(skPaint)) { |
| 114 | fFallbackTextContext->drawTextBlob(dc, rt, clip, skPaint, viewMatrix, skBlob, x, y, |
| 115 | drawFilter, clipBounds); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (drawFilter || skPaint.getPathEffect()) { |
| 120 | // This draw can't be cached. |
| 121 | INHERITED::drawTextBlob(dc, rt, clip, skPaint, viewMatrix, skBlob, x, y, drawFilter, |
| 122 | clipBounds); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (fContext->abandoned()) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | GrPaint paint; |
| 131 | if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &paint)) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const TextBlob& blob = this->findOrCreateTextBlob(skBlob, skPaint); |
| 136 | GrPipelineBuilder pipelineBuilder(paint, rt, clip); |
| 137 | |
| 138 | TextBlob::Iter iter(blob); |
| 139 | for (TextRun* run = iter.get(); run; run = iter.next()) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 140 | run->draw(fContext, dc, &pipelineBuilder, paint.getColor(), viewMatrix, x, y, clipBounds, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 141 | fFallbackTextContext, skPaint); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 142 | run->releaseGlyphCache(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | const GrStencilAndCoverTextContext::TextBlob& |
| 147 | GrStencilAndCoverTextContext::findOrCreateTextBlob(const SkTextBlob* skBlob, |
| 148 | const SkPaint& skPaint) { |
| 149 | // The font-related parameters are baked into the text blob and will override this skPaint, so |
| 150 | // the only remaining properties that can affect a TextBlob are the ones related to stroke. |
| 151 | if (SkPaint::kFill_Style == skPaint.getStyle()) { // Fast path. |
| 152 | if (TextBlob** found = fBlobIdCache.find(skBlob->uniqueID())) { |
| 153 | fLRUList.remove(*found); |
| 154 | fLRUList.addToTail(*found); |
| 155 | return **found; |
| 156 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 157 | TextBlob* blob = new TextBlob(skBlob->uniqueID(), skBlob, skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 158 | this->purgeToFit(*blob); |
| 159 | fBlobIdCache.set(skBlob->uniqueID(), blob); |
| 160 | fLRUList.addToTail(blob); |
| 161 | fCacheSize += blob->cpuMemorySize(); |
| 162 | return *blob; |
| 163 | } else { |
| 164 | GrStrokeInfo stroke(skPaint); |
| 165 | SkSTArray<4, uint32_t, true> key; |
| 166 | key.reset(1 + stroke.computeUniqueKeyFragmentData32Cnt()); |
| 167 | key[0] = skBlob->uniqueID(); |
| 168 | stroke.asUniqueKeyFragment(&key[1]); |
| 169 | if (TextBlob** found = fBlobKeyCache.find(key)) { |
| 170 | fLRUList.remove(*found); |
| 171 | fLRUList.addToTail(*found); |
| 172 | return **found; |
| 173 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 174 | TextBlob* blob = new TextBlob(key, skBlob, skPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 175 | this->purgeToFit(*blob); |
| 176 | fBlobKeyCache.set(blob); |
| 177 | fLRUList.addToTail(blob); |
| 178 | fCacheSize += blob->cpuMemorySize(); |
| 179 | return *blob; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void GrStencilAndCoverTextContext::purgeToFit(const TextBlob& blob) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 184 | 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] | 185 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 186 | size_t maxSizeForNewBlob = maxCacheSize - blob.cpuMemorySize(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 187 | while (fCacheSize && fCacheSize > maxSizeForNewBlob) { |
| 188 | TextBlob* lru = fLRUList.head(); |
| 189 | if (1 == lru->key().count()) { |
| 190 | // 1-length keys are unterstood to be the blob id. |
| 191 | fBlobIdCache.remove(lru->key()[0]); |
| 192 | } else { |
| 193 | fBlobKeyCache.remove(lru->key()); |
| 194 | } |
| 195 | fLRUList.remove(lru); |
| 196 | fCacheSize -= lru->cpuMemorySize(); |
| 197 | delete lru; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 202 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 203 | void GrStencilAndCoverTextContext::TextBlob::init(const SkTextBlob* skBlob, |
| 204 | const SkPaint& skPaint) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 205 | fCpuMemorySize = sizeof(TextBlob); |
| 206 | SkPaint runPaint(skPaint); |
| 207 | for (SkTextBlob::RunIterator iter(skBlob); !iter.done(); iter.next()) { |
| 208 | iter.applyFontToPaint(&runPaint); // No need to re-seed the paint. |
| 209 | TextRun* run = SkNEW_INSERT_AT_LLIST_TAIL(this, TextRun, (runPaint)); |
| 210 | |
| 211 | const char* text = reinterpret_cast<const char*>(iter.glyphs()); |
| 212 | size_t byteLength = sizeof(uint16_t) * iter.glyphCount(); |
| 213 | const SkPoint& runOffset = iter.offset(); |
| 214 | |
| 215 | switch (iter.positioning()) { |
| 216 | case SkTextBlob::kDefault_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 217 | run->setText(text, byteLength, runOffset.fX, runOffset.fY); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 218 | break; |
| 219 | case SkTextBlob::kHorizontal_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 220 | run->setPosText(text, byteLength, iter.pos(), 1, SkPoint::Make(0, runOffset.fY)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 221 | break; |
| 222 | case SkTextBlob::kFull_Positioning: |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 223 | run->setPosText(text, byteLength, iter.pos(), 2, SkPoint::Make(0, 0)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 227 | fCpuMemorySize += run->computeSizeInCache(); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 228 | } |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 232 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 233 | class GrStencilAndCoverTextContext::FallbackBlobBuilder { |
| 234 | public: |
| 235 | FallbackBlobBuilder() : fBuffIdx(0) {} |
| 236 | |
| 237 | bool isInitialized() const { return SkToBool(fBuilder); } |
| 238 | |
| 239 | void init(const SkPaint& font, SkScalar textRatio); |
| 240 | |
| 241 | void appendGlyph(uint16_t glyphId, const SkPoint& pos); |
| 242 | |
| 243 | const SkTextBlob* buildIfInitialized(); |
| 244 | |
| 245 | private: |
| 246 | enum { kWriteBufferSize = 1024 }; |
| 247 | |
| 248 | void flush(); |
| 249 | |
| 250 | SkAutoTDelete<SkTextBlobBuilder> fBuilder; |
| 251 | SkPaint fFont; |
| 252 | int fBuffIdx; |
| 253 | uint16_t fGlyphIds[kWriteBufferSize]; |
| 254 | SkPoint fPositions[kWriteBufferSize]; |
| 255 | }; |
| 256 | |
| 257 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 258 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 259 | GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke) |
| 260 | : fStroke(fontAndStroke), |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 261 | fFont(fontAndStroke), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 262 | fTotalGlyphCount(0), |
| 263 | fDetachedGlyphCache(nullptr), |
| 264 | fLastDrawnGlyphsID(SK_InvalidUniqueID) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 265 | SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported. |
| 266 | |
| 267 | // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path |
| 268 | // rendering API for stroking). |
| 269 | fFont.setStyle(SkPaint::kFill_Style); |
| 270 | |
| 271 | if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
| 272 | // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke. |
| 273 | SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(), |
| 274 | kStdFakeBoldInterpKeys, |
| 275 | kStdFakeBoldInterpValues, |
| 276 | kStdFakeBoldInterpLength); |
| 277 | SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale); |
| 278 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 279 | true /*strokeAndFill*/); |
| 280 | |
| 281 | fFont.setFakeBoldText(false); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 284 | if (!fFont.getPathEffect() && !fStroke.isDashed()) { |
| 285 | // We can draw the glyphs from canonically sized paths. |
| 286 | fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 287 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 288 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 289 | // Compensate for the glyphs being scaled by fTextRatio. |
| 290 | if (!fStroke.isFillStyle()) { |
| 291 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 292 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
| 293 | } |
| 294 | |
| 295 | fFont.setLinearText(true); |
| 296 | fFont.setLCDRenderText(false); |
| 297 | fFont.setAutohinted(false); |
| 298 | fFont.setHinting(SkPaint::kNo_Hinting); |
| 299 | fFont.setSubpixelText(true); |
| 300 | fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 301 | |
| 302 | fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() && |
| 303 | 0 == fFont.getTextSkewX() && |
| 304 | !fFont.isFakeBoldText() && |
| 305 | !fFont.isVerticalText(); |
| 306 | } else { |
| 307 | fTextRatio = fTextInverseRatio = 1.0f; |
| 308 | fUsingRawGlyphPaths = false; |
| 309 | } |
| 310 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 311 | // Generate the key that will be used to cache the GPU glyph path objects. |
| 312 | if (fUsingRawGlyphPaths && fStroke.isFillStyle()) { |
| 313 | static const GrUniqueKey::Domain kRawFillPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 314 | |
| 315 | const SkTypeface* typeface = fFont.getTypeface(); |
| 316 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kRawFillPathGlyphDomain, 1); |
| 317 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 318 | } else { |
| 319 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 320 | |
| 321 | int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt(); |
| 322 | if (fUsingRawGlyphPaths) { |
| 323 | const SkTypeface* typeface = fFont.getTypeface(); |
| 324 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, 2 + strokeDataCount); |
| 325 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 326 | reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount; |
| 327 | fStroke.asUniqueKeyFragment(&builder[2]); |
| 328 | } else { |
| 329 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
| 330 | const SkTypeface* typeface = glyphCache->getScalerContext()->getTypeface(); |
| 331 | const SkDescriptor* desc = &glyphCache->getDescriptor(); |
| 332 | int descDataCount = (desc->getLength() + 3) / 4; |
| 333 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, |
| 334 | 2 + strokeDataCount + descDataCount); |
| 335 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 336 | reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount | (descDataCount << 16); |
| 337 | fStroke.asUniqueKeyFragment(&builder[2]); |
| 338 | memcpy(&builder[2 + strokeDataCount], desc, desc->getLength()); |
| 339 | } |
| 340 | } |
| 341 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 342 | // When drawing from canonically sized paths, the actual local coords are fTextRatio * coords. |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 343 | fLocalMatrixTemplate.setScale(fTextRatio, fTextRatio); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | GrStencilAndCoverTextContext::TextRun::~TextRun() { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 347 | this->releaseGlyphCache(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t byteLength, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 351 | SkScalar x, SkScalar y) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 352 | SkASSERT(byteLength == 0 || text != nullptr); |
| 353 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 354 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 355 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 356 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 357 | fDraw.reset(GrPathRangeDraw::Create(GrPathRendering::kTranslate_PathTransformType, |
| 358 | fTotalGlyphCount = fFont.countText(text, byteLength))); |
| 359 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 360 | const char* stop = text + byteLength; |
| 361 | |
| 362 | // Measure first if needed. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 363 | if (fFont.getTextAlign() != SkPaint::kLeft_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 364 | SkFixed stopX = 0; |
| 365 | SkFixed stopY = 0; |
| 366 | |
| 367 | const char* textPtr = text; |
| 368 | while (textPtr < stop) { |
| 369 | // We don't need x, y here, since all subpixel variants will have the |
| 370 | // same advance. |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 371 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &textPtr, 0, 0); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 372 | |
| 373 | stopX += glyph.fAdvanceX; |
| 374 | stopY += glyph.fAdvanceY; |
| 375 | } |
| 376 | SkASSERT(textPtr == stop); |
| 377 | |
| 378 | SkScalar alignX = SkFixedToScalar(stopX) * fTextRatio; |
| 379 | SkScalar alignY = SkFixedToScalar(stopY) * fTextRatio; |
| 380 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 381 | if (fFont.getTextAlign() == SkPaint::kCenter_Align) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 382 | alignX = SkScalarHalf(alignX); |
| 383 | alignY = SkScalarHalf(alignY); |
| 384 | } |
| 385 | |
| 386 | x -= alignX; |
| 387 | y -= alignY; |
| 388 | } |
| 389 | |
| 390 | SkAutoKern autokern; |
| 391 | |
| 392 | SkFixed fixedSizeRatio = SkScalarToFixed(fTextRatio); |
| 393 | |
| 394 | SkFixed fx = SkScalarToFixed(x); |
| 395 | SkFixed fy = SkScalarToFixed(y); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 396 | FallbackBlobBuilder fallback; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 397 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 398 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 399 | fx += SkFixedMul(autokern.adjust(glyph), fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 400 | if (glyph.fWidth) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 401 | this->appendGlyph(glyph, SkPoint::Make(SkFixedToScalar(fx), SkFixedToScalar(fy)), |
| 402 | &fallback); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 403 | } |
| 404 | |
bungeman | d709ea8 | 2015-03-17 07:23:39 -0700 | [diff] [blame] | 405 | fx += SkFixedMul(glyph.fAdvanceX, fixedSizeRatio); |
| 406 | fy += SkFixedMul(glyph.fAdvanceY, fixedSizeRatio); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 407 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 408 | |
| 409 | fFallbackTextBlob.reset(fallback.buildIfInitialized()); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 410 | } |
| 411 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 412 | void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t byteLength, |
| 413 | const SkScalar pos[], int scalarsPerPosition, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 414 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 415 | SkASSERT(byteLength == 0 || text != nullptr); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 416 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 417 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 418 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 419 | SkDrawCacheProc glyphCacheProc = fFont.getDrawCacheProc(); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 420 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 421 | fDraw.reset(GrPathRangeDraw::Create(GrPathRendering::kTranslate_PathTransformType, |
| 422 | fTotalGlyphCount = fFont.countText(text, byteLength))); |
| 423 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 424 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 425 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 426 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 427 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 428 | FallbackBlobBuilder fallback; |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 429 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 430 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 431 | if (glyph.fWidth) { |
| 432 | SkPoint tmsLoc; |
| 433 | tmsProc(pos, &tmsLoc); |
| 434 | SkPoint loc; |
| 435 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 436 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 437 | this->appendGlyph(glyph, loc, &fallback); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 438 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 439 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 440 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 441 | |
| 442 | fFallbackTextBlob.reset(fallback.buildIfInitialized()); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 443 | } |
| 444 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 445 | GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx) const { |
| 446 | GrPathRange* glyphs = static_cast<GrPathRange*>( |
| 447 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(fGlyphPathsKey)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 448 | if (nullptr == glyphs) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 449 | if (fUsingRawGlyphPaths) { |
| 450 | glyphs = ctx->resourceProvider()->createGlyphs(fFont.getTypeface(), nullptr, fStroke); |
| 451 | } else { |
| 452 | SkGlyphCache* cache = this->getGlyphCache(); |
| 453 | glyphs = ctx->resourceProvider()->createGlyphs(cache->getScalerContext()->getTypeface(), |
| 454 | &cache->getDescriptor(), |
| 455 | fStroke); |
| 456 | } |
| 457 | ctx->resourceProvider()->assignUniqueKeyToResource(fGlyphPathsKey, glyphs); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 458 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 459 | return glyphs; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 460 | } |
| 461 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 462 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 463 | const SkPoint& pos, |
| 464 | FallbackBlobBuilder* fallback) { |
| 465 | // Stick the glyphs we can't draw into the fallback text blob. |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 466 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 467 | if (!fallback->isInitialized()) { |
| 468 | fallback->init(fFont, fTextRatio); |
| 469 | } |
| 470 | fallback->appendGlyph(glyph.getGlyphID(), pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 471 | } else { |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 472 | float translate[] = { fTextInverseRatio * pos.x(), fTextInverseRatio * pos.y() }; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 473 | fDraw->append(glyph.getGlyphID(), translate); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 474 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 475 | } |
| 476 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 477 | void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx, |
| 478 | GrDrawContext* dc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 479 | GrPipelineBuilder* pipelineBuilder, |
| 480 | GrColor color, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 481 | const SkMatrix& viewMatrix, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 482 | SkScalar x, SkScalar y, |
| 483 | const SkIRect& clipBounds, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 484 | GrTextContext* fallbackTextContext, |
| 485 | const SkPaint& originalSkPaint) const { |
| 486 | SkASSERT(fDraw); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 487 | SkASSERT(pipelineBuilder->getRenderTarget()->isStencilBufferMultisampled() || |
| 488 | !fFont.isAntiAlias()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 489 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 490 | if (fDraw->count()) { |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 491 | pipelineBuilder->setState(GrPipelineBuilder::kHWAntialias_Flag, fFont.isAntiAlias()); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 492 | |
| 493 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 494 | kZero_StencilOp, |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 495 | kKeep_StencilOp, |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 496 | kNotEqual_StencilFunc, |
| 497 | 0xffff, |
| 498 | 0x0000, |
| 499 | 0xffff); |
| 500 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 501 | *pipelineBuilder->stencil() = kStencilPass; |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 502 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 503 | SkAutoTUnref<GrPathRange> glyphs(this->createGlyphs(ctx)); |
| 504 | if (fLastDrawnGlyphsID != glyphs->getUniqueID()) { |
| 505 | // Either this is the first draw or the glyphs object was purged since last draw. |
| 506 | glyphs->loadPathsIfNeeded(fDraw->indices(), fDraw->count()); |
| 507 | fLastDrawnGlyphsID = glyphs->getUniqueID(); |
| 508 | } |
| 509 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 510 | SkMatrix drawMatrix(viewMatrix); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 511 | drawMatrix.preTranslate(x, y); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 512 | drawMatrix.preScale(fTextRatio, fTextRatio); |
| 513 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 514 | SkMatrix& localMatrix = fLocalMatrixTemplate; |
| 515 | localMatrix.setTranslateX(x); |
| 516 | localMatrix.setTranslateY(y); |
| 517 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 518 | dc->drawPathsFromRange(pipelineBuilder, drawMatrix, localMatrix, color, glyphs, fDraw, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 519 | GrPathRendering::kWinding_FillType); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 520 | } |
| 521 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 522 | if (fFallbackTextBlob) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 523 | SkPaint fallbackSkPaint(originalSkPaint); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 524 | fStroke.applyToPaint(&fallbackSkPaint); |
| 525 | if (!fStroke.isFillStyle()) { |
| 526 | fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 527 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 528 | |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 529 | fallbackTextContext->drawTextBlob(dc, pipelineBuilder->getRenderTarget(), |
| 530 | pipelineBuilder->clip(), fallbackSkPaint, viewMatrix, |
| 531 | fFallbackTextBlob, x, y, nullptr, clipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 532 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 533 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 534 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame^] | 535 | SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const { |
| 536 | if (!fDetachedGlyphCache) { |
| 537 | fDetachedGlyphCache = fFont.detachCache(nullptr, nullptr, true /*ignoreGamma*/); |
| 538 | } |
| 539 | return fDetachedGlyphCache; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const { |
| 544 | if (fDetachedGlyphCache) { |
| 545 | SkGlyphCache::AttachCache(fDetachedGlyphCache); |
| 546 | fDetachedGlyphCache = nullptr; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const { |
| 551 | size_t size = sizeof(TextRun) + |
| 552 | fGlyphPathsKey.size() + |
| 553 | fTotalGlyphCount * (sizeof(uint16_t) + 2 * sizeof(float)); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 554 | if (fDraw) { |
| 555 | size += sizeof(GrPathRangeDraw); |
| 556 | } |
| 557 | if (fFallbackTextBlob) { |
| 558 | size += sizeof(SkTextBlob); |
| 559 | } |
| 560 | return size; |
| 561 | } |
| 562 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 563 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 564 | |
| 565 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font, |
| 566 | SkScalar textRatio) { |
| 567 | SkASSERT(!this->isInitialized()); |
| 568 | fBuilder.reset(new SkTextBlobBuilder); |
| 569 | fFont = font; |
| 570 | fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align. |
| 571 | fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 572 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs |
| 573 | // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 574 | fFont.setSubpixelText(false); |
| 575 | fFont.setTextSize(fFont.getTextSize() * textRatio); |
| 576 | fBuffIdx = 0; |
| 577 | } |
| 578 | |
| 579 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId, |
| 580 | const SkPoint& pos) { |
| 581 | SkASSERT(this->isInitialized()); |
| 582 | if (fBuffIdx >= kWriteBufferSize) { |
| 583 | this->flush(); |
| 584 | } |
| 585 | fGlyphIds[fBuffIdx] = glyphId; |
| 586 | fPositions[fBuffIdx] = pos; |
| 587 | fBuffIdx++; |
| 588 | } |
| 589 | |
| 590 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() { |
| 591 | SkASSERT(this->isInitialized()); |
| 592 | SkASSERT(fBuffIdx <= kWriteBufferSize); |
| 593 | if (!fBuffIdx) { |
| 594 | return; |
| 595 | } |
| 596 | // This will automatically merge with previous runs since we use the same font. |
| 597 | const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx); |
| 598 | memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t)); |
| 599 | memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar)); |
| 600 | fBuffIdx = 0; |
| 601 | } |
| 602 | |
| 603 | const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfInitialized() { |
| 604 | if (!this->isInitialized()) { |
| 605 | return nullptr; |
| 606 | } |
| 607 | this->flush(); |
| 608 | return fBuilder->build(); |
| 609 | } |