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