AssetAtlas.cpp revision fbc8df03e498baf47ff1a5e05e182f1bcd60c770
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#define LOG_TAG "OpenGLRenderer"
18
19#include "AssetAtlas.h"
20#include "Caches.h"
21
22#include <GLES2/gl2ext.h>
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Lifecycle
29///////////////////////////////////////////////////////////////////////////////
30
31void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
32    if (mImage) {
33        return;
34    }
35
36    ATRACE_NAME("AssetAtlas::init");
37
38    mImage = new Image(buffer);
39
40    if (mImage->getTexture()) {
41        Caches& caches = Caches::getInstance();
42
43        mTexture = new Texture(caches);
44        mTexture->id = mImage->getTexture();
45        mTexture->width = buffer->getWidth();
46        mTexture->height = buffer->getHeight();
47
48        createEntries(caches, map, count);
49    } else {
50        ALOGW("Could not create atlas image");
51
52        delete mImage;
53        mImage = NULL;
54        mTexture = NULL;
55    }
56
57    mGenerationId++;
58}
59
60void AssetAtlas::terminate() {
61    if (mImage) {
62        delete mImage;
63        mImage = NULL;
64
65        delete mTexture;
66        mTexture = NULL;
67
68        for (size_t i = 0; i < mEntries.size(); i++) {
69            delete mEntries.valueAt(i);
70        }
71        mEntries.clear();
72    }
73}
74
75///////////////////////////////////////////////////////////////////////////////
76// Entries
77///////////////////////////////////////////////////////////////////////////////
78
79AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
80    ssize_t index = mEntries.indexOfKey(bitmap);
81    return index >= 0 ? mEntries.valueAt(index) : NULL;
82}
83
84Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
85    ssize_t index = mEntries.indexOfKey(bitmap);
86    return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
87}
88
89/**
90 * Delegates changes to wrapping and filtering to the base atlas texture
91 * instead of applying the changes to the virtual textures.
92 */
93struct DelegateTexture: public Texture {
94    DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
95
96    virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
97            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
98        mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
99    }
100
101    virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
102            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
103        mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
104    }
105
106private:
107    Texture* const mDelegate;
108}; // struct DelegateTexture
109
110/**
111 * TODO: This method does not take the rotation flag into account
112 */
113void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
114    const float width = float(mTexture->width);
115    const float height = float(mTexture->height);
116
117    for (int i = 0; i < count; ) {
118        SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]);
119        // NOTE: We're converting from 64 bit signed values to 32 bit
120        // signed values. This is guaranteed to be safe because the "x"
121        // and "y" coordinate values are guaranteed to be representable
122        // with 32 bits. The array is 64 bits wide so that it can carry
123        // pointers on 64 bit architectures.
124        const int x = static_cast<int>(map[i++]);
125        const int y = static_cast<int>(map[i++]);
126        bool rotated = map[i++] > 0;
127
128        // Bitmaps should never be null, we're just extra paranoid
129        if (!bitmap) continue;
130
131        const UvMapper mapper(
132                x / width, (x + bitmap->width()) / width,
133                y / height, (y + bitmap->height()) / height);
134
135        Texture* texture = new DelegateTexture(caches, mTexture);
136        texture->id = mTexture->id;
137        texture->blend = !bitmap->isOpaque();
138        texture->width = bitmap->width();
139        texture->height = bitmap->height();
140
141        Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
142        texture->uvMapper = &entry->uvMapper;
143
144        mEntries.add(entry->bitmap, entry);
145    }
146}
147
148}; // namespace uirenderer
149}; // namespace android
150