Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 19 | #include <SkUtils.h> |
| 20 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 22 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 25 | #include "Caches.h" |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 26 | #include "Debug.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 27 | #include "FontRenderer.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 28 | #include "Rect.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 29 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 34 | // FontRenderer |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 37 | static bool sLogFontRendererCreate = true; |
| 38 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 39 | FontRenderer::FontRenderer() : |
| 40 | mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity) { |
| 41 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 42 | if (sLogFontRendererCreate) { |
| 43 | INIT_LOGD("Creating FontRenderer"); |
| 44 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 45 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 46 | mGammaTable = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 47 | mInitialized = false; |
| 48 | mMaxNumberOfQuads = 1024; |
| 49 | mCurrentQuadIndex = 0; |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 50 | mLastQuadIndex = 0; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 51 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 52 | mTextMesh = NULL; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 53 | mCurrentCacheTexture = NULL; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 54 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 55 | mLinearFiltering = false; |
| 56 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 57 | mIndexBufferID = 0; |
| 58 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 59 | mSmallCacheWidth = DEFAULT_TEXT_SMALL_CACHE_WIDTH; |
| 60 | mSmallCacheHeight = DEFAULT_TEXT_SMALL_CACHE_HEIGHT; |
| 61 | mLargeCacheWidth = DEFAULT_TEXT_LARGE_CACHE_WIDTH; |
| 62 | mLargeCacheHeight = DEFAULT_TEXT_LARGE_CACHE_HEIGHT; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 63 | |
| 64 | char property[PROPERTY_VALUE_MAX]; |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 65 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_WIDTH, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 66 | mSmallCacheWidth = atoi(property); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 67 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 68 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 69 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_HEIGHT, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 70 | mSmallCacheHeight = atoi(property); |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 71 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 72 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 73 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_WIDTH, property, NULL) > 0) { |
| 74 | mLargeCacheWidth = atoi(property); |
| 75 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 76 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 77 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_HEIGHT, property, NULL) > 0) { |
| 78 | mLargeCacheHeight = atoi(property); |
| 79 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 80 | |
| 81 | uint32_t maxTextureSize = (uint32_t) Caches::getInstance().maxTextureSize; |
| 82 | mSmallCacheWidth = mSmallCacheWidth > maxTextureSize ? maxTextureSize : mSmallCacheWidth; |
| 83 | mSmallCacheHeight = mSmallCacheHeight > maxTextureSize ? maxTextureSize : mSmallCacheHeight; |
| 84 | mLargeCacheWidth = mLargeCacheWidth > maxTextureSize ? maxTextureSize : mLargeCacheWidth; |
| 85 | mLargeCacheHeight = mLargeCacheHeight > maxTextureSize ? maxTextureSize : mLargeCacheHeight; |
| 86 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 87 | if (sLogFontRendererCreate) { |
| 88 | INIT_LOGD(" Text cache sizes, in pixels: %i x %i, %i x %i, %i x %i, %i x %i", |
| 89 | mSmallCacheWidth, mSmallCacheHeight, |
| 90 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 91 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 92 | mLargeCacheWidth, mLargeCacheHeight); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 93 | } |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 94 | |
| 95 | sLogFontRendererCreate = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | FontRenderer::~FontRenderer() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 99 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 100 | delete mCacheTextures[i]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 101 | } |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 102 | mCacheTextures.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 103 | |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 104 | if (mInitialized) { |
Romain Guy | a9dd820 | 2012-03-26 14:52:00 -0700 | [diff] [blame] | 105 | // Unbinding the buffer shouldn't be necessary but it crashes with some drivers |
| 106 | Caches::getInstance().unbindIndicesBuffer(); |
Romain Guy | b031798 | 2012-03-21 11:52:52 -0700 | [diff] [blame] | 107 | glDeleteBuffers(1, &mIndexBufferID); |
| 108 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 109 | delete[] mTextMesh; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 110 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 111 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 112 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 113 | while (it.next()) { |
| 114 | delete it.value(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 115 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 116 | mActiveFonts.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void FontRenderer::flushAllAndInvalidate() { |
| 120 | if (mCurrentQuadIndex != 0) { |
| 121 | issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 122 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 123 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 124 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 125 | while (it.next()) { |
| 126 | it.value()->invalidateTextureCache(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 127 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 128 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 129 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 130 | mCacheTextures[i]->init(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 131 | } |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 132 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 133 | #if DEBUG_FONT_RENDERER |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 134 | uint16_t totalGlyphs = 0; |
| 135 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 136 | totalGlyphs += mCacheTextures[i]->getGlyphCount(); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 137 | // Erase caches, just as a debugging facility |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 138 | if (mCacheTextures[i]->getTexture()) { |
| 139 | memset(mCacheTextures[i]->getTexture(), 0, |
| 140 | mCacheTextures[i]->getWidth() * mCacheTextures[i]->getHeight()); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 141 | } |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 142 | } |
| 143 | ALOGD("Flushing caches: glyphs cached = %d", totalGlyphs); |
| 144 | #endif |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 147 | void FontRenderer::flushLargeCaches() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 148 | // Start from 1; don't deallocate smallest/default texture |
| 149 | for (uint32_t i = 1; i < mCacheTextures.size(); i++) { |
| 150 | CacheTexture* cacheTexture = mCacheTextures[i]; |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 151 | if (cacheTexture->getTexture()) { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 152 | cacheTexture->init(); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 153 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 154 | while (it.next()) { |
| 155 | it.value()->invalidateTextureCache(cacheTexture); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 156 | } |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 157 | cacheTexture->releaseTexture(); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 162 | CacheTexture* FontRenderer::cacheBitmapInTexture(const SkGlyph& glyph, |
| 163 | uint32_t* startX, uint32_t* startY) { |
| 164 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 165 | if (mCacheTextures[i]->fitBitmap(glyph, startX, startY)) { |
| 166 | return mCacheTextures[i]; |
| 167 | } |
| 168 | } |
| 169 | // Could not fit glyph into current cache textures |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 173 | void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 174 | uint32_t* retOriginX, uint32_t* retOriginY, bool precaching) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 175 | checkInit(); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 176 | cachedGlyph->mIsValid = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 177 | // If the glyph is too tall, don't cache it |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 178 | if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 179 | mCacheTextures[mCacheTextures.size() - 1]->getHeight()) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 180 | ALOGE("Font size too large to fit in cache. width, height = %i, %i", |
| 181 | (int) glyph.fWidth, (int) glyph.fHeight); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 182 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Now copy the bitmap into the cache texture |
| 186 | uint32_t startX = 0; |
| 187 | uint32_t startY = 0; |
| 188 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 189 | CacheTexture* cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 190 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 191 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 192 | if (!precaching) { |
| 193 | // If the new glyph didn't fit and we are not just trying to precache it, |
| 194 | // clear out the cache and try again |
| 195 | flushAllAndInvalidate(); |
| 196 | cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY); |
| 197 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 198 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 199 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 200 | // either the glyph didn't fit or we're precaching and will cache it when we draw |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 201 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 205 | cachedGlyph->mCacheTexture = cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 206 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 207 | *retOriginX = startX; |
| 208 | *retOriginY = startY; |
| 209 | |
| 210 | uint32_t endX = startX + glyph.fWidth; |
| 211 | uint32_t endY = startY + glyph.fHeight; |
| 212 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 213 | uint32_t cacheWidth = cacheTexture->getWidth(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 214 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 215 | if (!cacheTexture->getTexture()) { |
| 216 | Caches::getInstance().activeTexture(0); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 217 | // Large-glyph texture memory is allocated only as needed |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 218 | cacheTexture->allocateTexture(); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 219 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 220 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 221 | // Tells us whether the glyphs is B&W (1 bit per pixel) |
| 222 | // or anti-aliased (8 bits per pixel) |
| 223 | SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 224 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 225 | uint8_t* cacheBuffer = cacheTexture->getTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 226 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 227 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 228 | // Zero out the borders |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 229 | for (cacheX = startX - TEXTURE_BORDER_SIZE; cacheX < endX + TEXTURE_BORDER_SIZE; cacheX++) { |
| 230 | cacheBuffer[(startY - TEXTURE_BORDER_SIZE) * cacheWidth + cacheX] = 0; |
| 231 | cacheBuffer[(endY + TEXTURE_BORDER_SIZE - 1) * cacheWidth + cacheX] = 0; |
| 232 | } |
| 233 | |
| 234 | for (cacheY = startY - TEXTURE_BORDER_SIZE + 1; |
| 235 | cacheY < endY + TEXTURE_BORDER_SIZE - 1; cacheY++) { |
| 236 | cacheBuffer[cacheY * cacheWidth + startX - TEXTURE_BORDER_SIZE] = 0; |
| 237 | cacheBuffer[cacheY * cacheWidth + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
| 238 | } |
| 239 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 240 | // Copy the glyph image, taking the mask format into account |
| 241 | uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage; |
| 242 | int stride = glyph.rowBytes(); |
| 243 | |
| 244 | switch (format) { |
| 245 | case SkMask::kA8_Format: { |
| 246 | if (mGammaTable) { |
| 247 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) { |
| 248 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) { |
| 249 | uint8_t tempCol = bitmapBuffer[bY + bX]; |
| 250 | cacheBuffer[cacheY * cacheWidth + cacheX] = mGammaTable[tempCol]; |
| 251 | } |
| 252 | } |
| 253 | } else { |
| 254 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) { |
| 255 | memcpy(&cacheBuffer[cacheY * cacheWidth + startX], &bitmapBuffer[bY], |
| 256 | glyph.fWidth); |
| 257 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 258 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 259 | break; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 260 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 261 | case SkMask::kBW_Format: { |
| 262 | static const uint8_t COLORS[2] = { 0, 255 }; |
| 263 | |
| 264 | for (cacheY = startY; cacheY < endY; cacheY++) { |
| 265 | cacheX = startX; |
| 266 | int rowBytes = stride; |
| 267 | uint8_t* buffer = bitmapBuffer; |
| 268 | |
| 269 | while (--rowBytes >= 0) { |
| 270 | uint8_t b = *buffer++; |
| 271 | for (int8_t mask = 7; mask >= 0 && cacheX < endX; mask--) { |
| 272 | cacheBuffer[cacheY * cacheWidth + cacheX++] = COLORS[(b >> mask) & 0x1]; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | bitmapBuffer += stride; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 277 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 278 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 279 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 280 | default: |
| 281 | ALOGW("Unkown glyph format: 0x%x", format); |
| 282 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 283 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 284 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 285 | cachedGlyph->mIsValid = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 288 | CacheTexture* FontRenderer::createCacheTexture(int width, int height, bool allocate) { |
Romain Guy | 0aa87bb | 2012-07-20 11:14:32 -0700 | [diff] [blame] | 289 | CacheTexture* cacheTexture = new CacheTexture(width, height); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 290 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 291 | if (allocate) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 292 | Caches::getInstance().activeTexture(0); |
| 293 | cacheTexture->allocateTexture(); |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 294 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 295 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 296 | return cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void FontRenderer::initTextTexture() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 300 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 301 | delete mCacheTextures[i]; |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 302 | } |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 303 | mCacheTextures.clear(); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 304 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 305 | mUploadTexture = false; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 306 | mCacheTextures.push(createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, true)); |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 307 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false)); |
| 308 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false)); |
| 309 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight, false)); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 310 | mCurrentCacheTexture = mCacheTextures[0]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // Avoid having to reallocate memory and render quad by quad |
| 314 | void FontRenderer::initVertexArrayBuffers() { |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 315 | uint32_t numIndices = mMaxNumberOfQuads * 6; |
| 316 | uint32_t indexBufferSizeBytes = numIndices * sizeof(uint16_t); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 317 | uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 318 | |
| 319 | // Four verts, two triangles , six indices per quad |
| 320 | for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) { |
| 321 | int i6 = i * 6; |
| 322 | int i4 = i * 4; |
| 323 | |
| 324 | indexBufferData[i6 + 0] = i4 + 0; |
| 325 | indexBufferData[i6 + 1] = i4 + 1; |
| 326 | indexBufferData[i6 + 2] = i4 + 2; |
| 327 | |
| 328 | indexBufferData[i6 + 3] = i4 + 0; |
| 329 | indexBufferData[i6 + 4] = i4 + 2; |
| 330 | indexBufferData[i6 + 5] = i4 + 3; |
| 331 | } |
| 332 | |
| 333 | glGenBuffers(1, &mIndexBufferID); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 334 | Caches::getInstance().bindIndicesBuffer(mIndexBufferID); |
Romain Guy | 5d79441 | 2010-10-13 16:36:22 -0700 | [diff] [blame] | 335 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_STATIC_DRAW); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 336 | |
| 337 | free(indexBufferData); |
| 338 | |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 339 | uint32_t coordSize = 2; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 340 | uint32_t uvSize = 2; |
| 341 | uint32_t vertsPerQuad = 4; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 342 | uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 343 | mTextMesh = new float[vertexBufferSize]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // We don't want to allocate anything unless we actually draw text |
| 347 | void FontRenderer::checkInit() { |
| 348 | if (mInitialized) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | initTextTexture(); |
| 353 | initVertexArrayBuffers(); |
| 354 | |
| 355 | mInitialized = true; |
| 356 | } |
| 357 | |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 358 | void FontRenderer::updateDrawParams() { |
| 359 | if (mCurrentQuadIndex != mLastQuadIndex) { |
| 360 | mDrawOffsets.add((uint16_t*)(mLastQuadIndex * sizeof(uint16_t) * 6)); |
| 361 | mDrawCounts.add(mCurrentQuadIndex - mLastQuadIndex); |
| 362 | mDrawCacheTextures.add(mCurrentCacheTexture); |
| 363 | mLastQuadIndex = mCurrentQuadIndex; |
| 364 | } |
| 365 | } |
| 366 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 367 | void FontRenderer::checkTextureUpdate() { |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 368 | if (!mUploadTexture) { |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 369 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 372 | Caches& caches = Caches::getInstance(); |
| 373 | GLuint lastTextureId = 0; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 374 | // Iterate over all the cache textures and see which ones need to be updated |
| 375 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 376 | CacheTexture* cacheTexture = mCacheTextures[i]; |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 377 | if (cacheTexture->isDirty() && cacheTexture->getTexture()) { |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 378 | // Can't copy inner rect; glTexSubimage expects pointer to deal with entire buffer |
| 379 | // of data. So expand the dirty rect to the encompassing horizontal stripe. |
| 380 | const Rect* dirtyRect = cacheTexture->getDirtyRect(); |
| 381 | uint32_t x = 0; |
| 382 | uint32_t y = dirtyRect->top; |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 383 | uint32_t width = cacheTexture->getWidth(); |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 384 | uint32_t height = dirtyRect->getHeight(); |
| 385 | void* textureData = cacheTexture->getTexture() + y * width; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 386 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 387 | if (cacheTexture->getTextureId() != lastTextureId) { |
| 388 | lastTextureId = cacheTexture->getTextureId(); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 389 | caches.activeTexture(0); |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 390 | glBindTexture(GL_TEXTURE_2D, lastTextureId); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 391 | } |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 392 | #if DEBUG_FONT_RENDERER |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 393 | ALOGD("glTexSubimage for cacheTexture %d: x, y, width height = %d, %d, %d, %d", |
| 394 | i, x, y, width, height); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 395 | #endif |
Chet Haase | b92d8f7 | 2012-09-21 08:40:46 -0700 | [diff] [blame] | 396 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 397 | GL_ALPHA, GL_UNSIGNED_BYTE, textureData); |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 398 | cacheTexture->setDirty(false); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | mUploadTexture = false; |
| 403 | } |
| 404 | |
| 405 | void FontRenderer::issueDrawCommand() { |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 406 | updateDrawParams(); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 407 | checkTextureUpdate(); |
| 408 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 409 | Caches& caches = Caches::getInstance(); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 410 | caches.bindIndicesBuffer(mIndexBufferID); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 411 | if (!mDrawn) { |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 412 | float* buffer = mTextMesh; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 413 | int offset = 2; |
| 414 | |
| 415 | bool force = caches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 416 | caches.bindPositionVertexPointer(force, buffer); |
| 417 | caches.bindTexCoordsVertexPointer(force, buffer + offset); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 420 | for (uint32_t i = 0; i < mDrawOffsets.size(); i++) { |
| 421 | uint16_t* offset = mDrawOffsets[i]; |
| 422 | uint32_t count = mDrawCounts[i]; |
| 423 | CacheTexture* texture = mDrawCacheTextures[i]; |
| 424 | |
| 425 | caches.activeTexture(0); |
| 426 | glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); |
| 427 | |
| 428 | texture->setLinearFiltering(mLinearFiltering, false); |
| 429 | |
| 430 | glDrawElements(GL_TRIANGLES, count * 6, GL_UNSIGNED_SHORT, offset); |
| 431 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 432 | |
| 433 | mDrawn = true; |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 434 | |
| 435 | mCurrentQuadIndex = 0; |
| 436 | mLastQuadIndex = 0; |
| 437 | mDrawOffsets.clear(); |
| 438 | mDrawCounts.clear(); |
| 439 | mDrawCacheTextures.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 442 | void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 443 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 444 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 445 | if (texture != mCurrentCacheTexture) { |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 446 | updateDrawParams(); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 447 | // Now use the new texture id |
| 448 | mCurrentCacheTexture = texture; |
| 449 | } |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 450 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 451 | const uint32_t vertsPerQuad = 4; |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 452 | const uint32_t floatsPerVert = 4; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 453 | float* currentPos = mTextMesh + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 454 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 455 | (*currentPos++) = x1; |
| 456 | (*currentPos++) = y1; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 457 | (*currentPos++) = u1; |
| 458 | (*currentPos++) = v1; |
| 459 | |
| 460 | (*currentPos++) = x2; |
| 461 | (*currentPos++) = y2; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 462 | (*currentPos++) = u2; |
| 463 | (*currentPos++) = v2; |
| 464 | |
| 465 | (*currentPos++) = x3; |
| 466 | (*currentPos++) = y3; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 467 | (*currentPos++) = u3; |
| 468 | (*currentPos++) = v3; |
| 469 | |
| 470 | (*currentPos++) = x4; |
| 471 | (*currentPos++) = y4; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 472 | (*currentPos++) = u4; |
| 473 | (*currentPos++) = v4; |
| 474 | |
| 475 | mCurrentQuadIndex++; |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void FontRenderer::appendMeshQuad(float x1, float y1, float u1, float v1, |
| 479 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 480 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 481 | |
| 482 | if (mClip && |
| 483 | (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom)) { |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 488 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 489 | if (mBounds) { |
| 490 | mBounds->left = fmin(mBounds->left, x1); |
| 491 | mBounds->top = fmin(mBounds->top, y3); |
| 492 | mBounds->right = fmax(mBounds->right, x3); |
| 493 | mBounds->bottom = fmax(mBounds->bottom, y1); |
| 494 | } |
| 495 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 496 | if (mCurrentQuadIndex == mMaxNumberOfQuads) { |
| 497 | issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 501 | void FontRenderer::appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 502 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 503 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 504 | |
| 505 | appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture); |
| 506 | |
| 507 | if (mBounds) { |
| 508 | mBounds->left = fmin(mBounds->left, fmin(x1, fmin(x2, fmin(x3, x4)))); |
| 509 | mBounds->top = fmin(mBounds->top, fmin(y1, fmin(y2, fmin(y3, y4)))); |
| 510 | mBounds->right = fmax(mBounds->right, fmax(x1, fmax(x2, fmax(x3, x4)))); |
| 511 | mBounds->bottom = fmax(mBounds->bottom, fmax(y1, fmax(y2, fmax(y3, y4)))); |
| 512 | } |
| 513 | |
| 514 | if (mCurrentQuadIndex == mMaxNumberOfQuads) { |
| 515 | issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 519 | void FontRenderer::setFont(SkPaint* paint, const mat4& matrix) { |
| 520 | mCurrentFont = Font::create(this, paint, matrix); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 521 | } |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 522 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 523 | FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 524 | uint32_t startIndex, uint32_t len, int numGlyphs, uint32_t radius, const float* positions) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 525 | checkInit(); |
| 526 | |
| 527 | if (!mCurrentFont) { |
| 528 | DropShadow image; |
| 529 | image.width = 0; |
| 530 | image.height = 0; |
| 531 | image.image = NULL; |
| 532 | image.penX = 0; |
| 533 | image.penY = 0; |
| 534 | return image; |
| 535 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 536 | |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 537 | mDrawn = false; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 538 | mClip = NULL; |
| 539 | mBounds = NULL; |
| 540 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 541 | Rect bounds; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 542 | mCurrentFont->measure(paint, text, startIndex, len, numGlyphs, &bounds, positions); |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 543 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 544 | uint32_t paddedWidth = (uint32_t) (bounds.right - bounds.left) + 2 * radius; |
| 545 | uint32_t paddedHeight = (uint32_t) (bounds.top - bounds.bottom) + 2 * radius; |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 546 | uint8_t* dataBuffer = new uint8_t[paddedWidth * paddedHeight]; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 547 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 548 | for (uint32_t i = 0; i < paddedWidth * paddedHeight; i++) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 549 | dataBuffer[i] = 0; |
| 550 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 551 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 552 | int penX = radius - bounds.left; |
| 553 | int penY = radius - bounds.bottom; |
| 554 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 555 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, penX, penY, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 556 | Font::BITMAP, dataBuffer, paddedWidth, paddedHeight, NULL, positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 557 | blurImage(dataBuffer, paddedWidth, paddedHeight, radius); |
| 558 | |
| 559 | DropShadow image; |
| 560 | image.width = paddedWidth; |
| 561 | image.height = paddedHeight; |
| 562 | image.image = dataBuffer; |
| 563 | image.penX = penX; |
| 564 | image.penY = penY; |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 565 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 566 | return image; |
| 567 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 568 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 569 | void FontRenderer::initRender(const Rect* clip, Rect* bounds) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 570 | checkInit(); |
| 571 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 572 | mDrawn = false; |
| 573 | mBounds = bounds; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 574 | mClip = clip; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 575 | } |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 576 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 577 | void FontRenderer::finishRender() { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 578 | mBounds = NULL; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 579 | mClip = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 580 | |
| 581 | if (mCurrentQuadIndex != 0) { |
| 582 | issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 583 | } |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 586 | void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix) { |
| 587 | Font* font = Font::create(this, paint, matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 588 | font->precache(paint, text, numGlyphs); |
| 589 | } |
| 590 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 591 | bool FontRenderer::renderPosText(SkPaint* paint, const Rect* clip, const char *text, |
| 592 | uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, |
| 593 | const float* positions, Rect* bounds) { |
| 594 | if (!mCurrentFont) { |
| 595 | ALOGE("No font set"); |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | initRender(clip, bounds); |
| 600 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, x, y, positions); |
| 601 | finishRender(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 602 | |
| 603 | return mDrawn; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 606 | bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, |
| 607 | uint32_t startIndex, uint32_t len, int numGlyphs, SkPath* path, |
| 608 | float hOffset, float vOffset, Rect* bounds) { |
| 609 | if (!mCurrentFont) { |
| 610 | ALOGE("No font set"); |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | initRender(clip, bounds); |
| 615 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, path, hOffset, vOffset); |
| 616 | finishRender(); |
| 617 | |
| 618 | return mDrawn; |
| 619 | } |
| 620 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 621 | void FontRenderer::removeFont(const Font* font) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 622 | mActiveFonts.remove(font->getDescription()); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 623 | |
| 624 | if (mCurrentFont == font) { |
| 625 | mCurrentFont = NULL; |
| 626 | } |
| 627 | } |
| 628 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 629 | void FontRenderer::computeGaussianWeights(float* weights, int32_t radius) { |
| 630 | // Compute gaussian weights for the blur |
| 631 | // e is the euler's number |
| 632 | float e = 2.718281828459045f; |
| 633 | float pi = 3.1415926535897932f; |
| 634 | // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) |
| 635 | // x is of the form [-radius .. 0 .. radius] |
| 636 | // and sigma varies with radius. |
| 637 | // Based on some experimental radius values and sigma's |
| 638 | // we approximately fit sigma = f(radius) as |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 639 | // sigma = radius * 0.3 + 0.6 |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 640 | // The larger the radius gets, the more our gaussian blur |
| 641 | // will resemble a box blur since with large sigma |
| 642 | // the gaussian curve begins to lose its shape |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 643 | float sigma = 0.3f * (float) radius + 0.6f; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 644 | |
| 645 | // Now compute the coefficints |
| 646 | // We will store some redundant values to save some math during |
| 647 | // the blur calculations |
| 648 | // precompute some values |
| 649 | float coeff1 = 1.0f / (sqrt( 2.0f * pi ) * sigma); |
| 650 | float coeff2 = - 1.0f / (2.0f * sigma * sigma); |
| 651 | |
| 652 | float normalizeFactor = 0.0f; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 653 | for (int32_t r = -radius; r <= radius; r ++) { |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 654 | float floatR = (float) r; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 655 | weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2); |
| 656 | normalizeFactor += weights[r + radius]; |
| 657 | } |
| 658 | |
| 659 | //Now we need to normalize the weights because all our coefficients need to add up to one |
| 660 | normalizeFactor = 1.0f / normalizeFactor; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 661 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 662 | weights[r + radius] *= normalizeFactor; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void FontRenderer::horizontalBlur(float* weights, int32_t radius, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 667 | const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 668 | float blurredPixel = 0.0f; |
| 669 | float currentPixel = 0.0f; |
| 670 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 671 | for (int32_t y = 0; y < height; y ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 672 | |
| 673 | const uint8_t* input = source + y * width; |
| 674 | uint8_t* output = dest + y * width; |
| 675 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 676 | for (int32_t x = 0; x < width; x ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 677 | blurredPixel = 0.0f; |
| 678 | const float* gPtr = weights; |
| 679 | // Optimization for non-border pixels |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 680 | if (x > radius && x < (width - radius)) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 681 | const uint8_t *i = input + (x - radius); |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 682 | for (int r = -radius; r <= radius; r ++) { |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 683 | currentPixel = (float) (*i); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 684 | blurredPixel += currentPixel * gPtr[0]; |
| 685 | gPtr++; |
| 686 | i++; |
| 687 | } |
| 688 | } else { |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 689 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 690 | // Stepping left and right away from the pixel |
| 691 | int validW = x + r; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 692 | if (validW < 0) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 693 | validW = 0; |
| 694 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 695 | if (validW > width - 1) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 696 | validW = width - 1; |
| 697 | } |
| 698 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 699 | currentPixel = (float) input[validW]; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 700 | blurredPixel += currentPixel * gPtr[0]; |
| 701 | gPtr++; |
| 702 | } |
| 703 | } |
| 704 | *output = (uint8_t)blurredPixel; |
| 705 | output ++; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void FontRenderer::verticalBlur(float* weights, int32_t radius, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 711 | const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 712 | float blurredPixel = 0.0f; |
| 713 | float currentPixel = 0.0f; |
| 714 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 715 | for (int32_t y = 0; y < height; y ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 716 | uint8_t* output = dest + y * width; |
| 717 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 718 | for (int32_t x = 0; x < width; x ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 719 | blurredPixel = 0.0f; |
| 720 | const float* gPtr = weights; |
| 721 | const uint8_t* input = source + x; |
| 722 | // Optimization for non-border pixels |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 723 | if (y > radius && y < (height - radius)) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 724 | const uint8_t *i = input + ((y - radius) * width); |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 725 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 726 | currentPixel = (float)(*i); |
| 727 | blurredPixel += currentPixel * gPtr[0]; |
| 728 | gPtr++; |
| 729 | i += width; |
| 730 | } |
| 731 | } else { |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 732 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 733 | int validH = y + r; |
| 734 | // Clamp to zero and width |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 735 | if (validH < 0) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 736 | validH = 0; |
| 737 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 738 | if (validH > height - 1) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 739 | validH = height - 1; |
| 740 | } |
| 741 | |
| 742 | const uint8_t *i = input + validH * width; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 743 | currentPixel = (float) (*i); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 744 | blurredPixel += currentPixel * gPtr[0]; |
| 745 | gPtr++; |
| 746 | } |
| 747 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 748 | *output = (uint8_t) blurredPixel; |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 749 | output++; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | |
| 755 | void FontRenderer::blurImage(uint8_t *image, int32_t width, int32_t height, int32_t radius) { |
| 756 | float *gaussian = new float[2 * radius + 1]; |
| 757 | computeGaussianWeights(gaussian, radius); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 758 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 759 | uint8_t* scratch = new uint8_t[width * height]; |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 760 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 761 | horizontalBlur(gaussian, radius, image, scratch, width, height); |
| 762 | verticalBlur(gaussian, radius, scratch, image, width, height); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 763 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 764 | delete[] gaussian; |
| 765 | delete[] scratch; |
| 766 | } |
| 767 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 768 | }; // namespace uirenderer |
| 769 | }; // namespace android |