AssetAtlas.h revision 8aa195d7081b889f3a7b1f426cbd8556377aae5e
13b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy/*
23b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Copyright (C) 2013 The Android Open Source Project
33b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy *
43b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
53b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * you may not use this file except in compliance with the License.
63b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * You may obtain a copy of the License at
73b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy *
83b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
93b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy *
103b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Unless required by applicable law or agreed to in writing, software
113b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
123b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * See the License for the specific language governing permissions and
143b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * limitations under the License.
153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */
163b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
173b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#ifndef ANDROID_HWUI_ASSET_ATLAS_H
183b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#define ANDROID_HWUI_ASSET_ATLAS_H
193b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
203b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <GLES2/gl2.h>
213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <ui/GraphicBuffer.h>
233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <utils/KeyedVector.h>
253b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <cutils/compiler.h>
273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <SkBitmap.h>
293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
30877cfe0e32a845d5a58252b8a6e1f54f95b4379cRomain Guy#include "Image.h"
313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "Texture.h"
323b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "UvMapper.h"
333b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
343b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guynamespace android {
353b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guynamespace uirenderer {
363b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
378aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyclass Caches;
388aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy/**
403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * An asset atlas holds a collection of framework bitmaps in a single OpenGL
413b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * texture. Each bitmap is associated with a location, defined in pixels,
423b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * inside the atlas. The atlas is generated by the framework and bound as
433b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * an external texture using the EGLImageKHR extension.
443b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */
453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyclass AssetAtlas {
463b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guypublic:
473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Entry representing the position and rotation of a
493b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * bitmap inside the atlas.
503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
513b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    struct Entry {
523b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
533b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * The bitmap that generated this atlas entry.
543b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
553b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        SkBitmap* bitmap;
563b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
583b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * Location of the bitmap inside the atlas, in pixels.
593b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
603b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        int x;
613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        int y;
623b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * If set, the bitmap is rotated 90 degrees (clockwise)
653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * inside the atlas.
663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        bool rotated;
683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
69a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        /*
70a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy         * A "virtual texture" object that represents the texture
71a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy         * this entry belongs to. This texture should never be
72a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy         * modified.
73a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy         */
74a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        Texture* texture;
75a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy
763b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
773b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * Maps texture coordinates in the [0..1] range into the
783b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * correct range to sample this entry from the atlas.
793b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
803b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        const UvMapper uvMapper;
813b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
823b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
833b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * Atlas this entry belongs to.
843b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
853b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        const AssetAtlas& atlas;
863b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
873b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    private:
883b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        Entry(SkBitmap* bitmap, int x, int y, bool rotated,
89a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy                Texture* texture, const UvMapper& mapper, const AssetAtlas& atlas):
90a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy                bitmap(bitmap), x(x), y(y), rotated(rotated),
91a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy                texture(texture), uvMapper(mapper), atlas(atlas) { }
92a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy
93a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        ~Entry() {
94a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy            delete texture;
95a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        }
963b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
973b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        friend class AssetAtlas;
983b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    };
993b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
100a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy    AssetAtlas(): mTexture(NULL), mImage(NULL) { }
1013b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ~AssetAtlas() { terminate(); }
1023b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1033b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1043b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Initializes the atlas with the specified buffer and
1053b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * map. The buffer is a gralloc'd texture that will be
1063b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * used as an EGLImage. The map is a list of SkBitmap*
1073b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * and their (x, y) positions as well as their rotation
1083b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * flags.
1093b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     *
1103b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * This method returns immediately if the atlas is already
1113b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * initialized. To re-initialize the atlas, you must
1123b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * first call terminate().
1133b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1143b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ANDROID_API void init(sp<GraphicBuffer> buffer, int* map, int count);
1153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1163b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1173b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Destroys the atlas texture. This object can be
1183b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * re-initialized after calling this method.
1193b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     *
1203b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * After calling this method, the width, height
1213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * and texture are set to 0.
1223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ANDROID_API void terminate();
1243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1253b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the width of this atlas in pixels.
1273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t getWidth() const {
130a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        return mTexture ? mTexture->width : 0;
1313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1323b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1333b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1343b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the height of this atlas in pixels.
1353b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1363b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1373b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t getHeight() const {
138a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        return mTexture ? mTexture->height : 0;
1393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1413b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1423b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the OpenGL name of the texture backing this atlas.
1433b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1443b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    GLuint getTexture() const {
146a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy        return mTexture ? mTexture->id : 0;
1473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1493b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the entry in the atlas associated with the specified
1513b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * bitmap. If the bitmap is not in the atlas, return NULL.
1523b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1533b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    Entry* getEntry(SkBitmap* const bitmap) const;
1543b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1553b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1563b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the texture for the atlas entry associated with the
1573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * specified bitmap. If the bitmap is not in the atlas, return NULL.
1583b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1593b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    Texture* getEntryTexture(SkBitmap* const bitmap) const;
1603b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyprivate:
1628aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    void createEntries(Caches& caches, int* map, int count);
1633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
164a404e16e4933857464046d763ed7629cd0c86cbfRomain Guy    Texture* mTexture;
165877cfe0e32a845d5a58252b8a6e1f54f95b4379cRomain Guy    Image* mImage;
1663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    KeyedVector<SkBitmap*, Entry*> mEntries;
1683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // class AssetAtlas
1693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace uirenderer
1713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace android
1723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1733b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#endif // ANDROID_HWUI_ASSET_ATLAS_H
174