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