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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrAtlasTextOp.h" |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPoint3.h" |
Brian Osman | a5842bc | 2021-05-11 13:41:46 -0400 | [diff] [blame] | 11 | #include "include/core/SkSpan.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkMathPriv.h" |
| 14 | #include "src/core/SkMatrixPriv.h" |
Herb Derby | 7a1d942 | 2020-07-06 14:18:01 -0400 | [diff] [blame] | 15 | #include "src/core/SkMatrixProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkStrikeCache.h" |
| 17 | #include "src/gpu/GrCaps.h" |
| 18 | #include "src/gpu/GrMemoryPool.h" |
| 19 | #include "src/gpu/GrOpFlushState.h" |
| 20 | #include "src/gpu/GrRecordingContextPriv.h" |
| 21 | #include "src/gpu/GrResourceProvider.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrSurfaceDrawContext.h" |
Herb Derby | 7a1d942 | 2020-07-06 14:18:01 -0400 | [diff] [blame] | 23 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/effects/GrBitmapTextGeoProc.h" |
| 25 | #include "src/gpu/effects/GrDistanceFieldGeoProc.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 26 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/text/GrAtlasManager.h" |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 28 | #include "src/gpu/text/GrDistanceFieldAdjustTable.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 29 | |
Herb Derby | a08bde6 | 2020-06-12 15:46:06 -0400 | [diff] [blame] | 30 | #if GR_TEST_UTILS |
| 31 | #include "src/gpu/GrDrawOpTest.h" |
| 32 | #endif |
| 33 | |
Herb Derby | 93330c9 | 2021-02-26 12:18:26 -0500 | [diff] [blame] | 34 | #include <new> |
| 35 | #include <utility> |
| 36 | |
| 37 | // If we have thread local, then cache memory for a single GrAtlasTextOp. |
Herb Derby | 3230255 | 2021-03-11 18:27:35 -0500 | [diff] [blame] | 38 | #if defined(GR_HAS_THREAD_LOCAL) |
Herb Derby | 93330c9 | 2021-02-26 12:18:26 -0500 | [diff] [blame] | 39 | static thread_local void* gCache = nullptr; |
| 40 | void* GrAtlasTextOp::operator new(size_t s) { |
| 41 | if (gCache != nullptr) { |
| 42 | return std::exchange(gCache, nullptr); |
| 43 | } |
| 44 | |
| 45 | return ::operator new(s); |
| 46 | } |
| 47 | |
| 48 | void GrAtlasTextOp::operator delete(void* bytes) noexcept { |
| 49 | if (gCache == nullptr) { |
| 50 | gCache = bytes; |
| 51 | return; |
| 52 | } |
| 53 | ::operator delete(bytes); |
| 54 | } |
| 55 | |
| 56 | void GrAtlasTextOp::ClearCache() { |
| 57 | ::operator delete(gCache); |
| 58 | gCache = nullptr; |
| 59 | } |
| 60 | #endif |
joshualitt | 60ce86d | 2015-11-23 13:08:22 -0800 | [diff] [blame] | 61 | |
Herb Derby | 3c873af | 2020-04-29 15:56:07 -0400 | [diff] [blame] | 62 | GrAtlasTextOp::GrAtlasTextOp(MaskType maskType, |
Herb Derby | 268e48b | 2020-07-16 12:56:58 -0400 | [diff] [blame] | 63 | bool needsTransform, |
| 64 | int glyphCount, |
| 65 | SkRect deviceRect, |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 66 | Geometry* geo, |
Herb Derby | 1d17e49 | 2020-07-21 11:45:04 -0400 | [diff] [blame] | 67 | GrPaint&& paint) |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 68 | : INHERITED{ClassID()} |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 69 | , fProcessors(std::move(paint)) |
| 70 | , fNumGlyphs(glyphCount) |
| 71 | , fDFGPFlags(0) |
| 72 | , fMaskType(static_cast<uint32_t>(maskType)) |
| 73 | , fUsesLocalCoords(false) |
| 74 | , fNeedsGlyphTransform(needsTransform) |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 75 | , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective()) |
| 76 | , fUseGammaCorrectDistanceTable(false) |
| 77 | , fHead{geo} |
| 78 | , fTail{&fHead->fNext} { |
Herb Derby | 268e48b | 2020-07-16 12:56:58 -0400 | [diff] [blame] | 79 | // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds |
| 80 | // we treat this as a set of non-AA rects rendered with a texture. |
| 81 | this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo); |
| 82 | } |
| 83 | |
| 84 | GrAtlasTextOp::GrAtlasTextOp(MaskType maskType, |
| 85 | bool needsTransform, |
| 86 | int glyphCount, |
| 87 | SkRect deviceRect, |
Herb Derby | 3c873af | 2020-04-29 15:56:07 -0400 | [diff] [blame] | 88 | SkColor luminanceColor, |
| 89 | bool useGammaCorrectDistanceTable, |
Herb Derby | 268e48b | 2020-07-16 12:56:58 -0400 | [diff] [blame] | 90 | uint32_t DFGPFlags, |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 91 | Geometry* geo, |
Herb Derby | 1d17e49 | 2020-07-21 11:45:04 -0400 | [diff] [blame] | 92 | GrPaint&& paint) |
Herb Derby | 268e48b | 2020-07-16 12:56:58 -0400 | [diff] [blame] | 93 | : INHERITED{ClassID()} |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 94 | , fProcessors(std::move(paint)) |
| 95 | , fNumGlyphs(glyphCount) |
| 96 | , fDFGPFlags(DFGPFlags) |
| 97 | , fMaskType(static_cast<uint32_t>(maskType)) |
| 98 | , fUsesLocalCoords(false) |
| 99 | , fNeedsGlyphTransform(needsTransform) |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 100 | , fHasPerspective(needsTransform && geo->fDrawMatrix.hasPerspective()) |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 101 | , fUseGammaCorrectDistanceTable(useGammaCorrectDistanceTable) |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 102 | , fLuminanceColor(luminanceColor) |
| 103 | , fHead{geo} |
| 104 | , fTail{&fHead->fNext} { |
Herb Derby | 3c873af | 2020-04-29 15:56:07 -0400 | [diff] [blame] | 105 | // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds |
| 106 | // we treat this as a set of non-AA rects rendered with a texture. |
Herb Derby | 268e48b | 2020-07-16 12:56:58 -0400 | [diff] [blame] | 107 | this->setBounds(deviceRect, HasAABloat::kNo, IsHairline::kNo); |
Herb Derby | 3c873af | 2020-04-29 15:56:07 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Herb Derby | bba1880 | 2021-03-31 16:45:54 -0400 | [diff] [blame] | 110 | auto GrAtlasTextOp::Geometry::MakeForBlob(const GrAtlasSubRun& subRun, |
Herb Derby | 2e0b5bb | 2020-11-19 11:03:50 -0500 | [diff] [blame] | 111 | const SkMatrix& drawMatrix, |
| 112 | SkPoint drawOrigin, |
| 113 | SkIRect clipRect, |
| 114 | sk_sp<GrTextBlob> blob, |
Herb Derby | 0b1228d | 2021-04-05 18:38:35 -0400 | [diff] [blame] | 115 | const SkPMColor4f& color, |
| 116 | SkArenaAlloc* alloc) -> Geometry* { |
| 117 | // Bypass the automatic dtor behavior in SkArenaAlloc. I'm leaving this up to the Op to run |
| 118 | // all geometry dtors for now. |
| 119 | void* geo = alloc->makeBytesAlignedTo(sizeof(Geometry), alignof(Geometry)); |
| 120 | return new(geo) Geometry{subRun, |
| 121 | drawMatrix, |
| 122 | drawOrigin, |
| 123 | clipRect, |
| 124 | std::move(blob), |
| 125 | nullptr, |
| 126 | color}; |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Herb Derby | 64391c4 | 2020-05-16 14:32:15 -0400 | [diff] [blame] | 129 | void GrAtlasTextOp::Geometry::fillVertexData(void *dst, int offset, int count) const { |
Herb Derby | 4089418 | 2020-12-02 11:39:48 -0500 | [diff] [blame] | 130 | SkMatrix positionMatrix = fDrawMatrix; |
| 131 | positionMatrix.preTranslate(fDrawOrigin.x(), fDrawOrigin.y()); |
| 132 | fSubRun.fillVertexData( |
| 133 | dst, offset, count, fColor.toBytes_RGBA(), positionMatrix, fClipRect); |
Herb Derby | 64391c4 | 2020-05-16 14:32:15 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 136 | void GrAtlasTextOp::visitProxies(const GrVisitProxyFunc& func) const { |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 137 | fProcessors.visitProxies(func); |
Robert Phillips | e4fda6c | 2018-02-21 12:10:41 -0500 | [diff] [blame] | 138 | } |
| 139 | |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 140 | #if GR_TEST_UTILS |
John Stiles | 8dd1e22 | 2020-08-12 19:06:24 -0400 | [diff] [blame] | 141 | SkString GrAtlasTextOp::onDumpInfo() const { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 142 | SkString str; |
Michael Ludwig | 64596c5 | 2020-11-05 12:39:13 -0500 | [diff] [blame] | 143 | int i = 0; |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 144 | for(Geometry* geom = fHead; geom != nullptr; geom = geom->fNext) { |
Herb Derby | 1b8dcd1 | 2019-11-15 15:21:15 -0500 | [diff] [blame] | 145 | str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f\n", |
Michael Ludwig | 64596c5 | 2020-11-05 12:39:13 -0500 | [diff] [blame] | 146 | i++, |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 147 | geom->fColor.toBytes_RGBA(), |
| 148 | geom->fDrawOrigin.x(), |
| 149 | geom->fDrawOrigin.y()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 152 | str += fProcessors.dumpProcessors(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 153 | return str; |
| 154 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 155 | #endif |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 156 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 157 | GrDrawOp::FixedFunctionFlags GrAtlasTextOp::fixedFunctionFlags() const { |
| 158 | return FixedFunctionFlags::kNone; |
| 159 | } |
| 160 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 161 | GrProcessorSet::Analysis GrAtlasTextOp::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 162 | GrClampType clampType) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 163 | GrProcessorAnalysisCoverage coverage; |
| 164 | GrProcessorAnalysisColor color; |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 165 | if (this->maskType() == MaskType::kColorBitmap) { |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 166 | color.setToUnknown(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 167 | } else { |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 168 | // finalize() is called before any merging is done, so at this point there's at most one |
| 169 | // Geometry with a color. Later, for non-bitmap ops, we may have mixed colors. |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 170 | color.setToConstant(fHead->fColor); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 171 | } |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 172 | |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 173 | switch (this->maskType()) { |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 174 | case MaskType::kGrayscaleCoverage: |
| 175 | case MaskType::kAliasedDistanceField: |
| 176 | case MaskType::kGrayscaleDistanceField: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 177 | coverage = GrProcessorAnalysisCoverage::kSingleChannel; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 178 | break; |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 179 | case MaskType::kLCDCoverage: |
| 180 | case MaskType::kLCDDistanceField: |
| 181 | case MaskType::kLCDBGRDistanceField: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 182 | coverage = GrProcessorAnalysisCoverage::kLCD; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 183 | break; |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 184 | case MaskType::kColorBitmap: |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 185 | coverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 186 | break; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 187 | } |
Michael Ludwig | 136d878 | 2020-11-03 11:04:16 -0500 | [diff] [blame] | 188 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 189 | auto analysis = fProcessors.finalize(color, coverage, clip, &GrUserStencilSettings::kUnused, |
| 190 | caps, clampType, &fHead->fColor); |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 191 | // TODO(michaelludwig): Once processor analysis can be done external to op creation/finalization |
| 192 | // the atlas op metadata can be fully const. This is okay for now since finalize() happens |
| 193 | // before the op is merged, so during combineIfPossible, metadata is effectively const. |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 194 | fUsesLocalCoords = analysis.usesLocalCoords(); |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 195 | return analysis; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 198 | void GrAtlasTextOp::onPrepareDraws(GrMeshDrawTarget* target) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 199 | auto resourceProvider = target->resourceProvider(); |
| 200 | |
Michael Ludwig | 9597e2f | 2020-11-03 11:06:25 -0500 | [diff] [blame] | 201 | // If we need local coordinates, compute an inverse view matrix. If this is solid color, the |
| 202 | // processor analysis will not require local coords and the GPs will skip local coords when |
| 203 | // the matrix is identity. When the shaders require local coords, combineIfPossible requires all |
| 204 | // all geometries to have same draw matrix. |
| 205 | SkMatrix localMatrix = SkMatrix::I(); |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 206 | if (fUsesLocalCoords && !fHead->fDrawMatrix.invert(&localMatrix)) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 210 | GrAtlasManager* atlasManager = target->atlasManager(); |
Mike Klein | 99e002f | 2020-01-16 16:45:03 +0000 | [diff] [blame] | 211 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 212 | GrMaskFormat maskFormat = this->maskFormat(); |
| 213 | |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 214 | unsigned int numActiveViews; |
| 215 | const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews); |
| 216 | if (!views) { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 217 | SkDebugf("Could not allocate backing texture for atlas\n"); |
| 218 | return; |
| 219 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 220 | SkASSERT(views[0].proxy()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 221 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 222 | static constexpr int kMaxTextures = GrBitmapTextGeoProc::kMaxTextures; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 223 | static_assert(GrDistanceFieldA8TextGeoProc::kMaxTextures == kMaxTextures); |
| 224 | static_assert(GrDistanceFieldLCDTextGeoProc::kMaxTextures == kMaxTextures); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 225 | |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 226 | auto primProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 227 | for (unsigned i = 0; i < numActiveViews; ++i) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 228 | primProcProxies[i] = views[i].proxy(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 229 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the proxies |
| 230 | // don't get added during the visitProxies call. Thus we add them here. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 231 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 232 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 233 | |
| 234 | FlushInfo flushInfo; |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 235 | flushInfo.fPrimProcProxies = primProcProxies; |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 236 | flushInfo.fIndexBuffer = resourceProvider->refNonAAQuadIndexBuffer(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 237 | |
joshualitt | d9d30f7 | 2015-12-08 10:47:55 -0800 | [diff] [blame] | 238 | if (this->usesDistanceFields()) { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 239 | flushInfo.fGeometryProcessor = this->setupDfProcessor(target->allocator(), |
| 240 | *target->caps().shaderCaps(), |
Michael Ludwig | 9597e2f | 2020-11-03 11:06:25 -0500 | [diff] [blame] | 241 | localMatrix, views, numActiveViews); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 242 | } else { |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 243 | auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 244 | : GrSamplerState::Filter::kNearest; |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 245 | // Bitmap text uses a single color, combineIfPossible ensures all geometries have the same |
| 246 | // color, so we can use the first's without worry. |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 247 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 248 | target->allocator(), *target->caps().shaderCaps(), fHead->fColor, |
Michael Ludwig | 64596c5 | 2020-11-05 12:39:13 -0500 | [diff] [blame] | 249 | false, views, numActiveViews, filter, maskFormat, localMatrix, fHasPerspective); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 252 | const int vertexStride = (int)flushInfo.fGeometryProcessor->vertexStride(); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 253 | |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 254 | // Ensure we don't request an insanely large contiguous vertex allocation. |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 255 | static const int kMaxVertexBytes = GrBufferAllocPool::kDefaultBufferSize; |
| 256 | const int quadSize = vertexStride * kVerticesPerGlyph; |
| 257 | const int maxQuadsPerBuffer = kMaxVertexBytes / quadSize; |
| 258 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 259 | int allGlyphsCursor = 0; |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 260 | const int allGlyphsEnd = fNumGlyphs; |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 261 | int quadCursor; |
| 262 | int quadEnd; |
| 263 | char* vertices; |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 264 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 265 | auto resetVertexBuffer = [&] { |
| 266 | quadCursor = 0; |
| 267 | quadEnd = std::min(maxQuadsPerBuffer, allGlyphsEnd - allGlyphsCursor); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 268 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 269 | vertices = (char*)target->makeVertexSpace( |
| 270 | vertexStride, |
| 271 | kVerticesPerGlyph * quadEnd, |
| 272 | &flushInfo.fVertexBuffer, |
| 273 | &flushInfo.fVertexOffset); |
| 274 | |
| 275 | if (!vertices || !flushInfo.fVertexBuffer) { |
| 276 | SkDebugf("Could not allocate vertices\n"); |
| 277 | return false; |
| 278 | } |
| 279 | return true; |
| 280 | }; |
| 281 | |
| 282 | resetVertexBuffer(); |
| 283 | |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 284 | for (const Geometry* geo = fHead; geo != nullptr; geo = geo->fNext) { |
| 285 | const GrAtlasSubRun& subRun = geo->fSubRun; |
Herb Derby | 2e0b5bb | 2020-11-19 11:03:50 -0500 | [diff] [blame] | 286 | SkASSERTF((int) subRun.vertexStride(geo->fDrawMatrix) == vertexStride, |
| 287 | "subRun stride: %d vertex buffer stride: %d\n", |
| 288 | (int)subRun.vertexStride(geo->fDrawMatrix), vertexStride); |
Herb Derby | 62b12fe | 2020-01-14 17:57:24 -0500 | [diff] [blame] | 289 | |
Herb Derby | 43ad791 | 2020-07-20 16:14:19 -0400 | [diff] [blame] | 290 | const int subRunEnd = subRun.glyphCount(); |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 291 | for (int subRunCursor = 0; subRunCursor < subRunEnd;) { |
| 292 | // Regenerate the atlas for the remainder of the glyphs in the run, or the remainder |
| 293 | // of the glyphs to fill the vertex buffer. |
| 294 | int regenEnd = subRunCursor + std::min(subRunEnd - subRunCursor, quadEnd - quadCursor); |
Herb Derby | 43ad791 | 2020-07-20 16:14:19 -0400 | [diff] [blame] | 295 | auto[ok, glyphsRegenerated] = subRun.regenerateAtlas(subRunCursor, regenEnd, target); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 296 | // There was a problem allocating the glyph in the atlas. Bail. |
Robert Phillips | 1576e4e | 2020-04-01 12:49:45 -0400 | [diff] [blame] | 297 | if (!ok) { |
| 298 | return; |
| 299 | } |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 300 | |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 301 | geo->fillVertexData(vertices + quadCursor * quadSize, subRunCursor, glyphsRegenerated); |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 302 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 303 | subRunCursor += glyphsRegenerated; |
| 304 | quadCursor += glyphsRegenerated; |
| 305 | allGlyphsCursor += glyphsRegenerated; |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 306 | flushInfo.fGlyphsToFlush += glyphsRegenerated; |
| 307 | |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 308 | if (quadCursor == quadEnd || subRunCursor < subRunEnd) { |
| 309 | // Flush if not all the glyphs are drawn because either the quad buffer is full or |
| 310 | // the atlas is out of space. |
Herb Derby | 89acfe7 | 2021-01-28 10:57:40 -0500 | [diff] [blame] | 311 | if (subRunCursor < subRunEnd) { |
| 312 | ATRACE_ANDROID_FRAMEWORK_ALWAYS("Atlas full"); |
| 313 | } |
Herb Derby | 4513cdd | 2020-01-31 13:28:49 -0500 | [diff] [blame] | 314 | this->createDrawForGeneratedGlyphs(target, &flushInfo); |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 315 | if (quadCursor == quadEnd && allGlyphsCursor < allGlyphsEnd) { |
| 316 | // If the vertex buffer is full and there are still glyphs to draw then |
| 317 | // get a new buffer. |
| 318 | if(!resetVertexBuffer()) { |
Herb Derby | 23f2976 | 2020-01-10 16:26:14 -0500 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
Herb Derby | fd894ff | 2020-07-15 13:23:33 -0400 | [diff] [blame] | 324 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 327 | void GrAtlasTextOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 947a2eb | 2021-07-14 14:04:14 -0600 | [diff] [blame] | 328 | auto pipelineFlags = flushState->usesMSAASurface() ? GrPipeline::InputFlags::kHWAntialias |
| 329 | : GrPipeline::InputFlags::kNone; |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 330 | auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, |
| 331 | std::move(fProcessors), |
Chris Dalton | 947a2eb | 2021-07-14 14:04:14 -0600 | [diff] [blame] | 332 | pipelineFlags); |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 333 | |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 334 | flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline, |
| 335 | &GrUserStencilSettings::kUnused); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Herb Derby | 4513cdd | 2020-01-31 13:28:49 -0500 | [diff] [blame] | 338 | void GrAtlasTextOp::createDrawForGeneratedGlyphs( |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 339 | GrMeshDrawTarget* target, FlushInfo* flushInfo) const { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 340 | if (!flushInfo->fGlyphsToFlush) { |
| 341 | return; |
| 342 | } |
| 343 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 344 | auto atlasManager = target->atlasManager(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 345 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 346 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor; |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 347 | GrMaskFormat maskFormat = this->maskFormat(); |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 348 | |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 349 | unsigned int numActiveViews; |
| 350 | const GrSurfaceProxyView* views = atlasManager->getViews(maskFormat, &numActiveViews); |
| 351 | SkASSERT(views); |
Jim Van Verth | 9f2516f | 2019-11-22 14:58:37 -0500 | [diff] [blame] | 352 | // Something has gone terribly wrong, bail |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 353 | if (!views || 0 == numActiveViews) { |
Jim Van Verth | 9f2516f | 2019-11-22 14:58:37 -0500 | [diff] [blame] | 354 | return; |
| 355 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 356 | if (gp->numTextureSamplers() != (int) numActiveViews) { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 357 | // During preparation the number of atlas pages has increased. |
| 358 | // Update the proxies used in the GP to match. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 359 | for (unsigned i = gp->numTextureSamplers(); i < numActiveViews; ++i) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 360 | flushInfo->fPrimProcProxies[i] = views[i].proxy(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 361 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 362 | // proxies don't get added during the visitProxies call. Thus we add them here. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 363 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 364 | // These will get unreffed when the previously recorded draws destruct. |
| 365 | for (int d = 0; d < flushInfo->fNumDraws; ++d) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 366 | flushInfo->fPrimProcProxies[i]->ref(); |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 367 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 368 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 369 | if (this->usesDistanceFields()) { |
| 370 | if (this->isLCD()) { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 371 | reinterpret_cast<GrDistanceFieldLCDTextGeoProc*>(gp)->addNewViews( |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 372 | views, numActiveViews, GrSamplerState::Filter::kLinear); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 373 | } else { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 374 | reinterpret_cast<GrDistanceFieldA8TextGeoProc*>(gp)->addNewViews( |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 375 | views, numActiveViews, GrSamplerState::Filter::kLinear); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 376 | } |
| 377 | } else { |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 378 | auto filter = fNeedsGlyphTransform ? GrSamplerState::Filter::kLinear |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 379 | : GrSamplerState::Filter::kNearest; |
| 380 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(views, numActiveViews, filter); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 381 | } |
| 382 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 383 | int maxGlyphsPerDraw = static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6); |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 384 | GrSimpleMesh* mesh = target->allocMesh(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 385 | mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerGlyph, flushInfo->fGlyphsToFlush, |
| 386 | maxGlyphsPerDraw, flushInfo->fVertexBuffer, kVerticesPerGlyph, |
| 387 | flushInfo->fVertexOffset); |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 388 | target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies, |
Chris Dalton | 3bf2f3a | 2020-03-17 11:48:23 -0600 | [diff] [blame] | 389 | GrPrimitiveType::kTriangles); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 390 | flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush; |
| 391 | flushInfo->fGlyphsToFlush = 0; |
Brian Salomon | 43cbd72 | 2020-01-03 22:09:12 -0500 | [diff] [blame] | 392 | ++flushInfo->fNumDraws; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 395 | GrOp::CombineResult GrAtlasTextOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 396 | GrAtlasTextOp* that = t->cast<GrAtlasTextOp>(); |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 397 | |
| 398 | if (fDFGPFlags != that->fDFGPFlags || |
| 399 | fMaskType != that->fMaskType || |
| 400 | fUsesLocalCoords != that->fUsesLocalCoords || |
| 401 | fNeedsGlyphTransform != that->fNeedsGlyphTransform || |
| 402 | fHasPerspective != that->fHasPerspective || |
| 403 | fUseGammaCorrectDistanceTable != that->fUseGammaCorrectDistanceTable) { |
| 404 | // All flags must match for an op to be combined |
| 405 | return CombineResult::kCannotCombine; |
| 406 | } |
| 407 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 408 | if (fProcessors != that->fProcessors) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 409 | return CombineResult::kCannotCombine; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 412 | if (fUsesLocalCoords) { |
| 413 | // If the fragment processors use local coordinates, the GPs compute them using the inverse |
| 414 | // of the view matrix stored in a uniform, so all geometries must have the same matrix. |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 415 | const SkMatrix& thisFirstMatrix = fHead->fDrawMatrix; |
| 416 | const SkMatrix& thatFirstMatrix = that->fHead->fDrawMatrix; |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 417 | if (!SkMatrixPriv::CheapEqual(thisFirstMatrix, thatFirstMatrix)) { |
| 418 | return CombineResult::kCannotCombine; |
| 419 | } |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 422 | if (this->usesDistanceFields()) { |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 423 | SkASSERT(that->usesDistanceFields()); |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 424 | if (fLuminanceColor != that->fLuminanceColor) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 425 | return CombineResult::kCannotCombine; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 426 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 427 | } else { |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 428 | if (this->maskType() == MaskType::kColorBitmap && |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 429 | fHead->fColor != that->fHead->fColor) { |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 430 | // This ensures all merged bitmap color text ops have a constant color |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 431 | return CombineResult::kCannotCombine; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 432 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 435 | fNumGlyphs += that->fNumGlyphs; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 436 | |
Michael Ludwig | 64596c5 | 2020-11-05 12:39:13 -0500 | [diff] [blame] | 437 | // After concat, that's geometry list is emptied so it will not unref the blobs when destructed |
Herb Derby | 6b748e4 | 2020-12-02 17:44:54 -0500 | [diff] [blame] | 438 | this->addGeometry(that->fHead); |
| 439 | that->fHead = nullptr; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 440 | return CombineResult::kMerged; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 441 | } |
| 442 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 443 | // TODO trying to figure out why lcd is so whack |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 444 | GrGeometryProcessor* GrAtlasTextOp::setupDfProcessor(SkArenaAlloc* arena, |
| 445 | const GrShaderCaps& caps, |
Michael Ludwig | 9597e2f | 2020-11-03 11:06:25 -0500 | [diff] [blame] | 446 | const SkMatrix& localMatrix, |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 447 | const GrSurfaceProxyView* views, |
| 448 | unsigned int numActiveViews) const { |
Michael Ludwig | 9597e2f | 2020-11-03 11:06:25 -0500 | [diff] [blame] | 449 | static constexpr int kDistanceAdjustLumShift = 5; |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 450 | auto dfAdjustTable = GrDistanceFieldAdjustTable::Get(); |
| 451 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 452 | // see if we need to create a new effect |
Michael Ludwig | 9597e2f | 2020-11-03 11:06:25 -0500 | [diff] [blame] | 453 | if (this->isLCD()) { |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 454 | float redCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 455 | SkColorGetR(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 456 | fUseGammaCorrectDistanceTable); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 457 | float greenCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 458 | SkColorGetG(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 459 | fUseGammaCorrectDistanceTable); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 460 | float blueCorrection = dfAdjustTable->getAdjustment( |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 461 | SkColorGetB(fLuminanceColor) >> kDistanceAdjustLumShift, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 462 | fUseGammaCorrectDistanceTable); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 463 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust = |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 464 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make( |
| 465 | redCorrection, greenCorrection, blueCorrection); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 466 | return GrDistanceFieldLCDTextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 467 | GrSamplerState::Filter::kLinear, widthAdjust, |
Brian Osman | 0906825 | 2018-01-03 09:57:29 -0500 | [diff] [blame] | 468 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 469 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 470 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 471 | float correction = 0; |
Michael Ludwig | efc89d2 | 2020-11-05 11:43:10 -0500 | [diff] [blame] | 472 | if (this->maskType() != MaskType::kAliasedDistanceField) { |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 473 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, |
| 474 | fLuminanceColor); |
Robert Phillips | 841c9a5 | 2020-03-27 12:41:31 -0400 | [diff] [blame] | 475 | correction = dfAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift, |
| 476 | fUseGammaCorrectDistanceTable); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 477 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 478 | return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 479 | GrSamplerState::Filter::kLinear, correction, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 480 | fDFGPFlags, localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 481 | #else |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 482 | return GrDistanceFieldA8TextGeoProc::Make(arena, caps, views, numActiveViews, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 483 | GrSamplerState::Filter::kLinear, fDFGPFlags, |
| 484 | localMatrix); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 485 | #endif |
| 486 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 487 | } |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 488 | |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 489 | #if GR_TEST_UTILS |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 490 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 491 | GrOp::Owner GrAtlasTextOp::CreateOpTestingOnly(GrSurfaceDrawContext* rtc, |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 492 | const SkPaint& skPaint, |
| 493 | const SkFont& font, |
| 494 | const SkMatrixProvider& mtxProvider, |
| 495 | const char* text, |
| 496 | int x, |
| 497 | int y) { |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 498 | size_t textLen = (int)strlen(text); |
| 499 | |
Herb Derby | 861ef8f | 2021-02-26 16:49:06 -0500 | [diff] [blame] | 500 | SkMatrix drawMatrix(mtxProvider.localToDevice()); |
| 501 | drawMatrix.preTranslate(x, y); |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 502 | auto drawOrigin = SkPoint::Make(x, y); |
| 503 | SkGlyphRunBuilder builder; |
Herb Derby | 3c1ed9c | 2021-04-13 16:13:54 -0400 | [diff] [blame] | 504 | auto glyphRunList = builder.textToGlyphRunList(font, skPaint, text, textLen, drawOrigin); |
Ben Wagner | 525e876 | 2020-07-09 16:19:35 -0400 | [diff] [blame] | 505 | if (glyphRunList.empty()) { |
| 506 | return nullptr; |
| 507 | } |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 508 | |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 509 | auto rContext = rtc->recordingContext(); |
Herb Derby | bf2dd2a | 2021-03-04 10:13:22 -0500 | [diff] [blame] | 510 | GrSDFTControl control = |
| 511 | rContext->priv().getSDFTControl(rtc->surfaceProps().isUseDeviceIndependentFonts()); |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 512 | |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 513 | SkGlyphRunListPainter* painter = rtc->glyphRunPainter(); |
Herb Derby | 0da2c14 | 2021-03-22 15:28:23 -0400 | [diff] [blame] | 514 | sk_sp<GrTextBlob> blob = GrTextBlob::Make(glyphRunList, skPaint, drawMatrix, control, painter); |
Herb Derby | 63fe8e5 | 2021-03-08 13:22:56 -0500 | [diff] [blame] | 515 | |
Herb Derby | 55f795e | 2021-02-05 13:45:05 -0500 | [diff] [blame] | 516 | if (blob->subRunList().isEmpty()) { |
Ben Wagner | 525e876 | 2020-07-09 16:19:35 -0400 | [diff] [blame] | 517 | return nullptr; |
| 518 | } |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 519 | |
Herb Derby | 55f795e | 2021-02-05 13:45:05 -0500 | [diff] [blame] | 520 | GrAtlasSubRun* subRun = blob->subRunList().front().testingOnly_atlasSubRun(); |
Herb Derby | d90024d | 2020-11-20 10:21:32 -0500 | [diff] [blame] | 521 | SkASSERT(subRun); |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 522 | GrOp::Owner op; |
Herb Derby | 2e0b5bb | 2020-11-19 11:03:50 -0500 | [diff] [blame] | 523 | std::tie(std::ignore, op) = subRun->makeAtlasTextOp( |
Herb Derby | 0da2c14 | 2021-03-22 15:28:23 -0400 | [diff] [blame] | 524 | nullptr, mtxProvider, glyphRunList, skPaint, rtc, nullptr); |
Herb Derby | 411e7aa | 2020-07-09 16:02:08 -0400 | [diff] [blame] | 525 | return op; |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) { |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 529 | SkSimpleMatrixProvider matrixProvider(GrTest::TestMatrixInvertible(random)); |
| 530 | |
| 531 | SkPaint skPaint; |
| 532 | skPaint.setColor(random->nextU()); |
| 533 | |
| 534 | SkFont font; |
| 535 | if (random->nextBool()) { |
| 536 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 537 | } else { |
| 538 | font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias); |
| 539 | } |
| 540 | font.setSubpixel(random->nextBool()); |
| 541 | |
| 542 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 543 | |
| 544 | // create some random x/y offsets, including negative offsets |
| 545 | static const int kMaxTrans = 1024; |
| 546 | int xPos = (random->nextU() % 2) * 2 - 1; |
| 547 | int yPos = (random->nextU() % 2) * 2 - 1; |
| 548 | int xInt = (random->nextU() % kMaxTrans) * xPos; |
| 549 | int yInt = (random->nextU() % kMaxTrans) * yPos; |
| 550 | |
Herb Derby | 0b1228d | 2021-04-05 18:38:35 -0400 | [diff] [blame] | 551 | return GrAtlasTextOp::CreateOpTestingOnly(sdc, skPaint, font, matrixProvider, text, xInt, yInt); |
Herb Derby | 4598fa1 | 2020-06-10 14:54:22 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | #endif |