AssetAtlas.cpp revision 15c3f19a445b8df575911a16e8a6dba755a084b5
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#include "Caches.h"
19#include "Image.h"
20
21#include <GLES2/gl2ext.h>
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Lifecycle
28///////////////////////////////////////////////////////////////////////////////
29
30void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
31    if (mImage) {
32        return;
33    }
34
35    ATRACE_NAME("AssetAtlas::init");
36
37    mImage = new Image(buffer);
38    if (mImage->getTexture()) {
39        if (!mTexture) {
40            Caches& caches = Caches::getInstance();
41            mTexture = new Texture(caches);
42            mTexture->width = buffer->getWidth();
43            mTexture->height = buffer->getHeight();
44            createEntries(caches, map, count);
45        }
46    } else {
47        ALOGW("Could not create atlas image");
48        delete mImage;
49        mImage = nullptr;
50    }
51
52    updateTextureId();
53}
54
55void AssetAtlas::terminate() {
56    if (mImage) {
57        delete mImage;
58        mImage = nullptr;
59        updateTextureId();
60    }
61}
62
63
64void AssetAtlas::updateTextureId() {
65    mTexture->id = mImage ? mImage->getTexture() : 0;
66    if (mTexture->id) {
67        // Texture ID changed, force-set to defaults to sync the wrapper & GL
68        // state objects
69        mTexture->setWrap(GL_CLAMP_TO_EDGE, false, true);
70        mTexture->setFilter(GL_NEAREST, false, true);
71    }
72    for (size_t i = 0; i < mEntries.size(); i++) {
73        AssetAtlas::Entry* entry = mEntries.valueAt(i);
74        entry->texture->id = mTexture->id;
75    }
76}
77
78///////////////////////////////////////////////////////////////////////////////
79// Entries
80///////////////////////////////////////////////////////////////////////////////
81
82AssetAtlas::Entry* AssetAtlas::getEntry(const SkPixelRef* pixelRef) const {
83    ssize_t index = mEntries.indexOfKey(pixelRef);
84    return index >= 0 ? mEntries.valueAt(index) : nullptr;
85}
86
87Texture* AssetAtlas::getEntryTexture(const SkPixelRef* pixelRef) const {
88    ssize_t index = mEntries.indexOfKey(pixelRef);
89    return index >= 0 ? mEntries.valueAt(index)->texture : nullptr;
90}
91
92/**
93 * Delegates changes to wrapping and filtering to the base atlas texture
94 * instead of applying the changes to the virtual textures.
95 */
96struct DelegateTexture: public Texture {
97    DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
98
99    virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
100            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
101        mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
102    }
103
104    virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
105            bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
106        mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
107    }
108
109private:
110    Texture* const mDelegate;
111}; // struct DelegateTexture
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        SkPixelRef* pixelRef = reinterpret_cast<SkPixelRef*>(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
127        // Bitmaps should never be null, we're just extra paranoid
128        if (!pixelRef) continue;
129
130        const UvMapper mapper(
131                x / width, (x + pixelRef->info().width()) / width,
132                y / height, (y + pixelRef->info().height()) / height);
133
134        Texture* texture = new DelegateTexture(caches, mTexture);
135        texture->blend = !SkAlphaTypeIsOpaque(pixelRef->info().alphaType());
136        texture->width = pixelRef->info().width();
137        texture->height = pixelRef->info().height();
138
139        Entry* entry = new Entry(pixelRef, texture, mapper, *this);
140        texture->uvMapper = &entry->uvMapper;
141
142        mEntries.add(entry->pixelRef, entry);
143    }
144}
145
146}; // namespace uirenderer
147}; // namespace android
148