blob: b32e51851b9426a31b7185d5f71c62b708ee55ee [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
Romain Guy3b748a42013-04-17 18:54:38 -070020#include "Texture.h"
21#include "UvMapper.h"
22
John Reck38e0c322015-11-10 12:19:17 -080023#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 Guy3b748a42013-04-17 18:54:38 -070031namespace android {
32namespace uirenderer {
33
Romain Guy8aa195d2013-06-04 18:00:09 -070034class Caches;
Tom Hudson2dc236b2014-10-15 15:46:42 -040035class Image;
Romain Guy8aa195d2013-06-04 18:00:09 -070036
Romain Guy3b748a42013-04-17 18:54:38 -070037/**
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 /**
John Recka0391822015-05-07 10:49:55 -070046 * Entry representing the texture and uvMapper of a PixelRef in the
47 * atlas
Romain Guy3b748a42013-04-17 18:54:38 -070048 */
John Reckc6e2e8f2015-04-15 13:24:47 -070049 class Entry {
50 public:
Romain Guya404e162013-05-24 16:19:19 -070051 /*
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 Guy3b748a42013-04-17 18:54:38 -070058 /**
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 Guy7f6d6b02013-08-06 13:49:28 -070065 * 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 Reck38e0c322015-11-10 12:19:17 -080072 ~Entry() {
73 delete texture;
74 }
75
Romain Guy3b748a42013-04-17 18:54:38 -070076 private:
John Reckc6e2e8f2015-04-15 13:24:47 -070077 /**
78 * The pixel ref that generated this atlas entry.
79 */
80 SkPixelRef* pixelRef;
81
82 /**
John Reckc6e2e8f2015-04-15 13:24:47 -070083 * Atlas this entry belongs to.
84 */
85 const AssetAtlas& atlas;
86
John Recka0391822015-05-07 10:49:55 -070087 Entry(SkPixelRef* pixelRef, Texture* texture, const UvMapper& mapper,
88 const AssetAtlas& atlas)
John Reckc6e2e8f2015-04-15 13:24:47 -070089 : texture(texture)
90 , uvMapper(mapper)
91 , pixelRef(pixelRef)
John Reckc6e2e8f2015-04-15 13:24:47 -070092 , atlas(atlas) {
Romain Guy7f6d6b02013-08-06 13:49:28 -070093 }
Romain Guya404e162013-05-24 16:19:19 -070094
Romain Guy3b748a42013-04-17 18:54:38 -070095 friend class AssetAtlas;
96 };
97
Chris Craike84a2082014-12-22 14:28:49 -080098 AssetAtlas(): mTexture(nullptr), mImage(nullptr),
Romain Guy7f6d6b02013-08-06 13:49:28 -070099 mBlendKey(true), mOpaqueKey(false) { }
Romain Guy3b748a42013-04-17 18:54:38 -0700100 ~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 Recka0391822015-05-07 10:49:55 -0700106 * and their (x, y) positions
Romain Guy3b748a42013-04-17 18:54:38 -0700107 *
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 Hsiehf35c9392016-08-10 14:08:35 -0700112 ANDROID_API void init(const sp<GraphicBuffer>& buffer, int64_t* map, int count);
Romain Guy3b748a42013-04-17 18:54:38 -0700113
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 Reckebd52612014-12-10 16:47:36 -0800121 void terminate();
Romain Guy3b748a42013-04-17 18:54:38 -0700122
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 Reck38e0c322015-11-10 12:19:17 -0800128 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 {
John Reck38e0c322015-11-10 12:19:17 -0800136 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 {
John Reck38e0c322015-11-10 12:19:17 -0800144 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
Chris Craik15c3f192015-12-03 12:16:56 -0800149 * pixelRef. If the pixelRef is not in the atlas, return NULL.
Romain Guy3b748a42013-04-17 18:54:38 -0700150 */
Chris Craik15c3f192015-12-03 12:16:56 -0800151 Entry* getEntry(const SkPixelRef* pixelRef) const;
Romain Guy3b748a42013-04-17 18:54:38 -0700152
153 /**
154 * Returns the texture for the atlas entry associated with the
Chris Craik15c3f192015-12-03 12:16:56 -0800155 * specified pixelRef. If the pixelRef is not in the atlas, return NULL.
Romain Guy3b748a42013-04-17 18:54:38 -0700156 */
Chris Craik15c3f192015-12-03 12:16:56 -0800157 Texture* getEntryTexture(const SkPixelRef* pixelRef) const;
Romain Guy3b748a42013-04-17 18:54:38 -0700158
159private:
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000160 void createEntries(Caches& caches, int64_t* map, int count);
Romain Guy3b748a42013-04-17 18:54:38 -0700161
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
Romain Guy7f6d6b02013-08-06 13:49:28 -0700165 const bool mBlendKey;
166 const bool mOpaqueKey;
167
John Reck38e0c322015-11-10 12:19:17 -0800168 std::unordered_map<const SkPixelRef*, std::unique_ptr<Entry>> mEntries;
Romain Guy3b748a42013-04-17 18:54:38 -0700169}; // class AssetAtlas
170
171}; // namespace uirenderer
172}; // namespace android
173
174#endif // ANDROID_HWUI_ASSET_ATLAS_H