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