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