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 | |
Derek Sollenberger | ca79cf6 | 2012-08-14 16:44:52 -0400 | [diff] [blame] | 17 | #include <SkGlyph.h> |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 18 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 19 | #include "../Caches.h" |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 20 | #include "../Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 21 | #include "../Extensions.h" |
| 22 | #include "../PixelBuffer.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | #include "CacheTexture.h" |
| 24 | #include "FontUtil.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // CacheBlock |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | /** |
| 34 | * Insert new block into existing linked list of blocks. Blocks are sorted in increasing-width |
| 35 | * order, except for the final block (the remainder space at the right, since we fill from the |
| 36 | * left). |
| 37 | */ |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 38 | CacheBlock* CacheBlock::insertBlock(CacheBlock* head, CacheBlock* newBlock) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 39 | #if DEBUG_FONT_RENDERER |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 40 | ALOGD("insertBlock: this, x, y, w, h = %p, %d, %d, %d, %d", newBlock, newBlock->mX, |
| 41 | newBlock->mY, newBlock->mWidth, newBlock->mHeight); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 42 | #endif |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 43 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 44 | CacheBlock* currBlock = head; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 45 | CacheBlock* prevBlock = nullptr; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 46 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 47 | while (currBlock && currBlock->mY != TEXTURE_BORDER_SIZE) { |
| 48 | if (newBlock->mWidth < currBlock->mWidth) { |
| 49 | newBlock->mNext = currBlock; |
| 50 | newBlock->mPrev = prevBlock; |
| 51 | currBlock->mPrev = newBlock; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 52 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 53 | if (prevBlock) { |
| 54 | prevBlock->mNext = newBlock; |
| 55 | return head; |
| 56 | } else { |
| 57 | return newBlock; |
| 58 | } |
| 59 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 60 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 61 | prevBlock = currBlock; |
| 62 | currBlock = currBlock->mNext; |
| 63 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 64 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 65 | // new block larger than all others - insert at end (but before the remainder space, if there) |
| 66 | newBlock->mNext = currBlock; |
| 67 | newBlock->mPrev = prevBlock; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 68 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 69 | if (currBlock) { |
| 70 | currBlock->mPrev = newBlock; |
| 71 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 72 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 73 | if (prevBlock) { |
| 74 | prevBlock->mNext = newBlock; |
| 75 | return head; |
| 76 | } else { |
| 77 | return newBlock; |
| 78 | } |
| 79 | } |
| 80 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 81 | CacheBlock* CacheBlock::removeBlock(CacheBlock* head, CacheBlock* blockToRemove) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 82 | #if DEBUG_FONT_RENDERER |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 | ALOGD("removeBlock: this, x, y, w, h = %p, %d, %d, %d, %d", blockToRemove, blockToRemove->mX, |
| 84 | blockToRemove->mY, blockToRemove->mWidth, blockToRemove->mHeight); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 85 | #endif |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 86 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 87 | CacheBlock* newHead = head; |
| 88 | CacheBlock* nextBlock = blockToRemove->mNext; |
| 89 | CacheBlock* prevBlock = blockToRemove->mPrev; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 90 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 91 | if (prevBlock) { |
George Burgess IV | fa4eaa6 | 2017-07-18 16:28:28 -0700 | [diff] [blame] | 92 | // If this doesn't hold, we have a use-after-free below. |
| 93 | LOG_ALWAYS_FATAL_IF(head == blockToRemove, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | "removeBlock: head should not have a previous block"); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 95 | prevBlock->mNext = nextBlock; |
| 96 | } else { |
| 97 | newHead = nextBlock; |
| 98 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 99 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 100 | if (nextBlock) { |
| 101 | nextBlock->mPrev = prevBlock; |
| 102 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 103 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 104 | delete blockToRemove; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 105 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 106 | return newHead; |
| 107 | } |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | // CacheTexture |
| 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 113 | CacheTexture::CacheTexture(uint16_t width, uint16_t height, GLenum format, uint32_t maxQuadCount) |
| 114 | : mTexture(Caches::getInstance()) |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 115 | , mWidth(width) |
| 116 | , mHeight(height) |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 117 | , mFormat(format) |
| 118 | , mMaxQuadCount(maxQuadCount) |
| 119 | , mCaches(Caches::getInstance()) { |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 120 | mTexture.blend = true; |
| 121 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | mCacheBlocks = |
| 123 | new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 124 | getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 125 | |
| 126 | // OpenGL ES 3.0+ lets us specify the row length for unpack operations such |
| 127 | // as glTexSubImage2D(). This allows us to upload a sub-rectangle of a texture. |
| 128 | // With OpenGL ES 2.0 we have to upload entire stripes instead. |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 129 | mHasUnpackRowLength = mCaches.extensions().hasUnpackRowLength(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | CacheTexture::~CacheTexture() { |
| 133 | releaseMesh(); |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 134 | releasePixelBuffer(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 135 | reset(); |
| 136 | } |
| 137 | |
| 138 | void CacheTexture::reset() { |
| 139 | // Delete existing cache blocks |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 140 | while (mCacheBlocks != nullptr) { |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 141 | CacheBlock* tmpBlock = mCacheBlocks; |
| 142 | mCacheBlocks = mCacheBlocks->mNext; |
| 143 | delete tmpBlock; |
| 144 | } |
| 145 | mNumGlyphs = 0; |
| 146 | mCurrentQuad = 0; |
| 147 | } |
| 148 | |
| 149 | void CacheTexture::init() { |
| 150 | // reset, then create a new remainder space to start again |
| 151 | reset(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | mCacheBlocks = |
| 153 | new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 154 | getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void CacheTexture::releaseMesh() { |
| 158 | delete[] mMesh; |
| 159 | } |
| 160 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 161 | void CacheTexture::releasePixelBuffer() { |
| 162 | if (mPixelBuffer) { |
| 163 | delete mPixelBuffer; |
| 164 | mPixelBuffer = nullptr; |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 165 | } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 166 | mTexture.deleteTexture(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 167 | mDirty = false; |
| 168 | mCurrentQuad = 0; |
| 169 | } |
| 170 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 171 | void CacheTexture::setLinearFiltering(bool linearFiltering) { |
| 172 | mTexture.setFilter(linearFiltering ? GL_LINEAR : GL_NEAREST); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 175 | void CacheTexture::allocateMesh() { |
| 176 | if (!mMesh) { |
| 177 | mMesh = new TextureVertex[mMaxQuadCount * 4]; |
| 178 | } |
| 179 | } |
| 180 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 181 | void CacheTexture::allocatePixelBuffer() { |
| 182 | if (!mPixelBuffer) { |
| 183 | mPixelBuffer = PixelBuffer::create(mFormat, getWidth(), getHeight()); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 186 | GLint internalFormat = mFormat; |
| 187 | if (mFormat == GL_RGBA) { |
| 188 | internalFormat = mCaches.rgbaInternalFormat(); |
| 189 | } |
| 190 | |
| 191 | mTexture.resize(mWidth, mHeight, internalFormat, mFormat); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 192 | mTexture.setFilter(getLinearFiltering() ? GL_LINEAR : GL_NEAREST); |
| 193 | mTexture.setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 196 | bool CacheTexture::upload() { |
| 197 | const Rect& dirtyRect = mDirtyRect; |
| 198 | |
ywen | 229cad0 | 2016-02-15 16:09:40 +0800 | [diff] [blame] | 199 | // align the x direction to 32 and y direction to 4 for better performance |
| 200 | uint32_t x = (((uint32_t)dirtyRect.left) & (~0x1F)); |
| 201 | uint32_t y = (((uint32_t)dirtyRect.top) & (~0x3)); |
| 202 | uint32_t r = ((((uint32_t)dirtyRect.right) + 0x1F) & (~0x1F)) - x; |
| 203 | uint32_t b = ((((uint32_t)dirtyRect.bottom) + 0x3) & (~0x3)) - y; |
| 204 | uint32_t width = (r > getWidth() ? getWidth() : r); |
| 205 | uint32_t height = (b > getHeight() ? getHeight() : b); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 206 | |
| 207 | // The unpack row length only needs to be specified when a new |
| 208 | // texture is bound |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 209 | if (mHasUnpackRowLength) { |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 210 | glPixelStorei(GL_UNPACK_ROW_LENGTH, getWidth()); |
ywen | 229cad0 | 2016-02-15 16:09:40 +0800 | [diff] [blame] | 211 | } else { |
| 212 | x = 0; |
| 213 | width = getWidth(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 216 | mPixelBuffer->upload(x, y, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 217 | setDirty(false); |
| 218 | |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 219 | return mHasUnpackRowLength; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void CacheTexture::setDirty(bool dirty) { |
| 223 | mDirty = dirty; |
| 224 | if (!dirty) { |
| 225 | mDirtyRect.setEmpty(); |
| 226 | } |
| 227 | } |
| 228 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 229 | bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 230 | switch (glyph.fMaskFormat) { |
| 231 | case SkMask::kA8_Format: |
Victoria Lease | 723b2fe | 2013-08-12 14:38:44 -0700 | [diff] [blame] | 232 | case SkMask::kBW_Format: |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 233 | if (mFormat != GL_ALPHA) { |
| 234 | #if DEBUG_FONT_RENDERER |
Victoria Lease | 723b2fe | 2013-08-12 14:38:44 -0700 | [diff] [blame] | 235 | ALOGD("fitBitmap: texture format %x is inappropriate for monochromatic glyphs", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 236 | mFormat); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 237 | #endif |
| 238 | return false; |
| 239 | } |
| 240 | break; |
| 241 | case SkMask::kARGB32_Format: |
| 242 | if (mFormat != GL_RGBA) { |
| 243 | #if DEBUG_FONT_RENDERER |
Victoria Lease | 723b2fe | 2013-08-12 14:38:44 -0700 | [diff] [blame] | 244 | ALOGD("fitBitmap: texture format %x is inappropriate for colour glyphs", mFormat); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 245 | #endif |
| 246 | return false; |
| 247 | } |
| 248 | break; |
| 249 | default: |
| 250 | #if DEBUG_FONT_RENDERER |
| 251 | ALOGD("fitBitmap: unknown glyph format %x encountered", glyph.fMaskFormat); |
| 252 | #endif |
| 253 | return false; |
| 254 | } |
| 255 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 256 | if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > getHeight()) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
| 260 | uint16_t glyphW = glyph.fWidth + TEXTURE_BORDER_SIZE; |
| 261 | uint16_t glyphH = glyph.fHeight + TEXTURE_BORDER_SIZE; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 262 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 263 | // roundedUpW equals glyphW to the next multiple of CACHE_BLOCK_ROUNDING_SIZE. |
| 264 | // This columns for glyphs that are close but not necessarily exactly the same size. It trades |
| 265 | // off the loss of a few pixels for some glyphs against the ability to store more glyphs |
| 266 | // of varying sizes in one block. |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 267 | uint16_t roundedUpW = (glyphW + CACHE_BLOCK_ROUNDING_SIZE - 1) & -CACHE_BLOCK_ROUNDING_SIZE; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 268 | |
Romain Guy | e43f785 | 2012-09-04 18:58:46 -0700 | [diff] [blame] | 269 | CacheBlock* cacheBlock = mCacheBlocks; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 270 | while (cacheBlock) { |
| 271 | // Store glyph in this block iff: it fits the block's remaining space and: |
| 272 | // it's the remainder space (mY == 0) or there's only enough height for this one glyph |
| 273 | // or it's within ROUNDING_SIZE of the block width |
| 274 | if (roundedUpW <= cacheBlock->mWidth && glyphH <= cacheBlock->mHeight && |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 275 | (cacheBlock->mY == TEXTURE_BORDER_SIZE || |
| 276 | (cacheBlock->mWidth - roundedUpW < CACHE_BLOCK_ROUNDING_SIZE))) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 277 | if (cacheBlock->mHeight - glyphH < glyphH) { |
| 278 | // Only enough space for this glyph - don't bother rounding up the width |
| 279 | roundedUpW = glyphW; |
| 280 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 281 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 282 | *retOriginX = cacheBlock->mX; |
| 283 | *retOriginY = cacheBlock->mY; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 284 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 285 | // If this is the remainder space, create a new cache block for this column. Otherwise, |
| 286 | // adjust the info about this column. |
| 287 | if (cacheBlock->mY == TEXTURE_BORDER_SIZE) { |
| 288 | uint16_t oldX = cacheBlock->mX; |
| 289 | // Adjust remainder space dimensions |
| 290 | cacheBlock->mWidth -= roundedUpW; |
| 291 | cacheBlock->mX += roundedUpW; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 292 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 293 | if (getHeight() - glyphH >= glyphH) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 294 | // There's enough height left over to create a new CacheBlock |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 295 | CacheBlock* newBlock = |
| 296 | new CacheBlock(oldX, glyphH + TEXTURE_BORDER_SIZE, roundedUpW, |
| 297 | getHeight() - glyphH - TEXTURE_BORDER_SIZE); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 298 | #if DEBUG_FONT_RENDERER |
| 299 | ALOGD("fitBitmap: Created new block: this, x, y, w, h = %p, %d, %d, %d, %d", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 300 | newBlock, newBlock->mX, newBlock->mY, newBlock->mWidth, |
| 301 | newBlock->mHeight); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 302 | #endif |
| 303 | mCacheBlocks = CacheBlock::insertBlock(mCacheBlocks, newBlock); |
| 304 | } |
| 305 | } else { |
| 306 | // Insert into current column and adjust column dimensions |
| 307 | cacheBlock->mY += glyphH; |
| 308 | cacheBlock->mHeight -= glyphH; |
| 309 | #if DEBUG_FONT_RENDERER |
| 310 | ALOGD("fitBitmap: Added to existing block: this, x, y, w, h = %p, %d, %d, %d, %d", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 311 | cacheBlock, cacheBlock->mX, cacheBlock->mY, cacheBlock->mWidth, |
| 312 | cacheBlock->mHeight); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 313 | #endif |
| 314 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 315 | |
Chris Craik | e6a15ee | 2015-07-07 18:42:17 -0700 | [diff] [blame] | 316 | if (cacheBlock->mHeight < std::min(glyphH, glyphW)) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 317 | // If remaining space in this block is too small to be useful, remove it |
| 318 | mCacheBlocks = CacheBlock::removeBlock(mCacheBlocks, cacheBlock); |
| 319 | } |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 320 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 321 | mDirty = true; |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 322 | const Rect r(*retOriginX - TEXTURE_BORDER_SIZE, *retOriginY - TEXTURE_BORDER_SIZE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 323 | *retOriginX + glyphW, *retOriginY + glyphH); |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 324 | mDirtyRect.unionWith(r); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 325 | mNumGlyphs++; |
| 326 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 327 | #if DEBUG_FONT_RENDERER |
| 328 | ALOGD("fitBitmap: current block list:"); |
| 329 | mCacheBlocks->output(); |
| 330 | #endif |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 331 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | cacheBlock = cacheBlock->mNext; |
| 335 | } |
| 336 | #if DEBUG_FONT_RENDERER |
| 337 | ALOGD("fitBitmap: returning false for glyph of size %d, %d", glyphW, glyphH); |
| 338 | #endif |
| 339 | return false; |
| 340 | } |
| 341 | |
sergeyv | baf29e7 | 2016-09-08 11:09:34 -0700 | [diff] [blame] | 342 | uint32_t CacheTexture::calculateFreeMemory() const { |
| 343 | CacheBlock* cacheBlock = mCacheBlocks; |
| 344 | uint32_t free = 0; |
| 345 | // currently only two formats are supported: GL_ALPHA or GL_RGBA; |
| 346 | uint32_t bpp = mFormat == GL_RGBA ? 4 : 1; |
| 347 | while (cacheBlock) { |
| 348 | free += bpp * cacheBlock->mWidth * cacheBlock->mHeight; |
| 349 | cacheBlock = cacheBlock->mNext; |
| 350 | } |
| 351 | return free; |
| 352 | } |
| 353 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 354 | }; // namespace uirenderer |
| 355 | }; // namespace android |