AssetAtlas.h revision 877cfe0e32a845d5a58252b8a6e1f54f95b4379c
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#ifndef ANDROID_HWUI_ASSET_ATLAS_H 18#define ANDROID_HWUI_ASSET_ATLAS_H 19 20#include <GLES2/gl2.h> 21 22#include <ui/GraphicBuffer.h> 23 24#include <utils/KeyedVector.h> 25 26#include <cutils/compiler.h> 27 28#include <SkBitmap.h> 29 30#include "Image.h" 31#include "Texture.h" 32#include "UvMapper.h" 33 34namespace android { 35namespace uirenderer { 36 37/** 38 * An asset atlas holds a collection of framework bitmaps in a single OpenGL 39 * texture. Each bitmap is associated with a location, defined in pixels, 40 * inside the atlas. The atlas is generated by the framework and bound as 41 * an external texture using the EGLImageKHR extension. 42 */ 43class AssetAtlas { 44public: 45 /** 46 * Entry representing the position and rotation of a 47 * bitmap inside the atlas. 48 */ 49 struct Entry { 50 /** 51 * The bitmap that generated this atlas entry. 52 */ 53 SkBitmap* bitmap; 54 55 /** 56 * Location of the bitmap inside the atlas, in pixels. 57 */ 58 int x; 59 int y; 60 61 /** 62 * If set, the bitmap is rotated 90 degrees (clockwise) 63 * inside the atlas. 64 */ 65 bool rotated; 66 67 /** 68 * Maps texture coordinates in the [0..1] range into the 69 * correct range to sample this entry from the atlas. 70 */ 71 const UvMapper uvMapper; 72 73 /** 74 * Atlas this entry belongs to. 75 */ 76 const AssetAtlas& atlas; 77 78 /* 79 * A "virtual texture" object that represents the texture 80 * this entry belongs to. This texture should never be 81 * modified. 82 */ 83 Texture texture; 84 85 private: 86 Entry(SkBitmap* bitmap, int x, int y, bool rotated, 87 const UvMapper& mapper, const AssetAtlas& atlas): 88 bitmap(bitmap), x(x), y(y), rotated(rotated), uvMapper(mapper), atlas(atlas) { } 89 90 friend class AssetAtlas; 91 }; 92 93 AssetAtlas(): mWidth(0), mHeight(0), mTexture(0), mImage(NULL) { } 94 ~AssetAtlas() { terminate(); } 95 96 /** 97 * Initializes the atlas with the specified buffer and 98 * map. The buffer is a gralloc'd texture that will be 99 * used as an EGLImage. The map is a list of SkBitmap* 100 * and their (x, y) positions as well as their rotation 101 * flags. 102 * 103 * This method returns immediately if the atlas is already 104 * initialized. To re-initialize the atlas, you must 105 * first call terminate(). 106 */ 107 ANDROID_API void init(sp<GraphicBuffer> buffer, int* map, int count); 108 109 /** 110 * Destroys the atlas texture. This object can be 111 * re-initialized after calling this method. 112 * 113 * After calling this method, the width, height 114 * and texture are set to 0. 115 */ 116 ANDROID_API void terminate(); 117 118 /** 119 * Returns the width of this atlas in pixels. 120 * Can return 0 if the atlas is not initialized. 121 */ 122 uint32_t getWidth() const { 123 return mWidth; 124 } 125 126 /** 127 * Returns the height of this atlas in pixels. 128 * Can return 0 if the atlas is not initialized. 129 */ 130 uint32_t getHeight() const { 131 return mHeight; 132 } 133 134 /** 135 * Returns the OpenGL name of the texture backing this atlas. 136 * Can return 0 if the atlas is not initialized. 137 */ 138 GLuint getTexture() const { 139 return mTexture; 140 } 141 142 /** 143 * Returns the entry in the atlas associated with the specified 144 * bitmap. If the bitmap is not in the atlas, return NULL. 145 */ 146 Entry* getEntry(SkBitmap* const bitmap) const; 147 148 /** 149 * Returns the texture for the atlas entry associated with the 150 * specified bitmap. If the bitmap is not in the atlas, return NULL. 151 */ 152 Texture* getEntryTexture(SkBitmap* const bitmap) const; 153 154private: 155 void createEntries(int* map, int count); 156 157 uint32_t mWidth; 158 uint32_t mHeight; 159 160 GLuint mTexture; 161 Image* mImage; 162 163 KeyedVector<SkBitmap*, Entry*> mEntries; 164}; // class AssetAtlas 165 166}; // namespace uirenderer 167}; // namespace android 168 169#endif // ANDROID_HWUI_ASSET_ATLAS_H 170