blob: b1a13a866f2d240de902617c8b24790f616269f6 [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
joshualitt374b2f72015-07-21 08:05:03 -070016GrAtlasTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount,
joshualittb7133be2015-04-08 09:08:31 -070017 size_t maxVASize) {
joshualitt374b2f72015-07-21 08:05:03 -070018 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
joshualittb7133be2015-04-08 09:08:31 -070019 // and size for the glyphIds array.
20 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize;
joshualitt374b2f72015-07-21 08:05:03 -070021 size_t size = sizeof(GrAtlasTextBlob) +
joshualittb7133be2015-04-08 09:08:31 -070022 verticesCount +
joshualittae32c102015-04-21 09:37:57 -070023 glyphCount * sizeof(GrGlyph**) +
joshualitt374b2f72015-07-21 08:05:03 -070024 sizeof(GrAtlasTextBlob::Run) * runCount;
joshualittb7133be2015-04-08 09:08:31 -070025
joshualitt374b2f72015-07-21 08:05:03 -070026 GrAtlasTextBlob* cacheBlob = SkNEW_PLACEMENT(fPool.allocate(size), GrAtlasTextBlob);
joshualittb7133be2015-04-08 09:08:31 -070027
28 // setup offsets for vertices / glyphs
joshualitt374b2f72015-07-21 08:05:03 -070029 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
joshualittae32c102015-04-21 09:37:57 -070030 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
joshualitt374b2f72015-07-21 08:05:03 -070031 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualittb7133be2015-04-08 09:08:31 -070032
33 // Initialize runs
34 for (int i = 0; i < runCount; i++) {
joshualitt374b2f72015-07-21 08:05:03 -070035 SkNEW_PLACEMENT(&cacheBlob->fRuns[i], GrAtlasTextBlob::Run);
joshualittb7133be2015-04-08 09:08:31 -070036 }
37 cacheBlob->fRunCount = runCount;
38 cacheBlob->fPool = &fPool;
joshualittb7133be2015-04-08 09:08:31 -070039 return cacheBlob;
40}
joshualitt26ffc002015-04-16 11:24:04 -070041
42void GrTextBlobCache::freeAll() {
joshualitt374b2f72015-07-21 08:05:03 -070043 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key>::Iter iter(&fCache);
joshualitt26ffc002015-04-16 11:24:04 -070044 while (!iter.done()) {
joshualitt374b2f72015-07-21 08:05:03 -070045 GrAtlasTextBlob* blob = &(*iter);
joshualitt7a9c45c2015-05-26 12:32:23 -070046 fBlobList.remove(blob);
47 blob->unref();
joshualitt26ffc002015-04-16 11:24:04 -070048 ++iter;
49 }
50 fCache.rewind();
51}