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 | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 74 | void GrStencilAndCoverTextContext::onDrawText(GrDrawContext* dc, |
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); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 84 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), 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 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 90 | void GrStencilAndCoverTextContext::onDrawPosText(GrDrawContext* dc, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 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); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 102 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), 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 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 108 | void GrStencilAndCoverTextContext::drawTextBlob(GrDrawContext* dc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 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)) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 115 | fFallbackTextContext->drawTextBlob(dc, clip, skPaint, viewMatrix, skBlob, x, y, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 116 | drawFilter, clipBounds); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (drawFilter || skPaint.getPathEffect()) { |
| 121 | // This draw can't be cached. |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 122 | INHERITED::drawTextBlob(dc, clip, skPaint, viewMatrix, skBlob, x, y, drawFilter, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 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); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 137 | GrPipelineBuilder pipelineBuilder(paint, dc->accessRenderTarget(), clip); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 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. |
bsalomon | 5aaef1f | 2015-11-18 14:11:08 -0800 | [diff] [blame] | 210 | TextRun* run = this->addToTail(runPaint); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 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: |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 236 | FallbackBlobBuilder() : fBuffIdx(0), fCount(0) {} |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 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 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 244 | const SkTextBlob* buildIfNeeded(int* count); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 245 | |
| 246 | private: |
| 247 | enum { kWriteBufferSize = 1024 }; |
| 248 | |
| 249 | void flush(); |
| 250 | |
| 251 | SkAutoTDelete<SkTextBlobBuilder> fBuilder; |
| 252 | SkPaint fFont; |
| 253 | int fBuffIdx; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 254 | int fCount; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 255 | uint16_t fGlyphIds[kWriteBufferSize]; |
| 256 | SkPoint fPositions[kWriteBufferSize]; |
| 257 | }; |
| 258 | |
| 259 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 260 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 261 | GrStencilAndCoverTextContext::TextRun::TextRun(const SkPaint& fontAndStroke) |
| 262 | : fStroke(fontAndStroke), |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 263 | fFont(fontAndStroke), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 264 | fTotalGlyphCount(0), |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 265 | fFallbackGlyphCount(0), |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 266 | fDetachedGlyphCache(nullptr), |
| 267 | fLastDrawnGlyphsID(SK_InvalidUniqueID) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 268 | SkASSERT(!fStroke.isHairlineStyle()); // Hairlines are not supported. |
| 269 | |
| 270 | // Setting to "fill" ensures that no strokes get baked into font outlines. (We use the GPU path |
| 271 | // rendering API for stroking). |
| 272 | fFont.setStyle(SkPaint::kFill_Style); |
| 273 | |
| 274 | if (fFont.isFakeBoldText() && SkStrokeRec::kStroke_Style != fStroke.getStyle()) { |
| 275 | // Instead of letting fake bold get baked into the glyph outlines, do it with GPU stroke. |
| 276 | SkScalar fakeBoldScale = SkScalarInterpFunc(fFont.getTextSize(), |
| 277 | kStdFakeBoldInterpKeys, |
| 278 | kStdFakeBoldInterpValues, |
| 279 | kStdFakeBoldInterpLength); |
| 280 | SkScalar extra = SkScalarMul(fFont.getTextSize(), fakeBoldScale); |
| 281 | fStroke.setStrokeStyle(fStroke.needToApply() ? fStroke.getWidth() + extra : extra, |
| 282 | true /*strokeAndFill*/); |
| 283 | |
| 284 | fFont.setFakeBoldText(false); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 285 | } |
| 286 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 287 | if (!fFont.getPathEffect() && !fStroke.isDashed()) { |
| 288 | // We can draw the glyphs from canonically sized paths. |
| 289 | fTextRatio = fFont.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
| 290 | fTextInverseRatio = SkPaint::kCanonicalTextSizeForPaths / fFont.getTextSize(); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 291 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 292 | // Compensate for the glyphs being scaled by fTextRatio. |
| 293 | if (!fStroke.isFillStyle()) { |
| 294 | fStroke.setStrokeStyle(fStroke.getWidth() / fTextRatio, |
| 295 | SkStrokeRec::kStrokeAndFill_Style == fStroke.getStyle()); |
| 296 | } |
| 297 | |
| 298 | fFont.setLinearText(true); |
| 299 | fFont.setLCDRenderText(false); |
| 300 | fFont.setAutohinted(false); |
| 301 | fFont.setHinting(SkPaint::kNo_Hinting); |
| 302 | fFont.setSubpixelText(true); |
| 303 | fFont.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 304 | |
| 305 | fUsingRawGlyphPaths = SK_Scalar1 == fFont.getTextScaleX() && |
| 306 | 0 == fFont.getTextSkewX() && |
| 307 | !fFont.isFakeBoldText() && |
| 308 | !fFont.isVerticalText(); |
| 309 | } else { |
| 310 | fTextRatio = fTextInverseRatio = 1.0f; |
| 311 | fUsingRawGlyphPaths = false; |
| 312 | } |
| 313 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 314 | // Generate the key that will be used to cache the GPU glyph path objects. |
| 315 | if (fUsingRawGlyphPaths && fStroke.isFillStyle()) { |
| 316 | static const GrUniqueKey::Domain kRawFillPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 317 | |
| 318 | const SkTypeface* typeface = fFont.getTypeface(); |
| 319 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kRawFillPathGlyphDomain, 1); |
| 320 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 321 | } else { |
| 322 | static const GrUniqueKey::Domain kPathGlyphDomain = GrUniqueKey::GenerateDomain(); |
| 323 | |
| 324 | int strokeDataCount = fStroke.computeUniqueKeyFragmentData32Cnt(); |
| 325 | if (fUsingRawGlyphPaths) { |
| 326 | const SkTypeface* typeface = fFont.getTypeface(); |
| 327 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, 2 + strokeDataCount); |
| 328 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 329 | reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount; |
| 330 | fStroke.asUniqueKeyFragment(&builder[2]); |
| 331 | } else { |
| 332 | SkGlyphCache* glyphCache = this->getGlyphCache(); |
| 333 | const SkTypeface* typeface = glyphCache->getScalerContext()->getTypeface(); |
| 334 | const SkDescriptor* desc = &glyphCache->getDescriptor(); |
| 335 | int descDataCount = (desc->getLength() + 3) / 4; |
| 336 | GrUniqueKey::Builder builder(&fGlyphPathsKey, kPathGlyphDomain, |
| 337 | 2 + strokeDataCount + descDataCount); |
| 338 | reinterpret_cast<uint32_t&>(builder[0]) = typeface ? typeface->uniqueID() : 0; |
| 339 | reinterpret_cast<uint32_t&>(builder[1]) = strokeDataCount | (descDataCount << 16); |
| 340 | fStroke.asUniqueKeyFragment(&builder[2]); |
| 341 | memcpy(&builder[2 + strokeDataCount], desc, desc->getLength()); |
| 342 | } |
| 343 | } |
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 | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 357 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 358 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 359 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 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 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 410 | fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount)); |
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 | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 422 | fTotalGlyphCount = fFont.countText(text, byteLength); |
| 423 | fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, |
| 424 | fTotalGlyphCount)); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 425 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 426 | const char* stop = text + byteLength; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 427 | |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 428 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 429 | SkTextAlignProc alignProc(fFont.getTextAlign()); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 430 | FallbackBlobBuilder fallback; |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 431 | while (text < stop) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 432 | const SkGlyph& glyph = glyphCacheProc(glyphCache, &text, 0, 0); |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 433 | if (glyph.fWidth) { |
| 434 | SkPoint tmsLoc; |
| 435 | tmsProc(pos, &tmsLoc); |
| 436 | SkPoint loc; |
| 437 | alignProc(tmsLoc, glyph, &loc); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 438 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 439 | this->appendGlyph(glyph, loc, &fallback); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 440 | } |
cdalton | 38e13ad | 2014-11-07 06:02:15 -0800 | [diff] [blame] | 441 | pos += scalarsPerPosition; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 442 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 443 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 444 | fFallbackTextBlob.reset(fallback.buildIfNeeded(&fFallbackGlyphCount)); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 445 | } |
| 446 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 447 | GrPathRange* GrStencilAndCoverTextContext::TextRun::createGlyphs(GrContext* ctx) const { |
| 448 | GrPathRange* glyphs = static_cast<GrPathRange*>( |
| 449 | ctx->resourceProvider()->findAndRefResourceByUniqueKey(fGlyphPathsKey)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 450 | if (nullptr == glyphs) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 451 | if (fUsingRawGlyphPaths) { |
| 452 | glyphs = ctx->resourceProvider()->createGlyphs(fFont.getTypeface(), nullptr, fStroke); |
| 453 | } else { |
| 454 | SkGlyphCache* cache = this->getGlyphCache(); |
| 455 | glyphs = ctx->resourceProvider()->createGlyphs(cache->getScalerContext()->getTypeface(), |
| 456 | &cache->getDescriptor(), |
| 457 | fStroke); |
| 458 | } |
| 459 | ctx->resourceProvider()->assignUniqueKeyToResource(fGlyphPathsKey, glyphs); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 460 | } |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 461 | return glyphs; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 462 | } |
| 463 | |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 464 | inline void GrStencilAndCoverTextContext::TextRun::appendGlyph(const SkGlyph& glyph, |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 465 | const SkPoint& pos, |
| 466 | FallbackBlobBuilder* fallback) { |
| 467 | // Stick the glyphs we can't draw into the fallback text blob. |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 468 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 469 | if (!fallback->isInitialized()) { |
| 470 | fallback->init(fFont, fTextRatio); |
| 471 | } |
| 472 | fallback->appendGlyph(glyph.getGlyphID(), pos); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 473 | } else { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 474 | fInstanceData->append(glyph.getGlyphID(), fTextInverseRatio * pos.x(), |
| 475 | fTextInverseRatio * pos.y()); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 476 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 477 | } |
| 478 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 479 | void GrStencilAndCoverTextContext::TextRun::draw(GrContext* ctx, |
| 480 | GrDrawContext* dc, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 481 | GrPipelineBuilder* pipelineBuilder, |
| 482 | GrColor color, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 483 | const SkMatrix& viewMatrix, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 484 | SkScalar x, SkScalar y, |
| 485 | const SkIRect& clipBounds, |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 486 | GrTextContext* fallbackTextContext, |
| 487 | const SkPaint& originalSkPaint) const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 488 | SkASSERT(fInstanceData); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 489 | SkASSERT(dc->accessRenderTarget()->isStencilBufferMultisampled() || !fFont.isAntiAlias()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 490 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 491 | if (fInstanceData->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. |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 507 | glyphs->loadPathsIfNeeded(fInstanceData->indices(), fInstanceData->count()); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 508 | fLastDrawnGlyphsID = glyphs->getUniqueID(); |
| 509 | } |
| 510 | |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame] | 511 | // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy |
| 512 | // the entire dst. Realistically this is a moot point, because any context that supports |
| 513 | // NV_path_rendering will also support NV_blend_equation_advanced. |
| 514 | // For clipping we'll just skip any optimizations based on the bounds. This does, however, |
| 515 | // hurt batching. |
| 516 | SkRect bounds = SkRect::MakeIWH(pipelineBuilder->getRenderTarget()->width(), |
| 517 | pipelineBuilder->getRenderTarget()->height()); |
| 518 | |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 519 | SkAutoTUnref<GrDrawPathBatchBase> batch( |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 520 | GrDrawPathRangeBatch::Create(viewMatrix, fTextRatio, fTextInverseRatio * x, |
| 521 | fTextInverseRatio * y, color, |
| 522 | GrPathRendering::kWinding_FillType, glyphs, fInstanceData, |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 523 | bounds)); |
| 524 | |
| 525 | dc->drawPathBatch(*pipelineBuilder, batch); |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 526 | } |
| 527 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 528 | if (fFallbackTextBlob) { |
cdalton | 3bd909a | 2015-10-05 14:57:20 -0700 | [diff] [blame] | 529 | SkPaint fallbackSkPaint(originalSkPaint); |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 530 | fStroke.applyToPaint(&fallbackSkPaint); |
| 531 | if (!fStroke.isFillStyle()) { |
| 532 | fallbackSkPaint.setStrokeWidth(fStroke.getWidth() * fTextRatio); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 533 | } |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 534 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 535 | fallbackTextContext->drawTextBlob(dc, pipelineBuilder->clip(), fallbackSkPaint, viewMatrix, |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 536 | fFallbackTextBlob, x, y, nullptr, clipBounds); |
cdalton | 20b373c | 2014-12-01 08:38:55 -0800 | [diff] [blame] | 537 | } |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 538 | } |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 539 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 540 | SkGlyphCache* GrStencilAndCoverTextContext::TextRun::getGlyphCache() const { |
| 541 | if (!fDetachedGlyphCache) { |
| 542 | fDetachedGlyphCache = fFont.detachCache(nullptr, nullptr, true /*ignoreGamma*/); |
| 543 | } |
| 544 | return fDetachedGlyphCache; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | void GrStencilAndCoverTextContext::TextRun::releaseGlyphCache() const { |
| 549 | if (fDetachedGlyphCache) { |
| 550 | SkGlyphCache::AttachCache(fDetachedGlyphCache); |
| 551 | fDetachedGlyphCache = nullptr; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | size_t GrStencilAndCoverTextContext::TextRun::computeSizeInCache() const { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 556 | size_t size = sizeof(TextRun) + fGlyphPathsKey.size(); |
| 557 | // The instance data always reserves enough space for every glyph. |
| 558 | size += (fTotalGlyphCount + fFallbackGlyphCount) * (sizeof(uint16_t) + 2 * sizeof(float)); |
| 559 | if (fInstanceData) { |
| 560 | size += sizeof(InstanceData); |
cdalton | cdd7907 | 2015-10-05 15:37:35 -0700 | [diff] [blame] | 561 | } |
| 562 | if (fFallbackTextBlob) { |
| 563 | size += sizeof(SkTextBlob); |
| 564 | } |
| 565 | return size; |
| 566 | } |
| 567 | |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 568 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 569 | |
| 570 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::init(const SkPaint& font, |
| 571 | SkScalar textRatio) { |
| 572 | SkASSERT(!this->isInitialized()); |
| 573 | fBuilder.reset(new SkTextBlobBuilder); |
| 574 | fFont = font; |
| 575 | fFont.setTextAlign(SkPaint::kLeft_Align); // The glyph positions will already account for align. |
| 576 | fFont.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 577 | // No need for subpixel positioning with bitmap glyphs. TODO: revisit if non-bitmap color glyphs |
| 578 | // show up and https://code.google.com/p/skia/issues/detail?id=4408 gets resolved. |
| 579 | fFont.setSubpixelText(false); |
| 580 | fFont.setTextSize(fFont.getTextSize() * textRatio); |
| 581 | fBuffIdx = 0; |
| 582 | } |
| 583 | |
| 584 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::appendGlyph(uint16_t glyphId, |
| 585 | const SkPoint& pos) { |
| 586 | SkASSERT(this->isInitialized()); |
| 587 | if (fBuffIdx >= kWriteBufferSize) { |
| 588 | this->flush(); |
| 589 | } |
| 590 | fGlyphIds[fBuffIdx] = glyphId; |
| 591 | fPositions[fBuffIdx] = pos; |
| 592 | fBuffIdx++; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 593 | fCount++; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | void GrStencilAndCoverTextContext::FallbackBlobBuilder::flush() { |
| 597 | SkASSERT(this->isInitialized()); |
| 598 | SkASSERT(fBuffIdx <= kWriteBufferSize); |
| 599 | if (!fBuffIdx) { |
| 600 | return; |
| 601 | } |
| 602 | // This will automatically merge with previous runs since we use the same font. |
| 603 | const SkTextBlobBuilder::RunBuffer& buff = fBuilder->allocRunPos(fFont, fBuffIdx); |
| 604 | memcpy(buff.glyphs, fGlyphIds, fBuffIdx * sizeof(uint16_t)); |
| 605 | memcpy(buff.pos, fPositions[0].asScalars(), fBuffIdx * 2 * sizeof(SkScalar)); |
| 606 | fBuffIdx = 0; |
| 607 | } |
| 608 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 609 | const SkTextBlob* GrStencilAndCoverTextContext::FallbackBlobBuilder::buildIfNeeded(int *count) { |
| 610 | *count = fCount; |
| 611 | if (fCount) { |
| 612 | this->flush(); |
| 613 | return fBuilder->build(); |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 614 | } |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 615 | return nullptr; |
cdalton | 02015e5 | 2015-10-05 15:28:20 -0700 | [diff] [blame] | 616 | } |