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