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 | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 8 | #ifndef GrAtlasTextOp_DEFINED |
| 9 | #define GrAtlasTextOp_DEFINED |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 10 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 11 | #include "ops/GrMeshDrawOp.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 12 | |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 13 | #include "text/GrAtlasTextContext.h" |
| 14 | #include "text/GrDistanceFieldAdjustTable.h" |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 15 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 16 | class GrAtlasTextOp final : public GrLegacyMeshDrawOp { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 17 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 18 | DEFINE_OP_CLASS_ID |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 19 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 20 | ~GrAtlasTextOp() override { |
| 21 | for (int i = 0; i < fGeoCount; i++) { |
| 22 | fGeoData[i].fBlob->unref(); |
| 23 | } |
| 24 | } |
| 25 | |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 26 | static const int kVerticesPerGlyph = GrAtlasTextBlob::kVerticesPerGlyph; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 27 | static const int kIndicesPerGlyph = 6; |
| 28 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 29 | typedef GrAtlasTextBlob Blob; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 30 | struct Geometry { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 31 | SkMatrix fViewMatrix; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 32 | Blob* fBlob; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 33 | SkScalar fX; |
| 34 | SkScalar fY; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 35 | int fRun; |
| 36 | int fSubRun; |
| 37 | GrColor fColor; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 40 | static std::unique_ptr<GrAtlasTextOp> MakeBitmap(GrMaskFormat maskFormat, int glyphCount, |
| 41 | GrAtlasGlyphCache* fontCache) { |
| 42 | std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 43 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 44 | op->fFontCache = fontCache; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 45 | switch (maskFormat) { |
| 46 | case kA8_GrMaskFormat: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 47 | op->fMaskType = kGrayscaleCoverageMask_MaskType; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 48 | break; |
| 49 | case kA565_GrMaskFormat: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 50 | op->fMaskType = kLCDCoverageMask_MaskType; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 51 | break; |
| 52 | case kARGB_GrMaskFormat: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 53 | op->fMaskType = kColorBitmapMask_MaskType; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 54 | break; |
| 55 | } |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 56 | op->fNumGlyphs = glyphCount; |
| 57 | op->fGeoCount = 1; |
| 58 | op->fFilteredColor = 0; |
| 59 | op->fFontCache = fontCache; |
| 60 | op->fUseBGR = false; |
| 61 | return op; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 64 | static std::unique_ptr<GrAtlasTextOp> MakeDistanceField( |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 65 | int glyphCount, GrAtlasGlyphCache* fontCache, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 66 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 67 | bool useGammaCorrectDistanceTable, GrColor filteredColor, bool isLCD, bool useBGR) { |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 68 | std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 69 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 70 | op->fFontCache = fontCache; |
| 71 | op->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType; |
| 72 | op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable)); |
| 73 | op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable; |
| 74 | op->fFilteredColor = filteredColor; |
| 75 | op->fUseBGR = useBGR; |
| 76 | op->fNumGlyphs = glyphCount; |
| 77 | op->fGeoCount = 1; |
| 78 | return op; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 81 | // To avoid even the initial copy of the struct, we have a getter for the first item which |
| 82 | // is used to seed the op with its initial geometry. After seeding, the client should call |
| 83 | // init() so the op can initialize itself |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 84 | Geometry& geometry() { return fGeoData[0]; } |
| 85 | |
| 86 | void init() { |
| 87 | const Geometry& geo = fGeoData[0]; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 88 | fColor = geo.fColor; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 89 | SkRect bounds; |
| 90 | geo.fBlob->computeSubRunBounds(&bounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX, |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 91 | geo.fY); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 92 | // We don't have tight bounds on the glyph paths in device space. For the purposes of bounds |
| 93 | // we treat this as a set of non-AA rects rendered with a texture. |
| 94 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 97 | const char* name() const override { return "AtlasTextOp"; } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 98 | |
| 99 | SkString dumpInfo() const override; |
| 100 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 101 | private: |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 102 | void getProcessorAnalysisInputs(GrProcessorAnalysisColor*, |
| 103 | GrProcessorAnalysisCoverage*) const override; |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 104 | void applyPipelineOptimizations(const PipelineOptimizations&) override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 105 | |
| 106 | struct FlushInfo { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 107 | sk_sp<const GrBuffer> fVertexBuffer; |
| 108 | sk_sp<const GrBuffer> fIndexBuffer; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 109 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 110 | int fGlyphsToFlush; |
| 111 | int fVertexOffset; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 114 | void onPrepareDraws(Target* target) const override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 115 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 116 | GrAtlasTextOp() : INHERITED(ClassID()) {} // initialized in factory functions. |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 117 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 118 | GrMaskFormat maskFormat() const { |
| 119 | switch (fMaskType) { |
| 120 | case kLCDCoverageMask_MaskType: |
| 121 | return kA565_GrMaskFormat; |
| 122 | case kColorBitmapMask_MaskType: |
| 123 | return kARGB_GrMaskFormat; |
| 124 | case kGrayscaleCoverageMask_MaskType: |
| 125 | case kGrayscaleDistanceField_MaskType: |
| 126 | case kLCDDistanceField_MaskType: |
| 127 | return kA8_GrMaskFormat; |
| 128 | } |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 129 | return kA8_GrMaskFormat; // suppress warning |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool usesDistanceFields() const { |
| 133 | return kGrayscaleDistanceField_MaskType == fMaskType || |
| 134 | kLCDDistanceField_MaskType == fMaskType; |
| 135 | } |
| 136 | |
| 137 | bool isLCD() const { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 138 | return kLCDCoverageMask_MaskType == fMaskType || kLCDDistanceField_MaskType == fMaskType; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 141 | inline void flush(GrLegacyMeshDrawOp::Target* target, FlushInfo* flushInfo) const; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 142 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 143 | GrColor color() const { return fColor; } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 144 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 145 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 146 | int numGlyphs() const { return fNumGlyphs; } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 147 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 148 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 149 | |
| 150 | // TODO just use class params |
| 151 | // TODO trying to figure out why lcd is so whack |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 152 | sk_sp<GrGeometryProcessor> setupDfProcessor(GrResourceProvider*, |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 153 | const SkMatrix& viewMatrix, GrColor filteredColor, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 154 | GrColor color, sk_sp<GrTextureProxy> proxy) const; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 155 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 156 | GrColor fColor; |
| 157 | bool fUsesLocalCoords; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 158 | int fNumGlyphs; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 159 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 160 | // The minimum number of Geometry we will try to allocate. |
| 161 | enum { kMinGeometryAllocated = 4 }; |
| 162 | SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData; |
| 163 | int fGeoCount; |
| 164 | |
| 165 | enum MaskType { |
| 166 | kGrayscaleCoverageMask_MaskType, |
| 167 | kLCDCoverageMask_MaskType, |
| 168 | kColorBitmapMask_MaskType, |
| 169 | kGrayscaleDistanceField_MaskType, |
| 170 | kLCDDistanceField_MaskType, |
| 171 | } fMaskType; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 172 | bool fUseBGR; // fold this into the enum? |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 173 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 174 | GrAtlasGlyphCache* fFontCache; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 175 | |
| 176 | // Distance field properties |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 177 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 178 | GrColor fFilteredColor; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 179 | bool fUseGammaCorrectDistanceTable; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 180 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 181 | friend class GrBlobRegenHelper; // Needs to trigger flushes |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 182 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 183 | typedef GrLegacyMeshDrawOp INHERITED; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 186 | /* |
| 187 | * A simple helper class to abstract the interface GrAtlasTextBlob needs to regenerate itself. |
| 188 | * It'd be nicer if this was nested, but we need to forward declare it in GrAtlasTextBlob.h |
| 189 | */ |
| 190 | class GrBlobRegenHelper { |
| 191 | public: |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 192 | GrBlobRegenHelper(const GrAtlasTextOp* op, GrLegacyMeshDrawOp::Target* target, |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 193 | GrAtlasTextOp::FlushInfo* flushInfo) |
| 194 | : fOp(op), fTarget(target), fFlushInfo(flushInfo) {} |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 195 | |
| 196 | void flush(); |
| 197 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 198 | void incGlyphCount(int glyphCount = 1) { fFlushInfo->fGlyphsToFlush += glyphCount; } |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 199 | |
| 200 | private: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 201 | const GrAtlasTextOp* fOp; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 202 | GrLegacyMeshDrawOp::Target* fTarget; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 203 | GrAtlasTextOp::FlushInfo* fFlushInfo; |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 206 | #endif |