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