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