blob: 793e30005b2888802a1be38226180ee04d0d87ca [file] [log] [blame]
Romain Guy3b748a42013-04-17 18:54:38 -07001/*
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
Romain Guy3b748a42013-04-17 18:54:38 -070022#include <ui/GraphicBuffer.h>
23
24#include <utils/KeyedVector.h>
25
26#include <cutils/compiler.h>
27
28#include <SkBitmap.h>
29
Romain Guy877cfe02013-05-02 17:36:28 -070030#include "Image.h"
Romain Guy3b748a42013-04-17 18:54:38 -070031#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
Romain Guya404e162013-05-24 16:19:19 -070067 /*
68 * A "virtual texture" object that represents the texture
69 * this entry belongs to. This texture should never be
70 * modified.
71 */
72 Texture* texture;
73
Romain Guy3b748a42013-04-17 18:54:38 -070074 /**
75 * Maps texture coordinates in the [0..1] range into the
76 * correct range to sample this entry from the atlas.
77 */
78 const UvMapper uvMapper;
79
80 /**
81 * Atlas this entry belongs to.
82 */
83 const AssetAtlas& atlas;
84
Romain Guy3b748a42013-04-17 18:54:38 -070085 private:
86 Entry(SkBitmap* bitmap, int x, int y, bool rotated,
Romain Guya404e162013-05-24 16:19:19 -070087 Texture* texture, const UvMapper& mapper, const AssetAtlas& atlas):
88 bitmap(bitmap), x(x), y(y), rotated(rotated),
89 texture(texture), uvMapper(mapper), atlas(atlas) { }
90
91 ~Entry() {
92 delete texture;
93 }
Romain Guy3b748a42013-04-17 18:54:38 -070094
95 friend class AssetAtlas;
96 };
97
Romain Guya404e162013-05-24 16:19:19 -070098 AssetAtlas(): mTexture(NULL), mImage(NULL) { }
Romain Guy3b748a42013-04-17 18:54:38 -070099 ~AssetAtlas() { terminate(); }
100
101 /**
102 * Initializes the atlas with the specified buffer and
103 * map. The buffer is a gralloc'd texture that will be
104 * used as an EGLImage. The map is a list of SkBitmap*
105 * and their (x, y) positions as well as their rotation
106 * flags.
107 *
108 * This method returns immediately if the atlas is already
109 * initialized. To re-initialize the atlas, you must
110 * first call terminate().
111 */
112 ANDROID_API void init(sp<GraphicBuffer> buffer, int* map, int count);
113
114 /**
115 * Destroys the atlas texture. This object can be
116 * re-initialized after calling this method.
117 *
118 * After calling this method, the width, height
119 * and texture are set to 0.
120 */
121 ANDROID_API void terminate();
122
123 /**
124 * Returns the width of this atlas in pixels.
125 * Can return 0 if the atlas is not initialized.
126 */
127 uint32_t getWidth() const {
Romain Guya404e162013-05-24 16:19:19 -0700128 return mTexture ? mTexture->width : 0;
Romain Guy3b748a42013-04-17 18:54:38 -0700129 }
130
131 /**
132 * Returns the height of this atlas in pixels.
133 * Can return 0 if the atlas is not initialized.
134 */
135 uint32_t getHeight() const {
Romain Guya404e162013-05-24 16:19:19 -0700136 return mTexture ? mTexture->height : 0;
Romain Guy3b748a42013-04-17 18:54:38 -0700137 }
138
139 /**
140 * Returns the OpenGL name of the texture backing this atlas.
141 * Can return 0 if the atlas is not initialized.
142 */
143 GLuint getTexture() const {
Romain Guya404e162013-05-24 16:19:19 -0700144 return mTexture ? mTexture->id : 0;
Romain Guy3b748a42013-04-17 18:54:38 -0700145 }
146
147 /**
148 * Returns the entry in the atlas associated with the specified
149 * bitmap. If the bitmap is not in the atlas, return NULL.
150 */
151 Entry* getEntry(SkBitmap* const bitmap) const;
152
153 /**
154 * Returns the texture for the atlas entry associated with the
155 * specified bitmap. If the bitmap is not in the atlas, return NULL.
156 */
157 Texture* getEntryTexture(SkBitmap* const bitmap) const;
158
159private:
160 void createEntries(int* map, int count);
161
Romain Guya404e162013-05-24 16:19:19 -0700162 Texture* mTexture;
Romain Guy877cfe02013-05-02 17:36:28 -0700163 Image* mImage;
Romain Guy3b748a42013-04-17 18:54:38 -0700164
165 KeyedVector<SkBitmap*, Entry*> mEntries;
166}; // class AssetAtlas
167
168}; // namespace uirenderer
169}; // namespace android
170
171#endif // ANDROID_HWUI_ASSET_ATLAS_H