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