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 | |
Derek Sollenberger | ca79cf6 | 2012-08-14 16:44:52 -0400 | [diff] [blame] | 19 | #include <SkGlyph.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 20 | #include <SkUtils.h> |
| 21 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 23 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 24 | #include <utils/Functor.h> |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 27 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 28 | #include <RenderScript.h> |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 29 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 30 | |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 31 | #include "utils/Blur.h" |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 32 | #include "utils/Timing.h" |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 33 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 34 | #include "Caches.h" |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 35 | #include "Debug.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 36 | #include "FontRenderer.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 37 | #include "Rect.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 38 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 42 | // blur inputs smaller than this constant will bypass renderscript |
| 43 | #define RS_MIN_INPUT_CUTOFF 10000 |
| 44 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 45 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 46 | // FontRenderer |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 49 | static bool sLogFontRendererCreate = true; |
| 50 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 51 | FontRenderer::FontRenderer() : |
| 52 | mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity) { |
| 53 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 54 | if (sLogFontRendererCreate) { |
| 55 | INIT_LOGD("Creating FontRenderer"); |
| 56 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 57 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 58 | mGammaTable = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 59 | mInitialized = false; |
| 60 | mMaxNumberOfQuads = 1024; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 61 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 62 | mCurrentCacheTexture = NULL; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 63 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 64 | mLinearFiltering = false; |
| 65 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 66 | mIndexBufferID = 0; |
| 67 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 68 | mSmallCacheWidth = DEFAULT_TEXT_SMALL_CACHE_WIDTH; |
| 69 | mSmallCacheHeight = DEFAULT_TEXT_SMALL_CACHE_HEIGHT; |
| 70 | mLargeCacheWidth = DEFAULT_TEXT_LARGE_CACHE_WIDTH; |
| 71 | mLargeCacheHeight = DEFAULT_TEXT_LARGE_CACHE_HEIGHT; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 72 | |
| 73 | char property[PROPERTY_VALUE_MAX]; |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 74 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_WIDTH, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 75 | mSmallCacheWidth = atoi(property); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 76 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 77 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 78 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_HEIGHT, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 79 | mSmallCacheHeight = atoi(property); |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 80 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 81 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 82 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_WIDTH, property, NULL) > 0) { |
| 83 | mLargeCacheWidth = atoi(property); |
| 84 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 85 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 86 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_HEIGHT, property, NULL) > 0) { |
| 87 | mLargeCacheHeight = atoi(property); |
| 88 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 89 | |
| 90 | uint32_t maxTextureSize = (uint32_t) Caches::getInstance().maxTextureSize; |
| 91 | mSmallCacheWidth = mSmallCacheWidth > maxTextureSize ? maxTextureSize : mSmallCacheWidth; |
| 92 | mSmallCacheHeight = mSmallCacheHeight > maxTextureSize ? maxTextureSize : mSmallCacheHeight; |
| 93 | mLargeCacheWidth = mLargeCacheWidth > maxTextureSize ? maxTextureSize : mLargeCacheWidth; |
| 94 | mLargeCacheHeight = mLargeCacheHeight > maxTextureSize ? maxTextureSize : mLargeCacheHeight; |
| 95 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 96 | if (sLogFontRendererCreate) { |
| 97 | INIT_LOGD(" Text cache sizes, in pixels: %i x %i, %i x %i, %i x %i, %i x %i", |
| 98 | mSmallCacheWidth, mSmallCacheHeight, |
| 99 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 100 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 101 | mLargeCacheWidth, mLargeCacheHeight); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 102 | } |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 103 | |
| 104 | sLogFontRendererCreate = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | FontRenderer::~FontRenderer() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 108 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 109 | delete mCacheTextures[i]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 110 | } |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 111 | mCacheTextures.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 112 | |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 113 | if (mInitialized) { |
Romain Guy | a9dd820 | 2012-03-26 14:52:00 -0700 | [diff] [blame] | 114 | // Unbinding the buffer shouldn't be necessary but it crashes with some drivers |
| 115 | Caches::getInstance().unbindIndicesBuffer(); |
Romain Guy | b031798 | 2012-03-21 11:52:52 -0700 | [diff] [blame] | 116 | glDeleteBuffers(1, &mIndexBufferID); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 117 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 118 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 119 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 120 | while (it.next()) { |
| 121 | delete it.value(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 122 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 123 | mActiveFonts.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void FontRenderer::flushAllAndInvalidate() { |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 127 | issueDrawCommand(); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 128 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 129 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 130 | while (it.next()) { |
| 131 | it.value()->invalidateTextureCache(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 132 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 133 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 134 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 135 | mCacheTextures[i]->init(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 136 | } |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 137 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 138 | #if DEBUG_FONT_RENDERER |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 139 | uint16_t totalGlyphs = 0; |
| 140 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 141 | totalGlyphs += mCacheTextures[i]->getGlyphCount(); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 142 | // Erase caches, just as a debugging facility |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 143 | if (mCacheTextures[i]->getTexture()) { |
| 144 | memset(mCacheTextures[i]->getTexture(), 0, |
| 145 | mCacheTextures[i]->getWidth() * mCacheTextures[i]->getHeight()); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 146 | } |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 147 | } |
| 148 | ALOGD("Flushing caches: glyphs cached = %d", totalGlyphs); |
| 149 | #endif |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 152 | void FontRenderer::flushLargeCaches() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 153 | // Start from 1; don't deallocate smallest/default texture |
| 154 | for (uint32_t i = 1; i < mCacheTextures.size(); i++) { |
| 155 | CacheTexture* cacheTexture = mCacheTextures[i]; |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 156 | if (cacheTexture->getTexture()) { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 157 | cacheTexture->init(); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 158 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 159 | while (it.next()) { |
| 160 | it.value()->invalidateTextureCache(cacheTexture); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 161 | } |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 162 | cacheTexture->releaseTexture(); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 167 | CacheTexture* FontRenderer::cacheBitmapInTexture(const SkGlyph& glyph, |
| 168 | uint32_t* startX, uint32_t* startY) { |
| 169 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 170 | if (mCacheTextures[i]->fitBitmap(glyph, startX, startY)) { |
| 171 | return mCacheTextures[i]; |
| 172 | } |
| 173 | } |
| 174 | // Could not fit glyph into current cache textures |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 178 | void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 179 | uint32_t* retOriginX, uint32_t* retOriginY, bool precaching) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 180 | checkInit(); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 181 | |
| 182 | // If the glyph bitmap is empty let's assum the glyph is valid |
| 183 | // so we can avoid doing extra work later on |
| 184 | if (glyph.fWidth == 0 || glyph.fHeight == 0) { |
| 185 | cachedGlyph->mIsValid = true; |
| 186 | cachedGlyph->mCacheTexture = NULL; |
| 187 | return; |
| 188 | } |
| 189 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 190 | cachedGlyph->mIsValid = false; |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 191 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 192 | // If the glyph is too tall, don't cache it |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 193 | if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 194 | mCacheTextures[mCacheTextures.size() - 1]->getHeight()) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 195 | ALOGE("Font size too large to fit in cache. width, height = %i, %i", |
| 196 | (int) glyph.fWidth, (int) glyph.fHeight); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 197 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Now copy the bitmap into the cache texture |
| 201 | uint32_t startX = 0; |
| 202 | uint32_t startY = 0; |
| 203 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 204 | CacheTexture* cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 205 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 206 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 207 | if (!precaching) { |
| 208 | // If the new glyph didn't fit and we are not just trying to precache it, |
| 209 | // clear out the cache and try again |
| 210 | flushAllAndInvalidate(); |
| 211 | cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY); |
| 212 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 213 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 214 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 215 | // 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] | 216 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 220 | cachedGlyph->mCacheTexture = cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 221 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 222 | *retOriginX = startX; |
| 223 | *retOriginY = startY; |
| 224 | |
| 225 | uint32_t endX = startX + glyph.fWidth; |
| 226 | uint32_t endY = startY + glyph.fHeight; |
| 227 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 228 | uint32_t cacheWidth = cacheTexture->getWidth(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 229 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 230 | if (!cacheTexture->getTexture()) { |
| 231 | Caches::getInstance().activeTexture(0); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 232 | // Large-glyph texture memory is allocated only as needed |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 233 | cacheTexture->allocateTexture(); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 234 | } |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 235 | if (!cacheTexture->mesh()) { |
| 236 | cacheTexture->allocateMesh(); |
| 237 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 238 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 239 | // Tells us whether the glyphs is B&W (1 bit per pixel) |
| 240 | // or anti-aliased (8 bits per pixel) |
| 241 | SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 242 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 243 | uint8_t* cacheBuffer = cacheTexture->getTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 244 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 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 | |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 250 | uint32_t row = (startY - TEXTURE_BORDER_SIZE) * cacheWidth + startX - TEXTURE_BORDER_SIZE; |
| 251 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
| 252 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 253 | switch (format) { |
| 254 | case SkMask::kA8_Format: { |
| 255 | if (mGammaTable) { |
| 256 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) { |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 257 | row = cacheY * cacheWidth; |
| 258 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 259 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) { |
| 260 | uint8_t tempCol = bitmapBuffer[bY + bX]; |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 261 | cacheBuffer[row + cacheX] = mGammaTable[tempCol]; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 262 | } |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 263 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 264 | } |
| 265 | } else { |
| 266 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) { |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 267 | row = cacheY * cacheWidth; |
| 268 | memcpy(&cacheBuffer[row + startX], &bitmapBuffer[bY], glyph.fWidth); |
| 269 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
| 270 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 271 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 272 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 273 | break; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 274 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 275 | case SkMask::kBW_Format: { |
| 276 | static const uint8_t COLORS[2] = { 0, 255 }; |
| 277 | |
| 278 | for (cacheY = startY; cacheY < endY; cacheY++) { |
| 279 | cacheX = startX; |
| 280 | int rowBytes = stride; |
| 281 | uint8_t* buffer = bitmapBuffer; |
| 282 | |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 283 | row = cacheY * cacheWidth; |
| 284 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 285 | while (--rowBytes >= 0) { |
| 286 | uint8_t b = *buffer++; |
| 287 | for (int8_t mask = 7; mask >= 0 && cacheX < endX; mask--) { |
| 288 | cacheBuffer[cacheY * cacheWidth + cacheX++] = COLORS[(b >> mask) & 0x1]; |
| 289 | } |
| 290 | } |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 291 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 292 | |
| 293 | bitmapBuffer += stride; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 294 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 295 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 296 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 297 | default: |
| 298 | ALOGW("Unkown glyph format: 0x%x", format); |
| 299 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 300 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 301 | |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 302 | row = (endY + TEXTURE_BORDER_SIZE - 1) * cacheWidth + startX - TEXTURE_BORDER_SIZE; |
| 303 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
| 304 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 305 | cachedGlyph->mIsValid = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 308 | CacheTexture* FontRenderer::createCacheTexture(int width, int height, bool allocate) { |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 309 | CacheTexture* cacheTexture = new CacheTexture(width, height, mMaxNumberOfQuads); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 310 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 311 | if (allocate) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 312 | Caches::getInstance().activeTexture(0); |
| 313 | cacheTexture->allocateTexture(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 314 | cacheTexture->allocateMesh(); |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 315 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 316 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 317 | return cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void FontRenderer::initTextTexture() { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 321 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 322 | delete mCacheTextures[i]; |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 323 | } |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 324 | mCacheTextures.clear(); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 325 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 326 | mUploadTexture = false; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 327 | mCacheTextures.push(createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, true)); |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 328 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false)); |
| 329 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false)); |
| 330 | mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight, false)); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 331 | mCurrentCacheTexture = mCacheTextures[0]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Avoid having to reallocate memory and render quad by quad |
| 335 | void FontRenderer::initVertexArrayBuffers() { |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 336 | uint32_t numIndices = mMaxNumberOfQuads * 6; |
| 337 | uint32_t indexBufferSizeBytes = numIndices * sizeof(uint16_t); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 338 | uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 339 | |
| 340 | // Four verts, two triangles , six indices per quad |
| 341 | for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) { |
| 342 | int i6 = i * 6; |
| 343 | int i4 = i * 4; |
| 344 | |
| 345 | indexBufferData[i6 + 0] = i4 + 0; |
| 346 | indexBufferData[i6 + 1] = i4 + 1; |
| 347 | indexBufferData[i6 + 2] = i4 + 2; |
| 348 | |
| 349 | indexBufferData[i6 + 3] = i4 + 0; |
| 350 | indexBufferData[i6 + 4] = i4 + 2; |
| 351 | indexBufferData[i6 + 5] = i4 + 3; |
| 352 | } |
| 353 | |
| 354 | glGenBuffers(1, &mIndexBufferID); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 355 | Caches::getInstance().bindIndicesBuffer(mIndexBufferID); |
Romain Guy | 5d79441 | 2010-10-13 16:36:22 -0700 | [diff] [blame] | 356 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_STATIC_DRAW); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 357 | |
| 358 | free(indexBufferData); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // We don't want to allocate anything unless we actually draw text |
| 362 | void FontRenderer::checkInit() { |
| 363 | if (mInitialized) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | initTextTexture(); |
| 368 | initVertexArrayBuffers(); |
| 369 | |
| 370 | mInitialized = true; |
| 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() { |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 412 | bool first = true; |
| 413 | bool force = false; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 414 | |
Romain Guy | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame] | 415 | GLuint lastId = 0; |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 416 | Caches& caches = Caches::getInstance(); |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 417 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 418 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 419 | CacheTexture* texture = mCacheTextures[i]; |
| 420 | if (texture->canDraw()) { |
| 421 | if (first) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 422 | if (mFunctor) (*mFunctor)(0, NULL); |
| 423 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 424 | checkTextureUpdate(); |
| 425 | caches.bindIndicesBuffer(mIndexBufferID); |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 426 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 427 | if (!mDrawn) { |
| 428 | // If returns true, a VBO was bound and we must |
| 429 | // rebind our vertex attrib pointers even if |
| 430 | // they have the same values as the current pointers |
| 431 | force = caches.unbindMeshBuffer(); |
| 432 | } |
| 433 | |
| 434 | caches.activeTexture(0); |
| 435 | first = false; |
| 436 | } |
| 437 | |
| 438 | glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); |
| 439 | texture->setLinearFiltering(mLinearFiltering, false); |
| 440 | |
| 441 | TextureVertex* mesh = texture->mesh(); |
| 442 | caches.bindPositionVertexPointer(force, &mesh[0].position[0]); |
| 443 | caches.bindTexCoordsVertexPointer(force, &mesh[0].texture[0]); |
| 444 | force = false; |
| 445 | |
| 446 | glDrawElements(GL_TRIANGLES, texture->meshElementCount(), |
| 447 | GL_UNSIGNED_SHORT, texture->indices()); |
| 448 | |
| 449 | texture->resetMesh(); |
Romain Guy | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame] | 450 | } |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 451 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 452 | |
| 453 | mDrawn = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 456 | void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 457 | 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] | 458 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 459 | if (texture != mCurrentCacheTexture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 460 | // Now use the new texture id |
| 461 | mCurrentCacheTexture = texture; |
| 462 | } |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 463 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 464 | mCurrentCacheTexture->addQuad(x1, y1, u1, v1, x2, y2, u2, v2, |
| 465 | x3, y3, u3, v3, x4, y4, u4, v4); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void FontRenderer::appendMeshQuad(float x1, float y1, float u1, float v1, |
| 469 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 470 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 471 | |
| 472 | if (mClip && |
| 473 | (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom)) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | 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] | 478 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 479 | if (mBounds) { |
| 480 | mBounds->left = fmin(mBounds->left, x1); |
| 481 | mBounds->top = fmin(mBounds->top, y3); |
| 482 | mBounds->right = fmax(mBounds->right, x3); |
| 483 | mBounds->bottom = fmax(mBounds->bottom, y1); |
| 484 | } |
| 485 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 486 | if (mCurrentCacheTexture->endOfMesh()) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 487 | issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 491 | void FontRenderer::appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 492 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 493 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 494 | |
| 495 | appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture); |
| 496 | |
| 497 | if (mBounds) { |
| 498 | mBounds->left = fmin(mBounds->left, fmin(x1, fmin(x2, fmin(x3, x4)))); |
| 499 | mBounds->top = fmin(mBounds->top, fmin(y1, fmin(y2, fmin(y3, y4)))); |
| 500 | mBounds->right = fmax(mBounds->right, fmax(x1, fmax(x2, fmax(x3, x4)))); |
| 501 | mBounds->bottom = fmax(mBounds->bottom, fmax(y1, fmax(y2, fmax(y3, y4)))); |
| 502 | } |
| 503 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 504 | if (mCurrentCacheTexture->endOfMesh()) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 505 | issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 509 | void FontRenderer::setFont(SkPaint* paint, const mat4& matrix) { |
| 510 | mCurrentFont = Font::create(this, paint, matrix); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 511 | } |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 512 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 513 | FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 514 | 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] | 515 | checkInit(); |
| 516 | |
| 517 | if (!mCurrentFont) { |
| 518 | DropShadow image; |
| 519 | image.width = 0; |
| 520 | image.height = 0; |
| 521 | image.image = NULL; |
| 522 | image.penX = 0; |
| 523 | image.penY = 0; |
| 524 | return image; |
| 525 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 526 | |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 527 | mDrawn = false; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 528 | mClip = NULL; |
| 529 | mBounds = NULL; |
| 530 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 531 | Rect bounds; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 532 | mCurrentFont->measure(paint, text, startIndex, len, numGlyphs, &bounds, positions); |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 533 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 534 | uint32_t paddedWidth = (uint32_t) (bounds.right - bounds.left) + 2 * radius; |
| 535 | uint32_t paddedHeight = (uint32_t) (bounds.top - bounds.bottom) + 2 * radius; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 536 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 537 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 538 | // Align buffers for renderscript usage |
| 539 | if (paddedWidth & (RS_CPU_ALLOCATION_ALIGNMENT - 1)) { |
| 540 | paddedWidth += RS_CPU_ALLOCATION_ALIGNMENT - paddedWidth % RS_CPU_ALLOCATION_ALIGNMENT; |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 541 | } |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 542 | int size = paddedWidth * paddedHeight; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 543 | uint8_t* dataBuffer = (uint8_t*) memalign(RS_CPU_ALLOCATION_ALIGNMENT, size); |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 544 | #else |
| 545 | int size = paddedWidth * paddedHeight; |
| 546 | uint8_t* dataBuffer = (uint8_t*) malloc(size); |
| 547 | #endif |
| 548 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 549 | memset(dataBuffer, 0, size); |
| 550 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 551 | int penX = radius - bounds.left; |
| 552 | int penY = radius - bounds.bottom; |
| 553 | |
Chris Craik | dd8697c | 2013-02-22 10:41:36 -0800 | [diff] [blame] | 554 | if ((bounds.right > bounds.left) && (bounds.top > bounds.bottom)) { |
| 555 | // text has non-whitespace, so draw and blur to create the shadow |
| 556 | // NOTE: bounds.isEmpty() can't be used here, since vertical coordinates are inverted |
| 557 | // TODO: don't draw pure whitespace in the first place, and avoid needing this check |
| 558 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, penX, penY, |
| 559 | Font::BITMAP, dataBuffer, paddedWidth, paddedHeight, NULL, positions); |
| 560 | |
| 561 | blurImage(&dataBuffer, paddedWidth, paddedHeight, radius); |
| 562 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 563 | |
| 564 | DropShadow image; |
| 565 | image.width = paddedWidth; |
| 566 | image.height = paddedHeight; |
| 567 | image.image = dataBuffer; |
| 568 | image.penX = penX; |
| 569 | image.penY = penY; |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 570 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 571 | return image; |
| 572 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 573 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 574 | void FontRenderer::initRender(const Rect* clip, Rect* bounds, Functor* functor) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 575 | checkInit(); |
| 576 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 577 | mDrawn = false; |
| 578 | mBounds = bounds; |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 579 | mFunctor = functor; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 580 | mClip = clip; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 581 | } |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 582 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 583 | void FontRenderer::finishRender() { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 584 | mBounds = NULL; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 585 | mClip = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 586 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 587 | issueDrawCommand(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 590 | void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix) { |
| 591 | Font* font = Font::create(this, paint, matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 592 | font->precache(paint, text, numGlyphs); |
| 593 | } |
| 594 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 595 | bool FontRenderer::renderPosText(SkPaint* paint, const Rect* clip, const char *text, |
| 596 | uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 597 | const float* positions, Rect* bounds, Functor* functor) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 598 | if (!mCurrentFont) { |
| 599 | ALOGE("No font set"); |
| 600 | return false; |
| 601 | } |
| 602 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 603 | initRender(clip, bounds, functor); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 604 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, x, y, positions); |
| 605 | finishRender(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 606 | |
| 607 | return mDrawn; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 610 | bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, |
| 611 | uint32_t startIndex, uint32_t len, int numGlyphs, SkPath* path, |
| 612 | float hOffset, float vOffset, Rect* bounds) { |
| 613 | if (!mCurrentFont) { |
| 614 | ALOGE("No font set"); |
| 615 | return false; |
| 616 | } |
| 617 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 618 | initRender(clip, bounds, NULL); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 619 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, path, hOffset, vOffset); |
| 620 | finishRender(); |
| 621 | |
| 622 | return mDrawn; |
| 623 | } |
| 624 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 625 | void FontRenderer::removeFont(const Font* font) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 626 | mActiveFonts.remove(font->getDescription()); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 627 | |
| 628 | if (mCurrentFont == font) { |
| 629 | mCurrentFont = NULL; |
| 630 | } |
| 631 | } |
| 632 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 633 | void FontRenderer::blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius) { |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 634 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
| 635 | if (width * height * radius >= RS_MIN_INPUT_CUTOFF) { |
| 636 | uint8_t* outImage = (uint8_t*) memalign(RS_CPU_ALLOCATION_ALIGNMENT, width * height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 637 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 638 | if (mRs.get() == 0) { |
| 639 | mRs = new RSC::RS(); |
| 640 | if (!mRs->init(true, true)) { |
| 641 | ALOGE("blur RS failed to init"); |
| 642 | } |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 643 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 644 | mRsElement = RSC::Element::A_8(mRs); |
| 645 | mRsScript = new RSC::ScriptIntrinsicBlur(mRs, mRsElement); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 648 | sp<const RSC::Type> t = RSC::Type::create(mRs, mRsElement, width, height, 0); |
| 649 | sp<RSC::Allocation> ain = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE, |
| 650 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, *image); |
| 651 | sp<RSC::Allocation> aout = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE, |
| 652 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, outImage); |
| 653 | |
| 654 | mRsScript->setRadius(radius); |
| 655 | mRsScript->blur(ain, aout); |
| 656 | |
| 657 | // replace the original image's pointer, avoiding a copy back to the original buffer |
| 658 | free(*image); |
| 659 | *image = outImage; |
| 660 | |
| 661 | return; |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 662 | } |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 663 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 664 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 665 | float *gaussian = new float[2 * radius + 1]; |
| 666 | Blur::generateGaussianWeights(gaussian, radius); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 667 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 668 | uint8_t* scratch = new uint8_t[width * height]; |
| 669 | Blur::horizontal(gaussian, radius, *image, scratch, width, height); |
| 670 | Blur::vertical(gaussian, radius, scratch, *image, width, height); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 671 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame^] | 672 | delete[] gaussian; |
| 673 | delete[] scratch; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 676 | }; // namespace uirenderer |
| 677 | }; // namespace android |