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