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