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