blob: 32e1d85e3327689522352adb10d350c1c5e1de5a [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"
11#include "include/private/GrRecordingContext.h"
12#include "src/core/SkMathPriv.h"
13#include "src/core/SkMatrixPriv.h"
14#include "src/core/SkStrikeCache.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrMemoryPool.h"
17#include "src/gpu/GrOpFlushState.h"
18#include "src/gpu/GrRecordingContextPriv.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/effects/GrBitmapTextGeoProc.h"
21#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050022#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/text/GrAtlasManager.h"
Robert Phillips841c9a52020-03-27 12:41:31 -040024#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
joshualitta751c972015-11-20 13:37:32 -080025
joshualitt60ce86d2015-11-23 13:08:22 -080026///////////////////////////////////////////////////////////////////////////////////////////////////
27
Herb Derby3c873af2020-04-29 15:56:07 -040028GrAtlasTextOp::GrAtlasTextOp(MaskType maskType,
29 GrPaint&& paint,
30 GrTextBlob::SubRun* subrun,
31 const SkMatrix& drawMatrix,
32 SkPoint drawOrigin,
33 const SkIRect& clipRect,
34 const SkPMColor4f& filteredColor,
35 SkColor luminanceColor,
36 bool useGammaCorrectDistanceTable,
37 uint32_t DFGPFlags)
38 : INHERITED(ClassID())
39 , fMaskType{maskType}
40 , fNeedsGlyphTransform{subrun->needsTransform()}
41 , fLuminanceColor{luminanceColor}
42 , fUseGammaCorrectDistanceTable{useGammaCorrectDistanceTable}
43 , fDFGPFlags{DFGPFlags}
44 , fGeoDataAllocSize{kMinGeometryAllocated}
45 , fProcessors{std::move(paint)}
Herb Derbyd5cbc1e2020-05-16 13:45:55 -040046 , fNumGlyphs{subrun->glyphCount()} {
Herb Derby3c873af2020-04-29 15:56:07 -040047 GrAtlasTextOp::Geometry& geometry = fGeoData[0];
48
49 // Unref handled in ~GrAtlasTextOp().
50 geometry.fBlob = SkRef(subrun->fBlob);
51 geometry.fSubRunPtr = subrun;
52 geometry.fDrawMatrix = drawMatrix;
53 geometry.fDrawOrigin = drawOrigin;
54 geometry.fClipRect = clipRect;
55 geometry.fColor = subrun->maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE
56 : filteredColor;
57 fGeoCount = 1;
58
59 SkRect bounds = subrun->deviceRect(drawMatrix, drawOrigin);
60 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
61 // we treat this as a set of non-AA rects rendered with a texture.
62 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
63}
64
Herb Derby64391c42020-05-16 14:32:15 -040065void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const {
66 fSubRunPtr->fillVertexData(dst, offset, count, fColor.toBytes_RGBA(),
67 fDrawMatrix, fDrawOrigin, fClipRect);
68}
69
Robert Phillipsb97da532019-02-12 15:24:12 -050070std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeBitmap(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040071 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -040072 GrTextBlob::SubRun* subrun,
73 const SkMatrix& drawMatrix,
74 SkPoint drawOrigin,
75 const SkIRect& clipRect,
76 const SkPMColor4f& filteredColor) {
77 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040078
Herb Derby3c873af2020-04-29 15:56:07 -040079 MaskType maskType = [&]() {
80 switch (subrun->maskFormat()) {
81 case kA8_GrMaskFormat: return kGrayscaleCoverageMask_MaskType;
82 case kA565_GrMaskFormat: return kLCDCoverageMask_MaskType;
83 case kARGB_GrMaskFormat: return kColorBitmapMask_MaskType;
84 // Needed to placate some compilers.
85 default: return kGrayscaleCoverageMask_MaskType;
Robert Phillips7c525e62018-06-12 10:11:12 -040086 }
Herb Derby3c873af2020-04-29 15:56:07 -040087 }();
88
89 return pool->allocate<GrAtlasTextOp>(maskType,
90 std::move(paint),
91 subrun,
92 drawMatrix,
93 drawOrigin,
94 clipRect,
95 filteredColor,
96 0,
97 false,
98 0);
99}
Robert Phillips7c525e62018-06-12 10:11:12 -0400100
101std::unique_ptr<GrAtlasTextOp> GrAtlasTextOp::MakeDistanceField(
Robert Phillipsb97da532019-02-12 15:24:12 -0500102 GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400103 GrPaint&& paint,
Herb Derby3c873af2020-04-29 15:56:07 -0400104 GrTextBlob::SubRun* subrun,
105 const SkMatrix& drawMatrix,
106 SkPoint drawOrigin,
107 const SkIRect& clipRect,
108 const SkPMColor4f& filteredColor,
Robert Phillips7c525e62018-06-12 10:11:12 -0400109 bool useGammaCorrectDistanceTable,
110 SkColor luminanceColor,
Herb Derby3c873af2020-04-29 15:56:07 -0400111 const SkSurfaceProps& props) {
112 GrOpMemoryPool* pool = context->priv().opMemoryPool();
113 bool isBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
114 bool isLCD = subrun->hasUseLCDText() && SkPixelGeometryIsH(props.pixelGeometry());
115 MaskType maskType = !subrun->isAntiAliased() ? kAliasedDistanceField_MaskType
116 : isLCD ? (isBGR ? kLCDBGRDistanceField_MaskType
117 : kLCDDistanceField_MaskType)
118 : kGrayscaleDistanceField_MaskType;
Robert Phillipsc994a932018-06-19 13:09:54 -0400119
Herb Derby3c873af2020-04-29 15:56:07 -0400120 uint32_t DFGPFlags = drawMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
121 DFGPFlags |= drawMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
122 DFGPFlags |= drawMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
123 DFGPFlags |= useGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
124 DFGPFlags |= kAliasedDistanceField_MaskType == maskType ? kAliased_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400125
Herb Derby3c873af2020-04-29 15:56:07 -0400126 if (isLCD) {
127 DFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
128 DFGPFlags |= kLCDBGRDistanceField_MaskType == maskType ? kBGR_DistanceFieldEffectFlag : 0;
Robert Phillips7c525e62018-06-12 10:11:12 -0400129 }
130
Herb Derby3c873af2020-04-29 15:56:07 -0400131 return pool->allocate<GrAtlasTextOp>(maskType,
132 std::move(paint),
133 subrun,
134 drawMatrix,
135 drawOrigin,
136 clipRect,
137 filteredColor,
138 luminanceColor,
139 useGammaCorrectDistanceTable,
140 DFGPFlags);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500141}
142
Chris Dalton1706cbf2019-05-21 19:35:29 -0600143void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500144 fProcessors.visitProxies(func);
Robert Phillipse4fda6c2018-02-21 12:10:41 -0500145}
146
Brian Osman9a390ac2018-11-12 09:47:48 -0500147#ifdef SK_DEBUG
Brian Salomon344ec422016-12-15 10:58:41 -0500148SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -0800149 SkString str;
150
151 for (int i = 0; i < fGeoCount; ++i) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500152 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n",
joshualitta751c972015-11-20 13:37:32 -0800153 i,
Brian Osmancf860852018-10-31 14:04:39 -0400154 fGeoData[i].fColor.toBytes_RGBA(),
Herb Derby5bf5b042019-12-12 16:37:03 -0500155 fGeoData[i].fDrawOrigin.x(),
156 fGeoData[i].fDrawOrigin.y());
joshualitta751c972015-11-20 13:37:32 -0800157 }
158
Brian Salomon44acb5b2017-07-18 19:59:24 -0400159 str += fProcessors.dumpProcessors();
160 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -0800161 return str;
162}
Brian Osman9a390ac2018-11-12 09:47:48 -0500163#endif
joshualitta751c972015-11-20 13:37:32 -0800164
Brian Salomon44acb5b2017-07-18 19:59:24 -0400165GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
166 return FixedFunctionFlags::kNone;
167}
168
Chris Dalton6ce447a2019-06-23 18:07:38 -0600169GrProcessorSet::Analysis GrAtlasTextOp::finalize(
170 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
171 GrClampType clampType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400172 GrProcessorAnalysisCoverage coverage;
173 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -0800174 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400175 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -0800176 } else {
Brian Osman09068252018-01-03 09:57:29 -0500177 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800178 }
joshualitta751c972015-11-20 13:37:32 -0800179 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800180 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400181 case kAliasedDistanceField_MaskType:
182 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400183 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800184 break;
185 case kLCDCoverageMask_MaskType:
186 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400187 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400188 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800189 break;
190 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400191 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400192 break;
joshualitta751c972015-11-20 13:37:32 -0800193 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700194 auto analysis = fProcessors.finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600195 color, coverage, clip, &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
196 clampType, &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400197 fUsesLocalCoords = analysis.usesLocalCoords();
Chris Dalton4b62aed2019-01-15 11:53:00 -0700198 return analysis;
joshualitta751c972015-11-20 13:37:32 -0800199}
200
Brian Salomon91326c32017-08-09 16:02:19 -0400201void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500202 auto resourceProvider = target->resourceProvider();
203
joshualitta751c972015-11-20 13:37:32 -0800204 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
205 // TODO actually only invert if we don't have RGBA
206 SkMatrix localMatrix;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500207 if (this->usesLocalCoords() && !fGeoData[0].fDrawMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800208 return;
209 }
210
Robert Phillips5a66efb2018-03-07 15:13:18 -0500211 GrAtlasManager* atlasManager = target->atlasManager();
Mike Klein99e002f2020-01-16 16:45:03 +0000212
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400213 GrMaskFormat maskFormat = this->maskFormat();
214
Greg Daniel9715b6c2019-12-10 15:03:10 -0500215 unsigned int numActiveViews;
216 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
217 if (!views) {
joshualitta751c972015-11-20 13:37:32 -0800218 SkDebugf("Could not allocate backing texture for atlas\n");
219 return;
220 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500221 SkASSERT(views[0].proxy());
joshualitta751c972015-11-20 13:37:32 -0800222
Brian Salomon7eae3e02018-08-07 14:02:38 +0000223 static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500224 static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures);
225 static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000226
Chris Dalton304e14d2020-03-17 14:29:06 -0600227 auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500228 for (unsigned i = 0; i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600229 primProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400230 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies
231 // don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500232 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000233 }
Brian Salomon49348902018-06-26 09:12:38 -0400234
235 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600236 flushInfo.fPrimProcProxies = primProcProxies;
Brian Salomon49348902018-06-26 09:12:38 -0400237
Herb Derby1c5be7b2019-12-13 12:03:06 -0500238 bool vmPerspective = fGeoData[0].fDrawMatrix.hasPerspective();
joshualittd9d30f72015-12-08 10:47:55 -0800239 if (this->usesDistanceFields()) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500240 flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(),
241 *target->caps().shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500242 views, numActiveViews);
joshualitta751c972015-11-20 13:37:32 -0800243 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500244 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
245 : GrSamplerState::Filter::kNearest;
246 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
247 target->allocator(), *target->caps().shaderCaps(), this->color(), false, views,
248 numActiveViews, filter, maskFormat, localMatrix, vmPerspective);
joshualitta751c972015-11-20 13:37:32 -0800249 }
250
Herb Derby23f29762020-01-10 16:26:14 -0500251 int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride();
joshualitta751c972015-11-20 13:37:32 -0800252
Brian Salomon43cbd722020-01-03 22:09:12 -0500253 // Ensure we don't request an insanely large contiguous vertex allocation.
Herb Derby23f29762020-01-10 16:26:14 -0500254 static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize;
255 const int quadSize = vertexStride * kVerticesPerGlyph;
256 const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize;
257
258 // Where the quad buffer begins and ends relative to totalGlyphsRegened.
259 int quadBufferBegin = 0;
260 int quadBufferEnd = std::min(this->numGlyphs(), maxQuadsPerBuffer);
261
Robert Phillipsee08d522019-10-28 16:34:44 -0400262 flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer();
Herb Derby23f29762020-01-10 16:26:14 -0500263 void* vertices = target->makeVertexSpace(
264 vertexStride,
265 kVerticesPerGlyph * (quadBufferEnd - quadBufferBegin),
266 &flushInfo.fVertexBuffer,
267 &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800268 if (!vertices || !flushInfo.fVertexBuffer) {
269 SkDebugf("Could not allocate vertices\n");
270 return;
271 }
272
Herb Derby23f29762020-01-10 16:26:14 -0500273 // totalGlyphsRegened is all the glyphs for the op [0, this->numGlyphs()). The subRun glyph and
274 // quad buffer indices are calculated from this.
275 int totalGlyphsRegened = 0;
joshualitta751c972015-11-20 13:37:32 -0800276 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800277 const Geometry& args = fGeoData[i];
Herb Derby23f29762020-01-10 16:26:14 -0500278 auto subRun = args.fSubRunPtr;
279 SkASSERT((int)subRun->vertexStride() == vertexStride);
280
Robert Phillips207d24b2020-04-09 10:23:42 -0400281 subRun->prepareGrGlyphs(target->strikeCache());
Herb Derby62b12fe2020-01-14 17:57:24 -0500282
Brian Osman1be2b7c2018-10-29 16:07:15 -0400283 // TODO4F: Preserve float colors
Robert Phillips297a2192020-04-08 15:26:54 -0400284 GrTextBlob::VertexRegenerator regenerator(resourceProvider, subRun,
285 target->deferredUploadTarget(), atlasManager);
Herb Derby23f29762020-01-10 16:26:14 -0500286
287 // Where the subRun begins and ends relative to totalGlyphsRegened.
288 int subRunBegin = totalGlyphsRegened;
Herb Derbyd5cbc1e2020-05-16 13:45:55 -0400289 int subRunEnd = subRunBegin + subRun->glyphCount();
Herb Derby23f29762020-01-10 16:26:14 -0500290
291 // Draw all the glyphs in the subRun.
292 while (totalGlyphsRegened < subRunEnd) {
293 // drawBegin and drawEnd are indices for the subRun on the
294 // interval [0, subRun->fGlyphs.size()).
295 int drawBegin = totalGlyphsRegened - subRunBegin;
296 // drawEnd is either the end of the subRun or the end of the current quad buffer.
297 int drawEnd = std::min(subRunEnd, quadBufferEnd) - subRunBegin;
298 auto[ok, glyphsRegenerated] = regenerator.regenerate(drawBegin, drawEnd);
299
300 // There was a problem allocating the glyph in the atlas. Bail.
Robert Phillips1576e4e2020-04-01 12:49:45 -0400301 if (!ok) {
302 return;
303 }
Herb Derby23f29762020-01-10 16:26:14 -0500304
305 // Update all the vertices for glyphsRegenerate glyphs.
306 if (glyphsRegenerated > 0) {
307 int quadBufferIndex = totalGlyphsRegened - quadBufferBegin;
Herb Derby23f29762020-01-10 16:26:14 -0500308 auto regeneratedQuadBuffer =
309 SkTAddOffset<char>(vertices, subRun->quadOffset(quadBufferIndex));
Herb Derby64391c42020-05-16 14:32:15 -0400310 int subRunIndex = totalGlyphsRegened - subRunBegin;
311 args.fillVertexData(regeneratedQuadBuffer, subRunIndex, glyphsRegenerated);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500312 }
Herb Derby23f29762020-01-10 16:26:14 -0500313
314 totalGlyphsRegened += glyphsRegenerated;
315 flushInfo.fGlyphsToFlush += glyphsRegenerated;
316
317 // regenerate() has stopped part way through a SubRun. This means that either the atlas
318 // or the quad buffer is full or both. There is a case were the flow through
319 // the loop is strange. If we run out of quad buffer space at the same time the
320 // SubRun ends, then this is not triggered which is the right result for the last
321 // SubRun. But, if this is not the last SubRun, then advance to the next SubRun which
322 // will process no glyphs, and return to this point where the quad buffer will be
323 // expanded.
324 if (totalGlyphsRegened != subRunEnd) {
325 // Flush if not all glyphs drawn because either the quad buffer is full or the
326 // atlas is out of space.
Herb Derby4513cdd2020-01-31 13:28:49 -0500327 this->createDrawForGeneratedGlyphs(target, &flushInfo);
Herb Derby23f29762020-01-10 16:26:14 -0500328 if (totalGlyphsRegened == quadBufferEnd) {
329 // Quad buffer is full. Get more buffer.
330 quadBufferBegin = totalGlyphsRegened;
331 int quadBufferSize =
332 std::min(maxQuadsPerBuffer, this->numGlyphs() - totalGlyphsRegened);
333 quadBufferEnd = quadBufferBegin + quadBufferSize;
334
335 vertices = target->makeVertexSpace(
336 vertexStride,
337 kVerticesPerGlyph * quadBufferSize,
338 &flushInfo.fVertexBuffer,
339 &flushInfo.fVertexOffset);
340 if (!vertices || !flushInfo.fVertexBuffer) {
341 SkDebugf("Could not allocate vertices\n");
342 return;
343 }
344 }
345 }
346 }
Herb Derby5f6f8512020-01-10 12:50:35 -0500347 } // for all geometries
Herb Derby4513cdd2020-01-31 13:28:49 -0500348 this->createDrawForGeneratedGlyphs(target, &flushInfo);
joshualitta751c972015-11-20 13:37:32 -0800349}
350
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700351void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500352 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
353 std::move(fProcessors),
354 GrPipeline::InputFlags::kNone);
355
356 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700357}
358
Herb Derby4513cdd2020-01-31 13:28:49 -0500359void GrAtlasTextOp::createDrawForGeneratedGlyphs(
360 GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500361 if (!flushInfo->fGlyphsToFlush) {
362 return;
363 }
364
Robert Phillips5a66efb2018-03-07 15:13:18 -0500365 auto atlasManager = target->atlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500366
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500367 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500369
Greg Daniel9715b6c2019-12-10 15:03:10 -0500370 unsigned int numActiveViews;
371 const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews);
372 SkASSERT(views);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500373 // Something has gone terribly wrong, bail
Greg Daniel9715b6c2019-12-10 15:03:10 -0500374 if (!views || 0 == numActiveViews) {
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500375 return;
376 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500377 if (gp->numTextureSamplers() != (int) numActiveViews) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400378 // During preparation the number of atlas pages has increased.
379 // Update the proxies used in the GP to match.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500380 for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600381 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400382 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
383 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500384 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon43cbd722020-01-03 22:09:12 -0500385 // These will get unreffed when the previously recorded draws destruct.
386 for (int d = 0; d < flushInfo->fNumDraws; ++d) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600387 flushInfo->fPrimProcProxies[i]->ref();
Brian Salomon43cbd722020-01-03 22:09:12 -0500388 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000389 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400390 if (this->usesDistanceFields()) {
391 if (this->isLCD()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500392 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500393 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400394 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500395 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500396 views, numActiveViews, GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400397 }
398 } else {
Brian Salomonccb61422020-01-09 10:46:36 -0500399 auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kBilerp
400 : GrSamplerState::Filter::kNearest;
401 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400402 }
403 }
Brian Salomondbf70722019-02-07 11:31:24 -0500404 int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Chris Daltoneb694b72020-03-16 09:25:50 -0600405 GrSimpleMesh* mesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600406 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
407 maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph,
408 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600409 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600410 GrPrimitiveType::kTriangles);
joshualitta751c972015-11-20 13:37:32 -0800411 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
412 flushInfo->fGlyphsToFlush = 0;
Brian Salomon43cbd722020-01-03 22:09:12 -0500413 ++flushInfo->fNumDraws;
joshualitta751c972015-11-20 13:37:32 -0800414}
415
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500416GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
417 const GrCaps& caps) {
Brian Salomon344ec422016-12-15 10:58:41 -0500418 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400419 if (fProcessors != that->fProcessors) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000420 return CombineResult::kCannotCombine;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400421 }
422
joshualitta751c972015-11-20 13:37:32 -0800423 if (fMaskType != that->fMaskType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000424 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800425 }
426
Herb Derby1c5be7b2019-12-13 12:03:06 -0500427 const SkMatrix& thisFirstMatrix = fGeoData[0].fDrawMatrix;
428 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fDrawMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500429
Mike Reed2c383152019-12-18 16:47:47 -0500430 if (this->usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000431 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500432 }
433
Jim Van Verthb515ae72018-05-23 16:44:55 -0400434 if (fNeedsGlyphTransform != that->fNeedsGlyphTransform) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000435 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400436 }
437
438 if (fNeedsGlyphTransform &&
439 (thisFirstMatrix.hasPerspective() != thatFirstMatrix.hasPerspective())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000440 return CombineResult::kCannotCombine;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400441 }
442
Brian Salomon5c6ac642017-12-19 11:09:32 -0500443 if (this->usesDistanceFields()) {
444 if (fDFGPFlags != that->fDFGPFlags) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000445 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800446 }
447
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400448 if (fLuminanceColor != that->fLuminanceColor) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000449 return CombineResult::kCannotCombine;
joshualitta751c972015-11-20 13:37:32 -0800450 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500451 } else {
452 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000453 return CombineResult::kCannotCombine;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500454 }
joshualitta751c972015-11-20 13:37:32 -0800455 }
456
Brian Salomon344ec422016-12-15 10:58:41 -0500457 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800458
Jim Van Verth56c37142017-10-31 14:44:25 -0400459 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800460 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800461
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400462 // We reallocate at a rate of 1.5x to try to get better total memory usage
463 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400464 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400465 while (newAllocSize < newGeoCount) {
466 newAllocSize += newAllocSize / 2;
467 }
joshualitta751c972015-11-20 13:37:32 -0800468 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400469 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800470 }
471
Brian Salomon344ec422016-12-15 10:58:41 -0500472 // 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 -0800473 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500474 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800475#ifdef SK_DEBUG
476 for (int i = 0; i < that->fGeoCount; ++i) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500477 that->fGeoData.get()[i].fBlob = (GrTextBlob*)0x1;
joshualitta751c972015-11-20 13:37:32 -0800478 }
479#endif
480 that->fGeoCount = 0;
481 fGeoCount = newGeoCount;
482
Brian Salomon7eae3e02018-08-07 14:02:38 +0000483 return CombineResult::kMerged;
joshualitta751c972015-11-20 13:37:32 -0800484}
485
Herb Derby3c873af2020-04-29 15:56:07 -0400486static const int kDistanceAdjustLumShift = 5;
487
joshualitta751c972015-11-20 13:37:32 -0800488// TODO trying to figure out why lcd is so whack
Herb Derby26cbe512018-05-24 14:39:01 -0400489// (see comments in GrTextContext::ComputeCanonicalColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500490GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena,
491 const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500492 const GrSurfaceProxyView* views,
493 unsigned int numActiveViews) const {
joshualitta751c972015-11-20 13:37:32 -0800494 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500495
496 SkMatrix localMatrix = SkMatrix::I();
497 if (this->usesLocalCoords()) {
498 // If this fails we'll just use I().
Herb Derby1c5be7b2019-12-13 12:03:06 -0500499 bool result = fGeoData[0].fDrawMatrix.invert(&localMatrix);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500500 (void)result;
501 }
joshualitta751c972015-11-20 13:37:32 -0800502
Robert Phillips841c9a52020-03-27 12:41:31 -0400503 auto dfAdjustTable = GrDistanceFieldAdjustTable::Get();
504
joshualitta751c972015-11-20 13:37:32 -0800505 // see if we need to create a new effect
506 if (isLCD) {
Robert Phillips841c9a52020-03-27 12:41:31 -0400507 float redCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400508 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500509 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400510 float greenCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400511 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500512 fUseGammaCorrectDistanceTable);
Robert Phillips841c9a52020-03-27 12:41:31 -0400513 float blueCorrection = dfAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400514 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500515 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800516 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500517 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
518 redCorrection, greenCorrection, blueCorrection);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500519 return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500520 GrSamplerState::Filter::kBilerp, widthAdjust,
Brian Osman09068252018-01-03 09:57:29 -0500521 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800522 } else {
joshualitta751c972015-11-20 13:37:32 -0800523#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400524 float correction = 0;
525 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400526 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
527 fLuminanceColor);
Robert Phillips841c9a52020-03-27 12:41:31 -0400528 correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
529 fUseGammaCorrectDistanceTable);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400530 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500531 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500532 GrSamplerState::Filter::kBilerp, correction,
533 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800534#else
Greg Daniel9715b6c2019-12-10 15:03:10 -0500535 return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500536 GrSamplerState::Filter::kBilerp,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500537 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800538#endif
539 }
joshualitta751c972015-11-20 13:37:32 -0800540}
joshualittddd22d82016-02-16 06:47:52 -0800541