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