blob: 629a1670ba74cc1e2cb9719cf553d073db592d17 [file] [log] [blame]
joshualitta751c972015-11-20 13:37:32 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrAtlasTextOp.h"
Robert Phillipse4fda6c2018-02-21 12:10:41 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint3.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkMathPriv.h"
13#include "src/core/SkMatrixPriv.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040014#include "src/core/SkMatrixProvider.h"
Herb Derbyfd894ff2020-07-15 13:23:33 -040015#include "src/core/SkSpan.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 Derby2e0b5bb2020-11-19 11:03:50 -0500110auto GrAtlasTextOp::Geometry::MakeForBlob(GrRecordingContext* rc,
111 const GrAtlasSubRun& subRun,
112 const SkMatrix& drawMatrix,
113 SkPoint drawOrigin,
114 SkIRect clipRect,
115 sk_sp<GrTextBlob> blob,
116 const SkPMColor4f& color) -> Geometry* {
Herb Derby6b748e42020-12-02 17:44:54 -0500117 auto arena = rc->priv().recordTimeAllocator();
Herb Derby4f78f232021-02-18 10:42:35 -0500118 // Bypass the automatic dtor behavior in SkArenaAlloc. I'm leaving this up to the Op to run
119 // all geometry dtors for now.
120 void* geo = arena->makeBytesAlignedTo(sizeof(Geometry), alignof(Geometry));
121 return new (geo) Geometry{subRun,
122 drawMatrix,
123 drawOrigin,
124 clipRect,
125 std::move(blob),
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500126 nullptr,
Herb Derby4f78f232021-02-18 10:42:35 -0500127 color};
Herb Derby6b748e42020-12-02 17:44:54 -0500128}
129
Herb Derby64391c42020-05-16 14:32:15 -0400130void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500131 SkMatrix positionMatrix = fDrawMatrix;
132 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
133 fSubRun.fillVertexData(
134 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400135}
136
Chris Dalton1706cbf2019-05-21 19:35:29 -0600137void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500138 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500139}
140
John Stiles8d9bf642020-08-12 15:07:45 -0400141#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400142SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800143 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500144 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500145 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500146 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500147 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500148 geom->fColor.toBytes_RGBA(),
149 geom->fDrawOrigin.x(),
150 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800151 }
152
Brian Salomon44acb5b2017-07-18 19:59:24 -0400153 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800154 return str;
155}
Brian Osman9a390ac2018-11-12 09:47:48 -0500156#endif
joshualitta751c972015-11-20 13:37:32 -0800157
Brian Salomon44acb5b2017-07-18 19:59:24 -0400158GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
159 return FixedFunctionFlags::kNone;
160}
161
Chris Dalton6ce447a2019-06-23 18:07:38 -0600162GrProcessorSet::Analysis GrAtlasTextOp::finalize(
163 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
164 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400165 GrProcessorAnalysisCoverage coverage;
166 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500167 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400168 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800169 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500170 // finalize() is called before any merging is done, so at this point there's at most one
171 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500172 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800173 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500174
Michael Ludwigefc89d22020-11-05 11:43:10 -0500175 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500176 case MaskType::kGrayscaleCoverage:
177 case MaskType::kAliasedDistanceField:
178 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400179 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800180 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500181 case MaskType::kLCDCoverage:
182 case MaskType::kLCDDistanceField:
183 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400184 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800185 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500186 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400187 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400188 break;
joshualitta751c972015-11-20 13:37:32 -0800189 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500190
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700191 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600192 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Herb Derby6b748e42020-12-02 17:44:54 -0500193 clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500194 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
195 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
196 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400197 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700198 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800199}
200
Brian Salomon91326c32017-08-09 16:02:19 -0400201void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500202 auto resourceProvider = target->resourceProvider();
203
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500204 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
205 // processor analysis will not require local coords and the GPs will skip local coords when
206 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
207 // all geometries to have same draw matrix.
208 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500209 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800210 return;
211 }
212
Robert Phillips5a66efb2018-03-07 15:13:18 -0500213 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000214
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400215 GrMaskFormat maskFormat = this->maskFormat();
216
Greg Daniel9715b6c2019-12-10 15:03:10 -0500217 unsigned int numActiveViews;
218 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
219 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800220 SkDebugf("Could not allocate backing texture for atlas\n");
221 return;
222 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500223 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800224
Brian Salomon7eae3e02018-08-07 14:02:38 +0000225 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500226 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
227 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000228
Chris Dalton304e14d2020-03-17 14:29:06 -0600229 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500230 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600231 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400232 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
233 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500234 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000235 }
Brian Salomon49348902018-06-26 09:12:38 -0400236
237 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600238 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400239 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400240
joshualittd9d30f72015-12-08 10:47:55 -0800241 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500242 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
243 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500244 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800245 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400246 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500247 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500248 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
249 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500250 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500251 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500252 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800253 }
254
Herb Derbyfd894ff2020-07-15 13:23:33 -0400255 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800256
Brian Salomon43cbd722020-01-03 22:09:12 -0500257 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500258 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
259 const int quadSize = vertexStride * kVerticesPerGlyph;
260 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
261
Herb Derbyfd894ff2020-07-15 13:23:33 -0400262 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500263 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400264 int quadCursor;
265 int quadEnd;
266 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500267
Herb Derbyfd894ff2020-07-15 13:23:33 -0400268 auto resetVertexBuffer = [&] {
269 quadCursor = 0;
270 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800271
Herb Derbyfd894ff2020-07-15 13:23:33 -0400272 vertices = (char*)target->makeVertexSpace(
273 vertexStride,
274 kVerticesPerGlyph * quadEnd,
275 &flushInfo.fVertexBuffer,
276 &flushInfo.fVertexOffset);
277
278 if (!vertices || !flushInfo.fVertexBuffer) {
279 SkDebugf("Could not allocate vertices\n");
280 return false;
281 }
282 return true;
283 };
284
285 resetVertexBuffer();
286
Herb Derby6b748e42020-12-02 17:44:54 -0500287 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
288 const GrAtlasSubRun& subRun = geo->fSubRun;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500289 SkASSERTF((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride,
290 "subRun stride: %d vertex buffer stride: %d\n",
291 (int)subRun.vertexStride(geo->fDrawMatrix), vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500292
Herb Derby43ad7912020-07-20 16:14:19 -0400293 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400294 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
295 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
296 // of the glyphs to fill the vertex buffer.
297 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400298 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500299 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400300 if (!ok) {
301 return;
302 }
Herb Derby23f29762020-01-10 16:26:14 -0500303
Herb Derby6b748e42020-12-02 17:44:54 -0500304 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500305
Herb Derbyfd894ff2020-07-15 13:23:33 -0400306 subRunCursor += glyphsRegenerated;
307 quadCursor += glyphsRegenerated;
308 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500309 flushInfo.fGlyphsToFlush += glyphsRegenerated;
310
Herb Derbyfd894ff2020-07-15 13:23:33 -0400311 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
312 // Flush if not all the glyphs are drawn because either the quad buffer is full or
313 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500314 if (subRunCursor < subRunEnd) {
315 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
316 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500317 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400318 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
319 // If the vertex buffer is full and there are still glyphs to draw then
320 // get a new buffer.
321 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500322 return;
323 }
324 }
325 }
326 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400327 }
joshualitta751c972015-11-20 13:37:32 -0800328}
329
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700330void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500331 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
332 std::move(fProcessors),
333 GrPipeline::InputFlags::kNone);
334
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600335 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
336 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700337}
338
Herb Derby4513cdd2020-01-31 13:28:49 -0500339void GrAtlasTextOp::createDrawForGeneratedGlyphs(
340 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500341 if (!flushInfo->fGlyphsToFlush) {
342 return;
343 }
344
Robert Phillips5a66efb2018-03-07 15:13:18 -0500345 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500346
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500347 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400348 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500349
Greg Daniel9715b6c2019-12-10 15:03:10 -0500350 unsigned int numActiveViews;
351 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
352 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500353 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500354 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500355 return;
356 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500357 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400358 // During preparation the number of atlas pages has increased.
359 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500360 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600361 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400362 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
363 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500364 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500365 // These will get unreffed when the previously recorded draws destruct.
366 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600367 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500368 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000369 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400370 if (this->usesDistanceFields()) {
371 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500372 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400373 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400374 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500375 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400376 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400377 }
378 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400379 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500380 : GrSamplerState::Filter::kNearest;
381 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400382 }
383 }
Brian Salomondbf70722019-02-07 11:31:24 -0500384 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600385 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600386 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
387 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
388 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600389 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600390 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800391 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
392 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500393 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800394}
395
Herb Derbye25c3002020-10-27 15:57:27 -0400396GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500397 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500398
399 if (fDFGPFlags != that->fDFGPFlags ||
400 fMaskType != that->fMaskType ||
401 fUsesLocalCoords != that->fUsesLocalCoords ||
402 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
403 fHasPerspective != that->fHasPerspective ||
404 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
405 // All flags must match for an op to be combined
406 return CombineResult::kCannotCombine;
407 }
408
Brian Salomon44acb5b2017-07-18 19:59:24 -0400409 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000410 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400411 }
412
Michael Ludwigefc89d22020-11-05 11:43:10 -0500413 if (fUsesLocalCoords) {
414 // If the fragment processors use local coordinates, the GPs compute them using the inverse
415 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500416 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
417 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500418 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
419 return CombineResult::kCannotCombine;
420 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400421 }
422
Brian Salomon5c6ac642017-12-19 11:09:32 -0500423 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500424 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400425 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000426 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800427 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500428 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500429 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500430 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500431 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000432 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500433 }
joshualitta751c972015-11-20 13:37:32 -0800434 }
435
Michael Ludwigefc89d22020-11-05 11:43:10 -0500436 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800437
Michael Ludwig64596c52020-11-05 12:39:13 -0500438 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500439 this->addGeometry(that->fHead);
440 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000441 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800442}
443
joshualitta751c972015-11-20 13:37:32 -0800444// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500445GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
446 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500447 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500448 const GrSurfaceProxyView* views,
449 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500450 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400451 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
452
joshualitta751c972015-11-20 13:37:32 -0800453 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500454 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400455 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400456 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500457 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400458 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400459 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500460 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400461 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400462 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500463 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800464 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500465 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
466 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500467 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400468 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500469 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800470 } else {
joshualitta751c972015-11-20 13:37:32 -0800471#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400472 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500473 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400474 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
475 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400476 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
477 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400478 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500479 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400480 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500481 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800482#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500483 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400484 GrSamplerState::Filter::kLinear, fDFGPFlags,
485 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800486#endif
487 }
joshualitta751c972015-11-20 13:37:32 -0800488}
joshualittddd22d82016-02-16 06:47:52 -0800489
Herb Derby4598fa12020-06-10 14:54:22 -0400490#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400491
Brian Salomoneebe7352020-12-09 16:37:04 -0500492GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400493 const SkPaint& skPaint,
494 const SkFont& font,
495 const SkMatrixProvider& mtxProvider,
496 const char* text,
497 int x,
498 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400499 size_t textLen = (int)strlen(text);
500
Herb Derby861ef8f2021-02-26 16:49:06 -0500501 SkMatrix drawMatrix(mtxProvider.localToDevice());
502 drawMatrix.preTranslate(x, y);
Herb Derby4598fa12020-06-10 14:54:22 -0400503 auto drawOrigin = SkPoint::Make(x, y);
504 SkGlyphRunBuilder builder;
Herb Derby0da2c142021-03-22 15:28:23 -0400505 builder.drawTextUTF8(font, text, textLen, drawOrigin);
Herb Derby4598fa12020-06-10 14:54:22 -0400506
507 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400508 if (glyphRunList.empty()) {
509 return nullptr;
510 }
Herb Derby4598fa12020-06-10 14:54:22 -0400511
Brian Salomon70fe17e2020-11-30 14:33:58 -0500512 auto rContext = rtc->recordingContext();
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500513 GrSDFTControl control =
514 rContext->priv().getSDFTControl(rtc->surfaceProps().isUseDeviceIndependentFonts());
Herb Derby4598fa12020-06-10 14:54:22 -0400515
Brian Salomon70fe17e2020-11-30 14:33:58 -0500516 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby0da2c142021-03-22 15:28:23 -0400517 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, skPaint, drawMatrix, control, painter);
Herb Derby63fe8e52021-03-08 13:22:56 -0500518
Herb Derby55f795e2021-02-05 13:45:05 -0500519 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400520 return nullptr;
521 }
Herb Derby4598fa12020-06-10 14:54:22 -0400522
Herb Derby55f795e2021-02-05 13:45:05 -0500523 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500524 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400525 GrOp::Owner op;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500526 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(
Herb Derby0da2c142021-03-22 15:28:23 -0400527 nullptr, mtxProvider, glyphRunList, skPaint, rtc, nullptr);
Herb Derby411e7aa2020-07-09 16:02:08 -0400528 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400529}
530
531GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Brian Salomoneebe7352020-12-09 16:37:04 -0500532 // Setup dummy SkPaint / GrPaint / GrSurfaceDrawContext
533 auto rtc = GrSurfaceDrawContext::Make(
Herb Derby4598fa12020-06-10 14:54:22 -0400534 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
535
536 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
537
538 SkPaint skPaint;
539 skPaint.setColor(random->nextU());
540
541 SkFont font;
542 if (random->nextBool()) {
543 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
544 } else {
545 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
546 }
547 font.setSubpixel(random->nextBool());
548
549 const char* text = "The quick brown fox jumps over the lazy dog.";
550
551 // create some random x/y offsets, including negative offsets
552 static const int kMaxTrans = 1024;
553 int xPos = (random->nextU() % 2) * 2 - 1;
554 int yPos = (random->nextU() % 2) * 2 - 1;
555 int xInt = (random->nextU() % kMaxTrans) * xPos;
556 int yInt = (random->nextU() % kMaxTrans) * yPos;
557
558 return GrAtlasTextOp::CreateOpTestingOnly(
559 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
560}
561
562#endif