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 "PixelBuffer.h" 21 #include "Rect.h" 22 #include "Texture.h" 23 #include "Vertex.h" 24 25 #include <GLES3/gl3.h> 26 #include <SkGlyph.h> 27 #include <utils/Log.h> 28 29 namespace android { 30 namespace uirenderer { 31 32 class Caches; 33 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 CacheBlockCacheBlock55 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) {} 57 58 static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock); 59 static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove); 60 outputCacheBlock61 void output() { 62 CacheBlock* currBlock = this; 63 while (currBlock) { 64 ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d", currBlock, currBlock->mX, 65 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, GLenum format, uint32_t maxQuadCount); 74 ~CacheTexture(); 75 76 void reset(); 77 void init(); 78 79 void releaseMesh(); 80 void releasePixelBuffer(); 81 82 void allocatePixelBuffer(); 83 void allocateMesh(); 84 85 // Returns true if glPixelStorei(GL_UNPACK_ROW_LENGTH) must be reset 86 // This method will also call setDirty(false) 87 bool upload(); 88 89 bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY); 90 getWidth()91 inline uint16_t getWidth() const { return mWidth; } 92 getHeight()93 inline uint16_t getHeight() const { return mHeight; } 94 getFormat()95 inline GLenum getFormat() const { return mFormat; } 96 getOffset(uint16_t x,uint16_t y)97 inline uint32_t getOffset(uint16_t x, uint16_t y) const { 98 return (y * getWidth() + x) * PixelBuffer::formatSize(mFormat); 99 } 100 getDirtyRect()101 inline const Rect* getDirtyRect() const { return &mDirtyRect; } 102 getPixelBuffer()103 inline PixelBuffer* getPixelBuffer() const { return mPixelBuffer; } 104 getTexture()105 Texture& getTexture() { 106 allocatePixelBuffer(); 107 return mTexture; 108 } 109 getTextureId()110 GLuint getTextureId() { 111 allocatePixelBuffer(); 112 return mTexture.id(); 113 } 114 isDirty()115 inline bool isDirty() const { return mDirty; } 116 getLinearFiltering()117 inline bool getLinearFiltering() const { return mLinearFiltering; } 118 119 /** 120 * This method assumes that the proper texture unit is active. 121 */ 122 void setLinearFiltering(bool linearFiltering); 123 getGlyphCount()124 inline uint16_t getGlyphCount() const { return mNumGlyphs; } 125 mesh()126 TextureVertex* mesh() const { return mMesh; } 127 meshElementCount()128 uint32_t meshElementCount() const { return mCurrentQuad * 6; } 129 indices()130 uint16_t* indices() const { return (uint16_t*)nullptr; } 131 resetMesh()132 void resetMesh() { mCurrentQuad = 0; } 133 addQuad(float x1,float y1,float u1,float v1,float x2,float y2,float u2,float v2,float x3,float y3,float u3,float v3,float x4,float y4,float u4,float v4)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) { 137 TextureVertex* mesh = mMesh + mCurrentQuad * 4; 138 TextureVertex::set(mesh++, x2, y2, u2, v2); 139 TextureVertex::set(mesh++, x3, y3, u3, v3); 140 TextureVertex::set(mesh++, x1, y1, u1, v1); 141 TextureVertex::set(mesh++, x4, y4, u4, v4); 142 mCurrentQuad++; 143 } 144 canDraw()145 bool canDraw() const { return mCurrentQuad > 0; } 146 endOfMesh()147 bool endOfMesh() const { return mCurrentQuad == mMaxQuadCount; } 148 149 uint32_t calculateFreeMemory() const; 150 151 private: 152 void setDirty(bool dirty); 153 154 PixelBuffer* mPixelBuffer = nullptr; 155 Texture mTexture; 156 uint32_t mWidth, mHeight; 157 GLenum mFormat; 158 bool mLinearFiltering = false; 159 bool mDirty = false; 160 uint16_t mNumGlyphs = 0; 161 TextureVertex* mMesh = nullptr; 162 uint32_t mCurrentQuad = 0; 163 uint32_t mMaxQuadCount; 164 Caches& mCaches; 165 CacheBlock* mCacheBlocks; 166 bool mHasUnpackRowLength; 167 Rect mDirtyRect; 168 }; 169 170 }; // namespace uirenderer 171 }; // namespace android 172 173 #endif // ANDROID_HWUI_CACHE_TEXTURE_H 174