AssetAtlas.h revision 877cfe0e32a845d5a58252b8a6e1f54f95b4379c
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 373b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy/** 383b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * An asset atlas holds a collection of framework bitmaps in a single OpenGL 393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * texture. Each bitmap is associated with a location, defined in pixels, 403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * inside the atlas. The atlas is generated by the framework and bound as 413b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * an external texture using the EGLImageKHR extension. 423b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 433b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyclass AssetAtlas { 443b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guypublic: 453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 463b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Entry representing the position and rotation of a 473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * bitmap inside the atlas. 483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 493b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy struct Entry { 503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 513b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * The bitmap that generated this atlas entry. 523b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 533b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy SkBitmap* bitmap; 543b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 553b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 563b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Location of the bitmap inside the atlas, in pixels. 573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 583b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy int x; 593b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy int y; 603b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 623b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * If set, the bitmap is rotated 90 degrees (clockwise) 633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * inside the atlas. 643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy bool rotated; 663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Maps texture coordinates in the [0..1] range into the 693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * correct range to sample this entry from the atlas. 703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy const UvMapper uvMapper; 723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 733b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 743b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Atlas this entry belongs to. 753b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 763b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy const AssetAtlas& atlas; 773b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 783b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /* 793b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * A "virtual texture" object that represents the texture 803b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * this entry belongs to. This texture should never be 813b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * modified. 823b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 833b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy Texture texture; 843b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 853b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy private: 863b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy Entry(SkBitmap* bitmap, int x, int y, bool rotated, 873b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy const UvMapper& mapper, const AssetAtlas& atlas): 883b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy bitmap(bitmap), x(x), y(y), rotated(rotated), uvMapper(mapper), atlas(atlas) { } 893b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 903b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy friend class AssetAtlas; 913b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy }; 923b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 93877cfe0e32a845d5a58252b8a6e1f54f95b4379cRomain Guy AssetAtlas(): mWidth(0), mHeight(0), mTexture(0), mImage(NULL) { } 943b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy ~AssetAtlas() { terminate(); } 953b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 963b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 973b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Initializes the atlas with the specified buffer and 983b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * map. The buffer is a gralloc'd texture that will be 993b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * used as an EGLImage. The map is a list of SkBitmap* 1003b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * and their (x, y) positions as well as their rotation 1013b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * flags. 1023b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * 1033b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * This method returns immediately if the atlas is already 1043b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * initialized. To re-initialize the atlas, you must 1053b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * first call terminate(). 1063b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1073b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy ANDROID_API void init(sp<GraphicBuffer> buffer, int* map, int count); 1083b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1093b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1103b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Destroys the atlas texture. This object can be 1113b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * re-initialized after calling this method. 1123b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * 1133b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * After calling this method, the width, height 1143b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * and texture are set to 0. 1153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1163b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy ANDROID_API void terminate(); 1173b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1183b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1193b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Returns the width of this atlas in pixels. 1203b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Can return 0 if the atlas is not initialized. 1213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy uint32_t getWidth() const { 1233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy return mWidth; 1243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 1253b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Returns the height of this atlas in pixels. 1283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Can return 0 if the atlas is not initialized. 1293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1303b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy uint32_t getHeight() const { 1313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy return mHeight; 1323b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 1333b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1343b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1353b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Returns the OpenGL name of the texture backing this atlas. 1363b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Can return 0 if the atlas is not initialized. 1373b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1383b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy GLuint getTexture() const { 1393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy return mTexture; 1403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 1413b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1423b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1433b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Returns the entry in the atlas associated with the specified 1443b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * bitmap. If the bitmap is not in the atlas, return NULL. 1453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1463b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy Entry* getEntry(SkBitmap* const bitmap) const; 1473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 1493b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * Returns the texture for the atlas entry associated with the 1503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * specified bitmap. If the bitmap is not in the atlas, return NULL. 1513b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1523b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy Texture* getEntryTexture(SkBitmap* const bitmap) const; 1533b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1543b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyprivate: 1553b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy void createEntries(int* map, int count); 1563b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy uint32_t mWidth; 1583b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy uint32_t mHeight; 1593b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1603b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy GLuint mTexture; 161877cfe0e32a845d5a58252b8a6e1f54f95b4379cRomain Guy Image* mImage; 1623b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy KeyedVector<SkBitmap*, Entry*> mEntries; 1643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // class AssetAtlas 1653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace uirenderer 1673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy}; // namespace android 1683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#endif // ANDROID_HWUI_ASSET_ATLAS_H 170