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