joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [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 "src/gpu/ops/GrAtlasTextOp.h" |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPoint3.h" |
| 11 | #include "include/private/GrRecordingContext.h" |
| 12 | #include "src/core/SkMathPriv.h" |
| 13 | #include "src/core/SkMatrixPriv.h" |
| 14 | #include "src/core/SkStrikeCache.h" |
| 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrMemoryPool.h" |
| 17 | #include "src/gpu/GrOpFlushState.h" |
| 18 | #include "src/gpu/GrRecordingContextPriv.h" |
| 19 | #include "src/gpu/GrResourceProvider.h" |
| 20 | #include "src/gpu/effects/GrBitmapTextGeoProc.h" |
| 21 | #include "src/gpu/effects/GrDistanceFieldGeoProc.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 22 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/text/GrAtlasManager.h" |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 24 | #include "src/gpu/text/GrDistanceFieldAdjustTable.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/text/GrStrikeCache.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 26 | |
joshualitt | 60ce86d | 2015-11-23 13:08:22 -0800 | [diff] [blame] | 27 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 28 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 29 | std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 30 | GrPaint&& paint, |
| 31 | GrMaskFormat maskFormat, |
| 32 | int glyphCount, |
| 33 | bool needsTransform) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 34 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 35 | |
| 36 | std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint)); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 37 | |
| 38 | switch (maskFormat) { |
| 39 | case kA8_GrMaskFormat: |
| 40 | op->fMaskType = kGrayscaleCoverageMask_MaskType; |
| 41 | break; |
| 42 | case kA565_GrMaskFormat: |
| 43 | op->fMaskType = kLCDCoverageMask_MaskType; |
| 44 | break; |
| 45 | case kARGB_GrMaskFormat: |
| 46 | op->fMaskType = kColorBitmapMask_MaskType; |
| 47 | break; |
| 48 | } |
| 49 | op->fNumGlyphs = glyphCount; |
| 50 | op->fGeoCount = 1; |
| 51 | op->fLuminanceColor = 0; |
| 52 | op->fNeedsGlyphTransform = needsTransform; |
| 53 | return op; |
| 54 | } |
| 55 | |
| 56 | std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField( |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 57 | GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 58 | GrPaint&& paint, |
| 59 | int glyphCount, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 60 | bool useGammaCorrectDistanceTable, |
| 61 | SkColor luminanceColor, |
| 62 | const SkSurfaceProps& props, |
| 63 | bool isAntiAliased, |
| 64 | bool useLCD) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 65 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 66 | |
| 67 | std::unique_ptr<GrAtlasTextOp> op = pool->allocate<GrAtlasTextOp>(std::move(paint)); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 68 | |
| 69 | bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry()); |
| 70 | bool isLCD = useLCD && SkPixelGeometryIsH(props.pixelGeometry()); |
| 71 | op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType |
| 72 | : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType |
| 73 | : kLCDDistanceField_MaskType) |
| 74 | : kGrayscaleDistanceField_MaskType; |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 75 | op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable; |
| 76 | op->fLuminanceColor = luminanceColor; |
| 77 | op->fNumGlyphs = glyphCount; |
| 78 | op->fGeoCount = 1; |
| 79 | return op; |
| 80 | } |
| 81 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 82 | static const int kDistanceAdjustLumShift = 5; |
| 83 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 84 | void GrAtlasTextOp::init() { |
| 85 | const Geometry& geo = fGeoData[0]; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 86 | if (this->usesDistanceFields()) { |
| 87 | bool isLCD = this->isLCD(); |
| 88 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 89 | const SkMatrix& drawMatrix = geo.fDrawMatrix; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 90 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 91 | fDFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 92 | fDFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 93 | fDFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 94 | fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
| 95 | fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType) |
| 96 | ? kAliased_DistanceFieldEffectFlag |
| 97 | : 0; |
| 98 | |
| 99 | if (isLCD) { |
| 100 | fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag; |
| 101 | fDFGPFlags |= |
| 102 | (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0; |
| 103 | } |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 104 | |
| 105 | fNeedsGlyphTransform = true; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 106 | } |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 107 | |
| 108 | SkRect bounds; |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 109 | geo.fBlob->computeSubRunBounds( |
| 110 | &bounds, *geo.fSubRunPtr, geo.fDrawMatrix, geo.fDrawOrigin, fNeedsGlyphTransform); |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 111 | // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds |
| 112 | // we treat this as a set of non-AA rects rendered with a texture. |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 113 | this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 116 | void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const { |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 117 | fProcessors.visitProxies(func); |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 120 | #ifdef SK_DEBUG |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 121 | SkString GrAtlasTextOp::dumpInfo() const { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 122 | SkString str; |
| 123 | |
| 124 | for (int i = 0; i < fGeoCount; ++i) { |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 125 | str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n", |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 126 | i, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 127 | fGeoData[i].fColor.toBytes_RGBA(), |
Herb Derby | 5bf5b04 | 2019-12-12 16:37:03 -0500 | [diff] [blame] | 128 | fGeoData[i].fDrawOrigin.x(), |
| 129 | fGeoData[i].fDrawOrigin.y()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 132 | str += fProcessors.dumpProcessors(); |
| 133 | str += INHERITED::dumpInfo(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 134 | return str; |
| 135 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 136 | #endif |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 137 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 138 | GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const { |
| 139 | return FixedFunctionFlags::kNone; |
| 140 | } |
| 141 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 142 | GrProcessorSet::Analysis GrAtlasTextOp::finalize( |
| 143 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 144 | GrClampType clampType) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 145 | GrProcessorAnalysisCoverage coverage; |
| 146 | GrProcessorAnalysisColor color; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 147 | if (kColorBitmapMask_MaskType == fMaskType) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 148 | color.setToUnknown(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 149 | } else { |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 150 | color.setToConstant(this->color()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 151 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 152 | switch (fMaskType) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 153 | case kGrayscaleCoverageMask_MaskType: |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 154 | case kAliasedDistanceField_MaskType: |
| 155 | case kGrayscaleDistanceField_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 156 | coverage = GrProcessorAnalysisCoverage::kSingleChannel; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 157 | break; |
| 158 | case kLCDCoverageMask_MaskType: |
| 159 | case kLCDDistanceField_MaskType: |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 160 | case kLCDBGRDistanceField_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 161 | coverage = GrProcessorAnalysisCoverage::kLCD; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 162 | break; |
| 163 | case kColorBitmapMask_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 164 | coverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 165 | break; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 166 | } |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 167 | auto analysis = fProcessors.finalize( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 168 | color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, |
| 169 | clampType, &fGeoData[0].fColor); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 170 | fUsesLocalCoords = analysis.usesLocalCoords(); |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 171 | return analysis; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 174 | static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices, |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 175 | size_t vertexStride, int glyphCount) { |
| 176 | for (int i = 0; i < glyphCount; ++i) { |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 177 | const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices); |
| 178 | const SkPoint* blobPositionRB = |
| 179 | reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 180 | |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 181 | // positions for bitmap glyphs are pixel boundary aligned |
Brian Osman | 7ca90d2 | 2017-11-10 15:31:27 -0500 | [diff] [blame] | 182 | SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX), |
| 183 | SkScalarRoundToInt(blobPositionLT->fY), |
| 184 | SkScalarRoundToInt(blobPositionRB->fX), |
| 185 | SkScalarRoundToInt(blobPositionRB->fY)); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 186 | if (clipRect.contains(positionRect)) { |
| 187 | memcpy(currVertex, blobVertices, 4 * vertexStride); |
| 188 | currVertex += 4 * vertexStride; |
| 189 | } else { |
| 190 | // Pull out some more data that we'll need. |
| 191 | // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords |
| 192 | // and it avoids a lot of conditionals. |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 193 | auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint)); |
Jim Van Verth | 2f2c77a | 2020-01-24 15:48:23 +0000 | [diff] [blame] | 194 | size_t coordOffset = vertexStride - 2*sizeof(uint16_t); |
| 195 | auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset); |
| 196 | auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride + |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 197 | coordOffset); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 198 | // Pull out the texel coordinates and texture index bits |
Jim Van Verth | fb39510 | 2020-02-03 10:11:19 -0500 | [diff] [blame] | 199 | uint16_t coordsRectL = blobCoordsLT[0]; |
| 200 | uint16_t coordsRectT = blobCoordsLT[1]; |
| 201 | uint16_t coordsRectR = blobCoordsRB[0]; |
| 202 | uint16_t coordsRectB = blobCoordsRB[1]; |
| 203 | int index0, index1; |
| 204 | std::tie(coordsRectL, coordsRectT, index0) = |
| 205 | GrDrawOpAtlas::UnpackIndexFromTexCoords(coordsRectL, coordsRectT); |
| 206 | std::tie(coordsRectR, coordsRectB, index1) = |
| 207 | GrDrawOpAtlas::UnpackIndexFromTexCoords(coordsRectR, coordsRectB); |
| 208 | SkASSERT(index0 == index1); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 209 | |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 210 | int positionRectWidth = positionRect.width(); |
| 211 | int positionRectHeight = positionRect.height(); |
| 212 | SkASSERT(positionRectWidth == (coordsRectR - coordsRectL)); |
| 213 | SkASSERT(positionRectHeight == (coordsRectB - coordsRectT)); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 214 | |
| 215 | // Clip position and texCoords to the clipRect |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 216 | unsigned int delta; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 217 | delta = std::min(std::max(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 218 | coordsRectL += delta; |
| 219 | positionRect.fLeft += delta; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 220 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 221 | delta = std::min(std::max(clipRect.fTop - positionRect.fTop, 0), positionRectHeight); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 222 | coordsRectT += delta; |
| 223 | positionRect.fTop += delta; |
| 224 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 225 | delta = std::min(std::max(positionRect.fRight - clipRect.fRight, 0), positionRectWidth); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 226 | coordsRectR -= delta; |
| 227 | positionRect.fRight -= delta; |
| 228 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 229 | delta = std::min(std::max(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 230 | coordsRectB -= delta; |
| 231 | positionRect.fBottom -= delta; |
| 232 | |
| 233 | // Repack texel coordinates and index |
Jim Van Verth | fb39510 | 2020-02-03 10:11:19 -0500 | [diff] [blame] | 234 | std::tie(coordsRectL, coordsRectT) = |
| 235 | GrDrawOpAtlas::PackIndexInTexCoords(coordsRectL, coordsRectT, index0); |
| 236 | std::tie(coordsRectR, coordsRectB) = |
| 237 | GrDrawOpAtlas::PackIndexInTexCoords(coordsRectR, coordsRectB, index1); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 238 | |
| 239 | // Set new positions and coords |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 240 | SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex); |
| 241 | currPosition->fX = positionRect.fLeft; |
| 242 | currPosition->fY = positionRect.fTop; |
| 243 | *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color; |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 244 | uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 245 | currCoords[0] = coordsRectL; |
| 246 | currCoords[1] = coordsRectT; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 247 | currVertex += vertexStride; |
| 248 | |
| 249 | currPosition = reinterpret_cast<SkPoint*>(currVertex); |
| 250 | currPosition->fX = positionRect.fLeft; |
| 251 | currPosition->fY = positionRect.fBottom; |
| 252 | *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color; |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 253 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 254 | currCoords[0] = coordsRectL; |
| 255 | currCoords[1] = coordsRectB; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 256 | currVertex += vertexStride; |
| 257 | |
| 258 | currPosition = reinterpret_cast<SkPoint*>(currVertex); |
| 259 | currPosition->fX = positionRect.fRight; |
| 260 | currPosition->fY = positionRect.fTop; |
| 261 | *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color; |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 262 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 263 | currCoords[0] = coordsRectR; |
| 264 | currCoords[1] = coordsRectT; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 265 | currVertex += vertexStride; |
| 266 | |
| 267 | currPosition = reinterpret_cast<SkPoint*>(currVertex); |
| 268 | currPosition->fX = positionRect.fRight; |
| 269 | currPosition->fY = positionRect.fBottom; |
| 270 | *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color; |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 271 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 272 | currCoords[0] = coordsRectR; |
| 273 | currCoords[1] = coordsRectB; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 274 | currVertex += vertexStride; |
| 275 | } |
| 276 | |
| 277 | blobVertices += 4 * vertexStride; |
| 278 | } |
| 279 | } |
| 280 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 281 | void GrAtlasTextOp::onPrepareDraws(Target* target) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 282 | auto resourceProvider = target->resourceProvider(); |
| 283 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 284 | // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix. |
| 285 | // TODO actually only invert if we don't have RGBA |
| 286 | SkMatrix localMatrix; |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 287 | if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 291 | GrAtlasManager* atlasManager = target->atlasManager(); |
Mike Klein | 99e002f | 2020-01-16 16:45:03 +0000 | [diff] [blame] | 292 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 293 | GrMaskFormat maskFormat = this->maskFormat(); |
| 294 | |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 295 | unsigned int numActiveViews; |
| 296 | const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews); |
| 297 | if (!views) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 298 | SkDebugf("Could not allocate backing texture for atlas\n"); |
| 299 | return; |
| 300 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 301 | SkASSERT(views[0].proxy()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 302 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 303 | static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 304 | static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures); |
| 305 | static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 306 | |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 307 | auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 308 | for (unsigned i = 0; i < numActiveViews; ++i) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 309 | primProcProxies[i] = views[i].proxy(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 310 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies |
| 311 | // don't get added during the visitProxies call. Thus we add them here. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 312 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 313 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 314 | |
| 315 | FlushInfo flushInfo; |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 316 | flushInfo.fPrimProcProxies = primProcProxies; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 317 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 318 | bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective(); |
joshualitt | d9d30f7 | 2015-12-08 10:47:55 -0800 | [diff] [blame] | 319 | if (this->usesDistanceFields()) { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 320 | flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(), |
| 321 | *target->caps().shaderCaps(), |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 322 | views, numActiveViews); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 323 | } else { |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 324 | auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp |
| 325 | : GrSamplerState::Filter::kNearest; |
| 326 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
| 327 | target->allocator(), *target->caps().shaderCaps(), this->color(), false, views, |
| 328 | numActiveViews, filter, maskFormat, localMatrix, vmPerspective); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 331 | int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 332 | |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 333 | // Ensure we don't request an insanely large contiguous vertex allocation. |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 334 | static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize; |
| 335 | const int quadSize = vertexStride * kVerticesPerGlyph; |
| 336 | const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize; |
| 337 | |
| 338 | // Where the quad buffer begins and ends relative to totalGlyphsRegened. |
| 339 | int quadBufferBegin = 0; |
| 340 | int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer); |
| 341 | |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 342 | flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer(); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 343 | void* vertices = target->makeVertexSpace( |
| 344 | vertexStride, |
| 345 | kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin), |
| 346 | &flushInfo.fVertexBuffer, |
| 347 | &flushInfo.fVertexOffset); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 348 | if (!vertices || !flushInfo.fVertexBuffer) { |
| 349 | SkDebugf("Could not allocate vertices\n"); |
| 350 | return; |
| 351 | } |
| 352 | |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 353 | // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and |
| 354 | // quad buffer indices are calculated from this. |
| 355 | int totalGlyphsRegened = 0; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 356 | for (int i = 0; i < fGeoCount; i++) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 357 | const Geometry& args = fGeoData[i]; |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 358 | auto subRun = args.fSubRunPtr; |
| 359 | SkASSERT((int)subRun->vertexStride() == vertexStride); |
| 360 | |
Herb Derby | 62b12fe | 2020-01-14 17:57:24 -0500 | [diff] [blame] | 361 | subRun->updateVerticesColorIfNeeded(args.fColor.toBytes_RGBA()); |
| 362 | subRun->translateVerticesIfNeeded(args.fDrawMatrix, args.fDrawOrigin); |
| 363 | |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 364 | // TODO4F: Preserve float colors |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 365 | GrTextBlob::VertexRegenerator regenerator( |
Herb Derby | 5ddc34c | 2020-02-01 20:38:30 -0500 | [diff] [blame] | 366 | resourceProvider, args.fSubRunPtr, target->deferredUploadTarget(), atlasManager); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 367 | |
| 368 | // Where the subRun begins and ends relative to totalGlyphsRegened. |
| 369 | int subRunBegin = totalGlyphsRegened; |
Robert Phillips | 1576e4e | 2020-04-01 12:49:45 -0400 | [diff] [blame^] | 370 | int subRunEnd = subRunBegin + subRun->fGlyphs.count(); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 371 | |
| 372 | // Draw all the glyphs in the subRun. |
| 373 | while (totalGlyphsRegened < subRunEnd) { |
| 374 | // drawBegin and drawEnd are indices for the subRun on the |
| 375 | // interval [0, subRun->fGlyphs.size()). |
| 376 | int drawBegin = totalGlyphsRegened - subRunBegin; |
| 377 | // drawEnd is either the end of the subRun or the end of the current quad buffer. |
| 378 | int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin; |
| 379 | auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd); |
| 380 | |
| 381 | // There was a problem allocating the glyph in the atlas. Bail. |
Robert Phillips | 1576e4e | 2020-04-01 12:49:45 -0400 | [diff] [blame^] | 382 | if (!ok) { |
| 383 | return; |
| 384 | } |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 385 | |
| 386 | // Update all the vertices for glyphsRegenerate glyphs. |
| 387 | if (glyphsRegenerated > 0) { |
| 388 | int quadBufferIndex = totalGlyphsRegened - quadBufferBegin; |
| 389 | int subRunIndex = totalGlyphsRegened - subRunBegin; |
| 390 | auto regeneratedQuadBuffer = |
| 391 | SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex)); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 392 | if (args.fClipRect.isEmpty()) { |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 393 | memcpy(regeneratedQuadBuffer, |
| 394 | subRun->quadStart(subRunIndex), |
| 395 | glyphsRegenerated * quadSize); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 396 | } else { |
| 397 | SkASSERT(!vmPerspective); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 398 | clip_quads(args.fClipRect, |
| 399 | regeneratedQuadBuffer, |
| 400 | subRun->quadStart(subRunIndex), |
| 401 | vertexStride, |
| 402 | glyphsRegenerated); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 403 | } |
| 404 | if (fNeedsGlyphTransform && !args.fDrawMatrix.isIdentity()) { |
| 405 | // We always do the distance field view matrix transformation after copying |
| 406 | // rather than during blob vertex generation time in the blob as handling |
| 407 | // successive arbitrary transformations would be complicated and accumulate |
| 408 | // error. |
| 409 | if (args.fDrawMatrix.hasPerspective()) { |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 410 | auto* pos = reinterpret_cast<SkPoint3*>(regeneratedQuadBuffer); |
| 411 | SkMatrixPriv::MapHomogeneousPointsWithStride( |
| 412 | args.fDrawMatrix, pos, |
| 413 | vertexStride, pos, |
| 414 | vertexStride, |
| 415 | glyphsRegenerated * kVerticesPerGlyph); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 416 | } else { |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 417 | auto* pos = reinterpret_cast<SkPoint*>(regeneratedQuadBuffer); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 418 | SkMatrixPriv::MapPointsWithStride(args.fDrawMatrix, pos, vertexStride, |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 419 | glyphsRegenerated * kVerticesPerGlyph); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 420 | } |
| 421 | } |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 422 | } |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 423 | |
| 424 | totalGlyphsRegened += glyphsRegenerated; |
| 425 | flushInfo.fGlyphsToFlush += glyphsRegenerated; |
| 426 | |
| 427 | // regenerate() has stopped part way through a SubRun. This means that either the atlas |
| 428 | // or the quad buffer is full or both. There is a case were the flow through |
| 429 | // the loop is strange. If we run out of quad buffer space at the same time the |
| 430 | // SubRun ends, then this is not triggered which is the right result for the last |
| 431 | // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which |
| 432 | // will process no glyphs, and return to this point where the quad buffer will be |
| 433 | // expanded. |
| 434 | if (totalGlyphsRegened != subRunEnd) { |
| 435 | // Flush if not all glyphs drawn because either the quad buffer is full or the |
| 436 | // atlas is out of space. |
Herb Derby | 4513cdd | 2020-01-31 13:28:49 -0500 | [diff] [blame] | 437 | this->createDrawForGeneratedGlyphs(target, &flushInfo); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 438 | if (totalGlyphsRegened == quadBufferEnd) { |
| 439 | // Quad buffer is full. Get more buffer. |
| 440 | quadBufferBegin = totalGlyphsRegened; |
| 441 | int quadBufferSize = |
| 442 | std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened); |
| 443 | quadBufferEnd = quadBufferBegin + quadBufferSize; |
| 444 | |
| 445 | vertices = target->makeVertexSpace( |
| 446 | vertexStride, |
| 447 | kVerticesPerGlyph * quadBufferSize, |
| 448 | &flushInfo.fVertexBuffer, |
| 449 | &flushInfo.fVertexOffset); |
| 450 | if (!vertices || !flushInfo.fVertexBuffer) { |
| 451 | SkDebugf("Could not allocate vertices\n"); |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
Herb Derby | 5f6f851 | 2020-01-10 12:50:35 -0500 | [diff] [blame] | 457 | } // for all geometries |
Herb Derby | 4513cdd | 2020-01-31 13:28:49 -0500 | [diff] [blame] | 458 | this->createDrawForGeneratedGlyphs(target, &flushInfo); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 461 | void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 462 | auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, |
| 463 | std::move(fProcessors), |
| 464 | GrPipeline::InputFlags::kNone); |
| 465 | |
| 466 | flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Herb Derby | 4513cdd | 2020-01-31 13:28:49 -0500 | [diff] [blame] | 469 | void GrAtlasTextOp::createDrawForGeneratedGlyphs( |
| 470 | GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 471 | if (!flushInfo->fGlyphsToFlush) { |
| 472 | return; |
| 473 | } |
| 474 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 475 | auto atlasManager = target->atlasManager(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 476 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 477 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor; |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 478 | GrMaskFormat maskFormat = this->maskFormat(); |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 479 | |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 480 | unsigned int numActiveViews; |
| 481 | const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews); |
| 482 | SkASSERT(views); |
Jim Van Verth | 9f2516f | 2019-11-22 14:58:37 -0500 | [diff] [blame] | 483 | // Something has gone terribly wrong, bail |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 484 | if (!views || 0 == numActiveViews) { |
Jim Van Verth | 9f2516f | 2019-11-22 14:58:37 -0500 | [diff] [blame] | 485 | return; |
| 486 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 487 | if (gp->numTextureSamplers() != (int) numActiveViews) { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 488 | // During preparation the number of atlas pages has increased. |
| 489 | // Update the proxies used in the GP to match. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 490 | for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 491 | flushInfo->fPrimProcProxies[i] = views[i].proxy(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 492 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 493 | // proxies don't get added during the visitProxies call. Thus we add them here. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 494 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 495 | // These will get unreffed when the previously recorded draws destruct. |
| 496 | for (int d = 0; d < flushInfo->fNumDraws; ++d) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 497 | flushInfo->fPrimProcProxies[i]->ref(); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 498 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 499 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 500 | if (this->usesDistanceFields()) { |
| 501 | if (this->isLCD()) { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 502 | reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews( |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 503 | views, numActiveViews, GrSamplerState::Filter::kBilerp); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 504 | } else { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 505 | reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews( |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 506 | views, numActiveViews, GrSamplerState::Filter::kBilerp); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 507 | } |
| 508 | } else { |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 509 | auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp |
| 510 | : GrSamplerState::Filter::kNearest; |
| 511 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 512 | } |
| 513 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 514 | int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6); |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 515 | GrSimpleMesh* mesh = target->allocMesh(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 516 | mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush, |
| 517 | maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph, |
| 518 | flushInfo->fVertexOffset); |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 519 | target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies, |
Chris Dalton | 3bf2f3a | 2020-03-17 11:48:23 -0600 | [diff] [blame] | 520 | GrPrimitiveType::kTriangles); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 521 | flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush; |
| 522 | flushInfo->fGlyphsToFlush = 0; |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 523 | ++flushInfo->fNumDraws; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 526 | GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 527 | const GrCaps& caps) { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 528 | GrAtlasTextOp* that = t->cast<GrAtlasTextOp>(); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 529 | if (fProcessors != that->fProcessors) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 530 | return CombineResult::kCannotCombine; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 531 | } |
| 532 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 533 | if (fMaskType != that->fMaskType) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 534 | return CombineResult::kCannotCombine; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 537 | const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix; |
| 538 | const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 539 | |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 540 | if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 541 | return CombineResult::kCannotCombine; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 544 | if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 545 | return CombineResult::kCannotCombine; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | if (fNeedsGlyphTransform && |
| 549 | (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 550 | return CombineResult::kCannotCombine; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 553 | if (this->usesDistanceFields()) { |
| 554 | if (fDFGPFlags != that->fDFGPFlags) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 555 | return CombineResult::kCannotCombine; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 558 | if (fLuminanceColor != that->fLuminanceColor) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 559 | return CombineResult::kCannotCombine; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 560 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 561 | } else { |
| 562 | if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 563 | return CombineResult::kCannotCombine; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 564 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 567 | fNumGlyphs += that->numGlyphs(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 568 | |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 569 | // Reallocate space for geo data if necessary and then import that geo's data. |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 570 | int newGeoCount = that->fGeoCount + fGeoCount; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 571 | |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 572 | // We reallocate at a rate of 1.5x to try to get better total memory usage |
| 573 | if (newGeoCount > fGeoDataAllocSize) { |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 574 | int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2; |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 575 | while (newAllocSize < newGeoCount) { |
| 576 | newAllocSize += newAllocSize / 2; |
| 577 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 578 | fGeoData.realloc(newAllocSize); |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 579 | fGeoDataAllocSize = newAllocSize; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 582 | // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 583 | // it doesn't try to unref them. |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 584 | memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry)); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 585 | #ifdef SK_DEBUG |
| 586 | for (int i = 0; i < that->fGeoCount; ++i) { |
Herb Derby | c514e7d | 2019-12-11 17:00:31 -0500 | [diff] [blame] | 587 | that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 588 | } |
| 589 | #endif |
| 590 | that->fGeoCount = 0; |
| 591 | fGeoCount = newGeoCount; |
| 592 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 593 | return CombineResult::kMerged; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 594 | } |
| 595 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 596 | // TODO trying to figure out why lcd is so whack |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 597 | // (see comments in GrTextContext::ComputeCanonicalColor) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 598 | GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena, |
| 599 | const GrShaderCaps& caps, |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 600 | const GrSurfaceProxyView* views, |
| 601 | unsigned int numActiveViews) const { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 602 | bool isLCD = this->isLCD(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 603 | |
| 604 | SkMatrix localMatrix = SkMatrix::I(); |
| 605 | if (this->usesLocalCoords()) { |
| 606 | // If this fails we'll just use I(). |
Herb Derby | 1c5be7b | 2019-12-13 12:03:06 -0500 | [diff] [blame] | 607 | bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 608 | (void)result; |
| 609 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 610 | |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 611 | auto dfAdjustTable = GrDistanceFieldAdjustTable::Get(); |
| 612 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 613 | // see if we need to create a new effect |
| 614 | if (isLCD) { |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 615 | float redCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 616 | SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 617 | fUseGammaCorrectDistanceTable); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 618 | float greenCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 619 | SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 620 | fUseGammaCorrectDistanceTable); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 621 | float blueCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 622 | SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 623 | fUseGammaCorrectDistanceTable); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 624 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust = |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 625 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make( |
| 626 | redCorrection, greenCorrection, blueCorrection); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 627 | return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 628 | GrSamplerState::Filter::kBilerp, widthAdjust, |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 629 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 630 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 631 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 632 | float correction = 0; |
| 633 | if (kAliasedDistanceField_MaskType != fMaskType) { |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 634 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, |
| 635 | fLuminanceColor); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 636 | correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift, |
| 637 | fUseGammaCorrectDistanceTable); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 638 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 639 | return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 640 | GrSamplerState::Filter::kBilerp, correction, |
| 641 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 642 | #else |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 643 | return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 644 | GrSamplerState::Filter::kBilerp, |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 645 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 646 | #endif |
| 647 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 648 | } |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 649 | |