joshualitt | 259fbf1 | 2015-07-21 11:39:34 -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 | |
| 8 | #include "GrAtlasTextBlob.h" |
| 9 | |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 10 | #include "GrBlurUtils.h" |
| 11 | #include "GrContext.h" |
| 12 | #include "GrDrawContext.h" |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 13 | #include "GrTextUtils.h" |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 14 | #include "SkColorFilter.h" |
| 15 | #include "SkDrawFilter.h" |
joshualitt | e76b4bb | 2015-12-28 07:23:58 -0800 | [diff] [blame] | 16 | #include "SkGlyphCache.h" |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 17 | #include "SkTextBlobRunIterator.h" |
| 18 | #include "batches/GrAtlasTextBatch.h" |
| 19 | |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 20 | GrAtlasTextBlob* GrAtlasTextBlob::Create(GrMemoryPool* pool, int glyphCount, int runCount) { |
| 21 | // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array, |
| 22 | // and size for the glyphIds array. |
| 23 | size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize; |
| 24 | size_t size = sizeof(GrAtlasTextBlob) + |
| 25 | verticesCount + |
| 26 | glyphCount * sizeof(GrGlyph**) + |
| 27 | sizeof(GrAtlasTextBlob::Run) * runCount; |
| 28 | |
| 29 | void* allocation = pool->allocate(size); |
| 30 | if (CACHE_SANITY_CHECK) { |
| 31 | sk_bzero(allocation, size); |
| 32 | } |
| 33 | |
| 34 | GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob; |
| 35 | cacheBlob->fSize = size; |
| 36 | |
| 37 | // setup offsets for vertices / glyphs |
| 38 | cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob); |
| 39 | cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount); |
| 40 | cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount); |
| 41 | |
| 42 | // Initialize runs |
| 43 | for (int i = 0; i < runCount; i++) { |
| 44 | new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run; |
| 45 | } |
| 46 | cacheBlob->fRunCount = runCount; |
| 47 | cacheBlob->fPool = pool; |
| 48 | return cacheBlob; |
| 49 | } |
| 50 | |
| 51 | |
joshualitt | e76b4bb | 2015-12-28 07:23:58 -0800 | [diff] [blame] | 52 | SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex, |
| 53 | const SkSurfaceProps& props, |
| 54 | const SkPaint& skPaint, |
| 55 | const SkMatrix* viewMatrix, |
| 56 | bool noGamma) { |
| 57 | GrAtlasTextBlob::Run* run = &fRuns[runIndex]; |
| 58 | |
| 59 | // if we have an override descriptor for the run, then we should use that |
| 60 | SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() : |
| 61 | &run->fDescriptor; |
| 62 | skPaint.getScalerContextDescriptor(desc, props, viewMatrix, noGamma); |
| 63 | run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 64 | return SkGlyphCache::DetachCache(run->fTypeface, desc->getDesc()); |
| 65 | } |
| 66 | |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 67 | void GrAtlasTextBlob::appendGlyph(int runIndex, |
| 68 | const SkRect& positions, |
| 69 | GrColor color, |
| 70 | GrBatchTextStrike* strike, |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 71 | GrGlyph* glyph, |
| 72 | GrFontScaler* scaler, const SkGlyph& skGlyph, |
| 73 | SkScalar x, SkScalar y, SkScalar scale, bool applyVM) { |
| 74 | |
| 75 | // If the glyph is too large we fall back to paths |
| 76 | if (glyph->fTooLargeForAtlas) { |
| 77 | this->appendLargeGlyph(glyph, scaler, skGlyph, x, y, scale, applyVM); |
| 78 | return; |
| 79 | } |
| 80 | |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 81 | Run& run = fRuns[runIndex]; |
| 82 | GrMaskFormat format = glyph->fMaskFormat; |
| 83 | |
| 84 | Run::SubRunInfo* subRun = &run.fSubRunInfo.back(); |
| 85 | if (run.fInitialized && subRun->maskFormat() != format) { |
| 86 | subRun = &run.push_back(); |
| 87 | subRun->setStrike(strike); |
| 88 | } else if (!run.fInitialized) { |
| 89 | subRun->setStrike(strike); |
| 90 | } |
| 91 | |
| 92 | run.fInitialized = true; |
| 93 | |
| 94 | size_t vertexStride = GetVertexStride(format); |
| 95 | |
| 96 | subRun->setMaskFormat(format); |
| 97 | |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 98 | subRun->joinGlyphBounds(positions); |
joshualitt | f9e658b | 2015-12-09 09:26:44 -0800 | [diff] [blame] | 99 | subRun->setColor(color); |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 100 | |
| 101 | intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex()); |
| 102 | |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 103 | if (kARGB_GrMaskFormat != glyph->fMaskFormat) { |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 104 | // V0 |
| 105 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 106 | position->set(positions.fLeft, positions.fTop); |
| 107 | SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 108 | *colorPtr = color; |
| 109 | vertex += vertexStride; |
| 110 | |
| 111 | // V1 |
| 112 | position = reinterpret_cast<SkPoint*>(vertex); |
| 113 | position->set(positions.fLeft, positions.fBottom); |
| 114 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 115 | *colorPtr = color; |
| 116 | vertex += vertexStride; |
| 117 | |
| 118 | // V2 |
| 119 | position = reinterpret_cast<SkPoint*>(vertex); |
| 120 | position->set(positions.fRight, positions.fBottom); |
| 121 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 122 | *colorPtr = color; |
| 123 | vertex += vertexStride; |
| 124 | |
| 125 | // V3 |
| 126 | position = reinterpret_cast<SkPoint*>(vertex); |
| 127 | position->set(positions.fRight, positions.fTop); |
| 128 | colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 129 | *colorPtr = color; |
| 130 | } else { |
| 131 | // V0 |
| 132 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 133 | position->set(positions.fLeft, positions.fTop); |
| 134 | vertex += vertexStride; |
| 135 | |
| 136 | // V1 |
| 137 | position = reinterpret_cast<SkPoint*>(vertex); |
| 138 | position->set(positions.fLeft, positions.fBottom); |
| 139 | vertex += vertexStride; |
| 140 | |
| 141 | // V2 |
| 142 | position = reinterpret_cast<SkPoint*>(vertex); |
| 143 | position->set(positions.fRight, positions.fBottom); |
| 144 | vertex += vertexStride; |
| 145 | |
| 146 | // V3 |
| 147 | position = reinterpret_cast<SkPoint*>(vertex); |
| 148 | position->set(positions.fRight, positions.fTop); |
| 149 | } |
| 150 | subRun->appendVertices(vertexStride); |
| 151 | fGlyphs[subRun->glyphEndIndex()] = glyph; |
| 152 | subRun->glyphAppended(); |
| 153 | } |
| 154 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 155 | void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, GrFontScaler* scaler, const SkGlyph& skGlyph, |
| 156 | SkScalar x, SkScalar y, SkScalar scale, bool applyVM) { |
| 157 | if (nullptr == glyph->fPath) { |
| 158 | const SkPath* glyphPath = scaler->getGlyphPath(skGlyph); |
| 159 | if (!glyphPath) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | glyph->fPath = new SkPath(*glyphPath); |
| 164 | } |
| 165 | fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM)); |
| 166 | } |
| 167 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 168 | bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint, |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 169 | GrColor color, const SkMaskFilter::BlurRec& blurRec, |
| 170 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
| 171 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 172 | // to regenerate the blob on any color change |
| 173 | // We use the grPaint to get any color filter effects |
| 174 | if (fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
| 175 | fPaintColor != color) { |
| 176 | return true; |
| 177 | } |
| 178 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 179 | if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 183 | if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
| 187 | // We only cache one masked version |
| 188 | if (fKey.fHasBlur && |
| 189 | (fBlurRec.fSigma != blurRec.fSigma || |
| 190 | fBlurRec.fStyle != blurRec.fStyle || |
| 191 | fBlurRec.fQuality != blurRec.fQuality)) { |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | // Similarly, we only cache one version for each style |
| 196 | if (fKey.fStyle != SkPaint::kFill_Style && |
| 197 | (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 198 | fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 199 | fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 204 | // for mixed blobs if this becomes an issue. |
| 205 | if (this->hasBitmap() && this->hasDistanceField()) { |
| 206 | // Identical viewmatrices and we can reuse in all cases |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 207 | if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (this->hasBitmap()) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 214 | if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() || |
| 215 | fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() || |
| 216 | fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() || |
| 217 | fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
| 221 | // We can update the positions in the cachedtextblobs without regenerating the whole blob, |
| 222 | // but only for integer translations. |
| 223 | // This cool bit of math will determine the necessary translation to apply to the already |
| 224 | // generated vertex coordinates to move them to the correct position |
| 225 | SkScalar transX = viewMatrix.getTranslateX() + |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 226 | viewMatrix.getScaleX() * (x - fInitialX) + |
| 227 | viewMatrix.getSkewX() * (y - fInitialY) - |
| 228 | fInitialViewMatrix.getTranslateX(); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 229 | SkScalar transY = viewMatrix.getTranslateY() + |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 230 | viewMatrix.getSkewY() * (x - fInitialX) + |
| 231 | viewMatrix.getScaleY() * (y - fInitialY) - |
| 232 | fInitialViewMatrix.getTranslateY(); |
| 233 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 234 | return true; |
| 235 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 236 | } else if (this->hasDistanceField()) { |
| 237 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 238 | // distance field being generated, so we have to regenerate in those cases |
| 239 | SkScalar newMaxScale = viewMatrix.getMaxScale(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 240 | SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale(); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 241 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 242 | if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) { |
| 243 | return true; |
| 244 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 245 | } |
| 246 | |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 247 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 248 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 249 | // the blob anyways at flush time, so no need to regenerate explicitly |
| 250 | return false; |
| 251 | } |
| 252 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 253 | inline GrDrawBatch* GrAtlasTextBlob::createBatch( |
| 254 | const Run::SubRunInfo& info, |
| 255 | int glyphCount, int run, int subRun, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 256 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
| 257 | GrColor color, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 258 | const SkPaint& skPaint, const SkSurfaceProps& props, |
| 259 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 260 | GrBatchFontCache* cache) { |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 261 | GrMaskFormat format = info.maskFormat(); |
| 262 | GrColor subRunColor; |
| 263 | if (kARGB_GrMaskFormat == format) { |
| 264 | uint8_t paintAlpha = skPaint.getAlpha(); |
| 265 | subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha); |
| 266 | } else { |
| 267 | subRunColor = color; |
| 268 | } |
| 269 | |
| 270 | GrAtlasTextBatch* batch; |
| 271 | if (info.drawAsDistanceFields()) { |
| 272 | SkColor filteredColor; |
| 273 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 274 | if (colorFilter) { |
| 275 | filteredColor = colorFilter->filterColor(skPaint.getColor()); |
| 276 | } else { |
| 277 | filteredColor = skPaint.getColor(); |
| 278 | } |
| 279 | bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry()); |
| 280 | batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, cache, |
| 281 | distanceAdjustTable, filteredColor, |
| 282 | info.hasUseLCDText(), useBGR); |
| 283 | } else { |
| 284 | batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache); |
| 285 | } |
| 286 | GrAtlasTextBatch::Geometry& geometry = batch->geometry(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 287 | geometry.fViewMatrix = viewMatrix; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 288 | geometry.fBlob = SkRef(this); |
| 289 | geometry.fRun = run; |
| 290 | geometry.fSubRun = subRun; |
| 291 | geometry.fColor = subRunColor; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 292 | geometry.fX = x; |
| 293 | geometry.fY = y; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 294 | batch->init(); |
| 295 | |
| 296 | return batch; |
| 297 | } |
| 298 | |
| 299 | inline |
| 300 | void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 301 | int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
| 302 | GrColor color, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 303 | const SkPaint& skPaint, const SkSurfaceProps& props, |
| 304 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 305 | GrBatchFontCache* cache) { |
| 306 | for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) { |
| 307 | const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun]; |
| 308 | int glyphCount = info.glyphCount(); |
| 309 | if (0 == glyphCount) { |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 314 | subRun, viewMatrix, x, y, color, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 315 | skPaint, props, |
| 316 | distanceAdjustTable, cache)); |
| 317 | dc->drawBatch(pipelineBuilder, batch); |
| 318 | } |
| 319 | } |
| 320 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 321 | static void calculate_translation(bool applyVM, |
| 322 | const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY, |
| 323 | const SkMatrix& currentViewMatrix, SkScalar currentX, |
| 324 | SkScalar currentY, SkScalar* transX, SkScalar* transY) { |
| 325 | if (applyVM) { |
| 326 | *transX = newViewMatrix.getTranslateX() + |
| 327 | newViewMatrix.getScaleX() * (newX - currentX) + |
| 328 | newViewMatrix.getSkewX() * (newY - currentY) - |
| 329 | currentViewMatrix.getTranslateX(); |
| 330 | |
| 331 | *transY = newViewMatrix.getTranslateY() + |
| 332 | newViewMatrix.getSkewY() * (newX - currentX) + |
| 333 | newViewMatrix.getScaleY() * (newY - currentY) - |
| 334 | currentViewMatrix.getTranslateY(); |
| 335 | } else { |
| 336 | *transX = newX - currentX; |
| 337 | *transY = newY - currentY; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 342 | void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc, |
| 343 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 344 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 345 | const SkIRect& clipBounds) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 346 | SkScalar transX, transY; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 347 | for (int i = 0; i < fBigGlyphs.count(); i++) { |
| 348 | GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i]; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 349 | calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y, |
| 350 | fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 351 | SkMatrix ctm; |
| 352 | ctm.setScale(bigGlyph.fScale, bigGlyph.fScale); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 353 | ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 354 | if (bigGlyph.fApplyVM) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 355 | ctm.postConcat(viewMatrix); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath, |
| 359 | skPaint, ctm, nullptr, clipBounds, false); |
| 360 | } |
| 361 | } |
| 362 | |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 363 | void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 364 | const SkSurfaceProps& props, |
| 365 | const SkTextBlobRunIterator& it, |
| 366 | const GrClip& clip, const SkPaint& skPaint, |
| 367 | SkDrawFilter* drawFilter, const SkMatrix& viewMatrix, |
| 368 | const SkIRect& clipBounds, SkScalar x, SkScalar y) { |
| 369 | SkPaint runPaint = skPaint; |
| 370 | |
| 371 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 372 | const SkPoint& offset = it.offset(); |
| 373 | |
| 374 | it.applyFontToPaint(&runPaint); |
| 375 | |
| 376 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 377 | return; |
| 378 | } |
| 379 | |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 380 | runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint)); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 381 | |
| 382 | switch (it.positioning()) { |
| 383 | case SkTextBlob::kDefault_Positioning: |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 384 | GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 385 | (const char *)it.glyphs(), |
| 386 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 387 | break; |
| 388 | case SkTextBlob::kHorizontal_Positioning: |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 389 | GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 390 | (const char*)it.glyphs(), |
| 391 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 392 | clipBounds); |
| 393 | break; |
| 394 | case SkTextBlob::kFull_Positioning: |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 395 | GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 396 | (const char*)it.glyphs(), |
| 397 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 402 | void GrAtlasTextBlob::flushCached(GrContext* context, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 403 | GrDrawContext* dc, |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 404 | const SkTextBlob* blob, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 405 | const SkSurfaceProps& props, |
| 406 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 407 | const SkPaint& skPaint, |
| 408 | const GrPaint& grPaint, |
| 409 | SkDrawFilter* drawFilter, |
| 410 | const GrClip& clip, |
| 411 | const SkMatrix& viewMatrix, |
| 412 | const SkIRect& clipBounds, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 413 | SkScalar x, SkScalar y) { |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 414 | // We loop through the runs of the blob, flushing each. If any run is too large, then we flush |
| 415 | // it as paths |
| 416 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
| 417 | |
| 418 | GrColor color = grPaint.getColor(); |
| 419 | |
| 420 | SkTextBlobRunIterator it(blob); |
| 421 | for (int run = 0; !it.done(); it.next(), run++) { |
| 422 | if (fRuns[run].fDrawAsPaths) { |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 423 | this->flushRunAsPaths(context, dc, props, it, clip, skPaint, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 424 | drawFilter, viewMatrix, clipBounds, x, y); |
| 425 | continue; |
| 426 | } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 427 | this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 428 | distanceAdjustTable, context->getBatchFontCache()); |
| 429 | } |
| 430 | |
| 431 | // Now flush big glyphs |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 432 | this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void GrAtlasTextBlob::flushThrowaway(GrContext* context, |
| 436 | GrDrawContext* dc, |
| 437 | const SkSurfaceProps& props, |
| 438 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 439 | const SkPaint& skPaint, |
| 440 | const GrPaint& grPaint, |
| 441 | const GrClip& clip, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 442 | const SkMatrix& viewMatrix, |
| 443 | const SkIRect& clipBounds, |
| 444 | SkScalar x, SkScalar y) { |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 445 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
| 446 | |
| 447 | GrColor color = grPaint.getColor(); |
| 448 | for (int run = 0; run < fRunCount; run++) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 449 | this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 450 | distanceAdjustTable, context->getBatchFontCache()); |
| 451 | } |
| 452 | |
| 453 | // Now flush big glyphs |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 454 | this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 455 | } |
| 456 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 457 | GrDrawBatch* GrAtlasTextBlob::test_createBatch( |
| 458 | int glyphCount, int run, int subRun, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 459 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
| 460 | GrColor color, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 461 | const SkPaint& skPaint, const SkSurfaceProps& props, |
| 462 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 463 | GrBatchFontCache* cache) { |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 464 | const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun]; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 465 | return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 466 | props, distanceAdjustTable, cache); |
| 467 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 468 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 469 | void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 470 | SkASSERT_RELEASE(l.fSize == r.fSize); |
| 471 | SkASSERT_RELEASE(l.fPool == r.fPool); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 472 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 473 | SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma); |
| 474 | SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle); |
| 475 | SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 476 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 477 | SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth); |
| 478 | SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit); |
| 479 | SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 480 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 481 | SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 482 | for (int i = 0; i < l.fBigGlyphs.count(); i++) { |
| 483 | const BigGlyph& lBigGlyph = l.fBigGlyphs[i]; |
| 484 | const BigGlyph& rBigGlyph = r.fBigGlyphs[i]; |
| 485 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 486 | SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 487 | // We can't assert that these have the same translations |
| 488 | } |
| 489 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 490 | SkASSERT_RELEASE(l.fKey == r.fKey); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 491 | //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical |
| 492 | SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale); |
| 493 | SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale); |
| 494 | SkASSERT_RELEASE(l.fTextType == r.fTextType); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 495 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 496 | SkASSERT_RELEASE(l.fRunCount == r.fRunCount); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 497 | for (int i = 0; i < l.fRunCount; i++) { |
| 498 | const Run& lRun = l.fRuns[i]; |
| 499 | const Run& rRun = r.fRuns[i]; |
| 500 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 501 | if (lRun.fTypeface.get()) { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 502 | SkASSERT_RELEASE(rRun.fTypeface.get()); |
| 503 | SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface)); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 504 | } else { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 505 | SkASSERT_RELEASE(!rRun.fTypeface.get()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 506 | } |
| 507 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 508 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 509 | SkASSERT_RELEASE(lRun.fDescriptor.getDesc()); |
| 510 | SkASSERT_RELEASE(rRun.fDescriptor.getDesc()); |
| 511 | SkASSERT_RELEASE(lRun.fDescriptor.getDesc()->equals(*rRun.fDescriptor.getDesc())); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 512 | |
| 513 | if (lRun.fOverrideDescriptor.get()) { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 514 | SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc()); |
| 515 | SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc()); |
| 516 | SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc()->equals( |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 517 | *rRun.fOverrideDescriptor->getDesc())); |
| 518 | } else { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 519 | SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // color can be changed |
| 523 | //SkASSERT(lRun.fColor == rRun.fColor); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 524 | SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized); |
| 525 | SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 526 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 527 | SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 528 | for(int j = 0; j < lRun.fSubRunInfo.count(); j++) { |
| 529 | const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j]; |
| 530 | const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j]; |
| 531 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 532 | // TODO we can do this check, but we have to apply the VM to the old vertex bounds |
| 533 | //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 534 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 535 | if (lSubRun.strike()) { |
| 536 | SkASSERT_RELEASE(rSubRun.strike()); |
| 537 | SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) == |
| 538 | GrBatchTextStrike::GetKey(*rSubRun.strike())); |
| 539 | |
| 540 | } else { |
| 541 | SkASSERT_RELEASE(!rSubRun.strike()); |
| 542 | } |
| 543 | |
| 544 | SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex()); |
| 545 | SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex()); |
| 546 | SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex()); |
| 547 | SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex()); |
| 548 | SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat()); |
| 549 | SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields()); |
| 550 | SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 554 | |
| 555 | void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix, |
| 556 | SkScalar x, SkScalar y, SkScalar* transX, |
| 557 | SkScalar* transY) { |
| 558 | calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y, |
| 559 | fCurrentViewMatrix, fX, fY, transX, transY); |
| 560 | fCurrentViewMatrix = viewMatrix; |
| 561 | fX = x; |
| 562 | fY = y; |
| 563 | } |