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 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 20 | #include "Texture.h" |
| 21 | #include "UvMapper.h" |
| 22 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 23 | #include <cutils/compiler.h> |
| 24 | #include <GLES2/gl2.h> |
| 25 | #include <ui/GraphicBuffer.h> |
| 26 | #include <SkBitmap.h> |
| 27 | |
| 28 | #include <memory> |
| 29 | #include <unordered_map> |
| 30 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 34 | class Caches; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 35 | class Image; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 36 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 | */ |
| 43 | class AssetAtlas { |
| 44 | public: |
| 45 | /** |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 46 | * Entry representing the texture and uvMapper of a PixelRef in the |
| 47 | * atlas |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 48 | */ |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 49 | class Entry { |
| 50 | public: |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 51 | /* |
| 52 | * A "virtual texture" object that represents the texture |
| 53 | * this entry belongs to. This texture should never be |
| 54 | * modified. |
| 55 | */ |
| 56 | Texture* texture; |
| 57 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Maps texture coordinates in the [0..1] range into the |
| 60 | * correct range to sample this entry from the atlas. |
| 61 | */ |
| 62 | const UvMapper uvMapper; |
| 63 | |
| 64 | /** |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 65 | * Unique identifier used to merge bitmaps and 9-patches stored |
| 66 | * in the atlas. |
| 67 | */ |
| 68 | const void* getMergeId() const { |
| 69 | return texture->blend ? &atlas.mBlendKey : &atlas.mOpaqueKey; |
| 70 | } |
| 71 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 72 | ~Entry() { |
| 73 | delete texture; |
| 74 | } |
| 75 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 76 | private: |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 77 | /** |
| 78 | * The pixel ref that generated this atlas entry. |
| 79 | */ |
| 80 | SkPixelRef* pixelRef; |
| 81 | |
| 82 | /** |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 83 | * Atlas this entry belongs to. |
| 84 | */ |
| 85 | const AssetAtlas& atlas; |
| 86 | |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 87 | Entry(SkPixelRef* pixelRef, Texture* texture, const UvMapper& mapper, |
| 88 | const AssetAtlas& atlas) |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 89 | : texture(texture) |
| 90 | , uvMapper(mapper) |
| 91 | , pixelRef(pixelRef) |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 92 | , atlas(atlas) { |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 93 | } |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 94 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 95 | friend class AssetAtlas; |
| 96 | }; |
| 97 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 98 | AssetAtlas(): mTexture(nullptr), mImage(nullptr), |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 99 | mBlendKey(true), mOpaqueKey(false) { } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 100 | ~AssetAtlas() { terminate(); } |
| 101 | |
| 102 | /** |
| 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* |
John Reck | a039182 | 2015-05-07 10:49:55 -0700 | [diff] [blame] | 106 | * and their (x, y) positions |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 | */ |
Chih-Hung Hsieh | f35c939 | 2016-08-10 14:08:35 -0700 | [diff] [blame] | 112 | ANDROID_API void init(const sp<GraphicBuffer>& buffer, int64_t* map, int count); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 | */ |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 121 | void terminate(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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 { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 128 | return mTexture ? mTexture->width() : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 129 | } |
| 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 { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 136 | return mTexture ? mTexture->height() : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 137 | } |
| 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 { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 144 | return mTexture ? mTexture->id() : 0; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the entry in the atlas associated with the specified |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 149 | * pixelRef. If the pixelRef is not in the atlas, return NULL. |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 150 | */ |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 151 | Entry* getEntry(const SkPixelRef* pixelRef) const; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Returns the texture for the atlas entry associated with the |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 155 | * specified pixelRef. If the pixelRef is not in the atlas, return NULL. |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 156 | */ |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 157 | Texture* getEntryTexture(const SkPixelRef* pixelRef) const; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 158 | |
| 159 | private: |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 160 | void createEntries(Caches& caches, int64_t* map, int count); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 161 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 162 | Texture* mTexture; |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 163 | Image* mImage; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 164 | |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 165 | const bool mBlendKey; |
| 166 | const bool mOpaqueKey; |
| 167 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 168 | std::unordered_map<const SkPixelRef*, std::unique_ptr<Entry>> mEntries; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 169 | }; // class AssetAtlas |
| 170 | |
| 171 | }; // namespace uirenderer |
| 172 | }; // namespace android |
| 173 | |
| 174 | #endif // ANDROID_HWUI_ASSET_ATLAS_H |