1 /*
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
8 #include "GrAtlasTextOp.h"
9
10 #include "GrContext.h"
11 #include "GrOpFlushState.h"
12 #include "GrResourceProvider.h"
13 #include "SkGlyphCache.h"
14 #include "SkMathPriv.h"
15 #include "SkMatrixPriv.h"
16 #include "SkPoint3.h"
17 #include "effects/GrBitmapTextGeoProc.h"
18 #include "effects/GrDistanceFieldGeoProc.h"
19 #include "text/GrAtlasManager.h"
20 #include "text/GrGlyphCache.h"
21
22 ///////////////////////////////////////////////////////////////////////////////////////////////////
23
24 static const int kDistanceAdjustLumShift = 5;
25
init()26 void GrAtlasTextOp::init() {
27 const Geometry& geo = fGeoData[0];
28 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
visitProxies(const VisitProxyFunc & func) const54 void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const {
55 fProcessors.visitProxies(func);
56
57 // 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.
60 unsigned int numProxies;
61 const sk_sp<GrTextureProxy>* proxies = fRestrictedAtlasManager->getProxies(
62 this->maskFormat(), &numProxies);
63 for (unsigned int i = 0; i < numProxies; ++i) {
64 if (proxies[i]) {
65 func(proxies[i].get());
66 }
67 }
68 }
69
dumpInfo() const70 SkString GrAtlasTextOp::dumpInfo() const {
71 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,
77 fGeoData[i].fX,
78 fGeoData[i].fY,
79 fGeoData[i].fBlob->runCount());
80 }
81
82 str += fProcessors.dumpProcessors();
83 str += INHERITED::dumpInfo();
84 return str;
85 }
86
fixedFunctionFlags() const87 GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const {
88 return FixedFunctionFlags::kNone;
89 }
90
finalize(const GrCaps & caps,const GrAppliedClip * clip,GrPixelConfigIsClamped dstIsClamped)91 GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps,
92 const GrAppliedClip* clip,
93 GrPixelConfigIsClamped dstIsClamped) {
94 GrProcessorAnalysisCoverage coverage;
95 GrProcessorAnalysisColor color;
96 if (kColorBitmapMask_MaskType == fMaskType) {
97 color.setToUnknown();
98 } else {
99 color.setToConstant(this->color());
100 }
101 switch (fMaskType) {
102 case kGrayscaleCoverageMask_MaskType:
103 case kAliasedDistanceField_MaskType:
104 case kGrayscaleDistanceField_MaskType:
105 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
106 break;
107 case kLCDCoverageMask_MaskType:
108 case kLCDDistanceField_MaskType:
109 case kLCDBGRDistanceField_MaskType:
110 coverage = GrProcessorAnalysisCoverage::kLCD;
111 break;
112 case kColorBitmapMask_MaskType:
113 coverage = GrProcessorAnalysisCoverage::kNone;
114 break;
115 }
116 auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, dstIsClamped,
117 &fGeoData[0].fColor);
118 fUsesLocalCoords = analysis.usesLocalCoords();
119 fCanCombineOnTouchOrOverlap =
120 !analysis.requiresDstTexture() &&
121 !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps));
122 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
123 }
124
clip_quads(const SkIRect & clipRect,char * currVertex,const char * blobVertices,size_t vertexStride,int glyphCount)125 static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices,
126 size_t vertexStride, int glyphCount) {
127 for (int i = 0; i < glyphCount; ++i) {
128 const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices);
129 const SkPoint* blobPositionRB =
130 reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride);
131
132 // positions for bitmap glyphs are pixel boundary aligned
133 SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX),
134 SkScalarRoundToInt(blobPositionLT->fY),
135 SkScalarRoundToInt(blobPositionRB->fX),
136 SkScalarRoundToInt(blobPositionRB->fY));
137 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.
144 auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint));
145 size_t coordOffset = vertexStride - 2*sizeof(uint16_t);
146 auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset);
147 auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride +
148 coordOffset);
149 // 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;
156
157 int positionRectWidth = positionRect.width();
158 int positionRectHeight = positionRect.height();
159 SkASSERT(positionRectWidth == (coordsRectR - coordsRectL));
160 SkASSERT(positionRectHeight == (coordsRectB - coordsRectT));
161
162 // Clip position and texCoords to the clipRect
163 unsigned int delta;
164 delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth);
165 coordsRectL += delta;
166 positionRect.fLeft += delta;
167
168 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
187 SkPoint* currPosition = reinterpret_cast<SkPoint*>(currVertex);
188 currPosition->fX = positionRect.fLeft;
189 currPosition->fY = positionRect.fTop;
190 *(reinterpret_cast<SkColor*>(currVertex + sizeof(SkPoint))) = color;
191 uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
192 currCoords[0] = coordsRectL;
193 currCoords[1] = coordsRectT;
194 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;
200 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
201 currCoords[0] = coordsRectL;
202 currCoords[1] = coordsRectB;
203 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;
209 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
210 currCoords[0] = coordsRectR;
211 currCoords[1] = coordsRectT;
212 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;
218 currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset);
219 currCoords[0] = coordsRectR;
220 currCoords[1] = coordsRectB;
221 currVertex += vertexStride;
222 }
223
224 blobVertices += 4 * vertexStride;
225 }
226 }
227
onPrepareDraws(Target * target)228 void GrAtlasTextOp::onPrepareDraws(Target* target) {
229 auto resourceProvider = target->resourceProvider();
230
231 // 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;
234 if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) {
235 SkDebugf("Cannot invert viewmatrix\n");
236 return;
237 }
238
239 GrAtlasManager* fullAtlasManager = target->fullAtlasManager();
240 SkASSERT(fRestrictedAtlasManager == fullAtlasManager);
241 GrGlyphCache* glyphCache = target->glyphCache();
242
243 GrMaskFormat maskFormat = this->maskFormat();
244
245 unsigned int atlasPageCount;
246 const sk_sp<GrTextureProxy>* proxies = fullAtlasManager->getProxies(maskFormat,
247 &atlasPageCount);
248 if (!proxies[0]) {
249 SkDebugf("Could not allocate backing texture for atlas\n");
250 return;
251 }
252
253 FlushInfo flushInfo;
254 flushInfo.fPipeline =
255 target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip());
256 SkDEBUGCODE(bool dfPerspective = false);
257 if (this->usesDistanceFields()) {
258 flushInfo.fGeometryProcessor = this->setupDfProcessor(fullAtlasManager);
259 SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective());
260 } else {
261 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
262 this->color(), proxies, atlasPageCount, GrSamplerState::ClampNearest(), maskFormat,
263 localMatrix, this->usesLocalCoords());
264 }
265
266 flushInfo.fGlyphsToFlush = 0;
267 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
268 SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat, dfPerspective));
269
270 int glyphCount = this->numGlyphs();
271 const GrBuffer* vertexBuffer;
272
273 void* vertices = target->makeVertexSpace(
274 vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
275 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
276 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
277 if (!vertices || !flushInfo.fVertexBuffer) {
278 SkDebugf("Could not allocate vertices\n");
279 return;
280 }
281
282 char* currVertex = reinterpret_cast<char*>(vertices);
283
284 SkAutoGlyphCache autoGlyphCache;
285 // each of these is a SubRun
286 for (int i = 0; i < fGeoCount; i++) {
287 const Geometry& args = fGeoData[i];
288 Blob* blob = args.fBlob;
289 GrAtlasTextBlob::VertexRegenerator regenerator(
290 resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY,
291 args.fColor, target->deferredUploadTarget(), glyphCache, fullAtlasManager,
292 &autoGlyphCache);
293 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 {
301 SkASSERT(!dfPerspective);
302 clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride,
303 result.fGlyphsRegenerated);
304 }
305 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);
311 SkMatrixPriv::MapHomogeneousPointsWithStride(
312 args.fViewMatrix, pos, vertexStride, pos, vertexStride,
313 result.fGlyphsRegenerated * kVerticesPerGlyph);
314 } else {
315 auto* pos = reinterpret_cast<SkPoint*>(currVertex);
316 SkMatrixPriv::MapPointsWithStride(
317 args.fViewMatrix, pos, vertexStride,
318 result.fGlyphsRegenerated * kVerticesPerGlyph);
319 }
320 }
321 flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated;
322 if (!result.fFinished) {
323 this->flush(target, &flushInfo);
324 }
325 currVertex += vertexBytes;
326 } while (!result.fFinished);
327 }
328 this->flush(target, &flushInfo);
329 }
330
flush(GrMeshDrawOp::Target * target,FlushInfo * flushInfo) const331 void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
332 auto fullAtlasManager = target->fullAtlasManager();
333 SkASSERT(fRestrictedAtlasManager == fullAtlasManager);
334
335 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
336 GrMaskFormat maskFormat = this->maskFormat();
337
338 unsigned int numProxies;
339 const sk_sp<GrTextureProxy>* proxies = fullAtlasManager->getProxies(maskFormat, &numProxies);
340 if (gp->numTextureSamplers() != (int) numProxies) {
341 // 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(
346 proxies, numProxies, GrSamplerState::ClampBilerp());
347 } else {
348 reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies(
349 proxies, numProxies, GrSamplerState::ClampBilerp());
350 }
351 } else {
352 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
353 proxies, numProxies, GrSamplerState::ClampNearest());
354 }
355 }
356
357 GrMesh mesh(GrPrimitiveType::kTriangles);
358 int maxGlyphsPerDraw =
359 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
360 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph,
361 flushInfo->fGlyphsToFlush, maxGlyphsPerDraw);
362 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
363 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
364 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
365 flushInfo->fGlyphsToFlush = 0;
366 }
367
onCombineIfPossible(GrOp * t,const GrCaps & caps)368 bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
369 GrAtlasTextOp* that = t->cast<GrAtlasTextOp>();
370 if (fProcessors != that->fProcessors) {
371 return false;
372 }
373
374 if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
375 return false;
376 }
377
378 if (fMaskType != that->fMaskType) {
379 return false;
380 }
381
382 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) {
391 return false;
392 }
393
394 if (fLuminanceColor != that->fLuminanceColor) {
395 return false;
396 }
397 } else {
398 if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) {
399 return false;
400 }
401 }
402
403 // 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);
406 static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize);
407 if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) {
408 return false;
409 }
410
411 fNumGlyphs += that->numGlyphs();
412
413 // Reallocate space for geo data if necessary and then import that geo's data.
414 int newGeoCount = that->fGeoCount + fGeoCount;
415
416 // We reallocate at a rate of 1.5x to try to get better total memory usage
417 if (newGeoCount > fGeoDataAllocSize) {
418 int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2;
419 while (newAllocSize < newGeoCount) {
420 newAllocSize += newAllocSize / 2;
421 }
422 fGeoData.realloc(newAllocSize);
423 fGeoDataAllocSize = newAllocSize;
424 }
425
426 // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that
427 // it doesn't try to unref them.
428 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
429 #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
437 this->joinBounds(*that);
438 return true;
439 }
440
441 // TODO trying to figure out why lcd is so whack
442 // (see comments in GrAtlasTextContext::ComputeCanonicalColor)
setupDfProcessor(GrRestrictedAtlasManager * restrictedAtlasManager) const443 sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(
444 GrRestrictedAtlasManager* restrictedAtlasManager) const {
445 unsigned int numProxies;
446 const sk_sp<GrTextureProxy>* proxies = restrictedAtlasManager->getProxies(this->maskFormat(),
447 &numProxies);
448 bool isLCD = this->isLCD();
449
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 }
456
457 // see if we need to create a new effect
458 if (isLCD) {
459 float redCorrection = fDistanceAdjustTable->getAdjustment(
460 SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift,
461 fUseGammaCorrectDistanceTable);
462 float greenCorrection = fDistanceAdjustTable->getAdjustment(
463 SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift,
464 fUseGammaCorrectDistanceTable);
465 float blueCorrection = fDistanceAdjustTable->getAdjustment(
466 SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift,
467 fUseGammaCorrectDistanceTable);
468 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
469 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(
470 redCorrection, greenCorrection, blueCorrection);
471 return GrDistanceFieldLCDTextGeoProc::Make(proxies, numProxies,
472 GrSamplerState::ClampBilerp(), widthAdjust,
473 fDFGPFlags, localMatrix);
474 } else {
475 #ifdef SK_GAMMA_APPLY_TO_A8
476 float correction = 0;
477 if (kAliasedDistanceField_MaskType != fMaskType) {
478 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
479 fLuminanceColor);
480 correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
481 fUseGammaCorrectDistanceTable);
482 }
483 return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
484 GrSamplerState::ClampBilerp(),
485 correction, fDFGPFlags, localMatrix);
486 #else
487 return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies,
488 GrSamplerState::ClampBilerp(),
489 fDFGPFlags, localMatrix);
490 #endif
491 }
492 }
493
494