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 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 9 | #include "GrBatchFontCache.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 11 | #include "GrBatchTest.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 12 | #include "GrBlurUtils.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 13 | #include "GrDefaultGeoProcFactory.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 15 | #include "GrDrawTarget.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 16 | #include "GrFontScaler.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 18 | #include "GrStrokeInfo.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 19 | #include "GrTextBlobCache.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 20 | #include "GrTexturePriv.h" |
bsalomon | 72e3ae4 | 2015-04-28 08:08:46 -0700 | [diff] [blame] | 21 | #include "GrVertexBuffer.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 22 | |
| 23 | #include "SkAutoKern.h" |
| 24 | #include "SkColorPriv.h" |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 25 | #include "SkColorFilter.h" |
| 26 | #include "SkDistanceFieldGen.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 27 | #include "SkDraw.h" |
| 28 | #include "SkDrawFilter.h" |
| 29 | #include "SkDrawProcs.h" |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 30 | #include "SkFindAndPlaceGlyph.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 31 | #include "SkGlyphCache.h" |
| 32 | #include "SkGpuDevice.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 33 | #include "SkGrPriv.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 34 | #include "SkPath.h" |
| 35 | #include "SkRTConf.h" |
| 36 | #include "SkStrokeRec.h" |
| 37 | #include "SkTextBlob.h" |
| 38 | #include "SkTextMapStateProc.h" |
| 39 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 40 | #include "batches/GrVertexBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 41 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 42 | #include "effects/GrBitmapTextGeoProc.h" |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 43 | #include "effects/GrDistanceFieldGeoProc.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | static const size_t kLCDTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16); |
| 47 | |
| 48 | // position + local coord |
| 49 | static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16); |
| 50 | |
| 51 | static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16); |
| 52 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 53 | static const int kMinDFFontSize = 18; |
| 54 | static const int kSmallDFFontSize = 32; |
| 55 | static const int kSmallDFFontLimit = 32; |
| 56 | static const int kMediumDFFontSize = 72; |
| 57 | static const int kMediumDFFontLimit = 72; |
| 58 | static const int kLargeDFFontSize = 162; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 59 | #ifdef SK_BUILD_FOR_ANDROID |
| 60 | static const int kLargeDFFontLimit = 384; |
| 61 | #else |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 62 | static const int kLargeDFFontLimit = 2 * kLargeDFFontSize; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 63 | #endif |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 64 | |
| 65 | SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;) |
| 66 | static const int kDistanceAdjustLumShift = 5; |
| 67 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 68 | static const int kVerticesPerGlyph = 4; |
| 69 | static const int kIndicesPerGlyph = 6; |
| 70 | |
| 71 | static size_t get_vertex_stride(GrMaskFormat maskFormat) { |
| 72 | switch (maskFormat) { |
| 73 | case kA8_GrMaskFormat: |
| 74 | return kGrayTextVASize; |
| 75 | case kARGB_GrMaskFormat: |
| 76 | return kColorTextVASize; |
| 77 | default: |
| 78 | return kLCDTextVASize; |
| 79 | } |
| 80 | } |
| 81 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 82 | static size_t get_vertex_stride_df(GrMaskFormat maskFormat, bool useLCDText) { |
| 83 | SkASSERT(maskFormat == kA8_GrMaskFormat); |
| 84 | if (useLCDText) { |
| 85 | return kLCDTextVASize; |
| 86 | } else { |
| 87 | return kGrayTextVASize; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
| 92 | unsigned r = SkColorGetR(c); |
| 93 | unsigned g = SkColorGetG(c); |
| 94 | unsigned b = SkColorGetB(c); |
| 95 | return GrColorPackRGBA(r, g, b, 0xff); |
| 96 | } |
| 97 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 100 | GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps) |
| 101 | : INHERITED(context, surfaceProps) |
| 102 | , fDistanceAdjustTable(new DistanceAdjustTable) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 103 | // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest |
| 104 | // vertexStride |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 105 | static_assert(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize, |
| 106 | "vertex_attribute_changed"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | fCurrStrike = nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 108 | fCache = context->getTextBlobCache(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 109 | } |
| 110 | |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 111 | void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 112 | |
| 113 | // This is used for an approximation of the mask gamma hack, used by raster and bitmap |
| 114 | // text. The mask gamma hack is based off of guessing what the blend color is going to |
| 115 | // be, and adjusting the mask so that when run through the linear blend will |
| 116 | // produce the value closest to the desired result. However, in practice this means |
| 117 | // that the 'adjusted' mask is just increasing or decreasing the coverage of |
| 118 | // the mask depending on what it is thought it will blit against. For black (on |
| 119 | // assumed white) this means that coverages are decreased (on a curve). For white (on |
| 120 | // assumed black) this means that coverages are increased (on a a curve). At |
| 121 | // middle (perceptual) gray (which could be blit against anything) the coverages |
| 122 | // remain the same. |
| 123 | // |
| 124 | // The idea here is that instead of determining the initial (real) coverage and |
| 125 | // then adjusting that coverage, we determine an adjusted coverage directly by |
| 126 | // essentially manipulating the geometry (in this case, the distance to the glyph |
| 127 | // edge). So for black (on assumed white) this thins a bit; for white (on |
| 128 | // assumed black) this fake bolds the geometry a bit. |
| 129 | // |
| 130 | // The distance adjustment is calculated by determining the actual coverage value which |
| 131 | // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This |
| 132 | // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the |
| 133 | // actual edge. So by subtracting this distance adjustment and computing without the |
| 134 | // the coverage adjustment we should get 0.5 coverage at the same point. |
| 135 | // |
| 136 | // This has several implications: |
| 137 | // For non-gray lcd smoothed text, each subpixel essentially is using a |
| 138 | // slightly different geometry. |
| 139 | // |
| 140 | // For black (on assumed white) this may not cover some pixels which were |
| 141 | // previously covered; however those pixels would have been only slightly |
| 142 | // covered and that slight coverage would have been decreased anyway. Also, some pixels |
| 143 | // which were previously fully covered may no longer be fully covered. |
| 144 | // |
| 145 | // For white (on assumed black) this may cover some pixels which weren't |
| 146 | // previously covered at all. |
| 147 | |
| 148 | int width, height; |
| 149 | size_t size; |
| 150 | |
| 151 | #ifdef SK_GAMMA_CONTRAST |
| 152 | SkScalar contrast = SK_GAMMA_CONTRAST; |
| 153 | #else |
| 154 | SkScalar contrast = 0.5f; |
| 155 | #endif |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 156 | SkScalar paintGamma = SK_GAMMA_EXPONENT; |
| 157 | SkScalar deviceGamma = SK_GAMMA_EXPONENT; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 158 | |
| 159 | size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma, |
| 160 | &width, &height); |
| 161 | |
| 162 | SkASSERT(kExpectedDistanceAdjustTableSize == height); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 163 | fTable = new SkScalar[height]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 164 | |
| 165 | SkAutoTArray<uint8_t> data((int)size); |
| 166 | SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get()); |
| 167 | |
| 168 | // find the inverse points where we cross 0.5 |
| 169 | // binsearch might be better, but we only need to do this once on creation |
| 170 | for (int row = 0; row < height; ++row) { |
| 171 | uint8_t* rowPtr = data.get() + row*width; |
| 172 | for (int col = 0; col < width - 1; ++col) { |
| 173 | if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) { |
| 174 | // compute point where a mask value will give us a result of 0.5 |
| 175 | float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]); |
| 176 | float borderAlpha = (col + interp) / 255.f; |
| 177 | |
| 178 | // compute t value for that alpha |
| 179 | // this is an approximate inverse for smoothstep() |
| 180 | float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f; |
| 181 | |
| 182 | // compute distance which gives us that t value |
| 183 | const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor |
| 184 | float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor; |
| 185 | |
| 186 | fTable[row] = d; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 191 | } |
| 192 | |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 193 | GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 194 | const SkSurfaceProps& surfaceProps) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 195 | return new GrAtlasTextContext(context, surfaceProps); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 198 | bool GrAtlasTextContext::canDraw(const GrRenderTarget*, |
| 199 | const GrClip&, |
| 200 | const GrPaint&, |
| 201 | const SkPaint& skPaint, |
| 202 | const SkMatrix& viewMatrix) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 203 | return this->canDrawAsDistanceFields(skPaint, viewMatrix) || |
| 204 | !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 205 | } |
| 206 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 207 | GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
| 208 | GrColor canonicalColor = paint.computeLuminanceColor(); |
| 209 | if (lcd) { |
| 210 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 211 | // For now we just regenerate if any run in a textblob has LCD. |
| 212 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 213 | // at a higher level *OR* use sRGB |
| 214 | SkASSERT(false); |
| 215 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 216 | } else { |
| 217 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 218 | // gamma corrected masks anyways, nor color |
| 219 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 220 | SkColorGetG(canonicalColor), |
| 221 | SkColorGetB(canonicalColor)); |
| 222 | // reduce to our finite number of bits |
| 223 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 224 | } |
| 225 | return canonicalColor; |
| 226 | } |
| 227 | |
| 228 | // TODO if this function ever shows up in profiling, then we can compute this value when the |
| 229 | // textblob is being built and cache it. However, for the time being textblobs mostly only have 1 |
| 230 | // run so this is not a big deal to compute here. |
| 231 | bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 232 | SkTextBlobRunIterator it(blob); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 233 | for (; !it.done(); it.next()) { |
| 234 | if (it.isLCD()) { |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 241 | bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 242 | const GrAtlasTextBlob& blob, const SkPaint& paint, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 243 | GrColor color, const SkMaskFilter::BlurRec& blurRec, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 244 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 245 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 246 | // to regenerate the blob on any color change |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 247 | // We use the grPaint to get any color filter effects |
| 248 | if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
| 249 | blob.fPaintColor != color) { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
| 253 | if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) { |
| 258 | return true; |
| 259 | } |
| 260 | |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 261 | // We only cache one masked version |
| 262 | if (blob.fKey.fHasBlur && |
| 263 | (blob.fBlurRec.fSigma != blurRec.fSigma || |
| 264 | blob.fBlurRec.fStyle != blurRec.fStyle || |
| 265 | blob.fBlurRec.fQuality != blurRec.fQuality)) { |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | // Similarly, we only cache one version for each style |
| 270 | if (blob.fKey.fStyle != SkPaint::kFill_Style && |
| 271 | (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 272 | blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 273 | blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
| 274 | return true; |
| 275 | } |
| 276 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 277 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 278 | // for mixed blobs if this becomes an issue. |
| 279 | if (blob.hasBitmap() && blob.hasDistanceField()) { |
joshualitt | 473ffa1 | 2015-04-22 18:23:15 -0700 | [diff] [blame] | 280 | // Identical viewmatrices and we can reuse in all cases |
| 281 | if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) { |
| 282 | return false; |
| 283 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 287 | if (blob.hasBitmap()) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 288 | if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() || |
| 289 | blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() || |
| 290 | blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() || |
| 291 | blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) { |
| 292 | return true; |
| 293 | } |
| 294 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 295 | // We can update the positions in the cachedtextblobs without regenerating the whole blob, |
| 296 | // but only for integer translations. |
| 297 | // This cool bit of math will determine the necessary translation to apply to the already |
| 298 | // generated vertex coordinates to move them to the correct position |
| 299 | SkScalar transX = viewMatrix.getTranslateX() + |
| 300 | viewMatrix.getScaleX() * (x - blob.fX) + |
| 301 | viewMatrix.getSkewX() * (y - blob.fY) - |
| 302 | blob.fViewMatrix.getTranslateX(); |
| 303 | SkScalar transY = viewMatrix.getTranslateY() + |
| 304 | viewMatrix.getSkewY() * (x - blob.fX) + |
| 305 | viewMatrix.getScaleY() * (y - blob.fY) - |
| 306 | blob.fViewMatrix.getTranslateY(); |
joshualitt | f0c000d | 2015-04-27 09:36:55 -0700 | [diff] [blame] | 307 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 311 | (*outTransX) = transX; |
| 312 | (*outTransY) = transY; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 313 | } else if (blob.hasDistanceField()) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 314 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 315 | // distance field being generated, so we have to regenerate in those cases |
| 316 | SkScalar newMaxScale = viewMatrix.getMaxScale(); |
| 317 | SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale(); |
| 318 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 319 | if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) { |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | (*outTransX) = x - blob.fX; |
| 324 | (*outTransY) = y - blob.fY; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 325 | } |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 326 | |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 327 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 328 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 329 | // the blob anyways at flush time, so no need to regenerate explicitly |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 330 | return false; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 334 | inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 335 | const SkPaint& skPaint, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 336 | const SkMatrix* viewMatrix, |
| 337 | bool noGamma) { |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 338 | skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 339 | run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 340 | return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc()); |
| 341 | } |
| 342 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 343 | void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 344 | const GrClip& clip, const SkPaint& skPaint, |
| 345 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 346 | SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 347 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 348 | // If we have been abandoned, then don't draw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 349 | if (fContext->abandoned()) { |
| 350 | return; |
| 351 | } |
| 352 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 353 | SkAutoTUnref<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 354 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 355 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 356 | // It might be worth caching these things, but its not clear at this time |
| 357 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 358 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 359 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 360 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 361 | drawFilter); |
| 362 | |
| 363 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 364 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 365 | |
| 366 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 367 | SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 368 | kUnknown_SkPixelGeometry; |
| 369 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 370 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 371 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 372 | // ensure we always match the same key |
| 373 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 374 | ComputeCanonicalColor(skPaint, hasLCD); |
| 375 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 376 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 377 | key.fUniqueID = blob->uniqueID(); |
| 378 | key.fStyle = skPaint.getStyle(); |
| 379 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 380 | key.fCanonicalColor = canonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 381 | cacheBlob.reset(SkSafeRef(fCache->find(key))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 382 | } |
| 383 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 384 | SkIRect clipRect; |
| 385 | clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 386 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 387 | SkScalar transX = 0.f; |
| 388 | SkScalar transY = 0.f; |
| 389 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 390 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 391 | // info. |
| 392 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 393 | if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 394 | return; |
| 395 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 396 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 397 | if (cacheBlob) { |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 398 | if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec, |
| 399 | viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 400 | // We have to remake the blob because changes may invalidate our masks. |
| 401 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 402 | // but we'd have to clear the subrun information |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 403 | fCache->remove(cacheBlob); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 404 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
| 405 | kGrayTextVASize))); |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 406 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 407 | blob, x, y, drawFilter, clipRect, rt, clip); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 408 | } else { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 409 | // 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] | 410 | // offsets. Note, we offset the vertex bounds right before flushing |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 411 | cacheBlob->fViewMatrix = viewMatrix; |
| 412 | cacheBlob->fX = x; |
| 413 | cacheBlob->fY = y; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 414 | fCache->makeMRU(cacheBlob); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 415 | #ifdef CACHE_SANITY_CHECK |
| 416 | { |
| 417 | int glyphCount = 0; |
| 418 | int runCount = 0; |
| 419 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
| 420 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount, |
| 421 | kGrayTextVASize)); |
| 422 | GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint); |
| 423 | this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 424 | blob, x, y, drawFilter, clipRect, rt, clip); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 425 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 426 | } |
| 427 | |
| 428 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 429 | } |
| 430 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 431 | if (canCache) { |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 432 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
| 433 | kGrayTextVASize))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 434 | } else { |
| 435 | cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize)); |
| 436 | } |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 437 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 438 | blob, x, y, drawFilter, clipRect, rt, clip); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 439 | } |
| 440 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 441 | this->flush(blob, cacheBlob, dc, rt, skPaint, grPaint, drawFilter, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 442 | clip, viewMatrix, clipBounds, x, y, transX, transY); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 443 | } |
| 444 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 445 | inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, |
| 446 | const SkMatrix& viewMatrix) { |
| 447 | // TODO: support perspective (need getMaxScale replacement) |
| 448 | if (viewMatrix.hasPerspective()) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 453 | SkScalar scaledTextSize = maxScale*skPaint.getTextSize(); |
| 454 | // Hinted text looks far better at small resolutions |
| 455 | // Scaling up beyond 2x yields undesireable artifacts |
jvanverth | 34d7288 | 2015-06-22 08:08:09 -0700 | [diff] [blame] | 456 | if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 460 | bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts(); |
robertphillips | bcd7ab5 | 2015-06-18 05:27:18 -0700 | [diff] [blame] | 461 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 462 | useDFT = true; |
| 463 | #endif |
| 464 | |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 465 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
| 469 | // rasterizers and mask filters modify alpha, which doesn't |
| 470 | // translate well to distance |
| 471 | if (skPaint.getRasterizer() || skPaint.getMaskFilter() || |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 472 | !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
| 476 | // TODO: add some stroking support |
| 477 | if (skPaint.getStyle() != SkPaint::kFill_Style) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 484 | void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 485 | const SkPaint& skPaint, GrColor color, |
| 486 | const SkMatrix& viewMatrix, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 487 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 488 | SkDrawFilter* drawFilter, const SkIRect& clipRect, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 489 | GrRenderTarget* rt, const GrClip& clip) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 490 | // 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] | 491 | // have to regenerate LCD text blobs. |
| 492 | // We use this color vs the SkPaint color because it has the colorfilter applied. |
| 493 | cacheBlob->fPaintColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 494 | cacheBlob->fViewMatrix = viewMatrix; |
| 495 | cacheBlob->fX = x; |
| 496 | cacheBlob->fY = y; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 497 | |
| 498 | // Regenerate textblob |
| 499 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 500 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 501 | for (int run = 0; !it.done(); it.next(), run++) { |
| 502 | int glyphCount = it.glyphCount(); |
| 503 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 504 | const SkPoint& offset = it.offset(); |
| 505 | // applyFontToPaint() always overwrites the exact same attributes, |
| 506 | // so it is safe to not re-seed the paint for this reason. |
| 507 | it.applyFontToPaint(&runPaint); |
| 508 | |
| 509 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 510 | // A false return from filter() means we should abort the current draw. |
| 511 | runPaint = skPaint; |
| 512 | continue; |
| 513 | } |
| 514 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 515 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 516 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 517 | // setup vertex / glyphIndex for the new run |
| 518 | if (run > 0) { |
| 519 | PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back(); |
| 520 | PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back(); |
| 521 | |
| 522 | newRun.fVertexStartIndex = lastRun.fVertexEndIndex; |
| 523 | newRun.fVertexEndIndex = lastRun.fVertexEndIndex; |
| 524 | |
| 525 | newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex; |
| 526 | newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex; |
| 527 | } |
| 528 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 529 | if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) { |
| 530 | cacheBlob->setHasDistanceField(); |
| 531 | SkPaint dfPaint = runPaint; |
| 532 | SkScalar textRatio; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 533 | this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 534 | Run& runIdx = cacheBlob->fRuns[run]; |
| 535 | PerSubRunInfo& subRun = runIdx.fSubRunInfo.back(); |
| 536 | subRun.fUseLCDText = runPaint.isLCDRenderText(); |
| 537 | subRun.fDrawAsDistanceFields = true; |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 538 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 539 | SkTDArray<char> fallbackTxt; |
| 540 | SkTDArray<SkScalar> fallbackPos; |
| 541 | SkPoint dfOffset; |
| 542 | int scalarsPerPosition = 2; |
| 543 | switch (it.positioning()) { |
| 544 | case SkTextBlob::kDefault_Positioning: { |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 545 | this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 546 | (const char *)it.glyphs(), textLen, |
| 547 | x + offset.x(), y + offset.y(), clipRect, textRatio, |
| 548 | &fallbackTxt, &fallbackPos, &dfOffset, runPaint); |
| 549 | break; |
| 550 | } |
| 551 | case SkTextBlob::kHorizontal_Positioning: { |
| 552 | scalarsPerPosition = 1; |
| 553 | dfOffset = SkPoint::Make(x, y + offset.y()); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 554 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 555 | (const char*)it.glyphs(), textLen, it.pos(), |
| 556 | scalarsPerPosition, dfOffset, clipRect, textRatio, |
| 557 | &fallbackTxt, &fallbackPos); |
| 558 | break; |
| 559 | } |
| 560 | case SkTextBlob::kFull_Positioning: { |
| 561 | dfOffset = SkPoint::Make(x, y); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 562 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 563 | (const char*)it.glyphs(), textLen, it.pos(), |
| 564 | scalarsPerPosition, dfOffset, clipRect, textRatio, |
| 565 | &fallbackTxt, &fallbackPos); |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | if (fallbackTxt.count()) { |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 570 | this->fallbackDrawPosText(cacheBlob, run, rt, clip, color, runPaint, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 571 | fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset, |
| 572 | clipRect); |
| 573 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 574 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
| 575 | cacheBlob->fRuns[run].fDrawAsPaths = true; |
| 576 | } else { |
| 577 | cacheBlob->setHasBitmap(); |
| 578 | SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix, |
| 579 | false); |
| 580 | switch (it.positioning()) { |
| 581 | case SkTextBlob::kDefault_Positioning: |
| 582 | this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 583 | (const char *)it.glyphs(), textLen, |
| 584 | x + offset.x(), y + offset.y(), clipRect); |
| 585 | break; |
| 586 | case SkTextBlob::kHorizontal_Positioning: |
| 587 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 588 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
| 589 | SkPoint::Make(x, y + offset.y()), clipRect); |
| 590 | break; |
| 591 | case SkTextBlob::kFull_Positioning: |
| 592 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 593 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
| 594 | SkPoint::Make(x, y), clipRect); |
| 595 | break; |
| 596 | } |
| 597 | SkGlyphCache::AttachCache(cache); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (drawFilter) { |
| 601 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 602 | runPaint = skPaint; |
| 603 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 607 | inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 608 | SkPaint* skPaint, |
| 609 | SkScalar* textRatio, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 610 | const SkMatrix& viewMatrix) { |
| 611 | // getMaxScale doesn't support perspective, so neither do we at the moment |
| 612 | SkASSERT(!viewMatrix.hasPerspective()); |
| 613 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 614 | SkScalar textSize = skPaint->getTextSize(); |
| 615 | SkScalar scaledTextSize = textSize; |
| 616 | // if we have non-unity scale, we need to choose our base text size |
| 617 | // based on the SkPaint's text size multiplied by the max scale factor |
| 618 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 619 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 620 | scaledTextSize *= maxScale; |
| 621 | } |
| 622 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 623 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 624 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 625 | SkScalar dfMaskScaleFloor; |
| 626 | SkScalar dfMaskScaleCeil; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 627 | if (scaledTextSize <= kSmallDFFontLimit) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 628 | dfMaskScaleFloor = kMinDFFontSize; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 629 | dfMaskScaleCeil = kSmallDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 630 | *textRatio = textSize / kSmallDFFontSize; |
| 631 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 632 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 633 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 634 | dfMaskScaleCeil = kMediumDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 635 | *textRatio = textSize / kMediumDFFontSize; |
| 636 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 637 | } else { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 638 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 639 | dfMaskScaleCeil = kLargeDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 640 | *textRatio = textSize / kLargeDFFontSize; |
| 641 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 642 | } |
| 643 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 644 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 645 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 646 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 647 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 648 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 649 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 650 | // level) |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 651 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 652 | blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale); |
| 653 | blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale); |
| 654 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 655 | skPaint->setLCDRenderText(false); |
| 656 | skPaint->setAutohinted(false); |
| 657 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 658 | skPaint->setSubpixelText(true); |
| 659 | } |
| 660 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 661 | inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 662 | int runIndex, |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 663 | GrRenderTarget* rt, const GrClip& clip, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 664 | GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 665 | const SkPaint& skPaint, |
| 666 | const SkMatrix& viewMatrix, |
| 667 | const SkTDArray<char>& fallbackTxt, |
| 668 | const SkTDArray<SkScalar>& fallbackPos, |
| 669 | int scalarsPerPosition, |
| 670 | const SkPoint& offset, |
| 671 | const SkIRect& clipRect) { |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 672 | SkASSERT(fallbackTxt.count()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 673 | blob->setHasBitmap(); |
| 674 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 675 | // Push back a new subrun to fill and set the override descriptor |
| 676 | run.push_back(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 677 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 678 | skPaint.getScalerContextDescriptor(run.fOverrideDescriptor, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 679 | fSurfaceProps, &viewMatrix, false); |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 680 | SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface, |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 681 | run.fOverrideDescriptor->getDesc()); |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 682 | this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 683 | fallbackTxt.begin(), fallbackTxt.count(), |
| 684 | fallbackPos.begin(), scalarsPerPosition, offset, clipRect); |
| 685 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 686 | } |
| 687 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 688 | inline GrAtlasTextBlob* |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 689 | GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 690 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 691 | SkScalar* textRatio) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 692 | GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 693 | |
| 694 | *dfPaint = origPaint; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 695 | this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 696 | blob->fViewMatrix = viewMatrix; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 697 | Run& run = blob->fRuns[0]; |
| 698 | PerSubRunInfo& subRun = run.fSubRunInfo.back(); |
| 699 | subRun.fUseLCDText = origPaint.isLCDRenderText(); |
| 700 | subRun.fDrawAsDistanceFields = true; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 701 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 702 | return blob; |
| 703 | } |
| 704 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 705 | inline GrAtlasTextBlob* |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 706 | GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip, |
| 707 | const GrPaint& paint, const SkPaint& skPaint, |
| 708 | const SkMatrix& viewMatrix, |
| 709 | const char text[], size_t byteLength, |
| 710 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 711 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 712 | SkIRect clipRect; |
| 713 | clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 714 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 715 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 716 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 717 | SkPaint dfPaint; |
| 718 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 719 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 720 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 721 | SkTDArray<char> fallbackTxt; |
| 722 | SkTDArray<SkScalar> fallbackPos; |
| 723 | SkPoint offset; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 724 | this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 725 | byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos, |
| 726 | &offset, skPaint); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 727 | if (fallbackTxt.count()) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 728 | this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 729 | fallbackTxt, fallbackPos, 2, offset, clipRect); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 730 | } |
| 731 | } else { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 732 | blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 733 | blob->fViewMatrix = viewMatrix; |
| 734 | |
| 735 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 736 | this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
| 737 | byteLength, x, y, clipRect); |
| 738 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 739 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 740 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 741 | } |
| 742 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 743 | inline GrAtlasTextBlob* |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 744 | GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip, |
| 745 | const GrPaint& paint, const SkPaint& skPaint, |
| 746 | const SkMatrix& viewMatrix, |
| 747 | const char text[], size_t byteLength, |
| 748 | const SkScalar pos[], int scalarsPerPosition, |
| 749 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 750 | int glyphCount = skPaint.countText(text, byteLength); |
| 751 | |
| 752 | SkIRect clipRect; |
| 753 | clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 754 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 755 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 756 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 757 | SkPaint dfPaint; |
| 758 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 759 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 760 | |
| 761 | SkTDArray<char> fallbackTxt; |
| 762 | SkTDArray<SkScalar> fallbackPos; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 763 | this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 764 | byteLength, pos, scalarsPerPosition, offset, clipRect, |
| 765 | textRatio, &fallbackTxt, &fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 766 | if (fallbackTxt.count()) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 767 | this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix, |
| 768 | fallbackTxt, fallbackPos, scalarsPerPosition, offset, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 769 | clipRect); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 770 | } |
| 771 | } else { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 772 | blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 773 | blob->fViewMatrix = viewMatrix; |
| 774 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 775 | this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
| 776 | byteLength, pos, scalarsPerPosition, offset, clipRect); |
| 777 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 778 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 779 | return blob; |
| 780 | } |
| 781 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 782 | void GrAtlasTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 783 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 784 | const GrPaint& paint, const SkPaint& skPaint, |
| 785 | const SkMatrix& viewMatrix, |
| 786 | const char text[], size_t byteLength, |
| 787 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 788 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 789 | this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix, |
| 790 | text, byteLength, x, y, regionClipBounds)); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 791 | this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 792 | } |
| 793 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 794 | void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 795 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 796 | const GrPaint& paint, const SkPaint& skPaint, |
| 797 | const SkMatrix& viewMatrix, |
| 798 | const char text[], size_t byteLength, |
| 799 | const SkScalar pos[], int scalarsPerPosition, |
| 800 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 801 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 802 | this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix, |
| 803 | text, byteLength, |
| 804 | pos, scalarsPerPosition, |
| 805 | offset, regionClipBounds)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 806 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 807 | this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 808 | } |
| 809 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 810 | void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 811 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 812 | GrColor color, |
| 813 | const SkMatrix& viewMatrix, |
| 814 | const char text[], size_t byteLength, |
| 815 | SkScalar x, SkScalar y, const SkIRect& clipRect) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 816 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 817 | |
| 818 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 819 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 823 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 824 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
| 825 | |
| 826 | // Get GrFontScaler from cache |
| 827 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 828 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 829 | SkFindAndPlaceGlyph::ProcessText( |
| 830 | text, byteLength, {x, y}, viewMatrix, skPaint.getTextAlign(), glyphCacheProc, cache, |
| 831 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 832 | position += rounding; |
| 833 | this->bmpAppendGlyph( |
| 834 | blob, runIndex, glyph, |
| 835 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
| 836 | color, fontScaler, clipRect); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 837 | } |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 838 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 839 | } |
| 840 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 841 | void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 842 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 843 | GrColor color, |
| 844 | const SkMatrix& viewMatrix, |
| 845 | const char text[], size_t byteLength, |
| 846 | const SkScalar pos[], int scalarsPerPosition, |
| 847 | const SkPoint& offset, const SkIRect& clipRect) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 848 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 849 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 850 | |
| 851 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 852 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 856 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 857 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
| 858 | |
| 859 | // Get GrFontScaler from cache |
| 860 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 861 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 862 | SkFindAndPlaceGlyph::ProcessPosText( |
| 863 | text, byteLength, offset, viewMatrix, pos, scalarsPerPosition, |
| 864 | skPaint.getTextAlign(), glyphCacheProc, cache, |
| 865 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 866 | position += rounding; |
| 867 | this->bmpAppendGlyph( |
| 868 | blob, runIndex, glyph, |
| 869 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
| 870 | color, fontScaler, clipRect); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 871 | } |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 872 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 873 | } |
| 874 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 875 | void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 876 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 877 | const SkMatrix& viewMatrix, |
| 878 | const char text[], size_t byteLength, |
| 879 | SkScalar x, SkScalar y, const SkIRect& clipRect, |
| 880 | SkScalar textRatio, |
| 881 | SkTDArray<char>* fallbackTxt, |
| 882 | SkTDArray<SkScalar>* fallbackPos, |
| 883 | SkPoint* offset, |
| 884 | const SkPaint& origPaint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 885 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 886 | |
| 887 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 888 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
| 892 | SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc(); |
| 893 | SkAutoDescriptor desc; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 894 | origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true); |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 895 | SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(), |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 896 | desc.getDesc()); |
| 897 | |
| 898 | SkTArray<SkScalar> positions; |
| 899 | |
| 900 | const char* textPtr = text; |
| 901 | SkFixed stopX = 0; |
| 902 | SkFixed stopY = 0; |
| 903 | SkFixed origin = 0; |
| 904 | switch (origPaint.getTextAlign()) { |
| 905 | case SkPaint::kRight_Align: origin = SK_Fixed1; break; |
| 906 | case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; |
| 907 | case SkPaint::kLeft_Align: origin = 0; break; |
| 908 | } |
| 909 | |
| 910 | SkAutoKern autokern; |
| 911 | const char* stop = text + byteLength; |
| 912 | while (textPtr < stop) { |
| 913 | // don't need x, y here, since all subpixel variants will have the |
| 914 | // same advance |
| 915 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0); |
| 916 | |
| 917 | SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); |
| 918 | positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width))); |
| 919 | |
| 920 | SkFixed height = glyph.fAdvanceY; |
| 921 | positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height))); |
| 922 | |
| 923 | stopX += width; |
| 924 | stopY += height; |
| 925 | } |
| 926 | SkASSERT(textPtr == stop); |
| 927 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 928 | SkGlyphCache::AttachCache(origPaintCache); |
| 929 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 930 | // now adjust starting point depending on alignment |
| 931 | SkScalar alignX = SkFixedToScalar(stopX); |
| 932 | SkScalar alignY = SkFixedToScalar(stopY); |
| 933 | if (origPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 934 | alignX = SkScalarHalf(alignX); |
| 935 | alignY = SkScalarHalf(alignY); |
| 936 | } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 937 | alignX = 0; |
| 938 | alignY = 0; |
| 939 | } |
| 940 | x -= alignX; |
| 941 | y -= alignY; |
| 942 | *offset = SkPoint::Make(x, y); |
| 943 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 944 | this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 945 | positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt, |
| 946 | fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 947 | } |
| 948 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 949 | void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 950 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 951 | const SkMatrix& viewMatrix, |
| 952 | const char text[], size_t byteLength, |
| 953 | const SkScalar pos[], int scalarsPerPosition, |
| 954 | const SkPoint& offset, const SkIRect& clipRect, |
| 955 | SkScalar textRatio, |
| 956 | SkTDArray<char>* fallbackTxt, |
| 957 | SkTDArray<SkScalar>* fallbackPos) { |
| 958 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 959 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 960 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 961 | |
| 962 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 963 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 964 | return; |
| 965 | } |
| 966 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 967 | fCurrStrike = nullptr; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 968 | |
| 969 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 970 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 971 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 972 | |
| 973 | const char* stop = text + byteLength; |
| 974 | |
| 975 | if (SkPaint::kLeft_Align == skPaint.getTextAlign()) { |
| 976 | while (text < stop) { |
| 977 | const char* lastText = text; |
| 978 | // the last 2 parameters are ignored |
| 979 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 980 | |
| 981 | if (glyph.fWidth) { |
| 982 | SkScalar x = offset.x() + pos[0]; |
| 983 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 984 | |
| 985 | if (!this->dfAppendGlyph(blob, |
| 986 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 987 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 988 | x, y, color, fontScaler, clipRect, |
| 989 | textRatio, viewMatrix)) { |
| 990 | // couldn't append, send to fallback |
| 991 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 992 | *fallbackPos->append() = pos[0]; |
| 993 | if (2 == scalarsPerPosition) { |
| 994 | *fallbackPos->append() = pos[1]; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | pos += scalarsPerPosition; |
| 999 | } |
| 1000 | } else { |
| 1001 | SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf |
| 1002 | : SK_Scalar1; |
| 1003 | while (text < stop) { |
| 1004 | const char* lastText = text; |
| 1005 | // the last 2 parameters are ignored |
| 1006 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 1007 | |
| 1008 | if (glyph.fWidth) { |
| 1009 | SkScalar x = offset.x() + pos[0]; |
| 1010 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 1011 | |
| 1012 | SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 1013 | SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 1014 | |
| 1015 | if (!this->dfAppendGlyph(blob, |
| 1016 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1017 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1018 | x - advanceX, y - advanceY, color, |
| 1019 | fontScaler, |
| 1020 | clipRect, |
| 1021 | textRatio, |
| 1022 | viewMatrix)) { |
| 1023 | // couldn't append, send to fallback |
| 1024 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 1025 | *fallbackPos->append() = pos[0]; |
| 1026 | if (2 == scalarsPerPosition) { |
| 1027 | *fallbackPos->append() = pos[1]; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | pos += scalarsPerPosition; |
| 1032 | } |
| 1033 | } |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 1034 | |
| 1035 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1038 | void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1039 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1040 | int vx, int vy, GrColor color, GrFontScaler* scaler, |
| 1041 | const SkIRect& clipRect) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1042 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1043 | if (!fCurrStrike) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1044 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 1045 | } |
| 1046 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1047 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 1048 | skGlyph.getSubXFixed(), |
| 1049 | skGlyph.getSubYFixed(), |
| 1050 | GrGlyph::kCoverage_MaskStyle); |
| 1051 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1052 | if (!glyph) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | int x = vx + glyph->fBounds.fLeft; |
| 1057 | int y = vy + glyph->fBounds.fTop; |
| 1058 | |
| 1059 | // keep them as ints until we've done the clip-test |
| 1060 | int width = glyph->fBounds.width(); |
| 1061 | int height = glyph->fBounds.height(); |
| 1062 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1063 | #if 0 |
| 1064 | // Not checking the clip bounds might introduce a performance regression. However, its not |
| 1065 | // clear if this is still true today with the larger tiles we use in Chrome. For repositionable |
| 1066 | // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal. |
| 1067 | // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls |
| 1068 | // TODO verify this |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1069 | // check if we clipped out |
| 1070 | if (clipRect.quickReject(x, y, x + width, y + height)) { |
| 1071 | return; |
| 1072 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1073 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1074 | |
| 1075 | // If the glyph is too large we fall back to paths |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1076 | if (glyph->fTooLargeForAtlas) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1077 | this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1078 | return; |
| 1079 | } |
| 1080 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1081 | GrMaskFormat format = glyph->fMaskFormat; |
| 1082 | |
| 1083 | PerSubRunInfo* subRun = &run.fSubRunInfo.back(); |
| 1084 | if (run.fInitialized && subRun->fMaskFormat != format) { |
joshualitt | d9f13ae | 2015-07-24 11:24:31 -0700 | [diff] [blame] | 1085 | subRun = &run.push_back(); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1086 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
| 1087 | } else if (!run.fInitialized) { |
| 1088 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | run.fInitialized = true; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1092 | |
| 1093 | size_t vertexStride = get_vertex_stride(format); |
| 1094 | |
| 1095 | SkRect r; |
| 1096 | r.fLeft = SkIntToScalar(x); |
| 1097 | r.fTop = SkIntToScalar(y); |
| 1098 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 1099 | r.fBottom = r.fTop + SkIntToScalar(height); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1100 | subRun->fMaskFormat = format; |
| 1101 | this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1102 | glyph); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1103 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1104 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1105 | bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1106 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1107 | SkScalar sx, SkScalar sy, GrColor color, |
| 1108 | GrFontScaler* scaler, |
| 1109 | const SkIRect& clipRect, |
| 1110 | SkScalar textRatio, const SkMatrix& viewMatrix) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1111 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1112 | if (!fCurrStrike) { |
| 1113 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 1114 | } |
| 1115 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1116 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 1117 | skGlyph.getSubXFixed(), |
| 1118 | skGlyph.getSubYFixed(), |
| 1119 | GrGlyph::kDistance_MaskStyle); |
| 1120 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1121 | if (!glyph) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | // fallback to color glyph support |
| 1126 | if (kA8_GrMaskFormat != glyph->fMaskFormat) { |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
| 1130 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 1131 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 1132 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 1133 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 1134 | |
| 1135 | SkScalar scale = textRatio; |
| 1136 | dx *= scale; |
| 1137 | dy *= scale; |
| 1138 | width *= scale; |
| 1139 | height *= scale; |
| 1140 | sx += dx; |
| 1141 | sy += dy; |
| 1142 | SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height); |
| 1143 | |
| 1144 | #if 0 |
| 1145 | // check if we clipped out |
| 1146 | SkRect dstRect; |
| 1147 | viewMatrix.mapRect(&dstRect, glyphRect); |
| 1148 | if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()), |
| 1149 | SkScalarTruncToInt(dstRect.top()), |
| 1150 | SkScalarTruncToInt(dstRect.right()), |
| 1151 | SkScalarTruncToInt(dstRect.bottom()))) { |
| 1152 | return true; |
| 1153 | } |
| 1154 | #endif |
| 1155 | |
| 1156 | // TODO combine with the above |
| 1157 | // If the glyph is too large we fall back to paths |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1158 | if (glyph->fTooLargeForAtlas) { |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1159 | this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1160 | return true; |
| 1161 | } |
| 1162 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1163 | PerSubRunInfo* subRun = &run.fSubRunInfo.back(); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1164 | if (!run.fInitialized) { |
| 1165 | subRun->fStrike.reset(SkRef(fCurrStrike)); |
| 1166 | } |
| 1167 | run.fInitialized = true; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1168 | SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat); |
| 1169 | subRun->fMaskFormat = kA8_GrMaskFormat; |
| 1170 | |
| 1171 | size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText); |
| 1172 | |
| 1173 | bool useColorVerts = !subRun->fUseLCDText; |
| 1174 | this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1175 | glyph); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1176 | return true; |
| 1177 | } |
| 1178 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1179 | inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1180 | GrFontScaler* scaler, const SkGlyph& skGlyph, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1181 | SkScalar x, SkScalar y, SkScalar scale, |
| 1182 | bool applyVM) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1183 | if (nullptr == glyph->fPath) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1184 | const SkPath* glyphPath = scaler->getGlyphPath(skGlyph); |
| 1185 | if (!glyphPath) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1186 | return; |
| 1187 | } |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1188 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1189 | glyph->fPath = new SkPath(*glyphPath); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1190 | } |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1191 | blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM)); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1194 | inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1195 | Run::SubRunInfo* subRun, |
| 1196 | const SkRect& positions, GrColor color, |
| 1197 | size_t vertexStride, bool useVertexColor, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1198 | GrGlyph* glyph) { |
| 1199 | blob->fGlyphs[subRun->fGlyphEndIndex] = glyph; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1200 | run->fVertexBounds.joinNonEmptyArg(positions); |
| 1201 | run->fColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1202 | |
| 1203 | intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex); |
| 1204 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1205 | if (useVertexColor) { |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1206 | // V0 |
| 1207 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 1208 | position->set(positions.fLeft, positions.fTop); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1209 | SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 1210 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1211 | vertex += vertexStride; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1212 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1213 | // V1 |
| 1214 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1215 | position->set(positions.fLeft, positions.fBottom); |
| 1216 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1217 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1218 | vertex += vertexStride; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1219 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1220 | // V2 |
| 1221 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1222 | position->set(positions.fRight, positions.fBottom); |
| 1223 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1224 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1225 | vertex += vertexStride; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1226 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1227 | // V3 |
| 1228 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1229 | position->set(positions.fRight, positions.fTop); |
| 1230 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1231 | *colorPtr = color; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 1232 | } else { |
| 1233 | // V0 |
| 1234 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 1235 | position->set(positions.fLeft, positions.fTop); |
| 1236 | vertex += vertexStride; |
| 1237 | |
| 1238 | // V1 |
| 1239 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1240 | position->set(positions.fLeft, positions.fBottom); |
| 1241 | vertex += vertexStride; |
| 1242 | |
| 1243 | // V2 |
| 1244 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1245 | position->set(positions.fRight, positions.fBottom); |
| 1246 | vertex += vertexStride; |
| 1247 | |
| 1248 | // V3 |
| 1249 | position = reinterpret_cast<SkPoint*>(vertex); |
| 1250 | position->set(positions.fRight, positions.fTop); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | subRun->fGlyphEndIndex++; |
| 1254 | subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph; |
| 1255 | } |
| 1256 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1257 | class TextBatch : public GrVertexBatch { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1258 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1259 | DEFINE_BATCH_CLASS_ID |
| 1260 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1261 | typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1262 | typedef GrAtlasTextBlob Blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1263 | typedef Blob::Run Run; |
| 1264 | typedef Run::SubRunInfo TextInfo; |
| 1265 | struct Geometry { |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1266 | Blob* fBlob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1267 | int fRun; |
| 1268 | int fSubRun; |
| 1269 | GrColor fColor; |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1270 | SkScalar fTransX; |
| 1271 | SkScalar fTransY; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1272 | }; |
| 1273 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1274 | static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount, |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1275 | GrBatchFontCache* fontCache) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1276 | TextBatch* batch = new TextBatch; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1277 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1278 | batch->fFontCache = fontCache; |
| 1279 | switch (maskFormat) { |
| 1280 | case kA8_GrMaskFormat: |
| 1281 | batch->fMaskType = kGrayscaleCoverageMask_MaskType; |
| 1282 | break; |
| 1283 | case kA565_GrMaskFormat: |
| 1284 | batch->fMaskType = kLCDCoverageMask_MaskType; |
| 1285 | break; |
| 1286 | case kARGB_GrMaskFormat: |
| 1287 | batch->fMaskType = kColorBitmapMask_MaskType; |
| 1288 | break; |
| 1289 | } |
| 1290 | batch->fBatch.fNumGlyphs = glyphCount; |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1291 | batch->fGeoCount = 1; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1292 | batch->fFilteredColor = 0; |
| 1293 | batch->fFontCache = fontCache; |
| 1294 | batch->fUseBGR = false; |
| 1295 | return batch; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1298 | static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1299 | const DistanceAdjustTable* distanceAdjustTable, |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1300 | SkColor filteredColor, bool isLCD, |
| 1301 | bool useBGR) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1302 | TextBatch* batch = new TextBatch; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1303 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1304 | batch->fFontCache = fontCache; |
| 1305 | batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType; |
| 1306 | batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable)); |
| 1307 | batch->fFilteredColor = filteredColor; |
| 1308 | batch->fUseBGR = useBGR; |
| 1309 | batch->fBatch.fNumGlyphs = glyphCount; |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1310 | batch->fGeoCount = 1; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1311 | return batch; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1314 | // to avoid even the initial copy of the struct, we have a getter for the first item which |
| 1315 | // is used to seed the batch with its initial geometry. After seeding, the client should call |
| 1316 | // init() so the Batch can initialize itself |
| 1317 | Geometry& geometry() { return fGeoData[0]; } |
| 1318 | |
| 1319 | void init() { |
| 1320 | const Geometry& geo = fGeoData[0]; |
| 1321 | fBatch.fColor = geo.fColor; |
| 1322 | fBatch.fViewMatrix = geo.fBlob->fViewMatrix; |
| 1323 | |
| 1324 | // We don't yet position distance field text on the cpu, so we have to map the vertex bounds |
| 1325 | // into device space |
| 1326 | const Run& run = geo.fBlob->fRuns[geo.fRun]; |
| 1327 | if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) { |
| 1328 | SkRect bounds = run.fVertexBounds; |
| 1329 | fBatch.fViewMatrix.mapRect(&bounds); |
| 1330 | this->setBounds(bounds); |
| 1331 | } else { |
| 1332 | this->setBounds(run.fVertexBounds); |
| 1333 | } |
| 1334 | } |
| 1335 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1336 | const char* name() const override { return "TextBatch"; } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1337 | |
| 1338 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1339 | if (kColorBitmapMask_MaskType == fMaskType) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1340 | out->setUnknownFourComponents(); |
| 1341 | } else { |
| 1342 | out->setKnownFourComponents(fBatch.fColor); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1347 | switch (fMaskType) { |
| 1348 | case kGrayscaleDistanceField_MaskType: |
| 1349 | case kGrayscaleCoverageMask_MaskType: |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1350 | out->setUnknownSingleComponent(); |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1351 | break; |
| 1352 | case kLCDCoverageMask_MaskType: |
| 1353 | case kLCDDistanceField_MaskType: |
| 1354 | out->setUnknownOpaqueFourComponents(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1355 | out->setUsingLCDCoverage(); |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1356 | break; |
| 1357 | case kColorBitmapMask_MaskType: |
| 1358 | out->setKnownSingleComponent(0xff); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 1362 | private: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1363 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1364 | // Handle any color overrides |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1365 | if (!opt.readsColor()) { |
joshualitt | 416e14f | 2015-07-10 09:05:57 -0700 | [diff] [blame] | 1366 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1367 | } |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1368 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1369 | |
| 1370 | // setup batch properties |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1371 | fBatch.fColorIgnored = !opt.readsColor(); |
joshualitt | 416e14f | 2015-07-10 09:05:57 -0700 | [diff] [blame] | 1372 | fBatch.fColor = fGeoData[0].fColor; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 1373 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 1374 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1377 | struct FlushInfo { |
| 1378 | SkAutoTUnref<const GrVertexBuffer> fVertexBuffer; |
| 1379 | SkAutoTUnref<const GrIndexBuffer> fIndexBuffer; |
| 1380 | int fGlyphsToFlush; |
| 1381 | int fVertexOffset; |
| 1382 | }; |
| 1383 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1384 | void onPrepareDraws(Target* target) override { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1385 | // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix. |
| 1386 | // TODO actually only invert if we don't have RGBA |
| 1387 | SkMatrix localMatrix; |
| 1388 | if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) { |
| 1389 | SkDebugf("Cannot invert viewmatrix\n"); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1393 | GrTexture* texture = fFontCache->getTexture(this->maskFormat()); |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 1394 | if (!texture) { |
| 1395 | SkDebugf("Could not allocate backing texture for atlas\n"); |
| 1396 | return; |
| 1397 | } |
| 1398 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1399 | bool usesDistanceFields = this->usesDistanceFields(); |
| 1400 | GrMaskFormat maskFormat = this->maskFormat(); |
| 1401 | bool isLCD = this->isLCD(); |
| 1402 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1403 | SkAutoTUnref<const GrGeometryProcessor> gp; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1404 | if (usesDistanceFields) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1405 | gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(), |
| 1406 | texture)); |
| 1407 | } else { |
| 1408 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1409 | gp.reset(GrBitmapTextGeoProc::Create(this->color(), |
| 1410 | texture, |
| 1411 | params, |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1412 | maskFormat, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1413 | localMatrix, |
| 1414 | this->usesLocalCoords())); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1415 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1416 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1417 | FlushInfo flushInfo; |
| 1418 | flushInfo.fGlyphsToFlush = 0; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1419 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1420 | SkASSERT(vertexStride == (usesDistanceFields ? |
| 1421 | get_vertex_stride_df(maskFormat, isLCD) : |
| 1422 | get_vertex_stride(maskFormat))); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1423 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1424 | target->initDraw(gp, this->pipeline()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1425 | |
| 1426 | int glyphCount = this->numGlyphs(); |
bsalomon | 8415abe | 2015-05-04 11:41:41 -0700 | [diff] [blame] | 1427 | const GrVertexBuffer* vertexBuffer; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1428 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1429 | void* vertices = target->makeVertexSpace(vertexStride, |
| 1430 | glyphCount * kVerticesPerGlyph, |
| 1431 | &vertexBuffer, |
| 1432 | &flushInfo.fVertexOffset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1433 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1434 | flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1435 | if (!vertices || !flushInfo.fVertexBuffer) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1436 | SkDebugf("Could not allocate vertices\n"); |
| 1437 | return; |
| 1438 | } |
| 1439 | |
| 1440 | unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices); |
| 1441 | |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1442 | // We cache some values to avoid going to the glyphcache for the same fontScaler twice |
| 1443 | // in a row |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1444 | const SkDescriptor* desc = nullptr; |
| 1445 | SkGlyphCache* cache = nullptr; |
| 1446 | GrFontScaler* scaler = nullptr; |
| 1447 | SkTypeface* typeface = nullptr; |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1448 | |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1449 | for (int i = 0; i < fGeoCount; i++) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1450 | Geometry& args = fGeoData[i]; |
| 1451 | Blob* blob = args.fBlob; |
| 1452 | Run& run = blob->fRuns[args.fRun]; |
| 1453 | TextInfo& info = run.fSubRunInfo[args.fSubRun]; |
| 1454 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1455 | uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 1456 | bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen || |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1457 | info.fStrike->isAbandoned(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1458 | bool regenerateColors; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1459 | if (usesDistanceFields) { |
| 1460 | regenerateColors = !isLCD && run.fColor != args.fColor; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1461 | } else { |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1462 | regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1463 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1464 | bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1465 | int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex; |
| 1466 | |
| 1467 | // We regenerate both texture coords and colors in the blob itself, and update the |
| 1468 | // atlas generation. If we don't end up purging any unused plots, we can avoid |
| 1469 | // regenerating the coords. We could take a finer grained approach to updating texture |
| 1470 | // coords but its not clear if the extra bookkeeping would offset any gains. |
| 1471 | // To avoid looping over the glyphs twice, we do one loop and conditionally update color |
| 1472 | // or coords as needed. One final note, if we have to break a run for an atlas eviction |
| 1473 | // then we can't really trust the atlas has all of the correct data. Atlas evictions |
| 1474 | // should be pretty rare, so we just always regenerate in those cases |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1475 | if (regenerateTextureCoords || regenerateColors || regeneratePositions) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1476 | // first regenerate texture coordinates / colors if need be |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1477 | bool brokenRun = false; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1478 | |
| 1479 | // Because the GrBatchFontCache may evict the strike a blob depends on using for |
| 1480 | // generating its texture coords, we have to track whether or not the strike has |
| 1481 | // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is |
| 1482 | // otherwise we have to get the new strike, and use that to get the correct glyphs. |
| 1483 | // Because we do not have the packed ids, and thus can't look up our glyphs in the |
| 1484 | // new strike, we instead keep our ref to the old strike and use the packed ids from |
| 1485 | // it. These ids will still be valid as long as we hold the ref. When we are done |
| 1486 | // updating our cache of the GrGlyph*s, we drop our ref on the old strike |
| 1487 | bool regenerateGlyphs = false; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1488 | GrBatchTextStrike* strike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1489 | if (regenerateTextureCoords) { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 1490 | info.fBulkUseToken.reset(); |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1491 | |
| 1492 | // We can reuse if we have a valid strike and our descriptors / typeface are the |
joshualitt | 9446930 | 2015-09-02 06:13:39 -0700 | [diff] [blame] | 1493 | // same. The override descriptor is only for the non distance field text within |
| 1494 | // a run |
| 1495 | const SkDescriptor* newDesc = (run.fOverrideDescriptor && !usesDistanceFields) ? |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 1496 | run.fOverrideDescriptor->getDesc() : |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1497 | run.fDescriptor.getDesc(); |
| 1498 | if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) || |
| 1499 | !(desc->equals(*newDesc))) { |
| 1500 | if (cache) { |
| 1501 | SkGlyphCache::AttachCache(cache); |
| 1502 | } |
| 1503 | desc = newDesc; |
| 1504 | cache = SkGlyphCache::DetachCache(run.fTypeface, desc); |
| 1505 | scaler = GrTextContext::GetGrFontScaler(cache); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1506 | strike = info.fStrike; |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1507 | typeface = run.fTypeface; |
| 1508 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1509 | |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1510 | if (info.fStrike->isAbandoned()) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1511 | regenerateGlyphs = true; |
| 1512 | strike = fFontCache->getStrike(scaler); |
| 1513 | } else { |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1514 | strike = info.fStrike; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1519 | if (regenerateTextureCoords) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1520 | size_t glyphOffset = glyphIdx + info.fGlyphStartIndex; |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1521 | |
| 1522 | GrGlyph* glyph = blob->fGlyphs[glyphOffset]; |
| 1523 | GrGlyph::PackedID id = glyph->fPackedID; |
| 1524 | const SkGlyph& skGlyph = scaler->grToSkGlyph(id); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1525 | if (regenerateGlyphs) { |
| 1526 | // Get the id from the old glyph, and use the new strike to lookup |
| 1527 | // the glyph. |
joshualitt | 76cc657 | 2015-07-31 05:51:45 -0700 | [diff] [blame] | 1528 | blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat, |
| 1529 | scaler); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1530 | } |
| 1531 | glyph = blob->fGlyphs[glyphOffset]; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1532 | SkASSERT(glyph); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 1533 | SkASSERT(id == glyph->fPackedID); |
| 1534 | // We want to be able to assert this but cannot for testing purposes. |
| 1535 | // once skbug:4143 has landed we can revist this assert |
| 1536 | //SkASSERT(glyph->fMaskFormat == this->maskFormat()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1537 | |
| 1538 | if (!fFontCache->hasGlyph(glyph) && |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1539 | !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) { |
| 1540 | this->flush(target, &flushInfo); |
| 1541 | target->initDraw(gp, this->pipeline()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1542 | brokenRun = glyphIdx > 0; |
| 1543 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1544 | SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target, |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1545 | glyph, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 1546 | scaler, |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 1547 | skGlyph, |
| 1548 | maskFormat); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1549 | SkASSERT(success); |
| 1550 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 1551 | fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1552 | target->currentToken()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1553 | |
| 1554 | // Texture coords are the last vertex attribute so we get a pointer to the |
| 1555 | // first one and then map with stride in regenerateTextureCoords |
| 1556 | intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices); |
| 1557 | vertex += info.fVertexStartIndex; |
| 1558 | vertex += vertexStride * glyphIdx * kVerticesPerGlyph; |
| 1559 | vertex += vertexStride - sizeof(SkIPoint16); |
| 1560 | |
| 1561 | this->regenerateTextureCoords(glyph, vertex, vertexStride); |
| 1562 | } |
| 1563 | |
| 1564 | if (regenerateColors) { |
| 1565 | intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices); |
| 1566 | vertex += info.fVertexStartIndex; |
| 1567 | vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint); |
| 1568 | this->regenerateColors(vertex, vertexStride, args.fColor); |
| 1569 | } |
| 1570 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1571 | if (regeneratePositions) { |
| 1572 | intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices); |
| 1573 | vertex += info.fVertexStartIndex; |
| 1574 | vertex += vertexStride * glyphIdx * kVerticesPerGlyph; |
| 1575 | SkScalar transX = args.fTransX; |
| 1576 | SkScalar transY = args.fTransY; |
| 1577 | this->regeneratePositions(vertex, vertexStride, transX, transY); |
| 1578 | } |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1579 | flushInfo.fGlyphsToFlush++; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1582 | // We my have changed the color so update it here |
| 1583 | run.fColor = args.fColor; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1584 | if (regenerateTextureCoords) { |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1585 | if (regenerateGlyphs) { |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 1586 | info.fStrike.reset(SkRef(strike)); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 1587 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1588 | info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration : |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1589 | fFontCache->atlasGeneration(maskFormat); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1590 | } |
| 1591 | } else { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1592 | flushInfo.fGlyphsToFlush += glyphCount; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 1593 | |
| 1594 | // set use tokens for all of the glyphs in our subrun. This is only valid if we |
| 1595 | // have a valid atlas generation |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1596 | fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | // now copy all vertices |
| 1600 | size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex; |
| 1601 | memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount); |
| 1602 | |
| 1603 | currVertex += byteCount; |
| 1604 | } |
joshualitt | 25ba7ea | 2015-04-21 07:49:49 -0700 | [diff] [blame] | 1605 | // Make sure to attach the last cache if applicable |
| 1606 | if (cache) { |
| 1607 | SkGlyphCache::AttachCache(cache); |
| 1608 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1609 | this->flush(target, &flushInfo); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1612 | TextBatch() : INHERITED(ClassID()) {} // initialized in factory functions. |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1613 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1614 | ~TextBatch() { |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1615 | for (int i = 0; i < fGeoCount; i++) { |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1616 | fGeoData[i].fBlob->unref(); |
| 1617 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1620 | GrMaskFormat maskFormat() const { |
| 1621 | switch (fMaskType) { |
| 1622 | case kLCDCoverageMask_MaskType: |
| 1623 | return kA565_GrMaskFormat; |
| 1624 | case kColorBitmapMask_MaskType: |
| 1625 | return kARGB_GrMaskFormat; |
| 1626 | case kGrayscaleCoverageMask_MaskType: |
| 1627 | case kGrayscaleDistanceField_MaskType: |
| 1628 | case kLCDDistanceField_MaskType: |
| 1629 | return kA8_GrMaskFormat; |
| 1630 | } |
| 1631 | return kA8_GrMaskFormat; // suppress warning |
| 1632 | } |
| 1633 | |
| 1634 | bool usesDistanceFields() const { |
| 1635 | return kGrayscaleDistanceField_MaskType == fMaskType || |
| 1636 | kLCDDistanceField_MaskType == fMaskType; |
| 1637 | } |
| 1638 | |
| 1639 | bool isLCD() const { |
| 1640 | return kLCDCoverageMask_MaskType == fMaskType || |
| 1641 | kLCDDistanceField_MaskType == fMaskType; |
| 1642 | } |
| 1643 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1644 | void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) { |
| 1645 | int width = glyph->fBounds.width(); |
| 1646 | int height = glyph->fBounds.height(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1647 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1648 | int u0, v0, u1, v1; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1649 | if (this->usesDistanceFields()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1650 | u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset; |
| 1651 | v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset; |
| 1652 | u1 = u0 + width - 2 * SK_DistanceFieldInset; |
| 1653 | v1 = v0 + height - 2 * SK_DistanceFieldInset; |
| 1654 | } else { |
| 1655 | u0 = glyph->fAtlasLocation.fX; |
| 1656 | v0 = glyph->fAtlasLocation.fY; |
| 1657 | u1 = u0 + width; |
| 1658 | v1 = v0 + height; |
| 1659 | } |
| 1660 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1661 | SkIPoint16* textureCoords; |
| 1662 | // V0 |
| 1663 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex); |
| 1664 | textureCoords->set(u0, v0); |
| 1665 | vertex += vertexStride; |
| 1666 | |
| 1667 | // V1 |
| 1668 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex); |
| 1669 | textureCoords->set(u0, v1); |
| 1670 | vertex += vertexStride; |
| 1671 | |
| 1672 | // V2 |
| 1673 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex); |
| 1674 | textureCoords->set(u1, v1); |
| 1675 | vertex += vertexStride; |
| 1676 | |
| 1677 | // V3 |
| 1678 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex); |
| 1679 | textureCoords->set(u1, v0); |
| 1680 | } |
| 1681 | |
| 1682 | void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) { |
| 1683 | for (int i = 0; i < kVerticesPerGlyph; i++) { |
| 1684 | SkColor* vcolor = reinterpret_cast<SkColor*>(vertex); |
| 1685 | *vcolor = color; |
| 1686 | vertex += vertexStride; |
| 1687 | } |
| 1688 | } |
| 1689 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1690 | void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX, |
| 1691 | SkScalar transY) { |
| 1692 | for (int i = 0; i < kVerticesPerGlyph; i++) { |
| 1693 | SkPoint* point = reinterpret_cast<SkPoint*>(vertex); |
| 1694 | point->fX += transX; |
| 1695 | point->fY += transY; |
| 1696 | vertex += vertexStride; |
| 1697 | } |
| 1698 | } |
| 1699 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1700 | void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) { |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 1701 | GrVertices vertices; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1702 | int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads(); |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 1703 | vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1704 | flushInfo->fIndexBuffer, flushInfo->fVertexOffset, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1705 | kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1706 | maxGlyphsPerDraw); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1707 | target->draw(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 1708 | flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush; |
| 1709 | flushInfo->fGlyphsToFlush = 0; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | GrColor color() const { return fBatch.fColor; } |
| 1713 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 1714 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1715 | int numGlyphs() const { return fBatch.fNumGlyphs; } |
| 1716 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 1717 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1718 | TextBatch* that = t->cast<TextBatch>(); |
| 1719 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 1720 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 1721 | return false; |
| 1722 | } |
| 1723 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1724 | if (fMaskType != that->fMaskType) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1725 | return false; |
| 1726 | } |
| 1727 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1728 | if (!this->usesDistanceFields()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1729 | // TODO we can often batch across LCD text if we have dual source blending and don't |
| 1730 | // have to use the blend constant |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1731 | if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1732 | return false; |
| 1733 | } |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1734 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1735 | return false; |
| 1736 | } |
| 1737 | } else { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1738 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1739 | return false; |
| 1740 | } |
| 1741 | |
| 1742 | if (fFilteredColor != that->fFilteredColor) { |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1746 | if (fUseBGR != that->fUseBGR) { |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1750 | // TODO see note above |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1751 | if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) { |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1752 | return false; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1753 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | fBatch.fNumGlyphs += that->numGlyphs(); |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1757 | |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1758 | // Reallocate space for geo data if necessary and then import that's geo data. |
| 1759 | int newGeoCount = that->fGeoCount + fGeoCount; |
| 1760 | // We assume (and here enforce) that the allocation size is the smallest power of two that |
| 1761 | // is greater than or equal to the number of geometries (and at least |
| 1762 | // kMinGeometryAllocated). |
| 1763 | int newAllocSize = GrNextPow2(newGeoCount); |
| 1764 | int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount)); |
| 1765 | |
bsalomon | 16ed6ad | 2015-07-29 06:54:33 -0700 | [diff] [blame] | 1766 | if (newGeoCount > currAllocSize) { |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1767 | fGeoData.realloc(newAllocSize); |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1770 | memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry)); |
bsalomon | 1c63436 | 2015-07-27 07:00:00 -0700 | [diff] [blame] | 1771 | // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that |
| 1772 | // it doesn't try to unref them. |
| 1773 | #ifdef SK_DEBUG |
| 1774 | for (int i = 0; i < that->fGeoCount; ++i) { |
| 1775 | that->fGeoData.get()[i].fBlob = (Blob*)0x1; |
joshualitt | ad802c6 | 2015-04-15 05:31:57 -0700 | [diff] [blame] | 1776 | } |
bsalomon | 1c63436 | 2015-07-27 07:00:00 -0700 | [diff] [blame] | 1777 | #endif |
| 1778 | that->fGeoCount = 0; |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1779 | fGeoCount = newGeoCount; |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 1780 | |
| 1781 | this->joinBounds(that->bounds()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1782 | return true; |
| 1783 | } |
| 1784 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1785 | // TODO just use class params |
| 1786 | // TODO trying to figure out why lcd is so whack |
| 1787 | GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor, |
| 1788 | GrColor color, GrTexture* texture) { |
| 1789 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1790 | bool isLCD = this->isLCD(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1791 | // set up any flags |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1792 | uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1793 | |
| 1794 | // see if we need to create a new effect |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1795 | if (isLCD) { |
| 1796 | flags |= kUseLCD_DistanceFieldEffectFlag; |
| 1797 | flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0; |
| 1798 | flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0; |
| 1799 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1800 | GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor); |
| 1801 | |
| 1802 | float redCorrection = |
| 1803 | (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift]; |
| 1804 | float greenCorrection = |
| 1805 | (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift]; |
| 1806 | float blueCorrection = |
| 1807 | (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift]; |
| 1808 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust = |
| 1809 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection, |
| 1810 | greenCorrection, |
| 1811 | blueCorrection); |
| 1812 | |
| 1813 | return GrDistanceFieldLCDTextGeoProc::Create(color, |
| 1814 | viewMatrix, |
| 1815 | texture, |
| 1816 | params, |
| 1817 | widthAdjust, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1818 | flags, |
| 1819 | this->usesLocalCoords()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1820 | } else { |
| 1821 | flags |= kColorAttr_DistanceFieldEffectFlag; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1822 | #ifdef SK_GAMMA_APPLY_TO_A8 |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 1823 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1824 | float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift]; |
| 1825 | return GrDistanceFieldA8TextGeoProc::Create(color, |
| 1826 | viewMatrix, |
| 1827 | texture, |
| 1828 | params, |
| 1829 | correction, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1830 | flags, |
| 1831 | this->usesLocalCoords()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1832 | #else |
| 1833 | return GrDistanceFieldA8TextGeoProc::Create(color, |
| 1834 | viewMatrix, |
| 1835 | texture, |
| 1836 | params, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 1837 | flags, |
| 1838 | this->usesLocalCoords()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1839 | #endif |
| 1840 | } |
| 1841 | |
| 1842 | } |
| 1843 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1844 | struct BatchTracker { |
| 1845 | GrColor fColor; |
| 1846 | SkMatrix fViewMatrix; |
| 1847 | bool fUsesLocalCoords; |
| 1848 | bool fColorIgnored; |
| 1849 | bool fCoverageIgnored; |
| 1850 | int fNumGlyphs; |
| 1851 | }; |
| 1852 | |
| 1853 | BatchTracker fBatch; |
bsalomon | d602f4d | 2015-07-27 06:12:01 -0700 | [diff] [blame] | 1854 | // The minimum number of Geometry we will try to allocate. |
| 1855 | enum { kMinGeometryAllocated = 4 }; |
| 1856 | SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData; |
| 1857 | int fGeoCount; |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1858 | |
| 1859 | enum MaskType { |
| 1860 | kGrayscaleCoverageMask_MaskType, |
| 1861 | kLCDCoverageMask_MaskType, |
| 1862 | kColorBitmapMask_MaskType, |
| 1863 | kGrayscaleDistanceField_MaskType, |
| 1864 | kLCDDistanceField_MaskType, |
| 1865 | } fMaskType; |
| 1866 | bool fUseBGR; // fold this into the enum? |
| 1867 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1868 | GrBatchFontCache* fFontCache; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1869 | |
| 1870 | // Distance field properties |
robertphillips | 9fc8275 | 2015-06-19 04:46:45 -0700 | [diff] [blame] | 1871 | SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 1872 | SkColor fFilteredColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 1873 | |
| 1874 | typedef GrVertexBatch INHERITED; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1875 | }; |
| 1876 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1877 | void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, GrRenderTarget* rt, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 1878 | const SkTextBlobRunIterator& it, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1879 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1880 | SkDrawFilter* drawFilter, const SkMatrix& viewMatrix, |
| 1881 | const SkIRect& clipBounds, SkScalar x, SkScalar y) { |
| 1882 | SkPaint runPaint = skPaint; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1883 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1884 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 1885 | const SkPoint& offset = it.offset(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1886 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1887 | it.applyFontToPaint(&runPaint); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1888 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1889 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 1890 | return; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1893 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1894 | |
| 1895 | switch (it.positioning()) { |
| 1896 | case SkTextBlob::kDefault_Positioning: |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1897 | this->drawTextAsPath(dc, rt, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1898 | (const char *)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1899 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 1900 | break; |
| 1901 | case SkTextBlob::kHorizontal_Positioning: |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1902 | this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1903 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1904 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 1905 | clipBounds); |
| 1906 | break; |
| 1907 | case SkTextBlob::kFull_Positioning: |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1908 | this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1909 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1910 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 1911 | break; |
| 1912 | } |
| 1913 | } |
| 1914 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1915 | inline GrDrawBatch* |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1916 | GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1917 | int glyphCount, int run, int subRun, |
| 1918 | GrColor color, SkScalar transX, SkScalar transY, |
| 1919 | const SkPaint& skPaint) { |
| 1920 | GrMaskFormat format = info.fMaskFormat; |
| 1921 | GrColor subRunColor; |
| 1922 | if (kARGB_GrMaskFormat == format) { |
| 1923 | uint8_t paintAlpha = skPaint.getAlpha(); |
| 1924 | subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha); |
| 1925 | } else { |
| 1926 | subRunColor = color; |
| 1927 | } |
| 1928 | |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1929 | TextBatch* batch; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1930 | if (info.fDrawAsDistanceFields) { |
| 1931 | SkColor filteredColor; |
| 1932 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 1933 | if (colorFilter) { |
| 1934 | filteredColor = colorFilter->filterColor(skPaint.getColor()); |
| 1935 | } else { |
| 1936 | filteredColor = skPaint.getColor(); |
| 1937 | } |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1938 | bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry()); |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1939 | batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(), |
| 1940 | fDistanceAdjustTable, filteredColor, |
| 1941 | info.fUseLCDText, useBGR); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1942 | } else { |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1943 | batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1944 | } |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 1945 | TextBatch::Geometry& geometry = batch->geometry(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1946 | geometry.fBlob = SkRef(cacheBlob); |
| 1947 | geometry.fRun = run; |
| 1948 | geometry.fSubRun = subRun; |
| 1949 | geometry.fColor = subRunColor; |
| 1950 | geometry.fTransX = transX; |
| 1951 | geometry.fTransY = transY; |
| 1952 | batch->init(); |
| 1953 | |
| 1954 | return batch; |
| 1955 | } |
| 1956 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1957 | inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1958 | GrAtlasTextBlob* cacheBlob, int run, GrColor color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1959 | SkScalar transX, SkScalar transY, |
| 1960 | const SkPaint& skPaint) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1961 | for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) { |
| 1962 | const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun]; |
| 1963 | int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex; |
| 1964 | if (0 == glyphCount) { |
| 1965 | continue; |
| 1966 | } |
| 1967 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1968 | SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run, |
| 1969 | subRun, color, transX, transY, |
| 1970 | skPaint)); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1971 | dc->drawBatch(pipelineBuilder, batch); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 1975 | inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1976 | GrDrawContext* dc, GrRenderTarget* rt, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1977 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1978 | SkScalar transX, SkScalar transY, |
| 1979 | const SkIRect& clipBounds) { |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1980 | if (!cacheBlob->fBigGlyphs.count()) { |
| 1981 | return; |
| 1982 | } |
| 1983 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1984 | for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1985 | GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i]; |
joshualitt | 19e4c02 | 2015-05-13 11:23:03 -0700 | [diff] [blame] | 1986 | bigGlyph.fVx += transX; |
| 1987 | bigGlyph.fVy += transY; |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1988 | SkMatrix ctm; |
| 1989 | ctm.setScale(bigGlyph.fScale, bigGlyph.fScale); |
| 1990 | ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy); |
| 1991 | if (bigGlyph.fApplyVM) { |
| 1992 | ctm.postConcat(cacheBlob->fViewMatrix); |
| 1993 | } |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 1994 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1995 | GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, bigGlyph.fPath, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 1996 | skPaint, ctm, nullptr, clipBounds, false); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1997 | } |
| 1998 | } |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1999 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 2000 | void GrAtlasTextContext::flush(const SkTextBlob* blob, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 2001 | GrAtlasTextBlob* cacheBlob, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame^] | 2002 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2003 | GrRenderTarget* rt, |
| 2004 | const SkPaint& skPaint, |
| 2005 | const GrPaint& grPaint, |
| 2006 | SkDrawFilter* drawFilter, |
| 2007 | const GrClip& clip, |
| 2008 | const SkMatrix& viewMatrix, |
| 2009 | const SkIRect& clipBounds, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 2010 | SkScalar x, SkScalar y, |
| 2011 | SkScalar transX, SkScalar transY) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2012 | // We loop through the runs of the blob, flushing each. If any run is too large, then we flush |
| 2013 | // it as paths |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 2014 | GrPipelineBuilder pipelineBuilder(grPaint, rt, clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2015 | |
| 2016 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2017 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 2018 | SkTextBlobRunIterator it(blob); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2019 | for (int run = 0; !it.done(); it.next(), run++) { |
| 2020 | if (cacheBlob->fRuns[run].fDrawAsPaths) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2021 | this->flushRunAsPaths(dc, rt, it, clip, skPaint, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 2022 | drawFilter, viewMatrix, clipBounds, x, y); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2023 | continue; |
| 2024 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 2025 | cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2026 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2027 | transX, transY, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | // Now flush big glyphs |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2031 | this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, transX, transY, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 2034 | void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2035 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2036 | GrRenderTarget* rt, |
| 2037 | const SkPaint& skPaint, |
| 2038 | const GrPaint& grPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 2039 | const GrClip& clip, |
| 2040 | const SkIRect& clipBounds) { |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 2041 | GrPipelineBuilder pipelineBuilder(grPaint, rt, clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2042 | |
| 2043 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2044 | for (int run = 0; run < cacheBlob->fRunCount; run++) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2045 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | // Now flush big glyphs |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2049 | this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, 0, 0, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 2050 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2051 | |
| 2052 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2053 | |
| 2054 | #ifdef GR_TEST_UTILS |
| 2055 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 2056 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2057 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2058 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 2059 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2060 | |
| 2061 | if (context->uniqueID() != gContextID) { |
| 2062 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2063 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 2064 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2065 | // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly |
| 2066 | // because we don't really want to have a gpu device here. |
| 2067 | // We enable distance fields by twiddling a knob on the paint |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 2068 | gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | // create dummy render target |
| 2072 | GrSurfaceDesc desc; |
| 2073 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 2074 | desc.fWidth = 1024; |
| 2075 | desc.fHeight = 1024; |
| 2076 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
joshualitt | 1573206 | 2015-05-13 12:15:14 -0700 | [diff] [blame] | 2077 | desc.fSampleCnt = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2078 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, nullptr, 0)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2079 | SkASSERT(texture); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2080 | SkASSERT(nullptr != texture->asRenderTarget()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2081 | GrRenderTarget* rt = texture->asRenderTarget(); |
| 2082 | |
| 2083 | // Setup dummy SkPaint / GrPaint |
| 2084 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 2085 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2086 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2087 | skPaint.setColor(color); |
| 2088 | skPaint.setLCDRenderText(random->nextBool()); |
| 2089 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 2090 | skPaint.setSubpixelText(random->nextBool()); |
| 2091 | |
| 2092 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 2093 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2094 | SkFAIL("couldn't convert paint\n"); |
| 2095 | } |
| 2096 | |
| 2097 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 2098 | int textLen = (int)strlen(text); |
| 2099 | |
| 2100 | // Setup clip |
| 2101 | GrClip clip; |
| 2102 | SkIRect noClip = SkIRect::MakeLargest(); |
| 2103 | |
| 2104 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 2105 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 2106 | SkAutoTUnref<GrAtlasTextBlob> blob( |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2107 | gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text, |
| 2108 | static_cast<size_t>(textLen), 0, 0, noClip)); |
| 2109 | |
| 2110 | SkScalar transX = static_cast<SkScalar>(random->nextU()); |
| 2111 | SkScalar transY = static_cast<SkScalar>(random->nextU()); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 2112 | const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0]; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 2113 | return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint); |
| 2114 | } |
| 2115 | |
| 2116 | #endif |