joshualitt | b7133be | 2015-04-08 09:08:31 -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 | #include "GrTextBlobCache.h" |
| 9 | |
| 10 | static const int kVerticesPerGlyph = 4; |
| 11 | |
| 12 | GrTextBlobCache::~GrTextBlobCache() { |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 13 | this->freeAll(); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | } |
| 15 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 16 | GrAtlasTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount, size_t maxVASize) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 17 | // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array, |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 18 | // and size for the glyphIds array. |
| 19 | size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 20 | size_t size = sizeof(GrAtlasTextBlob) + |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 21 | verticesCount + |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 22 | glyphCount * sizeof(GrGlyph**) + |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 23 | sizeof(GrAtlasTextBlob::Run) * runCount; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 24 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 25 | void* allocation = fPool.allocate(size); |
| 26 | #ifdef CACHE_SANITY_CHECK |
| 27 | sk_bzero(allocation, size); |
| 28 | #endif |
| 29 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 30 | GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob; |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 31 | #ifdef CACHE_SANITY_CHECK |
| 32 | cacheBlob->fSize = size; |
| 33 | #endif |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 34 | |
| 35 | // setup offsets for vertices / glyphs |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 36 | cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 37 | cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 38 | cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 39 | |
| 40 | // Initialize runs |
| 41 | for (int i = 0; i < runCount; i++) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 42 | new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 43 | } |
| 44 | cacheBlob->fRunCount = runCount; |
| 45 | cacheBlob->fPool = &fPool; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 46 | return cacheBlob; |
| 47 | } |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 48 | |
| 49 | void GrTextBlobCache::freeAll() { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 50 | SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key>::Iter iter(&fCache); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 51 | while (!iter.done()) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 52 | GrAtlasTextBlob* blob = &(*iter); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 53 | fBlobList.remove(blob); |
| 54 | blob->unref(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 55 | ++iter; |
| 56 | } |
| 57 | fCache.rewind(); |
joshualitt | 20ccd40 | 2016-01-05 08:56:56 -0800 | [diff] [blame^] | 58 | |
| 59 | // There should be no allocations in the memory pool at this point |
| 60 | SkASSERT(fPool.isEmpty()); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 61 | } |