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