Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 "Texture.h" |
| 31 | #include "UvMapper.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 36 | class Caches; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 37 | class Image; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 38 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 | */ |
| 45 | class AssetAtlas { |
| 46 | public: |
| 47 | /** |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 48 | * Entry representing the texture and uvMapper of a PixelRef in the |
| 49 | * atlas |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 50 | */ |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 51 | class Entry { |
| 52 | public: |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 53 | /* |
| 54 | * A "virtual texture" object that represents the texture |
| 55 | * this entry belongs to. This texture should never be |
| 56 | * modified. |
| 57 | */ |
| 58 | Texture* texture; |
| 59 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Maps texture coordinates in the [0..1] range into the |
| 62 | * correct range to sample this entry from the atlas. |
| 63 | */ |
| 64 | const UvMapper uvMapper; |
| 65 | |
| 66 | /** |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 67 | * Unique identifier used to merge bitmaps and 9-patches stored |
| 68 | * in the atlas. |
| 69 | */ |
| 70 | const void* getMergeId() const { |
| 71 | return texture->blend ? &atlas.mBlendKey : &atlas.mOpaqueKey; |
| 72 | } |
| 73 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 74 | private: |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 75 | /** |
| 76 | * The pixel ref that generated this atlas entry. |
| 77 | */ |
| 78 | SkPixelRef* pixelRef; |
| 79 | |
| 80 | /** |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 81 | * Atlas this entry belongs to. |
| 82 | */ |
| 83 | const AssetAtlas& atlas; |
| 84 | |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 85 | Entry(SkPixelRef* pixelRef, Texture* texture, const UvMapper& mapper, |
| 86 | const AssetAtlas& atlas) |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 87 | : texture(texture) |
| 88 | , uvMapper(mapper) |
| 89 | , pixelRef(pixelRef) |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 90 | , atlas(atlas) { |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 91 | } |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 92 | |
| 93 | ~Entry() { |
| 94 | delete texture; |
| 95 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 96 | |
| 97 | friend class AssetAtlas; |
| 98 | }; |
| 99 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 100 | AssetAtlas(): mTexture(nullptr), mImage(nullptr), |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 101 | mBlendKey(true), mOpaqueKey(false) { } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 102 | ~AssetAtlas() { terminate(); } |
| 103 | |
| 104 | /** |
| 105 | * Initializes the atlas with the specified buffer and |
| 106 | * map. The buffer is a gralloc'd texture that will be |
| 107 | * used as an EGLImage. The map is a list of SkBitmap* |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 108 | * and their (x, y) positions |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 109 | * |
| 110 | * This method returns immediately if the atlas is already |
| 111 | * initialized. To re-initialize the atlas, you must |
| 112 | * first call terminate(). |
| 113 | */ |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 114 | ANDROID_API void init(sp<GraphicBuffer> buffer, int64_t* map, int count); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Destroys the atlas texture. This object can be |
| 118 | * re-initialized after calling this method. |
| 119 | * |
| 120 | * After calling this method, the width, height |
| 121 | * and texture are set to 0. |
| 122 | */ |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 123 | void terminate(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Returns the width of this atlas in pixels. |
| 127 | * Can return 0 if the atlas is not initialized. |
| 128 | */ |
| 129 | uint32_t getWidth() const { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 130 | return mTexture ? mTexture->width : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns the height of this atlas in pixels. |
| 135 | * Can return 0 if the atlas is not initialized. |
| 136 | */ |
| 137 | uint32_t getHeight() const { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 138 | return mTexture ? mTexture->height : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the OpenGL name of the texture backing this atlas. |
| 143 | * Can return 0 if the atlas is not initialized. |
| 144 | */ |
| 145 | GLuint getTexture() const { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 146 | return mTexture ? mTexture->id : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the entry in the atlas associated with the specified |
| 151 | * bitmap. If the bitmap is not in the atlas, return NULL. |
| 152 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 153 | Entry* getEntry(const SkBitmap* bitmap) const; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Returns the texture for the atlas entry associated with the |
| 157 | * specified bitmap. If the bitmap is not in the atlas, return NULL. |
| 158 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 159 | Texture* getEntryTexture(const SkBitmap* bitmap) const; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 160 | |
| 161 | private: |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 162 | void createEntries(Caches& caches, int64_t* map, int count); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 163 | void updateTextureId(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 164 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 165 | Texture* mTexture; |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 166 | Image* mImage; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 167 | |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 168 | const bool mBlendKey; |
| 169 | const bool mOpaqueKey; |
| 170 | |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 171 | KeyedVector<const SkPixelRef*, Entry*> mEntries; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 172 | }; // class AssetAtlas |
| 173 | |
| 174 | }; // namespace uirenderer |
| 175 | }; // namespace android |
| 176 | |
| 177 | #endif // ANDROID_HWUI_ASSET_ATLAS_H |