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