blob: 47d50b9a61baaf621e4c6a49a0bff505162f522b [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,
Herb Derby252a3c02020-07-14 12:15:34 -040038 GrAtlasSubRun* subrun,
Herb Derby3c873af2020-04-29 15:56:07 -040039 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
Herb Derby23f29762020-01-10 16:26:14 -0500219
220 // Where the subRun begins and ends relative to totalGlyphsRegened.
221 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400222 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500223
224 // Draw all the glyphs in the subRun.
225 while (totalGlyphsRegened < subRunEnd) {
226 // drawBegin and drawEnd are indices for the subRun on the
227 // interval [0, subRun->fGlyphs.size()).
228 int drawBegin = totalGlyphsRegened - subRunBegin;
229 // drawEnd is either the end of the subRun or the end of the current quad buffer.
230 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
Herb Derbyd68ad7d2020-07-13 15:08:32 -0400231 auto[ok, glyphsRegenerated] = subRun->regenerateAtlas(drawBegin, drawEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500232
233 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400234 if (!ok) {
235 return;
236 }
Herb Derby23f29762020-01-10 16:26:14 -0500237
238 // Update all the vertices for glyphsRegenerate glyphs.
239 if (glyphsRegenerated > 0) {
240 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500241 auto regeneratedQuadBuffer =
242 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400243 int subRunIndex = totalGlyphsRegened - subRunBegin;
244 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500245 }
Herb Derby23f29762020-01-10 16:26:14 -0500246
247 totalGlyphsRegened += glyphsRegenerated;
248 flushInfo.fGlyphsToFlush += glyphsRegenerated;
249
250 // regenerate() has stopped part way through a SubRun. This means that either the atlas
251 // or the quad buffer is full or both. There is a case were the flow through
252 // the loop is strange. If we run out of quad buffer space at the same time the
253 // SubRun ends, then this is not triggered which is the right result for the last
254 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
255 // will process no glyphs, and return to this point where the quad buffer will be
256 // expanded.
257 if (totalGlyphsRegened != subRunEnd) {
258 // Flush if not all glyphs drawn because either the quad buffer is full or the
259 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500260 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500261 if (totalGlyphsRegened == quadBufferEnd) {
262 // Quad buffer is full. Get more buffer.
263 quadBufferBegin = totalGlyphsRegened;
264 int quadBufferSize =
265 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
266 quadBufferEnd = quadBufferBegin + quadBufferSize;
267
268 vertices = target->makeVertexSpace(
269 vertexStride,
270 kVerticesPerGlyph * quadBufferSize,
271 &flushInfo.fVertexBuffer,
272 &flushInfo.fVertexOffset);
273 if (!vertices || !flushInfo.fVertexBuffer) {
274 SkDebugf("Could not allocate vertices\n");
275 return;
276 }
277 }
278 }
279 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500280 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500281 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800282}
283
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700284void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500285 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
286 std::move(fProcessors),
287 GrPipeline::InputFlags::kNone);
288
289 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700290}
291
Herb Derby4513cdd2020-01-31 13:28:49 -0500292void GrAtlasTextOp::createDrawForGeneratedGlyphs(
293 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500294 if (!flushInfo->fGlyphsToFlush) {
295 return;
296 }
297
Robert Phillips5a66efb2018-03-07 15:13:18 -0500298 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500299
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500300 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400301 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500302
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 unsigned int numActiveViews;
304 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
305 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500306 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500307 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500308 return;
309 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500310 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400311 // During preparation the number of atlas pages has increased.
312 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500313 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600314 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400315 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
316 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500317 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500318 // These will get unreffed when the previously recorded draws destruct.
319 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600320 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500321 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000322 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400323 if (this->usesDistanceFields()) {
324 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500325 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500326 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400327 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500328 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500329 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400330 }
331 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500332 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
333 : GrSamplerState::Filter::kNearest;
334 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400335 }
336 }
Brian Salomondbf70722019-02-07 11:31:24 -0500337 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600338 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600339 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
340 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
341 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600342 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600343 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800344 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
345 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500346 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800347}
348
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500349GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
350 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500351 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400352 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000353 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400354 }
355
joshualitta751c972015-11-20 13:37:32 -0800356 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000357 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800358 }
359
Herb Derby1c5be7b2019-12-13 12:03:06 -0500360 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
361 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500362
Mike Reed2c383152019-12-18 16:47:47 -0500363 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000364 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500365 }
366
Jim Van Verthb515ae72018-05-23 16:44:55 -0400367 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000368 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400369 }
370
371 if (fNeedsGlyphTransform &&
372 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000373 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400374 }
375
Brian Salomon5c6ac642017-12-19 11:09:32 -0500376 if (this->usesDistanceFields()) {
377 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000378 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800379 }
380
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400381 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000382 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800383 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500384 } else {
385 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000386 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500387 }
joshualitta751c972015-11-20 13:37:32 -0800388 }
389
Brian Salomon344ec422016-12-15 10:58:41 -0500390 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800391
Jim Van Verth56c37142017-10-31 14:44:25 -0400392 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800393 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800394
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400395 // We reallocate at a rate of 1.5x to try to get better total memory usage
396 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400397 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400398 while (newAllocSize < newGeoCount) {
399 newAllocSize += newAllocSize / 2;
400 }
joshualitta751c972015-11-20 13:37:32 -0800401 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400402 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800403 }
404
Brian Salomon344ec422016-12-15 10:58:41 -0500405 // 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 -0800406 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500407 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800408#ifdef SK_DEBUG
409 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500410 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800411 }
412#endif
413 that->fGeoCount = 0;
414 fGeoCount = newGeoCount;
415
Brian Salomon7eae3e02018-08-07 14:02:38 +0000416 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800417}
418
Herb Derby3c873af2020-04-29 15:56:07 -0400419static const int kDistanceAdjustLumShift = 5;
420
joshualitta751c972015-11-20 13:37:32 -0800421// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500422GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
423 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500424 const GrSurfaceProxyView* views,
425 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800426 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500427
428 SkMatrix localMatrix = SkMatrix::I();
429 if (this->usesLocalCoords()) {
430 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500431 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500432 (void)result;
433 }
joshualitta751c972015-11-20 13:37:32 -0800434
Robert Phillips841c9a52020-03-27 12:41:31 -0400435 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
436
joshualitta751c972015-11-20 13:37:32 -0800437 // see if we need to create a new effect
438 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400439 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400440 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500441 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400442 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400443 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500444 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400445 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400446 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500447 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800448 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500449 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
450 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500451 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500452 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500453 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800454 } else {
joshualitta751c972015-11-20 13:37:32 -0800455#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400456 float correction = 0;
457 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400458 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
459 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400460 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
461 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400462 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500463 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500464 GrSamplerState::Filter::kBilerp, correction,
465 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800466#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500467 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500468 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500469 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800470#endif
471 }
joshualitta751c972015-11-20 13:37:32 -0800472}
joshualittddd22d82016-02-16 06:47:52 -0800473
Herb Derby4598fa12020-06-10 14:54:22 -0400474#if GR_TEST_UTILS
475std::unique_ptr<GrDrawOp> GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
476 const SkPaint& skPaint,
477 const SkFont& font,
478 const SkMatrixProvider& mtxProvider,
479 const char* text,
480 int x,
481 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400482 size_t textLen = (int)strlen(text);
483
484 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
485
486 auto drawOrigin = SkPoint::Make(x, y);
487 SkGlyphRunBuilder builder;
488 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
489
490 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400491 if (glyphRunList.empty()) {
492 return nullptr;
493 }
Herb Derby4598fa12020-06-10 14:54:22 -0400494
Herb Derby411e7aa2020-07-09 16:02:08 -0400495 const GrRecordingContextPriv& contextPriv = rtc->priv().getContext()->priv();
496 GrSDFTOptions SDFOptions = contextPriv.SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400497
Herb Derby4598fa12020-06-10 14:54:22 -0400498 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400499 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby4598fa12020-06-10 14:54:22 -0400500 painter->processGlyphRunList(
Herb Derbyd5764642020-07-08 09:57:11 -0400501 glyphRunList, drawMatrix, rtc->surfaceProps(),
Herb Derby4598fa12020-06-10 14:54:22 -0400502 contextPriv.caps()->shaderCaps()->supportsDistanceFieldText(),
503 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400504 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400505 return nullptr;
506 }
Herb Derby4598fa12020-06-10 14:54:22 -0400507
Herb Derby252a3c02020-07-14 12:15:34 -0400508 GrAtlasSubRun* subRun = static_cast<GrAtlasSubRun*>(blob->subRunList().head());
Herb Derby411e7aa2020-07-09 16:02:08 -0400509 std::unique_ptr<GrDrawOp> op;
Herb Derby252a3c02020-07-14 12:15:34 -0400510 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400511 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400512}
513
514GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
515 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
516 auto rtc = GrRenderTargetContext::Make(
517 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
518
519 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
520
521 SkPaint skPaint;
522 skPaint.setColor(random->nextU());
523
524 SkFont font;
525 if (random->nextBool()) {
526 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
527 } else {
528 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
529 }
530 font.setSubpixel(random->nextBool());
531
532 const char* text = "The quick brown fox jumps over the lazy dog.";
533
534 // create some random x/y offsets, including negative offsets
535 static const int kMaxTrans = 1024;
536 int xPos = (random->nextU() % 2) * 2 - 1;
537 int yPos = (random->nextU() % 2) * 2 - 1;
538 int xInt = (random->nextU() % kMaxTrans) * xPos;
539 int yInt = (random->nextU() % kMaxTrans) * yPos;
540
541 return GrAtlasTextOp::CreateOpTestingOnly(
542 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
543}
544
545#endif
546
547