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