joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 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 | |
| 12 | GrTextBlobCache::~GrTextBlobCache() { |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame^] | 13 | this->freeAll(); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 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::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; |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 40 | |
| 41 | #ifdef SK_DEBUG |
| 42 | cacheBlob->fTotalXError = 0; |
| 43 | cacheBlob->fTotalYError = 0; |
| 44 | #endif |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 45 | return cacheBlob; |
| 46 | } |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame^] | 47 | |
| 48 | void GrTextBlobCache::freeAll() { |
| 49 | SkTDynamicHash<BitmapTextBlob, BitmapTextBlob::Key>::Iter iter(&fCache); |
| 50 | while (!iter.done()) { |
| 51 | (&(*iter))->unref(); |
| 52 | ++iter; |
| 53 | } |
| 54 | fCache.rewind(); |
| 55 | } |