blob: 6f171779fc3cdbbb390f18aed573c88bed0ed082 [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
16GrAtlasTextContext::BitmapTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount,
17 size_t maxVASize) {
18 // We allocate size for the BitmapTextBlob itself, plus size for the vertices array,
19 // and size for the glyphIds array.
20 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize;
21 size_t size = sizeof(BitmapTextBlob) +
22 verticesCount +
joshualittae32c102015-04-21 09:37:57 -070023 glyphCount * sizeof(GrGlyph**) +
joshualittb7133be2015-04-08 09:08:31 -070024 sizeof(BitmapTextBlob::Run) * runCount;
25
26 BitmapTextBlob* cacheBlob = SkNEW_PLACEMENT(fPool.allocate(size), BitmapTextBlob);
27
28 // setup offsets for vertices / glyphs
29 cacheBlob->fVertices = sizeof(BitmapTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
joshualittae32c102015-04-21 09:37:57 -070030 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
31 cacheBlob->fRuns = reinterpret_cast<BitmapTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
joshualittb7133be2015-04-08 09:08:31 -070032
33 // Initialize runs
34 for (int i = 0; i < runCount; i++) {
35 SkNEW_PLACEMENT(&cacheBlob->fRuns[i], BitmapTextBlob::Run);
36 }
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() {
43 SkTDynamicHash<BitmapTextBlob, BitmapTextBlob::Key>::Iter iter(&fCache);
44 while (!iter.done()) {
joshualitt7a9c45c2015-05-26 12:32:23 -070045 BitmapTextBlob* blob = &(*iter);
46 fBlobList.remove(blob);
47 blob->unref();
joshualitt26ffc002015-04-16 11:24:04 -070048 ++iter;
49 }
50 fCache.rewind();
51}