blob: 6714077e74c582e7811f470e242baef9ff8adc07 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkStrikeCache.h"
16#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrMemoryPool.h"
18#include "src/gpu/GrOpFlushState.h"
19#include "src/gpu/GrRecordingContextPriv.h"
Herb Derby4598fa12020-06-10 14:54:22 -040020#include "src/gpu/GrRenderTargetContext.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040021#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrResourceProvider.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
joshualitt60ce86d2015-11-23 13:08:22 -080034///////////////////////////////////////////////////////////////////////////////////////////////////
35
Herb Derby3c873af2020-04-29 15:56:07 -040036GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
37 GrPaint&& paint,
38 GrTextBlob::SubRun* subrun,
39 const SkMatrix& drawMatrix,
40 SkPoint drawOrigin,
41 const SkIRect& clipRect,
42 const SkPMColor4f& filteredColor,
43 SkColor luminanceColor,
44 bool useGammaCorrectDistanceTable,
45 uint32_t DFGPFlags)
46 : INHERITED(ClassID())
47 , fMaskType{maskType}
48 , fNeedsGlyphTransform{subrun->needsTransform()}
49 , fLuminanceColor{luminanceColor}
50 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
51 , fDFGPFlags{DFGPFlags}
52 , fGeoDataAllocSize{kMinGeometryAllocated}
53 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040054 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040055 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
56
57 // Unref handled in ~GrAtlasTextOp().
58 geometry.fBlob = SkRef(subrun->fBlob);
59 geometry.fSubRunPtr = subrun;
60 geometry.fDrawMatrix = drawMatrix;
61 geometry.fDrawOrigin = drawOrigin;
62 geometry.fClipRect = clipRect;
63 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
64 : filteredColor;
65 fGeoCount = 1;
66
67 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
68 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
69 // we treat this as a set of non-AA rects rendered with a texture.
70 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
71}
72
Herb Derby64391c42020-05-16 14:32:15 -040073void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
74 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
75 fDrawMatrix, fDrawOrigin, fClipRect);
76}
77
Chris Dalton1706cbf2019-05-21 19:35:29 -060078void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050079 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050080}
81
Brian Osman9a390ac2018-11-12 09:47:48 -050082#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -050083SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080084 SkString str;
85
86 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050087 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -080088 i,
Brian Osmancf860852018-10-31 14:04:39 -040089 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -050090 fGeoData[i].fDrawOrigin.x(),
91 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -080092 }
93
Brian Salomon44acb5b2017-07-18 19:59:24 -040094 str += fProcessors.dumpProcessors();
95 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -080096 return str;
97}
Brian Osman9a390ac2018-11-12 09:47:48 -050098#endif
joshualitta751c972015-11-20 13:37:32 -080099
Brian Salomon44acb5b2017-07-18 19:59:24 -0400100GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
101 return FixedFunctionFlags::kNone;
102}
103
Chris Dalton6ce447a2019-06-23 18:07:38 -0600104GrProcessorSet::Analysis GrAtlasTextOp::finalize(
105 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
106 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400107 GrProcessorAnalysisCoverage coverage;
108 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800109 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400110 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800111 } else {
Brian Osman09068252018-01-03 09:57:29 -0500112 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800113 }
joshualitta751c972015-11-20 13:37:32 -0800114 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800115 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400116 case kAliasedDistanceField_MaskType:
117 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400118 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800119 break;
120 case kLCDCoverageMask_MaskType:
121 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400122 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400123 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800124 break;
125 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400126 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400127 break;
joshualitta751c972015-11-20 13:37:32 -0800128 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700129 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600130 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
131 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400132 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700133 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800134}
135
Brian Salomon91326c32017-08-09 16:02:19 -0400136void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500137 auto resourceProvider = target->resourceProvider();
138
joshualitta751c972015-11-20 13:37:32 -0800139 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
140 // TODO actually only invert if we don't have RGBA
141 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500142 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800143 return;
144 }
145
Robert Phillips5a66efb2018-03-07 15:13:18 -0500146 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000147
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400148 GrMaskFormat maskFormat = this->maskFormat();
149
Greg Daniel9715b6c2019-12-10 15:03:10 -0500150 unsigned int numActiveViews;
151 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
152 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800153 SkDebugf("Could not allocate backing texture for atlas\n");
154 return;
155 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500156 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800157
Brian Salomon7eae3e02018-08-07 14:02:38 +0000158 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500159 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
160 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000161
Chris Dalton304e14d2020-03-17 14:29:06 -0600162 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500163 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600164 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400165 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
166 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500167 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000168 }
Brian Salomon49348902018-06-26 09:12:38 -0400169
170 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600171 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400172
Herb Derby1c5be7b2019-12-13 12:03:06 -0500173 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800174 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500175 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
176 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500177 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800178 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500179 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
180 : GrSamplerState::Filter::kNearest;
181 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
182 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
183 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800184 }
185
Herb Derby23f29762020-01-10 16:26:14 -0500186 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800187
Brian Salomon43cbd722020-01-03 22:09:12 -0500188 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500189 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
190 const int quadSize = vertexStride * kVerticesPerGlyph;
191 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
192
193 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
194 int quadBufferBegin = 0;
195 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
196
Robert Phillipsee08d522019-10-28 16:34:44 -0400197 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500198 void* vertices = target->makeVertexSpace(
199 vertexStride,
200 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
201 &flushInfo.fVertexBuffer,
202 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800203 if (!vertices || !flushInfo.fVertexBuffer) {
204 SkDebugf("Could not allocate vertices\n");
205 return;
206 }
207
Herb Derby23f29762020-01-10 16:26:14 -0500208 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
209 // quad buffer indices are calculated from this.
210 int totalGlyphsRegened = 0;
joshualitta751c972015-11-20 13:37:32 -0800211 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800212 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500213 auto subRun = args.fSubRunPtr;
214 SkASSERT((int)subRun->vertexStride() == vertexStride);
215
Robert Phillips207d24b2020-04-09 10:23:42 -0400216 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500217
Brian Osman1be2b7c2018-10-29 16:07:15 -0400218 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400219 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
220 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500221
222 // Where the subRun begins and ends relative to totalGlyphsRegened.
223 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400224 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500225
226 // Draw all the glyphs in the subRun.
227 while (totalGlyphsRegened < subRunEnd) {
228 // drawBegin and drawEnd are indices for the subRun on the
229 // interval [0, subRun->fGlyphs.size()).
230 int drawBegin = totalGlyphsRegened - subRunBegin;
231 // drawEnd is either the end of the subRun or the end of the current quad buffer.
232 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
233 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
234
235 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400236 if (!ok) {
237 return;
238 }
Herb Derby23f29762020-01-10 16:26:14 -0500239
240 // Update all the vertices for glyphsRegenerate glyphs.
241 if (glyphsRegenerated > 0) {
242 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500243 auto regeneratedQuadBuffer =
244 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400245 int subRunIndex = totalGlyphsRegened - subRunBegin;
246 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500247 }
Herb Derby23f29762020-01-10 16:26:14 -0500248
249 totalGlyphsRegened += glyphsRegenerated;
250 flushInfo.fGlyphsToFlush += glyphsRegenerated;
251
252 // regenerate() has stopped part way through a SubRun. This means that either the atlas
253 // or the quad buffer is full or both. There is a case were the flow through
254 // the loop is strange. If we run out of quad buffer space at the same time the
255 // SubRun ends, then this is not triggered which is the right result for the last
256 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
257 // will process no glyphs, and return to this point where the quad buffer will be
258 // expanded.
259 if (totalGlyphsRegened != subRunEnd) {
260 // Flush if not all glyphs drawn because either the quad buffer is full or the
261 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500262 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500263 if (totalGlyphsRegened == quadBufferEnd) {
264 // Quad buffer is full. Get more buffer.
265 quadBufferBegin = totalGlyphsRegened;
266 int quadBufferSize =
267 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
268 quadBufferEnd = quadBufferBegin + quadBufferSize;
269
270 vertices = target->makeVertexSpace(
271 vertexStride,
272 kVerticesPerGlyph * quadBufferSize,
273 &flushInfo.fVertexBuffer,
274 &flushInfo.fVertexOffset);
275 if (!vertices || !flushInfo.fVertexBuffer) {
276 SkDebugf("Could not allocate vertices\n");
277 return;
278 }
279 }
280 }
281 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500282 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500283 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800284}
285
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700286void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500287 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
288 std::move(fProcessors),
289 GrPipeline::InputFlags::kNone);
290
291 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700292}
293
Herb Derby4513cdd2020-01-31 13:28:49 -0500294void GrAtlasTextOp::createDrawForGeneratedGlyphs(
295 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500296 if (!flushInfo->fGlyphsToFlush) {
297 return;
298 }
299
Robert Phillips5a66efb2018-03-07 15:13:18 -0500300 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500301
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500302 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400303 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500304
Greg Daniel9715b6c2019-12-10 15:03:10 -0500305 unsigned int numActiveViews;
306 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
307 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500308 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500309 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500310 return;
311 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500312 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400313 // During preparation the number of atlas pages has increased.
314 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500315 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600316 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400317 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
318 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500319 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500320 // These will get unreffed when the previously recorded draws destruct.
321 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600322 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500323 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000324 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400325 if (this->usesDistanceFields()) {
326 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500327 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500328 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400329 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500330 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500331 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400332 }
333 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500334 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
335 : GrSamplerState::Filter::kNearest;
336 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400337 }
338 }
Brian Salomondbf70722019-02-07 11:31:24 -0500339 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600340 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600341 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
342 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
343 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600344 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600345 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800346 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
347 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500348 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800349}
350
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500351GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
352 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500353 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400354 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000355 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400356 }
357
joshualitta751c972015-11-20 13:37:32 -0800358 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000359 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800360 }
361
Herb Derby1c5be7b2019-12-13 12:03:06 -0500362 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
363 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500364
Mike Reed2c383152019-12-18 16:47:47 -0500365 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000366 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500367 }
368
Jim Van Verthb515ae72018-05-23 16:44:55 -0400369 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000370 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400371 }
372
373 if (fNeedsGlyphTransform &&
374 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000375 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400376 }
377
Brian Salomon5c6ac642017-12-19 11:09:32 -0500378 if (this->usesDistanceFields()) {
379 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000380 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800381 }
382
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400383 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000384 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800385 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500386 } else {
387 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000388 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500389 }
joshualitta751c972015-11-20 13:37:32 -0800390 }
391
Brian Salomon344ec422016-12-15 10:58:41 -0500392 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800393
Jim Van Verth56c37142017-10-31 14:44:25 -0400394 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800395 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800396
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400397 // We reallocate at a rate of 1.5x to try to get better total memory usage
398 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400399 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400400 while (newAllocSize < newGeoCount) {
401 newAllocSize += newAllocSize / 2;
402 }
joshualitta751c972015-11-20 13:37:32 -0800403 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400404 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800405 }
406
Brian Salomon344ec422016-12-15 10:58:41 -0500407 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
joshualitta751c972015-11-20 13:37:32 -0800408 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500409 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800410#ifdef SK_DEBUG
411 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500412 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800413 }
414#endif
415 that->fGeoCount = 0;
416 fGeoCount = newGeoCount;
417
Brian Salomon7eae3e02018-08-07 14:02:38 +0000418 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800419}
420
Herb Derby3c873af2020-04-29 15:56:07 -0400421static const int kDistanceAdjustLumShift = 5;
422
joshualitta751c972015-11-20 13:37:32 -0800423// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500424GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
425 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500426 const GrSurfaceProxyView* views,
427 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800428 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500429
430 SkMatrix localMatrix = SkMatrix::I();
431 if (this->usesLocalCoords()) {
432 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500433 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500434 (void)result;
435 }
joshualitta751c972015-11-20 13:37:32 -0800436
Robert Phillips841c9a52020-03-27 12:41:31 -0400437 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
438
joshualitta751c972015-11-20 13:37:32 -0800439 // see if we need to create a new effect
440 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400441 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400442 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500443 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400444 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400445 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500446 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400447 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400448 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500449 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800450 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500451 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
452 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500453 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500454 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500455 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800456 } else {
joshualitta751c972015-11-20 13:37:32 -0800457#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400458 float correction = 0;
459 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400460 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
461 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400462 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
463 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400464 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500465 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500466 GrSamplerState::Filter::kBilerp, correction,
467 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800468#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500469 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500470 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500471 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800472#endif
473 }
joshualitta751c972015-11-20 13:37:32 -0800474}
joshualittddd22d82016-02-16 06:47:52 -0800475
Herb Derby4598fa12020-06-10 14:54:22 -0400476#if GR_TEST_UTILS
477std::unique_ptr<GrDrawOp> GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
478 const SkPaint& skPaint,
479 const SkFont& font,
480 const SkMatrixProvider& mtxProvider,
481 const char* text,
482 int x,
483 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400484 size_t textLen = (int)strlen(text);
485
486 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
487
488 auto drawOrigin = SkPoint::Make(x, y);
489 SkGlyphRunBuilder builder;
490 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
491
492 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400493 if (glyphRunList.empty()) {
494 return nullptr;
495 }
Herb Derby4598fa12020-06-10 14:54:22 -0400496
Herb Derby411e7aa2020-07-09 16:02:08 -0400497 const GrRecordingContextPriv& contextPriv = rtc->priv().getContext()->priv();
498 GrSDFTOptions SDFOptions = contextPriv.SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400499
Herb Derby4598fa12020-06-10 14:54:22 -0400500 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400501 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby4598fa12020-06-10 14:54:22 -0400502 painter->processGlyphRunList(
Herb Derbyd5764642020-07-08 09:57:11 -0400503 glyphRunList, drawMatrix, rtc->surfaceProps(),
Herb Derby4598fa12020-06-10 14:54:22 -0400504 contextPriv.caps()->shaderCaps()->supportsDistanceFieldText(),
505 SDFOptions, blob.get());
Ben Wagner525e8762020-07-09 16:19:35 -0400506 if (!blob->firstSubRun()) {
507 return nullptr;
508 }
Herb Derby4598fa12020-06-10 14:54:22 -0400509
Herb Derby411e7aa2020-07-09 16:02:08 -0400510 std::unique_ptr<GrDrawOp> op;
511 std::tie(std::ignore, op) =
512 rtc->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, blob->firstSubRun());
513 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400514}
515
516GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
517 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
518 auto rtc = GrRenderTargetContext::Make(
519 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
520
521 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
522
523 SkPaint skPaint;
524 skPaint.setColor(random->nextU());
525
526 SkFont font;
527 if (random->nextBool()) {
528 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
529 } else {
530 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
531 }
532 font.setSubpixel(random->nextBool());
533
534 const char* text = "The quick brown fox jumps over the lazy dog.";
535
536 // create some random x/y offsets, including negative offsets
537 static const int kMaxTrans = 1024;
538 int xPos = (random->nextU() % 2) * 2 - 1;
539 int yPos = (random->nextU() % 2) * 2 - 1;
540 int xInt = (random->nextU() % kMaxTrans) * xPos;
541 int yInt = (random->nextU() % kMaxTrans) * yPos;
542
543 return GrAtlasTextOp::CreateOpTestingOnly(
544 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
545}
546
547#endif
548
549