blob: 2553c951f364b3448224ed9fc8ce246ed421f59b [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 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 Derby27ce7df2021-04-06 20:15:50 +0000115 const SkPMColor4f& color) -> Geometry* {
116 return new Geometry{subRun,
117 drawMatrix,
118 drawOrigin,
119 clipRect,
120 std::move(blob),
121 nullptr,
122 color};
Herb Derby6b748e42020-12-02 17:44:54 -0500123}
124
Herb Derby64391c42020-05-16 14:32:15 -0400125void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500126 SkMatrix positionMatrix = fDrawMatrix;
127 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
128 fSubRun.fillVertexData(
129 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400130}
131
Chris Dalton1706cbf2019-05-21 19:35:29 -0600132void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500133 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500134}
135
John Stiles8d9bf642020-08-12 15:07:45 -0400136#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400137SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800138 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500139 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500140 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500141 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500142 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500143 geom->fColor.toBytes_RGBA(),
144 geom->fDrawOrigin.x(),
145 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800146 }
147
Brian Salomon44acb5b2017-07-18 19:59:24 -0400148 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800149 return str;
150}
Brian Osman9a390ac2018-11-12 09:47:48 -0500151#endif
joshualitta751c972015-11-20 13:37:32 -0800152
Brian Salomon44acb5b2017-07-18 19:59:24 -0400153GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
154 return FixedFunctionFlags::kNone;
155}
156
Chris Dalton6ce447a2019-06-23 18:07:38 -0600157GrProcessorSet::Analysis GrAtlasTextOp::finalize(
158 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
159 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400160 GrProcessorAnalysisCoverage coverage;
161 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500162 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400163 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800164 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500165 // finalize() is called before any merging is done, so at this point there's at most one
166 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500167 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800168 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500169
Michael Ludwigefc89d22020-11-05 11:43:10 -0500170 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500171 case MaskType::kGrayscaleCoverage:
172 case MaskType::kAliasedDistanceField:
173 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400174 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800175 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500176 case MaskType::kLCDCoverage:
177 case MaskType::kLCDDistanceField:
178 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400179 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800180 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500181 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400182 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400183 break;
joshualitta751c972015-11-20 13:37:32 -0800184 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500185
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700186 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600187 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Herb Derby6b748e42020-12-02 17:44:54 -0500188 clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500189 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
190 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
191 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400192 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700193 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800194}
195
Brian Salomon91326c32017-08-09 16:02:19 -0400196void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500197 auto resourceProvider = target->resourceProvider();
198
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500199 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
200 // processor analysis will not require local coords and the GPs will skip local coords when
201 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
202 // all geometries to have same draw matrix.
203 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500204 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800205 return;
206 }
207
Robert Phillips5a66efb2018-03-07 15:13:18 -0500208 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000209
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400210 GrMaskFormat maskFormat = this->maskFormat();
211
Greg Daniel9715b6c2019-12-10 15:03:10 -0500212 unsigned int numActiveViews;
213 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
214 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800215 SkDebugf("Could not allocate backing texture for atlas\n");
216 return;
217 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500218 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800219
Brian Salomon7eae3e02018-08-07 14:02:38 +0000220 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500221 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
222 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000223
Chris Dalton304e14d2020-03-17 14:29:06 -0600224 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500225 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600226 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400227 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
228 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500229 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000230 }
Brian Salomon49348902018-06-26 09:12:38 -0400231
232 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600233 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400234 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400235
joshualittd9d30f72015-12-08 10:47:55 -0800236 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500237 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
238 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500239 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800240 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400241 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500242 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500243 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
244 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500245 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500246 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500247 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800248 }
249
Herb Derbyfd894ff2020-07-15 13:23:33 -0400250 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800251
Brian Salomon43cbd722020-01-03 22:09:12 -0500252 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500253 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
254 const int quadSize = vertexStride * kVerticesPerGlyph;
255 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
256
Herb Derbyfd894ff2020-07-15 13:23:33 -0400257 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500258 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400259 int quadCursor;
260 int quadEnd;
261 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500262
Herb Derbyfd894ff2020-07-15 13:23:33 -0400263 auto resetVertexBuffer = [&] {
264 quadCursor = 0;
265 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800266
Herb Derbyfd894ff2020-07-15 13:23:33 -0400267 vertices = (char*)target->makeVertexSpace(
268 vertexStride,
269 kVerticesPerGlyph * quadEnd,
270 &flushInfo.fVertexBuffer,
271 &flushInfo.fVertexOffset);
272
273 if (!vertices || !flushInfo.fVertexBuffer) {
274 SkDebugf("Could not allocate vertices\n");
275 return false;
276 }
277 return true;
278 };
279
280 resetVertexBuffer();
281
Herb Derby6b748e42020-12-02 17:44:54 -0500282 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
283 const GrAtlasSubRun& subRun = geo->fSubRun;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500284 SkASSERTF((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride,
285 "subRun stride: %d vertex buffer stride: %d\n",
286 (int)subRun.vertexStride(geo->fDrawMatrix), vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500287
Herb Derby43ad7912020-07-20 16:14:19 -0400288 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400289 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
290 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
291 // of the glyphs to fill the vertex buffer.
292 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400293 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500294 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400295 if (!ok) {
296 return;
297 }
Herb Derby23f29762020-01-10 16:26:14 -0500298
Herb Derby6b748e42020-12-02 17:44:54 -0500299 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500300
Herb Derbyfd894ff2020-07-15 13:23:33 -0400301 subRunCursor += glyphsRegenerated;
302 quadCursor += glyphsRegenerated;
303 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500304 flushInfo.fGlyphsToFlush += glyphsRegenerated;
305
Herb Derbyfd894ff2020-07-15 13:23:33 -0400306 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
307 // Flush if not all the glyphs are drawn because either the quad buffer is full or
308 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500309 if (subRunCursor < subRunEnd) {
310 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
311 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500312 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400313 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
314 // If the vertex buffer is full and there are still glyphs to draw then
315 // get a new buffer.
316 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500317 return;
318 }
319 }
320 }
321 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400322 }
joshualitta751c972015-11-20 13:37:32 -0800323}
324
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700325void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500326 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
327 std::move(fProcessors),
328 GrPipeline::InputFlags::kNone);
329
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600330 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
331 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700332}
333
Herb Derby4513cdd2020-01-31 13:28:49 -0500334void GrAtlasTextOp::createDrawForGeneratedGlyphs(
335 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500336 if (!flushInfo->fGlyphsToFlush) {
337 return;
338 }
339
Robert Phillips5a66efb2018-03-07 15:13:18 -0500340 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500341
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500342 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400343 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500344
Greg Daniel9715b6c2019-12-10 15:03:10 -0500345 unsigned int numActiveViews;
346 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
347 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500348 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500349 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500350 return;
351 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500352 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400353 // During preparation the number of atlas pages has increased.
354 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500355 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600356 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400357 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
358 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500359 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500360 // These will get unreffed when the previously recorded draws destruct.
361 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600362 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500363 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000364 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400365 if (this->usesDistanceFields()) {
366 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500367 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400368 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400369 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500370 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400371 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400372 }
373 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400374 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500375 : GrSamplerState::Filter::kNearest;
376 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400377 }
378 }
Brian Salomondbf70722019-02-07 11:31:24 -0500379 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600380 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600381 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
382 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
383 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600384 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600385 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800386 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
387 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500388 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800389}
390
Herb Derbye25c3002020-10-27 15:57:27 -0400391GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500392 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500393
394 if (fDFGPFlags != that->fDFGPFlags ||
395 fMaskType != that->fMaskType ||
396 fUsesLocalCoords != that->fUsesLocalCoords ||
397 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
398 fHasPerspective != that->fHasPerspective ||
399 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
400 // All flags must match for an op to be combined
401 return CombineResult::kCannotCombine;
402 }
403
Brian Salomon44acb5b2017-07-18 19:59:24 -0400404 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000405 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400406 }
407
Michael Ludwigefc89d22020-11-05 11:43:10 -0500408 if (fUsesLocalCoords) {
409 // If the fragment processors use local coordinates, the GPs compute them using the inverse
410 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500411 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
412 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500413 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
414 return CombineResult::kCannotCombine;
415 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400416 }
417
Brian Salomon5c6ac642017-12-19 11:09:32 -0500418 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500419 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400420 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000421 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800422 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500423 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500424 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500425 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500426 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000427 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500428 }
joshualitta751c972015-11-20 13:37:32 -0800429 }
430
Michael Ludwigefc89d22020-11-05 11:43:10 -0500431 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800432
Michael Ludwig64596c52020-11-05 12:39:13 -0500433 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500434 this->addGeometry(that->fHead);
435 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000436 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800437}
438
joshualitta751c972015-11-20 13:37:32 -0800439// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500440GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
441 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500442 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500443 const GrSurfaceProxyView* views,
444 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500445 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400446 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
447
joshualitta751c972015-11-20 13:37:32 -0800448 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500449 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400450 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400451 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500452 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400453 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400454 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500455 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400456 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400457 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500458 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800459 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500460 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
461 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500462 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400463 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500464 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800465 } else {
joshualitta751c972015-11-20 13:37:32 -0800466#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400467 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500468 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400469 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
470 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400471 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
472 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400473 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500474 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400475 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500476 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800477#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500478 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400479 GrSamplerState::Filter::kLinear, fDFGPFlags,
480 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800481#endif
482 }
joshualitta751c972015-11-20 13:37:32 -0800483}
joshualittddd22d82016-02-16 06:47:52 -0800484
Herb Derby4598fa12020-06-10 14:54:22 -0400485#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400486
Brian Salomoneebe7352020-12-09 16:37:04 -0500487GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400488 const SkPaint& skPaint,
489 const SkFont& font,
490 const SkMatrixProvider& mtxProvider,
491 const char* text,
492 int x,
493 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400494 size_t textLen = (int)strlen(text);
495
Herb Derby861ef8f2021-02-26 16:49:06 -0500496 SkMatrix drawMatrix(mtxProvider.localToDevice());
497 drawMatrix.preTranslate(x, y);
Herb Derby4598fa12020-06-10 14:54:22 -0400498 auto drawOrigin = SkPoint::Make(x, y);
499 SkGlyphRunBuilder builder;
Herb Derby3c1ed9c2021-04-13 16:13:54 -0400500 auto glyphRunList = builder.textToGlyphRunList(font, skPaint, text, textLen, drawOrigin);
Ben Wagner525e8762020-07-09 16:19:35 -0400501 if (glyphRunList.empty()) {
502 return nullptr;
503 }
Herb Derby4598fa12020-06-10 14:54:22 -0400504
Brian Salomon70fe17e2020-11-30 14:33:58 -0500505 auto rContext = rtc->recordingContext();
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500506 GrSDFTControl control =
507 rContext->priv().getSDFTControl(rtc->surfaceProps().isUseDeviceIndependentFonts());
Herb Derby4598fa12020-06-10 14:54:22 -0400508
Brian Salomon70fe17e2020-11-30 14:33:58 -0500509 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby0da2c142021-03-22 15:28:23 -0400510 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, skPaint, drawMatrix, control, painter);
Herb Derby63fe8e52021-03-08 13:22:56 -0500511
Herb Derby55f795e2021-02-05 13:45:05 -0500512 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400513 return nullptr;
514 }
Herb Derby4598fa12020-06-10 14:54:22 -0400515
Herb Derby55f795e2021-02-05 13:45:05 -0500516 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500517 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400518 GrOp::Owner op;
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500519 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(
Herb Derby0da2c142021-03-22 15:28:23 -0400520 nullptr, mtxProvider, glyphRunList, skPaint, rtc, nullptr);
Herb Derby411e7aa2020-07-09 16:02:08 -0400521 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400522}
523
524GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Herb Derby27ce7df2021-04-06 20:15:50 +0000525 // Setup dummy SkPaint / GrPaint / GrSurfaceDrawContext
526 auto rtc = GrSurfaceDrawContext::Make(
527 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
528
Herb Derby4598fa12020-06-10 14:54:22 -0400529 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
530
531 SkPaint skPaint;
532 skPaint.setColor(random->nextU());
533
534 SkFont font;
535 if (random->nextBool()) {
536 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
537 } else {
538 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
539 }
540 font.setSubpixel(random->nextBool());
541
542 const char* text = "The quick brown fox jumps over the lazy dog.";
543
544 // create some random x/y offsets, including negative offsets
545 static const int kMaxTrans = 1024;
546 int xPos = (random->nextU() % 2) * 2 - 1;
547 int yPos = (random->nextU() % 2) * 2 - 1;
548 int xInt = (random->nextU() % kMaxTrans) * xPos;
549 int yInt = (random->nextU() % kMaxTrans) * yPos;
550
Herb Derby27ce7df2021-04-06 20:15:50 +0000551 return GrAtlasTextOp::CreateOpTestingOnly(
552 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
Herb Derby4598fa12020-06-10 14:54:22 -0400553}
554
555#endif