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 | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 9 | #include "GrBlurUtils.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "GrFontScaler.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 13 | #include "GrStrokeInfo.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | #include "GrTextBlobCache.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 15 | #include "GrTexturePriv.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 | |
| 37 | namespace { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 38 | static const int kMinDFFontSize = 18; |
| 39 | static const int kSmallDFFontSize = 32; |
| 40 | static const int kSmallDFFontLimit = 32; |
| 41 | static const int kMediumDFFontSize = 72; |
| 42 | static const int kMediumDFFontLimit = 72; |
| 43 | static const int kLargeDFFontSize = 162; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 44 | #ifdef SK_BUILD_FOR_ANDROID |
| 45 | static const int kLargeDFFontLimit = 384; |
| 46 | #else |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 47 | static const int kLargeDFFontLimit = 2 * kLargeDFFontSize; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 48 | #endif |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 49 | |
| 50 | SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;) |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 53 | GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps) |
| 54 | : INHERITED(context, surfaceProps) |
| 55 | , fDistanceAdjustTable(new DistanceAdjustTable) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 56 | // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest |
| 57 | // vertexStride |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 58 | static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize && |
| 59 | GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize, |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 60 | "vertex_attribute_changed"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | fCurrStrike = nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 62 | fCache = context->getTextBlobCache(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 63 | } |
| 64 | |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 65 | void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 66 | |
| 67 | // This is used for an approximation of the mask gamma hack, used by raster and bitmap |
| 68 | // text. The mask gamma hack is based off of guessing what the blend color is going to |
| 69 | // be, and adjusting the mask so that when run through the linear blend will |
| 70 | // produce the value closest to the desired result. However, in practice this means |
| 71 | // that the 'adjusted' mask is just increasing or decreasing the coverage of |
| 72 | // the mask depending on what it is thought it will blit against. For black (on |
| 73 | // assumed white) this means that coverages are decreased (on a curve). For white (on |
| 74 | // assumed black) this means that coverages are increased (on a a curve). At |
| 75 | // middle (perceptual) gray (which could be blit against anything) the coverages |
| 76 | // remain the same. |
| 77 | // |
| 78 | // The idea here is that instead of determining the initial (real) coverage and |
| 79 | // then adjusting that coverage, we determine an adjusted coverage directly by |
| 80 | // essentially manipulating the geometry (in this case, the distance to the glyph |
| 81 | // edge). So for black (on assumed white) this thins a bit; for white (on |
| 82 | // assumed black) this fake bolds the geometry a bit. |
| 83 | // |
| 84 | // The distance adjustment is calculated by determining the actual coverage value which |
| 85 | // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This |
| 86 | // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the |
| 87 | // actual edge. So by subtracting this distance adjustment and computing without the |
| 88 | // the coverage adjustment we should get 0.5 coverage at the same point. |
| 89 | // |
| 90 | // This has several implications: |
| 91 | // For non-gray lcd smoothed text, each subpixel essentially is using a |
| 92 | // slightly different geometry. |
| 93 | // |
| 94 | // For black (on assumed white) this may not cover some pixels which were |
| 95 | // previously covered; however those pixels would have been only slightly |
| 96 | // covered and that slight coverage would have been decreased anyway. Also, some pixels |
| 97 | // which were previously fully covered may no longer be fully covered. |
| 98 | // |
| 99 | // For white (on assumed black) this may cover some pixels which weren't |
| 100 | // previously covered at all. |
| 101 | |
| 102 | int width, height; |
| 103 | size_t size; |
| 104 | |
| 105 | #ifdef SK_GAMMA_CONTRAST |
| 106 | SkScalar contrast = SK_GAMMA_CONTRAST; |
| 107 | #else |
| 108 | SkScalar contrast = 0.5f; |
| 109 | #endif |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 110 | SkScalar paintGamma = SK_GAMMA_EXPONENT; |
| 111 | SkScalar deviceGamma = SK_GAMMA_EXPONENT; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 112 | |
| 113 | size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma, |
| 114 | &width, &height); |
| 115 | |
| 116 | SkASSERT(kExpectedDistanceAdjustTableSize == height); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 117 | fTable = new SkScalar[height]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 118 | |
| 119 | SkAutoTArray<uint8_t> data((int)size); |
| 120 | SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get()); |
| 121 | |
| 122 | // find the inverse points where we cross 0.5 |
| 123 | // binsearch might be better, but we only need to do this once on creation |
| 124 | for (int row = 0; row < height; ++row) { |
| 125 | uint8_t* rowPtr = data.get() + row*width; |
| 126 | for (int col = 0; col < width - 1; ++col) { |
| 127 | if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) { |
| 128 | // compute point where a mask value will give us a result of 0.5 |
| 129 | float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]); |
| 130 | float borderAlpha = (col + interp) / 255.f; |
| 131 | |
| 132 | // compute t value for that alpha |
| 133 | // this is an approximate inverse for smoothstep() |
| 134 | float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f; |
| 135 | |
| 136 | // compute distance which gives us that t value |
| 137 | const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor |
| 138 | float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor; |
| 139 | |
| 140 | fTable[row] = d; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 147 | GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 148 | const SkSurfaceProps& surfaceProps) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 149 | return new GrAtlasTextContext(context, surfaceProps); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 152 | bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 153 | return this->canDrawAsDistanceFields(skPaint, viewMatrix) || |
| 154 | !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 155 | } |
| 156 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 157 | GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
| 158 | GrColor canonicalColor = paint.computeLuminanceColor(); |
| 159 | if (lcd) { |
| 160 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 161 | // For now we just regenerate if any run in a textblob has LCD. |
| 162 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 163 | // at a higher level *OR* use sRGB |
| 164 | SkASSERT(false); |
| 165 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 166 | } else { |
| 167 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 168 | // gamma corrected masks anyways, nor color |
| 169 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 170 | SkColorGetG(canonicalColor), |
| 171 | SkColorGetB(canonicalColor)); |
| 172 | // reduce to our finite number of bits |
| 173 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 174 | } |
| 175 | return canonicalColor; |
| 176 | } |
| 177 | |
| 178 | // TODO if this function ever shows up in profiling, then we can compute this value when the |
| 179 | // textblob is being built and cache it. However, for the time being textblobs mostly only have 1 |
| 180 | // run so this is not a big deal to compute here. |
| 181 | bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 182 | SkTextBlobRunIterator it(blob); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 183 | for (; !it.done(); it.next()) { |
| 184 | if (it.isLCD()) { |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 191 | inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 192 | const SkPaint& skPaint, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 193 | const SkMatrix* viewMatrix, |
| 194 | bool noGamma) { |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 195 | skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 196 | run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 197 | return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc()); |
| 198 | } |
| 199 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 200 | void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 201 | const GrClip& clip, const SkPaint& skPaint, |
| 202 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 203 | SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 204 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 205 | // If we have been abandoned, then don't draw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 206 | if (fContext->abandoned()) { |
| 207 | return; |
| 208 | } |
| 209 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 210 | SkAutoTUnref<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 211 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 212 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 213 | // It might be worth caching these things, but its not clear at this time |
| 214 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 215 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 216 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 217 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 218 | drawFilter); |
| 219 | |
| 220 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 221 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 222 | |
| 223 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 224 | SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 225 | kUnknown_SkPixelGeometry; |
| 226 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 227 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 228 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 229 | // ensure we always match the same key |
| 230 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 231 | ComputeCanonicalColor(skPaint, hasLCD); |
| 232 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 233 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 234 | key.fUniqueID = blob->uniqueID(); |
| 235 | key.fStyle = skPaint.getStyle(); |
| 236 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 237 | key.fCanonicalColor = canonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 238 | cacheBlob.reset(SkSafeRef(fCache->find(key))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 241 | SkScalar transX = 0.f; |
| 242 | SkScalar transY = 0.f; |
| 243 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 244 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 245 | // info. |
| 246 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 247 | if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 250 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 251 | if (cacheBlob) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 252 | if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec, |
| 253 | viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 254 | // We have to remake the blob because changes may invalidate our masks. |
| 255 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 256 | // but we'd have to clear the subrun information |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 257 | fCache->remove(cacheBlob); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 258 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 259 | GrAtlasTextBlob::kGrayTextVASize))); |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 260 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 261 | blob, x, y, drawFilter, clip); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 262 | } else { |
| 263 | fCache->makeMRU(cacheBlob); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 264 | #ifdef CACHE_SANITY_CHECK |
| 265 | { |
| 266 | int glyphCount = 0; |
| 267 | int runCount = 0; |
| 268 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
| 269 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount, |
| 270 | kGrayTextVASize)); |
| 271 | GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint); |
| 272 | this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 273 | blob, x, y, drawFilter, clip); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 274 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 275 | } |
| 276 | |
| 277 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 278 | } |
| 279 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 280 | if (canCache) { |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 281 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 282 | GrAtlasTextBlob::kGrayTextVASize))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 283 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 284 | cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize)); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 285 | } |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 286 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 287 | blob, x, y, drawFilter, clip); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 288 | } |
| 289 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 290 | this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 291 | clip, viewMatrix, clipBounds, x, y, transX, transY); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 292 | } |
| 293 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 294 | inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, |
| 295 | const SkMatrix& viewMatrix) { |
| 296 | // TODO: support perspective (need getMaxScale replacement) |
| 297 | if (viewMatrix.hasPerspective()) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 302 | SkScalar scaledTextSize = maxScale*skPaint.getTextSize(); |
| 303 | // Hinted text looks far better at small resolutions |
| 304 | // Scaling up beyond 2x yields undesireable artifacts |
jvanverth | 34d7288 | 2015-06-22 08:08:09 -0700 | [diff] [blame] | 305 | if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 309 | bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts(); |
robertphillips | bcd7ab5 | 2015-06-18 05:27:18 -0700 | [diff] [blame] | 310 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 311 | useDFT = true; |
| 312 | #endif |
| 313 | |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 314 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | |
| 318 | // rasterizers and mask filters modify alpha, which doesn't |
| 319 | // translate well to distance |
| 320 | if (skPaint.getRasterizer() || skPaint.getMaskFilter() || |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 321 | !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
| 325 | // TODO: add some stroking support |
| 326 | if (skPaint.getStyle() != SkPaint::kFill_Style) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 333 | void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 334 | const SkPaint& skPaint, GrColor color, |
| 335 | const SkMatrix& viewMatrix, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 336 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 337 | SkDrawFilter* drawFilter, |
| 338 | const GrClip& clip) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 339 | // 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] | 340 | // have to regenerate LCD text blobs. |
| 341 | // We use this color vs the SkPaint color because it has the colorfilter applied. |
| 342 | cacheBlob->fPaintColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 343 | cacheBlob->fViewMatrix = viewMatrix; |
| 344 | cacheBlob->fX = x; |
| 345 | cacheBlob->fY = y; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 346 | |
| 347 | // Regenerate textblob |
| 348 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 349 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 350 | for (int run = 0; !it.done(); it.next(), run++) { |
| 351 | int glyphCount = it.glyphCount(); |
| 352 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 353 | const SkPoint& offset = it.offset(); |
| 354 | // applyFontToPaint() always overwrites the exact same attributes, |
| 355 | // so it is safe to not re-seed the paint for this reason. |
| 356 | it.applyFontToPaint(&runPaint); |
| 357 | |
| 358 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 359 | // A false return from filter() means we should abort the current draw. |
| 360 | runPaint = skPaint; |
| 361 | continue; |
| 362 | } |
| 363 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 364 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 365 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 366 | cacheBlob->push_back_run(run); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 367 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 368 | if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) { |
| 369 | cacheBlob->setHasDistanceField(); |
| 370 | SkPaint dfPaint = runPaint; |
| 371 | SkScalar textRatio; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 372 | this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 373 | Run& runIdx = cacheBlob->fRuns[run]; |
| 374 | PerSubRunInfo& subRun = runIdx.fSubRunInfo.back(); |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 375 | subRun.setUseLCDText(runPaint.isLCDRenderText()); |
| 376 | subRun.setDrawAsDistanceFields(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 377 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 378 | SkTDArray<char> fallbackTxt; |
| 379 | SkTDArray<SkScalar> fallbackPos; |
| 380 | SkPoint dfOffset; |
| 381 | int scalarsPerPosition = 2; |
| 382 | switch (it.positioning()) { |
| 383 | case SkTextBlob::kDefault_Positioning: { |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 384 | this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 385 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 386 | x + offset.x(), y + offset.y(), textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 387 | &fallbackTxt, &fallbackPos, &dfOffset, runPaint); |
| 388 | break; |
| 389 | } |
| 390 | case SkTextBlob::kHorizontal_Positioning: { |
| 391 | scalarsPerPosition = 1; |
| 392 | dfOffset = SkPoint::Make(x, y + offset.y()); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 393 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 394 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 395 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 396 | &fallbackTxt, &fallbackPos); |
| 397 | break; |
| 398 | } |
| 399 | case SkTextBlob::kFull_Positioning: { |
| 400 | dfOffset = SkPoint::Make(x, y); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 401 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 402 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 403 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 404 | &fallbackTxt, &fallbackPos); |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 409 | this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix, |
| 410 | fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 411 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 412 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
| 413 | cacheBlob->fRuns[run].fDrawAsPaths = true; |
| 414 | } else { |
| 415 | cacheBlob->setHasBitmap(); |
| 416 | SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix, |
| 417 | false); |
| 418 | switch (it.positioning()) { |
| 419 | case SkTextBlob::kDefault_Positioning: |
| 420 | this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 421 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 422 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 423 | break; |
| 424 | case SkTextBlob::kHorizontal_Positioning: |
| 425 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 426 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 427 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 428 | break; |
| 429 | case SkTextBlob::kFull_Positioning: |
| 430 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 431 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 432 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | SkGlyphCache::AttachCache(cache); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (drawFilter) { |
| 439 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 440 | runPaint = skPaint; |
| 441 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 445 | inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 446 | SkPaint* skPaint, |
| 447 | SkScalar* textRatio, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 448 | const SkMatrix& viewMatrix) { |
| 449 | // getMaxScale doesn't support perspective, so neither do we at the moment |
| 450 | SkASSERT(!viewMatrix.hasPerspective()); |
| 451 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 452 | SkScalar textSize = skPaint->getTextSize(); |
| 453 | SkScalar scaledTextSize = textSize; |
| 454 | // if we have non-unity scale, we need to choose our base text size |
| 455 | // based on the SkPaint's text size multiplied by the max scale factor |
| 456 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 457 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 458 | scaledTextSize *= maxScale; |
| 459 | } |
| 460 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 461 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 462 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 463 | SkScalar dfMaskScaleFloor; |
| 464 | SkScalar dfMaskScaleCeil; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 465 | if (scaledTextSize <= kSmallDFFontLimit) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 466 | dfMaskScaleFloor = kMinDFFontSize; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 467 | dfMaskScaleCeil = kSmallDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 468 | *textRatio = textSize / kSmallDFFontSize; |
| 469 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 470 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 471 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 472 | dfMaskScaleCeil = kMediumDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 473 | *textRatio = textSize / kMediumDFFontSize; |
| 474 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 475 | } else { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 476 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 477 | dfMaskScaleCeil = kLargeDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 478 | *textRatio = textSize / kLargeDFFontSize; |
| 479 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 480 | } |
| 481 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 482 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 483 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 484 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 485 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 486 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 487 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 488 | // level) |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 489 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 490 | blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale); |
| 491 | blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale); |
| 492 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 493 | skPaint->setLCDRenderText(false); |
| 494 | skPaint->setAutohinted(false); |
| 495 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 496 | skPaint->setSubpixelText(true); |
| 497 | } |
| 498 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 499 | inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 500 | int runIndex, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 501 | const GrClip& clip, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 502 | GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 503 | const SkPaint& skPaint, |
| 504 | const SkMatrix& viewMatrix, |
| 505 | const SkTDArray<char>& fallbackTxt, |
| 506 | const SkTDArray<SkScalar>& fallbackPos, |
| 507 | int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 508 | const SkPoint& offset) { |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 509 | SkASSERT(fallbackTxt.count()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 510 | blob->setHasBitmap(); |
| 511 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 512 | // Push back a new subrun to fill and set the override descriptor |
| 513 | run.push_back(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 514 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 515 | skPaint.getScalerContextDescriptor(run.fOverrideDescriptor, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 516 | fSurfaceProps, &viewMatrix, false); |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 517 | SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface, |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 518 | run.fOverrideDescriptor->getDesc()); |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 519 | this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 520 | fallbackTxt.begin(), fallbackTxt.count(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 521 | fallbackPos.begin(), scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 522 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 523 | } |
| 524 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 525 | inline GrAtlasTextBlob* |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 526 | GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 527 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 528 | SkScalar* textRatio) { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 529 | GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 530 | |
| 531 | *dfPaint = origPaint; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 532 | this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 533 | blob->fViewMatrix = viewMatrix; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 534 | Run& run = blob->fRuns[0]; |
| 535 | PerSubRunInfo& subRun = run.fSubRunInfo.back(); |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 536 | subRun.setUseLCDText(origPaint.isLCDRenderText()); |
| 537 | subRun.setDrawAsDistanceFields(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 538 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 539 | return blob; |
| 540 | } |
| 541 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 542 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 543 | GrAtlasTextContext::createDrawTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 544 | const GrPaint& paint, const SkPaint& skPaint, |
| 545 | const SkMatrix& viewMatrix, |
| 546 | const char text[], size_t byteLength, |
| 547 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 548 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 549 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 550 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 551 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 552 | SkPaint dfPaint; |
| 553 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 554 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 555 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 556 | SkTDArray<char> fallbackTxt; |
| 557 | SkTDArray<SkScalar> fallbackPos; |
| 558 | SkPoint offset; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 559 | this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 560 | byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 561 | &offset, skPaint); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 562 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 563 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 564 | fallbackTxt, fallbackPos, 2, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 565 | } |
| 566 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 567 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 568 | blob->fViewMatrix = viewMatrix; |
| 569 | |
| 570 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 571 | this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 572 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 573 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 574 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 575 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 576 | } |
| 577 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 578 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 579 | GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 580 | const GrPaint& paint, const SkPaint& skPaint, |
| 581 | const SkMatrix& viewMatrix, |
| 582 | const char text[], size_t byteLength, |
| 583 | const SkScalar pos[], int scalarsPerPosition, |
| 584 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 585 | int glyphCount = skPaint.countText(text, byteLength); |
| 586 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 587 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 588 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 589 | SkPaint dfPaint; |
| 590 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 591 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 592 | |
| 593 | SkTDArray<char> fallbackTxt; |
| 594 | SkTDArray<SkScalar> fallbackPos; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 595 | this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 596 | byteLength, pos, scalarsPerPosition, offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 597 | textRatio, &fallbackTxt, &fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 598 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 599 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 600 | fallbackTxt, fallbackPos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 601 | } |
| 602 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 603 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 604 | blob->fViewMatrix = viewMatrix; |
| 605 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 606 | this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 607 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 608 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 609 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 610 | return blob; |
| 611 | } |
| 612 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 613 | void GrAtlasTextContext::onDrawText(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 614 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 615 | const GrPaint& paint, const SkPaint& skPaint, |
| 616 | const SkMatrix& viewMatrix, |
| 617 | const char text[], size_t byteLength, |
| 618 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 619 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 620 | this->createDrawTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 621 | text, byteLength, x, y, regionClipBounds)); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 622 | this->flush(blob, dc, skPaint, paint, clip, regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 623 | } |
| 624 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 625 | void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 626 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 627 | const GrPaint& paint, const SkPaint& skPaint, |
| 628 | const SkMatrix& viewMatrix, |
| 629 | const char text[], size_t byteLength, |
| 630 | const SkScalar pos[], int scalarsPerPosition, |
| 631 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 632 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 633 | this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 634 | text, byteLength, |
| 635 | pos, scalarsPerPosition, |
| 636 | offset, regionClipBounds)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 637 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 638 | this->flush(blob, dc, skPaint, paint, clip, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 639 | } |
| 640 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 641 | void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 642 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 643 | GrColor color, |
| 644 | const SkMatrix& viewMatrix, |
| 645 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 646 | SkScalar x, SkScalar y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 647 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 648 | |
| 649 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 650 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 654 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 655 | |
| 656 | // Get GrFontScaler from cache |
| 657 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 658 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 659 | SkFindAndPlaceGlyph::ProcessText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 660 | skPaint.getTextEncoding(), text, byteLength, |
| 661 | {x, y}, viewMatrix, skPaint.getTextAlign(), |
| 662 | cache, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 663 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 664 | position += rounding; |
| 665 | this->bmpAppendGlyph( |
| 666 | blob, runIndex, glyph, |
| 667 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 668 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 669 | } |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 670 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 671 | } |
| 672 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 673 | void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 674 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 675 | GrColor color, |
| 676 | const SkMatrix& viewMatrix, |
| 677 | const char text[], size_t byteLength, |
| 678 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 679 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 680 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 681 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 682 | |
| 683 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 684 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 688 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 689 | |
| 690 | // Get GrFontScaler from cache |
| 691 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 692 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 693 | SkFindAndPlaceGlyph::ProcessPosText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 694 | skPaint.getTextEncoding(), text, byteLength, |
| 695 | offset, viewMatrix, pos, scalarsPerPosition, |
| 696 | skPaint.getTextAlign(), cache, |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 697 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 698 | position += rounding; |
| 699 | this->bmpAppendGlyph( |
| 700 | blob, runIndex, glyph, |
| 701 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 702 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 703 | } |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 704 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 705 | } |
| 706 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 707 | void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 708 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 709 | const SkMatrix& viewMatrix, |
| 710 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 711 | SkScalar x, SkScalar y, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 712 | SkScalar textRatio, |
| 713 | SkTDArray<char>* fallbackTxt, |
| 714 | SkTDArray<SkScalar>* fallbackPos, |
| 715 | SkPoint* offset, |
| 716 | const SkPaint& origPaint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 717 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 718 | |
| 719 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 720 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc(); |
| 725 | SkAutoDescriptor desc; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 726 | origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true); |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 727 | SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(), |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 728 | desc.getDesc()); |
| 729 | |
| 730 | SkTArray<SkScalar> positions; |
| 731 | |
| 732 | const char* textPtr = text; |
| 733 | SkFixed stopX = 0; |
| 734 | SkFixed stopY = 0; |
| 735 | SkFixed origin = 0; |
| 736 | switch (origPaint.getTextAlign()) { |
| 737 | case SkPaint::kRight_Align: origin = SK_Fixed1; break; |
| 738 | case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; |
| 739 | case SkPaint::kLeft_Align: origin = 0; break; |
| 740 | } |
| 741 | |
| 742 | SkAutoKern autokern; |
| 743 | const char* stop = text + byteLength; |
| 744 | while (textPtr < stop) { |
| 745 | // don't need x, y here, since all subpixel variants will have the |
| 746 | // same advance |
| 747 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0); |
| 748 | |
| 749 | SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); |
| 750 | positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width))); |
| 751 | |
| 752 | SkFixed height = glyph.fAdvanceY; |
| 753 | positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height))); |
| 754 | |
| 755 | stopX += width; |
| 756 | stopY += height; |
| 757 | } |
| 758 | SkASSERT(textPtr == stop); |
| 759 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 760 | SkGlyphCache::AttachCache(origPaintCache); |
| 761 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 762 | // now adjust starting point depending on alignment |
| 763 | SkScalar alignX = SkFixedToScalar(stopX); |
| 764 | SkScalar alignY = SkFixedToScalar(stopY); |
| 765 | if (origPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 766 | alignX = SkScalarHalf(alignX); |
| 767 | alignY = SkScalarHalf(alignY); |
| 768 | } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 769 | alignX = 0; |
| 770 | alignY = 0; |
| 771 | } |
| 772 | x -= alignX; |
| 773 | y -= alignY; |
| 774 | *offset = SkPoint::Make(x, y); |
| 775 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 776 | this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 777 | positions.begin(), 2, *offset, textRatio, fallbackTxt, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 778 | fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 779 | } |
| 780 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 781 | void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 782 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 783 | const SkMatrix& viewMatrix, |
| 784 | const char text[], size_t byteLength, |
| 785 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 786 | const SkPoint& offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 787 | SkScalar textRatio, |
| 788 | SkTDArray<char>* fallbackTxt, |
| 789 | SkTDArray<SkScalar>* fallbackPos) { |
| 790 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 791 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 792 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 793 | |
| 794 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 795 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 796 | return; |
| 797 | } |
| 798 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 799 | fCurrStrike = nullptr; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 800 | |
| 801 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 802 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 803 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 804 | |
| 805 | const char* stop = text + byteLength; |
| 806 | |
| 807 | if (SkPaint::kLeft_Align == skPaint.getTextAlign()) { |
| 808 | while (text < stop) { |
| 809 | const char* lastText = text; |
| 810 | // the last 2 parameters are ignored |
| 811 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 812 | |
| 813 | if (glyph.fWidth) { |
| 814 | SkScalar x = offset.x() + pos[0]; |
| 815 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 816 | |
| 817 | if (!this->dfAppendGlyph(blob, |
| 818 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 819 | glyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 820 | x, y, color, fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 821 | textRatio, viewMatrix)) { |
| 822 | // couldn't append, send to fallback |
| 823 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 824 | *fallbackPos->append() = pos[0]; |
| 825 | if (2 == scalarsPerPosition) { |
| 826 | *fallbackPos->append() = pos[1]; |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | pos += scalarsPerPosition; |
| 831 | } |
| 832 | } else { |
| 833 | SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf |
| 834 | : SK_Scalar1; |
| 835 | while (text < stop) { |
| 836 | const char* lastText = text; |
| 837 | // the last 2 parameters are ignored |
| 838 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 839 | |
| 840 | if (glyph.fWidth) { |
| 841 | SkScalar x = offset.x() + pos[0]; |
| 842 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 843 | |
| 844 | SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 845 | SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 846 | |
| 847 | if (!this->dfAppendGlyph(blob, |
| 848 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 849 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 850 | x - advanceX, y - advanceY, color, |
| 851 | fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 852 | textRatio, |
| 853 | viewMatrix)) { |
| 854 | // couldn't append, send to fallback |
| 855 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 856 | *fallbackPos->append() = pos[0]; |
| 857 | if (2 == scalarsPerPosition) { |
| 858 | *fallbackPos->append() = pos[1]; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | pos += scalarsPerPosition; |
| 863 | } |
| 864 | } |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 865 | |
| 866 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 867 | } |
| 868 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 869 | void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 870 | const SkGlyph& skGlyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 871 | int vx, int vy, GrColor color, GrFontScaler* scaler) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 872 | if (!fCurrStrike) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 873 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 874 | } |
| 875 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 876 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 877 | skGlyph.getSubXFixed(), |
| 878 | skGlyph.getSubYFixed(), |
| 879 | GrGlyph::kCoverage_MaskStyle); |
| 880 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 881 | if (!glyph) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 882 | return; |
| 883 | } |
| 884 | |
| 885 | int x = vx + glyph->fBounds.fLeft; |
| 886 | int y = vy + glyph->fBounds.fTop; |
| 887 | |
| 888 | // keep them as ints until we've done the clip-test |
| 889 | int width = glyph->fBounds.width(); |
| 890 | int height = glyph->fBounds.height(); |
| 891 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 892 | SkRect r; |
| 893 | r.fLeft = SkIntToScalar(x); |
| 894 | r.fTop = SkIntToScalar(y); |
| 895 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 896 | r.fBottom = r.fTop + SkIntToScalar(height); |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 897 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 898 | blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph, |
| 899 | SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 900 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 901 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 902 | bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 903 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 904 | SkScalar sx, SkScalar sy, GrColor color, |
| 905 | GrFontScaler* scaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 906 | SkScalar textRatio, const SkMatrix& viewMatrix) { |
| 907 | if (!fCurrStrike) { |
| 908 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 909 | } |
| 910 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 911 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 912 | skGlyph.getSubXFixed(), |
| 913 | skGlyph.getSubYFixed(), |
| 914 | GrGlyph::kDistance_MaskStyle); |
| 915 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 916 | if (!glyph) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 917 | return true; |
| 918 | } |
| 919 | |
| 920 | // fallback to color glyph support |
| 921 | if (kA8_GrMaskFormat != glyph->fMaskFormat) { |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 926 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 927 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 928 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 929 | |
| 930 | SkScalar scale = textRatio; |
| 931 | dx *= scale; |
| 932 | dy *= scale; |
| 933 | width *= scale; |
| 934 | height *= scale; |
| 935 | sx += dx; |
| 936 | sy += dy; |
| 937 | SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height); |
| 938 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 939 | blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph, |
| 940 | sx - dx, sy - dy, scale, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 941 | return true; |
| 942 | } |
| 943 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 944 | void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 945 | const SkTextBlobRunIterator& it, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 946 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 947 | SkDrawFilter* drawFilter, const SkMatrix& viewMatrix, |
| 948 | const SkIRect& clipBounds, SkScalar x, SkScalar y) { |
| 949 | SkPaint runPaint = skPaint; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 950 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 951 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 952 | const SkPoint& offset = it.offset(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 953 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 954 | it.applyFontToPaint(&runPaint); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 955 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 956 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 957 | return; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 958 | } |
| 959 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 960 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 961 | |
| 962 | switch (it.positioning()) { |
| 963 | case SkTextBlob::kDefault_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 964 | this->drawTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 965 | (const char *)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 966 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 967 | break; |
| 968 | case SkTextBlob::kHorizontal_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 969 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 970 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 971 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 972 | clipBounds); |
| 973 | break; |
| 974 | case SkTextBlob::kFull_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 975 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 976 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 977 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 982 | inline GrDrawBatch* |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 983 | GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 984 | int glyphCount, int run, int subRun, |
| 985 | GrColor color, SkScalar transX, SkScalar transY, |
| 986 | const SkPaint& skPaint) { |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 987 | GrMaskFormat format = info.maskFormat(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 988 | GrColor subRunColor; |
| 989 | if (kARGB_GrMaskFormat == format) { |
| 990 | uint8_t paintAlpha = skPaint.getAlpha(); |
| 991 | subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha); |
| 992 | } else { |
| 993 | subRunColor = color; |
| 994 | } |
| 995 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 996 | GrAtlasTextBatch* batch; |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 997 | if (info.drawAsDistanceFields()) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 998 | SkColor filteredColor; |
| 999 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 1000 | if (colorFilter) { |
| 1001 | filteredColor = colorFilter->filterColor(skPaint.getColor()); |
| 1002 | } else { |
| 1003 | filteredColor = skPaint.getColor(); |
| 1004 | } |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1005 | bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1006 | batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(), |
| 1007 | fDistanceAdjustTable, filteredColor, |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 1008 | info.hasUseLCDText(), useBGR); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1009 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1010 | batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1011 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1012 | GrAtlasTextBatch::Geometry& geometry = batch->geometry(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1013 | geometry.fBlob = SkRef(cacheBlob); |
| 1014 | geometry.fRun = run; |
| 1015 | geometry.fSubRun = subRun; |
| 1016 | geometry.fColor = subRunColor; |
| 1017 | geometry.fTransX = transX; |
| 1018 | geometry.fTransY = transY; |
| 1019 | batch->init(); |
| 1020 | |
| 1021 | return batch; |
| 1022 | } |
| 1023 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1024 | inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1025 | GrAtlasTextBlob* cacheBlob, int run, GrColor color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1026 | SkScalar transX, SkScalar transY, |
| 1027 | const SkPaint& skPaint) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1028 | for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) { |
| 1029 | const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun]; |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 1030 | int glyphCount = info.glyphCount(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1031 | if (0 == glyphCount) { |
| 1032 | continue; |
| 1033 | } |
| 1034 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1035 | SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run, |
| 1036 | subRun, color, transX, transY, |
| 1037 | skPaint)); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1038 | dc->drawBatch(pipelineBuilder, batch); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1042 | inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1043 | GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1044 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1045 | SkScalar transX, SkScalar transY, |
| 1046 | const SkIRect& clipBounds) { |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1047 | if (!cacheBlob->fBigGlyphs.count()) { |
| 1048 | return; |
| 1049 | } |
| 1050 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1051 | for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1052 | GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i]; |
joshualitt | 19e4c02 | 2015-05-13 11:23:03 -0700 | [diff] [blame] | 1053 | bigGlyph.fVx += transX; |
| 1054 | bigGlyph.fVy += transY; |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1055 | SkMatrix ctm; |
| 1056 | ctm.setScale(bigGlyph.fScale, bigGlyph.fScale); |
| 1057 | ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy); |
| 1058 | if (bigGlyph.fApplyVM) { |
| 1059 | ctm.postConcat(cacheBlob->fViewMatrix); |
| 1060 | } |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1061 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1062 | GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1063 | skPaint, ctm, nullptr, clipBounds, false); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1066 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1067 | void GrAtlasTextContext::flush(const SkTextBlob* blob, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1068 | GrAtlasTextBlob* cacheBlob, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1069 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1070 | const SkPaint& skPaint, |
| 1071 | const GrPaint& grPaint, |
| 1072 | SkDrawFilter* drawFilter, |
| 1073 | const GrClip& clip, |
| 1074 | const SkMatrix& viewMatrix, |
| 1075 | const SkIRect& clipBounds, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1076 | SkScalar x, SkScalar y, |
| 1077 | SkScalar transX, SkScalar transY) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1078 | // We loop through the runs of the blob, flushing each. If any run is too large, then we flush |
| 1079 | // it as paths |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 1080 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1081 | |
| 1082 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1083 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 1084 | SkTextBlobRunIterator it(blob); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1085 | for (int run = 0; !it.done(); it.next(), run++) { |
| 1086 | if (cacheBlob->fRuns[run].fDrawAsPaths) { |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1087 | this->flushRunAsPaths(dc, it, clip, skPaint, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1088 | drawFilter, viewMatrix, clipBounds, x, y); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1089 | continue; |
| 1090 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1091 | cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1092 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1093 | transX, transY, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1097 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1100 | void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1101 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1102 | const SkPaint& skPaint, |
| 1103 | const GrPaint& grPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1104 | const GrClip& clip, |
| 1105 | const SkIRect& clipBounds) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 1106 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1107 | |
| 1108 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1109 | for (int run = 0; run < cacheBlob->fRunCount; run++) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1110 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1114 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1115 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1116 | |
| 1117 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1118 | |
| 1119 | #ifdef GR_TEST_UTILS |
| 1120 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1121 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1122 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1123 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1124 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (context->uniqueID() != gContextID) { |
| 1127 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1128 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1129 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1130 | // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly |
| 1131 | // because we don't really want to have a gpu device here. |
| 1132 | // We enable distance fields by twiddling a knob on the paint |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1133 | gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1136 | // Setup dummy SkPaint / GrPaint |
| 1137 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 1138 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1139 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1140 | skPaint.setColor(color); |
| 1141 | skPaint.setLCDRenderText(random->nextBool()); |
| 1142 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 1143 | skPaint.setSubpixelText(random->nextBool()); |
| 1144 | |
| 1145 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1146 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1147 | SkFAIL("couldn't convert paint\n"); |
| 1148 | } |
| 1149 | |
| 1150 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 1151 | int textLen = (int)strlen(text); |
| 1152 | |
| 1153 | // Setup clip |
| 1154 | GrClip clip; |
| 1155 | SkIRect noClip = SkIRect::MakeLargest(); |
| 1156 | |
| 1157 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 1158 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1159 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 1160 | gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1161 | static_cast<size_t>(textLen), 0, 0, noClip)); |
| 1162 | |
| 1163 | SkScalar transX = static_cast<SkScalar>(random->nextU()); |
| 1164 | SkScalar transY = static_cast<SkScalar>(random->nextU()); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1165 | const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0]; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1166 | return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint); |
| 1167 | } |
| 1168 | |
| 1169 | #endif |