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 | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 16 | class GrAtlasTextOp final : public GrMeshDrawOp { |
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, |
| 67 | bool useGammaCorrectDistanceTable, SkColor 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 | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 102 | void getFragmentProcessorAnalysisInputs(FragmentProcessorAnalysisInputs*) const override; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 103 | void applyPipelineOptimizations(const GrPipelineOptimizations&) override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 104 | |
| 105 | struct FlushInfo { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 106 | sk_sp<const GrBuffer> fVertexBuffer; |
| 107 | sk_sp<const GrBuffer> fIndexBuffer; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 108 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 109 | int fGlyphsToFlush; |
| 110 | int fVertexOffset; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 113 | void onPrepareDraws(Target* target) const override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 114 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 115 | GrAtlasTextOp() : INHERITED(ClassID()) {} // initialized in factory functions. |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 116 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 117 | GrMaskFormat maskFormat() const { |
| 118 | switch (fMaskType) { |
| 119 | case kLCDCoverageMask_MaskType: |
| 120 | return kA565_GrMaskFormat; |
| 121 | case kColorBitmapMask_MaskType: |
| 122 | return kARGB_GrMaskFormat; |
| 123 | case kGrayscaleCoverageMask_MaskType: |
| 124 | case kGrayscaleDistanceField_MaskType: |
| 125 | case kLCDDistanceField_MaskType: |
| 126 | return kA8_GrMaskFormat; |
| 127 | } |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 128 | return kA8_GrMaskFormat; // suppress warning |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool usesDistanceFields() const { |
| 132 | return kGrayscaleDistanceField_MaskType == fMaskType || |
| 133 | kLCDDistanceField_MaskType == fMaskType; |
| 134 | } |
| 135 | |
| 136 | bool isLCD() const { |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 137 | return kLCDCoverageMask_MaskType == fMaskType || kLCDDistanceField_MaskType == fMaskType; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 140 | inline void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 141 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 142 | GrColor color() const { return fColor; } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 143 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 144 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 145 | int numGlyphs() const { return fNumGlyphs; } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 146 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 147 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 148 | |
| 149 | // TODO just use class params |
| 150 | // TODO trying to figure out why lcd is so whack |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame^] | 151 | sk_sp<GrGeometryProcessor> setupDfProcessor(GrResourceProvider*, |
| 152 | const SkMatrix& viewMatrix, SkColor filteredColor, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 153 | GrColor color, sk_sp<GrTextureProxy> proxy) const; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 154 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 155 | GrColor fColor; |
| 156 | bool fUsesLocalCoords; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 157 | int fNumGlyphs; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 158 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 159 | // The minimum number of Geometry we will try to allocate. |
| 160 | enum { kMinGeometryAllocated = 4 }; |
| 161 | SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData; |
| 162 | int fGeoCount; |
| 163 | |
| 164 | enum MaskType { |
| 165 | kGrayscaleCoverageMask_MaskType, |
| 166 | kLCDCoverageMask_MaskType, |
| 167 | kColorBitmapMask_MaskType, |
| 168 | kGrayscaleDistanceField_MaskType, |
| 169 | kLCDDistanceField_MaskType, |
| 170 | } fMaskType; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 171 | bool fUseBGR; // fold this into the enum? |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 172 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 173 | GrAtlasGlyphCache* fFontCache; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 174 | |
| 175 | // Distance field properties |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 176 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 177 | SkColor fFilteredColor; |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 178 | bool fUseGammaCorrectDistanceTable; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 179 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 180 | friend class GrBlobRegenHelper; // Needs to trigger flushes |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 181 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 182 | typedef GrMeshDrawOp INHERITED; |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 185 | /* |
| 186 | * A simple helper class to abstract the interface GrAtlasTextBlob needs to regenerate itself. |
| 187 | * It'd be nicer if this was nested, but we need to forward declare it in GrAtlasTextBlob.h |
| 188 | */ |
| 189 | class GrBlobRegenHelper { |
| 190 | public: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 191 | GrBlobRegenHelper(const GrAtlasTextOp* op, GrMeshDrawOp::Target* target, |
| 192 | GrAtlasTextOp::FlushInfo* flushInfo) |
| 193 | : fOp(op), fTarget(target), fFlushInfo(flushInfo) {} |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 194 | |
| 195 | void flush(); |
| 196 | |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 197 | void incGlyphCount(int glyphCount = 1) { fFlushInfo->fGlyphsToFlush += glyphCount; } |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 198 | |
| 199 | private: |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 200 | const GrAtlasTextOp* fOp; |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 201 | GrMeshDrawOp::Target* fTarget; |
Brian Salomon | 344ec42 | 2016-12-15 10:58:41 -0500 | [diff] [blame] | 202 | GrAtlasTextOp::FlushInfo* fFlushInfo; |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 205 | #endif |