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