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