blob: 3727a44199e85a0afccf6e444bc9aa39f4261598 [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"
Brian Osmana5842bc2021-05-11 13:41:46 -040011#include "include/core/SkSpan.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkMathPriv.h"
14#include "src/core/SkMatrixPriv.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040015#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkStrikeCache.h"
17#include "src/gpu/GrCaps.h"
18#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrOpFlushState.h"
20#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050022#include "src/gpu/GrSurfaceDrawContext.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
Herb Derby93330c92021-02-26 12:18:26 -050034#include <new>
35#include <utility>
36
37// If we have thread local, then cache memory for a single GrAtlasTextOp.
Herb Derby32302552021-03-11 18:27:35 -050038#if defined(GR_HAS_THREAD_LOCAL)
Herb Derby93330c92021-02-26 12:18:26 -050039static thread_local void* gCache = nullptr;
40void* GrAtlasTextOp::operator new(size_t s) {
41 if (gCache != nullptr) {
42 return std::exchange(gCache, nullptr);
43 }
44
45 return ::operator new(s);
46}
47
48void GrAtlasTextOp::operator delete(void* bytes) noexcept {
49 if (gCache == nullptr) {
50 gCache = bytes;
51 return;
52 }
53 ::operator delete(bytes);
54}
55
56void GrAtlasTextOp::ClearCache() {
57 ::operator delete(gCache);
58 gCache = nullptr;
59}
60#endif
joshualitt60ce86d2015-11-23 13:08:22 -080061
Herb Derby3c873af2020-04-29 15:56:07 -040062GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
Herb Derby268e48b2020-07-16 12:56:58 -040063 bool needsTransform,
64 int glyphCount,
65 SkRect deviceRect,
Herb Derby6b748e42020-12-02 17:44:54 -050066 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040067 GrPaint&& paint)
Michael Ludwigefc89d22020-11-05 11:43:10 -050068 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050069 , fProcessors(std::move(paint))
70 , fNumGlyphs(glyphCount)
71 , fDFGPFlags(0)
72 , fMaskType(static_cast<uint32_t>(maskType))
73 , fUsesLocalCoords(false)
74 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -050075 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
76 , fUseGammaCorrectDistanceTable(false)
77 , fHead{geo}
78 , fTail{&fHead->fNext} {
Herb Derby268e48b2020-07-16 12:56:58 -040079 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
80 // we treat this as a set of non-AA rects rendered with a texture.
81 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
82}
83
84GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
85 bool needsTransform,
86 int glyphCount,
87 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040088 SkColor luminanceColor,
89 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040090 uint32_t DFGPFlags,
Herb Derby6b748e42020-12-02 17:44:54 -050091 Geometry* geo,
Herb Derby1d17e492020-07-21 11:45:04 -040092 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040093 : INHERITED{ClassID()}
Michael Ludwigefc89d22020-11-05 11:43:10 -050094 , fProcessors(std::move(paint))
95 , fNumGlyphs(glyphCount)
96 , fDFGPFlags(DFGPFlags)
97 , fMaskType(static_cast<uint32_t>(maskType))
98 , fUsesLocalCoords(false)
99 , fNeedsGlyphTransform(needsTransform)
Herb Derby6b748e42020-12-02 17:44:54 -0500100 , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective())
Michael Ludwigefc89d22020-11-05 11:43:10 -0500101 , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable)
Herb Derby6b748e42020-12-02 17:44:54 -0500102 , fLuminanceColor(luminanceColor)
103 , fHead{geo}
104 , fTail{&fHead->fNext} {
Herb Derby3c873af2020-04-29 15:56:07 -0400105 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
106 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -0400107 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -0400108}
109
Herb Derbybba18802021-03-31 16:45:54 -0400110auto GrAtlasTextOp::Geometry::MakeForBlob(const GrAtlasSubRun& subRun,
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500111 const SkMatrix& drawMatrix,
112 SkPoint drawOrigin,
113 SkIRect clipRect,
114 sk_sp<GrTextBlob> blob,
Herb Derby0b1228d2021-04-05 18:38:35 -0400115 const SkPMColor4f& color,
116 SkArenaAlloc* alloc) -> Geometry* {
117 // Bypass the automatic dtor behavior in SkArenaAlloc. I'm leaving this up to the Op to run
118 // all geometry dtors for now.
119 void* geo = alloc->makeBytesAlignedTo(sizeof(Geometry), alignof(Geometry));
120 return new(geo) Geometry{subRun,
121 drawMatrix,
122 drawOrigin,
123 clipRect,
124 std::move(blob),
125 nullptr,
126 color};
Herb Derby6b748e42020-12-02 17:44:54 -0500127}
128
Herb Derby64391c42020-05-16 14:32:15 -0400129void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500130 SkMatrix positionMatrix = fDrawMatrix;
131 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
132 fSubRun.fillVertexData(
133 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400134}
135
Robert Phillips294723d2021-06-17 09:23:58 -0400136void GrAtlasTextOp::visitProxies(const GrVisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500137 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500138}
139
John Stiles8d9bf642020-08-12 15:07:45 -0400140#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400141SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800142 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500143 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500144 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500145 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500146 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500147 geom->fColor.toBytes_RGBA(),
148 geom->fDrawOrigin.x(),
149 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800150 }
151
Brian Salomon44acb5b2017-07-18 19:59:24 -0400152 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800153 return str;
154}
Brian Osman9a390ac2018-11-12 09:47:48 -0500155#endif
joshualitta751c972015-11-20 13:37:32 -0800156
Brian Salomon44acb5b2017-07-18 19:59:24 -0400157GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
158 return FixedFunctionFlags::kNone;
159}
160
Chris Dalton57ab06c2021-04-22 12:57:28 -0600161GrProcessorSet::Analysis GrAtlasTextOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
162 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400163 GrProcessorAnalysisCoverage coverage;
164 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500165 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400166 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800167 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500168 // finalize() is called before any merging is done, so at this point there's at most one
169 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500170 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800171 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500172
Michael Ludwigefc89d22020-11-05 11:43:10 -0500173 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500174 case MaskType::kGrayscaleCoverage:
175 case MaskType::kAliasedDistanceField:
176 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400177 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800178 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500179 case MaskType::kLCDCoverage:
180 case MaskType::kLCDDistanceField:
181 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400182 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800183 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500184 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400185 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400186 break;
joshualitta751c972015-11-20 13:37:32 -0800187 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500188
Chris Dalton57ab06c2021-04-22 12:57:28 -0600189 auto analysis = fProcessors.finalize(color, coverage, clip, &GrUserStencilSettings::kUnused,
190 caps, clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500191 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
192 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
193 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400194 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700195 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800196}
197
Robert Phillips71143952021-06-17 14:55:07 -0400198void GrAtlasTextOp::onPrepareDraws(GrMeshDrawTarget* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500199 auto resourceProvider = target->resourceProvider();
200
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500201 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
202 // processor analysis will not require local coords and the GPs will skip local coords when
203 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
204 // all geometries to have same draw matrix.
205 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500206 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800207 return;
208 }
209
Robert Phillips5a66efb2018-03-07 15:13:18 -0500210 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000211
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400212 GrMaskFormat maskFormat = this->maskFormat();
213
Greg Daniel9715b6c2019-12-10 15:03:10 -0500214 unsigned int numActiveViews;
215 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
216 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800217 SkDebugf("Could not allocate backing texture for atlas\n");
218 return;
219 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500220 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800221
Brian Salomon7eae3e02018-08-07 14:02:38 +0000222 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500223 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
224 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000225
Chris Dalton304e14d2020-03-17 14:29:06 -0600226 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500227 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600228 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400229 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
230 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500231 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000232 }
Brian Salomon49348902018-06-26 09:12:38 -0400233
234 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600235 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400236 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400237
joshualittd9d30f72015-12-08 10:47:55 -0800238 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500239 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
240 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500241 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800242 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400243 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500244 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500245 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
246 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500247 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500248 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500249 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800250 }
251
Herb Derbyfd894ff2020-07-15 13:23:33 -0400252 const 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
Herb Derbyfd894ff2020-07-15 13:23:33 -0400259 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500260 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400261 int quadCursor;
262 int quadEnd;
263 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500264
Herb Derbyfd894ff2020-07-15 13:23:33 -0400265 auto resetVertexBuffer = [&] {
266 quadCursor = 0;
267 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800268
Herb Derbyfd894ff2020-07-15 13:23:33 -0400269 vertices = (char*)target->makeVertexSpace(
270 vertexStride,
271 kVerticesPerGlyph * quadEnd,
272 &flushInfo.fVertexBuffer,
273 &flushInfo.fVertexOffset);
274
275 if (!vertices || !flushInfo.fVertexBuffer) {
276 SkDebugf("Could not allocate vertices\n");
277 return false;
278 }
279 return true;
280 };
281
282 resetVertexBuffer();
283
Herb Derby6b748e42020-12-02 17:44:54 -0500284 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
285 const GrAtlasSubRun& subRun = geo->fSubRun;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500286 SkASSERTF((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride,
287 "subRun stride: %d vertex buffer stride: %d\n",
288 (int)subRun.vertexStride(geo->fDrawMatrix), vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500289
Herb Derby43ad7912020-07-20 16:14:19 -0400290 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400291 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
292 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
293 // of the glyphs to fill the vertex buffer.
294 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400295 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500296 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400297 if (!ok) {
298 return;
299 }
Herb Derby23f29762020-01-10 16:26:14 -0500300
Herb Derby6b748e42020-12-02 17:44:54 -0500301 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500302
Herb Derbyfd894ff2020-07-15 13:23:33 -0400303 subRunCursor += glyphsRegenerated;
304 quadCursor += glyphsRegenerated;
305 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500306 flushInfo.fGlyphsToFlush += glyphsRegenerated;
307
Herb Derbyfd894ff2020-07-15 13:23:33 -0400308 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
309 // Flush if not all the glyphs are drawn because either the quad buffer is full or
310 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500311 if (subRunCursor < subRunEnd) {
312 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
313 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500314 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400315 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
316 // If the vertex buffer is full and there are still glyphs to draw then
317 // get a new buffer.
318 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500319 return;
320 }
321 }
322 }
323 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400324 }
joshualitta751c972015-11-20 13:37:32 -0800325}
326
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700327void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500328 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
329 std::move(fProcessors),
330 GrPipeline::InputFlags::kNone);
331
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600332 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
333 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700334}
335
Herb Derby4513cdd2020-01-31 13:28:49 -0500336void GrAtlasTextOp::createDrawForGeneratedGlyphs(
Robert Phillips71143952021-06-17 14:55:07 -0400337 GrMeshDrawTarget* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500338 if (!flushInfo->fGlyphsToFlush) {
339 return;
340 }
341
Robert Phillips5a66efb2018-03-07 15:13:18 -0500342 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500343
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500344 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400345 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500346
Greg Daniel9715b6c2019-12-10 15:03:10 -0500347 unsigned int numActiveViews;
348 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
349 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500350 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500351 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500352 return;
353 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500354 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400355 // During preparation the number of atlas pages has increased.
356 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500357 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600358 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400359 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
360 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500361 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500362 // These will get unreffed when the previously recorded draws destruct.
363 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600364 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500365 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000366 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400367 if (this->usesDistanceFields()) {
368 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500369 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400370 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400371 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500372 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400373 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400374 }
375 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400376 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500377 : GrSamplerState::Filter::kNearest;
378 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400379 }
380 }
Brian Salomondbf70722019-02-07 11:31:24 -0500381 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600382 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600383 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
384 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
385 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600386 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600387 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800388 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
389 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500390 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800391}
392
Herb Derbye25c3002020-10-27 15:57:27 -0400393GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500394 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500395
396 if (fDFGPFlags != that->fDFGPFlags ||
397 fMaskType != that->fMaskType ||
398 fUsesLocalCoords != that->fUsesLocalCoords ||
399 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
400 fHasPerspective != that->fHasPerspective ||
401 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
402 // All flags must match for an op to be combined
403 return CombineResult::kCannotCombine;
404 }
405
Brian Salomon44acb5b2017-07-18 19:59:24 -0400406 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000407 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400408 }
409
Michael Ludwigefc89d22020-11-05 11:43:10 -0500410 if (fUsesLocalCoords) {
411 // If the fragment processors use local coordinates, the GPs compute them using the inverse
412 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500413 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
414 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500415 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
416 return CombineResult::kCannotCombine;
417 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400418 }
419
Brian Salomon5c6ac642017-12-19 11:09:32 -0500420 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500421 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400422 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000423 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800424 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500425 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500426 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500427 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500428 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000429 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500430 }
joshualitta751c972015-11-20 13:37:32 -0800431 }
432
Michael Ludwigefc89d22020-11-05 11:43:10 -0500433 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800434
Michael Ludwig64596c52020-11-05 12:39:13 -0500435 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500436 this->addGeometry(that->fHead);
437 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000438 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800439}
440
joshualitta751c972015-11-20 13:37:32 -0800441// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500442GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
443 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500444 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500445 const GrSurfaceProxyView* views,
446 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500447 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400448 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
449
joshualitta751c972015-11-20 13:37:32 -0800450 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500451 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400452 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400453 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500454 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400455 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400456 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500457 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400458 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400459 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500460 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800461 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500462 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
463 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500464 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400465 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500466 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800467 } else {
joshualitta751c972015-11-20 13:37:32 -0800468#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400469 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500470 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400471 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
472 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400473 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
474 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400475 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500476 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400477 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500478 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800479#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500480 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400481 GrSamplerState::Filter::kLinear, fDFGPFlags,
482 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800483#endif
484 }
joshualitta751c972015-11-20 13:37:32 -0800485}
joshualittddd22d82016-02-16 06:47:52 -0800486
Herb Derby4598fa12020-06-10 14:54:22 -0400487#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400488
Brian Salomoneebe7352020-12-09 16:37:04 -0500489GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400490 const SkPaint& skPaint,
491 const SkFont& font,
492 const SkMatrixProvider& mtxProvider,
493 const char* text,
494 int x,
495 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400496 size_t textLen = (int)strlen(text);
497
Herb Derby861ef8f2021-02-26 16:49:06 -0500498 SkMatrix drawMatrix(mtxProvider.localToDevice());
499 drawMatrix.preTranslate(x, y);
Herb Derby4598fa12020-06-10 14:54:22 -0400500 auto drawOrigin = SkPoint::Make(x, y);
501 SkGlyphRunBuilder builder;
Herb Derby3c1ed9c2021-04-13 16:13:54 -0400502 auto glyphRunList = builder.textToGlyphRunList(font, skPaint, text, textLen, drawOrigin);
Ben Wagner525e8762020-07-09 16:19:35 -0400503 if (glyphRunList.empty()) {
504 return nullptr;
505 }
Herb Derby4598fa12020-06-10 14:54:22 -0400506
Brian Salomon70fe17e2020-11-30 14:33:58 -0500507 auto rContext = rtc->recordingContext();
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500508 GrSDFTControl control =
509 rContext->priv().getSDFTControl(rtc->surfaceProps().isUseDeviceIndependentFonts());
Herb Derby4598fa12020-06-10 14:54:22 -0400510
Brian Salomon70fe17e2020-11-30 14:33:58 -0500511 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby0da2c142021-03-22 15:28:23 -0400512 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, skPaint, drawMatrix, control, painter);
Herb Derby63fe8e52021-03-08 13:22:56 -0500513
Herb Derby55f795e2021-02-05 13:45:05 -0500514 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400515 return nullptr;
516 }
Herb Derby4598fa12020-06-10 14:54:22 -0400517
Herb Derby55f795e2021-02-05 13:45:05 -0500518 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500519 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400520 GrOp::Owner op;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500521 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(
Herb Derby0da2c142021-03-22 15:28:23 -0400522 nullptr, mtxProvider, glyphRunList, skPaint, rtc, nullptr);
Herb Derby411e7aa2020-07-09 16:02:08 -0400523 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400524}
525
526GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Herb Derby4598fa12020-06-10 14:54:22 -0400527 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
528
529 SkPaint skPaint;
530 skPaint.setColor(random->nextU());
531
532 SkFont font;
533 if (random->nextBool()) {
534 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
535 } else {
536 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
537 }
538 font.setSubpixel(random->nextBool());
539
540 const char* text = "The quick brown fox jumps over the lazy dog.";
541
542 // create some random x/y offsets, including negative offsets
543 static const int kMaxTrans = 1024;
544 int xPos = (random->nextU() % 2) * 2 - 1;
545 int yPos = (random->nextU() % 2) * 2 - 1;
546 int xInt = (random->nextU() % kMaxTrans) * xPos;
547 int yInt = (random->nextU() % kMaxTrans) * yPos;
548
Herb Derby0b1228d2021-04-05 18:38:35 -0400549 return GrAtlasTextOp::CreateOpTestingOnly(sdc, skPaint, font, matrixProvider, text, xInt, yInt);
Herb Derby4598fa12020-06-10 14:54:22 -0400550}
551
552#endif