blob: e90f21d01a81ea8ec908520dff3ceb61cde9d59d [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"
Herb Derby4598fa12020-06-10 14:54:22 -040021#include "src/gpu/GrRenderTargetContext.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040022#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceProvider.h"
Herb Derby7a1d9422020-07-06 14:18:01 -040024#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/effects/GrBitmapTextGeoProc.h"
26#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050027#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040029#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080030
Herb Derbya08bde62020-06-12 15:46:06 -040031#if GR_TEST_UTILS
32#include "src/gpu/GrDrawOpTest.h"
33#endif
34
joshualitt60ce86d2015-11-23 13:08:22 -080035///////////////////////////////////////////////////////////////////////////////////////////////////
36
Herb Derby3c873af2020-04-29 15:56:07 -040037GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
Herb Derby268e48b2020-07-16 12:56:58 -040038 bool needsTransform,
39 int glyphCount,
40 SkRect deviceRect,
Herb Derby1d17e492020-07-21 11:45:04 -040041 const Geometry& geo,
42 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040043 : INHERITED{ClassID()}
44 , fMaskType{maskType}
45 , fNeedsGlyphTransform{needsTransform}
46 , fLuminanceColor{0}
47 , fUseGammaCorrectDistanceTable{false}
48 , fDFGPFlags{0}
49 , fGeoDataAllocSize{kMinGeometryAllocated}
50 , fProcessors{std::move(paint)}
51 , fNumGlyphs{glyphCount} {
Herb Derby1d17e492020-07-21 11:45:04 -040052 new (&fGeoData[0]) Geometry{geo};
Herb Derby268e48b2020-07-16 12:56:58 -040053 fGeoCount = 1;
54
55 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
56 // we treat this as a set of non-AA rects rendered with a texture.
57 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
58}
59
60GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
61 bool needsTransform,
62 int glyphCount,
63 SkRect deviceRect,
Herb Derby3c873af2020-04-29 15:56:07 -040064 SkColor luminanceColor,
65 bool useGammaCorrectDistanceTable,
Herb Derby268e48b2020-07-16 12:56:58 -040066 uint32_t DFGPFlags,
Herb Derby1d17e492020-07-21 11:45:04 -040067 const Geometry& geo,
68 GrPaint&& paint)
Herb Derby268e48b2020-07-16 12:56:58 -040069 : INHERITED{ClassID()}
Herb Derby3c873af2020-04-29 15:56:07 -040070 , fMaskType{maskType}
Herb Derby268e48b2020-07-16 12:56:58 -040071 , fNeedsGlyphTransform{needsTransform}
Herb Derby3c873af2020-04-29 15:56:07 -040072 , fLuminanceColor{luminanceColor}
73 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
74 , fDFGPFlags{DFGPFlags}
75 , fGeoDataAllocSize{kMinGeometryAllocated}
76 , fProcessors{std::move(paint)}
Herb Derby268e48b2020-07-16 12:56:58 -040077 , fNumGlyphs{glyphCount} {
Herb Derby1d17e492020-07-21 11:45:04 -040078 new (&fGeoData[0]) Geometry{geo};
Herb Derby3c873af2020-04-29 15:56:07 -040079 fGeoCount = 1;
80
Herb Derby3c873af2020-04-29 15:56:07 -040081 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
82 // we treat this as a set of non-AA rects rendered with a texture.
Herb Derby268e48b2020-07-16 12:56:58 -040083 this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo);
Herb Derby3c873af2020-04-29 15:56:07 -040084}
85
Herb Derby64391c42020-05-16 14:32:15 -040086void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
Herb Derby43ad7912020-07-20 16:14:19 -040087 fSubRun.fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
88 fDrawMatrix, fDrawOrigin, fClipRect);
Herb Derby64391c42020-05-16 14:32:15 -040089}
90
Chris Dalton1706cbf2019-05-21 19:35:29 -060091void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -050092 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050093}
94
John Stiles8d9bf642020-08-12 15:07:45 -040095#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -040096SkString GrAtlasTextOp::onDumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080097 SkString str;
98
99 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500100 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800101 i,
Brian Osmancf860852018-10-31 14:04:39 -0400102 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500103 fGeoData[i].fDrawOrigin.x(),
104 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800105 }
106
Brian Salomon44acb5b2017-07-18 19:59:24 -0400107 str += fProcessors.dumpProcessors();
joshualitta751c972015-11-20 13:37:32 -0800108 return str;
109}
Brian Osman9a390ac2018-11-12 09:47:48 -0500110#endif
joshualitta751c972015-11-20 13:37:32 -0800111
Brian Salomon44acb5b2017-07-18 19:59:24 -0400112GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
113 return FixedFunctionFlags::kNone;
114}
115
Chris Dalton6ce447a2019-06-23 18:07:38 -0600116GrProcessorSet::Analysis GrAtlasTextOp::finalize(
117 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
118 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400119 GrProcessorAnalysisCoverage coverage;
120 GrProcessorAnalysisColor color;
Michael Ludwig136d8782020-11-03 11:04:16 -0500121 if (fMaskType == MaskType::kColorBitmap) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400122 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800123 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500124 // finalize() is called before any merging is done, so at this point there's at most one
125 // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors.
Brian Osman09068252018-01-03 09:57:29 -0500126 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800127 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500128
joshualitta751c972015-11-20 13:37:32 -0800129 switch (fMaskType) {
Michael Ludwig136d8782020-11-03 11:04:16 -0500130 case MaskType::kGrayscaleCoverage:
131 case MaskType::kAliasedDistanceField:
132 case MaskType::kGrayscaleDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400133 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800134 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500135 case MaskType::kLCDCoverage:
136 case MaskType::kLCDDistanceField:
137 case MaskType::kLCDBGRDistanceField:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400138 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800139 break;
Michael Ludwig136d8782020-11-03 11:04:16 -0500140 case MaskType::kColorBitmap:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400141 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400142 break;
joshualitta751c972015-11-20 13:37:32 -0800143 }
Michael Ludwig136d8782020-11-03 11:04:16 -0500144
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700145 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600146 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
147 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400148 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700149 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800150}
151
Brian Salomon91326c32017-08-09 16:02:19 -0400152void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500153 auto resourceProvider = target->resourceProvider();
154
joshualitta751c972015-11-20 13:37:32 -0800155 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
156 // TODO actually only invert if we don't have RGBA
157 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500158 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800159 return;
160 }
161
Robert Phillips5a66efb2018-03-07 15:13:18 -0500162 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000163
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400164 GrMaskFormat maskFormat = this->maskFormat();
165
Greg Daniel9715b6c2019-12-10 15:03:10 -0500166 unsigned int numActiveViews;
167 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
168 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800169 SkDebugf("Could not allocate backing texture for atlas\n");
170 return;
171 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500172 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800173
Brian Salomon7eae3e02018-08-07 14:02:38 +0000174 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500175 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
176 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177
Chris Dalton304e14d2020-03-17 14:29:06 -0600178 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500179 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600180 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400181 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
182 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500183 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000184 }
Brian Salomon49348902018-06-26 09:12:38 -0400185
186 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600187 flushInfo.fPrimProcProxies = primProcProxies;
Herb Derbyfd894ff2020-07-15 13:23:33 -0400188 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400189
Herb Derby1c5be7b2019-12-13 12:03:06 -0500190 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800191 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500192 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
193 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500194 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800195 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400196 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500197 : GrSamplerState::Filter::kNearest;
198 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
199 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
200 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800201 }
202
Herb Derbyfd894ff2020-07-15 13:23:33 -0400203 const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800204
Brian Salomon43cbd722020-01-03 22:09:12 -0500205 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500206 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
207 const int quadSize = vertexStride * kVerticesPerGlyph;
208 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
209
Herb Derbyfd894ff2020-07-15 13:23:33 -0400210 int allGlyphsCursor = 0;
211 const int allGlyphsEnd = this->numGlyphs();
212 int quadCursor;
213 int quadEnd;
214 char* vertices;
Herb Derby23f29762020-01-10 16:26:14 -0500215
Herb Derbyfd894ff2020-07-15 13:23:33 -0400216 auto resetVertexBuffer = [&] {
217 quadCursor = 0;
218 quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor);
joshualitta751c972015-11-20 13:37:32 -0800219
Herb Derbyfd894ff2020-07-15 13:23:33 -0400220 vertices = (char*)target->makeVertexSpace(
221 vertexStride,
222 kVerticesPerGlyph * quadEnd,
223 &flushInfo.fVertexBuffer,
224 &flushInfo.fVertexOffset);
225
226 if (!vertices || !flushInfo.fVertexBuffer) {
227 SkDebugf("Could not allocate vertices\n");
228 return false;
229 }
230 return true;
231 };
232
233 resetVertexBuffer();
234
Herb Derbya80ce1a2020-05-26 12:58:59 -0400235 for (const Geometry& geo : SkSpan(fGeoData.get(), fGeoCount)) {
Herb Derby43ad7912020-07-20 16:14:19 -0400236 const GrAtlasSubRun& subRun = geo.fSubRun;
237 SkASSERT((int)subRun.vertexStride() == vertexStride);
Herb Derby62b12fe2020-01-14 17:57:24 -0500238
Herb Derby43ad7912020-07-20 16:14:19 -0400239 const int subRunEnd = subRun.glyphCount();
Herb Derbyfd894ff2020-07-15 13:23:33 -0400240 for (int subRunCursor = 0; subRunCursor < subRunEnd;) {
241 // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder
242 // of the glyphs to fill the vertex buffer.
243 int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor);
Herb Derby43ad7912020-07-20 16:14:19 -0400244 auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target);
Herb Derby23f29762020-01-10 16:26:14 -0500245 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400246 if (!ok) {
247 return;
248 }
Herb Derby23f29762020-01-10 16:26:14 -0500249
Herb Derbyfd894ff2020-07-15 13:23:33 -0400250 geo.fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated);
Herb Derby23f29762020-01-10 16:26:14 -0500251
Herb Derbyfd894ff2020-07-15 13:23:33 -0400252 subRunCursor += glyphsRegenerated;
253 quadCursor += glyphsRegenerated;
254 allGlyphsCursor += glyphsRegenerated;
Herb Derby23f29762020-01-10 16:26:14 -0500255 flushInfo.fGlyphsToFlush += glyphsRegenerated;
256
Herb Derbyfd894ff2020-07-15 13:23:33 -0400257 if (quadCursor == quadEnd || subRunCursor < subRunEnd) {
258 // Flush if not all the glyphs are drawn because either the quad buffer is full or
259 // the atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500260 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derbyfd894ff2020-07-15 13:23:33 -0400261 if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) {
262 // If the vertex buffer is full and there are still glyphs to draw then
263 // get a new buffer.
264 if(!resetVertexBuffer()) {
Herb Derby23f29762020-01-10 16:26:14 -0500265 return;
266 }
267 }
268 }
269 }
Herb Derbyfd894ff2020-07-15 13:23:33 -0400270 }
joshualitta751c972015-11-20 13:37:32 -0800271}
272
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700273void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500274 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
275 std::move(fProcessors),
276 GrPipeline::InputFlags::kNone);
277
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600278 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
279 &GrUserStencilSettings::kUnused);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700280}
281
Herb Derby4513cdd2020-01-31 13:28:49 -0500282void GrAtlasTextOp::createDrawForGeneratedGlyphs(
283 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500284 if (!flushInfo->fGlyphsToFlush) {
285 return;
286 }
287
Robert Phillips5a66efb2018-03-07 15:13:18 -0500288 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500289
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500290 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400291 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500292
Greg Daniel9715b6c2019-12-10 15:03:10 -0500293 unsigned int numActiveViews;
294 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
295 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500296 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500297 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500298 return;
299 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500300 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400301 // During preparation the number of atlas pages has increased.
302 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600304 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400305 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
306 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500307 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500308 // These will get unreffed when the previously recorded draws destruct.
309 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600310 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500311 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000312 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400313 if (this->usesDistanceFields()) {
314 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500315 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400316 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400317 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500318 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomona3b02f52020-07-15 16:02:01 -0400319 views, numActiveViews, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 }
321 } else {
Brian Salomona3b02f52020-07-15 16:02:01 -0400322 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear
Brian Salomonccb61422020-01-09 10:46:36 -0500323 : GrSamplerState::Filter::kNearest;
324 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400325 }
326 }
Brian Salomondbf70722019-02-07 11:31:24 -0500327 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600328 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600329 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
330 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
331 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600332 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600333 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800334 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
335 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500336 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800337}
338
Herb Derbye25c3002020-10-27 15:57:27 -0400339GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500340 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400341 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000342 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400343 }
344
joshualitta751c972015-11-20 13:37:32 -0800345 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000346 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800347 }
348
Herb Derby1c5be7b2019-12-13 12:03:06 -0500349 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
350 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500351
Mike Reed2c383152019-12-18 16:47:47 -0500352 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000353 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500354 }
355
Jim Van Verthb515ae72018-05-23 16:44:55 -0400356 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000357 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400358 }
359
360 if (fNeedsGlyphTransform &&
361 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000362 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400363 }
364
Brian Salomon5c6ac642017-12-19 11:09:32 -0500365 if (this->usesDistanceFields()) {
366 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000367 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800368 }
369
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400370 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000371 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800372 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500373 } else {
Michael Ludwig136d8782020-11-03 11:04:16 -0500374 if (fMaskType == MaskType::kColorBitmap && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000375 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500376 }
joshualitta751c972015-11-20 13:37:32 -0800377 }
378
Brian Salomon344ec422016-12-15 10:58:41 -0500379 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800380
Jim Van Verth56c37142017-10-31 14:44:25 -0400381 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800382 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800383
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400384 // We reallocate at a rate of 1.5x to try to get better total memory usage
385 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400386 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400387 while (newAllocSize < newGeoCount) {
388 newAllocSize += newAllocSize / 2;
389 }
joshualitta751c972015-11-20 13:37:32 -0800390 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400391 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800392 }
393
Brian Salomon344ec422016-12-15 10:58:41 -0500394 // 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 -0800395 // it doesn't try to unref them.
Herb Derby1d17e492020-07-21 11:45:04 -0400396 for (int i = 0; i < that->fGeoCount; i++) {
397 new (&fGeoData[fGeoCount + i]) Geometry{that->fGeoData[i]};
joshualitta751c972015-11-20 13:37:32 -0800398 }
Herb Derby1d17e492020-07-21 11:45:04 -0400399
joshualitta751c972015-11-20 13:37:32 -0800400 that->fGeoCount = 0;
401 fGeoCount = newGeoCount;
402
Brian Salomon7eae3e02018-08-07 14:02:38 +0000403 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800404}
405
Herb Derby3c873af2020-04-29 15:56:07 -0400406static const int kDistanceAdjustLumShift = 5;
407
joshualitta751c972015-11-20 13:37:32 -0800408// TODO trying to figure out why lcd is so whack
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500409GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
410 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500411 const GrSurfaceProxyView* views,
412 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800413 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500414
415 SkMatrix localMatrix = SkMatrix::I();
416 if (this->usesLocalCoords()) {
417 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500418 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500419 (void)result;
420 }
joshualitta751c972015-11-20 13:37:32 -0800421
Robert Phillips841c9a52020-03-27 12:41:31 -0400422 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
423
joshualitta751c972015-11-20 13:37:32 -0800424 // see if we need to create a new effect
425 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400426 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400427 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500428 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400429 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400430 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500431 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400432 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400433 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500434 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800435 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500436 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
437 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500438 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400439 GrSamplerState::Filter::kLinear, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500440 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800441 } else {
joshualitta751c972015-11-20 13:37:32 -0800442#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400443 float correction = 0;
Michael Ludwig136d8782020-11-03 11:04:16 -0500444 if (fMaskType != MaskType::kAliasedDistanceField) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400445 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
446 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400447 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
448 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400449 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500450 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400451 GrSamplerState::Filter::kLinear, correction,
Brian Salomonccb61422020-01-09 10:46:36 -0500452 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800453#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500454 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomona3b02f52020-07-15 16:02:01 -0400455 GrSamplerState::Filter::kLinear, fDFGPFlags,
456 localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800457#endif
458 }
joshualitta751c972015-11-20 13:37:32 -0800459}
joshualittddd22d82016-02-16 06:47:52 -0800460
Herb Derby4598fa12020-06-10 14:54:22 -0400461#if GR_TEST_UTILS
Herb Derbyc76d4092020-10-07 16:46:15 -0400462
463GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrRenderTargetContext* rtc,
464 const SkPaint& skPaint,
465 const SkFont& font,
466 const SkMatrixProvider& mtxProvider,
467 const char* text,
468 int x,
469 int y) {
Herb Derby4598fa12020-06-10 14:54:22 -0400470 size_t textLen = (int)strlen(text);
471
472 const SkMatrix& drawMatrix(mtxProvider.localToDevice());
473
474 auto drawOrigin = SkPoint::Make(x, y);
475 SkGlyphRunBuilder builder;
476 builder.drawTextUTF8(skPaint, font, text, textLen, drawOrigin);
477
478 auto glyphRunList = builder.useGlyphRunList();
Ben Wagner525e8762020-07-09 16:19:35 -0400479 if (glyphRunList.empty()) {
480 return nullptr;
481 }
Herb Derby4598fa12020-06-10 14:54:22 -0400482
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400483
484 auto rContext = rtc->priv().recordingContext();
485 GrSDFTOptions SDFOptions = rContext->priv().SDFTOptions();
Herb Derby4598fa12020-06-10 14:54:22 -0400486
Herb Derby4598fa12020-06-10 14:54:22 -0400487 sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby411e7aa2020-07-09 16:02:08 -0400488 SkGlyphRunListPainter* painter = rtc->priv().testingOnly_glyphRunPainter();
Herb Derby4598fa12020-06-10 14:54:22 -0400489 painter->processGlyphRunList(
Herb Derbyd5764642020-07-08 09:57:11 -0400490 glyphRunList, drawMatrix, rtc->surfaceProps(),
Robert Phillipsa9306eb2020-07-21 13:01:25 -0400491 rContext->priv().caps()->shaderCaps()->supportsDistanceFieldText(),
Herb Derby4598fa12020-06-10 14:54:22 -0400492 SDFOptions, blob.get());
Herb Derby342273c2020-07-13 10:22:32 -0400493 if (!blob->subRunList().head()) {
Ben Wagner525e8762020-07-09 16:19:35 -0400494 return nullptr;
495 }
Herb Derby4598fa12020-06-10 14:54:22 -0400496
Herb Derby252a3c02020-07-14 12:15:34 -0400497 GrAtlasSubRun* subRun = static_cast<GrAtlasSubRun*>(blob->subRunList().head());
Herb Derbyc76d4092020-10-07 16:46:15 -0400498 GrOp::Owner op;
Herb Derby252a3c02020-07-14 12:15:34 -0400499 std::tie(std::ignore, op) = subRun->makeAtlasTextOp(nullptr, mtxProvider, glyphRunList, rtc);
Herb Derby411e7aa2020-07-09 16:02:08 -0400500 return op;
Herb Derby4598fa12020-06-10 14:54:22 -0400501}
502
503GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
504 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
505 auto rtc = GrRenderTargetContext::Make(
506 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1024, 1024});
507
508 SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random));
509
510 SkPaint skPaint;
511 skPaint.setColor(random->nextU());
512
513 SkFont font;
514 if (random->nextBool()) {
515 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
516 } else {
517 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
518 }
519 font.setSubpixel(random->nextBool());
520
521 const char* text = "The quick brown fox jumps over the lazy dog.";
522
523 // create some random x/y offsets, including negative offsets
524 static const int kMaxTrans = 1024;
525 int xPos = (random->nextU() % 2) * 2 - 1;
526 int yPos = (random->nextU() % 2) * 2 - 1;
527 int xInt = (random->nextU() % kMaxTrans) * xPos;
528 int yInt = (random->nextU() % kMaxTrans) * yPos;
529
530 return GrAtlasTextOp::CreateOpTestingOnly(
531 rtc.get(), skPaint, font, matrixProvider, text, xInt, yInt);
532}
533
534#endif