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 | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 58 | static_assert(GrAtlasTextBatch::kGrayTextVASize >= GrAtlasTextBatch::kColorTextVASize && |
| 59 | GrAtlasTextBatch::kGrayTextVASize >= GrAtlasTextBatch::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 | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 191 | bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 192 | const GrAtlasTextBlob& blob, const SkPaint& paint, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 193 | GrColor color, const SkMaskFilter::BlurRec& blurRec, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 194 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 195 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 196 | // to regenerate the blob on any color change |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 197 | // We use the grPaint to get any color filter effects |
| 198 | if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
| 199 | blob.fPaintColor != color) { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
| 203 | if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) { |
| 208 | return true; |
| 209 | } |
| 210 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 211 | // We only cache one masked version |
| 212 | if (blob.fKey.fHasBlur && |
| 213 | (blob.fBlurRec.fSigma != blurRec.fSigma || |
| 214 | blob.fBlurRec.fStyle != blurRec.fStyle || |
| 215 | blob.fBlurRec.fQuality != blurRec.fQuality)) { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | // Similarly, we only cache one version for each style |
| 220 | if (blob.fKey.fStyle != SkPaint::kFill_Style && |
| 221 | (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 222 | blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 223 | blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
| 224 | return true; |
| 225 | } |
| 226 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 227 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 228 | // for mixed blobs if this becomes an issue. |
| 229 | if (blob.hasBitmap() && blob.hasDistanceField()) { |
joshualitt | 473ffa1 | 2015-04-22 18:23:15 -0700 | [diff] [blame] | 230 | // Identical viewmatrices and we can reuse in all cases |
| 231 | if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) { |
| 232 | return false; |
| 233 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 237 | if (blob.hasBitmap()) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 238 | if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() || |
| 239 | blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() || |
| 240 | blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() || |
| 241 | blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) { |
| 242 | return true; |
| 243 | } |
| 244 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 245 | // We can update the positions in the cachedtextblobs without regenerating the whole blob, |
| 246 | // but only for integer translations. |
| 247 | // This cool bit of math will determine the necessary translation to apply to the already |
| 248 | // generated vertex coordinates to move them to the correct position |
| 249 | SkScalar transX = viewMatrix.getTranslateX() + |
| 250 | viewMatrix.getScaleX() * (x - blob.fX) + |
| 251 | viewMatrix.getSkewX() * (y - blob.fY) - |
| 252 | blob.fViewMatrix.getTranslateX(); |
| 253 | SkScalar transY = viewMatrix.getTranslateY() + |
| 254 | viewMatrix.getSkewY() * (x - blob.fX) + |
| 255 | viewMatrix.getScaleY() * (y - blob.fY) - |
| 256 | blob.fViewMatrix.getTranslateY(); |
joshualitt | f0c000d | 2015-04-27 09:36:55 -0700 | [diff] [blame] | 257 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 261 | (*outTransX) = transX; |
| 262 | (*outTransY) = transY; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 263 | } else if (blob.hasDistanceField()) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 264 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 265 | // distance field being generated, so we have to regenerate in those cases |
| 266 | SkScalar newMaxScale = viewMatrix.getMaxScale(); |
| 267 | SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale(); |
| 268 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 269 | if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) { |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | (*outTransX) = x - blob.fX; |
| 274 | (*outTransY) = y - blob.fY; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 275 | } |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 276 | |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 277 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 278 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 279 | // the blob anyways at flush time, so no need to regenerate explicitly |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 280 | return false; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 284 | inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 285 | const SkPaint& skPaint, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 286 | const SkMatrix* viewMatrix, |
| 287 | bool noGamma) { |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 288 | skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 289 | run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 290 | return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc()); |
| 291 | } |
| 292 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 293 | void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 294 | const GrClip& clip, const SkPaint& skPaint, |
| 295 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 296 | SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 297 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 298 | // If we have been abandoned, then don't draw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 299 | if (fContext->abandoned()) { |
| 300 | return; |
| 301 | } |
| 302 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 303 | SkAutoTUnref<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 304 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 305 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 306 | // It might be worth caching these things, but its not clear at this time |
| 307 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 308 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 309 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 310 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 311 | drawFilter); |
| 312 | |
| 313 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 314 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 315 | |
| 316 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 317 | SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 318 | kUnknown_SkPixelGeometry; |
| 319 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 320 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 321 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 322 | // ensure we always match the same key |
| 323 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 324 | ComputeCanonicalColor(skPaint, hasLCD); |
| 325 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 326 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 327 | key.fUniqueID = blob->uniqueID(); |
| 328 | key.fStyle = skPaint.getStyle(); |
| 329 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 330 | key.fCanonicalColor = canonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 331 | cacheBlob.reset(SkSafeRef(fCache->find(key))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 332 | } |
| 333 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 334 | SkScalar transX = 0.f; |
| 335 | SkScalar transY = 0.f; |
| 336 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 337 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 338 | // info. |
| 339 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 340 | if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 341 | return; |
| 342 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 343 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 344 | if (cacheBlob) { |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 345 | if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec, |
| 346 | viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 347 | // We have to remake the blob because changes may invalidate our masks. |
| 348 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 349 | // but we'd have to clear the subrun information |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 350 | fCache->remove(cacheBlob); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 351 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 352 | GrAtlasTextBatch::kGrayTextVASize))); |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 353 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 354 | blob, x, y, drawFilter, clip); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 355 | } else { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 356 | // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y |
joshualitt | 7e7b5c5 | 2015-07-21 12:56:56 -0700 | [diff] [blame] | 357 | // offsets. Note, we offset the vertex bounds right before flushing |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 358 | cacheBlob->fViewMatrix = viewMatrix; |
| 359 | cacheBlob->fX = x; |
| 360 | cacheBlob->fY = y; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 361 | fCache->makeMRU(cacheBlob); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 362 | #ifdef CACHE_SANITY_CHECK |
| 363 | { |
| 364 | int glyphCount = 0; |
| 365 | int runCount = 0; |
| 366 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
| 367 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount, |
| 368 | kGrayTextVASize)); |
| 369 | GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint); |
| 370 | this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 371 | blob, x, y, drawFilter, clip); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 372 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 373 | } |
| 374 | |
| 375 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 376 | } |
| 377 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 378 | if (canCache) { |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 379 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 380 | GrAtlasTextBatch::kGrayTextVASize))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 381 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 382 | cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBatch::kGrayTextVASize)); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 383 | } |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 384 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 385 | blob, x, y, drawFilter, clip); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 386 | } |
| 387 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 388 | this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 389 | clip, viewMatrix, clipBounds, x, y, transX, transY); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 390 | } |
| 391 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 392 | inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, |
| 393 | const SkMatrix& viewMatrix) { |
| 394 | // TODO: support perspective (need getMaxScale replacement) |
| 395 | if (viewMatrix.hasPerspective()) { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 400 | SkScalar scaledTextSize = maxScale*skPaint.getTextSize(); |
| 401 | // Hinted text looks far better at small resolutions |
| 402 | // Scaling up beyond 2x yields undesireable artifacts |
jvanverth | 34d7288 | 2015-06-22 08:08:09 -0700 | [diff] [blame] | 403 | if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 407 | bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts(); |
robertphillips | bcd7ab5 | 2015-06-18 05:27:18 -0700 | [diff] [blame] | 408 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 409 | useDFT = true; |
| 410 | #endif |
| 411 | |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 412 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
| 416 | // rasterizers and mask filters modify alpha, which doesn't |
| 417 | // translate well to distance |
| 418 | if (skPaint.getRasterizer() || skPaint.getMaskFilter() || |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 419 | !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | |
| 423 | // TODO: add some stroking support |
| 424 | if (skPaint.getStyle() != SkPaint::kFill_Style) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return true; |
| 429 | } |
| 430 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 431 | void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 432 | const SkPaint& skPaint, GrColor color, |
| 433 | const SkMatrix& viewMatrix, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 434 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 435 | SkDrawFilter* drawFilter, |
| 436 | const GrClip& clip) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 437 | // 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] | 438 | // have to regenerate LCD text blobs. |
| 439 | // We use this color vs the SkPaint color because it has the colorfilter applied. |
| 440 | cacheBlob->fPaintColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 441 | cacheBlob->fViewMatrix = viewMatrix; |
| 442 | cacheBlob->fX = x; |
| 443 | cacheBlob->fY = y; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 444 | |
| 445 | // Regenerate textblob |
| 446 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 447 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 448 | for (int run = 0; !it.done(); it.next(), run++) { |
| 449 | int glyphCount = it.glyphCount(); |
| 450 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 451 | const SkPoint& offset = it.offset(); |
| 452 | // applyFontToPaint() always overwrites the exact same attributes, |
| 453 | // so it is safe to not re-seed the paint for this reason. |
| 454 | it.applyFontToPaint(&runPaint); |
| 455 | |
| 456 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 457 | // A false return from filter() means we should abort the current draw. |
| 458 | runPaint = skPaint; |
| 459 | continue; |
| 460 | } |
| 461 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 462 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 463 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 464 | // setup vertex / glyphIndex for the new run |
| 465 | if (run > 0) { |
| 466 | PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back(); |
| 467 | PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back(); |
| 468 | |
| 469 | newRun.fVertexStartIndex = lastRun.fVertexEndIndex; |
| 470 | newRun.fVertexEndIndex = lastRun.fVertexEndIndex; |
| 471 | |
| 472 | newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex; |
| 473 | newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex; |
| 474 | } |
| 475 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 476 | if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) { |
| 477 | cacheBlob->setHasDistanceField(); |
| 478 | SkPaint dfPaint = runPaint; |
| 479 | SkScalar textRatio; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 480 | this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 481 | Run& runIdx = cacheBlob->fRuns[run]; |
| 482 | PerSubRunInfo& subRun = runIdx.fSubRunInfo.back(); |
| 483 | subRun.fUseLCDText = runPaint.isLCDRenderText(); |
| 484 | subRun.fDrawAsDistanceFields = true; |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 485 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 486 | SkTDArray<char> fallbackTxt; |
| 487 | SkTDArray<SkScalar> fallbackPos; |
| 488 | SkPoint dfOffset; |
| 489 | int scalarsPerPosition = 2; |
| 490 | switch (it.positioning()) { |
| 491 | case SkTextBlob::kDefault_Positioning: { |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 492 | this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 493 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 494 | x + offset.x(), y + offset.y(), textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 495 | &fallbackTxt, &fallbackPos, &dfOffset, runPaint); |
| 496 | break; |
| 497 | } |
| 498 | case SkTextBlob::kHorizontal_Positioning: { |
| 499 | scalarsPerPosition = 1; |
| 500 | dfOffset = SkPoint::Make(x, y + offset.y()); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 501 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 502 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 503 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 504 | &fallbackTxt, &fallbackPos); |
| 505 | break; |
| 506 | } |
| 507 | case SkTextBlob::kFull_Positioning: { |
| 508 | dfOffset = SkPoint::Make(x, y); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 509 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 510 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 511 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 512 | &fallbackTxt, &fallbackPos); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 517 | this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix, |
| 518 | fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 519 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 520 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
| 521 | cacheBlob->fRuns[run].fDrawAsPaths = true; |
| 522 | } else { |
| 523 | cacheBlob->setHasBitmap(); |
| 524 | SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix, |
| 525 | false); |
| 526 | switch (it.positioning()) { |
| 527 | case SkTextBlob::kDefault_Positioning: |
| 528 | this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 529 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 530 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 531 | break; |
| 532 | case SkTextBlob::kHorizontal_Positioning: |
| 533 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 534 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 535 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 536 | break; |
| 537 | case SkTextBlob::kFull_Positioning: |
| 538 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 539 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 540 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | SkGlyphCache::AttachCache(cache); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | if (drawFilter) { |
| 547 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 548 | runPaint = skPaint; |
| 549 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 553 | inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 554 | SkPaint* skPaint, |
| 555 | SkScalar* textRatio, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 556 | const SkMatrix& viewMatrix) { |
| 557 | // getMaxScale doesn't support perspective, so neither do we at the moment |
| 558 | SkASSERT(!viewMatrix.hasPerspective()); |
| 559 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 560 | SkScalar textSize = skPaint->getTextSize(); |
| 561 | SkScalar scaledTextSize = textSize; |
| 562 | // if we have non-unity scale, we need to choose our base text size |
| 563 | // based on the SkPaint's text size multiplied by the max scale factor |
| 564 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 565 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 566 | scaledTextSize *= maxScale; |
| 567 | } |
| 568 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 569 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 570 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 571 | SkScalar dfMaskScaleFloor; |
| 572 | SkScalar dfMaskScaleCeil; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 573 | if (scaledTextSize <= kSmallDFFontLimit) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 574 | dfMaskScaleFloor = kMinDFFontSize; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 575 | dfMaskScaleCeil = kSmallDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 576 | *textRatio = textSize / kSmallDFFontSize; |
| 577 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 578 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 579 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 580 | dfMaskScaleCeil = kMediumDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 581 | *textRatio = textSize / kMediumDFFontSize; |
| 582 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 583 | } else { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 584 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 585 | dfMaskScaleCeil = kLargeDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 586 | *textRatio = textSize / kLargeDFFontSize; |
| 587 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 588 | } |
| 589 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 590 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 591 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 592 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 593 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 594 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 595 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 596 | // level) |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 597 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 598 | blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale); |
| 599 | blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale); |
| 600 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 601 | skPaint->setLCDRenderText(false); |
| 602 | skPaint->setAutohinted(false); |
| 603 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 604 | skPaint->setSubpixelText(true); |
| 605 | } |
| 606 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 607 | inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 608 | int runIndex, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 609 | const GrClip& clip, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 610 | GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 611 | const SkPaint& skPaint, |
| 612 | const SkMatrix& viewMatrix, |
| 613 | const SkTDArray<char>& fallbackTxt, |
| 614 | const SkTDArray<SkScalar>& fallbackPos, |
| 615 | int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 616 | const SkPoint& offset) { |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 617 | SkASSERT(fallbackTxt.count()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 618 | blob->setHasBitmap(); |
| 619 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 620 | // Push back a new subrun to fill and set the override descriptor |
| 621 | run.push_back(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 622 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 623 | skPaint.getScalerContextDescriptor(run.fOverrideDescriptor, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 624 | fSurfaceProps, &viewMatrix, false); |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 625 | SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface, |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 626 | run.fOverrideDescriptor->getDesc()); |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 627 | this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 628 | fallbackTxt.begin(), fallbackTxt.count(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 629 | fallbackPos.begin(), scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 630 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 631 | } |
| 632 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 633 | inline GrAtlasTextBlob* |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 634 | GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 635 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 636 | SkScalar* textRatio) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 637 | GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 638 | |
| 639 | *dfPaint = origPaint; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 640 | this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 641 | blob->fViewMatrix = viewMatrix; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 642 | Run& run = blob->fRuns[0]; |
| 643 | PerSubRunInfo& subRun = run.fSubRunInfo.back(); |
| 644 | subRun.fUseLCDText = origPaint.isLCDRenderText(); |
| 645 | subRun.fDrawAsDistanceFields = true; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 646 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 647 | return blob; |
| 648 | } |
| 649 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 650 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 651 | GrAtlasTextContext::createDrawTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 652 | const GrPaint& paint, const SkPaint& skPaint, |
| 653 | const SkMatrix& viewMatrix, |
| 654 | const char text[], size_t byteLength, |
| 655 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 656 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 657 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 658 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 659 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 660 | SkPaint dfPaint; |
| 661 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 662 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 663 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 664 | SkTDArray<char> fallbackTxt; |
| 665 | SkTDArray<SkScalar> fallbackPos; |
| 666 | SkPoint offset; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 667 | this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 668 | byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 669 | &offset, skPaint); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 670 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 671 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 672 | fallbackTxt, fallbackPos, 2, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 673 | } |
| 674 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 675 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 676 | blob->fViewMatrix = viewMatrix; |
| 677 | |
| 678 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 679 | this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 680 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 681 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 682 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 683 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 684 | } |
| 685 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 686 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 687 | GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 688 | const GrPaint& paint, const SkPaint& skPaint, |
| 689 | const SkMatrix& viewMatrix, |
| 690 | const char text[], size_t byteLength, |
| 691 | const SkScalar pos[], int scalarsPerPosition, |
| 692 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 693 | int glyphCount = skPaint.countText(text, byteLength); |
| 694 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 695 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 696 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 697 | SkPaint dfPaint; |
| 698 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 699 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 700 | |
| 701 | SkTDArray<char> fallbackTxt; |
| 702 | SkTDArray<SkScalar> fallbackPos; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 703 | this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 704 | byteLength, pos, scalarsPerPosition, offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 705 | textRatio, &fallbackTxt, &fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 706 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 707 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 708 | fallbackTxt, fallbackPos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 709 | } |
| 710 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 711 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 712 | blob->fViewMatrix = viewMatrix; |
| 713 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 714 | this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 715 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 716 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 717 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 718 | return blob; |
| 719 | } |
| 720 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 721 | void GrAtlasTextContext::onDrawText(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 722 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 723 | const GrPaint& paint, const SkPaint& skPaint, |
| 724 | const SkMatrix& viewMatrix, |
| 725 | const char text[], size_t byteLength, |
| 726 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 727 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 728 | this->createDrawTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 729 | text, byteLength, x, y, regionClipBounds)); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 730 | this->flush(blob, dc, skPaint, paint, clip, regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 731 | } |
| 732 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 733 | void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 734 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 735 | const GrPaint& paint, const SkPaint& skPaint, |
| 736 | const SkMatrix& viewMatrix, |
| 737 | const char text[], size_t byteLength, |
| 738 | const SkScalar pos[], int scalarsPerPosition, |
| 739 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 740 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 741 | this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 742 | text, byteLength, |
| 743 | pos, scalarsPerPosition, |
| 744 | offset, regionClipBounds)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 745 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 746 | this->flush(blob, dc, skPaint, paint, clip, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 747 | } |
| 748 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 749 | void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 750 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 751 | GrColor color, |
| 752 | const SkMatrix& viewMatrix, |
| 753 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 754 | SkScalar x, SkScalar y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 755 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 756 | |
| 757 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 758 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 759 | return; |
| 760 | } |
| 761 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 762 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 763 | |
| 764 | // Get GrFontScaler from cache |
| 765 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 766 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 767 | SkFindAndPlaceGlyph::ProcessText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 768 | skPaint.getTextEncoding(), text, byteLength, |
| 769 | {x, y}, viewMatrix, skPaint.getTextAlign(), |
| 770 | cache, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 771 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 772 | position += rounding; |
| 773 | this->bmpAppendGlyph( |
| 774 | blob, runIndex, glyph, |
| 775 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 776 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 777 | } |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 778 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 779 | } |
| 780 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 781 | void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 782 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 783 | GrColor color, |
| 784 | const SkMatrix& viewMatrix, |
| 785 | const char text[], size_t byteLength, |
| 786 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 787 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 788 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 789 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 790 | |
| 791 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 792 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 796 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 797 | |
| 798 | // Get GrFontScaler from cache |
| 799 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 800 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 801 | SkFindAndPlaceGlyph::ProcessPosText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 802 | skPaint.getTextEncoding(), text, byteLength, |
| 803 | offset, viewMatrix, pos, scalarsPerPosition, |
| 804 | skPaint.getTextAlign(), cache, |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 805 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 806 | position += rounding; |
| 807 | this->bmpAppendGlyph( |
| 808 | blob, runIndex, glyph, |
| 809 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 810 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 811 | } |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 812 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 813 | } |
| 814 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 815 | void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 816 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 817 | const SkMatrix& viewMatrix, |
| 818 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 819 | SkScalar x, SkScalar y, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 820 | SkScalar textRatio, |
| 821 | SkTDArray<char>* fallbackTxt, |
| 822 | SkTDArray<SkScalar>* fallbackPos, |
| 823 | SkPoint* offset, |
| 824 | const SkPaint& origPaint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 825 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 826 | |
| 827 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 828 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc(); |
| 833 | SkAutoDescriptor desc; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 834 | origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true); |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 835 | SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(), |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 836 | desc.getDesc()); |
| 837 | |
| 838 | SkTArray<SkScalar> positions; |
| 839 | |
| 840 | const char* textPtr = text; |
| 841 | SkFixed stopX = 0; |
| 842 | SkFixed stopY = 0; |
| 843 | SkFixed origin = 0; |
| 844 | switch (origPaint.getTextAlign()) { |
| 845 | case SkPaint::kRight_Align: origin = SK_Fixed1; break; |
| 846 | case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; |
| 847 | case SkPaint::kLeft_Align: origin = 0; break; |
| 848 | } |
| 849 | |
| 850 | SkAutoKern autokern; |
| 851 | const char* stop = text + byteLength; |
| 852 | while (textPtr < stop) { |
| 853 | // don't need x, y here, since all subpixel variants will have the |
| 854 | // same advance |
| 855 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0); |
| 856 | |
| 857 | SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); |
| 858 | positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width))); |
| 859 | |
| 860 | SkFixed height = glyph.fAdvanceY; |
| 861 | positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height))); |
| 862 | |
| 863 | stopX += width; |
| 864 | stopY += height; |
| 865 | } |
| 866 | SkASSERT(textPtr == stop); |
| 867 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 868 | SkGlyphCache::AttachCache(origPaintCache); |
| 869 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 870 | // now adjust starting point depending on alignment |
| 871 | SkScalar alignX = SkFixedToScalar(stopX); |
| 872 | SkScalar alignY = SkFixedToScalar(stopY); |
| 873 | if (origPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 874 | alignX = SkScalarHalf(alignX); |
| 875 | alignY = SkScalarHalf(alignY); |
| 876 | } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 877 | alignX = 0; |
| 878 | alignY = 0; |
| 879 | } |
| 880 | x -= alignX; |
| 881 | y -= alignY; |
| 882 | *offset = SkPoint::Make(x, y); |
| 883 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 884 | this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 885 | positions.begin(), 2, *offset, textRatio, fallbackTxt, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 886 | fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 887 | } |
| 888 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 889 | void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 890 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 891 | const SkMatrix& viewMatrix, |
| 892 | const char text[], size_t byteLength, |
| 893 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 894 | const SkPoint& offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 895 | SkScalar textRatio, |
| 896 | SkTDArray<char>* fallbackTxt, |
| 897 | SkTDArray<SkScalar>* fallbackPos) { |
| 898 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 899 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 900 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 901 | |
| 902 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 903 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 907 | fCurrStrike = nullptr; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 908 | |
| 909 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 910 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 911 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 912 | |
| 913 | const char* stop = text + byteLength; |
| 914 | |
| 915 | if (SkPaint::kLeft_Align == skPaint.getTextAlign()) { |
| 916 | while (text < stop) { |
| 917 | const char* lastText = text; |
| 918 | // the last 2 parameters are ignored |
| 919 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 920 | |
| 921 | if (glyph.fWidth) { |
| 922 | SkScalar x = offset.x() + pos[0]; |
| 923 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 924 | |
| 925 | if (!this->dfAppendGlyph(blob, |
| 926 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 927 | glyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 928 | x, y, color, fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 929 | textRatio, viewMatrix)) { |
| 930 | // couldn't append, send to fallback |
| 931 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 932 | *fallbackPos->append() = pos[0]; |
| 933 | if (2 == scalarsPerPosition) { |
| 934 | *fallbackPos->append() = pos[1]; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | pos += scalarsPerPosition; |
| 939 | } |
| 940 | } else { |
| 941 | SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf |
| 942 | : SK_Scalar1; |
| 943 | while (text < stop) { |
| 944 | const char* lastText = text; |
| 945 | // the last 2 parameters are ignored |
| 946 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 947 | |
| 948 | if (glyph.fWidth) { |
| 949 | SkScalar x = offset.x() + pos[0]; |
| 950 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 951 | |
| 952 | SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 953 | SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 954 | |
| 955 | if (!this->dfAppendGlyph(blob, |
| 956 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 957 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 958 | x - advanceX, y - advanceY, color, |
| 959 | fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 960 | textRatio, |
| 961 | viewMatrix)) { |
| 962 | // couldn't append, send to fallback |
| 963 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 964 | *fallbackPos->append() = pos[0]; |
| 965 | if (2 == scalarsPerPosition) { |
| 966 | *fallbackPos->append() = pos[1]; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | pos += scalarsPerPosition; |
| 971 | } |
| 972 | } |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 973 | |
| 974 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 975 | } |
| 976 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 977 | void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 978 | const SkGlyph& skGlyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 979 | int vx, int vy, GrColor color, GrFontScaler* scaler) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 980 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 981 | if (!fCurrStrike) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 982 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 983 | } |
| 984 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 985 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 986 | skGlyph.getSubXFixed(), |
| 987 | skGlyph.getSubYFixed(), |
| 988 | GrGlyph::kCoverage_MaskStyle); |
| 989 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 990 | if (!glyph) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
| 994 | int x = vx + glyph->fBounds.fLeft; |
| 995 | int y = vy + glyph->fBounds.fTop; |
| 996 | |
| 997 | // keep them as ints until we've done the clip-test |
| 998 | int width = glyph->fBounds.width(); |
| 999 | int height = glyph->fBounds.height(); |
| 1000 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1001 | // If the glyph is too large we fall back to paths |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1002 | if (glyph->fTooLargeForAtlas) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1003 | this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1007 | GrMaskFormat format = glyph->fMaskFormat; |
| 1008 | |
| 1009 | PerSubRunInfo* subRun = &run.fSubRunInfo.back(); |
| 1010 | if (run.fInitialized && subRun->fMaskFormat != format) { |
joshualitt | d9f13ae | 2015-07-24 11:24:31 -0700 | [diff] [blame] | 1011 | subRun = &run.push_back(); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1012 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
| 1013 | } else if (!run.fInitialized) { |
| 1014 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | run.fInitialized = true; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1018 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1019 | size_t vertexStride = GrAtlasTextBatch::GetVertexStride(format); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1020 | |
| 1021 | SkRect r; |
| 1022 | r.fLeft = SkIntToScalar(x); |
| 1023 | r.fTop = SkIntToScalar(y); |
| 1024 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 1025 | r.fBottom = r.fTop + SkIntToScalar(height); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1026 | subRun->fMaskFormat = format; |
| 1027 | this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1028 | glyph); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1029 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1030 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1031 | bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1032 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1033 | SkScalar sx, SkScalar sy, GrColor color, |
| 1034 | GrFontScaler* scaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1035 | SkScalar textRatio, const SkMatrix& viewMatrix) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1036 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1037 | if (!fCurrStrike) { |
| 1038 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 1039 | } |
| 1040 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1041 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 1042 | skGlyph.getSubXFixed(), |
| 1043 | skGlyph.getSubYFixed(), |
| 1044 | GrGlyph::kDistance_MaskStyle); |
| 1045 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1046 | if (!glyph) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1047 | return true; |
| 1048 | } |
| 1049 | |
| 1050 | // fallback to color glyph support |
| 1051 | if (kA8_GrMaskFormat != glyph->fMaskFormat) { |
| 1052 | return false; |
| 1053 | } |
| 1054 | |
| 1055 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 1056 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 1057 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 1058 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 1059 | |
| 1060 | SkScalar scale = textRatio; |
| 1061 | dx *= scale; |
| 1062 | dy *= scale; |
| 1063 | width *= scale; |
| 1064 | height *= scale; |
| 1065 | sx += dx; |
| 1066 | sy += dy; |
| 1067 | SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height); |
| 1068 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1069 | // TODO combine with the above |
| 1070 | // If the glyph is too large we fall back to paths |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1071 | if (glyph->fTooLargeForAtlas) { |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1072 | this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1073 | return true; |
| 1074 | } |
| 1075 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1076 | PerSubRunInfo* subRun = &run.fSubRunInfo.back(); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1077 | if (!run.fInitialized) { |
| 1078 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
| 1079 | } |
| 1080 | run.fInitialized = true; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1081 | SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat); |
| 1082 | subRun->fMaskFormat = kA8_GrMaskFormat; |
| 1083 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1084 | size_t vertexStride = GrAtlasTextBatch::GetVertexStrideDf(kA8_GrMaskFormat, |
| 1085 | subRun->fUseLCDText); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1086 | |
| 1087 | bool useColorVerts = !subRun->fUseLCDText; |
| 1088 | this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1089 | glyph); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1090 | return true; |
| 1091 | } |
| 1092 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1093 | inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1094 | GrFontScaler* scaler, const SkGlyph& skGlyph, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1095 | SkScalar x, SkScalar y, SkScalar scale, |
| 1096 | bool applyVM) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1097 | if (nullptr == glyph->fPath) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1098 | const SkPath* glyphPath = scaler->getGlyphPath(skGlyph); |
| 1099 | if (!glyphPath) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1100 | return; |
| 1101 | } |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1102 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1103 | glyph->fPath = new SkPath(*glyphPath); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1104 | } |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1105 | blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM)); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1108 | inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1109 | Run::SubRunInfo* subRun, |
| 1110 | const SkRect& positions, GrColor color, |
| 1111 | size_t vertexStride, bool useVertexColor, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1112 | GrGlyph* glyph) { |
| 1113 | blob->fGlyphs[subRun->fGlyphEndIndex] = glyph; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1114 | run->fVertexBounds.joinNonEmptyArg(positions); |
| 1115 | run->fColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1116 | |
| 1117 | intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex); |
| 1118 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1119 | if (useVertexColor) { |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1120 | // V0 |
| 1121 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 1122 | position->set(positions.fLeft, positions.fTop); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1123 | SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 1124 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1125 | vertex += vertexStride; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1126 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1127 | // V1 |
| 1128 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1129 | position->set(positions.fLeft, positions.fBottom); |
| 1130 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1131 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1132 | vertex += vertexStride; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1133 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1134 | // V2 |
| 1135 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1136 | position->set(positions.fRight, positions.fBottom); |
| 1137 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1138 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1139 | vertex += vertexStride; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1140 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1141 | // V3 |
| 1142 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1143 | position->set(positions.fRight, positions.fTop); |
| 1144 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1145 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1146 | } else { |
| 1147 | // V0 |
| 1148 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 1149 | position->set(positions.fLeft, positions.fTop); |
| 1150 | vertex += vertexStride; |
| 1151 | |
| 1152 | // V1 |
| 1153 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1154 | position->set(positions.fLeft, positions.fBottom); |
| 1155 | vertex += vertexStride; |
| 1156 | |
| 1157 | // V2 |
| 1158 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1159 | position->set(positions.fRight, positions.fBottom); |
| 1160 | vertex += vertexStride; |
| 1161 | |
| 1162 | // V3 |
| 1163 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1164 | position->set(positions.fRight, positions.fTop); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | subRun->fGlyphEndIndex++; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1168 | subRun->fVertexEndIndex += vertexStride * GrAtlasTextBatch::kVerticesPerGlyph; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1171 | void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1172 | const SkTextBlobRunIterator& it, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1173 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1174 | SkDrawFilter* drawFilter, const SkMatrix& viewMatrix, |
| 1175 | const SkIRect& clipBounds, SkScalar x, SkScalar y) { |
| 1176 | SkPaint runPaint = skPaint; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1177 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1178 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 1179 | const SkPoint& offset = it.offset(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1180 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1181 | it.applyFontToPaint(&runPaint); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1182 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1183 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 1184 | return; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1187 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1188 | |
| 1189 | switch (it.positioning()) { |
| 1190 | case SkTextBlob::kDefault_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1191 | this->drawTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1192 | (const char *)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1193 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 1194 | break; |
| 1195 | case SkTextBlob::kHorizontal_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1196 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1197 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1198 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 1199 | clipBounds); |
| 1200 | break; |
| 1201 | case SkTextBlob::kFull_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1202 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1203 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1204 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1209 | inline GrDrawBatch* |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1210 | GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1211 | int glyphCount, int run, int subRun, |
| 1212 | GrColor color, SkScalar transX, SkScalar transY, |
| 1213 | const SkPaint& skPaint) { |
| 1214 | GrMaskFormat format = info.fMaskFormat; |
| 1215 | GrColor subRunColor; |
| 1216 | if (kARGB_GrMaskFormat == format) { |
| 1217 | uint8_t paintAlpha = skPaint.getAlpha(); |
| 1218 | subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha); |
| 1219 | } else { |
| 1220 | subRunColor = color; |
| 1221 | } |
| 1222 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1223 | GrAtlasTextBatch* batch; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1224 | if (info.fDrawAsDistanceFields) { |
| 1225 | SkColor filteredColor; |
| 1226 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 1227 | if (colorFilter) { |
| 1228 | filteredColor = colorFilter->filterColor(skPaint.getColor()); |
| 1229 | } else { |
| 1230 | filteredColor = skPaint.getColor(); |
| 1231 | } |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1232 | bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1233 | batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(), |
| 1234 | fDistanceAdjustTable, filteredColor, |
| 1235 | info.fUseLCDText, useBGR); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1236 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1237 | batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1238 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 1239 | GrAtlasTextBatch::Geometry& geometry = batch->geometry(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1240 | geometry.fBlob = SkRef(cacheBlob); |
| 1241 | geometry.fRun = run; |
| 1242 | geometry.fSubRun = subRun; |
| 1243 | geometry.fColor = subRunColor; |
| 1244 | geometry.fTransX = transX; |
| 1245 | geometry.fTransY = transY; |
| 1246 | batch->init(); |
| 1247 | |
| 1248 | return batch; |
| 1249 | } |
| 1250 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1251 | inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1252 | GrAtlasTextBlob* cacheBlob, int run, GrColor color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1253 | SkScalar transX, SkScalar transY, |
| 1254 | const SkPaint& skPaint) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1255 | for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) { |
| 1256 | const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun]; |
| 1257 | int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex; |
| 1258 | if (0 == glyphCount) { |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1262 | SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run, |
| 1263 | subRun, color, transX, transY, |
| 1264 | skPaint)); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1265 | dc->drawBatch(pipelineBuilder, batch); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1269 | inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1270 | GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1271 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1272 | SkScalar transX, SkScalar transY, |
| 1273 | const SkIRect& clipBounds) { |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1274 | if (!cacheBlob->fBigGlyphs.count()) { |
| 1275 | return; |
| 1276 | } |
| 1277 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1278 | for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1279 | GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i]; |
joshualitt | 19e4c02 | 2015-05-13 11:23:03 -0700 | [diff] [blame] | 1280 | bigGlyph.fVx += transX; |
| 1281 | bigGlyph.fVy += transY; |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1282 | SkMatrix ctm; |
| 1283 | ctm.setScale(bigGlyph.fScale, bigGlyph.fScale); |
| 1284 | ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy); |
| 1285 | if (bigGlyph.fApplyVM) { |
| 1286 | ctm.postConcat(cacheBlob->fViewMatrix); |
| 1287 | } |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1288 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1289 | GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1290 | skPaint, ctm, nullptr, clipBounds, false); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1293 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1294 | void GrAtlasTextContext::flush(const SkTextBlob* blob, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1295 | GrAtlasTextBlob* cacheBlob, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1296 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1297 | const SkPaint& skPaint, |
| 1298 | const GrPaint& grPaint, |
| 1299 | SkDrawFilter* drawFilter, |
| 1300 | const GrClip& clip, |
| 1301 | const SkMatrix& viewMatrix, |
| 1302 | const SkIRect& clipBounds, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1303 | SkScalar x, SkScalar y, |
| 1304 | SkScalar transX, SkScalar transY) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1305 | // We loop through the runs of the blob, flushing each. If any run is too large, then we flush |
| 1306 | // it as paths |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 1307 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1310 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 1311 | SkTextBlobRunIterator it(blob); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1312 | for (int run = 0; !it.done(); it.next(), run++) { |
| 1313 | if (cacheBlob->fRuns[run].fDrawAsPaths) { |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1314 | this->flushRunAsPaths(dc, it, clip, skPaint, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1315 | drawFilter, viewMatrix, clipBounds, x, y); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1316 | continue; |
| 1317 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1318 | cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1319 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1320 | transX, transY, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1324 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1327 | void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1328 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1329 | const SkPaint& skPaint, |
| 1330 | const GrPaint& grPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1331 | const GrClip& clip, |
| 1332 | const SkIRect& clipBounds) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame^] | 1333 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1334 | |
| 1335 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1336 | for (int run = 0; run < cacheBlob->fRunCount; run++) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1337 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1341 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1342 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1343 | |
| 1344 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1345 | |
| 1346 | #ifdef GR_TEST_UTILS |
| 1347 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1348 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1349 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1350 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1351 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (context->uniqueID() != gContextID) { |
| 1354 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1355 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1356 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1357 | // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly |
| 1358 | // because we don't really want to have a gpu device here. |
| 1359 | // We enable distance fields by twiddling a knob on the paint |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1360 | gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1363 | // Setup dummy SkPaint / GrPaint |
| 1364 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 1365 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1366 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1367 | skPaint.setColor(color); |
| 1368 | skPaint.setLCDRenderText(random->nextBool()); |
| 1369 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 1370 | skPaint.setSubpixelText(random->nextBool()); |
| 1371 | |
| 1372 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1373 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1374 | SkFAIL("couldn't convert paint\n"); |
| 1375 | } |
| 1376 | |
| 1377 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 1378 | int textLen = (int)strlen(text); |
| 1379 | |
| 1380 | // Setup clip |
| 1381 | GrClip clip; |
| 1382 | SkIRect noClip = SkIRect::MakeLargest(); |
| 1383 | |
| 1384 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 1385 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1386 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 1387 | gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1388 | static_cast<size_t>(textLen), 0, 0, noClip)); |
| 1389 | |
| 1390 | SkScalar transX = static_cast<SkScalar>(random->nextU()); |
| 1391 | SkScalar transY = static_cast<SkScalar>(random->nextU()); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1392 | const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0]; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1393 | return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint); |
| 1394 | } |
| 1395 | |
| 1396 | #endif |