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