blob: b2b9b2506f8f91757de410652e3355a928dad649 [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 Derby6b748e42020-12-02 17:44:54 -0500110auto GrAtlasTextOp::Geometry::Make(GrRecordingContext* rc,
111 const GrAtlasSubRun& subRun,
112 const SkMatrix& drawMatrix,
113 SkPoint drawOrigin,
114 SkIRect clipRect,
Herb Derby4f78f232021-02-18 10:42:35 -0500115 sk_sp<GrTextBlob> blob,
Herb Derby6b748e42020-12-02 17:44:54 -0500116 const SkPMColor4f& color) -> Geometry* {
117 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),
126 color};
Herb Derby6b748e42020-12-02 17:44:54 -0500127}
128
129
130
Herb Derby64391c42020-05-16 14:32:15 -0400131void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby40894182020-12-02 11:39:48 -0500132 SkMatrix positionMatrix = fDrawMatrix;
133 positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y());
134 fSubRun.fillVertexData(
135 dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -0400136}
137
Chris Dalton1706cbf2019-05-21 19:35:29 -0600138void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500139 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500140}
141
John Stiles8d9bf642020-08-12 15:07:45 -0400142#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400143SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800144 SkString str;
Michael Ludwig64596c52020-11-05 12:39:13 -0500145 int i = 0;
Herb Derby6b748e42020-12-02 17:44:54 -0500146 for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500147 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
Michael Ludwig64596c52020-11-05 12:39:13 -0500148 i++,
Herb Derby6b748e42020-12-02 17:44:54 -0500149 geom->fColor.toBytes_RGBA(),
150 geom->fDrawOrigin.x(),
151 geom->fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800152 }
153
Brian Salomon44acb5b2017-07-18 19:59:24 -0400154 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800155 return str;
156}
Brian Osman9a390ac2018-11-12 09:47:48 -0500157#endif
joshualitta751c972015-11-20 13:37:32 -0800158
Brian Salomon44acb5b2017-07-18 19:59:24 -0400159GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
160 return FixedFunctionFlags::kNone;
161}
162
Chris Dalton6ce447a2019-06-23 18:07:38 -0600163GrProcessorSet::Analysis GrAtlasTextOp::finalize(
164 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
165 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400166 GrProcessorAnalysisCoverage coverage;
167 GrProcessorAnalysisColor color;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500168 if (this->maskType() == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400169 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800170 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500171 // finalize() is called before any merging is done, so at this point there's at most one
172 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Herb Derby6b748e42020-12-02 17:44:54 -0500173 color.setToConstant(fHead->fColor);
joshualitta751c972015-11-20 13:37:32 -0800174 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500175
Michael Ludwigefc89d22020-11-05 11:43:10 -0500176 switch (this->maskType()) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500177 case MaskType::kGrayscaleCoverage:
178 case MaskType::kAliasedDistanceField:
179 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400180 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800181 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500182 case MaskType::kLCDCoverage:
183 case MaskType::kLCDDistanceField:
184 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400185 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800186 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500187 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400188 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400189 break;
joshualitta751c972015-11-20 13:37:32 -0800190 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500191
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700192 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600193 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
Herb Derby6b748e42020-12-02 17:44:54 -0500194 clampType, &fHead->fColor);
Michael Ludwigefc89d22020-11-05 11:43:10 -0500195 // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization
196 // the atlas op metadata can be fully const. This is okay for now since finalize() happens
197 // before the op is merged, so during combineIfPossible, metadata is effectively const.
Brian Salomon44acb5b2017-07-18 19:59:24 -0400198 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700199 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800200}
201
Brian Salomon91326c32017-08-09 16:02:19 -0400202void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500203 auto resourceProvider = target->resourceProvider();
204
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500205 // If we need local coordinates, compute an inverse view matrix. If this is solid color, the
206 // processor analysis will not require local coords and the GPs will skip local coords when
207 // the matrix is identity. When the shaders require local coords, combineIfPossible requires all
208 // all geometries to have same draw matrix.
209 SkMatrix localMatrix = SkMatrix::I();
Herb Derby6b748e42020-12-02 17:44:54 -0500210 if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800211 return;
212 }
213
Robert Phillips5a66efb2018-03-07 15:13:18 -0500214 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000215
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400216 GrMaskFormat maskFormat = this->maskFormat();
217
Greg Daniel9715b6c2019-12-10 15:03:10 -0500218 unsigned int numActiveViews;
219 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
220 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800221 SkDebugf("Could not allocate backing texture for atlas\n");
222 return;
223 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500224 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800225
Brian Salomon7eae3e02018-08-07 14:02:38 +0000226 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500227 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
228 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000229
Chris Dalton304e14d2020-03-17 14:29:06 -0600230 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500231 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600232 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400233 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
234 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500235 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000236 }
Brian Salomon49348902018-06-26 09:12:38 -0400237
238 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600239 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400240 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400241
joshualittd9d30f72015-12-08 10:47:55 -0800242 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500243 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
244 *target->caps().shaderCaps(),
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500245 localMatrix, views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800246 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400247 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500248 : GrSamplerState::Filter::kNearest;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500249 // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same
250 // color, so we can use the first's without worry.
Brian Salomonccb61422020-01-09 10:46:36 -0500251 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Herb Derby6b748e42020-12-02 17:44:54 -0500252 target->allocator(), *target->caps().shaderCaps(), fHead->fColor,
Michael Ludwig64596c52020-11-05 12:39:13 -0500253 false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective);
joshualitta751c972015-11-20 13:37:32 -0800254 }
255
Herb Derbyfd894ff2020-07-15 13:23:33 -0400256 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800257
Brian Salomon43cbd722020-01-03 22:09:12 -0500258 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500259 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
260 const int quadSize = vertexStride * kVerticesPerGlyph;
261 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
262
Herb Derbyfd894ff2020-07-15 13:23:33 -0400263 int allGlyphsCursor = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500264 const int allGlyphsEnd = fNumGlyphs;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400265 int quadCursor;
266 int quadEnd;
267 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500268
Herb Derbyfd894ff2020-07-15 13:23:33 -0400269 auto resetVertexBuffer = [&] {
270 quadCursor = 0;
271 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800272
Herb Derbyfd894ff2020-07-15 13:23:33 -0400273 vertices = (char*)target->makeVertexSpace(
274 vertexStride,
275 kVerticesPerGlyph * quadEnd,
276 &flushInfo.fVertexBuffer,
277 &flushInfo.fVertexOffset);
278
279 if (!vertices || !flushInfo.fVertexBuffer) {
280 SkDebugf("Could not allocate vertices\n");
281 return false;
282 }
283 return true;
284 };
285
286 resetVertexBuffer();
287
Herb Derby6b748e42020-12-02 17:44:54 -0500288 for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) {
289 const GrAtlasSubRun& subRun = geo->fSubRun;
290 SkASSERT((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500291
Herb Derby43ad7912020-07-20 16:14:19 -0400292 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400293 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
294 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
295 // of the glyphs to fill the vertex buffer.
296 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400297 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500298 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400299 if (!ok) {
300 return;
301 }
Herb Derby23f29762020-01-10 16:26:14 -0500302
Herb Derby6b748e42020-12-02 17:44:54 -0500303 geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500304
Herb Derbyfd894ff2020-07-15 13:23:33 -0400305 subRunCursor += glyphsRegenerated;
306 quadCursor += glyphsRegenerated;
307 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500308 flushInfo.fGlyphsToFlush += glyphsRegenerated;
309
Herb Derbyfd894ff2020-07-15 13:23:33 -0400310 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
311 // Flush if not all the glyphs are drawn because either the quad buffer is full or
312 // the atlas is out of space.
Herb Derby89acfe72021-01-28 10:57:40 -0500313 if (subRunCursor < subRunEnd) {
314 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full");
315 }
Herb Derby4513cdd2020-01-31 13:28:49 -0500316 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400317 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
318 // If the vertex buffer is full and there are still glyphs to draw then
319 // get a new buffer.
320 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500321 return;
322 }
323 }
324 }
325 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400326 }
joshualitta751c972015-11-20 13:37:32 -0800327}
328
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700329void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500330 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
331 std::move(fProcessors),
332 GrPipeline::InputFlags::kNone);
333
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600334 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
335 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700336}
337
Herb Derby4513cdd2020-01-31 13:28:49 -0500338void GrAtlasTextOp::createDrawForGeneratedGlyphs(
339 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500340 if (!flushInfo->fGlyphsToFlush) {
341 return;
342 }
343
Robert Phillips5a66efb2018-03-07 15:13:18 -0500344 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500345
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500346 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500348
Greg Daniel9715b6c2019-12-10 15:03:10 -0500349 unsigned int numActiveViews;
350 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
351 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500352 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500353 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500354 return;
355 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500356 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400357 // During preparation the number of atlas pages has increased.
358 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500359 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600360 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400361 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
362 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500363 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500364 // These will get unreffed when the previously recorded draws destruct.
365 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600366 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500367 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000368 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400369 if (this->usesDistanceFields()) {
370 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500371 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400372 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400373 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500374 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400375 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400376 }
377 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400378 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500379 : GrSamplerState::Filter::kNearest;
380 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400381 }
382 }
Brian Salomondbf70722019-02-07 11:31:24 -0500383 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600384 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600385 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
386 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
387 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600388 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600389 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800390 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
391 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500392 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800393}
394
Herb Derbye25c3002020-10-27 15:57:27 -0400395GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500396 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Michael Ludwigefc89d22020-11-05 11:43:10 -0500397
398 if (fDFGPFlags != that->fDFGPFlags ||
399 fMaskType != that->fMaskType ||
400 fUsesLocalCoords != that->fUsesLocalCoords ||
401 fNeedsGlyphTransform != that->fNeedsGlyphTransform ||
402 fHasPerspective != that->fHasPerspective ||
403 fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) {
404 // All flags must match for an op to be combined
405 return CombineResult::kCannotCombine;
406 }
407
Brian Salomon44acb5b2017-07-18 19:59:24 -0400408 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000409 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400410 }
411
Michael Ludwigefc89d22020-11-05 11:43:10 -0500412 if (fUsesLocalCoords) {
413 // If the fragment processors use local coordinates, the GPs compute them using the inverse
414 // of the view matrix stored in a uniform, so all geometries must have the same matrix.
Herb Derby6b748e42020-12-02 17:44:54 -0500415 const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix;
416 const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500417 if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
418 return CombineResult::kCannotCombine;
419 }
Jim Van Verthb515ae72018-05-23 16:44:55 -0400420 }
421
Brian Salomon5c6ac642017-12-19 11:09:32 -0500422 if (this->usesDistanceFields()) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500423 SkASSERT(that->usesDistanceFields());
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400424 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000425 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800426 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500427 } else {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500428 if (this->maskType() == MaskType::kColorBitmap &&
Herb Derby6b748e42020-12-02 17:44:54 -0500429 fHead->fColor != that->fHead->fColor) {
Michael Ludwigefc89d22020-11-05 11:43:10 -0500430 // This ensures all merged bitmap color text ops have a constant color
Brian Salomon7eae3e02018-08-07 14:02:38 +0000431 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500432 }
joshualitta751c972015-11-20 13:37:32 -0800433 }
434
Michael Ludwigefc89d22020-11-05 11:43:10 -0500435 fNumGlyphs += that->fNumGlyphs;
joshualitta751c972015-11-20 13:37:32 -0800436
Michael Ludwig64596c52020-11-05 12:39:13 -0500437 // After concat, that's geometry list is emptied so it will not unref the blobs when destructed
Herb Derby6b748e42020-12-02 17:44:54 -0500438 this->addGeometry(that->fHead);
439 that->fHead = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000440 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800441}
442
joshualitta751c972015-11-20 13:37:32 -0800443// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500444GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
445 const GrShaderCaps& caps,
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500446 const SkMatrix& localMatrix,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500447 const GrSurfaceProxyView* views,
448 unsigned int numActiveViews) const {
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500449 static constexpr int kDistanceAdjustLumShift = 5;
Robert Phillips841c9a52020-03-27 12:41:31 -0400450 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
451
joshualitta751c972015-11-20 13:37:32 -0800452 // see if we need to create a new effect
Michael Ludwig9597e2f2020-11-03 11:06:25 -0500453 if (this->isLCD()) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400454 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400455 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500456 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400457 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400458 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500459 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400460 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400461 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500462 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800463 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500464 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
465 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500466 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400467 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500468 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800469 } else {
joshualitta751c972015-11-20 13:37:32 -0800470#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400471 float correction = 0;
Michael Ludwigefc89d22020-11-05 11:43:10 -0500472 if (this->maskType() != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400473 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
474 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400475 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
476 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400477 }
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, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500480 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800481#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500482 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400483 GrSamplerState::Filter::kLinear, fDFGPFlags,
484 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800485#endif
486 }
joshualitta751c972015-11-20 13:37:32 -0800487}
joshualittddd22d82016-02-16 06:47:52 -0800488
Herb Derby4598fa12020-06-10 14:54:22 -0400489#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400490
Brian Salomoneebe7352020-12-09 16:37:04 -0500491GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc,
Herb Derbyc76d4092020-10-07 16:46:15 -0400492 const SkPaint& skPaint,
493 const SkFont& font,
494 const SkMatrixProvider& mtxProvider,
495 const char* text,
496 int x,
497 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400498 size_t textLen = (int)strlen(text);
499
Herb Derby861ef8f2021-02-26 16:49:06 -0500500 SkMatrix drawMatrix(mtxProvider.localToDevice());
501 drawMatrix.preTranslate(x, y);
Herb Derby4598fa12020-06-10 14:54:22 -0400502 auto drawOrigin = SkPoint::Make(x, y);
503 SkGlyphRunBuilder builder;
504 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
505
506 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400507 if (glyphRunList.empty()) {
508 return nullptr;
509 }
Herb Derby4598fa12020-06-10 14:54:22 -0400510
Brian Salomon70fe17e2020-11-30 14:33:58 -0500511 auto rContext = rtc->recordingContext();
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500512 GrSDFTControl control =
513 rContext->priv().getSDFTControl(rtc->surfaceProps().isUseDeviceIndependentFonts());
Herb Derby4598fa12020-06-10 14:54:22 -0400514
Brian Salomon70fe17e2020-11-30 14:33:58 -0500515 SkGlyphRunListPainter* painter = rtc->glyphRunPainter();
Herb Derby63fe8e52021-03-08 13:22:56 -0500516 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix, control, painter);
517
Herb Derby55f795e2021-02-05 13:45:05 -0500518 if (blob->subRunList().isEmpty()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400519 return nullptr;
520 }
Herb Derby4598fa12020-06-10 14:54:22 -0400521
Herb Derby55f795e2021-02-05 13:45:05 -0500522 GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -0500523 SkASSERT(subRun);
Herb Derbyc76d4092020-10-07 16:46:15 -0400524 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400525 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400526 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400527}
528
529GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
Brian Salomoneebe7352020-12-09 16:37:04 -0500530 // Setup dummy SkPaint / GrPaint / GrSurfaceDrawContext
531 auto rtc = GrSurfaceDrawContext::Make(
Herb Derby4598fa12020-06-10 14:54:22 -0400532 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
533
534 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
535
536 SkPaint skPaint;
537 skPaint.setColor(random->nextU());
538
539 SkFont font;
540 if (random->nextBool()) {
541 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
542 } else {
543 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
544 }
545 font.setSubpixel(random->nextBool());
546
547 const char* text = "The quick brown fox jumps over the lazy dog.";
548
549 // create some random x/y offsets, including negative offsets
550 static const int kMaxTrans = 1024;
551 int xPos = (random->nextU() % 2) * 2 - 1;
552 int yPos = (random->nextU() % 2) * 2 - 1;
553 int xInt = (random->nextU() % kMaxTrans) * xPos;
554 int yInt = (random->nextU() % kMaxTrans) * yPos;
555
556 return GrAtlasTextOp::CreateOpTestingOnly(
557 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
558}
559
560#endif