joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [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 | |
| 8 | #ifndef GrAtlasTextBlob_DEFINED |
| 9 | #define GrAtlasTextBlob_DEFINED |
| 10 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 11 | #include "GrAtlasGlyphCache.h" |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 12 | #include "GrColor.h" |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 13 | #include "GrDrawOpAtlas.h" |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 15 | #include "GrTextUtils.h" |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 16 | #include "SkDescriptor.h" |
| 17 | #include "SkMaskFilter.h" |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 18 | #include "SkOpts.h" |
bsalomon | 8b6fa5e | 2016-05-19 16:23:47 -0700 | [diff] [blame] | 19 | #include "SkPathEffect.h" |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 20 | #include "SkPoint3.h" |
bsalomon | 8b6fa5e | 2016-05-19 16:23:47 -0700 | [diff] [blame] | 21 | #include "SkRasterizer.h" |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 22 | #include "SkSurfaceProps.h" |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 23 | #include "SkTInternalLList.h" |
| 24 | |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 25 | struct GrDistanceFieldAdjustTable; |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 26 | class GrMemoryPool; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 27 | class SkDrawFilter; |
| 28 | class SkTextBlob; |
| 29 | class SkTextBlobRunIterator; |
| 30 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 31 | // With this flag enabled, the GrAtlasTextContext will, as a sanity check, regenerate every blob |
| 32 | // that comes in to verify the integrity of its cache |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 33 | #define CACHE_SANITY_CHECK 0 |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 34 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 35 | /* |
| 36 | * A GrAtlasTextBlob contains a fully processed SkTextBlob, suitable for nearly immediate drawing |
| 37 | * on the GPU. These are initially created with valid positions and colors, but invalid |
| 38 | * texture coordinates. The GrAtlasTextBlob itself has a few Blob-wide properties, and also |
| 39 | * consists of a number of runs. Runs inside a blob are flushed individually so they can be |
| 40 | * reordered. |
| 41 | * |
| 42 | * The only thing(aside from a memcopy) required to flush a GrAtlasTextBlob is to ensure that |
| 43 | * the GrAtlas will not evict anything the Blob needs. |
| 44 | * |
| 45 | * Note: This struct should really be named GrCachedAtasTextBlob, but that is too verbose. |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 46 | * |
| 47 | * *WARNING* If you add new fields to this struct, then you may need to to update AssertEqual |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 48 | */ |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 49 | class GrAtlasTextBlob : public SkNVRefCnt<GrAtlasTextBlob> { |
| 50 | public: |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 51 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrAtlasTextBlob); |
| 52 | |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 53 | class VertexRegenerator; |
| 54 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 55 | static sk_sp<GrAtlasTextBlob> Make(GrMemoryPool* pool, int glyphCount, int runCount); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 56 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 57 | /** |
| 58 | * We currently force regeneration of a blob if old or new matrix differ in having perspective. |
| 59 | * If we ever change that then the key must contain the perspectiveness when there are distance |
| 60 | * fields as perspective distance field use 3 component vertex positions and non-perspective |
| 61 | * uses 2. |
| 62 | */ |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 63 | struct Key { |
| 64 | Key() { |
| 65 | sk_bzero(this, sizeof(Key)); |
| 66 | } |
| 67 | uint32_t fUniqueID; |
| 68 | // Color may affect the gamma of the mask we generate, but in a fairly limited way. |
| 69 | // Each color is assigned to on of a fixed number of buckets based on its |
| 70 | // luminance. For each luminance bucket there is a "canonical color" that |
| 71 | // represents the bucket. This functionality is currently only supported for A8 |
| 72 | SkColor fCanonicalColor; |
| 73 | SkPaint::Style fStyle; |
| 74 | SkPixelGeometry fPixelGeometry; |
| 75 | bool fHasBlur; |
brianosman | 8d7ffce | 2016-04-21 08:29:06 -0700 | [diff] [blame] | 76 | uint32_t fScalerContextFlags; |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 77 | |
| 78 | bool operator==(const Key& other) const { |
| 79 | return 0 == memcmp(this, &other, sizeof(Key)); |
| 80 | } |
| 81 | }; |
| 82 | |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 83 | void setupKey(const GrAtlasTextBlob::Key& key, |
| 84 | const SkMaskFilter::BlurRec& blurRec, |
| 85 | const SkPaint& paint) { |
| 86 | fKey = key; |
| 87 | if (key.fHasBlur) { |
| 88 | fBlurRec = blurRec; |
| 89 | } |
| 90 | if (key.fStyle != SkPaint::kFill_Style) { |
| 91 | fStrokeInfo.fFrameWidth = paint.getStrokeWidth(); |
| 92 | fStrokeInfo.fMiterLimit = paint.getStrokeMiter(); |
| 93 | fStrokeInfo.fJoin = paint.getStrokeJoin(); |
| 94 | } |
| 95 | } |
| 96 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 97 | static const Key& GetKey(const GrAtlasTextBlob& blob) { |
| 98 | return blob.fKey; |
| 99 | } |
| 100 | |
| 101 | static uint32_t Hash(const Key& key) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 102 | return SkOpts::hash(&key, sizeof(Key)); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void operator delete(void* p) { |
| 106 | GrAtlasTextBlob* blob = reinterpret_cast<GrAtlasTextBlob*>(p); |
| 107 | blob->fPool->release(p); |
| 108 | } |
| 109 | void* operator new(size_t) { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 110 | SK_ABORT("All blobs are created by placement new."); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 111 | return sk_malloc_throw(0); |
| 112 | } |
| 113 | |
| 114 | void* operator new(size_t, void* p) { return p; } |
| 115 | void operator delete(void* target, void* placement) { |
| 116 | ::operator delete(target, placement); |
| 117 | } |
| 118 | |
| 119 | bool hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); } |
| 120 | bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); } |
| 121 | void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; } |
| 122 | void setHasBitmap() { fTextType |= kHasBitmap_TextType; } |
| 123 | |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 124 | int runCount() const { return fRunCount; } |
| 125 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 126 | void push_back_run(int currRun) { |
| 127 | SkASSERT(currRun < fRunCount); |
| 128 | if (currRun > 0) { |
| 129 | Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back(); |
| 130 | Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back(); |
| 131 | newRun.setAsSuccessor(lastRun); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // sets the last subrun of runIndex to use distance field text |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 136 | void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias, bool hasWCoord) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 137 | Run& run = fRuns[runIndex]; |
| 138 | Run::SubRunInfo& subRun = run.fSubRunInfo.back(); |
| 139 | subRun.setUseLCDText(hasLCD); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 140 | subRun.setAntiAliased(isAntiAlias); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 141 | subRun.setDrawAsDistanceFields(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 142 | subRun.setHasWCoord(hasWCoord); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void setRunDrawAsPaths(int runIndex) { |
| 146 | fRuns[runIndex].fDrawAsPaths = true; |
| 147 | } |
| 148 | |
| 149 | void setMinAndMaxScale(SkScalar scaledMax, SkScalar scaledMin) { |
| 150 | // we init fMaxMinScale and fMinMaxScale in the constructor |
| 151 | fMaxMinScale = SkMaxScalar(scaledMax, fMaxMinScale); |
| 152 | fMinMaxScale = SkMinScalar(scaledMin, fMinMaxScale); |
| 153 | } |
| 154 | |
| 155 | // inits the override descriptor on the current run. All following subruns must use this |
| 156 | // descriptor |
| 157 | void initOverride(int runIndex) { |
| 158 | Run& run = fRuns[runIndex]; |
| 159 | // Push back a new subrun to fill and set the override descriptor |
| 160 | run.push_back(); |
| 161 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
| 162 | } |
| 163 | |
| 164 | SkGlyphCache* setupCache(int runIndex, |
| 165 | const SkSurfaceProps& props, |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 166 | uint32_t scalerContextFlags, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 167 | const SkPaint& skPaint, |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 168 | const SkMatrix* viewMatrix); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 169 | |
| 170 | // Appends a glyph to the blob. If the glyph is too large, the glyph will be appended |
| 171 | // as a path. |
| 172 | void appendGlyph(int runIndex, |
| 173 | const SkRect& positions, |
| 174 | GrColor color, |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 175 | GrAtlasTextStrike* strike, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 176 | GrGlyph* glyph, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 177 | SkGlyphCache*, const SkGlyph& skGlyph, |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 178 | SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 179 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 180 | static size_t GetVertexStride(GrMaskFormat maskFormat, bool isDistanceFieldWithWCoord) { |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 181 | switch (maskFormat) { |
| 182 | case kA8_GrMaskFormat: |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 183 | return isDistanceFieldWithWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize; |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 184 | case kARGB_GrMaskFormat: |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 185 | SkASSERT(!isDistanceFieldWithWCoord); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 186 | return kColorTextVASize; |
| 187 | default: |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 188 | SkASSERT(!isDistanceFieldWithWCoord); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 189 | return kLCDTextVASize; |
| 190 | } |
| 191 | } |
| 192 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 193 | bool mustRegenerate(const GrTextUtils::Paint&, const SkMaskFilter::BlurRec& blurRec, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 194 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y); |
| 195 | |
| 196 | // flush a GrAtlasTextBlob associated with a SkTextBlob |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 197 | void flushCached(GrContext* context, GrTextUtils::Target*, const SkTextBlob* blob, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 198 | const SkSurfaceProps& props, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 199 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
| 200 | const GrTextUtils::Paint&, SkDrawFilter* drawFilter, const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 201 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, SkScalar y); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 202 | |
| 203 | // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 204 | void flushThrowaway(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 205 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 206 | const GrTextUtils::Paint& paint, const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 207 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, |
| 208 | SkScalar y); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 209 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 210 | void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex, |
| 211 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 212 | // We don't yet position distance field text on the cpu, so we have to map the vertex bounds |
| 213 | // into device space. |
| 214 | // We handle vertex bounds differently for distance field text and bitmap text because |
| 215 | // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs |
| 216 | // from one blob then we are going to pay the price here of mapping the rect for each run. |
| 217 | const Run& run = fRuns[runIndex]; |
| 218 | const Run::SubRunInfo& subRun = run.fSubRunInfo[subRunIndex]; |
| 219 | *outBounds = subRun.vertexBounds(); |
| 220 | if (subRun.drawAsDistanceFields()) { |
| 221 | // Distance field text is positioned with the (X,Y) as part of the glyph position, |
| 222 | // and currently the view matrix is applied on the GPU |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 223 | outBounds->offset(x - fInitialX, y - fInitialY); |
| 224 | viewMatrix.mapRect(outBounds); |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 225 | } else { |
| 226 | // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in |
| 227 | // device space. |
| 228 | SkMatrix boundsMatrix = fInitialViewMatrixInverse; |
| 229 | |
| 230 | boundsMatrix.postTranslate(-fInitialX, -fInitialY); |
| 231 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 232 | boundsMatrix.postTranslate(x, y); |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 233 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 234 | boundsMatrix.postConcat(viewMatrix); |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 235 | boundsMatrix.mapRect(outBounds); |
| 236 | |
| 237 | // Due to floating point numerical inaccuracies, we have to round out here |
| 238 | outBounds->roundOut(outBounds); |
| 239 | } |
| 240 | } |
| 241 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 242 | // position + local coord |
| 243 | static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16); |
| 244 | static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 245 | static const size_t kGrayTextDFPerspectiveVASize = |
| 246 | sizeof(SkPoint3) + sizeof(GrColor) + sizeof(SkIPoint16); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 247 | static const size_t kLCDTextVASize = kGrayTextVASize; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 248 | static const size_t kMaxVASize = kGrayTextDFPerspectiveVASize; |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 249 | static const int kVerticesPerGlyph = 4; |
| 250 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 251 | static void AssertEqual(const GrAtlasTextBlob&, const GrAtlasTextBlob&); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 252 | |
| 253 | // The color here is the GrPaint color, and it is used to determine whether we |
| 254 | // have to regenerate LCD text blobs. |
| 255 | // We use this color vs the SkPaint color because it has the colorfilter applied. |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 256 | void initReusableBlob(SkColor luminanceColor, const SkMatrix& viewMatrix, |
| 257 | SkScalar x, SkScalar y) { |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 258 | fLuminanceColor = luminanceColor; |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 259 | this->setupViewMatrix(viewMatrix, x, y); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 260 | } |
| 261 | |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 262 | void initThrowawayBlob(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
| 263 | this->setupViewMatrix(viewMatrix, x, y); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 264 | } |
| 265 | |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 266 | const Key& key() const { return fKey; } |
| 267 | |
| 268 | ~GrAtlasTextBlob() { |
| 269 | for (int i = 0; i < fRunCount; i++) { |
| 270 | fRuns[i].~Run(); |
| 271 | } |
| 272 | } |
| 273 | |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 274 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 275 | // Internal test methods |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 276 | std::unique_ptr<GrDrawOp> test_makeOp(int glyphCount, uint16_t run, uint16_t subRun, |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 277 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
| 278 | const GrTextUtils::Paint&, const SkSurfaceProps&, |
| 279 | const GrDistanceFieldAdjustTable*, GrAtlasGlyphCache*, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 280 | GrTextUtils::Target*); |
joshualitt | bc81111 | 2016-02-11 12:42:02 -0800 | [diff] [blame] | 281 | |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 282 | private: |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 283 | GrAtlasTextBlob() |
| 284 | : fMaxMinScale(-SK_ScalarMax) |
| 285 | , fMinMaxScale(SK_ScalarMax) |
| 286 | , fTextType(0) {} |
| 287 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 288 | void appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph, |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 289 | SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 290 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 291 | inline void flushRun(GrTextUtils::Target*, const GrClip&, int run, const SkMatrix& viewMatrix, |
| 292 | SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, |
| 293 | const SkSurfaceProps& props, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 294 | const GrDistanceFieldAdjustTable* distanceAdjustTable, |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 295 | GrAtlasGlyphCache* cache); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 296 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 297 | void flushBigGlyphs(GrContext* context, GrTextUtils::Target*, const GrClip& clip, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 298 | const SkPaint& paint, const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 299 | const SkIRect& clipBounds); |
| 300 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 301 | void flushRunAsPaths(GrContext* context, GrTextUtils::Target*, const SkSurfaceProps& props, |
| 302 | const SkTextBlobRunIterator& it, const GrClip& clip, |
| 303 | const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter, |
| 304 | const SkMatrix& viewMatrix, const SkIRect& clipBounds, SkScalar x, |
| 305 | SkScalar y); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 306 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 307 | // This function will only be called when we are generating a blob from scratch. We record the |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 308 | // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to |
| 309 | // these numbers. When blobs are reused with new matrices, we need to return to model space so |
| 310 | // we can update the vertex bounds appropriately. |
| 311 | void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 312 | fInitialViewMatrix = viewMatrix; |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 313 | if (!viewMatrix.invert(&fInitialViewMatrixInverse)) { |
| 314 | fInitialViewMatrixInverse = SkMatrix::I(); |
| 315 | SkDebugf("Could not invert viewmatrix\n"); |
| 316 | } |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 317 | fInitialX = x; |
| 318 | fInitialY = y; |
| 319 | |
| 320 | // make sure all initial subruns have the correct VM and X/Y applied |
| 321 | for (int i = 0; i < fRunCount; i++) { |
| 322 | fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y); |
| 323 | } |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 324 | } |
| 325 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 326 | /* |
| 327 | * Each Run inside of the blob can have its texture coordinates regenerated if required. |
| 328 | * To determine if regeneration is necessary, fAtlasGeneration is used. If there have been |
| 329 | * any evictions inside of the atlas, then we will simply regenerate Runs. We could track |
| 330 | * this at a more fine grained level, but its not clear if this is worth it, as evictions |
| 331 | * should be fairly rare. |
| 332 | * |
| 333 | * One additional point, each run can contain glyphs with any of the three mask formats. |
| 334 | * We call these SubRuns. Because a subrun must be a contiguous range, we have to create |
| 335 | * a new subrun each time the mask format changes in a run. In theory, a run can have as |
| 336 | * many SubRuns as it has glyphs, ie if a run alternates between color emoji and A8. In |
| 337 | * practice, the vast majority of runs have only a single subrun. |
| 338 | * |
| 339 | * Finally, for runs where the entire thing is too large for the GrAtlasTextContext to |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 340 | * handle, we have a bit to mark the run as flushable via rendering as paths. It is worth |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 341 | * pointing. It would be a bit expensive to figure out ahead of time whether or not a run |
| 342 | * can flush in this manner, so we always allocate vertices for the run, regardless of |
| 343 | * whether or not it is too large. The benefit of this strategy is that we can always reuse |
| 344 | * a blob allocation regardless of viewmatrix changes. We could store positions for these |
| 345 | * glyphs. However, its not clear if this is a win because we'd still have to either go the |
| 346 | * glyph cache to get the path at flush time, or hold onto the path in the cache, which |
| 347 | * would greatly increase the memory of these cached items. |
| 348 | */ |
| 349 | struct Run { |
| 350 | Run() |
joshualitt | f9e658b | 2015-12-09 09:26:44 -0800 | [diff] [blame] | 351 | : fInitialized(false) |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 352 | , fDrawAsPaths(false) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 353 | // To ensure we always have one subrun, we push back a fresh run here |
| 354 | fSubRunInfo.push_back(); |
| 355 | } |
| 356 | struct SubRunInfo { |
| 357 | SubRunInfo() |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 358 | : fAtlasGeneration(GrDrawOpAtlas::kInvalidAtlasGeneration) |
| 359 | , fVertexStartIndex(0) |
| 360 | , fVertexEndIndex(0) |
| 361 | , fGlyphStartIndex(0) |
| 362 | , fGlyphEndIndex(0) |
| 363 | , fColor(GrColor_ILLEGAL) |
| 364 | , fMaskFormat(kA8_GrMaskFormat) |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 365 | , fFlags(0) { |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 366 | fVertexBounds.setLargestInverted(); |
| 367 | } |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 368 | SubRunInfo(const SubRunInfo& that) |
| 369 | : fBulkUseToken(that.fBulkUseToken) |
| 370 | , fStrike(SkSafeRef(that.fStrike.get())) |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 371 | , fCurrentViewMatrix(that.fCurrentViewMatrix) |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 372 | , fVertexBounds(that.fVertexBounds) |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 373 | , fAtlasGeneration(that.fAtlasGeneration) |
| 374 | , fVertexStartIndex(that.fVertexStartIndex) |
| 375 | , fVertexEndIndex(that.fVertexEndIndex) |
| 376 | , fGlyphStartIndex(that.fGlyphStartIndex) |
| 377 | , fGlyphEndIndex(that.fGlyphEndIndex) |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 378 | , fX(that.fX) |
| 379 | , fY(that.fY) |
joshualitt | f9e658b | 2015-12-09 09:26:44 -0800 | [diff] [blame] | 380 | , fColor(that.fColor) |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 381 | , fMaskFormat(that.fMaskFormat) |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 382 | , fFlags(that.fFlags) { |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 383 | } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 384 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 385 | // TODO when this object is more internal, drop the privacy |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 386 | void resetBulkUseToken() { fBulkUseToken.reset(); } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 387 | GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; } |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 388 | void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); } |
| 389 | GrAtlasTextStrike* strike() const { return fStrike.get(); } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 390 | |
| 391 | void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;} |
| 392 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
| 393 | |
| 394 | size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 395 | size_t vertexStartIndex() const { return fVertexStartIndex; } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 396 | size_t vertexEndIndex() const { return fVertexEndIndex; } |
| 397 | void appendVertices(size_t vertexStride) { |
| 398 | fVertexEndIndex += vertexStride * kVerticesPerGlyph; |
| 399 | } |
| 400 | |
| 401 | uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 402 | uint32_t glyphStartIndex() const { return fGlyphStartIndex; } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 403 | uint32_t glyphEndIndex() const { return fGlyphEndIndex; } |
| 404 | void glyphAppended() { fGlyphEndIndex++; } |
joshualitt | f9e658b | 2015-12-09 09:26:44 -0800 | [diff] [blame] | 405 | void setColor(GrColor color) { fColor = color; } |
| 406 | GrColor color() const { return fColor; } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 407 | void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; } |
| 408 | GrMaskFormat maskFormat() const { return fMaskFormat; } |
| 409 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 410 | void setAsSuccessor(const SubRunInfo& prev) { |
| 411 | fGlyphStartIndex = prev.glyphEndIndex(); |
| 412 | fGlyphEndIndex = prev.glyphEndIndex(); |
| 413 | |
| 414 | fVertexStartIndex = prev.vertexEndIndex(); |
| 415 | fVertexEndIndex = prev.vertexEndIndex(); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 416 | |
| 417 | // copy over viewmatrix settings |
| 418 | this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY); |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 419 | } |
| 420 | |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 421 | const SkRect& vertexBounds() const { return fVertexBounds; } |
| 422 | void joinGlyphBounds(const SkRect& glyphBounds) { |
| 423 | fVertexBounds.joinNonEmptyArg(glyphBounds); |
| 424 | } |
| 425 | |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 426 | void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) { |
| 427 | fCurrentViewMatrix = viewMatrix; |
| 428 | fX = x; |
| 429 | fY = y; |
| 430 | } |
| 431 | |
| 432 | // This function assumes the translation will be applied before it is called again |
| 433 | void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y, |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 434 | SkScalar* transX, SkScalar* transY); |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 435 | |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 436 | // df properties |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 437 | void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; } |
| 438 | bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); } |
| 439 | void setUseLCDText(bool useLCDText) { |
| 440 | fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag; |
| 441 | } |
| 442 | bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); } |
| 443 | void setAntiAliased(bool antiAliased) { |
| 444 | fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag; |
| 445 | } |
| 446 | bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 447 | void setHasWCoord(bool hasW) { |
| 448 | fFlags = hasW ? (fFlags | kHasWCoord_Flag) : fFlags & ~kHasWCoord_Flag; |
| 449 | } |
| 450 | bool hasWCoord() const { return SkToBool(fFlags & kHasWCoord_Flag); } |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 451 | |
| 452 | private: |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 453 | enum Flag { |
| 454 | kDrawAsSDF_Flag = 0x1, |
| 455 | kUseLCDText_Flag = 0x2, |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame^] | 456 | kAntiAliased_Flag = 0x4, |
| 457 | kHasWCoord_Flag = 0x8 |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 458 | }; |
| 459 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 460 | GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 461 | sk_sp<GrAtlasTextStrike> fStrike; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 462 | SkMatrix fCurrentViewMatrix; |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 463 | SkRect fVertexBounds; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 464 | uint64_t fAtlasGeneration; |
| 465 | size_t fVertexStartIndex; |
| 466 | size_t fVertexEndIndex; |
| 467 | uint32_t fGlyphStartIndex; |
| 468 | uint32_t fGlyphEndIndex; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 469 | SkScalar fX; |
| 470 | SkScalar fY; |
joshualitt | f9e658b | 2015-12-09 09:26:44 -0800 | [diff] [blame] | 471 | GrColor fColor; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 472 | GrMaskFormat fMaskFormat; |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 473 | uint32_t fFlags; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 474 | }; // SubRunInfo |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 475 | |
| 476 | SubRunInfo& push_back() { |
| 477 | // Forward glyph / vertex information to seed the new sub run |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 478 | SubRunInfo& newSubRun = fSubRunInfo.push_back(); |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 479 | const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1); |
joshualitt | e43e3bd | 2015-07-29 11:10:38 -0700 | [diff] [blame] | 480 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 481 | newSubRun.setAsSuccessor(prevSubRun); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 482 | return newSubRun; |
| 483 | } |
| 484 | static const int kMinSubRuns = 1; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 485 | sk_sp<SkTypeface> fTypeface; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 486 | SkSTArray<kMinSubRuns, SubRunInfo> fSubRunInfo; |
| 487 | SkAutoDescriptor fDescriptor; |
bsalomon | 8b6fa5e | 2016-05-19 16:23:47 -0700 | [diff] [blame] | 488 | |
| 489 | // Effects from the paint that are used to build a SkScalerContext. |
| 490 | sk_sp<SkPathEffect> fPathEffect; |
| 491 | sk_sp<SkRasterizer> fRasterizer; |
| 492 | sk_sp<SkMaskFilter> fMaskFilter; |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 493 | |
| 494 | // Distance field text cannot draw coloremoji, and so has to fall back. However, |
| 495 | // though the distance field text and the coloremoji may share the same run, they |
| 496 | // will have different descriptors. If fOverrideDescriptor is non-nullptr, then it |
| 497 | // will be used in place of the run's descriptor to regen texture coords |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 498 | std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 499 | bool fInitialized; |
| 500 | bool fDrawAsPaths; |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 501 | }; // Run |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 502 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 503 | inline std::unique_ptr<GrAtlasTextOp> makeOp( |
Jim Van Verth | 56c3714 | 2017-10-31 14:44:25 -0400 | [diff] [blame] | 504 | const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 505 | const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect, |
| 506 | const GrTextUtils::Paint& paint, const SkSurfaceProps& props, |
| 507 | const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache, |
| 508 | GrTextUtils::Target*); |
joshualitt | 323c2eb | 2016-01-20 06:48:47 -0800 | [diff] [blame] | 509 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 510 | struct BigGlyph { |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 511 | BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool treatAsBMP) |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 512 | : fPath(path) |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 513 | , fScale(scale) |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 514 | , fX(vx) |
| 515 | , fY(vy) |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 516 | , fTreatAsBMP(treatAsBMP) {} |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 517 | SkPath fPath; |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 518 | SkScalar fScale; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 519 | SkScalar fX; |
| 520 | SkScalar fY; |
Jim Van Verth | 08576e6 | 2016-11-16 10:15:23 -0500 | [diff] [blame] | 521 | bool fTreatAsBMP; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 524 | struct StrokeInfo { |
| 525 | SkScalar fFrameWidth; |
| 526 | SkScalar fMiterLimit; |
| 527 | SkPaint::Join fJoin; |
| 528 | }; |
| 529 | |
| 530 | enum TextType { |
| 531 | kHasDistanceField_TextType = 0x1, |
| 532 | kHasBitmap_TextType = 0x2, |
| 533 | }; |
| 534 | |
| 535 | // all glyph / vertex offsets are into these pools. |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 536 | char* fVertices; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 537 | GrGlyph** fGlyphs; |
| 538 | Run* fRuns; |
| 539 | GrMemoryPool* fPool; |
| 540 | SkMaskFilter::BlurRec fBlurRec; |
| 541 | StrokeInfo fStrokeInfo; |
| 542 | SkTArray<BigGlyph> fBigGlyphs; |
| 543 | Key fKey; |
joshualitt | 8e0ef29 | 2016-02-19 14:13:03 -0800 | [diff] [blame] | 544 | SkMatrix fInitialViewMatrix; |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 545 | SkMatrix fInitialViewMatrixInverse; |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 546 | size_t fSize; |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 547 | SkColor fLuminanceColor; |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 548 | SkScalar fInitialX; |
| 549 | SkScalar fInitialY; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 550 | |
| 551 | // We can reuse distance field text, but only if the new viewmatrix would not result in |
| 552 | // a mip change. Because there can be multiple runs in a blob, we track the overall |
| 553 | // maximum minimum scale, and minimum maximum scale, we can support before we need to regen |
| 554 | SkScalar fMaxMinScale; |
| 555 | SkScalar fMinMaxScale; |
| 556 | int fRunCount; |
| 557 | uint8_t fTextType; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 558 | }; |
| 559 | |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 560 | /** |
| 561 | * Used to produce vertices for a subrun of a blob. The vertices are cached in the blob itself. |
| 562 | * This is invoked each time a sub run is drawn. It regenerates the vertex data as required either |
| 563 | * because of changes to the atlas or because of different draw parameters (e.g. color change). In |
| 564 | * rare cases the draw may have to interrupted and flushed in the middle of the sub run in order to |
| 565 | * free up atlas space. Thus, this generator is stateful and should be invoked in a loop until the |
| 566 | * entire sub run has been completed. |
| 567 | */ |
| 568 | class GrAtlasTextBlob::VertexRegenerator { |
| 569 | public: |
| 570 | /** |
| 571 | * Consecutive VertexRegenerators often use the same SkGlyphCache. If the same instance of |
| 572 | * SkAutoGlyphCache is reused then it can save the cost of multiple detach/attach operations of |
| 573 | * SkGlyphCache. |
| 574 | */ |
| 575 | VertexRegenerator(GrAtlasTextBlob* blob, int runIdx, int subRunIdx, const SkMatrix& viewMatrix, |
| 576 | SkScalar x, SkScalar y, GrColor color, GrDeferredUploadTarget*, |
Brian Salomon | deb53cc | 2017-11-08 13:50:53 -0500 | [diff] [blame] | 577 | GrAtlasGlyphCache*, SkAutoGlyphCache*); |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 578 | |
| 579 | struct Result { |
| 580 | /** |
| 581 | * Was regenerate() able to draw all the glyphs from the sub run? If not flush all glyph |
| 582 | * draws and call regenerate() again. |
| 583 | */ |
| 584 | bool fFinished = true; |
| 585 | |
| 586 | /** |
| 587 | * How many glyphs were regenerated. Will be equal to the sub run's glyph count if |
| 588 | * fType is kFinished. |
| 589 | */ |
| 590 | int fGlyphsRegenerated = 0; |
| 591 | |
| 592 | /** |
| 593 | * Pointer where the caller finds the first regenerated vertex. |
| 594 | */ |
| 595 | const char* fFirstVertex; |
| 596 | }; |
| 597 | |
| 598 | Result regenerate(); |
| 599 | |
| 600 | private: |
| 601 | template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs> |
| 602 | Result doRegen(); |
| 603 | |
| 604 | const SkMatrix& fViewMatrix; |
| 605 | GrAtlasTextBlob* fBlob; |
| 606 | GrDeferredUploadTarget* fUploadTarget; |
| 607 | GrAtlasGlyphCache* fGlyphCache; |
| 608 | SkAutoGlyphCache* fLazyCache; |
| 609 | Run* fRun; |
| 610 | Run::SubRunInfo* fSubRun; |
Brian Salomon | 18923f9 | 2017-11-06 16:26:02 -0500 | [diff] [blame] | 611 | GrColor fColor; |
| 612 | SkScalar fTransX; |
| 613 | SkScalar fTransY; |
| 614 | |
| 615 | uint32_t fRegenFlags = 0; |
| 616 | int fCurrGlyph = 0; |
| 617 | bool fBrokenRun = false; |
| 618 | }; |
| 619 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 620 | #endif |