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