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