blob: 6c5267d338a70cfa84eca0a3f1de686aff694410 [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"
Romain Guy09087642013-04-04 12:27:54 -070020#include "../Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070021#include "../Extensions.h"
22#include "../PixelBuffer.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070023
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// CacheBlock
29///////////////////////////////////////////////////////////////////////////////
30
31/**
32 * Insert new block into existing linked list of blocks. Blocks are sorted in increasing-width
33 * order, except for the final block (the remainder space at the right, since we fill from the
34 * left).
35 */
Romain Guye43f7852012-09-04 18:58:46 -070036CacheBlock* CacheBlock::insertBlock(CacheBlock* head, CacheBlock* newBlock) {
Romain Guy9f5dab32012-09-04 12:55:44 -070037#if DEBUG_FONT_RENDERER
38 ALOGD("insertBlock: this, x, y, w, h = %p, %d, %d, %d, %d",
39 newBlock, newBlock->mX, newBlock->mY,
40 newBlock->mWidth, newBlock->mHeight);
41#endif
Romain Guy9b1204b2012-09-04 15:22:57 -070042
Romain Guye43f7852012-09-04 18:58:46 -070043 CacheBlock* currBlock = head;
44 CacheBlock* prevBlock = NULL;
Romain Guy9b1204b2012-09-04 15:22:57 -070045
Romain Guy9f5dab32012-09-04 12:55:44 -070046 while (currBlock && currBlock->mY != TEXTURE_BORDER_SIZE) {
47 if (newBlock->mWidth < currBlock->mWidth) {
48 newBlock->mNext = currBlock;
49 newBlock->mPrev = prevBlock;
50 currBlock->mPrev = newBlock;
Romain Guy9b1204b2012-09-04 15:22:57 -070051
Romain Guy9f5dab32012-09-04 12:55:44 -070052 if (prevBlock) {
53 prevBlock->mNext = newBlock;
54 return head;
55 } else {
56 return newBlock;
57 }
58 }
Romain Guy9b1204b2012-09-04 15:22:57 -070059
Romain Guy9f5dab32012-09-04 12:55:44 -070060 prevBlock = currBlock;
61 currBlock = currBlock->mNext;
62 }
Romain Guy9b1204b2012-09-04 15:22:57 -070063
Romain Guy9f5dab32012-09-04 12:55:44 -070064 // new block larger than all others - insert at end (but before the remainder space, if there)
65 newBlock->mNext = currBlock;
66 newBlock->mPrev = prevBlock;
Romain Guy9b1204b2012-09-04 15:22:57 -070067
Romain Guy9f5dab32012-09-04 12:55:44 -070068 if (currBlock) {
69 currBlock->mPrev = newBlock;
70 }
Romain Guy9b1204b2012-09-04 15:22:57 -070071
Romain Guy9f5dab32012-09-04 12:55:44 -070072 if (prevBlock) {
73 prevBlock->mNext = newBlock;
74 return head;
75 } else {
76 return newBlock;
77 }
78}
79
Romain Guye43f7852012-09-04 18:58:46 -070080CacheBlock* CacheBlock::removeBlock(CacheBlock* head, CacheBlock* blockToRemove) {
Romain Guy9f5dab32012-09-04 12:55:44 -070081#if DEBUG_FONT_RENDERER
82 ALOGD("removeBlock: this, x, y, w, h = %p, %d, %d, %d, %d",
83 blockToRemove, blockToRemove->mX, blockToRemove->mY,
84 blockToRemove->mWidth, blockToRemove->mHeight);
85#endif
Romain Guy9b1204b2012-09-04 15:22:57 -070086
Romain Guy9f5dab32012-09-04 12:55:44 -070087 CacheBlock* newHead = head;
88 CacheBlock* nextBlock = blockToRemove->mNext;
89 CacheBlock* prevBlock = blockToRemove->mPrev;
Romain Guy9b1204b2012-09-04 15:22:57 -070090
Romain Guy9f5dab32012-09-04 12:55:44 -070091 if (prevBlock) {
92 prevBlock->mNext = nextBlock;
93 } else {
94 newHead = nextBlock;
95 }
Romain Guy9b1204b2012-09-04 15:22:57 -070096
Romain Guy9f5dab32012-09-04 12:55:44 -070097 if (nextBlock) {
98 nextBlock->mPrev = prevBlock;
99 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700100
Romain Guy9f5dab32012-09-04 12:55:44 -0700101 delete blockToRemove;
Romain Guy9b1204b2012-09-04 15:22:57 -0700102
Romain Guy9f5dab32012-09-04 12:55:44 -0700103 return newHead;
104}
105
106///////////////////////////////////////////////////////////////////////////////
107// CacheTexture
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guy661a87e2013-03-19 15:24:36 -0700110CacheTexture::CacheTexture(uint16_t width, uint16_t height, uint32_t maxQuadCount) :
111 mTexture(NULL), mTextureId(0), mWidth(width), mHeight(height),
112 mLinearFiltering(false), mDirty(false), mNumGlyphs(0),
113 mMesh(NULL), mCurrentQuad(0), mMaxQuadCount(maxQuadCount) {
114 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
115 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
Romain Guycf51a412013-04-08 19:40:31 -0700116
117 // OpenGL ES 3.0+ lets us specify the row length for unpack operations such
118 // as glTexSubImage2D(). This allows us to upload a sub-rectangle of a texture.
119 // With OpenGL ES 2.0 we have to upload entire stripes instead.
120 mHasES3 = Extensions::getInstance().getMajorGlVersion() >= 3;
Romain Guy661a87e2013-03-19 15:24:36 -0700121}
122
123CacheTexture::~CacheTexture() {
124 releaseMesh();
125 releaseTexture();
126 reset();
127}
128
129void CacheTexture::reset() {
130 // Delete existing cache blocks
131 while (mCacheBlocks != NULL) {
132 CacheBlock* tmpBlock = mCacheBlocks;
133 mCacheBlocks = mCacheBlocks->mNext;
134 delete tmpBlock;
135 }
136 mNumGlyphs = 0;
137 mCurrentQuad = 0;
138}
139
140void CacheTexture::init() {
141 // reset, then create a new remainder space to start again
142 reset();
143 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
144 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
145}
146
147void CacheTexture::releaseMesh() {
148 delete[] mMesh;
149}
150
151void CacheTexture::releaseTexture() {
152 if (mTexture) {
Romain Guycf51a412013-04-08 19:40:31 -0700153 delete mTexture;
Romain Guy661a87e2013-03-19 15:24:36 -0700154 mTexture = NULL;
155 }
156 if (mTextureId) {
157 glDeleteTextures(1, &mTextureId);
158 mTextureId = 0;
159 }
160 mDirty = false;
161 mCurrentQuad = 0;
162}
163
Romain Guycf51a412013-04-08 19:40:31 -0700164void CacheTexture::setLinearFiltering(bool linearFiltering, bool bind) {
165 if (linearFiltering != mLinearFiltering) {
166 mLinearFiltering = linearFiltering;
167
168 const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST;
169 if (bind) glBindTexture(GL_TEXTURE_2D, getTextureId());
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
172 }
173}
174
Romain Guy661a87e2013-03-19 15:24:36 -0700175void CacheTexture::allocateMesh() {
176 if (!mMesh) {
177 mMesh = new TextureVertex[mMaxQuadCount * 4];
178 }
179}
180
181void CacheTexture::allocateTexture() {
182 if (!mTexture) {
Romain Guycf51a412013-04-08 19:40:31 -0700183 mTexture = PixelBuffer::create(GL_ALPHA, mWidth, mHeight);
Romain Guy661a87e2013-03-19 15:24:36 -0700184 }
185
186 if (!mTextureId) {
187 glGenTextures(1, &mTextureId);
188
189 glBindTexture(GL_TEXTURE_2D, mTextureId);
190 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
191 // Initialize texture dimensions
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, mWidth, mHeight, 0,
193 GL_ALPHA, GL_UNSIGNED_BYTE, 0);
194
195 const GLenum filtering = getLinearFiltering() ? GL_LINEAR : GL_NEAREST;
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
198
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
201 }
202}
203
Romain Guycf51a412013-04-08 19:40:31 -0700204bool CacheTexture::upload() {
205 const Rect& dirtyRect = mDirtyRect;
206
207 uint32_t x = mHasES3 ? dirtyRect.left : 0;
208 uint32_t y = dirtyRect.top;
209 uint32_t width = mHasES3 ? dirtyRect.getWidth() : mWidth;
210 uint32_t height = dirtyRect.getHeight();
211
212 // The unpack row length only needs to be specified when a new
213 // texture is bound
214 if (mHasES3) {
215 glPixelStorei(GL_UNPACK_ROW_LENGTH, mWidth);
216 }
217
218 mTexture->upload(x, y, width, height, y * mWidth + x);
219
220 setDirty(false);
221
222 return mHasES3;
223}
224
225void CacheTexture::setDirty(bool dirty) {
226 mDirty = dirty;
227 if (!dirty) {
228 mDirtyRect.setEmpty();
229 }
230}
231
Romain Guye43f7852012-09-04 18:58:46 -0700232bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) {
233 if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > mHeight) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700234 return false;
235 }
236
237 uint16_t glyphW = glyph.fWidth + TEXTURE_BORDER_SIZE;
238 uint16_t glyphH = glyph.fHeight + TEXTURE_BORDER_SIZE;
Romain Guy9b1204b2012-09-04 15:22:57 -0700239
Romain Guy9f5dab32012-09-04 12:55:44 -0700240 // roundedUpW equals glyphW to the next multiple of CACHE_BLOCK_ROUNDING_SIZE.
241 // This columns for glyphs that are close but not necessarily exactly the same size. It trades
242 // off the loss of a few pixels for some glyphs against the ability to store more glyphs
243 // of varying sizes in one block.
Romain Guye43f7852012-09-04 18:58:46 -0700244 uint16_t roundedUpW = (glyphW + CACHE_BLOCK_ROUNDING_SIZE - 1) & -CACHE_BLOCK_ROUNDING_SIZE;
Romain Guy9b1204b2012-09-04 15:22:57 -0700245
Romain Guye43f7852012-09-04 18:58:46 -0700246 CacheBlock* cacheBlock = mCacheBlocks;
Romain Guy9f5dab32012-09-04 12:55:44 -0700247 while (cacheBlock) {
248 // Store glyph in this block iff: it fits the block's remaining space and:
249 // it's the remainder space (mY == 0) or there's only enough height for this one glyph
250 // or it's within ROUNDING_SIZE of the block width
251 if (roundedUpW <= cacheBlock->mWidth && glyphH <= cacheBlock->mHeight &&
252 (cacheBlock->mY == TEXTURE_BORDER_SIZE ||
253 (cacheBlock->mWidth - roundedUpW < CACHE_BLOCK_ROUNDING_SIZE))) {
254 if (cacheBlock->mHeight - glyphH < glyphH) {
255 // Only enough space for this glyph - don't bother rounding up the width
256 roundedUpW = glyphW;
257 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700258
Romain Guy9f5dab32012-09-04 12:55:44 -0700259 *retOriginX = cacheBlock->mX;
260 *retOriginY = cacheBlock->mY;
Romain Guy9b1204b2012-09-04 15:22:57 -0700261
Romain Guy9f5dab32012-09-04 12:55:44 -0700262 // If this is the remainder space, create a new cache block for this column. Otherwise,
263 // adjust the info about this column.
264 if (cacheBlock->mY == TEXTURE_BORDER_SIZE) {
265 uint16_t oldX = cacheBlock->mX;
266 // Adjust remainder space dimensions
267 cacheBlock->mWidth -= roundedUpW;
268 cacheBlock->mX += roundedUpW;
Romain Guy9b1204b2012-09-04 15:22:57 -0700269
Romain Guy9f5dab32012-09-04 12:55:44 -0700270 if (mHeight - glyphH >= glyphH) {
271 // There's enough height left over to create a new CacheBlock
Romain Guye43f7852012-09-04 18:58:46 -0700272 CacheBlock* newBlock = new CacheBlock(oldX, glyphH + TEXTURE_BORDER_SIZE,
Romain Guy9f5dab32012-09-04 12:55:44 -0700273 roundedUpW, mHeight - glyphH - TEXTURE_BORDER_SIZE);
274#if DEBUG_FONT_RENDERER
275 ALOGD("fitBitmap: Created new block: this, x, y, w, h = %p, %d, %d, %d, %d",
276 newBlock, newBlock->mX, newBlock->mY,
277 newBlock->mWidth, newBlock->mHeight);
278#endif
279 mCacheBlocks = CacheBlock::insertBlock(mCacheBlocks, newBlock);
280 }
281 } else {
282 // Insert into current column and adjust column dimensions
283 cacheBlock->mY += glyphH;
284 cacheBlock->mHeight -= glyphH;
285#if DEBUG_FONT_RENDERER
286 ALOGD("fitBitmap: Added to existing block: this, x, y, w, h = %p, %d, %d, %d, %d",
287 cacheBlock, cacheBlock->mX, cacheBlock->mY,
288 cacheBlock->mWidth, cacheBlock->mHeight);
289#endif
290 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700291
Romain Guy9f5dab32012-09-04 12:55:44 -0700292 if (cacheBlock->mHeight < fmin(glyphH, glyphW)) {
293 // If remaining space in this block is too small to be useful, remove it
294 mCacheBlocks = CacheBlock::removeBlock(mCacheBlocks, cacheBlock);
295 }
Romain Guy9b1204b2012-09-04 15:22:57 -0700296
Romain Guy9f5dab32012-09-04 12:55:44 -0700297 mDirty = true;
Chet Haaseb92d8f72012-09-21 08:40:46 -0700298 const Rect r(*retOriginX - TEXTURE_BORDER_SIZE, *retOriginY - TEXTURE_BORDER_SIZE,
299 *retOriginX + glyphW, *retOriginY + glyphH);
300 mDirtyRect.unionWith(r);
Romain Guy9b1204b2012-09-04 15:22:57 -0700301 mNumGlyphs++;
302
Romain Guy9f5dab32012-09-04 12:55:44 -0700303#if DEBUG_FONT_RENDERER
304 ALOGD("fitBitmap: current block list:");
305 mCacheBlocks->output();
306#endif
Romain Guy9b1204b2012-09-04 15:22:57 -0700307
Romain Guy9f5dab32012-09-04 12:55:44 -0700308 return true;
309 }
310 cacheBlock = cacheBlock->mNext;
311 }
312#if DEBUG_FONT_RENDERER
313 ALOGD("fitBitmap: returning false for glyph of size %d, %d", glyphW, glyphH);
314#endif
315 return false;
316}
317
318}; // namespace uirenderer
319}; // namespace android