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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkColorFilter.h" |
| 9 | #include "include/gpu/GrContext.h" |
| 10 | #include "src/core/SkMaskFilterBase.h" |
| 11 | #include "src/core/SkPaintPriv.h" |
| 12 | #include "src/gpu/GrBlurUtils.h" |
| 13 | #include "src/gpu/GrClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrStyle.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 15 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/ops/GrAtlasTextOp.h" |
| 17 | #include "src/gpu/text/GrTextBlob.h" |
| 18 | #include "src/gpu/text/GrTextTarget.h" |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 19 | |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 20 | #include <new> |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 21 | |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 22 | template <size_t N> static size_t sk_align(size_t s) { |
| 23 | return ((s + (N-1)) / N) * N; |
| 24 | } |
| 25 | |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 26 | sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, |
| 27 | int runCount, |
| 28 | GrColor color, |
| 29 | GrStrikeCache* strikeCache) { |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 30 | // We allocate size for the GrTextBlob itself, plus size for the vertices array, |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 31 | // and size for the glyphIds array. |
| 32 | size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize; |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 33 | |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 34 | size_t blobStart = 0; |
| 35 | size_t vertex = sk_align<alignof(char)> (blobStart + sizeof(GrTextBlob) * 1); |
Ben Wagner | 75d6db7 | 2018-09-20 14:39:39 -0400 | [diff] [blame] | 36 | size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount); |
| 37 | size_t runs = sk_align<alignof(GrTextBlob::Run)>(glyphs + sizeof(GrGlyph*) * glyphCount); |
| 38 | size_t size = (runs + sizeof(GrTextBlob::Run) * runCount); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 39 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 40 | void* allocation = ::operator new (size); |
| 41 | |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 42 | if (CACHE_SANITY_CHECK) { |
| 43 | sk_bzero(allocation, size); |
| 44 | } |
| 45 | |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 46 | sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{strikeCache}}; |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 47 | blob->fSize = size; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 48 | |
| 49 | // setup offsets for vertices / glyphs |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 50 | blob->fVertices = SkTAddOffset<char>(blob.get(), vertex); |
| 51 | blob->fGlyphs = SkTAddOffset<GrGlyph*>(blob.get(), glyphs); |
| 52 | blob->fRuns = SkTAddOffset<GrTextBlob::Run>(blob.get(), runs); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 53 | |
| 54 | // Initialize runs |
| 55 | for (int i = 0; i < runCount; i++) { |
Herb Derby | 5424a5e | 2018-11-14 12:04:38 -0500 | [diff] [blame] | 56 | new (&blob->fRuns[i]) GrTextBlob::Run{blob.get(), color}; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 57 | } |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 58 | blob->fRunCountLimit = runCount; |
| 59 | return blob; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Herb Derby | 36a54c1 | 2019-06-06 10:50:56 -0400 | [diff] [blame^] | 62 | void GrTextBlob::Run::setupFont(const SkStrikeSpec& strikeSpec) { |
Herb Derby | e7efd08 | 2019-05-28 11:30:33 -0400 | [diff] [blame] | 63 | |
| 64 | if (fFallbackStrikeSpec != nullptr) { |
| 65 | *fFallbackStrikeSpec = strikeSpec; |
| 66 | } else { |
| 67 | fStrikeSpec = strikeSpec; |
| 68 | } |
Herbert Derby | 86dbae9 | 2019-01-03 13:10:59 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Herb Derby | 2bb343c | 2018-11-08 15:03:48 -0500 | [diff] [blame] | 71 | void GrTextBlob::Run::appendPathGlyph(const SkPath& path, SkPoint position, |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 72 | SkScalar scale, bool preTransformed) { |
Herb Derby | 2bb343c | 2018-11-08 15:03:48 -0500 | [diff] [blame] | 73 | fPathGlyphs.push_back(PathGlyph(path, position.x(), position.y(), scale, preTransformed)); |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Herb Derby | 0edb214 | 2018-10-16 17:04:11 -0400 | [diff] [blame] | 76 | bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition, |
| 77 | const SkMaskFilterBase::BlurRec& blurRec, |
| 78 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 79 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 80 | // to regenerate the blob on any color change |
| 81 | // We use the grPaint to get any color filter effects |
| 82 | if (fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
Mike Reed | ec7278e | 2019-02-01 14:00:34 -0500 | [diff] [blame] | 83 | fLuminanceColor != SkPaintPriv::ComputeLuminanceColor(paint)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 87 | if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 91 | /** This could be relaxed for blobs with only distance field glyphs. */ |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 92 | if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | // We only cache one masked version |
| 97 | if (fKey.fHasBlur && |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 98 | (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Similarly, we only cache one version for each style |
| 103 | if (fKey.fStyle != SkPaint::kFill_Style && |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 104 | (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 105 | fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 106 | fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 111 | // for mixed blobs if this becomes an issue. |
| 112 | if (this->hasBitmap() && this->hasDistanceField()) { |
| 113 | // Identical viewmatrices and we can reuse in all cases |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 114 | if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | if (this->hasBitmap()) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 121 | if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() || |
| 122 | fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() || |
| 123 | fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() || |
| 124 | fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Herb Derby | cb65ce7 | 2018-10-17 11:39:41 -0400 | [diff] [blame] | 128 | // If the text blob only has full pixel glyphs, then fractional part of the position does |
| 129 | // not affect the SkGlyphs used. |
| 130 | if (anyRunHasSubpixelPosition) { |
| 131 | // We can update the positions in the text blob without regenerating the whole |
Herb Derby | 0edb214 | 2018-10-16 17:04:11 -0400 | [diff] [blame] | 132 | // blob, but only for integer translations. |
| 133 | // This cool bit of math will determine the necessary translation to apply to the |
| 134 | // already generated vertex coordinates to move them to the correct position. |
| 135 | SkScalar transX = viewMatrix.getTranslateX() + |
| 136 | viewMatrix.getScaleX() * (x - fInitialX) + |
| 137 | viewMatrix.getSkewX() * (y - fInitialY) - |
| 138 | fInitialViewMatrix.getTranslateX(); |
| 139 | SkScalar transY = viewMatrix.getTranslateY() + |
| 140 | viewMatrix.getSkewY() * (x - fInitialX) + |
| 141 | viewMatrix.getScaleY() * (y - fInitialY) - |
| 142 | fInitialViewMatrix.getTranslateY(); |
| 143 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) { |
| 144 | return true; |
| 145 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 146 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 147 | } else if (this->hasDistanceField()) { |
| 148 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 149 | // distance field being generated, so we have to regenerate in those cases |
| 150 | SkScalar newMaxScale = viewMatrix.getMaxScale(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 151 | SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale(); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 152 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 153 | if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) { |
| 154 | return true; |
| 155 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 156 | } |
| 157 | |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 158 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 159 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 160 | // the blob anyways at flush time, so no need to regenerate explicitly |
| 161 | return false; |
| 162 | } |
| 163 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 164 | inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp( |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 165 | const SubRun& info, int glyphCount, uint16_t run, uint16_t subRun, |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 166 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 167 | const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props, |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 168 | const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) { |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 169 | GrMaskFormat format = info.maskFormat(); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 170 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 171 | GrPaint grPaint; |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 172 | target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 173 | std::unique_ptr<GrAtlasTextOp> op; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 174 | if (info.drawAsDistanceFields()) { |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 175 | // TODO: Can we be even smarter based on the dest transfer function? |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 176 | op = GrAtlasTextOp::MakeDistanceField( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 177 | target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable, |
Mike Reed | ec7278e | 2019-02-01 14:00:34 -0500 | [diff] [blame] | 178 | target->colorSpaceInfo().isLinearlyBlended(), |
| 179 | SkPaintPriv::ComputeLuminanceColor(paint), |
Ben Wagner | 4c32956 | 2018-04-18 16:04:46 -0400 | [diff] [blame] | 180 | props, info.isAntiAliased(), info.hasUseLCDText()); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 181 | } else { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 182 | op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount, |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 183 | info.needsTransform()); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 184 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 185 | GrAtlasTextOp::Geometry& geometry = op->geometry(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 186 | geometry.fViewMatrix = viewMatrix; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 187 | geometry.fClipRect = clipRect; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 188 | geometry.fBlob = SkRef(this); |
| 189 | geometry.fRun = run; |
| 190 | geometry.fSubRun = subRun; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 191 | geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 192 | geometry.fX = x; |
| 193 | geometry.fY = y; |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 194 | op->init(); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 195 | return op; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 196 | } |
| 197 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 198 | static void calculate_translation(bool applyVM, |
| 199 | const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY, |
| 200 | const SkMatrix& currentViewMatrix, SkScalar currentX, |
| 201 | SkScalar currentY, SkScalar* transX, SkScalar* transY) { |
| 202 | if (applyVM) { |
| 203 | *transX = newViewMatrix.getTranslateX() + |
| 204 | newViewMatrix.getScaleX() * (newX - currentX) + |
| 205 | newViewMatrix.getSkewX() * (newY - currentY) - |
| 206 | currentViewMatrix.getTranslateX(); |
| 207 | |
| 208 | *transY = newViewMatrix.getTranslateY() + |
| 209 | newViewMatrix.getSkewY() * (newX - currentX) + |
| 210 | newViewMatrix.getScaleY() * (newY - currentY) - |
| 211 | currentViewMatrix.getTranslateY(); |
| 212 | } else { |
| 213 | *transX = newX - currentX; |
| 214 | *transY = newY - currentY; |
| 215 | } |
| 216 | } |
| 217 | |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 218 | void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props, |
| 219 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 220 | const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip, |
Robert Phillips | e4643cc | 2018-08-14 13:01:29 -0400 | [diff] [blame] | 221 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 222 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 223 | // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices. |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 224 | // Encountering something larger than this is highly unlikely, so we'll just not draw it. |
Herb Derby | 2bb343c | 2018-11-08 15:03:48 -0500 | [diff] [blame] | 225 | int lastRun = SkTMin(fRunCountLimit, (1 << 16)) - 1; |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 226 | // For each run in the GrTextBlob we're going to churn through all the glyphs. |
| 227 | // Each run is broken into a path part and a Mask / DFT / ARGB part. |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 228 | for (int runIndex = 0; runIndex <= lastRun; runIndex++) { |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 229 | |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 230 | Run& run = fRuns[runIndex]; |
| 231 | |
| 232 | // first flush any path glyphs |
| 233 | if (run.fPathGlyphs.count()) { |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 234 | SkPaint runPaint{paint}; |
Mike Reed | 48b958b | 2018-12-03 13:09:02 -0500 | [diff] [blame] | 235 | runPaint.setAntiAlias(run.fAntiAlias); |
Herb Derby | 9f49148 | 2018-08-08 11:53:00 -0400 | [diff] [blame] | 236 | |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 237 | for (int i = 0; i < run.fPathGlyphs.count(); i++) { |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 238 | GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i]; |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 239 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 240 | SkMatrix ctm; |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 241 | const SkPath* path = &pathGlyph.fPath; |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 242 | |
| 243 | // TmpPath must be in the same scope as GrShape shape below. |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 244 | SkTLazy<SkPath> tmpPath; |
| 245 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 246 | // The glyph positions and glyph outlines are either in device space or in source |
| 247 | // space based on fPreTransformed. |
| 248 | if (!pathGlyph.fPreTransformed) { |
| 249 | // Positions and outlines are in source space. |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 250 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 251 | ctm = viewMatrix; |
Robert Phillips | d20d261 | 2018-08-28 10:09:01 -0400 | [diff] [blame] | 252 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 253 | SkMatrix pathMatrix = SkMatrix::MakeScale(pathGlyph.fScale, pathGlyph.fScale); |
Robert Phillips | d20d261 | 2018-08-28 10:09:01 -0400 | [diff] [blame] | 254 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 255 | // The origin for the blob may have changed, so figure out the delta. |
| 256 | SkVector originShift = SkPoint{x, y} - SkPoint{fInitialX, fInitialY}; |
| 257 | |
| 258 | // Shift the original glyph location in source space to the position of the new |
| 259 | // blob. |
| 260 | pathMatrix.postTranslate(originShift.x() + pathGlyph.fX, |
| 261 | originShift.y() + pathGlyph.fY); |
| 262 | |
| 263 | // If there are shaders, blurs or styles, the path must be scaled into source |
| 264 | // space independently of the CTM. This allows the CTM to be correct for the |
| 265 | // different effects. |
| 266 | GrStyle style(runPaint); |
| 267 | bool scalePath = runPaint.getShader() |
| 268 | || style.applies() |
| 269 | || runPaint.getMaskFilter(); |
| 270 | if (!scalePath) { |
| 271 | // Scale can be applied to CTM -- no effects. |
| 272 | |
| 273 | ctm.preConcat(pathMatrix); |
| 274 | } else { |
| 275 | // Scale the outline into source space. |
| 276 | |
| 277 | // Transform the path form the normalized outline to source space. This |
| 278 | // way the CTM will remain the same so it can be used by the effects. |
| 279 | SkPath* sourceOutline = tmpPath.init(); |
| 280 | path->transform(pathMatrix, sourceOutline); |
| 281 | sourceOutline->setIsVolatile(true); |
| 282 | path = sourceOutline; |
| 283 | } |
| 284 | |
| 285 | |
Robert Phillips | d20d261 | 2018-08-28 10:09:01 -0400 | [diff] [blame] | 286 | } else { |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 287 | // Positions and outlines are in device space. |
| 288 | |
| 289 | SkPoint originalOrigin = {fInitialX, fInitialY}; |
| 290 | fInitialViewMatrix.mapPoints(&originalOrigin, 1); |
| 291 | |
| 292 | SkPoint newOrigin = {x, y}; |
| 293 | viewMatrix.mapPoints(&newOrigin, 1); |
| 294 | |
| 295 | // The origin shift in device space. |
| 296 | SkPoint originShift = newOrigin - originalOrigin; |
| 297 | |
| 298 | // Shift the original glyph location in device space to the position of the |
| 299 | // new blob. |
| 300 | ctm = SkMatrix::MakeTrans(originShift.x() + pathGlyph.fX, |
| 301 | originShift.y() + pathGlyph.fY); |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Robert Phillips | 46a1338 | 2018-08-23 13:53:01 -0400 | [diff] [blame] | 304 | // TODO: we are losing the mutability of the path here |
| 305 | GrShape shape(*path, paint); |
| 306 | |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 307 | target->drawShape(clip, runPaint, ctm, shape); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 308 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 309 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 310 | |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 311 | // then flush each subrun, if any |
| 312 | if (!run.fInitialized) { |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 313 | continue; |
| 314 | } |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 315 | |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 316 | int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1; |
| 317 | for (int subRun = 0; subRun <= lastSubRun; subRun++) { |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 318 | const SubRun& info = run.fSubRunInfo[subRun]; |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 319 | int glyphCount = info.glyphCount(); |
| 320 | if (0 == glyphCount) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | bool skipClip = false; |
| 325 | bool submitOp = true; |
| 326 | SkIRect clipRect = SkIRect::MakeEmpty(); |
| 327 | SkRect rtBounds = SkRect::MakeWH(target->width(), target->height()); |
| 328 | SkRRect clipRRect; |
| 329 | GrAA aa; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 330 | // We can clip geometrically if we're not using SDFs or transformed glyphs, |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 331 | // and we have an axis-aligned rectangular non-AA clip |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 332 | if (!info.drawAsDistanceFields() && !info.needsTransform() && |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 333 | clip.isRRect(rtBounds, &clipRRect, &aa) && |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 334 | clipRRect.isRect() && GrAA::kNo == aa) { |
| 335 | skipClip = true; |
| 336 | // We only need to do clipping work if the subrun isn't contained by the clip |
| 337 | SkRect subRunBounds; |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 338 | this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y, |
| 339 | false); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 340 | if (!clipRRect.getBounds().contains(subRunBounds)) { |
| 341 | // If the subrun is completely outside, don't add an op for it |
| 342 | if (!clipRRect.getBounds().intersects(subRunBounds)) { |
| 343 | submitOp = false; |
| 344 | } |
| 345 | else { |
| 346 | clipRRect.getBounds().round(&clipRect); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (submitOp) { |
| 352 | auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 353 | clipRect, paint, filteredColor, props, distanceAdjustTable, |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 354 | target); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 355 | if (op) { |
| 356 | if (skipClip) { |
| 357 | target->addDrawOp(GrNoClip(), std::move(op)); |
| 358 | } |
| 359 | else { |
| 360 | target->addDrawOp(clip, std::move(op)); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 366 | } |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 369 | std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp( |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 370 | int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 371 | SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 372 | const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 373 | GrTextTarget* target) { |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 374 | const GrTextBlob::SubRun& info = fRuns[run].fSubRunInfo[subRun]; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 375 | SkIRect emptyRect = SkIRect::MakeEmpty(); |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 376 | return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, |
| 377 | paint, filteredColor, props, distanceAdjustTable, target); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 378 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 379 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 380 | void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 381 | SkASSERT_RELEASE(l.fSize == r.fSize); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 382 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 383 | SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma); |
| 384 | SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 385 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 386 | SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth); |
| 387 | SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit); |
| 388 | SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 389 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 390 | SkASSERT_RELEASE(l.fKey == r.fKey); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 391 | //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical |
| 392 | SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale); |
| 393 | SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale); |
| 394 | SkASSERT_RELEASE(l.fTextType == r.fTextType); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 395 | |
Herb Derby | 2bb343c | 2018-11-08 15:03:48 -0500 | [diff] [blame] | 396 | SkASSERT_RELEASE(l.fRunCountLimit == r.fRunCountLimit); |
| 397 | for (int i = 0; i < l.fRunCountLimit; i++) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 398 | const Run& lRun = l.fRuns[i]; |
| 399 | const Run& rRun = r.fRuns[i]; |
| 400 | |
Herb Derby | e7efd08 | 2019-05-28 11:30:33 -0400 | [diff] [blame] | 401 | SkASSERT_RELEASE(lRun.fStrikeSpec.descriptor() == rRun.fStrikeSpec.descriptor()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 402 | |
| 403 | // color can be changed |
| 404 | //SkASSERT(lRun.fColor == rRun.fColor); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 405 | SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 406 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 407 | SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 408 | for(int j = 0; j < lRun.fSubRunInfo.count(); j++) { |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 409 | const SubRun& lSubRun = lRun.fSubRunInfo[j]; |
| 410 | const SubRun& rSubRun = rRun.fSubRunInfo[j]; |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 411 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 412 | // TODO we can do this check, but we have to apply the VM to the old vertex bounds |
| 413 | //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 414 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 415 | if (lSubRun.strike()) { |
| 416 | SkASSERT_RELEASE(rSubRun.strike()); |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 417 | SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) == |
| 418 | GrTextStrike::GetKey(*rSubRun.strike())); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 419 | |
| 420 | } else { |
| 421 | SkASSERT_RELEASE(!rSubRun.strike()); |
| 422 | } |
| 423 | |
| 424 | SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex()); |
| 425 | SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex()); |
| 426 | SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex()); |
| 427 | SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex()); |
| 428 | SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat()); |
| 429 | SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields()); |
| 430 | SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 431 | } |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 432 | |
| 433 | SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count()); |
| 434 | for (int i = 0; i < lRun.fPathGlyphs.count(); i++) { |
| 435 | const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i]; |
| 436 | const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i]; |
| 437 | |
| 438 | SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath); |
| 439 | // We can't assert that these have the same translations |
| 440 | } |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 443 | |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 444 | void GrTextBlob::SubRun::computeTranslation(const SkMatrix& viewMatrix, |
| 445 | SkScalar x, SkScalar y, SkScalar* transX, |
| 446 | SkScalar* transY) { |
Herb Derby | 5f7b014 | 2018-12-14 16:47:45 -0500 | [diff] [blame] | 447 | // Don't use the matrix to translate on distance field for fallback subruns. |
| 448 | calculate_translation(!this->drawAsDistanceFields() && !this->isFallback(), viewMatrix, |
| 449 | x, y, fCurrentViewMatrix, fX, fY, transX, transY); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 450 | fCurrentViewMatrix = viewMatrix; |
| 451 | fX = x; |
| 452 | fY = y; |
| 453 | } |