joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 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 | |
Brian Salomon | 09e019e | 2016-12-15 10:20:35 -0500 | [diff] [blame] | 8 | #include "GrAtlasTextOp.h" |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 9 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 10 | #include "GrContext.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrOpFlushState.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 12 | #include "GrResourceProvider.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 13 | #include "SkGlyphCache.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 14 | #include "SkMathPriv.h" |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 15 | #include "SkMatrixPriv.h" |
| 16 | #include "SkPoint3.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 17 | #include "effects/GrBitmapTextGeoProc.h" |
| 18 | #include "effects/GrDistanceFieldGeoProc.h" |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 19 | #include "text/GrAtlasGlyphCache.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 20 | |
joshualitt | 60ce86d | 2015-11-23 13:08:22 -0800 | [diff] [blame] | 21 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 22 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 23 | static const int kDistanceAdjustLumShift = 5; |
| 24 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 25 | void GrAtlasTextOp::init() { |
| 26 | const Geometry& geo = fGeoData[0]; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 27 | 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 Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 53 | void GrAtlasTextOp::visitProxies(const VisitProxyFunc& func) const { |
| 54 | fProcessors.visitProxies(func); |
| 55 | |
| 56 | unsigned int numProxies; |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 57 | const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat(), |
| 58 | &numProxies); |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 59 | for (unsigned int i = 0; i < numProxies; ++i) { |
| 60 | if (proxies[i]) { |
| 61 | func(proxies[i].get()); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 66 | SkString GrAtlasTextOp::dumpInfo() const { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 67 | 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, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 73 | fGeoData[i].fX, |
| 74 | fGeoData[i].fY, |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 75 | fGeoData[i].fBlob->runCount()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 78 | str += fProcessors.dumpProcessors(); |
| 79 | str += INHERITED::dumpInfo(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 80 | return str; |
| 81 | } |
| 82 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 83 | GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const { |
| 84 | return FixedFunctionFlags::kNone; |
| 85 | } |
| 86 | |
| 87 | GrDrawOp::RequiresDstTexture GrAtlasTextOp::finalize(const GrCaps& caps, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 88 | const GrAppliedClip* clip, |
| 89 | GrPixelConfigIsClamped dstIsClamped) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 90 | GrProcessorAnalysisCoverage coverage; |
| 91 | GrProcessorAnalysisColor color; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 92 | if (kColorBitmapMask_MaskType == fMaskType) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 93 | color.setToUnknown(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 94 | } else { |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 95 | color.setToConstant(this->color()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 96 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 97 | switch (fMaskType) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 98 | case kGrayscaleCoverageMask_MaskType: |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 99 | case kAliasedDistanceField_MaskType: |
| 100 | case kGrayscaleDistanceField_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 101 | coverage = GrProcessorAnalysisCoverage::kSingleChannel; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 102 | break; |
| 103 | case kLCDCoverageMask_MaskType: |
| 104 | case kLCDDistanceField_MaskType: |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 105 | case kLCDBGRDistanceField_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 106 | coverage = GrProcessorAnalysisCoverage::kLCD; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 107 | break; |
| 108 | case kColorBitmapMask_MaskType: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 109 | coverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 110 | break; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 111 | } |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 112 | auto analysis = fProcessors.finalize(color, coverage, clip, false, caps, dstIsClamped, |
| 113 | &fGeoData[0].fColor); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 114 | fUsesLocalCoords = analysis.usesLocalCoords(); |
| 115 | fCanCombineOnTouchOrOverlap = |
| 116 | !analysis.requiresDstTexture() && |
| 117 | !(fProcessors.xferProcessor() && fProcessors.xferProcessor()->xferBarrierType(caps)); |
| 118 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 121 | static void clip_quads(const SkIRect& clipRect, char* currVertex, const char* blobVertices, |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 122 | size_t vertexStride, int glyphCount) { |
| 123 | for (int i = 0; i < glyphCount; ++i) { |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 124 | const SkPoint* blobPositionLT = reinterpret_cast<const SkPoint*>(blobVertices); |
| 125 | const SkPoint* blobPositionRB = |
| 126 | reinterpret_cast<const SkPoint*>(blobVertices + 3 * vertexStride); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 127 | |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 128 | // positions for bitmap glyphs are pixel boundary aligned |
Brian Osman | 7ca90d2 | 2017-11-10 15:31:27 -0500 | [diff] [blame] | 129 | SkIRect positionRect = SkIRect::MakeLTRB(SkScalarRoundToInt(blobPositionLT->fX), |
| 130 | SkScalarRoundToInt(blobPositionLT->fY), |
| 131 | SkScalarRoundToInt(blobPositionRB->fX), |
| 132 | SkScalarRoundToInt(blobPositionRB->fY)); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 133 | 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 Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 140 | auto color = *reinterpret_cast<const SkColor*>(blobVertices + sizeof(SkPoint)); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 141 | size_t coordOffset = vertexStride - 2*sizeof(uint16_t); |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 142 | auto* blobCoordsLT = reinterpret_cast<const uint16_t*>(blobVertices + coordOffset); |
| 143 | auto* blobCoordsRB = reinterpret_cast<const uint16_t*>(blobVertices + 3 * vertexStride + |
| 144 | coordOffset); |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 145 | // 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 Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 152 | |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 153 | int positionRectWidth = positionRect.width(); |
| 154 | int positionRectHeight = positionRect.height(); |
| 155 | SkASSERT(positionRectWidth == (coordsRectR - coordsRectL)); |
| 156 | SkASSERT(positionRectHeight == (coordsRectB - coordsRectT)); |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 157 | |
| 158 | // Clip position and texCoords to the clipRect |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 159 | unsigned int delta; |
| 160 | delta = SkTMin(SkTMax(clipRect.fLeft - positionRect.fLeft, 0), positionRectWidth); |
| 161 | coordsRectL += delta; |
| 162 | positionRect.fLeft += delta; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 163 | |
Jim Van Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 164 | 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 Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 183 | 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 Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 187 | uint16_t* currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 188 | currCoords[0] = coordsRectL; |
| 189 | currCoords[1] = coordsRectT; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 190 | 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 Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 196 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 197 | currCoords[0] = coordsRectL; |
| 198 | currCoords[1] = coordsRectB; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 199 | 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 Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 205 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 206 | currCoords[0] = coordsRectR; |
| 207 | currCoords[1] = coordsRectT; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 208 | 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 Verth | 328a33f | 2017-10-20 12:14:22 -0400 | [diff] [blame] | 214 | currCoords = reinterpret_cast<uint16_t*>(currVertex + coordOffset); |
| 215 | currCoords[0] = coordsRectR; |
| 216 | currCoords[1] = coordsRectB; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 217 | currVertex += vertexStride; |
| 218 | } |
| 219 | |
| 220 | blobVertices += 4 * vertexStride; |
| 221 | } |
| 222 | } |
| 223 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 224 | void GrAtlasTextOp::onPrepareDraws(Target* target) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 225 | auto resourceProvider = target->resourceProvider(); |
| 226 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 227 | // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix. |
| 228 | // TODO actually only invert if we don't have RGBA |
| 229 | SkMatrix localMatrix; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 230 | if (this->usesLocalCoords() && !fGeoData[0].fViewMatrix.invert(&localMatrix)) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 231 | SkDebugf("Cannot invert viewmatrix\n"); |
| 232 | return; |
| 233 | } |
| 234 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 235 | GrMaskFormat maskFormat = this->maskFormat(); |
| 236 | |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 237 | unsigned int atlasPageCount; |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 238 | const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &atlasPageCount); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 239 | if (!proxies[0]) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 240 | SkDebugf("Could not allocate backing texture for atlas\n"); |
| 241 | return; |
| 242 | } |
| 243 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 244 | FlushInfo flushInfo; |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 245 | flushInfo.fPipeline = |
| 246 | target->makePipeline(fSRGBFlags, std::move(fProcessors), target->detachAppliedClip()); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 247 | SkDEBUGCODE(bool dfPerspective = false); |
joshualitt | d9d30f7 | 2015-12-08 10:47:55 -0800 | [diff] [blame] | 248 | if (this->usesDistanceFields()) { |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 249 | flushInfo.fGeometryProcessor = this->setupDfProcessor(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 250 | SkDEBUGCODE(dfPerspective = fGeoData[0].fViewMatrix.hasPerspective()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 251 | } else { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 252 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 253 | this->color(), proxies, atlasPageCount, GrSamplerState::ClampNearest(), maskFormat, |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 254 | localMatrix, this->usesLocalCoords()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 255 | } |
| 256 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 257 | flushInfo.fGlyphsToFlush = 0; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 258 | size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 259 | SkASSERT(vertexStride == GrAtlasTextBlob::GetVertexStride(maskFormat, dfPerspective)); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 260 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 261 | int glyphCount = this->numGlyphs(); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 262 | const GrBuffer* vertexBuffer; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 263 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 264 | void* vertices = target->makeVertexSpace( |
| 265 | vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 266 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 267 | flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 268 | if (!vertices || !flushInfo.fVertexBuffer) { |
| 269 | SkDebugf("Could not allocate vertices\n"); |
| 270 | return; |
| 271 | } |
| 272 | |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 273 | char* currVertex = reinterpret_cast<char*>(vertices); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 274 | |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 275 | SkAutoGlyphCache glyphCache; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 276 | // each of these is a SubRun |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 277 | for (int i = 0; i < fGeoCount; i++) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 278 | const Geometry& args = fGeoData[i]; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 279 | Blob* blob = args.fBlob; |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 280 | GrAtlasTextBlob::VertexRegenerator regenerator( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 281 | resourceProvider, blob, args.fRun, args.fSubRun, args.fViewMatrix, args.fX, args.fY, |
| 282 | args.fColor, target->deferredUploadTarget(), fFontCache, &glyphCache); |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 283 | GrAtlasTextBlob::VertexRegenerator::Result result; |
| 284 | do { |
| 285 | result = regenerator.regenerate(); |
| 286 | // Copy regenerated vertices from the blob to our vertex buffer. |
| 287 | size_t vertexBytes = result.fGlyphsRegenerated * kVerticesPerGlyph * vertexStride; |
| 288 | if (args.fClipRect.isEmpty()) { |
| 289 | memcpy(currVertex, result.fFirstVertex, vertexBytes); |
| 290 | } else { |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 291 | SkASSERT(!dfPerspective); |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 292 | clip_quads(args.fClipRect, currVertex, result.fFirstVertex, vertexStride, |
| 293 | result.fGlyphsRegenerated); |
| 294 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 295 | if (this->usesDistanceFields() && !args.fViewMatrix.isIdentity()) { |
| 296 | // We always do the distance field view matrix transformation after copying rather |
| 297 | // than during blob vertex generation time in the blob as handling successive |
| 298 | // arbitrary transformations would be complicated and accumulate error. |
| 299 | if (args.fViewMatrix.hasPerspective()) { |
| 300 | auto* pos = reinterpret_cast<SkPoint3*>(currVertex); |
Brian Salomon | 7c2192b | 2018-01-08 09:47:57 -0500 | [diff] [blame] | 301 | SkMatrixPriv::MapHomogeneousPointsWithStride( |
| 302 | args.fViewMatrix, pos, vertexStride, pos, vertexStride, |
| 303 | result.fGlyphsRegenerated * kVerticesPerGlyph); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 304 | } else { |
| 305 | auto* pos = reinterpret_cast<SkPoint*>(currVertex); |
Brian Salomon | fa3783f | 2018-01-05 13:49:07 -0500 | [diff] [blame] | 306 | SkMatrixPriv::MapPointsWithStride( |
| 307 | args.fViewMatrix, pos, vertexStride, |
| 308 | result.fGlyphsRegenerated * kVerticesPerGlyph); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 309 | } |
| 310 | } |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 311 | flushInfo.fGlyphsToFlush += result.fGlyphsRegenerated; |
| 312 | if (!result.fFinished) { |
| 313 | this->flush(target, &flushInfo); |
| 314 | } |
| 315 | currVertex += vertexBytes; |
| 316 | } while (!result.fFinished); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 317 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 318 | this->flush(target, &flushInfo); |
| 319 | } |
| 320 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 321 | void GrAtlasTextOp::flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 322 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get(); |
| 323 | GrMaskFormat maskFormat = this->maskFormat(); |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 324 | |
| 325 | unsigned int numProxies; |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 326 | const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(maskFormat, &numProxies); |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 327 | if (gp->numTextureSamplers() != (int) numProxies) { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 328 | // During preparation the number of atlas pages has increased. |
| 329 | // Update the proxies used in the GP to match. |
| 330 | if (this->usesDistanceFields()) { |
| 331 | if (this->isLCD()) { |
| 332 | reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewProxies( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 333 | proxies, numProxies, GrSamplerState::ClampBilerp()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 334 | } else { |
| 335 | reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewProxies( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 336 | proxies, numProxies, GrSamplerState::ClampBilerp()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 337 | } |
| 338 | } else { |
| 339 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 340 | proxies, numProxies, GrSamplerState::ClampNearest()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 344 | GrMesh mesh(GrPrimitiveType::kTriangles); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 345 | int maxGlyphsPerDraw = |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 346 | static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 347 | mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerGlyph, kVerticesPerGlyph, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 348 | flushInfo->fGlyphsToFlush, maxGlyphsPerDraw); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 349 | mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 350 | target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 351 | flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush; |
| 352 | flushInfo->fGlyphsToFlush = 0; |
| 353 | } |
| 354 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 355 | bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 356 | GrAtlasTextOp* that = t->cast<GrAtlasTextOp>(); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 357 | if (fProcessors != that->fProcessors) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | if (!fCanCombineOnTouchOrOverlap && GrRectsTouchOrOverlap(this->bounds(), that->bounds())) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | if (fMaskType != that->fMaskType) { |
| 366 | return false; |
| 367 | } |
| 368 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 369 | const SkMatrix& thisFirstMatrix = fGeoData[0].fViewMatrix; |
| 370 | const SkMatrix& thatFirstMatrix = that->fGeoData[0].fViewMatrix; |
| 371 | |
| 372 | if (this->usesLocalCoords() && !thisFirstMatrix.cheapEqualTo(thatFirstMatrix)) { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | if (this->usesDistanceFields()) { |
| 377 | if (fDFGPFlags != that->fDFGPFlags) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 381 | if (fLuminanceColor != that->fLuminanceColor) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 382 | return false; |
| 383 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 384 | } else { |
| 385 | if (kColorBitmapMask_MaskType == fMaskType && this->color() != that->color()) { |
| 386 | return false; |
| 387 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 390 | // Keep the batch vertex buffer size below 32K so we don't have to create a special one |
| 391 | // We use the largest possible vertex size for this |
| 392 | static const int kVertexSize = sizeof(SkPoint) + sizeof(SkColor) + 2 * sizeof(uint16_t); |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 393 | static const int kMaxGlyphs = 32768 / (kVerticesPerGlyph * kVertexSize); |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 394 | if (this->fNumGlyphs + that->fNumGlyphs > kMaxGlyphs) { |
| 395 | return false; |
| 396 | } |
| 397 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 398 | fNumGlyphs += that->numGlyphs(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 399 | |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 400 | // Reallocate space for geo data if necessary and then import that geo's data. |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 401 | int newGeoCount = that->fGeoCount + fGeoCount; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 402 | |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 403 | // We reallocate at a rate of 1.5x to try to get better total memory usage |
| 404 | if (newGeoCount > fGeoDataAllocSize) { |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 405 | int newAllocSize = fGeoDataAllocSize + fGeoDataAllocSize / 2; |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 406 | while (newAllocSize < newGeoCount) { |
| 407 | newAllocSize += newAllocSize / 2; |
| 408 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 409 | fGeoData.realloc(newAllocSize); |
Jim Van Verth | c8a65e3 | 2017-10-25 14:25:27 -0400 | [diff] [blame] | 410 | fGeoDataAllocSize = newAllocSize; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 413 | // We steal the ref on the blobs from the other AtlasTextOp and set its count to 0 so that |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 414 | // it doesn't try to unref them. |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 415 | memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry)); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 416 | #ifdef SK_DEBUG |
| 417 | for (int i = 0; i < that->fGeoCount; ++i) { |
| 418 | that->fGeoData.get()[i].fBlob = (Blob*)0x1; |
| 419 | } |
| 420 | #endif |
| 421 | that->fGeoCount = 0; |
| 422 | fGeoCount = newGeoCount; |
| 423 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 424 | this->joinBounds(*that); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 428 | // TODO trying to figure out why lcd is so whack |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 429 | // (see comments in GrAtlasTextContext::ComputeCanonicalColor) |
Robert Phillips | 5c56af1 | 2018-02-28 16:37:34 +0000 | [diff] [blame] | 430 | sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor() const { |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 431 | unsigned int numProxies; |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 432 | const sk_sp<GrTextureProxy>* proxies = fFontCache->getProxies(this->maskFormat(), &numProxies); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 433 | bool isLCD = this->isLCD(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 434 | |
| 435 | SkMatrix localMatrix = SkMatrix::I(); |
| 436 | if (this->usesLocalCoords()) { |
| 437 | // If this fails we'll just use I(). |
| 438 | bool result = fGeoData[0].fViewMatrix.invert(&localMatrix); |
| 439 | (void)result; |
| 440 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 441 | |
| 442 | // see if we need to create a new effect |
| 443 | if (isLCD) { |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 444 | float redCorrection = fDistanceAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 445 | SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 446 | fUseGammaCorrectDistanceTable); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 447 | float greenCorrection = fDistanceAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 448 | SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 449 | fUseGammaCorrectDistanceTable); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 450 | float blueCorrection = fDistanceAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 451 | SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 452 | fUseGammaCorrectDistanceTable); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 453 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust = |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 454 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make( |
| 455 | redCorrection, greenCorrection, blueCorrection); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 456 | return GrDistanceFieldLCDTextGeoProc::Make(proxies, numProxies, |
| 457 | GrSamplerState::ClampBilerp(), widthAdjust, |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 458 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 459 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 460 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 461 | float correction = 0; |
| 462 | if (kAliasedDistanceField_MaskType != fMaskType) { |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 463 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, |
| 464 | fLuminanceColor); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 465 | correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift, |
| 466 | fUseGammaCorrectDistanceTable); |
| 467 | } |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 468 | return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies, |
| 469 | GrSamplerState::ClampBilerp(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 470 | correction, fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 471 | #else |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 472 | return GrDistanceFieldA8TextGeoProc::Make(proxies, numProxies, |
| 473 | GrSamplerState::ClampBilerp(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 474 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 475 | #endif |
| 476 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 477 | } |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 478 | |