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 #include <SkGlyph.h>
18 
19 #include "CacheTexture.h"
20 #include "FontUtil.h"
21 #include "../Caches.h"
22 #include "../Debug.h"
23 #include "../Extensions.h"
24 #include "../PixelBuffer.h"
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  */
insertBlock(CacheBlock * head,CacheBlock * newBlock)38 CacheBlock* CacheBlock::insertBlock(CacheBlock* head, CacheBlock* newBlock) {
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
44 
45     CacheBlock* currBlock = head;
46     CacheBlock* prevBlock = nullptr;
47 
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;
53 
54             if (prevBlock) {
55                 prevBlock->mNext = newBlock;
56                 return head;
57             } else {
58                 return newBlock;
59             }
60         }
61 
62         prevBlock = currBlock;
63         currBlock = currBlock->mNext;
64     }
65 
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;
69 
70     if (currBlock) {
71         currBlock->mPrev = newBlock;
72     }
73 
74     if (prevBlock) {
75         prevBlock->mNext = newBlock;
76         return head;
77     } else {
78         return newBlock;
79     }
80 }
81 
removeBlock(CacheBlock * head,CacheBlock * blockToRemove)82 CacheBlock* CacheBlock::removeBlock(CacheBlock* head, CacheBlock* blockToRemove) {
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
88 
89     CacheBlock* newHead = head;
90     CacheBlock* nextBlock = blockToRemove->mNext;
91     CacheBlock* prevBlock = blockToRemove->mPrev;
92 
93     if (prevBlock) {
94         prevBlock->mNext = nextBlock;
95     } else {
96         newHead = nextBlock;
97     }
98 
99     if (nextBlock) {
100         nextBlock->mPrev = prevBlock;
101     }
102 
103     delete blockToRemove;
104 
105     return newHead;
106 }
107 
108 ///////////////////////////////////////////////////////////////////////////////
109 // CacheTexture
110 ///////////////////////////////////////////////////////////////////////////////
111 
CacheTexture(uint16_t width,uint16_t height,GLenum format,uint32_t maxQuadCount)112 CacheTexture::CacheTexture(uint16_t width, uint16_t height, GLenum format, uint32_t maxQuadCount)
113         : mTexture(Caches::getInstance())
114         , mWidth(width)
115         , mHeight(height)
116         , mFormat(format)
117         , mMaxQuadCount(maxQuadCount)
118         , mCaches(Caches::getInstance()) {
119     mTexture.blend = true;
120 
121     mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
122             getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE);
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.
127     mHasUnpackRowLength = mCaches.extensions().hasUnpackRowLength();
128 }
129 
~CacheTexture()130 CacheTexture::~CacheTexture() {
131     releaseMesh();
132     releasePixelBuffer();
133     reset();
134 }
135 
reset()136 void CacheTexture::reset() {
137     // Delete existing cache blocks
138     while (mCacheBlocks != nullptr) {
139         CacheBlock* tmpBlock = mCacheBlocks;
140         mCacheBlocks = mCacheBlocks->mNext;
141         delete tmpBlock;
142     }
143     mNumGlyphs = 0;
144     mCurrentQuad = 0;
145 }
146 
init()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,
151             getWidth() - TEXTURE_BORDER_SIZE, getHeight() - TEXTURE_BORDER_SIZE);
152 }
153 
releaseMesh()154 void CacheTexture::releaseMesh() {
155     delete[] mMesh;
156 }
157 
releasePixelBuffer()158 void CacheTexture::releasePixelBuffer() {
159     if (mPixelBuffer) {
160         delete mPixelBuffer;
161         mPixelBuffer = nullptr;
162     }
163     mTexture.deleteTexture();
164     mDirty = false;
165     mCurrentQuad = 0;
166 }
167 
setLinearFiltering(bool linearFiltering)168 void CacheTexture::setLinearFiltering(bool linearFiltering) {
169     mTexture.setFilter(linearFiltering ? GL_LINEAR : GL_NEAREST);
170 }
171 
allocateMesh()172 void CacheTexture::allocateMesh() {
173     if (!mMesh) {
174         mMesh = new TextureVertex[mMaxQuadCount * 4];
175     }
176 }
177 
allocatePixelBuffer()178 void CacheTexture::allocatePixelBuffer() {
179     if (!mPixelBuffer) {
180         mPixelBuffer = PixelBuffer::create(mFormat, getWidth(), getHeight());
181     }
182 
183     mTexture.resize(mWidth, mHeight, mFormat);
184     mTexture.setFilter(getLinearFiltering() ? GL_LINEAR : GL_NEAREST);
185     mTexture.setWrap(GL_CLAMP_TO_EDGE);
186 }
187 
upload()188 bool CacheTexture::upload() {
189     const Rect& dirtyRect = mDirtyRect;
190 
191     uint32_t x = mHasUnpackRowLength ? dirtyRect.left : 0;
192     uint32_t y = dirtyRect.top;
193     uint32_t width = mHasUnpackRowLength ? dirtyRect.getWidth() : getWidth();
194     uint32_t height = dirtyRect.getHeight();
195 
196     // The unpack row length only needs to be specified when a new
197     // texture is bound
198     if (mHasUnpackRowLength) {
199         glPixelStorei(GL_UNPACK_ROW_LENGTH, getWidth());
200     }
201 
202     mPixelBuffer->upload(x, y, width, height);
203     setDirty(false);
204 
205     return mHasUnpackRowLength;
206 }
207 
setDirty(bool dirty)208 void CacheTexture::setDirty(bool dirty) {
209     mDirty = dirty;
210     if (!dirty) {
211         mDirtyRect.setEmpty();
212     }
213 }
214 
fitBitmap(const SkGlyph & glyph,uint32_t * retOriginX,uint32_t * retOriginY)215 bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) {
216     switch (glyph.fMaskFormat) {
217         case SkMask::kA8_Format:
218         case SkMask::kBW_Format:
219             if (mFormat != GL_ALPHA) {
220 #if DEBUG_FONT_RENDERER
221                 ALOGD("fitBitmap: texture format %x is inappropriate for monochromatic glyphs",
222                         mFormat);
223 #endif
224                 return false;
225             }
226             break;
227         case SkMask::kARGB32_Format:
228             if (mFormat != GL_RGBA) {
229 #if DEBUG_FONT_RENDERER
230                 ALOGD("fitBitmap: texture format %x is inappropriate for colour glyphs", mFormat);
231 #endif
232                 return false;
233             }
234             break;
235         default:
236 #if DEBUG_FONT_RENDERER
237             ALOGD("fitBitmap: unknown glyph format %x encountered", glyph.fMaskFormat);
238 #endif
239             return false;
240     }
241 
242     if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > getHeight()) {
243         return false;
244     }
245 
246     uint16_t glyphW = glyph.fWidth + TEXTURE_BORDER_SIZE;
247     uint16_t glyphH = glyph.fHeight + TEXTURE_BORDER_SIZE;
248 
249     // roundedUpW equals glyphW to the next multiple of CACHE_BLOCK_ROUNDING_SIZE.
250     // This columns for glyphs that are close but not necessarily exactly the same size. It trades
251     // off the loss of a few pixels for some glyphs against the ability to store more glyphs
252     // of varying sizes in one block.
253     uint16_t roundedUpW = (glyphW + CACHE_BLOCK_ROUNDING_SIZE - 1) & -CACHE_BLOCK_ROUNDING_SIZE;
254 
255     CacheBlock* cacheBlock = mCacheBlocks;
256     while (cacheBlock) {
257         // Store glyph in this block iff: it fits the block's remaining space and:
258         // it's the remainder space (mY == 0) or there's only enough height for this one glyph
259         // or it's within ROUNDING_SIZE of the block width
260         if (roundedUpW <= cacheBlock->mWidth && glyphH <= cacheBlock->mHeight &&
261                 (cacheBlock->mY == TEXTURE_BORDER_SIZE ||
262                         (cacheBlock->mWidth - roundedUpW < CACHE_BLOCK_ROUNDING_SIZE))) {
263             if (cacheBlock->mHeight - glyphH < glyphH) {
264                 // Only enough space for this glyph - don't bother rounding up the width
265                 roundedUpW = glyphW;
266             }
267 
268             *retOriginX = cacheBlock->mX;
269             *retOriginY = cacheBlock->mY;
270 
271             // If this is the remainder space, create a new cache block for this column. Otherwise,
272             // adjust the info about this column.
273             if (cacheBlock->mY == TEXTURE_BORDER_SIZE) {
274                 uint16_t oldX = cacheBlock->mX;
275                 // Adjust remainder space dimensions
276                 cacheBlock->mWidth -= roundedUpW;
277                 cacheBlock->mX += roundedUpW;
278 
279                 if (getHeight() - glyphH >= glyphH) {
280                     // There's enough height left over to create a new CacheBlock
281                     CacheBlock* newBlock = new CacheBlock(oldX, glyphH + TEXTURE_BORDER_SIZE,
282                             roundedUpW, getHeight() - glyphH - TEXTURE_BORDER_SIZE);
283 #if DEBUG_FONT_RENDERER
284                     ALOGD("fitBitmap: Created new block: this, x, y, w, h = %p, %d, %d, %d, %d",
285                             newBlock, newBlock->mX, newBlock->mY,
286                             newBlock->mWidth, newBlock->mHeight);
287 #endif
288                     mCacheBlocks = CacheBlock::insertBlock(mCacheBlocks, newBlock);
289                 }
290             } else {
291                 // Insert into current column and adjust column dimensions
292                 cacheBlock->mY += glyphH;
293                 cacheBlock->mHeight -= glyphH;
294 #if DEBUG_FONT_RENDERER
295                 ALOGD("fitBitmap: Added to existing block: this, x, y, w, h = %p, %d, %d, %d, %d",
296                         cacheBlock, cacheBlock->mX, cacheBlock->mY,
297                         cacheBlock->mWidth, cacheBlock->mHeight);
298 #endif
299             }
300 
301             if (cacheBlock->mHeight < std::min(glyphH, glyphW)) {
302                 // If remaining space in this block is too small to be useful, remove it
303                 mCacheBlocks = CacheBlock::removeBlock(mCacheBlocks, cacheBlock);
304             }
305 
306             mDirty = true;
307             const Rect r(*retOriginX - TEXTURE_BORDER_SIZE, *retOriginY - TEXTURE_BORDER_SIZE,
308                     *retOriginX + glyphW, *retOriginY + glyphH);
309             mDirtyRect.unionWith(r);
310             mNumGlyphs++;
311 
312 #if DEBUG_FONT_RENDERER
313             ALOGD("fitBitmap: current block list:");
314             mCacheBlocks->output();
315 #endif
316 
317             return true;
318         }
319         cacheBlock = cacheBlock->mNext;
320     }
321 #if DEBUG_FONT_RENDERER
322     ALOGD("fitBitmap: returning false for glyph of size %d, %d", glyphW, glyphH);
323 #endif
324     return false;
325 }
326 
327 }; // namespace uirenderer
328 }; // namespace android
329