Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_CACHE_TEXTURE_H |
| 18 | #define ANDROID_HWUI_CACHE_TEXTURE_H |
| 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | #include <SkScalerContext.h> |
| 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include "FontUtil.h" |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame^] | 27 | #include "Rect.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
| 32 | /** |
| 33 | * CacheBlock is a node in a linked list of current free space areas in a CacheTexture. |
| 34 | * Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right. |
| 35 | * When we add a glyph to the cache, we see if it fits within one of the existing columns that |
| 36 | * have already been started (this is the case if the glyph fits vertically as well as |
| 37 | * horizontally, and if its width is sufficiently close to the column width to avoid |
| 38 | * sub-optimal packing of small glyphs into wide columns). If there is no column in which the |
| 39 | * glyph fits, we check the final node, which is the remaining space in the cache, creating |
| 40 | * a new column as appropriate. |
| 41 | * |
| 42 | * As columns fill up, we remove their CacheBlock from the list to avoid having to check |
| 43 | * small blocks in the future. |
| 44 | */ |
| 45 | struct CacheBlock { |
| 46 | uint16_t mX; |
| 47 | uint16_t mY; |
| 48 | uint16_t mWidth; |
| 49 | uint16_t mHeight; |
| 50 | CacheBlock* mNext; |
| 51 | CacheBlock* mPrev; |
| 52 | |
| 53 | CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool empty = false): |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 54 | mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 57 | static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 58 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 59 | static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 60 | |
| 61 | void output() { |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 62 | CacheBlock* currBlock = this; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 63 | while (currBlock) { |
| 64 | ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d", |
| 65 | currBlock, currBlock->mX, currBlock->mY, currBlock->mWidth, currBlock->mHeight); |
| 66 | currBlock = currBlock->mNext; |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | class CacheTexture { |
| 72 | public: |
| 73 | CacheTexture(uint16_t width, uint16_t height) : |
| 74 | mTexture(NULL), mTextureId(0), mWidth(width), mHeight(height), |
| 75 | mLinearFiltering(false), mDirty(false), mNumGlyphs(0) { |
| 76 | mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 77 | mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true); |
| 78 | } |
| 79 | |
| 80 | ~CacheTexture() { |
| 81 | if (mTexture) { |
| 82 | delete[] mTexture; |
| 83 | } |
| 84 | if (mTextureId) { |
| 85 | glDeleteTextures(1, &mTextureId); |
| 86 | } |
| 87 | reset(); |
| 88 | } |
| 89 | |
| 90 | void reset() { |
| 91 | // Delete existing cache blocks |
| 92 | while (mCacheBlocks != NULL) { |
| 93 | CacheBlock* tmpBlock = mCacheBlocks; |
| 94 | mCacheBlocks = mCacheBlocks->mNext; |
| 95 | delete tmpBlock; |
| 96 | } |
| 97 | mNumGlyphs = 0; |
| 98 | } |
| 99 | |
| 100 | void init() { |
| 101 | // reset, then create a new remainder space to start again |
| 102 | reset(); |
| 103 | mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 104 | mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true); |
| 105 | } |
| 106 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 107 | void releaseTexture() { |
| 108 | if (mTexture) { |
| 109 | glDeleteTextures(1, &mTextureId); |
| 110 | delete[] mTexture; |
| 111 | mTexture = NULL; |
| 112 | mTextureId = 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * This method assumes that the proper texture unit is active. |
| 118 | */ |
| 119 | void allocateTexture() { |
| 120 | int width = mWidth; |
| 121 | int height = mHeight; |
| 122 | |
| 123 | mTexture = new uint8_t[width * height]; |
| 124 | |
| 125 | if (!mTextureId) { |
| 126 | glGenTextures(1, &mTextureId); |
| 127 | } |
| 128 | |
| 129 | glBindTexture(GL_TEXTURE_2D, mTextureId); |
| 130 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 131 | // Initialize texture dimensions |
| 132 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, |
| 133 | GL_ALPHA, GL_UNSIGNED_BYTE, 0); |
| 134 | |
| 135 | const GLenum filtering = getLinearFiltering() ? GL_LINEAR : GL_NEAREST; |
| 136 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); |
| 137 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); |
| 138 | |
| 139 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 140 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 141 | } |
| 142 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 143 | bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 144 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 145 | inline uint16_t getWidth() const { |
| 146 | return mWidth; |
| 147 | } |
| 148 | |
| 149 | inline uint16_t getHeight() const { |
| 150 | return mHeight; |
| 151 | } |
| 152 | |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame^] | 153 | inline const Rect* getDirtyRect() const { |
| 154 | return &mDirtyRect; |
| 155 | } |
| 156 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 157 | inline uint8_t* getTexture() const { |
| 158 | return mTexture; |
| 159 | } |
| 160 | |
| 161 | inline GLuint getTextureId() const { |
| 162 | return mTextureId; |
| 163 | } |
| 164 | |
| 165 | inline bool isDirty() const { |
| 166 | return mDirty; |
| 167 | } |
| 168 | |
| 169 | inline void setDirty(bool dirty) { |
| 170 | mDirty = dirty; |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame^] | 171 | if (!dirty) { |
| 172 | mDirtyRect.setEmpty(); |
| 173 | } |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | inline bool getLinearFiltering() const { |
| 177 | return mLinearFiltering; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * This method assumes that the proper texture unit is active. |
| 182 | */ |
| 183 | void setLinearFiltering(bool linearFiltering, bool bind = true) { |
| 184 | if (linearFiltering != mLinearFiltering) { |
| 185 | mLinearFiltering = linearFiltering; |
| 186 | |
| 187 | const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST; |
| 188 | if (bind) glBindTexture(GL_TEXTURE_2D, getTextureId()); |
| 189 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); |
| 190 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | inline uint16_t getGlyphCount() const { |
| 195 | return mNumGlyphs; |
| 196 | } |
| 197 | |
| 198 | private: |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 199 | uint8_t* mTexture; |
| 200 | GLuint mTextureId; |
| 201 | uint16_t mWidth; |
| 202 | uint16_t mHeight; |
| 203 | bool mLinearFiltering; |
| 204 | bool mDirty; |
| 205 | uint16_t mNumGlyphs; |
| 206 | CacheBlock* mCacheBlocks; |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame^] | 207 | Rect mDirtyRect; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | }; // namespace uirenderer |
| 211 | }; // namespace android |
| 212 | |
| 213 | #endif // ANDROID_HWUI_CACHE_TEXTURE_H |