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