AssetAtlas.h revision 3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9
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 <EGL/egl.h>
233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <EGL/eglext.h>
243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
253b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <ui/GraphicBuffer.h>
263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <utils/KeyedVector.h>
283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <cutils/compiler.h>
303b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <SkBitmap.h>
323b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
333b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "Texture.h"
343b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "UvMapper.h"
353b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
363b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guynamespace android {
373b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guynamespace uirenderer {
383b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain 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
693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * Maps texture coordinates in the [0..1] range into the
713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * correct range to sample this entry from the atlas.
723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
733b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        const UvMapper uvMapper;
743b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
753b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /**
763b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * Atlas this entry belongs to.
773b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
783b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        const AssetAtlas& atlas;
793b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
803b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        /*
813b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * A "virtual texture" object that represents the texture
823b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * this entry belongs to. This texture should never be
833b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         * modified.
843b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy         */
853b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        Texture texture;
863b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
873b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    private:
883b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        Entry(SkBitmap* bitmap, int x, int y, bool rotated,
893b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                const UvMapper& mapper, const AssetAtlas& atlas):
903b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                bitmap(bitmap), x(x), y(y), rotated(rotated), uvMapper(mapper), atlas(atlas) { }
913b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
923b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        friend class AssetAtlas;
933b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    };
943b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
953b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    AssetAtlas(): mWidth(0), mHeight(0), mTexture(0), mImage(EGL_NO_IMAGE_KHR) { }
963b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ~AssetAtlas() { terminate(); }
973b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
983b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
993b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Initializes the atlas with the specified buffer and
1003b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * map. The buffer is a gralloc'd texture that will be
1013b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * used as an EGLImage. The map is a list of SkBitmap*
1023b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * and their (x, y) positions as well as their rotation
1033b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * flags.
1043b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     *
1053b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * This method returns immediately if the atlas is already
1063b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * initialized. To re-initialize the atlas, you must
1073b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * first call terminate().
1083b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1093b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ANDROID_API void init(sp<GraphicBuffer> buffer, int* map, int count);
1103b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1113b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1123b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Destroys the atlas texture. This object can be
1133b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * re-initialized after calling this method.
1143b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     *
1153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * After calling this method, the width, height
1163b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * and texture are set to 0.
1173b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1183b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    ANDROID_API void terminate();
1193b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1203b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the width of this atlas in pixels.
1223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t getWidth() const {
1253b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mWidth;
1263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the height of this atlas in pixels.
1303b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1323b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t getHeight() const {
1333b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mHeight;
1343b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1353b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1363b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1373b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the OpenGL name of the texture backing this atlas.
1383b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Can return 0 if the atlas is not initialized.
1393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    GLuint getTexture() const {
1413b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mTexture;
1423b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
1433b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1443b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the entry in the atlas associated with the specified
1463b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * bitmap. If the bitmap is not in the atlas, return NULL.
1473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    Entry* getEntry(SkBitmap* const bitmap) const;
1493b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    /**
1513b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * Returns the texture for the atlas entry associated with the
1523b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     * specified bitmap. If the bitmap is not in the atlas, return NULL.
1533b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy     */
1543b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    Texture* getEntryTexture(SkBitmap* const bitmap) const;
1553b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1563b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyprivate:
1573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    void createEntries(int* map, int count);
1583b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1593b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t mWidth;
1603b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t mHeight;
1613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1623b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    GLuint mTexture;
1633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    EGLImageKHR mImage;
1643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    KeyedVector<SkBitmap*, Entry*> mEntries;
1663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // class AssetAtlas
1673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace uirenderer
1693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace android
1703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#endif // ANDROID_HWUI_ASSET_ATLAS_H
172