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