blob: 685284bee5c02fd062bb84ab29b4ed85feabf8b9 [file] [log] [blame]
joshualittb7133be2015-04-08 09:08:31 -07001/*
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
10static const int kVerticesPerGlyph = 4;
11
12GrTextBlobCache::~GrTextBlobCache() {
joshualitt26ffc002015-04-16 11:24:04 -070013 this->freeAll();
joshualittb7133be2015-04-08 09:08:31 -070014}
15
joshualitt259fbf12015-07-21 11:39:34 -070016GrAtlasTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount, size_t maxVASize) {
joshualitt374b2f72015-07-21 08:05:03 -070017 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
joshualittb7133be2015-04-08 09:08:31 -070018 // and size for the glyphIds array.
19 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize;
joshualitt374b2f72015-07-21 08:05:03 -070020 size_t size = sizeof(GrAtlasTextBlob) +
joshualittb7133be2015-04-08 09:08:31 -070021 verticesCount +
joshualittae32c102015-04-21 09:37:57 -070022 glyphCount * sizeof(GrGlyph**) +
joshualitt374b2f72015-07-21 08:05:03 -070023 sizeof(GrAtlasTextBlob::Run) * runCount;
joshualittb7133be2015-04-08 09:08:31 -070024
joshualitt259fbf12015-07-21 11:39:34 -070025 void* allocation = fPool.allocate(size);
26#ifdef CACHE_SANITY_CHECK
27 sk_bzero(allocation, size);
28#endif
29
halcanary385fe4d2015-08-26 13:07:48 -070030 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob;
joshualitt259fbf12015-07-21 11:39:34 -070031#ifdef CACHE_SANITY_CHECK
32 cacheBlob->fSize = size;
33#endif
joshualittb7133be2015-04-08 09:08:31 -070034
35 // setup offsets for vertices / glyphs
joshualitt374b2f72015-07-21 08:05:03 -070036 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
joshualittae32c102015-04-21 09:37:57 -070037 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
joshualitt374b2f72015-07-21 08:05:03 -070038 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualittb7133be2015-04-08 09:08:31 -070039
40 // Initialize runs
41 for (int i = 0; i < runCount; i++) {
halcanary385fe4d2015-08-26 13:07:48 -070042 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
joshualittb7133be2015-04-08 09:08:31 -070043 }
44 cacheBlob->fRunCount = runCount;
45 cacheBlob->fPool = &fPool;
joshualittb7133be2015-04-08 09:08:31 -070046 return cacheBlob;
47}
joshualitt26ffc002015-04-16 11:24:04 -070048
49void GrTextBlobCache::freeAll() {
joshualitt374b2f72015-07-21 08:05:03 -070050 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key>::Iter iter(&fCache);
joshualitt26ffc002015-04-16 11:24:04 -070051 while (!iter.done()) {
joshualitt374b2f72015-07-21 08:05:03 -070052 GrAtlasTextBlob* blob = &(*iter);
joshualitt7a9c45c2015-05-26 12:32:23 -070053 fBlobList.remove(blob);
54 blob->unref();
joshualitt26ffc002015-04-16 11:24:04 -070055 ++iter;
56 }
57 fCache.rewind();
joshualitt20ccd402016-01-05 08:56:56 -080058
59 // There should be no allocations in the memory pool at this point
60 SkASSERT(fPool.isEmpty());
joshualitt26ffc002015-04-16 11:24:04 -070061}