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