blob: 93aee4132a7edbe7a9dc39a8da5c55a16d8b7847 [file] [log] [blame]
joshualitta751c972015-11-20 13:37:32 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrAtlasTextOp.h"
Robert Phillipse4fda6c2018-02-21 12:10:41 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint3.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkMathPriv.h"
13#include "src/core/SkMatrixPriv.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040014#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkStrikeCache.h"
16#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrMemoryPool.h"
18#include "src/gpu/GrOpFlushState.h"
19#include "src/gpu/GrRecordingContextPriv.h"
Herb Derby4598fa12020-06-10 14:54:22 -040020#include "src/gpu/GrRenderTargetContext.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040021#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrResourceProvider.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040023#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/effects/GrBitmapTextGeoProc.h"
25#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050026#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040028#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080029
Herb Derbya08bde62020-06-12 15:46:06 -040030#if GR_TEST_UTILS
31#include "src/gpu/GrDrawOpTest.h"
32#endif
33
joshualitt60ce86d2015-11-23 13:08:22 -080034///////////////////////////////////////////////////////////////////////////////////////////////////
35
Herb Derby3c873af2020-04-29 15:56:07 -040036GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
37 GrPaint&& paint,
38 GrTextBlob::SubRun* subrun,
39 const SkMatrix& drawMatrix,
40 SkPoint drawOrigin,
41 const SkIRect& clipRect,
42 const SkPMColor4f& filteredColor,
43 SkColor luminanceColor,
44 bool useGammaCorrectDistanceTable,
45 uint32_t DFGPFlags)
46 : INHERITED(ClassID())
47 , fMaskType{maskType}
48 , fNeedsGlyphTransform{subrun->needsTransform()}
49 , fLuminanceColor{luminanceColor}
50 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
51 , fDFGPFlags{DFGPFlags}
52 , fGeoDataAllocSize{kMinGeometryAllocated}
53 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040054 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040055 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
56
57 // Unref handled in ~GrAtlasTextOp().
58 geometry.fBlob = SkRef(subrun->fBlob);
59 geometry.fSubRunPtr = subrun;
60 geometry.fDrawMatrix = drawMatrix;
61 geometry.fDrawOrigin = drawOrigin;
62 geometry.fClipRect = clipRect;
63 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
64 : filteredColor;
65 fGeoCount = 1;
66
67 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
68 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
69 // we treat this as a set of non-AA rects rendered with a texture.
70 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
71}
72
Herb Derby64391c42020-05-16 14:32:15 -040073void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
74 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
75 fDrawMatrix, fDrawOrigin, fClipRect);
76}
77
Herb Derby7a1d9422020-07-06 14:18:01 -040078static SkPMColor4f generate_filtered_color(const SkPaint& paint, const GrColorInfo& colorInfo) {
79 SkColor4f c = paint.getColor4f();
80 if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) {
81 c = xform->apply(c);
82 }
83 if (auto* cf = paint.getColorFilter()) {
84 c = cf->filterColor4f(c, colorInfo.colorSpace(), colorInfo.colorSpace());
85 }
86 return c.premul();
87}
88
Herb Derby786c5b22020-06-09 15:33:43 -040089std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRenderTargetContext* rtc,
Herb Derby7a1d9422020-07-06 14:18:01 -040090 const SkPaint& paint,
Herb Derby3c873af2020-04-29 15:56:07 -040091 GrTextBlob::SubRun* subrun,
Herb Derby7a1d9422020-07-06 14:18:01 -040092 const SkMatrixProvider& matrixProvider,
Herb Derby3c873af2020-04-29 15:56:07 -040093 SkPoint drawOrigin,
Herb Derby7a1d9422020-07-06 14:18:01 -040094 const SkIRect& clipRect) {
95 GrPaint grPaint;
96 GrRecordingContext* context = rtc->priv().getContext();
97 const GrColorInfo& colorInfo = rtc->colorInfo();
98 if (kARGB_GrMaskFormat == subrun->maskFormat()) {
99 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, paint, matrixProvider, &grPaint);
100 } else {
101 SkPaintToGrPaint(context, colorInfo, paint, matrixProvider, &grPaint);
102 }
103
104 // This is the color the op will use to draw.
105 SkPMColor4f drawingColor = generate_filtered_color(paint, colorInfo);
106
107 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400108
Herb Derby3c873af2020-04-29 15:56:07 -0400109 MaskType maskType = [&]() {
110 switch (subrun->maskFormat()) {
111 case kA8_GrMaskFormat: return kGrayscaleCoverageMask_MaskType;
112 case kA565_GrMaskFormat: return kLCDCoverageMask_MaskType;
113 case kARGB_GrMaskFormat: return kColorBitmapMask_MaskType;
114 // Needed to placate some compilers.
115 default: return kGrayscaleCoverageMask_MaskType;
Robert Phillips7c525e62018-06-12 10:11:12 -0400116 }
Herb Derby3c873af2020-04-29 15:56:07 -0400117 }();
118
119 return pool->allocate<GrAtlasTextOp>(maskType,
Herb Derby7a1d9422020-07-06 14:18:01 -0400120 std::move(grPaint),
Herb Derby3c873af2020-04-29 15:56:07 -0400121 subrun,
Herb Derby7a1d9422020-07-06 14:18:01 -0400122 matrixProvider.localToDevice(),
Herb Derby3c873af2020-04-29 15:56:07 -0400123 drawOrigin,
124 clipRect,
Herb Derby7a1d9422020-07-06 14:18:01 -0400125 drawingColor,
Herb Derby3c873af2020-04-29 15:56:07 -0400126 0,
127 false,
128 0);
129}
Robert Phillips7c525e62018-06-12 10:11:12 -0400130
131std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Herb Derby7a1d9422020-07-06 14:18:01 -0400132 GrRenderTargetContext* rtc,
133 const SkPaint& paint,
134 GrTextBlob::SubRun* subrun,
135 const SkMatrixProvider& matrixProvider,
136 SkPoint drawOrigin,
137 const SkIRect& clipRect) {
138 GrPaint grPaint;
139 GrRecordingContext* context = rtc->priv().getContext();
140 const GrColorInfo& colorInfo = rtc->colorInfo();
141 SkPaintToGrPaint(context, colorInfo, paint, matrixProvider, &grPaint);
142
143 // This is the color the op will use to draw.
144 SkPMColor4f drawingColor = generate_filtered_color(paint, colorInfo);
145
146 const SkSurfaceProps& props = rtc->surfaceProps();
147 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Herb Derby3c873af2020-04-29 15:56:07 -0400148 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
149 bool isLCD = subrun->hasUseLCDText() && SkPixelGeometryIsH(props.pixelGeometry());
150 MaskType maskType = !subrun->isAntiAliased() ? kAliasedDistanceField_MaskType
151 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
152 : kLCDDistanceField_MaskType)
153 : kGrayscaleDistanceField_MaskType;
Robert Phillipsc994a932018-06-19 13:09:54 -0400154
Herb Derby7a1d9422020-07-06 14:18:01 -0400155 const SkMatrix& drawMatrix = matrixProvider.localToDevice();
156 bool useGammaCorrectDistanceTable = colorInfo.isLinearlyBlended();
Herb Derby3c873af2020-04-29 15:56:07 -0400157 uint32_t DFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
158 DFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
159 DFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
160 DFGPFlags |= useGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
161 DFGPFlags |= kAliasedDistanceField_MaskType == maskType ? kAliased_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400162
Herb Derby3c873af2020-04-29 15:56:07 -0400163 if (isLCD) {
164 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
165 DFGPFlags |= kLCDBGRDistanceField_MaskType == maskType ? kBGR_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400166 }
167
Herb Derby3c873af2020-04-29 15:56:07 -0400168 return pool->allocate<GrAtlasTextOp>(maskType,
Herb Derby7a1d9422020-07-06 14:18:01 -0400169 std::move(grPaint),
Herb Derby3c873af2020-04-29 15:56:07 -0400170 subrun,
171 drawMatrix,
172 drawOrigin,
173 clipRect,
Herb Derby7a1d9422020-07-06 14:18:01 -0400174 drawingColor,
175 SkPaintPriv::ComputeLuminanceColor(paint),
Herb Derby3c873af2020-04-29 15:56:07 -0400176 useGammaCorrectDistanceTable,
177 DFGPFlags);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500178}
179
Chris Dalton1706cbf2019-05-21 19:35:29 -0600180void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500181 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500182}
183
Brian Osman9a390ac2018-11-12 09:47:48 -0500184#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500185SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800186 SkString str;
187
188 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500189 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800190 i,
Brian Osmancf860852018-10-31 14:04:39 -0400191 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500192 fGeoData[i].fDrawOrigin.x(),
193 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800194 }
195
Brian Salomon44acb5b2017-07-18 19:59:24 -0400196 str += fProcessors.dumpProcessors();
197 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800198 return str;
199}
Brian Osman9a390ac2018-11-12 09:47:48 -0500200#endif
joshualitta751c972015-11-20 13:37:32 -0800201
Brian Salomon44acb5b2017-07-18 19:59:24 -0400202GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
203 return FixedFunctionFlags::kNone;
204}
205
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206GrProcessorSet::Analysis GrAtlasTextOp::finalize(
207 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
208 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400209 GrProcessorAnalysisCoverage coverage;
210 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800211 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400212 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800213 } else {
Brian Osman09068252018-01-03 09:57:29 -0500214 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800215 }
joshualitta751c972015-11-20 13:37:32 -0800216 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800217 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400218 case kAliasedDistanceField_MaskType:
219 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400220 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800221 break;
222 case kLCDCoverageMask_MaskType:
223 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400224 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400225 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800226 break;
227 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400228 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400229 break;
joshualitta751c972015-11-20 13:37:32 -0800230 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700231 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600232 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
233 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400234 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700235 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800236}
237
Brian Salomon91326c32017-08-09 16:02:19 -0400238void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500239 auto resourceProvider = target->resourceProvider();
240
joshualitta751c972015-11-20 13:37:32 -0800241 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
242 // TODO actually only invert if we don't have RGBA
243 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500244 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800245 return;
246 }
247
Robert Phillips5a66efb2018-03-07 15:13:18 -0500248 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000249
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400250 GrMaskFormat maskFormat = this->maskFormat();
251
Greg Daniel9715b6c2019-12-10 15:03:10 -0500252 unsigned int numActiveViews;
253 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
254 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800255 SkDebugf("Could not allocate backing texture for atlas\n");
256 return;
257 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500258 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800259
Brian Salomon7eae3e02018-08-07 14:02:38 +0000260 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500261 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
262 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000263
Chris Dalton304e14d2020-03-17 14:29:06 -0600264 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500265 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600266 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400267 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
268 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500269 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000270 }
Brian Salomon49348902018-06-26 09:12:38 -0400271
272 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600273 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400274
Herb Derby1c5be7b2019-12-13 12:03:06 -0500275 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800276 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500277 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
278 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500279 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800280 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500281 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
282 : GrSamplerState::Filter::kNearest;
283 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
284 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
285 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800286 }
287
Herb Derby23f29762020-01-10 16:26:14 -0500288 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800289
Brian Salomon43cbd722020-01-03 22:09:12 -0500290 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500291 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
292 const int quadSize = vertexStride * kVerticesPerGlyph;
293 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
294
295 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
296 int quadBufferBegin = 0;
297 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
298
Robert Phillipsee08d522019-10-28 16:34:44 -0400299 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500300 void* vertices = target->makeVertexSpace(
301 vertexStride,
302 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
303 &flushInfo.fVertexBuffer,
304 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800305 if (!vertices || !flushInfo.fVertexBuffer) {
306 SkDebugf("Could not allocate vertices\n");
307 return;
308 }
309
Herb Derby23f29762020-01-10 16:26:14 -0500310 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
311 // quad buffer indices are calculated from this.
312 int totalGlyphsRegened = 0;
joshualitta751c972015-11-20 13:37:32 -0800313 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800314 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500315 auto subRun = args.fSubRunPtr;
316 SkASSERT((int)subRun->vertexStride() == vertexStride);
317
Robert Phillips207d24b2020-04-09 10:23:42 -0400318 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500319
Brian Osman1be2b7c2018-10-29 16:07:15 -0400320 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400321 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
322 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500323
324 // Where the subRun begins and ends relative to totalGlyphsRegened.
325 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400326 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500327
328 // Draw all the glyphs in the subRun.
329 while (totalGlyphsRegened < subRunEnd) {
330 // drawBegin and drawEnd are indices for the subRun on the
331 // interval [0, subRun->fGlyphs.size()).
332 int drawBegin = totalGlyphsRegened - subRunBegin;
333 // drawEnd is either the end of the subRun or the end of the current quad buffer.
334 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
335 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
336
337 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400338 if (!ok) {
339 return;
340 }
Herb Derby23f29762020-01-10 16:26:14 -0500341
342 // Update all the vertices for glyphsRegenerate glyphs.
343 if (glyphsRegenerated > 0) {
344 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500345 auto regeneratedQuadBuffer =
346 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400347 int subRunIndex = totalGlyphsRegened - subRunBegin;
348 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500349 }
Herb Derby23f29762020-01-10 16:26:14 -0500350
351 totalGlyphsRegened += glyphsRegenerated;
352 flushInfo.fGlyphsToFlush += glyphsRegenerated;
353
354 // regenerate() has stopped part way through a SubRun. This means that either the atlas
355 // or the quad buffer is full or both. There is a case were the flow through
356 // the loop is strange. If we run out of quad buffer space at the same time the
357 // SubRun ends, then this is not triggered which is the right result for the last
358 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
359 // will process no glyphs, and return to this point where the quad buffer will be
360 // expanded.
361 if (totalGlyphsRegened != subRunEnd) {
362 // Flush if not all glyphs drawn because either the quad buffer is full or the
363 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500364 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500365 if (totalGlyphsRegened == quadBufferEnd) {
366 // Quad buffer is full. Get more buffer.
367 quadBufferBegin = totalGlyphsRegened;
368 int quadBufferSize =
369 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
370 quadBufferEnd = quadBufferBegin + quadBufferSize;
371
372 vertices = target->makeVertexSpace(
373 vertexStride,
374 kVerticesPerGlyph * quadBufferSize,
375 &flushInfo.fVertexBuffer,
376 &flushInfo.fVertexOffset);
377 if (!vertices || !flushInfo.fVertexBuffer) {
378 SkDebugf("Could not allocate vertices\n");
379 return;
380 }
381 }
382 }
383 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500384 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500385 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800386}
387
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700388void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500389 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
390 std::move(fProcessors),
391 GrPipeline::InputFlags::kNone);
392
393 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700394}
395
Herb Derby4513cdd2020-01-31 13:28:49 -0500396void GrAtlasTextOp::createDrawForGeneratedGlyphs(
397 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500398 if (!flushInfo->fGlyphsToFlush) {
399 return;
400 }
401
Robert Phillips5a66efb2018-03-07 15:13:18 -0500402 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500403
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500404 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400405 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500406
Greg Daniel9715b6c2019-12-10 15:03:10 -0500407 unsigned int numActiveViews;
408 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
409 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500410 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500411 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500412 return;
413 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500414 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400415 // During preparation the number of atlas pages has increased.
416 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500417 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600418 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400419 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
420 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500421 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500422 // These will get unreffed when the previously recorded draws destruct.
423 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600424 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500425 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000426 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400427 if (this->usesDistanceFields()) {
428 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500429 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500430 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400431 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500432 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500433 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400434 }
435 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500436 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
437 : GrSamplerState::Filter::kNearest;
438 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400439 }
440 }
Brian Salomondbf70722019-02-07 11:31:24 -0500441 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600442 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600443 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
444 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
445 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600446 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600447 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800448 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
449 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500450 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800451}
452
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500453GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
454 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500455 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400456 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000457 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400458 }
459
joshualitta751c972015-11-20 13:37:32 -0800460 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000461 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800462 }
463
Herb Derby1c5be7b2019-12-13 12:03:06 -0500464 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
465 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500466
Mike Reed2c383152019-12-18 16:47:47 -0500467 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000468 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500469 }
470
Jim Van Verthb515ae72018-05-23 16:44:55 -0400471 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000472 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400473 }
474
475 if (fNeedsGlyphTransform &&
476 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000477 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400478 }
479
Brian Salomon5c6ac642017-12-19 11:09:32 -0500480 if (this->usesDistanceFields()) {
481 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000482 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800483 }
484
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400485 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000486 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800487 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500488 } else {
489 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000490 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500491 }
joshualitta751c972015-11-20 13:37:32 -0800492 }
493
Brian Salomon344ec422016-12-15 10:58:41 -0500494 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800495
Jim Van Verth56c37142017-10-31 14:44:25 -0400496 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800497 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800498
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400499 // We reallocate at a rate of 1.5x to try to get better total memory usage
500 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400501 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400502 while (newAllocSize < newGeoCount) {
503 newAllocSize += newAllocSize / 2;
504 }
joshualitta751c972015-11-20 13:37:32 -0800505 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400506 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800507 }
508
Brian Salomon344ec422016-12-15 10:58:41 -0500509 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
joshualitta751c972015-11-20 13:37:32 -0800510 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500511 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800512#ifdef SK_DEBUG
513 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500514 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800515 }
516#endif
517 that->fGeoCount = 0;
518 fGeoCount = newGeoCount;
519
Brian Salomon7eae3e02018-08-07 14:02:38 +0000520 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800521}
522
Herb Derby3c873af2020-04-29 15:56:07 -0400523static const int kDistanceAdjustLumShift = 5;
524
joshualitta751c972015-11-20 13:37:32 -0800525// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500526GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
527 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500528 const GrSurfaceProxyView* views,
529 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800530 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500531
532 SkMatrix localMatrix = SkMatrix::I();
533 if (this->usesLocalCoords()) {
534 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500535 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500536 (void)result;
537 }
joshualitta751c972015-11-20 13:37:32 -0800538
Robert Phillips841c9a52020-03-27 12:41:31 -0400539 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
540
joshualitta751c972015-11-20 13:37:32 -0800541 // see if we need to create a new effect
542 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400543 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400544 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500545 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400546 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400547 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500548 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400549 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400550 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500551 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800552 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500553 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
554 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500555 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500556 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500557 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800558 } else {
joshualitta751c972015-11-20 13:37:32 -0800559#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400560 float correction = 0;
561 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400562 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
563 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400564 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
565 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400566 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500567 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500568 GrSamplerState::Filter::kBilerp, correction,
569 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800570#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500571 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500572 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500573 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800574#endif
575 }
joshualitta751c972015-11-20 13:37:32 -0800576}
joshualittddd22d82016-02-16 06:47:52 -0800577
Herb Derby4598fa12020-06-10 14:54:22 -0400578#if GR_TEST_UTILS
579std::unique_ptr<GrDrawOp> GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
580 const SkPaint& skPaint,
581 const SkFont& font,
582 const SkMatrixProvider& mtxProvider,
583 const char* text,
584 int x,
585 int y) {
586 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
587
588 size_t textLen = (int)strlen(text);
589
590 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
591
592 auto drawOrigin = SkPoint::Make(x, y);
593 SkGlyphRunBuilder builder;
594 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
595
596 auto glyphRunList = builder.useGlyphRunList();
597
598 const GrRecordingContextPriv& contextPriv = rtc->fContext->priv();
Herb Derbya08bde62020-06-12 15:46:06 -0400599 GrSDFTOptions SDFOptions = rtc->fContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400600
601 if (glyphRunList.empty()) {
602 return nullptr;
603 }
604 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
605 SkGlyphRunListPainter* painter = &rtc->fGlyphPainter;
606 painter->processGlyphRunList(
607 glyphRunList, drawMatrix, surfaceProps,
608 contextPriv.caps()->shaderCaps()->supportsDistanceFieldText(),
609 SDFOptions, blob.get());
610
611 return blob->firstSubRun()->makeOp(mtxProvider,
612 drawOrigin,
613 SkIRect::MakeEmpty(),
614 skPaint,
615 surfaceProps,
616 rtc->textTarget());
617}
618
619GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
620 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
621 auto rtc = GrRenderTargetContext::Make(
622 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
623
624 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
625
626 SkPaint skPaint;
627 skPaint.setColor(random->nextU());
628
629 SkFont font;
630 if (random->nextBool()) {
631 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
632 } else {
633 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
634 }
635 font.setSubpixel(random->nextBool());
636
637 const char* text = "The quick brown fox jumps over the lazy dog.";
638
639 // create some random x/y offsets, including negative offsets
640 static const int kMaxTrans = 1024;
641 int xPos = (random->nextU() % 2) * 2 - 1;
642 int yPos = (random->nextU() % 2) * 2 - 1;
643 int xInt = (random->nextU() % kMaxTrans) * xPos;
644 int yInt = (random->nextU() % kMaxTrans) * yPos;
645
646 return GrAtlasTextOp::CreateOpTestingOnly(
647 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
648}
649
650#endif
651
652