blob: 8f0c555295a7999e3894fa0f2b0f5b8d167ef65a [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
Brian Salomon09e019e2016-12-15 10:20:35 -05008#include "GrAtlasTextOp.h"
Robert Phillipse4fda6c2018-02-21 12:10:41 -05009
Robert Phillips296b1cc2017-03-15 10:42:12 -040010#include "GrContext.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
joshualitta751c972015-11-20 13:37:32 -080012#include "GrResourceProvider.h"
joshualitta751c972015-11-20 13:37:32 -080013#include "SkGlyphCache.h"
halcanary4dbbd042016-06-07 17:21:10 -070014#include "SkMathPriv.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050015#include "SkMatrixPriv.h"
16#include "SkPoint3.h"
joshualitta751c972015-11-20 13:37:32 -080017#include "effects/GrBitmapTextGeoProc.h"
18#include "effects/GrDistanceFieldGeoProc.h"
Robert Phillipsacf17902018-02-27 16:43:18 -050019#include "text/GrAtlasManager.h"
20#include "text/GrGlyphCache.h"
joshualitta751c972015-11-20 13:37:32 -080021
joshualitt60ce86d2015-11-23 13:08:22 -080022///////////////////////////////////////////////////////////////////////////////////////////////////
23
joshualitta751c972015-11-20 13:37:32 -080024static const int kDistanceAdjustLumShift = 5;
25
Brian Salomon5c6ac642017-12-19 11:09:32 -050026void GrAtlasTextOp::init() {
27 const Geometry& geo = fGeoData[0];
Brian Salomon5c6ac642017-12-19 11:09:32 -050028 SkRect bounds;
29 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY);
30 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
31 // we treat this as a set of non-AA rects rendered with a texture.
32 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
33 if (this->usesDistanceFields()) {
34 bool isLCD = this->isLCD();
35
36 const SkMatrix& viewMatrix = geo.fViewMatrix;
37
38 fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
39 fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
40 fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
41 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
42 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
43 ? kAliased_DistanceFieldEffectFlag
44 : 0;
45
46 if (isLCD) {
47 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
48 fDFGPFlags |=
49 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
50 }
51 }
52}
53
Robert Phillipse4fda6c2018-02-21 12:10:41 -050054void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
55 fProcessors.visitProxies(func);
56
Robert Phillipsacf17902018-02-27 16:43:18 -050057 // We need to visit the atlasManager's proxies because, although the atlasManager explicitly
58 // manages their lifetimes, if they fail to allocate the draws that reference them need to
59 // be dropped.
Robert Phillipse4fda6c2018-02-21 12:10:41 -050060 unsigned int numProxies;
Robert Phillipsacf17902018-02-27 16:43:18 -050061 const sk_sp<GrTextureProxy>* proxies = fRestrictedAtlasManager->getProxies(
62 this->maskFormat(), &numProxies);
Robert Phillipse4fda6c2018-02-21 12:10:41 -050063 for (unsigned int i = 0; i < numProxies; ++i) {
64 if (proxies[i]) {
65 func(proxies[i].get());
66 }
67 }
68}
69
Brian Salomon344ec422016-12-15 10:58:41 -050070SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080071 SkString str;
72
73 for (int i = 0; i < fGeoCount; ++i) {
74 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
75 i,
76 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -080077 fGeoData[i].fX,
78 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -080079 fGeoData[i].fBlob->runCount());
joshualitta751c972015-11-20 13:37:32 -080080 }
81
Brian Salomon44acb5b2017-07-18 19:59:24 -040082 str += fProcessors.dumpProcessors();
83 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -080084 return str;
85}
86
Brian Salomon44acb5b2017-07-18 19:59:24 -040087GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
88 return FixedFunctionFlags::kNone;
89}
90
91GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040092 const GrAppliedClip* clip,
93 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040094 GrProcessorAnalysisCoverage coverage;
95 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -080096 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040097 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -080098 } else {
Brian Osman09068252018-01-03 09:57:29 -050099 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -0800100 }
joshualitta751c972015-11-20 13:37:32 -0800101 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -0800102 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400103 case kAliasedDistanceField_MaskType:
104 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400105 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800106 break;
107 case kLCDCoverageMask_MaskType:
108 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400109 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400110 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800111 break;
112 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400113 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400114 break;
joshualitta751c972015-11-20 13:37:32 -0800115 }
Brian Osman09068252018-01-03 09:57:29 -0500116 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, dstIsClamped,
117 &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400118 fUsesLocalCoords = analysis.usesLocalCoords();
119 fCanCombineOnTouchOrOverlap =
120 !analysis.requiresDstTexture() &&
121 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
122 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitta751c972015-11-20 13:37:32 -0800123}
124
Brian Salomon18923f92017-11-06 16:26:02 -0500125static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400126 size_t vertexStride, int glyphCount) {
127 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500128 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
129 const SkPoint* blobPositionRB =
130 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400131
Jim Van Verth328a33f2017-10-20 12:14:22 -0400132 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500133 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
134 SkScalarRoundToInt(blobPositionLT->fY),
135 SkScalarRoundToInt(blobPositionRB->fX),
136 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400137 if (clipRect.contains(positionRect)) {
138 memcpy(currVertex, blobVertices, 4 * vertexStride);
139 currVertex += 4 * vertexStride;
140 } else {
141 // Pull out some more data that we'll need.
142 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
143 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500144 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400145 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500146 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
147 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
148 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400149 // Pull out the texel coordinates and texture index bits
150 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
151 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
152 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
153 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
154 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
155 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400156
Jim Van Verth328a33f2017-10-20 12:14:22 -0400157 int positionRectWidth = positionRect.width();
158 int positionRectHeight = positionRect.height();
159 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
160 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400161
162 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400163 unsigned int delta;
164 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
165 coordsRectL += delta;
166 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400167
Jim Van Verth328a33f2017-10-20 12:14:22 -0400168 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
169 coordsRectT += delta;
170 positionRect.fTop += delta;
171
172 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
173 coordsRectR -= delta;
174 positionRect.fRight -= delta;
175
176 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
177 coordsRectB -= delta;
178 positionRect.fBottom -= delta;
179
180 // Repack texel coordinates and index
181 coordsRectL = coordsRectL << 1 | pageIndexX;
182 coordsRectT = coordsRectT << 1 | pageIndexY;
183 coordsRectR = coordsRectR << 1 | pageIndexX;
184 coordsRectB = coordsRectB << 1 | pageIndexY;
185
186 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400187 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
188 currPosition->fX = positionRect.fLeft;
189 currPosition->fY = positionRect.fTop;
190 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400191 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
192 currCoords[0] = coordsRectL;
193 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400194 currVertex += vertexStride;
195
196 currPosition = reinterpret_cast<SkPoint*>(currVertex);
197 currPosition->fX = positionRect.fLeft;
198 currPosition->fY = positionRect.fBottom;
199 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400200 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
201 currCoords[0] = coordsRectL;
202 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400203 currVertex += vertexStride;
204
205 currPosition = reinterpret_cast<SkPoint*>(currVertex);
206 currPosition->fX = positionRect.fRight;
207 currPosition->fY = positionRect.fTop;
208 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400209 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
210 currCoords[0] = coordsRectR;
211 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400212 currVertex += vertexStride;
213
214 currPosition = reinterpret_cast<SkPoint*>(currVertex);
215 currPosition->fX = positionRect.fRight;
216 currPosition->fY = positionRect.fBottom;
217 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400218 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
219 currCoords[0] = coordsRectR;
220 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400221 currVertex += vertexStride;
222 }
223
224 blobVertices += 4 * vertexStride;
225 }
226}
227
Brian Salomon91326c32017-08-09 16:02:19 -0400228void GrAtlasTextOp::onPrepareDraws(Target* target) {
Robert Phillips934c3d02018-02-26 16:49:16 +0000229 auto resourceProvider = target->resourceProvider();
230
joshualitta751c972015-11-20 13:37:32 -0800231 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
232 // TODO actually only invert if we don't have RGBA
233 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500234 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800235 SkDebugf("Cannot invert viewmatrix\n");
236 return;
237 }
238
Robert Phillipsacf17902018-02-27 16:43:18 -0500239 GrAtlasManager* fullAtlasManager = target->fullAtlasManager();
240 SkASSERT(fRestrictedAtlasManager == fullAtlasManager);
241 GrGlyphCache* glyphCache = target->glyphCache();
242
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400243 GrMaskFormat maskFormat = this->maskFormat();
244
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500245 unsigned int atlasPageCount;
Robert Phillipsacf17902018-02-27 16:43:18 -0500246 const sk_sp<GrTextureProxy>* proxies = fullAtlasManager->getProxies(maskFormat,
247 &atlasPageCount);
Robert Phillips934c3d02018-02-26 16:49:16 +0000248 if (!proxies[0]) {
joshualitta751c972015-11-20 13:37:32 -0800249 SkDebugf("Could not allocate backing texture for atlas\n");
250 return;
251 }
252
bsalomon342bfc22016-04-01 06:06:20 -0700253 FlushInfo flushInfo;
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400254 flushInfo.fPipeline =
255 target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500256 SkDEBUGCODE(bool dfPerspective = false);
joshualittd9d30f72015-12-08 10:47:55 -0800257 if (this->usesDistanceFields()) {
Robert Phillipsacf17902018-02-27 16:43:18 -0500258 flushInfo.fGeometryProcessor = this->setupDfProcessor(fullAtlasManager);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500259 SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective());
joshualitta751c972015-11-20 13:37:32 -0800260 } else {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400261 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400262 this->color(), proxies, GrSamplerState::ClampNearest(), maskFormat,
263 localMatrix, this->usesLocalCoords());
joshualitta751c972015-11-20 13:37:32 -0800264 }
265
joshualitta751c972015-11-20 13:37:32 -0800266 flushInfo.fGlyphsToFlush = 0;
bsalomon342bfc22016-04-01 06:06:20 -0700267 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500268 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat, dfPerspective));
joshualitta751c972015-11-20 13:37:32 -0800269
joshualitta751c972015-11-20 13:37:32 -0800270 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700271 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800272
Brian Salomon344ec422016-12-15 10:58:41 -0500273 void* vertices = target->makeVertexSpace(
274 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800275 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400276 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800277 if (!vertices || !flushInfo.fVertexBuffer) {
278 SkDebugf("Could not allocate vertices\n");
279 return;
280 }
281
Brian Salomon18923f92017-11-06 16:26:02 -0500282 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800283
Robert Phillipsacf17902018-02-27 16:43:18 -0500284 SkAutoGlyphCache autoGlyphCache;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400285 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800286 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800287 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800288 Blob* blob = args.fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500289 GrAtlasTextBlob::VertexRegenerator regenerator(
Robert Phillips934c3d02018-02-26 16:49:16 +0000290 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
Robert Phillipsacf17902018-02-27 16:43:18 -0500291 args.fColor, target->deferredUploadTarget(), glyphCache, fullAtlasManager,
292 &autoGlyphCache);
Brian Salomon18923f92017-11-06 16:26:02 -0500293 GrAtlasTextBlob::VertexRegenerator::Result result;
294 do {
295 result = regenerator.regenerate();
296 // Copy regenerated vertices from the blob to our vertex buffer.
297 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
298 if (args.fClipRect.isEmpty()) {
299 memcpy(currVertex, result.fFirstVertex, vertexBytes);
300 } else {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500301 SkASSERT(!dfPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500302 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
303 result.fGlyphsRegenerated);
304 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500305 if (this->usesDistanceFields() && !args.fViewMatrix.isIdentity()) {
306 // We always do the distance field view matrix transformation after copying rather
307 // than during blob vertex generation time in the blob as handling successive
308 // arbitrary transformations would be complicated and accumulate error.
309 if (args.fViewMatrix.hasPerspective()) {
310 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500311 SkMatrixPriv::MapHomogeneousPointsWithStride(
312 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
313 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500314 } else {
315 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500316 SkMatrixPriv::MapPointsWithStride(
317 args.fViewMatrix, pos, vertexStride,
318 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500319 }
320 }
Brian Salomon18923f92017-11-06 16:26:02 -0500321 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
322 if (!result.fFinished) {
323 this->flush(target, &flushInfo);
324 }
325 currVertex += vertexBytes;
326 } while (!result.fFinished);
joshualitta751c972015-11-20 13:37:32 -0800327 }
joshualitta751c972015-11-20 13:37:32 -0800328 this->flush(target, &flushInfo);
329}
330
Brian Salomone5b399e2017-07-19 13:50:54 -0400331void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillipsacf17902018-02-27 16:43:18 -0500332 auto fullAtlasManager = target->fullAtlasManager();
333 SkASSERT(fRestrictedAtlasManager == fullAtlasManager);
334
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400335 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
336 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500337
338 unsigned int numProxies;
Robert Phillipsacf17902018-02-27 16:43:18 -0500339 const sk_sp<GrTextureProxy>* proxies = fullAtlasManager->getProxies(maskFormat, &numProxies);
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500340 if (gp->numTextureSamplers() != (int) numProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400341 // During preparation the number of atlas pages has increased.
342 // Update the proxies used in the GP to match.
343 if (this->usesDistanceFields()) {
344 if (this->isLCD()) {
345 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500346 proxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347 } else {
348 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500349 proxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400350 }
351 } else {
352 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500353 proxies, GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400354 }
355 }
356
Chris Dalton3809bab2017-06-13 10:55:06 -0600357 GrMesh mesh(GrPrimitiveType::kTriangles);
cdalton397536c2016-03-25 12:15:03 -0700358 int maxGlyphsPerDraw =
Brian Salomon344ec422016-12-15 10:58:41 -0500359 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600360 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
Chris Daltonbca46e22017-05-15 11:03:26 -0600361 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
Chris Dalton114a3c02017-05-26 15:17:19 -0600362 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400363 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
joshualitta751c972015-11-20 13:37:32 -0800364 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
365 flushInfo->fGlyphsToFlush = 0;
366}
367
Brian Salomon344ec422016-12-15 10:58:41 -0500368bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
369 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400370 if (fProcessors != that->fProcessors) {
371 return false;
372 }
373
374 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
joshualitta751c972015-11-20 13:37:32 -0800375 return false;
376 }
377
378 if (fMaskType != that->fMaskType) {
379 return false;
380 }
381
Brian Salomon5c6ac642017-12-19 11:09:32 -0500382 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
383 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
384
385 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
386 return false;
387 }
388
389 if (this->usesDistanceFields()) {
390 if (fDFGPFlags != that->fDFGPFlags) {
joshualitta751c972015-11-20 13:37:32 -0800391 return false;
392 }
393
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400394 if (fLuminanceColor != that->fLuminanceColor) {
joshualitta751c972015-11-20 13:37:32 -0800395 return false;
396 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500397 } else {
398 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
399 return false;
400 }
joshualitta751c972015-11-20 13:37:32 -0800401 }
402
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400403 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
404 // We use the largest possible vertex size for this
405 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400406 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400407 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
408 return false;
409 }
410
Brian Salomon344ec422016-12-15 10:58:41 -0500411 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800412
Jim Van Verth56c37142017-10-31 14:44:25 -0400413 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800414 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800415
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400416 // We reallocate at a rate of 1.5x to try to get better total memory usage
417 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400418 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400419 while (newAllocSize < newGeoCount) {
420 newAllocSize += newAllocSize / 2;
421 }
joshualitta751c972015-11-20 13:37:32 -0800422 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400423 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800424 }
425
Brian Salomon344ec422016-12-15 10:58:41 -0500426 // 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 -0800427 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500428 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800429#ifdef SK_DEBUG
430 for (int i = 0; i < that->fGeoCount; ++i) {
431 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
432 }
433#endif
434 that->fGeoCount = 0;
435 fGeoCount = newGeoCount;
436
bsalomon88cf17d2016-07-08 06:40:56 -0700437 this->joinBounds(*that);
joshualitta751c972015-11-20 13:37:32 -0800438 return true;
439}
440
joshualitta751c972015-11-20 13:37:32 -0800441// TODO trying to figure out why lcd is so whack
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400442// (see comments in GrAtlasTextContext::ComputeCanonicalColor)
Robert Phillipsacf17902018-02-27 16:43:18 -0500443sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(
444 GrRestrictedAtlasManager* restrictedAtlasManager) const {
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500445 unsigned int numProxies;
Robert Phillipsacf17902018-02-27 16:43:18 -0500446 const sk_sp<GrTextureProxy>* p = restrictedAtlasManager->getProxies(this->maskFormat(),
447 &numProxies);
joshualitta751c972015-11-20 13:37:32 -0800448 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500449
450 SkMatrix localMatrix = SkMatrix::I();
451 if (this->usesLocalCoords()) {
452 // If this fails we'll just use I().
453 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
454 (void)result;
455 }
joshualitta751c972015-11-20 13:37:32 -0800456
457 // see if we need to create a new effect
458 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700459 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400460 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500461 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700462 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400463 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500464 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700465 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400466 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500467 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800468 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500469 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
470 redCorrection, greenCorrection, blueCorrection);
Brian Osman09068252018-01-03 09:57:29 -0500471 return GrDistanceFieldLCDTextGeoProc::Make(p, GrSamplerState::ClampBilerp(), widthAdjust,
472 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800473 } else {
joshualitta751c972015-11-20 13:37:32 -0800474#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400475 float correction = 0;
476 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400477 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
478 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400479 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
480 fUseGammaCorrectDistanceTable);
481 }
Brian Osman09068252018-01-03 09:57:29 -0500482 return GrDistanceFieldA8TextGeoProc::Make(p, GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500483 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800484#else
Brian Osman09068252018-01-03 09:57:29 -0500485 return GrDistanceFieldA8TextGeoProc::Make(p, GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500486 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800487#endif
488 }
joshualitta751c972015-11-20 13:37:32 -0800489}
joshualittddd22d82016-02-16 06:47:52 -0800490