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