blob: 46f38099ec783a298e4ca9f258c185619f528091 [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"
Brian Salomonf856fd12016-12-16 14:24:34 -050019#include "text/GrAtlasGlyphCache.h"
joshualitta751c972015-11-20 13:37:32 -080020
joshualitt60ce86d2015-11-23 13:08:22 -080021///////////////////////////////////////////////////////////////////////////////////////////////////
22
joshualitta751c972015-11-20 13:37:32 -080023static const int kDistanceAdjustLumShift = 5;
24
Brian Salomon5c6ac642017-12-19 11:09:32 -050025void GrAtlasTextOp::init() {
26 const Geometry& geo = fGeoData[0];
Brian Salomon5c6ac642017-12-19 11:09:32 -050027 SkRect bounds;
28 geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, geo.fY);
29 // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds
30 // we treat this as a set of non-AA rects rendered with a texture.
31 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
32 if (this->usesDistanceFields()) {
33 bool isLCD = this->isLCD();
34
35 const SkMatrix& viewMatrix = geo.fViewMatrix;
36
37 fDFGPFlags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
38 fDFGPFlags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
39 fDFGPFlags |= viewMatrix.hasPerspective() ? kPerspective_DistanceFieldEffectFlag : 0;
40 fDFGPFlags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
41 fDFGPFlags |= (kAliasedDistanceField_MaskType == fMaskType)
42 ? kAliased_DistanceFieldEffectFlag
43 : 0;
44
45 if (isLCD) {
46 fDFGPFlags |= kUseLCD_DistanceFieldEffectFlag;
47 fDFGPFlags |=
48 (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
49 }
50 }
51}
52
Robert Phillipse4fda6c2018-02-21 12:10:41 -050053void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
54 fProcessors.visitProxies(func);
55
56 unsigned int numProxies;
57 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat(),
58 &numProxies);
59 for (unsigned int i = 0; i < numProxies; ++i) {
60 if (proxies[i]) {
61 func(proxies[i].get());
62 }
63 }
64}
65
Brian Salomon344ec422016-12-15 10:58:41 -050066SkString GrAtlasTextOp::dumpInfo() const {
joshualitta751c972015-11-20 13:37:32 -080067 SkString str;
68
69 for (int i = 0; i < fGeoCount; ++i) {
70 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
71 i,
72 fGeoData[i].fColor,
joshualitt8e0ef292016-02-19 14:13:03 -080073 fGeoData[i].fX,
74 fGeoData[i].fY,
joshualittddd22d82016-02-16 06:47:52 -080075 fGeoData[i].fBlob->runCount());
joshualitta751c972015-11-20 13:37:32 -080076 }
77
Brian Salomon44acb5b2017-07-18 19:59:24 -040078 str += fProcessors.dumpProcessors();
79 str += INHERITED::dumpInfo();
joshualitta751c972015-11-20 13:37:32 -080080 return str;
81}
82
Brian Salomon44acb5b2017-07-18 19:59:24 -040083GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
84 return FixedFunctionFlags::kNone;
85}
86
87GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040088 const GrAppliedClip* clip,
89 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040090 GrProcessorAnalysisCoverage coverage;
91 GrProcessorAnalysisColor color;
joshualitta751c972015-11-20 13:37:32 -080092 if (kColorBitmapMask_MaskType == fMaskType) {
Brian Salomon44acb5b2017-07-18 19:59:24 -040093 color.setToUnknown();
joshualitta751c972015-11-20 13:37:32 -080094 } else {
Brian Osman09068252018-01-03 09:57:29 -050095 color.setToConstant(this->color());
joshualitta751c972015-11-20 13:37:32 -080096 }
joshualitta751c972015-11-20 13:37:32 -080097 switch (fMaskType) {
joshualitta751c972015-11-20 13:37:32 -080098 case kGrayscaleCoverageMask_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -040099 case kAliasedDistanceField_MaskType:
100 case kGrayscaleDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400101 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
joshualitta751c972015-11-20 13:37:32 -0800102 break;
103 case kLCDCoverageMask_MaskType:
104 case kLCDDistanceField_MaskType:
Jim Van Verth90e89b32017-07-06 16:36:55 -0400105 case kLCDBGRDistanceField_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400106 coverage = GrProcessorAnalysisCoverage::kLCD;
joshualitta751c972015-11-20 13:37:32 -0800107 break;
108 case kColorBitmapMask_MaskType:
Brian Salomon44acb5b2017-07-18 19:59:24 -0400109 coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400110 break;
joshualitta751c972015-11-20 13:37:32 -0800111 }
Brian Osman09068252018-01-03 09:57:29 -0500112 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, dstIsClamped,
113 &fGeoData[0].fColor);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400114 fUsesLocalCoords = analysis.usesLocalCoords();
115 fCanCombineOnTouchOrOverlap =
116 !analysis.requiresDstTexture() &&
117 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
118 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitta751c972015-11-20 13:37:32 -0800119}
120
Brian Salomon18923f92017-11-06 16:26:02 -0500121static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400122 size_t vertexStride, int glyphCount) {
123 for (int i = 0; i < glyphCount; ++i) {
Brian Salomon18923f92017-11-06 16:26:02 -0500124 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
125 const SkPoint* blobPositionRB =
126 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400127
Jim Van Verth328a33f2017-10-20 12:14:22 -0400128 // positions for bitmap glyphs are pixel boundary aligned
Brian Osman7ca90d22017-11-10 15:31:27 -0500129 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
130 SkScalarRoundToInt(blobPositionLT->fY),
131 SkScalarRoundToInt(blobPositionRB->fX),
132 SkScalarRoundToInt(blobPositionRB->fY));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400133 if (clipRect.contains(positionRect)) {
134 memcpy(currVertex, blobVertices, 4 * vertexStride);
135 currVertex += 4 * vertexStride;
136 } else {
137 // Pull out some more data that we'll need.
138 // In the LCD case the color will be garbage, but we'll overwrite it with the texcoords
139 // and it avoids a lot of conditionals.
Brian Salomon18923f92017-11-06 16:26:02 -0500140 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
Jim Van Verth328a33f2017-10-20 12:14:22 -0400141 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
Brian Salomon18923f92017-11-06 16:26:02 -0500142 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
143 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
144 coordOffset);
Jim Van Verth328a33f2017-10-20 12:14:22 -0400145 // Pull out the texel coordinates and texture index bits
146 uint16_t coordsRectL = blobCoordsLT[0] >> 1;
147 uint16_t coordsRectT = blobCoordsLT[1] >> 1;
148 uint16_t coordsRectR = blobCoordsRB[0] >> 1;
149 uint16_t coordsRectB = blobCoordsRB[1] >> 1;
150 uint16_t pageIndexX = blobCoordsLT[0] & 0x1;
151 uint16_t pageIndexY = blobCoordsLT[1] & 0x1;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400152
Jim Van Verth328a33f2017-10-20 12:14:22 -0400153 int positionRectWidth = positionRect.width();
154 int positionRectHeight = positionRect.height();
155 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
156 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400157
158 // Clip position and texCoords to the clipRect
Jim Van Verth328a33f2017-10-20 12:14:22 -0400159 unsigned int delta;
160 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
161 coordsRectL += delta;
162 positionRect.fLeft += delta;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400163
Jim Van Verth328a33f2017-10-20 12:14:22 -0400164 delta = SkTMin(SkTMax(clipRect.fTop - positionRect.fTop, 0), positionRectHeight);
165 coordsRectT += delta;
166 positionRect.fTop += delta;
167
168 delta = SkTMin(SkTMax(positionRect.fRight - clipRect.fRight, 0), positionRectWidth);
169 coordsRectR -= delta;
170 positionRect.fRight -= delta;
171
172 delta = SkTMin(SkTMax(positionRect.fBottom - clipRect.fBottom, 0), positionRectHeight);
173 coordsRectB -= delta;
174 positionRect.fBottom -= delta;
175
176 // Repack texel coordinates and index
177 coordsRectL = coordsRectL << 1 | pageIndexX;
178 coordsRectT = coordsRectT << 1 | pageIndexY;
179 coordsRectR = coordsRectR << 1 | pageIndexX;
180 coordsRectB = coordsRectB << 1 | pageIndexY;
181
182 // Set new positions and coords
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400183 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
184 currPosition->fX = positionRect.fLeft;
185 currPosition->fY = positionRect.fTop;
186 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400187 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
188 currCoords[0] = coordsRectL;
189 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400190 currVertex += vertexStride;
191
192 currPosition = reinterpret_cast<SkPoint*>(currVertex);
193 currPosition->fX = positionRect.fLeft;
194 currPosition->fY = positionRect.fBottom;
195 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400196 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
197 currCoords[0] = coordsRectL;
198 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400199 currVertex += vertexStride;
200
201 currPosition = reinterpret_cast<SkPoint*>(currVertex);
202 currPosition->fX = positionRect.fRight;
203 currPosition->fY = positionRect.fTop;
204 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400205 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
206 currCoords[0] = coordsRectR;
207 currCoords[1] = coordsRectT;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400208 currVertex += vertexStride;
209
210 currPosition = reinterpret_cast<SkPoint*>(currVertex);
211 currPosition->fX = positionRect.fRight;
212 currPosition->fY = positionRect.fBottom;
213 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
Jim Van Verth328a33f2017-10-20 12:14:22 -0400214 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
215 currCoords[0] = coordsRectR;
216 currCoords[1] = coordsRectB;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400217 currVertex += vertexStride;
218 }
219
220 blobVertices += 4 * vertexStride;
221 }
222}
223
Brian Salomon91326c32017-08-09 16:02:19 -0400224void GrAtlasTextOp::onPrepareDraws(Target* target) {
joshualitta751c972015-11-20 13:37:32 -0800225 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
226 // TODO actually only invert if we don't have RGBA
227 SkMatrix localMatrix;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500228 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
joshualitta751c972015-11-20 13:37:32 -0800229 SkDebugf("Cannot invert viewmatrix\n");
230 return;
231 }
232
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400233 GrMaskFormat maskFormat = this->maskFormat();
234
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500235 unsigned int atlasPageCount;
236 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &atlasPageCount);
Robert Phillips84d823a2018-02-26 16:03:50 +0000237 if (!atlasPageCount || !proxies[0]) {
joshualitta751c972015-11-20 13:37:32 -0800238 SkDebugf("Could not allocate backing texture for atlas\n");
239 return;
240 }
241
bsalomon342bfc22016-04-01 06:06:20 -0700242 FlushInfo flushInfo;
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400243 flushInfo.fPipeline =
244 target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip());
Brian Salomon5c6ac642017-12-19 11:09:32 -0500245 SkDEBUGCODE(bool dfPerspective = false);
joshualittd9d30f72015-12-08 10:47:55 -0800246 if (this->usesDistanceFields()) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400247 flushInfo.fGeometryProcessor = this->setupDfProcessor();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500248 SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective());
joshualitta751c972015-11-20 13:37:32 -0800249 } else {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400250 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400251 this->color(), proxies, GrSamplerState::ClampNearest(), maskFormat,
252 localMatrix, this->usesLocalCoords());
joshualitta751c972015-11-20 13:37:32 -0800253 }
254
joshualitta751c972015-11-20 13:37:32 -0800255 flushInfo.fGlyphsToFlush = 0;
bsalomon342bfc22016-04-01 06:06:20 -0700256 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500257 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat, dfPerspective));
joshualitta751c972015-11-20 13:37:32 -0800258
joshualitta751c972015-11-20 13:37:32 -0800259 int glyphCount = this->numGlyphs();
cdalton397536c2016-03-25 12:15:03 -0700260 const GrBuffer* vertexBuffer;
joshualitta751c972015-11-20 13:37:32 -0800261
Brian Salomon344ec422016-12-15 10:58:41 -0500262 void* vertices = target->makeVertexSpace(
263 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
joshualitta751c972015-11-20 13:37:32 -0800264 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400265 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
joshualitta751c972015-11-20 13:37:32 -0800266 if (!vertices || !flushInfo.fVertexBuffer) {
267 SkDebugf("Could not allocate vertices\n");
268 return;
269 }
270
Brian Salomon18923f92017-11-06 16:26:02 -0500271 char* currVertex = reinterpret_cast<char*>(vertices);
joshualitta751c972015-11-20 13:37:32 -0800272
bsalomond1c71fd2016-05-19 12:51:46 -0700273 SkAutoGlyphCache glyphCache;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400274 // each of these is a SubRun
joshualitta751c972015-11-20 13:37:32 -0800275 for (int i = 0; i < fGeoCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800276 const Geometry& args = fGeoData[i];
joshualitta751c972015-11-20 13:37:32 -0800277 Blob* blob = args.fBlob;
Brian Salomon18923f92017-11-06 16:26:02 -0500278 GrAtlasTextBlob::VertexRegenerator regenerator(
Robert Phillips84d823a2018-02-26 16:03:50 +0000279 blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY, args.fColor,
280 target->deferredUploadTarget(), fFontCache, &glyphCache);
Brian Salomon18923f92017-11-06 16:26:02 -0500281 GrAtlasTextBlob::VertexRegenerator::Result result;
282 do {
283 result = regenerator.regenerate();
284 // Copy regenerated vertices from the blob to our vertex buffer.
285 size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride;
286 if (args.fClipRect.isEmpty()) {
287 memcpy(currVertex, result.fFirstVertex, vertexBytes);
288 } else {
Brian Salomon5c6ac642017-12-19 11:09:32 -0500289 SkASSERT(!dfPerspective);
Brian Salomon18923f92017-11-06 16:26:02 -0500290 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
291 result.fGlyphsRegenerated);
292 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500293 if (this->usesDistanceFields() && !args.fViewMatrix.isIdentity()) {
294 // We always do the distance field view matrix transformation after copying rather
295 // than during blob vertex generation time in the blob as handling successive
296 // arbitrary transformations would be complicated and accumulate error.
297 if (args.fViewMatrix.hasPerspective()) {
298 auto* pos = reinterpret_cast<SkPoint3*>(currVertex);
Brian Salomon7c2192b2018-01-08 09:47:57 -0500299 SkMatrixPriv::MapHomogeneousPointsWithStride(
300 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
301 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500302 } else {
303 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500304 SkMatrixPriv::MapPointsWithStride(
305 args.fViewMatrix, pos, vertexStride,
306 result.fGlyphsRegenerated * kVerticesPerGlyph);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500307 }
308 }
Brian Salomon18923f92017-11-06 16:26:02 -0500309 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
310 if (!result.fFinished) {
311 this->flush(target, &flushInfo);
312 }
313 currVertex += vertexBytes;
314 } while (!result.fFinished);
joshualitta751c972015-11-20 13:37:32 -0800315 }
joshualitta751c972015-11-20 13:37:32 -0800316 this->flush(target, &flushInfo);
317}
318
Brian Salomone5b399e2017-07-19 13:50:54 -0400319void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
321 GrMaskFormat maskFormat = this->maskFormat();
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500322
323 unsigned int numProxies;
324 const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &numProxies);
325 if (gp->numTextureSamplers() != (int) numProxies) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400326 // During preparation the number of atlas pages has increased.
327 // Update the proxies used in the GP to match.
328 if (this->usesDistanceFields()) {
329 if (this->isLCD()) {
330 reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500331 proxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400332 } else {
333 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500334 proxies, GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400335 }
336 } else {
337 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500338 proxies, GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 }
340 }
341
Chris Dalton3809bab2017-06-13 10:55:06 -0600342 GrMesh mesh(GrPrimitiveType::kTriangles);
cdalton397536c2016-03-25 12:15:03 -0700343 int maxGlyphsPerDraw =
Brian Salomon344ec422016-12-15 10:58:41 -0500344 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600345 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
Chris Daltonbca46e22017-05-15 11:03:26 -0600346 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
Chris Dalton114a3c02017-05-26 15:17:19 -0600347 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400348 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
joshualitta751c972015-11-20 13:37:32 -0800349 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
350 flushInfo->fGlyphsToFlush = 0;
351}
352
Brian Salomon344ec422016-12-15 10:58:41 -0500353bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
354 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
Brian Salomon44acb5b2017-07-18 19:59:24 -0400355 if (fProcessors != that->fProcessors) {
356 return false;
357 }
358
359 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
joshualitta751c972015-11-20 13:37:32 -0800360 return false;
361 }
362
363 if (fMaskType != that->fMaskType) {
364 return false;
365 }
366
Brian Salomon5c6ac642017-12-19 11:09:32 -0500367 const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix;
368 const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix;
369
370 if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) {
371 return false;
372 }
373
374 if (this->usesDistanceFields()) {
375 if (fDFGPFlags != that->fDFGPFlags) {
joshualitta751c972015-11-20 13:37:32 -0800376 return false;
377 }
378
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400379 if (fLuminanceColor != that->fLuminanceColor) {
joshualitta751c972015-11-20 13:37:32 -0800380 return false;
381 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500382 } else {
383 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
384 return false;
385 }
joshualitta751c972015-11-20 13:37:32 -0800386 }
387
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400388 // Keep the batch vertex buffer size below 32K so we don't have to create a special one
389 // We use the largest possible vertex size for this
390 static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t);
Jim Van Verth56c37142017-10-31 14:44:25 -0400391 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400392 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
393 return false;
394 }
395
Brian Salomon344ec422016-12-15 10:58:41 -0500396 fNumGlyphs += that->numGlyphs();
joshualitta751c972015-11-20 13:37:32 -0800397
Jim Van Verth56c37142017-10-31 14:44:25 -0400398 // Reallocate space for geo data if necessary and then import that geo's data.
joshualitta751c972015-11-20 13:37:32 -0800399 int newGeoCount = that->fGeoCount + fGeoCount;
joshualitta751c972015-11-20 13:37:32 -0800400
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400401 // We reallocate at a rate of 1.5x to try to get better total memory usage
402 if (newGeoCount > fGeoDataAllocSize) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400403 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400404 while (newAllocSize < newGeoCount) {
405 newAllocSize += newAllocSize / 2;
406 }
joshualitta751c972015-11-20 13:37:32 -0800407 fGeoData.realloc(newAllocSize);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400408 fGeoDataAllocSize = newAllocSize;
joshualitta751c972015-11-20 13:37:32 -0800409 }
410
Brian Salomon344ec422016-12-15 10:58:41 -0500411 // 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 -0800412 // it doesn't try to unref them.
Brian Salomon344ec422016-12-15 10:58:41 -0500413 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
joshualitta751c972015-11-20 13:37:32 -0800414#ifdef SK_DEBUG
415 for (int i = 0; i < that->fGeoCount; ++i) {
416 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
417 }
418#endif
419 that->fGeoCount = 0;
420 fGeoCount = newGeoCount;
421
bsalomon88cf17d2016-07-08 06:40:56 -0700422 this->joinBounds(*that);
joshualitta751c972015-11-20 13:37:32 -0800423 return true;
424}
425
joshualitta751c972015-11-20 13:37:32 -0800426// TODO trying to figure out why lcd is so whack
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400427// (see comments in GrAtlasTextContext::ComputeCanonicalColor)
428sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor() const {
Robert Phillipsf3690dd2018-02-20 15:18:59 -0500429 unsigned int numProxies;
430 const sk_sp<GrTextureProxy>* p = fFontCache->getProxies(this->maskFormat(), &numProxies);
joshualitta751c972015-11-20 13:37:32 -0800431 bool isLCD = this->isLCD();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500432
433 SkMatrix localMatrix = SkMatrix::I();
434 if (this->usesLocalCoords()) {
435 // If this fails we'll just use I().
436 bool result = fGeoData[0].fViewMatrix.invert(&localMatrix);
437 (void)result;
438 }
joshualitta751c972015-11-20 13:37:32 -0800439
440 // see if we need to create a new effect
441 if (isLCD) {
brianosman0586f5c2016-04-12 12:48:21 -0700442 float redCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400443 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500444 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700445 float greenCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400446 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500447 fUseGammaCorrectDistanceTable);
brianosman0586f5c2016-04-12 12:48:21 -0700448 float blueCorrection = fDistanceAdjustTable->getAdjustment(
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400449 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
Brian Salomon344ec422016-12-15 10:58:41 -0500450 fUseGammaCorrectDistanceTable);
joshualitta751c972015-11-20 13:37:32 -0800451 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
Brian Salomon344ec422016-12-15 10:58:41 -0500452 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
453 redCorrection, greenCorrection, blueCorrection);
Brian Osman09068252018-01-03 09:57:29 -0500454 return GrDistanceFieldLCDTextGeoProc::Make(p, GrSamplerState::ClampBilerp(), widthAdjust,
455 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800456 } else {
joshualitta751c972015-11-20 13:37:32 -0800457#ifdef SK_GAMMA_APPLY_TO_A8
Jim Van Verth90e89b32017-07-06 16:36:55 -0400458 float correction = 0;
459 if (kAliasedDistanceField_MaskType != fMaskType) {
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400460 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
461 fLuminanceColor);
Jim Van Verth90e89b32017-07-06 16:36:55 -0400462 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
463 fUseGammaCorrectDistanceTable);
464 }
Brian Osman09068252018-01-03 09:57:29 -0500465 return GrDistanceFieldA8TextGeoProc::Make(p, GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500466 correction, fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800467#else
Brian Osman09068252018-01-03 09:57:29 -0500468 return GrDistanceFieldA8TextGeoProc::Make(p, GrSamplerState::ClampBilerp(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500469 fDFGPFlags, localMatrix);
joshualitta751c972015-11-20 13:37:32 -0800470#endif
471 }
joshualitta751c972015-11-20 13:37:32 -0800472}
joshualittddd22d82016-02-16 06:47:52 -0800473