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