2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_ASSET_ATLAS_H
18 #define ANDROID_HWUI_ASSET_ATLAS_H
23 #include <cutils/compiler.h>
24 #include <GLES2/gl2.h>
25 #include <ui/GraphicBuffer.h>
29 #include <unordered_map>
32 namespace uirenderer {
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.
46 * Entry representing the texture and uvMapper of a PixelRef in the
52 * A "virtual texture" object that represents the texture
53 * this entry belongs to. This texture should never be
59 * Maps texture coordinates in the [0..1] range into the
60 * correct range to sample this entry from the atlas.
62 const UvMapper uvMapper;
65 * Unique identifier used to merge bitmaps and 9-patches stored
68 const void* getMergeId() const {
69 return texture->blend ? &atlas.mBlendKey : &atlas.mOpaqueKey;
78 * The pixel ref that generated this atlas entry.
83 * Atlas this entry belongs to.
85 const AssetAtlas& atlas;
87 Entry(SkPixelRef* pixelRef, Texture* texture, const UvMapper& mapper,
88 const AssetAtlas& atlas)
95 friend class AssetAtlas;
98 AssetAtlas(): mTexture(nullptr), mImage(nullptr),
99 mBlendKey(true), mOpaqueKey(false) { }
100 ~AssetAtlas() { terminate(); }
103 * Initializes the atlas with the specified buffer and
104 * map. The buffer is a gralloc'd texture that will be
105 * used as an EGLImage. The map is a list of SkBitmap*
106 * and their (x, y) positions
108 * This method returns immediately if the atlas is already
109 * initialized. To re-initialize the atlas, you must
110 * first call terminate().
112 ANDROID_API void init(sp<GraphicBuffer> buffer, int64_t* map, int count);
115 * Destroys the atlas texture. This object can be
116 * re-initialized after calling this method.
118 * After calling this method, the width, height
119 * and texture are set to 0.
124 * Returns the width of this atlas in pixels.
125 * Can return 0 if the atlas is not initialized.
127 uint32_t getWidth() const {
128 return mTexture ? mTexture->width() : 0;
132 * Returns the height of this atlas in pixels.
133 * Can return 0 if the atlas is not initialized.
135 uint32_t getHeight() const {
136 return mTexture ? mTexture->height() : 0;
140 * Returns the OpenGL name of the texture backing this atlas.
141 * Can return 0 if the atlas is not initialized.
143 GLuint getTexture() const {
144 return mTexture ? mTexture->id() : 0;
148 * Returns the entry in the atlas associated with the specified
149 * pixelRef. If the pixelRef is not in the atlas, return NULL.
151 Entry* getEntry(const SkPixelRef* pixelRef) const;
154 * Returns the texture for the atlas entry associated with the
155 * specified pixelRef. If the pixelRef is not in the atlas, return NULL.
157 Texture* getEntryTexture(const SkPixelRef* pixelRef) const;
160 void createEntries(Caches& caches, int64_t* map, int count);
165 const bool mBlendKey;
166 const bool mOpaqueKey;
168 std::unordered_map<const SkPixelRef*, std::unique_ptr<Entry>> mEntries;
169 }; // class AssetAtlas
171 }; // namespace uirenderer
172 }; // namespace android
174 #endif // ANDROID_HWUI_ASSET_ATLAS_H