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