blob: 4e4e7f7e2e624ab2a8ae10b8d84b913aab8fd53f [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"
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 Derby4598fa12020-06-10 14:54:22 -040019#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceProvider.h"
21#include "src/gpu/effects/GrBitmapTextGeoProc.h"
22#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050023#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040025#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080026
Herb Derbya08bde62020-06-12 15:46:06 -040027#if GR_TEST_UTILS
28#include "src/gpu/GrDrawOpTest.h"
29#endif
30
joshualitt60ce86d2015-11-23 13:08:22 -080031///////////////////////////////////////////////////////////////////////////////////////////////////
32
Herb Derby3c873af2020-04-29 15:56:07 -040033GrAtlasTextOp::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 Derbyd5cbc1e2020-05-16 13:45:55 -040051 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040052 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 Derby64391c42020-05-16 14:32:15 -040070void 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 Phillipsb97da532019-02-12 15:24:12 -050075std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040076 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -040077 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 Phillipsc994a932018-06-19 13:09:54 -040083
Herb Derby3c873af2020-04-29 15:56:07 -040084 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 Phillips7c525e62018-06-12 10:11:12 -040091 }
Herb Derby3c873af2020-04-29 15:56:07 -040092 }();
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 Phillips7c525e62018-06-12 10:11:12 -0400105
106std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Robert Phillipsb97da532019-02-12 15:24:12 -0500107 GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400108 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -0400109 GrTextBlob::SubRun* subrun,
110 const SkMatrix& drawMatrix,
111 SkPoint drawOrigin,
112 const SkIRect& clipRect,
113 const SkPMColor4f& filteredColor,
Robert Phillips7c525e62018-06-12 10:11:12 -0400114 bool useGammaCorrectDistanceTable,
115 SkColor luminanceColor,
Herb Derby3c873af2020-04-29 15:56:07 -0400116 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 Phillipsc994a932018-06-19 13:09:54 -0400124
Herb Derby3c873af2020-04-29 15:56:07 -0400125 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 Phillips7c525e62018-06-12 10:11:12 -0400130
Herb Derby3c873af2020-04-29 15:56:07 -0400131 if (isLCD) {
132 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
133 DFGPFlags |= kLCDBGRDistanceField_MaskType == maskType ? kBGR_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400134 }
135
Herb Derby3c873af2020-04-29 15:56:07 -0400136 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 Salomon5c6ac642017-12-19 11:09:32 -0500146}
147
Chris Dalton1706cbf2019-05-21 19:35:29 -0600148void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500149 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500150}
151
Brian Osman9a390ac2018-11-12 09:47:48 -0500152#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500153SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800154 SkString str;
155
156 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500157 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800158 i,
Brian Osmancf860852018-10-31 14:04:39 -0400159 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500160 fGeoData[i].fDrawOrigin.x(),
161 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800162 }
163
Brian Salomon44acb5b2017-07-18 19:59:24 -0400164 str += fProcessors.dumpProcessors();
165 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800166 return str;
167}
Brian Osman9a390ac2018-11-12 09:47:48 -0500168#endif
joshualitta751c972015-11-20 13:37:32 -0800169
Brian Salomon44acb5b2017-07-18 19:59:24 -0400170GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
171 return FixedFunctionFlags::kNone;
172}
173
Chris Dalton6ce447a2019-06-23 18:07:38 -0600174GrProcessorSet::Analysis GrAtlasTextOp::finalize(
175 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
176 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400177 GrProcessorAnalysisCoverage coverage;
178 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800179 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400180 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800181 } else {
Brian Osman09068252018-01-03 09:57:29 -0500182 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800183 }
joshualitta751c972015-11-20 13:37:32 -0800184 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800185 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400186 case kAliasedDistanceField_MaskType:
187 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400188 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800189 break;
190 case kLCDCoverageMask_MaskType:
191 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400192 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400193 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800194 break;
195 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400196 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400197 break;
joshualitta751c972015-11-20 13:37:32 -0800198 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700199 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600200 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
201 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400202 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700203 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800204}
205
Brian Salomon91326c32017-08-09 16:02:19 -0400206void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500207 auto resourceProvider = target->resourceProvider();
208
joshualitta751c972015-11-20 13:37:32 -0800209 // 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 Derby1c5be7b2019-12-13 12:03:06 -0500212 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800213 return;
214 }
215
Robert Phillips5a66efb2018-03-07 15:13:18 -0500216 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000217
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400218 GrMaskFormat maskFormat = this->maskFormat();
219
Greg Daniel9715b6c2019-12-10 15:03:10 -0500220 unsigned int numActiveViews;
221 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
222 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800223 SkDebugf("Could not allocate backing texture for atlas\n");
224 return;
225 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500226 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800227
Brian Salomon7eae3e02018-08-07 14:02:38 +0000228 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500229 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
230 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000231
Chris Dalton304e14d2020-03-17 14:29:06 -0600232 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500233 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600234 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400235 // 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 Daniel9715b6c2019-12-10 15:03:10 -0500237 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000238 }
Brian Salomon49348902018-06-26 09:12:38 -0400239
240 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600241 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400242
Herb Derby1c5be7b2019-12-13 12:03:06 -0500243 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800244 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500245 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
246 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500247 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800248 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500249 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);
joshualitta751c972015-11-20 13:37:32 -0800254 }
255
Herb Derby23f29762020-01-10 16:26:14 -0500256 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800257
Brian Salomon43cbd722020-01-03 22:09:12 -0500258 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500259 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 Phillipsee08d522019-10-28 16:34:44 -0400267 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500268 void* vertices = target->makeVertexSpace(
269 vertexStride,
270 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
271 &flushInfo.fVertexBuffer,
272 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800273 if (!vertices || !flushInfo.fVertexBuffer) {
274 SkDebugf("Could not allocate vertices\n");
275 return;
276 }
277
Herb Derby23f29762020-01-10 16:26:14 -0500278 // 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;
joshualitta751c972015-11-20 13:37:32 -0800281 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800282 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500283 auto subRun = args.fSubRunPtr;
284 SkASSERT((int)subRun->vertexStride() == vertexStride);
285
Robert Phillips207d24b2020-04-09 10:23:42 -0400286 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500287
Brian Osman1be2b7c2018-10-29 16:07:15 -0400288 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400289 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
290 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500291
292 // Where the subRun begins and ends relative to totalGlyphsRegened.
293 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400294 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500295
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 Phillips1576e4e2020-04-01 12:49:45 -0400306 if (!ok) {
307 return;
308 }
Herb Derby23f29762020-01-10 16:26:14 -0500309
310 // Update all the vertices for glyphsRegenerate glyphs.
311 if (glyphsRegenerated > 0) {
312 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500313 auto regeneratedQuadBuffer =
314 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400315 int subRunIndex = totalGlyphsRegened - subRunBegin;
316 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500317 }
Herb Derby23f29762020-01-10 16:26:14 -0500318
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 Derby4513cdd2020-01-31 13:28:49 -0500332 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500333 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 Derby5f6f8512020-01-10 12:50:35 -0500352 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500353 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800354}
355
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700356void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500357 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
358 std::move(fProcessors),
359 GrPipeline::InputFlags::kNone);
360
361 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700362}
363
Herb Derby4513cdd2020-01-31 13:28:49 -0500364void GrAtlasTextOp::createDrawForGeneratedGlyphs(
365 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500366 if (!flushInfo->fGlyphsToFlush) {
367 return;
368 }
369
Robert Phillips5a66efb2018-03-07 15:13:18 -0500370 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500371
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500372 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400373 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500374
Greg Daniel9715b6c2019-12-10 15:03:10 -0500375 unsigned int numActiveViews;
376 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
377 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500378 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500379 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500380 return;
381 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500382 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400383 // During preparation the number of atlas pages has increased.
384 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500385 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600386 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400387 // 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 Daniel9715b6c2019-12-10 15:03:10 -0500389 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500390 // These will get unreffed when the previously recorded draws destruct.
391 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600392 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500393 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000394 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400395 if (this->usesDistanceFields()) {
396 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500397 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500398 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400399 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500400 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500401 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400402 }
403 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500404 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
405 : GrSamplerState::Filter::kNearest;
406 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400407 }
408 }
Brian Salomondbf70722019-02-07 11:31:24 -0500409 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600410 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600411 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
412 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
413 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600414 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600415 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800416 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
417 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500418 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800419}
420
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500421GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
422 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500423 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400424 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000425 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400426 }
427
joshualitta751c972015-11-20 13:37:32 -0800428 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000429 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800430 }
431
Herb Derby1c5be7b2019-12-13 12:03:06 -0500432 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
433 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500434
Mike Reed2c383152019-12-18 16:47:47 -0500435 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000436 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500437 }
438
Jim Van Verthb515ae72018-05-23 16:44:55 -0400439 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000440 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400441 }
442
443 if (fNeedsGlyphTransform &&
444 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000445 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400446 }
447
Brian Salomon5c6ac642017-12-19 11:09:32 -0500448 if (this->usesDistanceFields()) {
449 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000450 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800451 }
452
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400453 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000454 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800455 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500456 } else {
457 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000458 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500459 }
joshualitta751c972015-11-20 13:37:32 -0800460 }
461
Brian Salomon344ec422016-12-15 10:58:41 -0500462 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800463
Jim Van Verth56c37142017-10-31 14:44:25 -0400464 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800465 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800466
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400467 // We reallocate at a rate of 1.5x to try to get better total memory usage
468 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400469 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400470 while (newAllocSize < newGeoCount) {
471 newAllocSize += newAllocSize / 2;
472 }
joshualitta751c972015-11-20 13:37:32 -0800473 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400474 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800475 }
476
Brian Salomon344ec422016-12-15 10:58:41 -0500477 // 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 -0800478 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500479 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800480#ifdef SK_DEBUG
481 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500482 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800483 }
484#endif
485 that->fGeoCount = 0;
486 fGeoCount = newGeoCount;
487
Brian Salomon7eae3e02018-08-07 14:02:38 +0000488 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800489}
490
Herb Derby3c873af2020-04-29 15:56:07 -0400491static const int kDistanceAdjustLumShift = 5;
492
joshualitta751c972015-11-20 13:37:32 -0800493// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500494GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
495 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500496 const GrSurfaceProxyView* views,
497 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800498 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500499
500 SkMatrix localMatrix = SkMatrix::I();
501 if (this->usesLocalCoords()) {
502 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500503 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500504 (void)result;
505 }
joshualitta751c972015-11-20 13:37:32 -0800506
Robert Phillips841c9a52020-03-27 12:41:31 -0400507 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
508
joshualitta751c972015-11-20 13:37:32 -0800509 // see if we need to create a new effect
510 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400511 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400512 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500513 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400514 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400515 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500516 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400517 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400518 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500519 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800520 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500521 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
522 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500523 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500524 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500525 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800526 } else {
joshualitta751c972015-11-20 13:37:32 -0800527#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400528 float correction = 0;
529 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400530 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
531 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400532 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
533 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400534 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500535 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500536 GrSamplerState::Filter::kBilerp, correction,
537 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800538#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500539 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500540 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500541 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800542#endif
543 }
joshualitta751c972015-11-20 13:37:32 -0800544}
joshualittddd22d82016-02-16 06:47:52 -0800545
Herb Derby4598fa12020-06-10 14:54:22 -0400546#if GR_TEST_UTILS
547std::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 Derbya08bde62020-06-12 15:46:06 -0400567 GrSDFTOptions SDFOptions = rtc->fContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400568
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
587GR_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