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