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