1 /*
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 
10 static const int kVerticesPerGlyph = 4;
11 
~GrTextBlobCache()12 GrTextBlobCache::~GrTextBlobCache() {
13     this->freeAll();
14 }
15 
createBlob(int glyphCount,int runCount,size_t maxVASize)16 GrAtlasTextContext::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**) +
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->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
31     cacheBlob->fRuns = reinterpret_cast<BitmapTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
32 
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;
39     return cacheBlob;
40 }
41 
freeAll()42 void GrTextBlobCache::freeAll() {
43     SkTDynamicHash<BitmapTextBlob, BitmapTextBlob::Key>::Iter iter(&fCache);
44     while (!iter.done()) {
45         (&(*iter))->unref();
46         ++iter;
47     }
48     fCache.rewind();
49 }
50