blob: e2844ad0649a4ac3ed60169802552b80ecb67c0d [file] [log] [blame]
Romain Guy9f5dab32012-09-04 12:55:44 -07001/*
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 Sollenbergerca79cf62012-08-14 16:44:52 -040017#include <SkGlyph.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070018
Romain Guy9f5dab32012-09-04 12:55:44 -070019#include "CacheTexture.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040020#include "FontUtil.h"
Romain Guy8aa195d2013-06-04 18:00:09 -070021#include "../Caches.h"
Romain Guy09087642013-04-04 12:27:54 -070022#include "../Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070023#include "../Extensions.h"
24#include "../PixelBuffer.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070025
26namespace android {
27namespace 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 Guye43f7852012-09-04 18:58:46 -070038CacheBlock* CacheBlock::insertBlock(CacheBlock* head, CacheBlock* newBlock) {
Romain Guy9f5dab32012-09-04 12:55:44 -070039#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 Guy9b1204b2012-09-04 15:22:57 -070044
Romain Guye43f7852012-09-04 18:58:46 -070045 CacheBlock* currBlock = head;
Chris Craikd41c4d82015-01-05 15:51:13 -080046 CacheBlock* prevBlock = nullptr;
Romain Guy9b1204b2012-09-04 15:22:57 -070047
Romain Guy9f5dab32012-09-04 12:55:44 -070048 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 Guy9b1204b2012-09-04 15:22:57 -070053
Romain Guy9f5dab32012-09-04 12:55:44 -070054 if (prevBlock) {
55 prevBlock->mNext = newBlock;
56 return head;
57 } else {
58 return newBlock;
59 }
60 }
Romain Guy9b1204b2012-09-04 15:22:57 -070061
Romain Guy9f5dab32012-09-04 12:55:44 -070062 prevBlock = currBlock;
63 currBlock = currBlock->mNext;
64 }
Romain Guy9b1204b2012-09-04 15:22:57 -070065
Romain Guy9f5dab32012-09-04 12:55:44 -070066 // 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 Guy9b1204b2012-09-04 15:22:57 -070069
Romain Guy9f5dab32012-09-04 12:55:44 -070070 if (currBlock) {
71 currBlock->mPrev = newBlock;
72 }
Romain Guy9b1204b2012-09-04 15:22:57 -070073
Romain Guy9f5dab32012-09-04 12:55:44 -070074 if (prevBlock) {
75 prevBlock->mNext = newBlock;
76 return head;
77 } else {
78 return newBlock;
79 }
80}
81
Romain Guye43f7852012-09-04 18:58:46 -070082CacheBlock* CacheBlock::removeBlock(CacheBlock* head, CacheBlock* blockToRemove) {
Romain Guy9f5dab32012-09-04 12:55:44 -070083#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 Guy9b1204b2012-09-04 15:22:57 -070088
Romain Guy9f5dab32012-09-04 12:55:44 -070089 CacheBlock* newHead = head;
90 CacheBlock* nextBlock = blockToRemove->mNext;
91 CacheBlock* prevBlock = blockToRemove->mPrev;
Romain Guy9b1204b2012-09-04 15:22:57 -070092
Romain Guy9f5dab32012-09-04 12:55:44 -070093 if (prevBlock) {
94 prevBlock->mNext = nextBlock;
95 } else {
96 newHead = nextBlock;
97 }
Romain Guy9b1204b2012-09-04 15:22:57 -070098
Romain Guy9f5dab32012-09-04 12:55:44 -070099 if (nextBlock) {
100 nextBlock->mPrev = prevBlock;
101 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700102
Romain Guy9f5dab32012-09-04 12:55:44 -0700103 delete blockToRemove;
Romain Guy9b1204b2012-09-04 15:22:57 -0700104
Romain Guy9f5dab32012-09-04 12:55:44 -0700105 return newHead;
106}
107
108///////////////////////////////////////////////////////////////////////////////
109// CacheTexture
110///////////////////////////////////////////////////////////////////////////////
111
Chris Craike2bb3802015-03-13 15:07:52 -0700112CacheTexture::CacheTexture(uint16_t width, uint16_t height, GLenum format, uint32_t maxQuadCount)
113 : mTexture(Caches::getInstance())
John Reck38e0c322015-11-10 12:19:17 -0800114 , mWidth(width)
115 , mHeight(height)
Chris Craike2bb3802015-03-13 15:07:52 -0700116 , mFormat(format)
117 , mMaxQuadCount(maxQuadCount)
118 , mCaches(Caches::getInstance()) {
Chris Craike2bb3802015-03-13 15:07:52 -0700119 mTexture.blend = true;
120
Romain Guy661a87e2013-03-19 15:24:36 -0700121 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
Chris Craike2bb3802015-03-13 15:07:52 -0700122 getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE);
Romain Guycf51a412013-04-08 19:40:31 -0700123
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 Craik117bdbc2015-02-05 10:12:38 -0800127 mHasUnpackRowLength = mCaches.extensions().hasUnpackRowLength();
Romain Guy661a87e2013-03-19 15:24:36 -0700128}
129
130CacheTexture::~CacheTexture() {
131 releaseMesh();
Chris Craike2bb3802015-03-13 15:07:52 -0700132 releasePixelBuffer();
Romain Guy661a87e2013-03-19 15:24:36 -0700133 reset();
134}
135
136void CacheTexture::reset() {
137 // Delete existing cache blocks
Chris Craikd41c4d82015-01-05 15:51:13 -0800138 while (mCacheBlocks != nullptr) {
Romain Guy661a87e2013-03-19 15:24:36 -0700139 CacheBlock* tmpBlock = mCacheBlocks;
140 mCacheBlocks = mCacheBlocks->mNext;
141 delete tmpBlock;
142 }
143 mNumGlyphs = 0;
144 mCurrentQuad = 0;
145}
146
147void 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 Craike2bb3802015-03-13 15:07:52 -0700151 getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE);
Romain Guy661a87e2013-03-19 15:24:36 -0700152}
153
154void CacheTexture::releaseMesh() {
155 delete[] mMesh;
156}
157
Chris Craike2bb3802015-03-13 15:07:52 -0700158void CacheTexture::releasePixelBuffer() {
159 if (mPixelBuffer) {
160 delete mPixelBuffer;
161 mPixelBuffer = nullptr;
Romain Guy661a87e2013-03-19 15:24:36 -0700162 }
John Reck38e0c322015-11-10 12:19:17 -0800163 mTexture.deleteTexture();
Romain Guy661a87e2013-03-19 15:24:36 -0700164 mDirty = false;
165 mCurrentQuad = 0;
166}
167
Chris Craike2bb3802015-03-13 15:07:52 -0700168void CacheTexture::setLinearFiltering(bool linearFiltering) {
169 mTexture.setFilter(linearFiltering ? GL_LINEAR : GL_NEAREST);
Romain Guycf51a412013-04-08 19:40:31 -0700170}
171
Romain Guy661a87e2013-03-19 15:24:36 -0700172void CacheTexture::allocateMesh() {
173 if (!mMesh) {
174 mMesh = new TextureVertex[mMaxQuadCount * 4];
175 }
176}
177
Chris Craike2bb3802015-03-13 15:07:52 -0700178void CacheTexture::allocatePixelBuffer() {
179 if (!mPixelBuffer) {
180 mPixelBuffer = PixelBuffer::create(mFormat, getWidth(), getHeight());
Romain Guy661a87e2013-03-19 15:24:36 -0700181 }
182
Romain Guy253f2c22016-09-28 17:34:42 -0700183 GLint internalFormat = mFormat;
184 if (mFormat == GL_RGBA) {
185 internalFormat = mCaches.rgbaInternalFormat();
186 }
187
188 mTexture.resize(mWidth, mHeight, internalFormat, mFormat);
John Reck38e0c322015-11-10 12:19:17 -0800189 mTexture.setFilter(getLinearFiltering() ? GL_LINEAR : GL_NEAREST);
190 mTexture.setWrap(GL_CLAMP_TO_EDGE);
Romain Guy661a87e2013-03-19 15:24:36 -0700191}
192
Romain Guycf51a412013-04-08 19:40:31 -0700193bool CacheTexture::upload() {
194 const Rect& dirtyRect = mDirtyRect;
195
ywen229cad02016-02-15 16:09:40 +0800196 // align the x direction to 32 and y direction to 4 for better performance
197 uint32_t x = (((uint32_t)dirtyRect.left) & (~0x1F));
198 uint32_t y = (((uint32_t)dirtyRect.top) & (~0x3));
199 uint32_t r = ((((uint32_t)dirtyRect.right) + 0x1F) & (~0x1F)) - x;
200 uint32_t b = ((((uint32_t)dirtyRect.bottom) + 0x3) & (~0x3)) - y;
201 uint32_t width = (r > getWidth() ? getWidth() : r);
202 uint32_t height = (b > getHeight() ? getHeight() : b);
Romain Guycf51a412013-04-08 19:40:31 -0700203
204 // The unpack row length only needs to be specified when a new
205 // texture is bound
Romain Guy318ae7b2013-09-24 18:44:54 -0700206 if (mHasUnpackRowLength) {
Chris Craike2bb3802015-03-13 15:07:52 -0700207 glPixelStorei(GL_UNPACK_ROW_LENGTH, getWidth());
ywen229cad02016-02-15 16:09:40 +0800208 } else {
209 x = 0;
210 width = getWidth();
Romain Guycf51a412013-04-08 19:40:31 -0700211 }
212
Chris Craike2bb3802015-03-13 15:07:52 -0700213 mPixelBuffer->upload(x, y, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700214 setDirty(false);
215
Romain Guy318ae7b2013-09-24 18:44:54 -0700216 return mHasUnpackRowLength;
Romain Guycf51a412013-04-08 19:40:31 -0700217}
218
219void CacheTexture::setDirty(bool dirty) {
220 mDirty = dirty;
221 if (!dirty) {
222 mDirtyRect.setEmpty();
223 }
224}
225
Romain Guye43f7852012-09-04 18:58:46 -0700226bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) {
Victoria Lease1e546812013-06-25 14:25:17 -0700227 switch (glyph.fMaskFormat) {
228 case SkMask::kA8_Format:
Victoria Lease723b2fe2013-08-12 14:38:44 -0700229 case SkMask::kBW_Format:
Victoria Lease1e546812013-06-25 14:25:17 -0700230 if (mFormat != GL_ALPHA) {
231#if DEBUG_FONT_RENDERER
Victoria Lease723b2fe2013-08-12 14:38:44 -0700232 ALOGD("fitBitmap: texture format %x is inappropriate for monochromatic glyphs",
233 mFormat);
Victoria Lease1e546812013-06-25 14:25:17 -0700234#endif
235 return false;
236 }
237 break;
238 case SkMask::kARGB32_Format:
239 if (mFormat != GL_RGBA) {
240#if DEBUG_FONT_RENDERER
Victoria Lease723b2fe2013-08-12 14:38:44 -0700241 ALOGD("fitBitmap: texture format %x is inappropriate for colour glyphs", mFormat);
Victoria Lease1e546812013-06-25 14:25:17 -0700242#endif
243 return false;
244 }
245 break;
246 default:
247#if DEBUG_FONT_RENDERER
248 ALOGD("fitBitmap: unknown glyph format %x encountered", glyph.fMaskFormat);
249#endif
250 return false;
251 }
252
Chris Craike2bb3802015-03-13 15:07:52 -0700253 if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > getHeight()) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700254 return false;
255 }
256
257 uint16_t glyphW = glyph.fWidth + TEXTURE_BORDER_SIZE;
258 uint16_t glyphH = glyph.fHeight + TEXTURE_BORDER_SIZE;
Romain Guy9b1204b2012-09-04 15:22:57 -0700259
Romain Guy9f5dab32012-09-04 12:55:44 -0700260 // roundedUpW equals glyphW to the next multiple of CACHE_BLOCK_ROUNDING_SIZE.
261 // This columns for glyphs that are close but not necessarily exactly the same size. It trades
262 // off the loss of a few pixels for some glyphs against the ability to store more glyphs
263 // of varying sizes in one block.
Romain Guye43f7852012-09-04 18:58:46 -0700264 uint16_t roundedUpW = (glyphW + CACHE_BLOCK_ROUNDING_SIZE - 1) & -CACHE_BLOCK_ROUNDING_SIZE;
Romain Guy9b1204b2012-09-04 15:22:57 -0700265
Romain Guye43f7852012-09-04 18:58:46 -0700266 CacheBlock* cacheBlock = mCacheBlocks;
Romain Guy9f5dab32012-09-04 12:55:44 -0700267 while (cacheBlock) {
268 // Store glyph in this block iff: it fits the block's remaining space and:
269 // it's the remainder space (mY == 0) or there's only enough height for this one glyph
270 // or it's within ROUNDING_SIZE of the block width
271 if (roundedUpW <= cacheBlock->mWidth && glyphH <= cacheBlock->mHeight &&
272 (cacheBlock->mY == TEXTURE_BORDER_SIZE ||
273 (cacheBlock->mWidth - roundedUpW < CACHE_BLOCK_ROUNDING_SIZE))) {
274 if (cacheBlock->mHeight - glyphH < glyphH) {
275 // Only enough space for this glyph - don't bother rounding up the width
276 roundedUpW = glyphW;
277 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700278
Romain Guy9f5dab32012-09-04 12:55:44 -0700279 *retOriginX = cacheBlock->mX;
280 *retOriginY = cacheBlock->mY;
Romain Guy9b1204b2012-09-04 15:22:57 -0700281
Romain Guy9f5dab32012-09-04 12:55:44 -0700282 // If this is the remainder space, create a new cache block for this column. Otherwise,
283 // adjust the info about this column.
284 if (cacheBlock->mY == TEXTURE_BORDER_SIZE) {
285 uint16_t oldX = cacheBlock->mX;
286 // Adjust remainder space dimensions
287 cacheBlock->mWidth -= roundedUpW;
288 cacheBlock->mX += roundedUpW;
Romain Guy9b1204b2012-09-04 15:22:57 -0700289
Chris Craike2bb3802015-03-13 15:07:52 -0700290 if (getHeight() - glyphH >= glyphH) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700291 // There's enough height left over to create a new CacheBlock
Romain Guye43f7852012-09-04 18:58:46 -0700292 CacheBlock* newBlock = new CacheBlock(oldX, glyphH + TEXTURE_BORDER_SIZE,
Chris Craike2bb3802015-03-13 15:07:52 -0700293 roundedUpW, getHeight() - glyphH - TEXTURE_BORDER_SIZE);
Romain Guy9f5dab32012-09-04 12:55:44 -0700294#if DEBUG_FONT_RENDERER
295 ALOGD("fitBitmap: Created new block: this, x, y, w, h = %p, %d, %d, %d, %d",
296 newBlock, newBlock->mX, newBlock->mY,
297 newBlock->mWidth, newBlock->mHeight);
298#endif
299 mCacheBlocks = CacheBlock::insertBlock(mCacheBlocks, newBlock);
300 }
301 } else {
302 // Insert into current column and adjust column dimensions
303 cacheBlock->mY += glyphH;
304 cacheBlock->mHeight -= glyphH;
305#if DEBUG_FONT_RENDERER
306 ALOGD("fitBitmap: Added to existing block: this, x, y, w, h = %p, %d, %d, %d, %d",
307 cacheBlock, cacheBlock->mX, cacheBlock->mY,
308 cacheBlock->mWidth, cacheBlock->mHeight);
309#endif
310 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700311
Chris Craike6a15ee2015-07-07 18:42:17 -0700312 if (cacheBlock->mHeight < std::min(glyphH, glyphW)) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700313 // If remaining space in this block is too small to be useful, remove it
314 mCacheBlocks = CacheBlock::removeBlock(mCacheBlocks, cacheBlock);
315 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700316
Romain Guy9f5dab32012-09-04 12:55:44 -0700317 mDirty = true;
Chet Haaseb92d8f72012-09-21 08:40:46 -0700318 const Rect r(*retOriginX - TEXTURE_BORDER_SIZE, *retOriginY - TEXTURE_BORDER_SIZE,
319 *retOriginX + glyphW, *retOriginY + glyphH);
320 mDirtyRect.unionWith(r);
Romain Guy9b1204b2012-09-04 15:22:57 -0700321 mNumGlyphs++;
322
Romain Guy9f5dab32012-09-04 12:55:44 -0700323#if DEBUG_FONT_RENDERER
324 ALOGD("fitBitmap: current block list:");
325 mCacheBlocks->output();
326#endif
Romain Guy9b1204b2012-09-04 15:22:57 -0700327
Romain Guy9f5dab32012-09-04 12:55:44 -0700328 return true;
329 }
330 cacheBlock = cacheBlock->mNext;
331 }
332#if DEBUG_FONT_RENDERER
333 ALOGD("fitBitmap: returning false for glyph of size %d, %d", glyphW, glyphH);
334#endif
335 return false;
336}
337
sergeyvbaf29e72016-09-08 11:09:34 -0700338uint32_t CacheTexture::calculateFreeMemory() const {
339 CacheBlock* cacheBlock = mCacheBlocks;
340 uint32_t free = 0;
341 // currently only two formats are supported: GL_ALPHA or GL_RGBA;
342 uint32_t bpp = mFormat == GL_RGBA ? 4 : 1;
343 while (cacheBlock) {
344 free += bpp * cacheBlock->mWidth * cacheBlock->mHeight;
345 cacheBlock = cacheBlock->mNext;
346 }
347 return free;
348}
349
Romain Guy9f5dab32012-09-04 12:55:44 -0700350}; // namespace uirenderer
351}; // namespace android