HbFontCache.cpp 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Minikin"

19
#include "HbFontCache.h"
20

21
#include <cutils/log.h>
22
#include <hb.h>
23
#include <hb-ot.h>
24 25 26 27 28 29 30
#include <utils/LruCache.h>

#include <minikin/MinikinFont.h>
#include "MinikinInternal.h"

namespace android {

31
static hb_blob_t* referenceTable(hb_face_t* /* face */, hb_tag_t tag, void* userData) {
32
    MinikinFont* font = reinterpret_cast<MinikinFont*>(userData);
R
Raph Levien 已提交
33 34 35 36 37
    MinikinDestroyFunc destroy = 0;
    size_t size = 0;
    const void* buffer = font->GetTable(tag, &size, &destroy);
    if (buffer == nullptr) {
        return nullptr;
38 39
    }
#ifdef VERBOSE_DEBUG
R
Raph Levien 已提交
40 41
    ALOGD("referenceTable %c%c%c%c length=%zd",
        (tag >>24)&0xff, (tag>>16)&0xff, (tag>>8)&0xff, tag&0xff, size);
42
#endif
R
Raph Levien 已提交
43 44
    return hb_blob_create(reinterpret_cast<const char*>(buffer), size,
            HB_MEMORY_MODE_READONLY, const_cast<void*>(buffer), destroy);
45 46
}

47
class HbFontCache : private OnEntryRemoved<int32_t, hb_font_t*> {
48
public:
49
    HbFontCache() : mCache(kMaxEntries) {
50 51 52 53
        mCache.setOnEntryRemovedListener(this);
    }

    // callback for OnEntryRemoved
54 55
    void operator()(int32_t& /* key */, hb_font_t*& value) {
        hb_font_destroy(value);
56 57
    }

58
    hb_font_t* get(int32_t fontId) {
59 60 61
        return mCache.get(fontId);
    }

62 63
    void put(int32_t fontId, hb_font_t* font) {
        mCache.put(fontId, font);
64 65 66 67 68 69
    }

    void clear() {
        mCache.clear();
    }

70 71 72 73
    void remove(int32_t fontId) {
        mCache.remove(fontId);
    }

74 75 76
private:
    static const size_t kMaxEntries = 100;

77
    LruCache<int32_t, hb_font_t*> mCache;
78 79
};

80
HbFontCache* getFontCacheLocked() {
81
    assertMinikinLocked();
82
    static HbFontCache* cache = nullptr;
83
    if (cache == nullptr) {
84
        cache = new HbFontCache();
85 86 87 88
    }
    return cache;
}

89
void purgeHbFontCacheLocked() {
90
    assertMinikinLocked();
91
    getFontCacheLocked()->clear();
92 93
}

94 95 96 97 98 99
void purgeHbFont(const MinikinFont* minikinFont) {
    AutoMutex _l(gMinikinLock);
    const int32_t fontId = minikinFont->GetUniqueId();
    getFontCacheLocked()->remove(fontId);
}

R
Raph Levien 已提交
100 101
// Returns a new reference to a hb_font_t object, caller is
// responsible for calling hb_font_destroy() on it.
102
hb_font_t* getHbFontLocked(MinikinFont* minikinFont) {
103
    assertMinikinLocked();
R
Raph Levien 已提交
104
    // TODO: get rid of nullFaceFont
105
    static hb_font_t* nullFaceFont = nullptr;
106
    if (minikinFont == nullptr) {
107 108 109
        if (nullFaceFont == nullptr) {
            nullFaceFont = hb_font_create(nullptr);
        }
R
Raph Levien 已提交
110
        return hb_font_reference(nullFaceFont);
111 112
    }

113
    HbFontCache* fontCache = getFontCacheLocked();
114
    const int32_t fontId = minikinFont->GetUniqueId();
115 116
    hb_font_t* font = fontCache->get(fontId);
    if (font != nullptr) {
R
Raph Levien 已提交
117
        return hb_font_reference(font);
118 119
    }

R
Raph Levien 已提交
120 121 122 123 124 125 126 127 128 129 130
    hb_face_t* face;
    const void* buf = minikinFont->GetFontData();
    if (buf == nullptr) {
        face = hb_face_create_for_tables(referenceTable, minikinFont, nullptr);
    } else {
        size_t size = minikinFont->GetFontSize();
        hb_blob_t* blob = hb_blob_create(reinterpret_cast<const char*>(buf), size,
            HB_MEMORY_MODE_READONLY, nullptr, nullptr);
        face = hb_face_create(blob, minikinFont->GetFontIndex());
        hb_blob_destroy(blob);
    }
131 132 133 134 135 136 137 138 139 140
    hb_font_t* parent_font = hb_font_create(face);
    hb_ot_font_set_funcs(parent_font);

    unsigned int upem = hb_face_get_upem(face);
    hb_font_set_scale(parent_font, upem, upem);

    font = hb_font_create_sub_font(parent_font);
    hb_font_destroy(parent_font);
    hb_face_destroy(face);
    fontCache->put(fontId, font);
R
Raph Levien 已提交
141
    return hb_font_reference(font);
142 143 144
}

}  // namespace android