blob: a5ef07c9f6c6c22a5d141abc6d8e704b7452219b [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() {
13 SkTDynamicHash<BitmapTextBlob, uint32_t>::Iter iter(&fCache);
14 while (!iter.done()) {
15 (&(*iter))->unref();
16 ++iter;
17 }
18}
19
20GrAtlasTextContext::BitmapTextBlob* GrTextBlobCache::createBlob(int glyphCount, int runCount,
21 size_t maxVASize) {
22 // We allocate size for the BitmapTextBlob itself, plus size for the vertices array,
23 // and size for the glyphIds array.
24 size_t verticesCount = glyphCount * kVerticesPerGlyph * maxVASize;
25 size_t size = sizeof(BitmapTextBlob) +
26 verticesCount +
27 glyphCount * sizeof(GrGlyph::PackedID) +
28 sizeof(BitmapTextBlob::Run) * runCount;
29
30 BitmapTextBlob* cacheBlob = SkNEW_PLACEMENT(fPool.allocate(size), BitmapTextBlob);
31
32 // setup offsets for vertices / glyphs
33 cacheBlob->fVertices = sizeof(BitmapTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
34 cacheBlob->fGlyphIDs =
35 reinterpret_cast<GrGlyph::PackedID*>(cacheBlob->fVertices + verticesCount);
36 cacheBlob->fRuns = reinterpret_cast<BitmapTextBlob::Run*>(cacheBlob->fGlyphIDs + glyphCount);
37
38 // Initialize runs
39 for (int i = 0; i < runCount; i++) {
40 SkNEW_PLACEMENT(&cacheBlob->fRuns[i], BitmapTextBlob::Run);
41 }
42 cacheBlob->fRunCount = runCount;
43 cacheBlob->fPool = &fPool;
joshualitt2a0e9f32015-04-13 06:12:21 -070044
45#ifdef SK_DEBUG
46 cacheBlob->fTotalXError = 0;
47 cacheBlob->fTotalYError = 0;
48#endif
joshualittb7133be2015-04-08 09:08:31 -070049 return cacheBlob;
50}