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