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 | |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 20 | #include <GLES3/gl3.h> |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 21 | |
| 22 | #include <SkScalerContext.h> |
| 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include "FontUtil.h" |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 27 | #include "../Rect.h" |
| 28 | #include "../Vertex.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 33 | class Caches; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 34 | class PixelBuffer; |
| 35 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 36 | /** |
| 37 | * CacheBlock is a node in a linked list of current free space areas in a CacheTexture. |
| 38 | * Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right. |
| 39 | * When we add a glyph to the cache, we see if it fits within one of the existing columns that |
| 40 | * have already been started (this is the case if the glyph fits vertically as well as |
| 41 | * horizontally, and if its width is sufficiently close to the column width to avoid |
| 42 | * sub-optimal packing of small glyphs into wide columns). If there is no column in which the |
| 43 | * glyph fits, we check the final node, which is the remaining space in the cache, creating |
| 44 | * a new column as appropriate. |
| 45 | * |
| 46 | * As columns fill up, we remove their CacheBlock from the list to avoid having to check |
| 47 | * small blocks in the future. |
| 48 | */ |
| 49 | struct CacheBlock { |
| 50 | uint16_t mX; |
| 51 | uint16_t mY; |
| 52 | uint16_t mWidth; |
| 53 | uint16_t mHeight; |
| 54 | CacheBlock* mNext; |
| 55 | CacheBlock* mPrev; |
| 56 | |
| 57 | 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] | 58 | mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 61 | static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock); |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 62 | static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 63 | |
| 64 | void output() { |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 65 | CacheBlock* currBlock = this; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 66 | while (currBlock) { |
| 67 | ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d", |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 68 | currBlock, currBlock->mX, currBlock->mY, |
| 69 | currBlock->mWidth, currBlock->mHeight); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 70 | currBlock = currBlock->mNext; |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | class CacheTexture { |
| 76 | public: |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 77 | CacheTexture(uint16_t width, uint16_t height, uint32_t maxQuadCount); |
| 78 | ~CacheTexture(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 79 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 80 | void reset(); |
| 81 | void init(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 82 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 83 | void releaseMesh(); |
| 84 | void releaseTexture(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 85 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 86 | void allocateTexture(); |
| 87 | void allocateMesh(); |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 88 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 89 | // Returns true if glPixelStorei(GL_UNPACK_ROW_LENGTH) must be reset |
| 90 | // This method will also call setDirty(false) |
| 91 | bool upload(); |
| 92 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 93 | bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 94 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 95 | inline uint16_t getWidth() const { |
| 96 | return mWidth; |
| 97 | } |
| 98 | |
| 99 | inline uint16_t getHeight() const { |
| 100 | return mHeight; |
| 101 | } |
| 102 | |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 103 | inline const Rect* getDirtyRect() const { |
| 104 | return &mDirtyRect; |
| 105 | } |
| 106 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 107 | inline PixelBuffer* getPixelBuffer() const { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 108 | return mTexture; |
| 109 | } |
| 110 | |
Romain Guy | 574cf60 | 2012-09-23 14:45:31 -0700 | [diff] [blame] | 111 | GLuint getTextureId() { |
| 112 | allocateTexture(); |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 113 | return mTextureId; |
| 114 | } |
| 115 | |
| 116 | inline bool isDirty() const { |
| 117 | return mDirty; |
| 118 | } |
| 119 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 120 | inline bool getLinearFiltering() const { |
| 121 | return mLinearFiltering; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * This method assumes that the proper texture unit is active. |
| 126 | */ |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 127 | void setLinearFiltering(bool linearFiltering, bool bind = true); |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 128 | |
| 129 | inline uint16_t getGlyphCount() const { |
| 130 | return mNumGlyphs; |
| 131 | } |
| 132 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 133 | TextureVertex* mesh() const { |
| 134 | return mMesh; |
| 135 | } |
| 136 | |
| 137 | uint32_t meshElementCount() const { |
| 138 | return mCurrentQuad * 6; |
| 139 | } |
| 140 | |
| 141 | uint16_t* indices() const { |
| 142 | return (uint16_t*) 0; |
| 143 | } |
| 144 | |
| 145 | void resetMesh() { |
| 146 | mCurrentQuad = 0; |
| 147 | } |
| 148 | |
| 149 | inline void addQuad(float x1, float y1, float u1, float v1, |
| 150 | float x2, float y2, float u2, float v2, |
| 151 | float x3, float y3, float u3, float v3, |
| 152 | float x4, float y4, float u4, float v4) { |
| 153 | TextureVertex* mesh = mMesh + mCurrentQuad * 4; |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 154 | TextureVertex::set(mesh++, x2, y2, u2, v2); |
| 155 | TextureVertex::set(mesh++, x3, y3, u3, v3); |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame^] | 156 | TextureVertex::set(mesh++, x1, y1, u1, v1); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 157 | TextureVertex::set(mesh++, x4, y4, u4, v4); |
| 158 | mCurrentQuad++; |
| 159 | } |
| 160 | |
| 161 | bool canDraw() const { |
| 162 | return mCurrentQuad > 0; |
| 163 | } |
| 164 | |
| 165 | bool endOfMesh() const { |
| 166 | return mCurrentQuad == mMaxQuadCount; |
| 167 | } |
| 168 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 169 | private: |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 170 | void setDirty(bool dirty); |
| 171 | |
| 172 | PixelBuffer* mTexture; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 173 | GLuint mTextureId; |
| 174 | uint16_t mWidth; |
| 175 | uint16_t mHeight; |
| 176 | bool mLinearFiltering; |
| 177 | bool mDirty; |
| 178 | uint16_t mNumGlyphs; |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 179 | TextureVertex* mMesh; |
| 180 | uint32_t mCurrentQuad; |
| 181 | uint32_t mMaxQuadCount; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 182 | Caches& mCaches; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 183 | CacheBlock* mCacheBlocks; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 184 | bool mHasES3; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 185 | Rect mDirtyRect; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | }; // namespace uirenderer |
| 189 | }; // namespace android |
| 190 | |
| 191 | #endif // ANDROID_HWUI_CACHE_TEXTURE_H |