Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Derek Sollenberger | ca79cf6 | 2012-08-14 16:44:52 -0400 | [diff] [blame] | 19 | #include <SkGlyph.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 20 | #include <SkUtils.h> |
| 21 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 23 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 26 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 27 | #include <RenderScript.h> |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 28 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 29 | |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 30 | #include "utils/Blur.h" |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 31 | #include "utils/Timing.h" |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 32 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 33 | #include "Caches.h" |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 34 | #include "Debug.h" |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 35 | #include "Extensions.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 36 | #include "FontRenderer.h" |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 37 | #include "OpenGLRenderer.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 | /////////////////////////////////////////////////////////////////////////////// |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 48 | // TextSetupFunctor |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | status_t TextSetupFunctor::operator ()(int what, void* data) { |
| 51 | Data* typedData = reinterpret_cast<Data*>(data); |
| 52 | GLenum glyphFormat = typedData ? typedData->glyphFormat : GL_ALPHA; |
| 53 | |
| 54 | renderer->setupDraw(); |
| 55 | renderer->setupDrawTextGamma(paint); |
| 56 | renderer->setupDrawDirtyRegionsDisabled(); |
| 57 | renderer->setupDrawWithTexture(glyphFormat == GL_ALPHA); |
| 58 | switch (glyphFormat) { |
| 59 | case GL_ALPHA: { |
| 60 | renderer->setupDrawAlpha8Color(paint->getColor(), alpha); |
| 61 | break; |
| 62 | } |
| 63 | case GL_RGBA: { |
| 64 | float floatAlpha = alpha / 255.0f; |
| 65 | renderer->setupDrawColor(floatAlpha, floatAlpha, floatAlpha, floatAlpha); |
| 66 | break; |
| 67 | } |
| 68 | default: { |
| 69 | #if DEBUG_FONT_RENDERER |
| 70 | ALOGD("TextSetupFunctor: called with unknown glyph format %x", glyphFormat); |
| 71 | #endif |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | renderer->setupDrawColorFilter(); |
| 76 | renderer->setupDrawShader(); |
| 77 | renderer->setupDrawBlending(true, mode); |
| 78 | renderer->setupDrawProgram(); |
| 79 | renderer->setupDrawModelView(x, y, x, y, pureTranslate, true); |
| 80 | // Calling setupDrawTexture with the name 0 will enable the |
| 81 | // uv attributes and increase the texture unit count |
| 82 | // texture binding will be performed by the font renderer as |
| 83 | // needed |
| 84 | renderer->setupDrawTexture(0); |
| 85 | renderer->setupDrawPureColorUniforms(); |
| 86 | renderer->setupDrawColorFilterUniforms(); |
| 87 | renderer->setupDrawShaderUniforms(pureTranslate); |
| 88 | renderer->setupDrawTextGammaUniforms(); |
| 89 | |
| 90 | return NO_ERROR; |
| 91 | } |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 94 | // FontRenderer |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 97 | static bool sLogFontRendererCreate = true; |
| 98 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 99 | FontRenderer::FontRenderer() : |
| 100 | mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity) { |
| 101 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 102 | if (sLogFontRendererCreate) { |
| 103 | INIT_LOGD("Creating FontRenderer"); |
| 104 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 105 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 106 | mGammaTable = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 107 | mInitialized = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 108 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 109 | mCurrentCacheTexture = NULL; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 110 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 111 | mLinearFiltering = false; |
| 112 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 113 | mSmallCacheWidth = DEFAULT_TEXT_SMALL_CACHE_WIDTH; |
| 114 | mSmallCacheHeight = DEFAULT_TEXT_SMALL_CACHE_HEIGHT; |
| 115 | mLargeCacheWidth = DEFAULT_TEXT_LARGE_CACHE_WIDTH; |
| 116 | mLargeCacheHeight = DEFAULT_TEXT_LARGE_CACHE_HEIGHT; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 117 | |
| 118 | char property[PROPERTY_VALUE_MAX]; |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 119 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_WIDTH, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 120 | mSmallCacheWidth = atoi(property); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 121 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 122 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 123 | if (property_get(PROPERTY_TEXT_SMALL_CACHE_HEIGHT, property, NULL) > 0) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 124 | mSmallCacheHeight = atoi(property); |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 125 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 126 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 127 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_WIDTH, property, NULL) > 0) { |
| 128 | mLargeCacheWidth = atoi(property); |
| 129 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 130 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 131 | if (property_get(PROPERTY_TEXT_LARGE_CACHE_HEIGHT, property, NULL) > 0) { |
| 132 | mLargeCacheHeight = atoi(property); |
| 133 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 134 | |
| 135 | uint32_t maxTextureSize = (uint32_t) Caches::getInstance().maxTextureSize; |
| 136 | mSmallCacheWidth = mSmallCacheWidth > maxTextureSize ? maxTextureSize : mSmallCacheWidth; |
| 137 | mSmallCacheHeight = mSmallCacheHeight > maxTextureSize ? maxTextureSize : mSmallCacheHeight; |
| 138 | mLargeCacheWidth = mLargeCacheWidth > maxTextureSize ? maxTextureSize : mLargeCacheWidth; |
| 139 | mLargeCacheHeight = mLargeCacheHeight > maxTextureSize ? maxTextureSize : mLargeCacheHeight; |
| 140 | |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 141 | if (sLogFontRendererCreate) { |
| 142 | INIT_LOGD(" Text cache sizes, in pixels: %i x %i, %i x %i, %i x %i, %i x %i", |
| 143 | mSmallCacheWidth, mSmallCacheHeight, |
| 144 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 145 | mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 146 | mLargeCacheWidth, mLargeCacheHeight); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 147 | } |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 148 | |
| 149 | sLogFontRendererCreate = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 152 | void clearCacheTextures(Vector<CacheTexture*>& cacheTextures) { |
| 153 | for (uint32_t i = 0; i < cacheTextures.size(); i++) { |
| 154 | delete cacheTextures[i]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 155 | } |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 156 | cacheTextures.clear(); |
| 157 | } |
| 158 | |
| 159 | FontRenderer::~FontRenderer() { |
| 160 | clearCacheTextures(mACacheTextures); |
| 161 | clearCacheTextures(mRGBACacheTextures); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 162 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 163 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 164 | while (it.next()) { |
| 165 | delete it.value(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 166 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 167 | mActiveFonts.clear(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void FontRenderer::flushAllAndInvalidate() { |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 171 | issueDrawCommand(); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 172 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 173 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 174 | while (it.next()) { |
| 175 | it.value()->invalidateTextureCache(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 176 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 177 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 178 | for (uint32_t i = 0; i < mACacheTextures.size(); i++) { |
| 179 | mACacheTextures[i]->init(); |
| 180 | } |
| 181 | |
| 182 | for (uint32_t i = 0; i < mRGBACacheTextures.size(); i++) { |
| 183 | mRGBACacheTextures[i]->init(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 187 | void FontRenderer::flushLargeCaches(Vector<CacheTexture*>& cacheTextures) { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 188 | // Start from 1; don't deallocate smallest/default texture |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 189 | for (uint32_t i = 1; i < cacheTextures.size(); i++) { |
| 190 | CacheTexture* cacheTexture = cacheTextures[i]; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 191 | if (cacheTexture->getPixelBuffer()) { |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 192 | cacheTexture->init(); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 193 | LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts); |
| 194 | while (it.next()) { |
| 195 | it.value()->invalidateTextureCache(cacheTexture); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 196 | } |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 197 | cacheTexture->releaseTexture(); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 202 | void FontRenderer::flushLargeCaches() { |
| 203 | flushLargeCaches(mACacheTextures); |
| 204 | flushLargeCaches(mRGBACacheTextures); |
| 205 | } |
| 206 | |
| 207 | CacheTexture* FontRenderer::cacheBitmapInTexture(Vector<CacheTexture*>& cacheTextures, |
| 208 | const SkGlyph& glyph, uint32_t* startX, uint32_t* startY) { |
| 209 | for (uint32_t i = 0; i < cacheTextures.size(); i++) { |
| 210 | if (cacheTextures[i]->fitBitmap(glyph, startX, startY)) { |
| 211 | return cacheTextures[i]; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | // Could not fit glyph into current cache textures |
| 215 | return NULL; |
| 216 | } |
| 217 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 218 | void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 219 | uint32_t* retOriginX, uint32_t* retOriginY, bool precaching) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 220 | checkInit(); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 221 | |
| 222 | // If the glyph bitmap is empty let's assum the glyph is valid |
| 223 | // so we can avoid doing extra work later on |
| 224 | if (glyph.fWidth == 0 || glyph.fHeight == 0) { |
| 225 | cachedGlyph->mIsValid = true; |
| 226 | cachedGlyph->mCacheTexture = NULL; |
| 227 | return; |
| 228 | } |
| 229 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 230 | cachedGlyph->mIsValid = false; |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 231 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 232 | // choose an appropriate cache texture list for this glyph format |
| 233 | SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat); |
| 234 | Vector<CacheTexture*>* cacheTextures = NULL; |
| 235 | switch (format) { |
| 236 | case SkMask::kA8_Format: |
| 237 | cacheTextures = &mACacheTextures; |
| 238 | break; |
| 239 | case SkMask::kARGB32_Format: |
| 240 | cacheTextures = &mRGBACacheTextures; |
| 241 | break; |
| 242 | default: |
| 243 | #if DEBUG_FONT_RENDERER |
| 244 | ALOGD("getCacheTexturesForFormat: unknown SkMask format %x", format); |
| 245 | #endif |
| 246 | return; |
| 247 | } |
| 248 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 249 | // If the glyph is too tall, don't cache it |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 250 | if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 > |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 251 | (*cacheTextures)[cacheTextures->size() - 1]->getHeight()) { |
Chet Haase | 2efd5c5 | 2012-08-15 13:15:16 -0700 | [diff] [blame] | 252 | ALOGE("Font size too large to fit in cache. width, height = %i, %i", |
| 253 | (int) glyph.fWidth, (int) glyph.fHeight); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 254 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Now copy the bitmap into the cache texture |
| 258 | uint32_t startX = 0; |
| 259 | uint32_t startY = 0; |
| 260 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 261 | CacheTexture* cacheTexture = cacheBitmapInTexture(*cacheTextures, glyph, &startX, &startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 262 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 263 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 264 | if (!precaching) { |
| 265 | // If the new glyph didn't fit and we are not just trying to precache it, |
| 266 | // clear out the cache and try again |
| 267 | flushAllAndInvalidate(); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 268 | cacheTexture = cacheBitmapInTexture(*cacheTextures, glyph, &startX, &startY); |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 269 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 270 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 271 | if (!cacheTexture) { |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 272 | // 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] | 273 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 277 | cachedGlyph->mCacheTexture = cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 278 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 279 | *retOriginX = startX; |
| 280 | *retOriginY = startY; |
| 281 | |
| 282 | uint32_t endX = startX + glyph.fWidth; |
| 283 | uint32_t endY = startY + glyph.fHeight; |
| 284 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 285 | uint32_t cacheWidth = cacheTexture->getWidth(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 286 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 287 | if (!cacheTexture->getPixelBuffer()) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 288 | Caches::getInstance().activeTexture(0); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 289 | // Large-glyph texture memory is allocated only as needed |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 290 | cacheTexture->allocateTexture(); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 291 | } |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 292 | if (!cacheTexture->mesh()) { |
| 293 | cacheTexture->allocateMesh(); |
| 294 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 295 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 296 | uint8_t* cacheBuffer = cacheTexture->getPixelBuffer()->map(); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 297 | uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage; |
| 298 | int srcStride = glyph.rowBytes(); |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 299 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 300 | // Copy the glyph image, taking the mask format into account |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 301 | switch (format) { |
| 302 | case SkMask::kA8_Format: { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 303 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
| 304 | uint32_t row = (startY - TEXTURE_BORDER_SIZE) * cacheWidth + startX |
| 305 | - TEXTURE_BORDER_SIZE; |
| 306 | // write leading border line |
| 307 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
| 308 | // write glyph data |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 309 | if (mGammaTable) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 310 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += srcStride) { |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 311 | row = cacheY * cacheWidth; |
| 312 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 313 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) { |
| 314 | uint8_t tempCol = bitmapBuffer[bY + bX]; |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 315 | cacheBuffer[row + cacheX] = mGammaTable[tempCol]; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 316 | } |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 317 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 318 | } |
| 319 | } else { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 320 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += srcStride) { |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 321 | row = cacheY * cacheWidth; |
| 322 | memcpy(&cacheBuffer[row + startX], &bitmapBuffer[bY], glyph.fWidth); |
| 323 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
| 324 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 325 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 326 | } |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 327 | // write trailing border line |
| 328 | row = (endY + TEXTURE_BORDER_SIZE - 1) * cacheWidth + startX - TEXTURE_BORDER_SIZE; |
| 329 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
| 330 | break; |
| 331 | } |
| 332 | case SkMask::kARGB32_Format: { |
| 333 | // prep data lengths |
| 334 | const size_t formatSize = PixelBuffer::formatSize(GL_RGBA); |
| 335 | const size_t borderSize = formatSize * TEXTURE_BORDER_SIZE; |
| 336 | size_t rowSize = formatSize * glyph.fWidth; |
| 337 | // prep advances |
| 338 | size_t dstStride = formatSize * cacheWidth; |
| 339 | // prep indices |
| 340 | // - we actually start one row early, and then increment before first copy |
| 341 | uint8_t* src = &bitmapBuffer[0 - srcStride]; |
| 342 | uint8_t* dst = &cacheBuffer[cacheTexture->getOffset(startX, startY - 1)]; |
| 343 | uint8_t* dstEnd = &cacheBuffer[cacheTexture->getOffset(startX, endY - 1)]; |
| 344 | uint8_t* dstL = dst - borderSize; |
| 345 | uint8_t* dstR = dst + rowSize; |
| 346 | // write leading border line |
| 347 | memset(dstL, 0, rowSize + 2 * borderSize); |
| 348 | // write glyph data |
| 349 | while (dst < dstEnd) { |
| 350 | memset(dstL += dstStride, 0, borderSize); // leading border column |
| 351 | memcpy(dst += dstStride, src += srcStride, rowSize); // glyph data |
| 352 | memset(dstR += dstStride, 0, borderSize); // trailing border column |
| 353 | } |
| 354 | // write trailing border line |
| 355 | memset(dstL, 0, rowSize + 2 * borderSize); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 356 | break; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 357 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 358 | case SkMask::kBW_Format: { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 359 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
| 360 | uint32_t row = (startY - TEXTURE_BORDER_SIZE) * cacheWidth + startX |
| 361 | - TEXTURE_BORDER_SIZE; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 362 | static const uint8_t COLORS[2] = { 0, 255 }; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 363 | // write leading border line |
| 364 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
| 365 | // write glyph data |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 366 | for (cacheY = startY; cacheY < endY; cacheY++) { |
| 367 | cacheX = startX; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 368 | int rowBytes = srcStride; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 369 | uint8_t* buffer = bitmapBuffer; |
| 370 | |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 371 | row = cacheY * cacheWidth; |
| 372 | cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 373 | while (--rowBytes >= 0) { |
| 374 | uint8_t b = *buffer++; |
| 375 | for (int8_t mask = 7; mask >= 0 && cacheX < endX; mask--) { |
| 376 | cacheBuffer[cacheY * cacheWidth + cacheX++] = COLORS[(b >> mask) & 0x1]; |
| 377 | } |
| 378 | } |
Romain Guy | 0b58a3d | 2013-03-05 12:16:27 -0800 | [diff] [blame] | 379 | cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 380 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 381 | bitmapBuffer += srcStride; |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 382 | } |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 383 | // write trailing border line |
| 384 | row = (endY + TEXTURE_BORDER_SIZE - 1) * cacheWidth + startX - TEXTURE_BORDER_SIZE; |
| 385 | memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 386 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 387 | } |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 388 | default: |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 389 | ALOGW("Unknown glyph format: 0x%x", format); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 390 | break; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 391 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 392 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 393 | cachedGlyph->mIsValid = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 396 | CacheTexture* FontRenderer::createCacheTexture(int width, int height, GLenum format, |
| 397 | bool allocate) { |
| 398 | CacheTexture* cacheTexture = new CacheTexture(width, height, format, gMaxNumberOfQuads); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 399 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 400 | if (allocate) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 401 | Caches::getInstance().activeTexture(0); |
| 402 | cacheTexture->allocateTexture(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 403 | cacheTexture->allocateMesh(); |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 404 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 405 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 406 | return cacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void FontRenderer::initTextTexture() { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 410 | clearCacheTextures(mACacheTextures); |
| 411 | clearCacheTextures(mRGBACacheTextures); |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 412 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 413 | mUploadTexture = false; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 414 | mACacheTextures.push(createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, |
| 415 | GL_ALPHA, true)); |
| 416 | mACacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 417 | GL_ALPHA, false)); |
| 418 | mACacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 419 | GL_ALPHA, false)); |
| 420 | mACacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight, |
| 421 | GL_ALPHA, false)); |
| 422 | mRGBACacheTextures.push(createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, |
| 423 | GL_RGBA, false)); |
| 424 | mRGBACacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, |
| 425 | GL_RGBA, false)); |
| 426 | mCurrentCacheTexture = mACacheTextures[0]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 429 | // We don't want to allocate anything unless we actually draw text |
| 430 | void FontRenderer::checkInit() { |
| 431 | if (mInitialized) { |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | initTextTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 436 | |
| 437 | mInitialized = true; |
| 438 | } |
| 439 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 440 | void checkTextureUpdateForCache(Caches& caches, Vector<CacheTexture*>& cacheTextures, |
| 441 | bool& resetPixelStore, GLuint& lastTextureId) { |
| 442 | for (uint32_t i = 0; i < cacheTextures.size(); i++) { |
| 443 | CacheTexture* cacheTexture = cacheTextures[i]; |
| 444 | if (cacheTexture->isDirty() && cacheTexture->getPixelBuffer()) { |
| 445 | if (cacheTexture->getTextureId() != lastTextureId) { |
| 446 | lastTextureId = cacheTexture->getTextureId(); |
| 447 | caches.activeTexture(0); |
| 448 | caches.bindTexture(lastTextureId); |
| 449 | } |
| 450 | |
| 451 | if (cacheTexture->upload()) { |
| 452 | resetPixelStore = true; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 458 | void FontRenderer::checkTextureUpdate() { |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 459 | if (!mUploadTexture) { |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 460 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 463 | Caches& caches = Caches::getInstance(); |
| 464 | GLuint lastTextureId = 0; |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 465 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 466 | bool resetPixelStore = false; |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 467 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 468 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 469 | // Iterate over all the cache textures and see which ones need to be updated |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 470 | checkTextureUpdateForCache(caches, mACacheTextures, resetPixelStore, lastTextureId); |
| 471 | checkTextureUpdateForCache(caches, mRGBACacheTextures, resetPixelStore, lastTextureId); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 472 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 473 | // Unbind any PBO we might have used to update textures |
| 474 | caches.unbindPixelBuffer(); |
| 475 | |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 476 | // Reset to default unpack row length to avoid affecting texture |
| 477 | // uploads in other parts of the renderer |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 478 | if (resetPixelStore) { |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 479 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 480 | } |
| 481 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 482 | mUploadTexture = false; |
| 483 | } |
| 484 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 485 | void FontRenderer::issueDrawCommand(Vector<CacheTexture*>& cacheTextures) { |
| 486 | Caches& caches = Caches::getInstance(); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 487 | bool first = true; |
| 488 | bool force = false; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 489 | for (uint32_t i = 0; i < cacheTextures.size(); i++) { |
| 490 | CacheTexture* texture = cacheTextures[i]; |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 491 | if (texture->canDraw()) { |
| 492 | if (first) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 493 | if (mFunctor) { |
| 494 | TextSetupFunctor::Data functorData(texture->getFormat()); |
| 495 | (*mFunctor)(0, &functorData); |
| 496 | } |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 497 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 498 | checkTextureUpdate(); |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 499 | caches.bindIndicesBuffer(); |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 500 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 501 | if (!mDrawn) { |
| 502 | // If returns true, a VBO was bound and we must |
| 503 | // rebind our vertex attrib pointers even if |
| 504 | // they have the same values as the current pointers |
| 505 | force = caches.unbindMeshBuffer(); |
| 506 | } |
| 507 | |
| 508 | caches.activeTexture(0); |
| 509 | first = false; |
| 510 | } |
| 511 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 512 | caches.bindTexture(texture->getTextureId()); |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 513 | texture->setLinearFiltering(mLinearFiltering, false); |
| 514 | |
| 515 | TextureVertex* mesh = texture->mesh(); |
| 516 | caches.bindPositionVertexPointer(force, &mesh[0].position[0]); |
| 517 | caches.bindTexCoordsVertexPointer(force, &mesh[0].texture[0]); |
| 518 | force = false; |
| 519 | |
| 520 | glDrawElements(GL_TRIANGLES, texture->meshElementCount(), |
| 521 | GL_UNSIGNED_SHORT, texture->indices()); |
| 522 | |
| 523 | texture->resetMesh(); |
Romain Guy | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame] | 524 | } |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 525 | } |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 526 | } |
| 527 | |
| 528 | void FontRenderer::issueDrawCommand() { |
| 529 | issueDrawCommand(mACacheTextures); |
| 530 | issueDrawCommand(mRGBACacheTextures); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 531 | |
| 532 | mDrawn = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 535 | void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 536 | 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] | 537 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 538 | if (texture != mCurrentCacheTexture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 539 | // Now use the new texture id |
| 540 | mCurrentCacheTexture = texture; |
| 541 | } |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 542 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 543 | mCurrentCacheTexture->addQuad(x1, y1, u1, v1, x2, y2, u2, v2, |
| 544 | x3, y3, u3, v3, x4, y4, u4, v4); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void FontRenderer::appendMeshQuad(float x1, float y1, float u1, float v1, |
| 548 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 549 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 550 | |
| 551 | if (mClip && |
| 552 | (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom)) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | 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] | 557 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 558 | if (mBounds) { |
| 559 | mBounds->left = fmin(mBounds->left, x1); |
| 560 | mBounds->top = fmin(mBounds->top, y3); |
| 561 | mBounds->right = fmax(mBounds->right, x3); |
| 562 | mBounds->bottom = fmax(mBounds->bottom, y1); |
| 563 | } |
| 564 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 565 | if (mCurrentCacheTexture->endOfMesh()) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 566 | issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 570 | void FontRenderer::appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 571 | float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3, |
| 572 | float x4, float y4, float u4, float v4, CacheTexture* texture) { |
| 573 | |
| 574 | appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture); |
| 575 | |
| 576 | if (mBounds) { |
| 577 | mBounds->left = fmin(mBounds->left, fmin(x1, fmin(x2, fmin(x3, x4)))); |
| 578 | mBounds->top = fmin(mBounds->top, fmin(y1, fmin(y2, fmin(y3, y4)))); |
| 579 | mBounds->right = fmax(mBounds->right, fmax(x1, fmax(x2, fmax(x3, x4)))); |
| 580 | mBounds->bottom = fmax(mBounds->bottom, fmax(y1, fmax(y2, fmax(y3, y4)))); |
| 581 | } |
| 582 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 583 | if (mCurrentCacheTexture->endOfMesh()) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 584 | issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 588 | void FontRenderer::setFont(SkPaint* paint, const mat4& matrix) { |
| 589 | mCurrentFont = Font::create(this, paint, matrix); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 590 | } |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 591 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 592 | FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 593 | 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] | 594 | checkInit(); |
| 595 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 596 | DropShadow image; |
| 597 | image.width = 0; |
| 598 | image.height = 0; |
| 599 | image.image = NULL; |
| 600 | image.penX = 0; |
| 601 | image.penY = 0; |
| 602 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 603 | if (!mCurrentFont) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 604 | return image; |
| 605 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 606 | |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 607 | mDrawn = false; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 608 | mClip = NULL; |
| 609 | mBounds = NULL; |
| 610 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 611 | Rect bounds; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 612 | mCurrentFont->measure(paint, text, startIndex, len, numGlyphs, &bounds, positions); |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 613 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 614 | uint32_t paddedWidth = (uint32_t) (bounds.right - bounds.left) + 2 * radius; |
| 615 | uint32_t paddedHeight = (uint32_t) (bounds.top - bounds.bottom) + 2 * radius; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 616 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 617 | uint32_t maxSize = Caches::getInstance().maxTextureSize; |
| 618 | if (paddedWidth > maxSize || paddedHeight > maxSize) { |
| 619 | return image; |
| 620 | } |
| 621 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 622 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 623 | // Align buffers for renderscript usage |
| 624 | if (paddedWidth & (RS_CPU_ALLOCATION_ALIGNMENT - 1)) { |
| 625 | paddedWidth += RS_CPU_ALLOCATION_ALIGNMENT - paddedWidth % RS_CPU_ALLOCATION_ALIGNMENT; |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 626 | } |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 627 | int size = paddedWidth * paddedHeight; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 628 | uint8_t* dataBuffer = (uint8_t*) memalign(RS_CPU_ALLOCATION_ALIGNMENT, size); |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 629 | #else |
| 630 | int size = paddedWidth * paddedHeight; |
| 631 | uint8_t* dataBuffer = (uint8_t*) malloc(size); |
| 632 | #endif |
| 633 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 634 | memset(dataBuffer, 0, size); |
| 635 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 636 | int penX = radius - bounds.left; |
| 637 | int penY = radius - bounds.bottom; |
| 638 | |
Chris Craik | dd8697c | 2013-02-22 10:41:36 -0800 | [diff] [blame] | 639 | if ((bounds.right > bounds.left) && (bounds.top > bounds.bottom)) { |
| 640 | // text has non-whitespace, so draw and blur to create the shadow |
| 641 | // NOTE: bounds.isEmpty() can't be used here, since vertical coordinates are inverted |
| 642 | // TODO: don't draw pure whitespace in the first place, and avoid needing this check |
| 643 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, penX, penY, |
| 644 | Font::BITMAP, dataBuffer, paddedWidth, paddedHeight, NULL, positions); |
| 645 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 646 | // Unbind any PBO we might have used |
| 647 | Caches::getInstance().unbindPixelBuffer(); |
| 648 | |
Chris Craik | dd8697c | 2013-02-22 10:41:36 -0800 | [diff] [blame] | 649 | blurImage(&dataBuffer, paddedWidth, paddedHeight, radius); |
| 650 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 651 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 652 | image.width = paddedWidth; |
| 653 | image.height = paddedHeight; |
| 654 | image.image = dataBuffer; |
| 655 | image.penX = penX; |
| 656 | image.penY = penY; |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 657 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 658 | return image; |
| 659 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 660 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 661 | void FontRenderer::initRender(const Rect* clip, Rect* bounds, Functor* functor) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 662 | checkInit(); |
| 663 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 664 | mDrawn = false; |
| 665 | mBounds = bounds; |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 666 | mFunctor = functor; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 667 | mClip = clip; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 668 | } |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 669 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 670 | void FontRenderer::finishRender() { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 671 | mBounds = NULL; |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 672 | mClip = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 673 | |
Romain Guy | 661a87e | 2013-03-19 15:24:36 -0700 | [diff] [blame] | 674 | issueDrawCommand(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 677 | void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix) { |
| 678 | Font* font = Font::create(this, paint, matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 679 | font->precache(paint, text, numGlyphs); |
| 680 | } |
| 681 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 682 | void FontRenderer::endPrecaching() { |
| 683 | checkTextureUpdate(); |
| 684 | } |
| 685 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 686 | bool FontRenderer::renderPosText(SkPaint* paint, const Rect* clip, const char *text, |
| 687 | uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 688 | const float* positions, Rect* bounds, Functor* functor, bool forceFinish) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 689 | if (!mCurrentFont) { |
| 690 | ALOGE("No font set"); |
| 691 | return false; |
| 692 | } |
| 693 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 694 | initRender(clip, bounds, functor); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 695 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, x, y, positions); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 696 | |
| 697 | if (forceFinish) { |
| 698 | finishRender(); |
| 699 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 700 | |
| 701 | return mDrawn; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 704 | bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, |
| 705 | uint32_t startIndex, uint32_t len, int numGlyphs, SkPath* path, |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 706 | float hOffset, float vOffset, Rect* bounds, Functor* functor) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 707 | if (!mCurrentFont) { |
| 708 | ALOGE("No font set"); |
| 709 | return false; |
| 710 | } |
| 711 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 712 | initRender(clip, bounds, functor); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 713 | mCurrentFont->render(paint, text, startIndex, len, numGlyphs, path, hOffset, vOffset); |
| 714 | finishRender(); |
| 715 | |
| 716 | return mDrawn; |
| 717 | } |
| 718 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 719 | void FontRenderer::removeFont(const Font* font) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 720 | mActiveFonts.remove(font->getDescription()); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 721 | |
| 722 | if (mCurrentFont == font) { |
| 723 | mCurrentFont = NULL; |
| 724 | } |
| 725 | } |
| 726 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 727 | 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] | 728 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
| 729 | if (width * height * radius >= RS_MIN_INPUT_CUTOFF) { |
| 730 | uint8_t* outImage = (uint8_t*) memalign(RS_CPU_ALLOCATION_ALIGNMENT, width * height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 731 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 732 | if (mRs.get() == 0) { |
| 733 | mRs = new RSC::RS(); |
| 734 | if (!mRs->init(true, true)) { |
| 735 | ALOGE("blur RS failed to init"); |
| 736 | } |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 737 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 738 | mRsElement = RSC::Element::A_8(mRs); |
| 739 | mRsScript = new RSC::ScriptIntrinsicBlur(mRs, mRsElement); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 742 | sp<const RSC::Type> t = RSC::Type::create(mRs, mRsElement, width, height, 0); |
| 743 | sp<RSC::Allocation> ain = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE, |
| 744 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, *image); |
| 745 | sp<RSC::Allocation> aout = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE, |
| 746 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, outImage); |
| 747 | |
| 748 | mRsScript->setRadius(radius); |
| 749 | mRsScript->blur(ain, aout); |
| 750 | |
| 751 | // replace the original image's pointer, avoiding a copy back to the original buffer |
| 752 | free(*image); |
| 753 | *image = outImage; |
| 754 | |
| 755 | return; |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 756 | } |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 757 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 758 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 759 | float *gaussian = new float[2 * radius + 1]; |
| 760 | Blur::generateGaussianWeights(gaussian, radius); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 761 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 762 | uint8_t* scratch = new uint8_t[width * height]; |
| 763 | Blur::horizontal(gaussian, radius, *image, scratch, width, height); |
| 764 | Blur::vertical(gaussian, radius, scratch, *image, width, height); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 765 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 766 | delete[] gaussian; |
| 767 | delete[] scratch; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 770 | static uint32_t calculateCacheSize(const Vector<CacheTexture*>& cacheTextures) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 771 | uint32_t size = 0; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 772 | for (uint32_t i = 0; i < cacheTextures.size(); i++) { |
| 773 | CacheTexture* cacheTexture = cacheTextures[i]; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 774 | if (cacheTexture && cacheTexture->getPixelBuffer()) { |
| 775 | size += cacheTexture->getPixelBuffer()->getSize(); |
| 776 | } |
| 777 | } |
| 778 | return size; |
| 779 | } |
| 780 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame^] | 781 | uint32_t FontRenderer::getCacheSize(GLenum format) const { |
| 782 | switch (format) { |
| 783 | case GL_ALPHA: { |
| 784 | return calculateCacheSize(mACacheTextures); |
| 785 | } |
| 786 | case GL_RGBA: { |
| 787 | return calculateCacheSize(mRGBACacheTextures); |
| 788 | } |
| 789 | default: { |
| 790 | return 0; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 795 | }; // namespace uirenderer |
| 796 | }; // namespace android |