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, |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 27 | bool forceWForDistanceFields, |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 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; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 35 | size_t vertex = sk_align<alignof(char)> (blobStart + sizeof(GrTextBlob) * 1); |
| 36 | size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount); |
| 37 | size_t size = (glyphs + sizeof(GrGlyph*) * glyphCount); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 38 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 39 | void* allocation = ::operator new (size); |
| 40 | |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 41 | if (CACHE_SANITY_CHECK) { |
| 42 | sk_bzero(allocation, size); |
| 43 | } |
| 44 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 45 | sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{strikeCache, color, forceWForDistanceFields}}; |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 46 | blob->fSize = size; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 47 | |
| 48 | // setup offsets for vertices / glyphs |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 49 | blob->fVertices = SkTAddOffset<char>(blob.get(), vertex); |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 50 | blob->fGlyphs = SkTAddOffset<GrGlyph*>(blob.get(), glyphs); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 51 | |
Herb Derby | f7d5d74 | 2018-11-16 13:24:32 -0500 | [diff] [blame] | 52 | return blob; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Herb Derby | 0edb214 | 2018-10-16 17:04:11 -0400 | [diff] [blame] | 55 | bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition, |
| 56 | const SkMaskFilterBase::BlurRec& blurRec, |
| 57 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 58 | // If we have LCD text then our canonical color will be set to transparent, in this case we have |
| 59 | // to regenerate the blob on any color change |
| 60 | // We use the grPaint to get any color filter effects |
| 61 | if (fKey.fCanonicalColor == SK_ColorTRANSPARENT && |
Mike Reed | ec7278e | 2019-02-01 14:00:34 -0500 | [diff] [blame] | 62 | fLuminanceColor != SkPaintPriv::ComputeLuminanceColor(paint)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 66 | if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 70 | /** This could be relaxed for blobs with only distance field glyphs. */ |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 71 | if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // We only cache one masked version |
| 76 | if (fKey.fHasBlur && |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 77 | (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // Similarly, we only cache one version for each style |
| 82 | if (fKey.fStyle != SkPaint::kFill_Style && |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 83 | (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() || |
| 84 | fStrokeInfo.fMiterLimit != paint.getStrokeMiter() || |
| 85 | fStrokeInfo.fJoin != paint.getStrokeJoin())) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls |
| 90 | // for mixed blobs if this becomes an issue. |
| 91 | if (this->hasBitmap() && this->hasDistanceField()) { |
| 92 | // Identical viewmatrices and we can reuse in all cases |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 93 | if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | if (this->hasBitmap()) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 100 | if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() || |
| 101 | fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() || |
| 102 | fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() || |
| 103 | fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
Herb Derby | 9830fc4 | 2019-09-12 10:58:26 -0400 | [diff] [blame] | 107 | // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust |
| 108 | // the quads properly. Devise a system that regenerates the quads from original data |
| 109 | // using the transform to allow this to be used in general. |
| 110 | |
| 111 | // We can update the positions in the text blob without regenerating the whole |
| 112 | // blob, but only for integer translations. |
| 113 | // This cool bit of math will determine the necessary translation to apply to the |
| 114 | // already generated vertex coordinates to move them to the correct position. |
| 115 | // Figure out the translation in view space given a translation in source space. |
| 116 | SkScalar transX = viewMatrix.getTranslateX() + |
| 117 | viewMatrix.getScaleX() * (x - fInitialX) + |
| 118 | viewMatrix.getSkewX() * (y - fInitialY) - |
| 119 | fInitialViewMatrix.getTranslateX(); |
| 120 | SkScalar transY = viewMatrix.getTranslateY() + |
| 121 | viewMatrix.getSkewY() * (x - fInitialX) + |
| 122 | viewMatrix.getScaleY() * (y - fInitialY) - |
| 123 | fInitialViewMatrix.getTranslateY(); |
| 124 | if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) { |
| 125 | return true; |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 126 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 127 | } else if (this->hasDistanceField()) { |
| 128 | // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different |
| 129 | // distance field being generated, so we have to regenerate in those cases |
| 130 | SkScalar newMaxScale = viewMatrix.getMaxScale(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 131 | SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale(); |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 132 | SkScalar scaleAdjust = newMaxScale / oldMaxScale; |
| 133 | if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) { |
| 134 | return true; |
| 135 | } |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 138 | // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case |
| 139 | // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate |
| 140 | // the blob anyways at flush time, so no need to regenerate explicitly |
| 141 | return false; |
| 142 | } |
| 143 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 144 | inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp( |
Herb Derby | a6c4a30 | 2019-11-21 11:58:10 -0500 | [diff] [blame] | 145 | SubRun& info, int glyphCount, |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 146 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 147 | const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props, |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 148 | const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) { |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 149 | GrMaskFormat format = info.maskFormat(); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 150 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 151 | GrPaint grPaint; |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 152 | target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 153 | std::unique_ptr<GrAtlasTextOp> op; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 154 | if (info.drawAsDistanceFields()) { |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 155 | // TODO: Can we be even smarter based on the dest transfer function? |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 156 | op = GrAtlasTextOp::MakeDistanceField( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 157 | target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 158 | target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint), |
Ben Wagner | 4c32956 | 2018-04-18 16:04:46 -0400 | [diff] [blame] | 159 | props, info.isAntiAliased(), info.hasUseLCDText()); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 160 | } else { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 161 | op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount, |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 162 | info.needsTransform()); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 163 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 164 | GrAtlasTextOp::Geometry& geometry = op->geometry(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 165 | geometry.fViewMatrix = viewMatrix; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 166 | geometry.fClipRect = clipRect; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 167 | geometry.fBlob = SkRef(this); |
Herb Derby | 660c2ff | 2019-11-14 18:22:41 -0500 | [diff] [blame] | 168 | geometry.fSubRunPtr = &info; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 169 | geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 170 | geometry.fX = x; |
| 171 | geometry.fY = y; |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 172 | op->init(); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 173 | return op; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 174 | } |
| 175 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 176 | static void calculate_translation(bool applyVM, |
| 177 | const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY, |
| 178 | const SkMatrix& currentViewMatrix, SkScalar currentX, |
| 179 | SkScalar currentY, SkScalar* transX, SkScalar* transY) { |
| 180 | if (applyVM) { |
| 181 | *transX = newViewMatrix.getTranslateX() + |
| 182 | newViewMatrix.getScaleX() * (newX - currentX) + |
| 183 | newViewMatrix.getSkewX() * (newY - currentY) - |
| 184 | currentViewMatrix.getTranslateX(); |
| 185 | |
| 186 | *transY = newViewMatrix.getTranslateY() + |
| 187 | newViewMatrix.getSkewY() * (newX - currentX) + |
| 188 | newViewMatrix.getScaleY() * (newY - currentY) - |
| 189 | currentViewMatrix.getTranslateY(); |
| 190 | } else { |
| 191 | *transX = newX - currentX; |
| 192 | *transY = newY - currentY; |
| 193 | } |
| 194 | } |
| 195 | |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 196 | void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props, |
| 197 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 198 | const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip, |
Robert Phillips | e4643cc | 2018-08-14 13:01:29 -0400 | [diff] [blame] | 199 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 200 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 201 | for (auto& subRun : fSubRuns) { |
| 202 | if (subRun.drawAsPaths()) { |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 203 | SkPaint runPaint{paint}; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 204 | runPaint.setAntiAlias(subRun.isAntiAliased()); |
| 205 | // If there are shaders, blurs or styles, the path must be scaled into source |
| 206 | // space independently of the CTM. This allows the CTM to be correct for the |
| 207 | // different effects. |
| 208 | GrStyle style(runPaint); |
Herb Derby | 9f49148 | 2018-08-08 11:53:00 -0400 | [diff] [blame] | 209 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 210 | bool scalePath = runPaint.getShader() |
| 211 | || style.applies() |
| 212 | || runPaint.getMaskFilter(); |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 213 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 214 | // The origin for the blob may have changed, so figure out the delta. |
| 215 | SkVector originShift = SkPoint{x, y} - SkPoint{fInitialX, fInitialY}; |
| 216 | |
| 217 | for (const auto& pathGlyph : subRun.fPaths) { |
| 218 | SkMatrix ctm{viewMatrix}; |
| 219 | SkMatrix pathMatrix = SkMatrix::MakeScale(subRun.fStrikeSpec.strikeToSourceRatio()); |
| 220 | // Shift the original glyph location in source space to the position of the new |
| 221 | // blob. |
| 222 | pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(), |
| 223 | originShift.y() + pathGlyph.fOrigin.y()); |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 224 | |
| 225 | // TmpPath must be in the same scope as GrShape shape below. |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 226 | SkTLazy<SkPath> tmpPath; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 227 | const SkPath* path = &pathGlyph.fPath; |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 228 | if (!scalePath) { |
| 229 | // Scale can be applied to CTM -- no effects. |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 230 | ctm.preConcat(pathMatrix); |
Robert Phillips | d20d261 | 2018-08-28 10:09:01 -0400 | [diff] [blame] | 231 | } else { |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 232 | // Scale the outline into source space. |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 233 | |
Herb Derby | 2984d26 | 2019-11-20 14:40:39 -0500 | [diff] [blame] | 234 | // Transform the path form the normalized outline to source space. This |
| 235 | // way the CTM will remain the same so it can be used by the effects. |
| 236 | SkPath* sourceOutline = tmpPath.init(); |
| 237 | path->transform(pathMatrix, sourceOutline); |
| 238 | sourceOutline->setIsVolatile(true); |
| 239 | path = sourceOutline; |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Robert Phillips | 46a1338 | 2018-08-23 13:53:01 -0400 | [diff] [blame] | 242 | // TODO: we are losing the mutability of the path here |
| 243 | GrShape shape(*path, paint); |
Herb Derby | dac1ed5 | 2018-09-12 17:04:21 -0400 | [diff] [blame] | 244 | target->drawShape(clip, runPaint, ctm, shape); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 245 | } |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 246 | } else { |
| 247 | int glyphCount = subRun.glyphCount(); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 248 | if (0 == glyphCount) { |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | bool skipClip = false; |
| 253 | bool submitOp = true; |
| 254 | SkIRect clipRect = SkIRect::MakeEmpty(); |
| 255 | SkRect rtBounds = SkRect::MakeWH(target->width(), target->height()); |
| 256 | SkRRect clipRRect; |
| 257 | GrAA aa; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 258 | // 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] | 259 | // and we have an axis-aligned rectangular non-AA clip |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 260 | if (!subRun.drawAsDistanceFields() && !subRun.needsTransform() && |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 261 | clip.isRRect(rtBounds, &clipRRect, &aa) && |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 262 | clipRRect.isRect() && GrAA::kNo == aa) { |
| 263 | skipClip = true; |
| 264 | // We only need to do clipping work if the subrun isn't contained by the clip |
| 265 | SkRect subRunBounds; |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 266 | this->computeSubRunBounds(&subRunBounds, subRun, viewMatrix, x, y, false); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 267 | if (!clipRRect.getBounds().contains(subRunBounds)) { |
| 268 | // If the subrun is completely outside, don't add an op for it |
| 269 | if (!clipRRect.getBounds().intersects(subRunBounds)) { |
| 270 | submitOp = false; |
| 271 | } |
| 272 | else { |
| 273 | clipRRect.getBounds().round(&clipRect); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (submitOp) { |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 279 | auto op = this->makeOp(subRun, glyphCount, viewMatrix, x, y, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 280 | clipRect, paint, filteredColor, props, distanceAdjustTable, |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 281 | target); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 282 | if (op) { |
| 283 | if (skipClip) { |
| 284 | target->addDrawOp(GrNoClip(), std::move(op)); |
| 285 | } |
| 286 | else { |
| 287 | target->addDrawOp(clip, std::move(op)); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 292 | } |
Jim Van Verth | 89737de | 2018-02-06 21:30:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 295 | std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp( |
Herb Derby | a6c4a30 | 2019-11-21 11:58:10 -0500 | [diff] [blame] | 296 | int glyphCount, const SkMatrix& viewMatrix, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 297 | SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 298 | const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 299 | GrTextTarget* target) { |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 300 | GrTextBlob::SubRun& info = fSubRuns[0]; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 301 | SkIRect emptyRect = SkIRect::MakeEmpty(); |
Herb Derby | a6c4a30 | 2019-11-21 11:58:10 -0500 | [diff] [blame] | 302 | return this->makeOp(info, glyphCount, viewMatrix, x, y, emptyRect, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 303 | paint, filteredColor, props, distanceAdjustTable, target); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 304 | } |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 305 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 306 | void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) { |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 307 | SkASSERT_RELEASE(l.fSize == r.fSize); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 308 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 309 | SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma); |
| 310 | SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 311 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 312 | SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth); |
| 313 | SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit); |
| 314 | SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 315 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 316 | SkASSERT_RELEASE(l.fKey == r.fKey); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 317 | //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical |
| 318 | SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale); |
| 319 | SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale); |
| 320 | SkASSERT_RELEASE(l.fTextType == r.fTextType); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 321 | |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 322 | for(auto t : SkMakeZip(l.fSubRuns, r.fSubRuns)) { |
| 323 | const SubRun& lSubRun = std::get<0>(t); |
| 324 | const SubRun& rSubRun = std::get<1>(t); |
| 325 | SkASSERT(lSubRun.drawAsPaths() == rSubRun.drawAsPaths()); |
| 326 | if (!lSubRun.drawAsPaths()) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 327 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 328 | // TODO we can do this check, but we have to apply the VM to the old vertex bounds |
| 329 | //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds()); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 330 | |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 331 | if (lSubRun.strike()) { |
| 332 | SkASSERT_RELEASE(rSubRun.strike()); |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 333 | SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) == |
| 334 | GrTextStrike::GetKey(*rSubRun.strike())); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 335 | |
| 336 | } else { |
| 337 | SkASSERT_RELEASE(!rSubRun.strike()); |
| 338 | } |
| 339 | |
| 340 | SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex()); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 341 | SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex()); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 342 | SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat()); |
| 343 | SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields()); |
| 344 | SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText()); |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame^] | 345 | } else { |
| 346 | SkASSERT_RELEASE(lSubRun.fPaths.size() == rSubRun.fPaths.size()); |
| 347 | for(auto p : SkMakeZip(lSubRun.fPaths, rSubRun.fPaths)) { |
| 348 | const PathGlyph& lPath = std::get<0>(p); |
| 349 | const PathGlyph& rPath = std::get<1>(p); |
| 350 | SkASSERT_RELEASE(lPath.fPath == rPath.fPath); |
| 351 | // We can't assert that these have the same translations |
| 352 | } |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 353 | } |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 356 | |
Herb Derby | 69ff895 | 2018-11-12 11:39:12 -0500 | [diff] [blame] | 357 | void GrTextBlob::SubRun::computeTranslation(const SkMatrix& viewMatrix, |
| 358 | SkScalar x, SkScalar y, SkScalar* transX, |
| 359 | SkScalar* transY) { |
Herb Derby | 5f7b014 | 2018-12-14 16:47:45 -0500 | [diff] [blame] | 360 | // Don't use the matrix to translate on distance field for fallback subruns. |
Herb Derby | d667e61 | 2019-11-18 18:58:20 -0500 | [diff] [blame] | 361 | calculate_translation(!this->drawAsDistanceFields() && !this->needsTransform(), viewMatrix, |
Herb Derby | 5f7b014 | 2018-12-14 16:47:45 -0500 | [diff] [blame] | 362 | x, y, fCurrentViewMatrix, fX, fY, transX, transY); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 363 | fCurrentViewMatrix = viewMatrix; |
| 364 | fX = x; |
| 365 | fY = y; |
| 366 | } |