blob: 2451f167e5b24b489cf26bf2cf0a4abdd6800c1d [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
joshualitt60ce86d2015-11-23 13:08:22 -080027///////////////////////////////////////////////////////////////////////////////////////////////////
28
Herb Derby3c873af2020-04-29 15:56:07 -040029GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
30 GrPaint&& paint,
31 GrTextBlob::SubRun* subrun,
32 const SkMatrix& drawMatrix,
33 SkPoint drawOrigin,
34 const SkIRect& clipRect,
35 const SkPMColor4f& filteredColor,
36 SkColor luminanceColor,
37 bool useGammaCorrectDistanceTable,
38 uint32_t DFGPFlags)
39 : INHERITED(ClassID())
40 , fMaskType{maskType}
41 , fNeedsGlyphTransform{subrun->needsTransform()}
42 , fLuminanceColor{luminanceColor}
43 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
44 , fDFGPFlags{DFGPFlags}
45 , fGeoDataAllocSize{kMinGeometryAllocated}
46 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040047 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040048 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
49
50 // Unref handled in ~GrAtlasTextOp().
51 geometry.fBlob = SkRef(subrun->fBlob);
52 geometry.fSubRunPtr = subrun;
53 geometry.fDrawMatrix = drawMatrix;
54 geometry.fDrawOrigin = drawOrigin;
55 geometry.fClipRect = clipRect;
56 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
57 : filteredColor;
58 fGeoCount = 1;
59
60 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
61 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
62 // we treat this as a set of non-AA rects rendered with a texture.
63 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
64}
65
Herb Derby64391c42020-05-16 14:32:15 -040066void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
67 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
68 fDrawMatrix, fDrawOrigin, fClipRect);
69}
70
Robert Phillipsb97da532019-02-12 15:24:12 -050071std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040072 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -040073 GrTextBlob::SubRun* subrun,
74 const SkMatrix& drawMatrix,
75 SkPoint drawOrigin,
76 const SkIRect& clipRect,
77 const SkPMColor4f& filteredColor) {
78 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040079
Herb Derby3c873af2020-04-29 15:56:07 -040080 MaskType maskType = [&]() {
81 switch (subrun->maskFormat()) {
82 case kA8_GrMaskFormat: return kGrayscaleCoverageMask_MaskType;
83 case kA565_GrMaskFormat: return kLCDCoverageMask_MaskType;
84 case kARGB_GrMaskFormat: return kColorBitmapMask_MaskType;
85 // Needed to placate some compilers.
86 default: return kGrayscaleCoverageMask_MaskType;
Robert Phillips7c525e62018-06-12 10:11:12 -040087 }
Herb Derby3c873af2020-04-29 15:56:07 -040088 }();
89
90 return pool->allocate<GrAtlasTextOp>(maskType,
91 std::move(paint),
92 subrun,
93 drawMatrix,
94 drawOrigin,
95 clipRect,
96 filteredColor,
97 0,
98 false,
99 0);
100}
Robert Phillips7c525e62018-06-12 10:11:12 -0400101
102std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Robert Phillipsb97da532019-02-12 15:24:12 -0500103 GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400104 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -0400105 GrTextBlob::SubRun* subrun,
106 const SkMatrix& drawMatrix,
107 SkPoint drawOrigin,
108 const SkIRect& clipRect,
109 const SkPMColor4f& filteredColor,
Robert Phillips7c525e62018-06-12 10:11:12 -0400110 bool useGammaCorrectDistanceTable,
111 SkColor luminanceColor,
Herb Derby3c873af2020-04-29 15:56:07 -0400112 const SkSurfaceProps& props) {
113 GrOpMemoryPool* pool = context->priv().opMemoryPool();
114 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
115 bool isLCD = subrun->hasUseLCDText() && SkPixelGeometryIsH(props.pixelGeometry());
116 MaskType maskType = !subrun->isAntiAliased() ? kAliasedDistanceField_MaskType
117 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
118 : kLCDDistanceField_MaskType)
119 : kGrayscaleDistanceField_MaskType;
Robert Phillipsc994a932018-06-19 13:09:54 -0400120
Herb Derby3c873af2020-04-29 15:56:07 -0400121 uint32_t DFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
122 DFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
123 DFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
124 DFGPFlags |= useGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
125 DFGPFlags |= kAliasedDistanceField_MaskType == maskType ? kAliased_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400126
Herb Derby3c873af2020-04-29 15:56:07 -0400127 if (isLCD) {
128 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
129 DFGPFlags |= kLCDBGRDistanceField_MaskType == maskType ? kBGR_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400130 }
131
Herb Derby3c873af2020-04-29 15:56:07 -0400132 return pool->allocate<GrAtlasTextOp>(maskType,
133 std::move(paint),
134 subrun,
135 drawMatrix,
136 drawOrigin,
137 clipRect,
138 filteredColor,
139 luminanceColor,
140 useGammaCorrectDistanceTable,
141 DFGPFlags);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500142}
143
Chris Dalton1706cbf2019-05-21 19:35:29 -0600144void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500145 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500146}
147
Brian Osman9a390ac2018-11-12 09:47:48 -0500148#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500149SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800150 SkString str;
151
152 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500153 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800154 i,
Brian Osmancf860852018-10-31 14:04:39 -0400155 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500156 fGeoData[i].fDrawOrigin.x(),
157 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800158 }
159
Brian Salomon44acb5b2017-07-18 19:59:24 -0400160 str += fProcessors.dumpProcessors();
161 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800162 return str;
163}
Brian Osman9a390ac2018-11-12 09:47:48 -0500164#endif
joshualitta751c972015-11-20 13:37:32 -0800165
Brian Salomon44acb5b2017-07-18 19:59:24 -0400166GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
167 return FixedFunctionFlags::kNone;
168}
169
Chris Dalton6ce447a2019-06-23 18:07:38 -0600170GrProcessorSet::Analysis GrAtlasTextOp::finalize(
171 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
172 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400173 GrProcessorAnalysisCoverage coverage;
174 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800175 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400176 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800177 } else {
Brian Osman09068252018-01-03 09:57:29 -0500178 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800179 }
joshualitta751c972015-11-20 13:37:32 -0800180 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800181 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400182 case kAliasedDistanceField_MaskType:
183 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400184 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800185 break;
186 case kLCDCoverageMask_MaskType:
187 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400188 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400189 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800190 break;
191 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400192 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400193 break;
joshualitta751c972015-11-20 13:37:32 -0800194 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700195 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600196 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
197 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400198 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700199 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800200}
201
Brian Salomon91326c32017-08-09 16:02:19 -0400202void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500203 auto resourceProvider = target->resourceProvider();
204
joshualitta751c972015-11-20 13:37:32 -0800205 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
206 // TODO actually only invert if we don't have RGBA
207 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500208 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800209 return;
210 }
211
Robert Phillips5a66efb2018-03-07 15:13:18 -0500212 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000213
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400214 GrMaskFormat maskFormat = this->maskFormat();
215
Greg Daniel9715b6c2019-12-10 15:03:10 -0500216 unsigned int numActiveViews;
217 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
218 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800219 SkDebugf("Could not allocate backing texture for atlas\n");
220 return;
221 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500222 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800223
Brian Salomon7eae3e02018-08-07 14:02:38 +0000224 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500225 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
226 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000227
Chris Dalton304e14d2020-03-17 14:29:06 -0600228 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500229 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600230 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400231 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
232 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500233 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000234 }
Brian Salomon49348902018-06-26 09:12:38 -0400235
236 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600237 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400238
Herb Derby1c5be7b2019-12-13 12:03:06 -0500239 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800240 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500241 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
242 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500243 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800244 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500245 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
246 : GrSamplerState::Filter::kNearest;
247 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
248 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
249 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800250 }
251
Herb Derby23f29762020-01-10 16:26:14 -0500252 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800253
Brian Salomon43cbd722020-01-03 22:09:12 -0500254 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500255 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
256 const int quadSize = vertexStride * kVerticesPerGlyph;
257 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
258
259 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
260 int quadBufferBegin = 0;
261 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
262
Robert Phillipsee08d522019-10-28 16:34:44 -0400263 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500264 void* vertices = target->makeVertexSpace(
265 vertexStride,
266 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
267 &flushInfo.fVertexBuffer,
268 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800269 if (!vertices || !flushInfo.fVertexBuffer) {
270 SkDebugf("Could not allocate vertices\n");
271 return;
272 }
273
Herb Derby23f29762020-01-10 16:26:14 -0500274 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
275 // quad buffer indices are calculated from this.
276 int totalGlyphsRegened = 0;
joshualitta751c972015-11-20 13:37:32 -0800277 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800278 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500279 auto subRun = args.fSubRunPtr;
280 SkASSERT((int)subRun->vertexStride() == vertexStride);
281
Robert Phillips207d24b2020-04-09 10:23:42 -0400282 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500283
Brian Osman1be2b7c2018-10-29 16:07:15 -0400284 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400285 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
286 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500287
288 // Where the subRun begins and ends relative to totalGlyphsRegened.
289 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400290 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500291
292 // Draw all the glyphs in the subRun.
293 while (totalGlyphsRegened < subRunEnd) {
294 // drawBegin and drawEnd are indices for the subRun on the
295 // interval [0, subRun->fGlyphs.size()).
296 int drawBegin = totalGlyphsRegened - subRunBegin;
297 // drawEnd is either the end of the subRun or the end of the current quad buffer.
298 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
299 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
300
301 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400302 if (!ok) {
303 return;
304 }
Herb Derby23f29762020-01-10 16:26:14 -0500305
306 // Update all the vertices for glyphsRegenerate glyphs.
307 if (glyphsRegenerated > 0) {
308 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500309 auto regeneratedQuadBuffer =
310 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400311 int subRunIndex = totalGlyphsRegened - subRunBegin;
312 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500313 }
Herb Derby23f29762020-01-10 16:26:14 -0500314
315 totalGlyphsRegened += glyphsRegenerated;
316 flushInfo.fGlyphsToFlush += glyphsRegenerated;
317
318 // regenerate() has stopped part way through a SubRun. This means that either the atlas
319 // or the quad buffer is full or both. There is a case were the flow through
320 // the loop is strange. If we run out of quad buffer space at the same time the
321 // SubRun ends, then this is not triggered which is the right result for the last
322 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
323 // will process no glyphs, and return to this point where the quad buffer will be
324 // expanded.
325 if (totalGlyphsRegened != subRunEnd) {
326 // Flush if not all glyphs drawn because either the quad buffer is full or the
327 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500328 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500329 if (totalGlyphsRegened == quadBufferEnd) {
330 // Quad buffer is full. Get more buffer.
331 quadBufferBegin = totalGlyphsRegened;
332 int quadBufferSize =
333 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
334 quadBufferEnd = quadBufferBegin + quadBufferSize;
335
336 vertices = target->makeVertexSpace(
337 vertexStride,
338 kVerticesPerGlyph * quadBufferSize,
339 &flushInfo.fVertexBuffer,
340 &flushInfo.fVertexOffset);
341 if (!vertices || !flushInfo.fVertexBuffer) {
342 SkDebugf("Could not allocate vertices\n");
343 return;
344 }
345 }
346 }
347 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500348 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500349 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800350}
351
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700352void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500353 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
354 std::move(fProcessors),
355 GrPipeline::InputFlags::kNone);
356
357 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700358}
359
Herb Derby4513cdd2020-01-31 13:28:49 -0500360void GrAtlasTextOp::createDrawForGeneratedGlyphs(
361 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500362 if (!flushInfo->fGlyphsToFlush) {
363 return;
364 }
365
Robert Phillips5a66efb2018-03-07 15:13:18 -0500366 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500367
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500368 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400369 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500370
Greg Daniel9715b6c2019-12-10 15:03:10 -0500371 unsigned int numActiveViews;
372 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
373 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500374 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500375 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500376 return;
377 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500378 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400379 // During preparation the number of atlas pages has increased.
380 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500381 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600382 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400383 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
384 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500385 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500386 // These will get unreffed when the previously recorded draws destruct.
387 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600388 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500389 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000390 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400391 if (this->usesDistanceFields()) {
392 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500393 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500394 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400395 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500396 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500397 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400398 }
399 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500400 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
401 : GrSamplerState::Filter::kNearest;
402 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400403 }
404 }
Brian Salomondbf70722019-02-07 11:31:24 -0500405 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600406 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600407 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
408 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
409 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600410 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600411 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800412 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
413 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500414 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800415}
416
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500417GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
418 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500419 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400420 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000421 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400422 }
423
joshualitta751c972015-11-20 13:37:32 -0800424 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000425 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800426 }
427
Herb Derby1c5be7b2019-12-13 12:03:06 -0500428 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
429 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500430
Mike Reed2c383152019-12-18 16:47:47 -0500431 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000432 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500433 }
434
Jim Van Verthb515ae72018-05-23 16:44:55 -0400435 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000436 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400437 }
438
439 if (fNeedsGlyphTransform &&
440 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000441 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400442 }
443
Brian Salomon5c6ac642017-12-19 11:09:32 -0500444 if (this->usesDistanceFields()) {
445 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000446 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800447 }
448
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400449 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000450 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800451 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500452 } else {
453 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000454 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500455 }
joshualitta751c972015-11-20 13:37:32 -0800456 }
457
Brian Salomon344ec422016-12-15 10:58:41 -0500458 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800459
Jim Van Verth56c37142017-10-31 14:44:25 -0400460 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800461 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800462
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400463 // We reallocate at a rate of 1.5x to try to get better total memory usage
464 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400465 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400466 while (newAllocSize < newGeoCount) {
467 newAllocSize += newAllocSize / 2;
468 }
joshualitta751c972015-11-20 13:37:32 -0800469 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400470 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800471 }
472
Brian Salomon344ec422016-12-15 10:58:41 -0500473 // 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 -0800474 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500475 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800476#ifdef SK_DEBUG
477 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500478 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800479 }
480#endif
481 that->fGeoCount = 0;
482 fGeoCount = newGeoCount;
483
Brian Salomon7eae3e02018-08-07 14:02:38 +0000484 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800485}
486
Herb Derby3c873af2020-04-29 15:56:07 -0400487static const int kDistanceAdjustLumShift = 5;
488
joshualitta751c972015-11-20 13:37:32 -0800489// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400490// (see comments in GrTextContext::ComputeCanonicalColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500491GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
492 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500493 const GrSurfaceProxyView* views,
494 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800495 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500496
497 SkMatrix localMatrix = SkMatrix::I();
498 if (this->usesLocalCoords()) {
499 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500500 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500501 (void)result;
502 }
joshualitta751c972015-11-20 13:37:32 -0800503
Robert Phillips841c9a52020-03-27 12:41:31 -0400504 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
505
joshualitta751c972015-11-20 13:37:32 -0800506 // see if we need to create a new effect
507 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400508 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400509 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500510 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400511 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400512 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500513 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400514 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400515 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500516 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800517 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500518 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
519 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500520 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500521 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500522 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800523 } else {
joshualitta751c972015-11-20 13:37:32 -0800524#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400525 float correction = 0;
526 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400527 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
528 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400529 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
530 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400531 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500532 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500533 GrSamplerState::Filter::kBilerp, correction,
534 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800535#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500536 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500537 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500538 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800539#endif
540 }
joshualitta751c972015-11-20 13:37:32 -0800541}
joshualittddd22d82016-02-16 06:47:52 -0800542
Herb Derby4598fa12020-06-10 14:54:22 -0400543#if GR_TEST_UTILS
544std::unique_ptr<GrDrawOp> GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
545 const SkPaint& skPaint,
546 const SkFont& font,
547 const SkMatrixProvider& mtxProvider,
548 const char* text,
549 int x,
550 int y) {
551 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
552
553 size_t textLen = (int)strlen(text);
554
555 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
556
557 auto drawOrigin = SkPoint::Make(x, y);
558 SkGlyphRunBuilder builder;
559 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
560
561 auto glyphRunList = builder.useGlyphRunList();
562
563 const GrRecordingContextPriv& contextPriv = rtc->fContext->priv();
564 GrTextContext::Options SDFOptions = {
565 contextPriv.options().fGlyphsAsPathsFontSize,
566 contextPriv.options().fMinDistanceFieldFontSize
567 };
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
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