OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrTextBlobCache.h" | 8 #include "GrTextBlobCache.h" |
9 | 9 |
10 static const int kVerticesPerGlyph = 4; | 10 static const int kVerticesPerGlyph = 4; |
11 | 11 |
12 GrTextBlobCache::~GrTextBlobCache() { | 12 GrTextBlobCache::~GrTextBlobCache() { |
13 this->freeAll(); | 13 this->freeAll(); |
14 } | 14 } |
15 | 15 |
16 GrAtlasTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount, size_
t maxVASize) { | 16 GrAtlasTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount, size_
t maxVASize) { |
17 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertic
es array, | 17 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertic
es array, |
18 // and size for the glyphIds array. | 18 // and size for the glyphIds array. |
19 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize; | 19 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize; |
20 size_t size = sizeof(GrAtlasTextBlob) + | 20 size_t size = sizeof(GrAtlasTextBlob) + |
21 verticesCount + | 21 verticesCount + |
22 glyphCount * sizeof(GrGlyph**) + | 22 glyphCount * sizeof(GrGlyph**) + |
23 sizeof(GrAtlasTextBlob::Run) * runCount; | 23 sizeof(GrAtlasTextBlob::Run) * runCount; |
24 | 24 |
25 void* allocation = fPool.allocate(size); | 25 void* allocation = fPool.allocate(size); |
26 #ifdef CACHE_SANITY_CHECK | 26 if (CACHE_SANITY_CHECK) { |
27 sk_bzero(allocation, size); | 27 sk_bzero(allocation, size); |
28 #endif | 28 } |
29 | 29 |
30 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob; | 30 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob; |
31 #ifdef CACHE_SANITY_CHECK | |
32 cacheBlob->fSize = size; | 31 cacheBlob->fSize = size; |
33 #endif | |
34 | 32 |
35 // setup offsets for vertices / glyphs | 33 // setup offsets for vertices / glyphs |
36 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned c
har*>(cacheBlob); | 34 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned c
har*>(cacheBlob); |
37 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + vert
icesCount); | 35 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + vert
icesCount); |
38 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyph
s + glyphCount); | 36 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyph
s + glyphCount); |
39 | 37 |
40 // Initialize runs | 38 // Initialize runs |
41 for (int i = 0; i < runCount; i++) { | 39 for (int i = 0; i < runCount; i++) { |
42 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run; | 40 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run; |
43 } | 41 } |
44 cacheBlob->fRunCount = runCount; | 42 cacheBlob->fRunCount = runCount; |
45 cacheBlob->fPool = &fPool; | 43 cacheBlob->fPool = &fPool; |
46 return cacheBlob; | 44 return cacheBlob; |
47 } | 45 } |
48 | 46 |
49 void GrTextBlobCache::freeAll() { | 47 void GrTextBlobCache::freeAll() { |
50 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key>::Iter iter(&fCache); | 48 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key>::Iter iter(&fCache); |
51 while (!iter.done()) { | 49 while (!iter.done()) { |
52 GrAtlasTextBlob* blob = &(*iter); | 50 GrAtlasTextBlob* blob = &(*iter); |
53 fBlobList.remove(blob); | 51 fBlobList.remove(blob); |
54 blob->unref(); | 52 blob->unref(); |
55 ++iter; | 53 ++iter; |
56 } | 54 } |
57 fCache.rewind(); | 55 fCache.rewind(); |
58 | 56 |
59 // There should be no allocations in the memory pool at this point | 57 // There should be no allocations in the memory pool at this point |
60 SkASSERT(fPool.isEmpty()); | 58 SkASSERT(fPool.isEmpty()); |
61 } | 59 } |
OLD | NEW |