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