AssetAtlas.cpp revision 877cfe0e32a845d5a58252b8a6e1f54f95b4379c
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AssetAtlas.h"
18
19#include <GLES2/gl2ext.h>
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Lifecycle
26///////////////////////////////////////////////////////////////////////////////
27
28void AssetAtlas::init(sp<GraphicBuffer> buffer, int* map, int count) {
29    if (mImage) {
30        return;
31    }
32
33    mImage = new Image(buffer);
34    mTexture = mImage->getTexture();
35
36    if (mTexture) {
37        mWidth = buffer->getWidth();
38        mHeight = buffer->getHeight();
39
40        createEntries(map, count);
41    } else {
42        delete mImage;
43    }
44}
45
46void AssetAtlas::terminate() {
47    if (mImage) {
48        delete mImage;
49
50        for (size_t i = 0; i < mEntries.size(); i++) {
51            delete mEntries.valueAt(i);
52        }
53        mEntries.clear();
54
55        mWidth = mHeight = 0;
56    }
57}
58
59///////////////////////////////////////////////////////////////////////////////
60// Entries
61///////////////////////////////////////////////////////////////////////////////
62
63AssetAtlas::Entry* AssetAtlas::getEntry(SkBitmap* const bitmap) const {
64    ssize_t index = mEntries.indexOfKey(bitmap);
65    return index >= 0 ? mEntries.valueAt(index) : NULL;
66}
67
68Texture* AssetAtlas::getEntryTexture(SkBitmap* const bitmap) const {
69    ssize_t index = mEntries.indexOfKey(bitmap);
70    return index >= 0 ? &mEntries.valueAt(index)->texture : NULL;
71}
72
73/**
74 * TODO: This method does not take the rotation flag into account
75 */
76void AssetAtlas::createEntries(int* map, int count) {
77    for (int i = 0; i < count; ) {
78        SkBitmap* bitmap = (SkBitmap*) map[i++];
79        int x = map[i++];
80        int y = map[i++];
81        bool rotated = map[i++] > 0;
82
83        // Bitmaps should never be null, we're just extra paranoid
84        if (!bitmap) continue;
85
86        const UvMapper mapper(
87                x / (float) mWidth, (x + bitmap->width()) / (float) mWidth,
88                y / (float) mHeight, (y + bitmap->height()) / (float) mHeight);
89
90        Entry* entry = new Entry(bitmap, x, y, rotated, mapper, *this);
91        entry->texture.id = mTexture;
92        entry->texture.blend = !bitmap->isOpaque();
93        entry->texture.width = bitmap->width();
94        entry->texture.height = bitmap->height();
95        entry->texture.uvMapper = &entry->uvMapper;
96
97        mEntries.add(entry->bitmap, entry);
98    }
99}
100
101}; // namespace uirenderer
102}; // namespace android
103