blob: d32e24bc1a3ddda0ce3828b003b6a6eef28ac353 [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 +
23 glyphCount * sizeof(GrGlyph::PackedID) +
24 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);
30 cacheBlob->fGlyphIDs =
31 reinterpret_cast<GrGlyph::PackedID*>(cacheBlob->fVertices + verticesCount);
32 cacheBlob->fRuns = reinterpret_cast<BitmapTextBlob::Run*>(cacheBlob->fGlyphIDs + glyphCount);
33
34 // Initialize runs
35 for (int i = 0; i < runCount; i++) {
36 SkNEW_PLACEMENT(&cacheBlob->fRuns[i], BitmapTextBlob::Run);
37 }
38 cacheBlob->fRunCount = runCount;
39 cacheBlob->fPool = &fPool;
joshualitt2a0e9f32015-04-13 06:12:21 -070040
41#ifdef SK_DEBUG
42 cacheBlob->fTotalXError = 0;
43 cacheBlob->fTotalYError = 0;
44#endif
joshualittb7133be2015-04-08 09:08:31 -070045 return cacheBlob;
46}
joshualitt26ffc002015-04-16 11:24:04 -070047
48void GrTextBlobCache::freeAll() {
49 SkTDynamicHash<BitmapTextBlob, BitmapTextBlob::Key>::Iter iter(&fCache);
50 while (!iter.done()) {
51 (&(*iter))->unref();
52 ++iter;
53 }
54 fCache.rewind();
55}