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 | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 9 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 10 | #include "GrDrawTarget.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 | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 112 | SkScalar transX = 0.f; |
| 113 | SkScalar transY = 0.f; |
| 114 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 115 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 116 | // info. |
| 117 | GrPaint grPaint; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 118 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 121 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 122 | if (cacheBlob) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 123 | if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec, |
| 124 | viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 125 | // We have to remake the blob because changes may invalidate our masks. |
| 126 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 127 | // but we'd have to clear the subrun information |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 128 | cache->remove(cacheBlob); |
| 129 | cacheBlob.reset(SkRef(cache->createCachedBlob(blob, key, blurRec, skPaint))); |
| 130 | RegenerateTextBlob(cacheBlob, context->getBatchFontCache(), |
| 131 | *context->caps()->shaderCaps(), skPaint, grPaint.getColor(), |
| 132 | viewMatrix, props, |
| 133 | blob, x, y, drawFilter); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 134 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 135 | cache->makeMRU(cacheBlob); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 136 | |
| 137 | if (CACHE_SANITY_CHECK) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 138 | int glyphCount = 0; |
| 139 | int runCount = 0; |
| 140 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 141 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(cache->createBlob(glyphCount, runCount)); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 142 | sanityBlob->setupKey(key, blurRec, skPaint); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 143 | RegenerateTextBlob(sanityBlob, context->getBatchFontCache(), |
| 144 | *context->caps()->shaderCaps(), skPaint, |
| 145 | grPaint.getColor(), viewMatrix, props, |
| 146 | blob, x, y, drawFilter); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 147 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 148 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 149 | } |
| 150 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 151 | if (canCache) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 152 | cacheBlob.reset(SkRef(cache->createCachedBlob(blob, key, blurRec, skPaint))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 153 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 154 | cacheBlob.reset(cache->createBlob(blob)); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 155 | } |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 156 | RegenerateTextBlob(cacheBlob, context->getBatchFontCache(), |
| 157 | *context->caps()->shaderCaps(), skPaint, grPaint.getColor(), |
| 158 | viewMatrix, props, |
| 159 | blob, x, y, drawFilter); |
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 | cacheBlob->flushCached(context, dc, blob, props, fDistanceAdjustTable, skPaint, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 163 | grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 164 | } |
| 165 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 166 | void GrAtlasTextContext::RegenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
| 167 | GrBatchFontCache* fontCache, |
| 168 | const GrShaderCaps& shaderCaps, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 169 | const SkPaint& skPaint, GrColor color, |
| 170 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 171 | const SkSurfaceProps& props, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 172 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | a6bf4c5 | 2016-01-19 06:59:29 -0800 | [diff] [blame] | 173 | SkDrawFilter* drawFilter) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 174 | cacheBlob->initReusableBlob(color, viewMatrix, x, y); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 175 | |
| 176 | // Regenerate textblob |
| 177 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 178 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 179 | for (int run = 0; !it.done(); it.next(), run++) { |
| 180 | int glyphCount = it.glyphCount(); |
| 181 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 182 | const SkPoint& offset = it.offset(); |
| 183 | // applyFontToPaint() always overwrites the exact same attributes, |
| 184 | // so it is safe to not re-seed the paint for this reason. |
| 185 | it.applyFontToPaint(&runPaint); |
| 186 | |
| 187 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 188 | // A false return from filter() means we should abort the current draw. |
| 189 | runPaint = skPaint; |
| 190 | continue; |
| 191 | } |
| 192 | |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 193 | runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 194 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 195 | cacheBlob->push_back_run(run); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 196 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 197 | if (GrTextUtils::CanDrawAsDistanceFields(runPaint, viewMatrix, props, shaderCaps)) { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 198 | switch (it.positioning()) { |
| 199 | case SkTextBlob::kDefault_Positioning: { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 200 | GrTextUtils::DrawDFText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 201 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 202 | (const char *)it.glyphs(), textLen, |
| 203 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | case SkTextBlob::kHorizontal_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 207 | SkPoint dfOffset = SkPoint::Make(x, y + offset.y()); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 208 | GrTextUtils::DrawDFPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 209 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 210 | (const char*)it.glyphs(), textLen, it.pos(), |
| 211 | 1, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | case SkTextBlob::kFull_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 215 | SkPoint dfOffset = SkPoint::Make(x, y); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 216 | GrTextUtils::DrawDFPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 217 | props, runPaint, color, viewMatrix, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 218 | (const char*)it.glyphs(), textLen, it.pos(), |
| 219 | 2, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 223 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 224 | cacheBlob->setRunDrawAsPaths(run); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 225 | } else { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 226 | switch (it.positioning()) { |
| 227 | case SkTextBlob::kDefault_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 228 | GrTextUtils::DrawBmpText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 229 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 230 | (const char *)it.glyphs(), textLen, |
| 231 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 232 | break; |
| 233 | case SkTextBlob::kHorizontal_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 234 | GrTextUtils::DrawBmpPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 235 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 236 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
| 237 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 238 | break; |
| 239 | case SkTextBlob::kFull_Positioning: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 240 | GrTextUtils::DrawBmpPosText(cacheBlob, run, fontCache, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 241 | props, runPaint, color, viewMatrix, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 242 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
| 243 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 244 | break; |
| 245 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (drawFilter) { |
| 249 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 250 | runPaint = skPaint; |
| 251 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 255 | inline GrAtlasTextBlob* |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 256 | GrAtlasTextContext::CreateDrawTextBlob(GrTextBlobCache* blobCache, |
| 257 | GrBatchFontCache* fontCache, |
| 258 | const GrShaderCaps& shaderCaps, |
| 259 | const GrPaint& paint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 260 | const SkPaint& skPaint, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 261 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 262 | const SkSurfaceProps& props, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 263 | const char text[], size_t byteLength, |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 264 | SkScalar x, SkScalar y) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 265 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 266 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 267 | GrAtlasTextBlob* blob = blobCache->createBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 268 | blob->initThrowawayBlob(viewMatrix, x, y); |
joshualitt | a6bf4c5 | 2016-01-19 06:59:29 -0800 | [diff] [blame] | 269 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 270 | if (GrTextUtils::CanDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps)) { |
| 271 | GrTextUtils::DrawDFText(blob, 0, fontCache, props, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 272 | skPaint, paint.getColor(), viewMatrix, text, |
| 273 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 274 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 275 | GrTextUtils::DrawBmpText(blob, 0, fontCache, props, skPaint, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 276 | paint.getColor(), viewMatrix, text, byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 277 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 278 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 279 | } |
| 280 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 281 | inline GrAtlasTextBlob* |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 282 | GrAtlasTextContext::CreateDrawPosTextBlob(GrTextBlobCache* blobCache, GrBatchFontCache* fontCache, |
| 283 | const GrShaderCaps& shaderCaps, const GrPaint& paint, |
| 284 | const SkPaint& skPaint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 285 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 286 | const char text[], size_t byteLength, |
| 287 | const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 288 | const SkPoint& offset) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 289 | int glyphCount = skPaint.countText(text, byteLength); |
| 290 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 291 | GrAtlasTextBlob* blob = blobCache->createBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 292 | blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 293 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 294 | if (GrTextUtils::CanDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps)) { |
| 295 | GrTextUtils::DrawDFPosText(blob, 0, fontCache, props, |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 296 | skPaint, paint.getColor(), viewMatrix, text, |
| 297 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 298 | } else { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 299 | GrTextUtils::DrawBmpPosText(blob, 0, fontCache, props, skPaint, |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 300 | paint.getColor(), viewMatrix, text, |
| 301 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 302 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 303 | return blob; |
| 304 | } |
| 305 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 306 | void GrAtlasTextContext::drawText(GrContext* context, |
| 307 | GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 308 | const GrClip& clip, |
| 309 | const GrPaint& paint, const SkPaint& skPaint, |
| 310 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 311 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 312 | const char text[], size_t byteLength, |
| 313 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 314 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 315 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 316 | } else if (this->canDraw(skPaint, viewMatrix, props, *context->caps()->shaderCaps())) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 317 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 318 | CreateDrawTextBlob(context->getTextBlobCache(), context->getBatchFontCache(), |
| 319 | *context->caps()->shaderCaps(), |
| 320 | paint, skPaint, |
| 321 | viewMatrix, props, |
| 322 | text, byteLength, x, y)); |
| 323 | blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 324 | clip, regionClipBounds); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | // fall back to drawing as a path |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 329 | GrTextUtils::DrawTextAsPath(context, dc, clip, skPaint, viewMatrix, text, byteLength, x, y, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 330 | regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 331 | } |
| 332 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 333 | void GrAtlasTextContext::drawPosText(GrContext* context, |
| 334 | GrDrawContext* dc, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 335 | const GrClip& clip, |
| 336 | const GrPaint& paint, const SkPaint& skPaint, |
| 337 | const SkMatrix& viewMatrix, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 338 | const SkSurfaceProps& props, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 339 | const char text[], size_t byteLength, |
| 340 | const SkScalar pos[], int scalarsPerPosition, |
| 341 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 342 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 343 | return; |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 344 | } else if (this->canDraw(skPaint, viewMatrix, props, *context->caps()->shaderCaps())) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 345 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 346 | CreateDrawPosTextBlob(context->getTextBlobCache(), |
| 347 | context->getBatchFontCache(), |
| 348 | *context->caps()->shaderCaps(), |
| 349 | paint, skPaint, viewMatrix, props, |
| 350 | text, byteLength, |
| 351 | pos, scalarsPerPosition, |
| 352 | offset)); |
| 353 | blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 354 | clip, regionClipBounds); |
| 355 | return; |
| 356 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 357 | |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 358 | // fall back to drawing as a path |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 359 | GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, skPaint, viewMatrix, text, |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 360 | byteLength, pos, scalarsPerPosition, offset, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 361 | } |
| 362 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 363 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 364 | |
| 365 | #ifdef GR_TEST_UTILS |
| 366 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 367 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 368 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 369 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 370 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 371 | |
| 372 | if (context->uniqueID() != gContextID) { |
| 373 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 374 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 375 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 376 | gTextContext = GrAtlasTextContext::Create(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 377 | } |
| 378 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 379 | // Setup dummy SkPaint / GrPaint |
| 380 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 381 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 382 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 383 | skPaint.setColor(color); |
| 384 | skPaint.setLCDRenderText(random->nextBool()); |
| 385 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 386 | skPaint.setSubpixelText(random->nextBool()); |
| 387 | |
| 388 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 389 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 390 | SkFAIL("couldn't convert paint\n"); |
| 391 | } |
| 392 | |
| 393 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 394 | int textLen = (int)strlen(text); |
| 395 | |
| 396 | // Setup clip |
| 397 | GrClip clip; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 398 | |
| 399 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 400 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 401 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 402 | GrAtlasTextContext::CreateDrawTextBlob(context->getTextBlobCache(), |
| 403 | context->getBatchFontCache(), |
| 404 | *context->caps()->shaderCaps(), grPaint, skPaint, |
| 405 | viewMatrix, |
| 406 | gSurfaceProps, text, |
| 407 | static_cast<size_t>(textLen), 0, 0)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 408 | |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 409 | // We'd like to be able to test this with random translations, but currently the vertex |
| 410 | // bounds and vertices will get out of sync |
| 411 | SkScalar transX = 0.f;//SkIntToScalar(random->nextU()); |
| 412 | SkScalar transY = 0.f;//SkIntToScalar(random->nextU()); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 413 | return blob->test_createBatch(textLen, 0, 0, color, transX, transY, skPaint, |
| 414 | gSurfaceProps, gTextContext->dfAdjustTable(), |
| 415 | context->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | #endif |