joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #include "GrAtlasTextContext.h" |
| 8 | |
robertphillips | 73c4e64 | 2016-03-02 11:36:59 -0800 | [diff] [blame] | 9 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 11 | #include "GrTextBlobCache.h" |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 12 | #include "GrTextUtils.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 13 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 14 | #include "SkDraw.h" |
| 15 | #include "SkDrawFilter.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 16 | #include "SkGrPriv.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 17 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 18 | GrAtlasTextContext::GrAtlasTextContext() |
| 19 | : fDistanceAdjustTable(new GrDistanceFieldAdjustTable) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 20 | } |
| 21 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 22 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 23 | GrAtlasTextContext* GrAtlasTextContext::Create() { |
| 24 | return new GrAtlasTextContext(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 25 | } |
| 26 | |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 27 | bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, |
| 28 | const SkMatrix& viewMatrix, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 29 | const SkSurfaceProps& props, |
| 30 | const GrShaderCaps& shaderCaps) { |
| 31 | return GrTextUtils::CanDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps) || |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 32 | !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 33 | } |
| 34 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 35 | GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
| 36 | GrColor canonicalColor = paint.computeLuminanceColor(); |
| 37 | if (lcd) { |
| 38 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 39 | // For now we just regenerate if any run in a textblob has LCD. |
| 40 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 41 | // at a higher level *OR* use sRGB |
| 42 | SkASSERT(false); |
| 43 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 44 | } else { |
| 45 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 46 | // gamma corrected masks anyways, nor color |
| 47 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 48 | SkColorGetG(canonicalColor), |
| 49 | SkColorGetB(canonicalColor)); |
| 50 | // reduce to our finite number of bits |
| 51 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 52 | } |
| 53 | return canonicalColor; |
| 54 | } |
| 55 | |
| 56 | // TODO if this function ever shows up in profiling, then we can compute this value when the |
| 57 | // textblob is being built and cache it. However, for the time being textblobs mostly only have 1 |
| 58 | // run so this is not a big deal to compute here. |
| 59 | bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 60 | SkTextBlobRunIterator it(blob); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 61 | for (; !it.done(); it.next()) { |
| 62 | if (it.isLCD()) { |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 69 | void GrAtlasTextContext::drawTextBlob(GrContext* context, GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 70 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 71 | const SkMatrix& viewMatrix, |
| 72 | const SkSurfaceProps& props, const SkTextBlob* blob, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 73 | SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 74 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 75 | // If we have been abandoned, then don't draw |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 76 | if (context->abandoned()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 80 | SkAutoTUnref<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 81 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 82 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 83 | // It might be worth caching these things, but its not clear at this time |
| 84 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 85 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 86 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 87 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 88 | drawFilter); |
| 89 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 90 | GrTextBlobCache* cache = context->getTextBlobCache(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 91 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 92 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 93 | |
| 94 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 95 | SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 96 | kUnknown_SkPixelGeometry; |
| 97 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 98 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 99 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 100 | // ensure we always match the same key |
| 101 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 102 | ComputeCanonicalColor(skPaint, hasLCD); |
| 103 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 104 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 105 | key.fUniqueID = blob->uniqueID(); |
| 106 | key.fStyle = skPaint.getStyle(); |
| 107 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 108 | key.fCanonicalColor = canonicalColor; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 109 | cacheBlob.reset(SkSafeRef(cache->find(key))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 110 | } |
| 111 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 112 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 113 | // info. |
| 114 | GrPaint grPaint; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 115 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 118 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 119 | if (cacheBlob) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 120 | if (cacheBlob->mustRegenerate(skPaint, grPaint.getColor(), blurRec, viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 121 | // We have to remake the blob because changes may invalidate our masks. |
| 122 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 123 | // but we'd have to clear the subrun information |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 124 | cache->remove(cacheBlob); |
| 125 | cacheBlob.reset(SkRef(cache->createCachedBlob(blob, key, blurRec, skPaint))); |
| 126 | RegenerateTextBlob(cacheBlob, context->getBatchFontCache(), |
| 127 | *context->caps()->shaderCaps(), skPaint, grPaint.getColor(), |
| 128 | viewMatrix, props, |
| 129 | blob, x, y, drawFilter); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 130 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 131 | cache->makeMRU(cacheBlob); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 132 | |
| 133 | if (CACHE_SANITY_CHECK) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 134 | int glyphCount = 0; |
| 135 | int runCount = 0; |
| 136 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 137 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(cache->createBlob(glyphCount, runCount)); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 138 | sanityBlob->setupKey(key, blurRec, skPaint); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 139 | RegenerateTextBlob(sanityBlob, context->getBatchFontCache(), |
| 140 | *context->caps()->shaderCaps(), skPaint, |
| 141 | grPaint.getColor(), viewMatrix, props, |
| 142 | blob, x, y, drawFilter); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 143 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 144 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 145 | } |
| 146 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 147 | if (canCache) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 148 | cacheBlob.reset(SkRef(cache->createCachedBlob(blob, key, blurRec, skPaint))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 149 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 150 | cacheBlob.reset(cache->createBlob(blob)); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 151 | } |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 152 | RegenerateTextBlob(cacheBlob, context->getBatchFontCache(), |
| 153 | *context->caps()->shaderCaps(), skPaint, grPaint.getColor(), |
| 154 | viewMatrix, props, |
| 155 | blob, x, y, drawFilter); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 156 | } |
| 157 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 158 | cacheBlob->flushCached(context, dc, blob, props, fDistanceAdjustTable, skPaint, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 159 | grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 160 | } |
| 161 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 162 | void GrAtlasTextContext::RegenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
| 163 | GrBatchFontCache* fontCache, |
| 164 | const GrShaderCaps& shaderCaps, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 165 | const SkPaint& skPaint, GrColor color, |
| 166 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 167 | const SkSurfaceProps& props, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 168 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | a6bf4c5 | 2016-01-19 06:59:29 -0800 | [diff] [blame] | 169 | SkDrawFilter* drawFilter) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 170 | cacheBlob->initReusableBlob(color, viewMatrix, x, y); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 171 | |
| 172 | // Regenerate textblob |
| 173 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 174 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 175 | for (int run = 0; !it.done(); it.next(), run++) { |
| 176 | int glyphCount = it.glyphCount(); |
| 177 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 178 | const SkPoint& offset = it.offset(); |
| 179 | // applyFontToPaint() always overwrites the exact same attributes, |
| 180 | // so it is safe to not re-seed the paint for this reason. |
| 181 | it.applyFontToPaint(&runPaint); |
| 182 | |
| 183 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 184 | // A false return from filter() means we should abort the current draw. |
| 185 | runPaint = skPaint; |
| 186 | continue; |
| 187 | } |
| 188 | |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 189 | runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 190 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 191 | cacheBlob->push_back_run(run); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 192 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 193 | if (GrTextUtils::CanDrawAsDistanceFields(runPaint, viewMatrix, props, shaderCaps)) { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 194 | switch (it.positioning()) { |
| 195 | case SkTextBlob::kDefault_Positioning: { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 196 | GrTextUtils::DrawDFText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 197 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 198 | (const char *)it.glyphs(), textLen, |
| 199 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | case SkTextBlob::kHorizontal_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 203 | SkPoint dfOffset = SkPoint::Make(x, y + offset.y()); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 204 | GrTextUtils::DrawDFPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 205 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 206 | (const char*)it.glyphs(), textLen, it.pos(), |
| 207 | 1, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | case SkTextBlob::kFull_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 211 | SkPoint dfOffset = SkPoint::Make(x, y); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 212 | GrTextUtils::DrawDFPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 213 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 214 | (const char*)it.glyphs(), textLen, it.pos(), |
| 215 | 2, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 219 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 220 | cacheBlob->setRunDrawAsPaths(run); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 221 | } else { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 222 | switch (it.positioning()) { |
| 223 | case SkTextBlob::kDefault_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 224 | GrTextUtils::DrawBmpText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 225 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 226 | (const char *)it.glyphs(), textLen, |
| 227 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 228 | break; |
| 229 | case SkTextBlob::kHorizontal_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 230 | GrTextUtils::DrawBmpPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 231 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 232 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
| 233 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 234 | break; |
| 235 | case SkTextBlob::kFull_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 236 | GrTextUtils::DrawBmpPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 237 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 238 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
| 239 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 240 | break; |
| 241 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (drawFilter) { |
| 245 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 246 | runPaint = skPaint; |
| 247 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 251 | inline GrAtlasTextBlob* |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 252 | GrAtlasTextContext::CreateDrawTextBlob(GrTextBlobCache* blobCache, |
| 253 | GrBatchFontCache* fontCache, |
| 254 | const GrShaderCaps& shaderCaps, |
| 255 | const GrPaint& paint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 256 | const SkPaint& skPaint, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 257 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 258 | const SkSurfaceProps& props, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 259 | const char text[], size_t byteLength, |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 260 | SkScalar x, SkScalar y) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 261 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 262 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 263 | GrAtlasTextBlob* blob = blobCache->createBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 264 | blob->initThrowawayBlob(viewMatrix, x, y); |
joshualitt | a6bf4c5 | 2016-01-19 06:59:29 -0800 | [diff] [blame] | 265 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 266 | if (GrTextUtils::CanDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps)) { |
| 267 | GrTextUtils::DrawDFText(blob, 0, fontCache, props, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 268 | skPaint, paint.getColor(), viewMatrix, text, |
| 269 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 270 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 271 | GrTextUtils::DrawBmpText(blob, 0, fontCache, props, skPaint, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 272 | paint.getColor(), viewMatrix, text, byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 273 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 274 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 275 | } |
| 276 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 277 | inline GrAtlasTextBlob* |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 278 | GrAtlasTextContext::CreateDrawPosTextBlob(GrTextBlobCache* blobCache, GrBatchFontCache* fontCache, |
| 279 | const GrShaderCaps& shaderCaps, const GrPaint& paint, |
| 280 | const SkPaint& skPaint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 281 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 282 | const char text[], size_t byteLength, |
| 283 | const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 284 | const SkPoint& offset) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 285 | int glyphCount = skPaint.countText(text, byteLength); |
| 286 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 287 | GrAtlasTextBlob* blob = blobCache->createBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 288 | blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 289 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 290 | if (GrTextUtils::CanDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps)) { |
| 291 | GrTextUtils::DrawDFPosText(blob, 0, fontCache, props, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 292 | skPaint, paint.getColor(), viewMatrix, text, |
| 293 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 294 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 295 | GrTextUtils::DrawBmpPosText(blob, 0, fontCache, props, skPaint, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 296 | paint.getColor(), viewMatrix, text, |
| 297 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 298 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 299 | return blob; |
| 300 | } |
| 301 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 302 | void GrAtlasTextContext::drawText(GrContext* context, |
| 303 | GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 304 | const GrClip& clip, |
| 305 | const GrPaint& paint, const SkPaint& skPaint, |
| 306 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 307 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 308 | const char text[], size_t byteLength, |
| 309 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 310 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 311 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 312 | } else if (this->canDraw(skPaint, viewMatrix, props, *context->caps()->shaderCaps())) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 313 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 314 | CreateDrawTextBlob(context->getTextBlobCache(), context->getBatchFontCache(), |
| 315 | *context->caps()->shaderCaps(), |
| 316 | paint, skPaint, |
| 317 | viewMatrix, props, |
| 318 | text, byteLength, x, y)); |
| 319 | blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 320 | clip, viewMatrix, regionClipBounds, x, y); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | |
| 324 | // fall back to drawing as a path |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 325 | GrTextUtils::DrawTextAsPath(context, dc, clip, skPaint, viewMatrix, text, byteLength, x, y, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 326 | regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 327 | } |
| 328 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 329 | void GrAtlasTextContext::drawPosText(GrContext* context, |
| 330 | GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 331 | const GrClip& clip, |
| 332 | const GrPaint& paint, const SkPaint& skPaint, |
| 333 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 334 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 335 | const char text[], size_t byteLength, |
| 336 | const SkScalar pos[], int scalarsPerPosition, |
| 337 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 338 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 339 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 340 | } else if (this->canDraw(skPaint, viewMatrix, props, *context->caps()->shaderCaps())) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 341 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 342 | CreateDrawPosTextBlob(context->getTextBlobCache(), |
| 343 | context->getBatchFontCache(), |
| 344 | *context->caps()->shaderCaps(), |
| 345 | paint, skPaint, viewMatrix, props, |
| 346 | text, byteLength, |
| 347 | pos, scalarsPerPosition, |
| 348 | offset)); |
| 349 | blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 350 | clip, viewMatrix, regionClipBounds, offset.fX, offset.fY); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 351 | return; |
| 352 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 353 | |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 354 | // fall back to drawing as a path |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 355 | GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, skPaint, viewMatrix, text, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 356 | byteLength, pos, scalarsPerPosition, offset, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 357 | } |
| 358 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 359 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 360 | |
| 361 | #ifdef GR_TEST_UTILS |
| 362 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 363 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 364 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 365 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 366 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 367 | |
| 368 | if (context->uniqueID() != gContextID) { |
| 369 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 370 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 371 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 372 | gTextContext = GrAtlasTextContext::Create(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 373 | } |
| 374 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 375 | // Setup dummy SkPaint / GrPaint |
| 376 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 377 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 378 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 379 | skPaint.setColor(color); |
| 380 | skPaint.setLCDRenderText(random->nextBool()); |
| 381 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 382 | skPaint.setSubpixelText(random->nextBool()); |
| 383 | |
| 384 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 385 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 386 | SkFAIL("couldn't convert paint\n"); |
| 387 | } |
| 388 | |
| 389 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 390 | int textLen = (int)strlen(text); |
| 391 | |
joshualitt | b8d8649 | 2016-02-24 09:23:03 -0800 | [diff] [blame] | 392 | // create some random x/y offsets, including negative offsets |
| 393 | static const int kMaxTrans = 1024; |
| 394 | int xPos = (random->nextU() % 2) * 2 - 1; |
| 395 | int yPos = (random->nextU() % 2) * 2 - 1; |
| 396 | int xInt = (random->nextU() % kMaxTrans) * xPos; |
| 397 | int yInt = (random->nextU() % kMaxTrans) * yPos; |
| 398 | SkScalar x = SkIntToScalar(xInt); |
| 399 | SkScalar y = SkIntToScalar(yInt); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 400 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 401 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 402 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 403 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 404 | GrAtlasTextContext::CreateDrawTextBlob(context->getTextBlobCache(), |
| 405 | context->getBatchFontCache(), |
| 406 | *context->caps()->shaderCaps(), grPaint, skPaint, |
| 407 | viewMatrix, |
| 408 | gSurfaceProps, text, |
joshualitt | b8d8649 | 2016-02-24 09:23:03 -0800 | [diff] [blame] | 409 | static_cast<size_t>(textLen), x, y)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 410 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 411 | return blob->test_createBatch(textLen, 0, 0, viewMatrix, x, y, color, skPaint, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 412 | gSurfaceProps, gTextContext->dfAdjustTable(), |
| 413 | context->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | #endif |