2010-07-21 21:33:20 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
|
|
|
|
2012-08-14 16:44:52 -04:00
|
|
|
#include <SkGlyph.h>
|
2010-07-21 21:33:20 -07:00
|
|
|
#include <SkUtils.h>
|
|
|
|
|
2010-07-23 00:28:00 -07:00
|
|
|
#include <cutils/properties.h>
|
2010-09-24 18:39:22 -07:00
|
|
|
|
2010-07-23 00:28:00 -07:00
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
#include "RenderScript.h"
|
|
|
|
|
|
|
|
#include "utils/Timing.h"
|
2011-12-13 13:11:32 -08:00
|
|
|
#include "Caches.h"
|
2011-01-21 21:14:15 -08:00
|
|
|
#include "Debug.h"
|
2010-07-23 00:28:00 -07:00
|
|
|
#include "FontRenderer.h"
|
2012-09-04 12:55:44 -07:00
|
|
|
#include "Rect.h"
|
2010-07-23 00:28:00 -07:00
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
// blur inputs smaller than this constant will bypass renderscript
|
|
|
|
#define RS_MIN_INPUT_CUTOFF 10000
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FontRenderer
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-01-19 14:38:29 -08:00
|
|
|
static bool sLogFontRendererCreate = true;
|
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
FontRenderer::FontRenderer() :
|
|
|
|
mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity) {
|
|
|
|
|
2011-01-21 21:14:15 -08:00
|
|
|
if (sLogFontRendererCreate) {
|
|
|
|
INIT_LOGD("Creating FontRenderer");
|
|
|
|
}
|
2010-07-23 00:28:00 -07:00
|
|
|
|
2010-08-26 20:35:23 -07:00
|
|
|
mGammaTable = NULL;
|
2010-07-21 21:33:20 -07:00
|
|
|
mInitialized = false;
|
|
|
|
mMaxNumberOfQuads = 1024;
|
|
|
|
mCurrentQuadIndex = 0;
|
2012-11-16 00:03:17 +09:00
|
|
|
mLastQuadIndex = 0;
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2012-09-04 15:22:57 -07:00
|
|
|
mTextMesh = NULL;
|
2011-12-05 16:35:38 -08:00
|
|
|
mCurrentCacheTexture = NULL;
|
2010-08-07 23:46:15 -07:00
|
|
|
|
2011-12-14 15:22:56 -08:00
|
|
|
mLinearFiltering = false;
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
mIndexBufferID = 0;
|
|
|
|
|
2012-08-31 13:54:03 -07:00
|
|
|
mSmallCacheWidth = DEFAULT_TEXT_SMALL_CACHE_WIDTH;
|
|
|
|
mSmallCacheHeight = DEFAULT_TEXT_SMALL_CACHE_HEIGHT;
|
|
|
|
mLargeCacheWidth = DEFAULT_TEXT_LARGE_CACHE_WIDTH;
|
|
|
|
mLargeCacheHeight = DEFAULT_TEXT_LARGE_CACHE_HEIGHT;
|
2010-07-23 00:28:00 -07:00
|
|
|
|
|
|
|
char property[PROPERTY_VALUE_MAX];
|
2012-08-31 13:54:03 -07:00
|
|
|
if (property_get(PROPERTY_TEXT_SMALL_CACHE_WIDTH, property, NULL) > 0) {
|
2011-12-05 16:35:38 -08:00
|
|
|
mSmallCacheWidth = atoi(property);
|
2010-07-23 00:28:00 -07:00
|
|
|
}
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2012-08-31 13:54:03 -07:00
|
|
|
if (property_get(PROPERTY_TEXT_SMALL_CACHE_HEIGHT, property, NULL) > 0) {
|
2011-12-05 16:35:38 -08:00
|
|
|
mSmallCacheHeight = atoi(property);
|
2012-08-31 13:54:03 -07:00
|
|
|
}
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2012-08-31 13:54:03 -07:00
|
|
|
if (property_get(PROPERTY_TEXT_LARGE_CACHE_WIDTH, property, NULL) > 0) {
|
|
|
|
mLargeCacheWidth = atoi(property);
|
|
|
|
}
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2012-08-31 13:54:03 -07:00
|
|
|
if (property_get(PROPERTY_TEXT_LARGE_CACHE_HEIGHT, property, NULL) > 0) {
|
|
|
|
mLargeCacheHeight = atoi(property);
|
|
|
|
}
|
2012-09-04 12:55:44 -07:00
|
|
|
|
|
|
|
uint32_t maxTextureSize = (uint32_t) Caches::getInstance().maxTextureSize;
|
|
|
|
mSmallCacheWidth = mSmallCacheWidth > maxTextureSize ? maxTextureSize : mSmallCacheWidth;
|
|
|
|
mSmallCacheHeight = mSmallCacheHeight > maxTextureSize ? maxTextureSize : mSmallCacheHeight;
|
|
|
|
mLargeCacheWidth = mLargeCacheWidth > maxTextureSize ? maxTextureSize : mLargeCacheWidth;
|
|
|
|
mLargeCacheHeight = mLargeCacheHeight > maxTextureSize ? maxTextureSize : mLargeCacheHeight;
|
|
|
|
|
2012-08-31 13:54:03 -07:00
|
|
|
if (sLogFontRendererCreate) {
|
|
|
|
INIT_LOGD(" Text cache sizes, in pixels: %i x %i, %i x %i, %i x %i, %i x %i",
|
|
|
|
mSmallCacheWidth, mSmallCacheHeight,
|
|
|
|
mLargeCacheWidth, mLargeCacheHeight >> 1,
|
|
|
|
mLargeCacheWidth, mLargeCacheHeight >> 1,
|
|
|
|
mLargeCacheWidth, mLargeCacheHeight);
|
2010-07-23 00:28:00 -07:00
|
|
|
}
|
2011-01-19 14:38:29 -08:00
|
|
|
|
|
|
|
sLogFontRendererCreate = false;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FontRenderer::~FontRenderer() {
|
2012-08-15 15:54:54 -07:00
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
|
|
|
delete mCacheTextures[i];
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2012-08-15 15:54:54 -07:00
|
|
|
mCacheTextures.clear();
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2010-08-07 23:46:15 -07:00
|
|
|
if (mInitialized) {
|
2012-03-26 14:52:00 -07:00
|
|
|
// Unbinding the buffer shouldn't be necessary but it crashes with some drivers
|
|
|
|
Caches::getInstance().unbindIndicesBuffer();
|
2012-03-21 11:52:52 -07:00
|
|
|
glDeleteBuffers(1, &mIndexBufferID);
|
|
|
|
|
2012-09-04 15:22:57 -07:00
|
|
|
delete[] mTextMesh;
|
2010-07-23 14:45:49 -07:00
|
|
|
}
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts);
|
|
|
|
while (it.next()) {
|
|
|
|
delete it.value();
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2013-01-08 11:15:30 -08:00
|
|
|
mActiveFonts.clear();
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::flushAllAndInvalidate() {
|
|
|
|
if (mCurrentQuadIndex != 0) {
|
|
|
|
issueDrawCommand();
|
|
|
|
}
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts);
|
|
|
|
while (it.next()) {
|
|
|
|
it.value()->invalidateTextureCache();
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
|
|
|
mCacheTextures[i]->init();
|
Optimize interactions with glyph cache
There are two fixes here:
- precaching: instead of caching-then-drawing whenever there is a new
glyph, we cache at DisplayList record time. Then when we finally draw that
DisplayList, we just upload the affected texture(s) once, instead of once
per change. This is a huge savings in upload time, especially when there are
larger glyphs being used by the app.
- packing: Previously, glyphs would line up horizontally on each cache line, leaving
potentially tons of space vertically, especially when smaller glyphs got put into cache
lines intended for large glyphs (which can happen when an app uses lots of unique
glyphs, a common case with, for example, chinese/japanese/korean languages). The new
approach packs glyphs vertically as well as horizontally to use the space more efficiently
and provide space for more glyphs in these situations.
Change-Id: I84338aa25db208c7bf13f3f92b4d05ed40c33527
2012-08-09 13:39:02 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
#if DEBUG_FONT_RENDERER
|
2012-08-15 15:54:54 -07:00
|
|
|
uint16_t totalGlyphs = 0;
|
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
2012-09-04 16:42:01 -07:00
|
|
|
totalGlyphs += mCacheTextures[i]->getGlyphCount();
|
2012-08-15 15:54:54 -07:00
|
|
|
// Erase caches, just as a debugging facility
|
2012-09-04 16:42:01 -07:00
|
|
|
if (mCacheTextures[i]->getTexture()) {
|
|
|
|
memset(mCacheTextures[i]->getTexture(), 0,
|
|
|
|
mCacheTextures[i]->getWidth() * mCacheTextures[i]->getHeight());
|
2012-08-15 15:54:54 -07:00
|
|
|
}
|
Optimize interactions with glyph cache
There are two fixes here:
- precaching: instead of caching-then-drawing whenever there is a new
glyph, we cache at DisplayList record time. Then when we finally draw that
DisplayList, we just upload the affected texture(s) once, instead of once
per change. This is a huge savings in upload time, especially when there are
larger glyphs being used by the app.
- packing: Previously, glyphs would line up horizontally on each cache line, leaving
potentially tons of space vertically, especially when smaller glyphs got put into cache
lines intended for large glyphs (which can happen when an app uses lots of unique
glyphs, a common case with, for example, chinese/japanese/korean languages). The new
approach packs glyphs vertically as well as horizontally to use the space more efficiently
and provide space for more glyphs in these situations.
Change-Id: I84338aa25db208c7bf13f3f92b4d05ed40c33527
2012-08-09 13:39:02 -07:00
|
|
|
}
|
|
|
|
ALOGD("Flushing caches: glyphs cached = %d", totalGlyphs);
|
|
|
|
#endif
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2011-12-16 15:44:59 -08:00
|
|
|
void FontRenderer::flushLargeCaches() {
|
2012-08-15 15:54:54 -07:00
|
|
|
// Start from 1; don't deallocate smallest/default texture
|
|
|
|
for (uint32_t i = 1; i < mCacheTextures.size(); i++) {
|
|
|
|
CacheTexture* cacheTexture = mCacheTextures[i];
|
2012-09-04 16:42:01 -07:00
|
|
|
if (cacheTexture->getTexture()) {
|
2012-08-15 15:54:54 -07:00
|
|
|
cacheTexture->init();
|
2013-01-08 11:15:30 -08:00
|
|
|
LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts);
|
|
|
|
while (it.next()) {
|
|
|
|
it.value()->invalidateTextureCache(cacheTexture);
|
2011-12-16 15:44:59 -08:00
|
|
|
}
|
2012-09-04 16:42:01 -07:00
|
|
|
cacheTexture->releaseTexture();
|
2011-12-16 15:44:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
CacheTexture* FontRenderer::cacheBitmapInTexture(const SkGlyph& glyph,
|
|
|
|
uint32_t* startX, uint32_t* startY) {
|
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
|
|
|
if (mCacheTextures[i]->fitBitmap(glyph, startX, startY)) {
|
|
|
|
return mCacheTextures[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Could not fit glyph into current cache textures
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-05 16:35:38 -08:00
|
|
|
void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
|
2012-08-30 09:06:46 -07:00
|
|
|
uint32_t* retOriginX, uint32_t* retOriginY, bool precaching) {
|
2012-08-15 13:15:16 -07:00
|
|
|
checkInit();
|
2013-02-28 12:15:35 -08:00
|
|
|
|
|
|
|
// If the glyph bitmap is empty let's assum the glyph is valid
|
|
|
|
// so we can avoid doing extra work later on
|
|
|
|
if (glyph.fWidth == 0 || glyph.fHeight == 0) {
|
|
|
|
cachedGlyph->mIsValid = true;
|
|
|
|
cachedGlyph->mCacheTexture = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-05 16:35:38 -08:00
|
|
|
cachedGlyph->mIsValid = false;
|
2013-02-28 12:15:35 -08:00
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
// If the glyph is too tall, don't cache it
|
2012-08-15 15:54:54 -07:00
|
|
|
if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 >
|
2012-09-04 16:42:01 -07:00
|
|
|
mCacheTextures[mCacheTextures.size() - 1]->getHeight()) {
|
2012-08-15 13:15:16 -07:00
|
|
|
ALOGE("Font size too large to fit in cache. width, height = %i, %i",
|
|
|
|
(int) glyph.fWidth, (int) glyph.fHeight);
|
2011-12-05 16:35:38 -08:00
|
|
|
return;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy the bitmap into the cache texture
|
|
|
|
uint32_t startX = 0;
|
|
|
|
uint32_t startY = 0;
|
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
CacheTexture* cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
if (!cacheTexture) {
|
2012-08-30 09:06:46 -07:00
|
|
|
if (!precaching) {
|
|
|
|
// If the new glyph didn't fit and we are not just trying to precache it,
|
|
|
|
// clear out the cache and try again
|
|
|
|
flushAllAndInvalidate();
|
|
|
|
cacheTexture = cacheBitmapInTexture(glyph, &startX, &startY);
|
|
|
|
}
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
if (!cacheTexture) {
|
2012-08-30 09:06:46 -07:00
|
|
|
// either the glyph didn't fit or we're precaching and will cache it when we draw
|
2011-12-05 16:35:38 -08:00
|
|
|
return;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 15:54:54 -07:00
|
|
|
cachedGlyph->mCacheTexture = cacheTexture;
|
2011-12-05 16:35:38 -08:00
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
*retOriginX = startX;
|
|
|
|
*retOriginY = startY;
|
|
|
|
|
|
|
|
uint32_t endX = startX + glyph.fWidth;
|
|
|
|
uint32_t endY = startY + glyph.fHeight;
|
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
uint32_t cacheWidth = cacheTexture->getWidth();
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
if (!cacheTexture->getTexture()) {
|
|
|
|
Caches::getInstance().activeTexture(0);
|
2011-12-05 16:35:38 -08:00
|
|
|
// Large-glyph texture memory is allocated only as needed
|
2012-09-04 16:42:01 -07:00
|
|
|
cacheTexture->allocateTexture();
|
2011-12-05 16:35:38 -08:00
|
|
|
}
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2013-02-05 14:38:40 -08:00
|
|
|
// Tells us whether the glyphs is B&W (1 bit per pixel)
|
|
|
|
// or anti-aliased (8 bits per pixel)
|
|
|
|
SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2013-02-05 14:38:40 -08:00
|
|
|
uint8_t* cacheBuffer = cacheTexture->getTexture();
|
2010-07-21 21:33:20 -07:00
|
|
|
uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0;
|
2012-08-07 19:09:57 -07:00
|
|
|
|
2013-02-05 14:38:40 -08:00
|
|
|
// Copy the glyph image, taking the mask format into account
|
|
|
|
uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage;
|
|
|
|
int stride = glyph.rowBytes();
|
|
|
|
|
2013-03-05 12:16:27 -08:00
|
|
|
uint32_t row = (startY - TEXTURE_BORDER_SIZE) * cacheWidth + startX - TEXTURE_BORDER_SIZE;
|
|
|
|
memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE);
|
|
|
|
|
2013-02-05 14:38:40 -08:00
|
|
|
switch (format) {
|
|
|
|
case SkMask::kA8_Format: {
|
|
|
|
if (mGammaTable) {
|
|
|
|
for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) {
|
2013-03-05 12:16:27 -08:00
|
|
|
row = cacheY * cacheWidth;
|
|
|
|
cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0;
|
2013-02-05 14:38:40 -08:00
|
|
|
for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) {
|
|
|
|
uint8_t tempCol = bitmapBuffer[bY + bX];
|
2013-03-05 12:16:27 -08:00
|
|
|
cacheBuffer[row + cacheX] = mGammaTable[tempCol];
|
2013-02-05 14:38:40 -08:00
|
|
|
}
|
2013-03-05 12:16:27 -08:00
|
|
|
cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0;
|
2013-02-05 14:38:40 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY += stride) {
|
2013-03-05 12:16:27 -08:00
|
|
|
row = cacheY * cacheWidth;
|
|
|
|
memcpy(&cacheBuffer[row + startX], &bitmapBuffer[bY], glyph.fWidth);
|
|
|
|
cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0;
|
|
|
|
cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0;
|
2013-02-05 14:38:40 -08:00
|
|
|
}
|
2012-07-13 18:25:35 -07:00
|
|
|
}
|
2013-02-05 14:38:40 -08:00
|
|
|
break;
|
2012-07-13 18:25:35 -07:00
|
|
|
}
|
2013-02-05 14:38:40 -08:00
|
|
|
case SkMask::kBW_Format: {
|
|
|
|
static const uint8_t COLORS[2] = { 0, 255 };
|
|
|
|
|
|
|
|
for (cacheY = startY; cacheY < endY; cacheY++) {
|
|
|
|
cacheX = startX;
|
|
|
|
int rowBytes = stride;
|
|
|
|
uint8_t* buffer = bitmapBuffer;
|
|
|
|
|
2013-03-05 12:16:27 -08:00
|
|
|
row = cacheY * cacheWidth;
|
|
|
|
cacheBuffer[row + startX - TEXTURE_BORDER_SIZE] = 0;
|
2013-02-05 14:38:40 -08:00
|
|
|
while (--rowBytes >= 0) {
|
|
|
|
uint8_t b = *buffer++;
|
|
|
|
for (int8_t mask = 7; mask >= 0 && cacheX < endX; mask--) {
|
|
|
|
cacheBuffer[cacheY * cacheWidth + cacheX++] = COLORS[(b >> mask) & 0x1];
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 12:16:27 -08:00
|
|
|
cacheBuffer[row + endX + TEXTURE_BORDER_SIZE - 1] = 0;
|
2013-02-05 14:38:40 -08:00
|
|
|
|
|
|
|
bitmapBuffer += stride;
|
2012-07-13 18:25:35 -07:00
|
|
|
}
|
2013-02-05 14:38:40 -08:00
|
|
|
break;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2013-02-05 14:38:40 -08:00
|
|
|
default:
|
|
|
|
ALOGW("Unkown glyph format: 0x%x", format);
|
|
|
|
break;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2012-02-28 18:17:02 -08:00
|
|
|
|
2013-03-05 12:16:27 -08:00
|
|
|
row = (endY + TEXTURE_BORDER_SIZE - 1) * cacheWidth + startX - TEXTURE_BORDER_SIZE;
|
|
|
|
memset(&cacheBuffer[row], 0, glyph.fWidth + 2 * TEXTURE_BORDER_SIZE);
|
|
|
|
|
2011-12-05 16:35:38 -08:00
|
|
|
cachedGlyph->mIsValid = true;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2011-12-05 16:35:38 -08:00
|
|
|
CacheTexture* FontRenderer::createCacheTexture(int width, int height, bool allocate) {
|
2012-07-20 11:14:32 -07:00
|
|
|
CacheTexture* cacheTexture = new CacheTexture(width, height);
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2011-12-14 15:22:56 -08:00
|
|
|
if (allocate) {
|
2012-09-04 16:42:01 -07:00
|
|
|
Caches::getInstance().activeTexture(0);
|
|
|
|
cacheTexture->allocateTexture();
|
2011-12-14 15:22:56 -08:00
|
|
|
}
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2011-12-14 15:22:56 -08:00
|
|
|
return cacheTexture;
|
2011-12-05 16:35:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::initTextTexture() {
|
2012-08-15 15:54:54 -07:00
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
|
|
|
delete mCacheTextures[i];
|
2012-05-14 15:19:58 -07:00
|
|
|
}
|
2012-08-15 15:54:54 -07:00
|
|
|
mCacheTextures.clear();
|
2012-05-14 15:19:58 -07:00
|
|
|
|
2011-12-05 16:35:38 -08:00
|
|
|
mUploadTexture = false;
|
2012-08-15 15:54:54 -07:00
|
|
|
mCacheTextures.push(createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, true));
|
2012-08-31 13:54:03 -07:00
|
|
|
mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false));
|
|
|
|
mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight >> 1, false));
|
|
|
|
mCacheTextures.push(createCacheTexture(mLargeCacheWidth, mLargeCacheHeight, false));
|
2012-08-15 15:54:54 -07:00
|
|
|
mCurrentCacheTexture = mCacheTextures[0];
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid having to reallocate memory and render quad by quad
|
|
|
|
void FontRenderer::initVertexArrayBuffers() {
|
2011-12-12 19:03:35 -08:00
|
|
|
uint32_t numIndices = mMaxNumberOfQuads * 6;
|
|
|
|
uint32_t indexBufferSizeBytes = numIndices * sizeof(uint16_t);
|
2010-07-23 00:28:00 -07:00
|
|
|
uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
// Four verts, two triangles , six indices per quad
|
|
|
|
for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) {
|
|
|
|
int i6 = i * 6;
|
|
|
|
int i4 = i * 4;
|
|
|
|
|
|
|
|
indexBufferData[i6 + 0] = i4 + 0;
|
|
|
|
indexBufferData[i6 + 1] = i4 + 1;
|
|
|
|
indexBufferData[i6 + 2] = i4 + 2;
|
|
|
|
|
|
|
|
indexBufferData[i6 + 3] = i4 + 0;
|
|
|
|
indexBufferData[i6 + 4] = i4 + 2;
|
|
|
|
indexBufferData[i6 + 5] = i4 + 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
glGenBuffers(1, &mIndexBufferID);
|
2011-12-13 13:11:32 -08:00
|
|
|
Caches::getInstance().bindIndicesBuffer(mIndexBufferID);
|
2010-10-13 16:36:22 -07:00
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_STATIC_DRAW);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
free(indexBufferData);
|
|
|
|
|
2011-12-12 19:03:35 -08:00
|
|
|
uint32_t coordSize = 2;
|
2010-07-21 21:33:20 -07:00
|
|
|
uint32_t uvSize = 2;
|
|
|
|
uint32_t vertsPerQuad = 4;
|
2010-07-23 14:45:49 -07:00
|
|
|
uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize;
|
2012-09-04 15:22:57 -07:00
|
|
|
mTextMesh = new float[vertexBufferSize];
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to allocate anything unless we actually draw text
|
|
|
|
void FontRenderer::checkInit() {
|
|
|
|
if (mInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initTextTexture();
|
|
|
|
initVertexArrayBuffers();
|
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
}
|
|
|
|
|
2012-11-16 00:03:17 +09:00
|
|
|
void FontRenderer::updateDrawParams() {
|
|
|
|
if (mCurrentQuadIndex != mLastQuadIndex) {
|
|
|
|
mDrawOffsets.add((uint16_t*)(mLastQuadIndex * sizeof(uint16_t) * 6));
|
|
|
|
mDrawCounts.add(mCurrentQuadIndex - mLastQuadIndex);
|
|
|
|
mDrawCacheTextures.add(mCurrentCacheTexture);
|
|
|
|
mLastQuadIndex = mCurrentQuadIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 14:45:49 -07:00
|
|
|
void FontRenderer::checkTextureUpdate() {
|
2012-11-16 00:03:17 +09:00
|
|
|
if (!mUploadTexture) {
|
2010-07-23 14:45:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-13 22:00:19 -08:00
|
|
|
Caches& caches = Caches::getInstance();
|
|
|
|
GLuint lastTextureId = 0;
|
2012-08-15 15:54:54 -07:00
|
|
|
// Iterate over all the cache textures and see which ones need to be updated
|
|
|
|
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
|
|
|
|
CacheTexture* cacheTexture = mCacheTextures[i];
|
2012-09-04 16:42:01 -07:00
|
|
|
if (cacheTexture->isDirty() && cacheTexture->getTexture()) {
|
2012-09-21 08:40:46 -07:00
|
|
|
// Can't copy inner rect; glTexSubimage expects pointer to deal with entire buffer
|
|
|
|
// of data. So expand the dirty rect to the encompassing horizontal stripe.
|
|
|
|
const Rect* dirtyRect = cacheTexture->getDirtyRect();
|
|
|
|
uint32_t x = 0;
|
|
|
|
uint32_t y = dirtyRect->top;
|
2012-09-04 16:42:01 -07:00
|
|
|
uint32_t width = cacheTexture->getWidth();
|
2012-09-21 08:40:46 -07:00
|
|
|
uint32_t height = dirtyRect->getHeight();
|
|
|
|
void* textureData = cacheTexture->getTexture() + y * width;
|
2010-07-23 14:45:49 -07:00
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
if (cacheTexture->getTextureId() != lastTextureId) {
|
|
|
|
lastTextureId = cacheTexture->getTextureId();
|
2011-12-13 22:00:19 -08:00
|
|
|
caches.activeTexture(0);
|
2012-09-04 16:42:01 -07:00
|
|
|
glBindTexture(GL_TEXTURE_2D, lastTextureId);
|
2011-12-13 22:00:19 -08:00
|
|
|
}
|
Optimize interactions with glyph cache
There are two fixes here:
- precaching: instead of caching-then-drawing whenever there is a new
glyph, we cache at DisplayList record time. Then when we finally draw that
DisplayList, we just upload the affected texture(s) once, instead of once
per change. This is a huge savings in upload time, especially when there are
larger glyphs being used by the app.
- packing: Previously, glyphs would line up horizontally on each cache line, leaving
potentially tons of space vertically, especially when smaller glyphs got put into cache
lines intended for large glyphs (which can happen when an app uses lots of unique
glyphs, a common case with, for example, chinese/japanese/korean languages). The new
approach packs glyphs vertically as well as horizontally to use the space more efficiently
and provide space for more glyphs in these situations.
Change-Id: I84338aa25db208c7bf13f3f92b4d05ed40c33527
2012-08-09 13:39:02 -07:00
|
|
|
#if DEBUG_FONT_RENDERER
|
2012-09-21 08:40:46 -07:00
|
|
|
ALOGD("glTexSubimage for cacheTexture %d: x, y, width height = %d, %d, %d, %d",
|
|
|
|
i, x, y, width, height);
|
Optimize interactions with glyph cache
There are two fixes here:
- precaching: instead of caching-then-drawing whenever there is a new
glyph, we cache at DisplayList record time. Then when we finally draw that
DisplayList, we just upload the affected texture(s) once, instead of once
per change. This is a huge savings in upload time, especially when there are
larger glyphs being used by the app.
- packing: Previously, glyphs would line up horizontally on each cache line, leaving
potentially tons of space vertically, especially when smaller glyphs got put into cache
lines intended for large glyphs (which can happen when an app uses lots of unique
glyphs, a common case with, for example, chinese/japanese/korean languages). The new
approach packs glyphs vertically as well as horizontally to use the space more efficiently
and provide space for more glyphs in these situations.
Change-Id: I84338aa25db208c7bf13f3f92b4d05ed40c33527
2012-08-09 13:39:02 -07:00
|
|
|
#endif
|
2012-09-21 08:40:46 -07:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
|
2010-08-13 19:39:53 -07:00
|
|
|
GL_ALPHA, GL_UNSIGNED_BYTE, textureData);
|
2012-09-04 16:42:01 -07:00
|
|
|
cacheTexture->setDirty(false);
|
2010-07-23 14:45:49 -07:00
|
|
|
}
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2010-07-23 14:45:49 -07:00
|
|
|
mUploadTexture = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::issueDrawCommand() {
|
2012-11-16 00:03:17 +09:00
|
|
|
updateDrawParams();
|
2010-07-23 14:45:49 -07:00
|
|
|
checkTextureUpdate();
|
|
|
|
|
2011-12-13 13:11:32 -08:00
|
|
|
Caches& caches = Caches::getInstance();
|
2011-12-13 22:00:19 -08:00
|
|
|
caches.bindIndicesBuffer(mIndexBufferID);
|
2011-12-13 13:11:32 -08:00
|
|
|
if (!mDrawn) {
|
2012-09-04 15:22:57 -07:00
|
|
|
float* buffer = mTextMesh;
|
2011-12-13 13:11:32 -08:00
|
|
|
int offset = 2;
|
|
|
|
|
|
|
|
bool force = caches.unbindMeshBuffer();
|
2012-09-25 12:00:29 -07:00
|
|
|
caches.bindPositionVertexPointer(force, buffer);
|
|
|
|
caches.bindTexCoordsVertexPointer(force, buffer + offset);
|
2011-12-13 13:11:32 -08:00
|
|
|
}
|
|
|
|
|
2012-11-16 00:03:17 +09:00
|
|
|
for (uint32_t i = 0; i < mDrawOffsets.size(); i++) {
|
|
|
|
uint16_t* offset = mDrawOffsets[i];
|
|
|
|
uint32_t count = mDrawCounts[i];
|
|
|
|
CacheTexture* texture = mDrawCacheTextures[i];
|
|
|
|
|
|
|
|
caches.activeTexture(0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
|
|
|
|
|
|
|
texture->setLinearFiltering(mLinearFiltering, false);
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, count * 6, GL_UNSIGNED_SHORT, offset);
|
|
|
|
}
|
2010-10-27 18:57:51 -07:00
|
|
|
|
|
|
|
mDrawn = true;
|
2012-11-16 00:03:17 +09:00
|
|
|
|
|
|
|
mCurrentQuadIndex = 0;
|
|
|
|
mLastQuadIndex = 0;
|
|
|
|
mDrawOffsets.clear();
|
|
|
|
mDrawCounts.clear();
|
|
|
|
mDrawCacheTextures.clear();
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 18:17:02 -08:00
|
|
|
void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
|
|
|
|
float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3,
|
2011-12-05 16:35:38 -08:00
|
|
|
float x4, float y4, float u4, float v4, CacheTexture* texture) {
|
|
|
|
if (texture != mCurrentCacheTexture) {
|
2012-11-16 00:03:17 +09:00
|
|
|
updateDrawParams();
|
2011-12-05 16:35:38 -08:00
|
|
|
// Now use the new texture id
|
|
|
|
mCurrentCacheTexture = texture;
|
|
|
|
}
|
2010-07-22 13:08:20 -07:00
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
const uint32_t vertsPerQuad = 4;
|
2011-12-12 19:03:35 -08:00
|
|
|
const uint32_t floatsPerVert = 4;
|
2012-09-04 15:22:57 -07:00
|
|
|
float* currentPos = mTextMesh + mCurrentQuadIndex * vertsPerQuad * floatsPerVert;
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
(*currentPos++) = x1;
|
|
|
|
(*currentPos++) = y1;
|
|
|
|
(*currentPos++) = u1;
|
|
|
|
(*currentPos++) = v1;
|
|
|
|
|
|
|
|
(*currentPos++) = x2;
|
|
|
|
(*currentPos++) = y2;
|
|
|
|
(*currentPos++) = u2;
|
|
|
|
(*currentPos++) = v2;
|
|
|
|
|
|
|
|
(*currentPos++) = x3;
|
|
|
|
(*currentPos++) = y3;
|
|
|
|
(*currentPos++) = u3;
|
|
|
|
(*currentPos++) = v3;
|
|
|
|
|
|
|
|
(*currentPos++) = x4;
|
|
|
|
(*currentPos++) = y4;
|
|
|
|
(*currentPos++) = u4;
|
|
|
|
(*currentPos++) = v4;
|
|
|
|
|
|
|
|
mCurrentQuadIndex++;
|
2012-02-28 18:17:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::appendMeshQuad(float x1, float y1, float u1, float v1,
|
|
|
|
float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3,
|
|
|
|
float x4, float y4, float u4, float v4, CacheTexture* texture) {
|
|
|
|
|
|
|
|
if (mClip &&
|
|
|
|
(x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
if (mBounds) {
|
|
|
|
mBounds->left = fmin(mBounds->left, x1);
|
|
|
|
mBounds->top = fmin(mBounds->top, y3);
|
|
|
|
mBounds->right = fmax(mBounds->right, x3);
|
|
|
|
mBounds->bottom = fmax(mBounds->bottom, y1);
|
|
|
|
}
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
if (mCurrentQuadIndex == mMaxNumberOfQuads) {
|
|
|
|
issueDrawCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:17:02 -08:00
|
|
|
void FontRenderer::appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
|
|
|
|
float x2, float y2, float u2, float v2, float x3, float y3, float u3, float v3,
|
|
|
|
float x4, float y4, float u4, float v4, CacheTexture* texture) {
|
|
|
|
|
|
|
|
appendMeshQuadNoClip(x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, texture);
|
|
|
|
|
|
|
|
if (mBounds) {
|
|
|
|
mBounds->left = fmin(mBounds->left, fmin(x1, fmin(x2, fmin(x3, x4))));
|
|
|
|
mBounds->top = fmin(mBounds->top, fmin(y1, fmin(y2, fmin(y3, y4))));
|
|
|
|
mBounds->right = fmax(mBounds->right, fmax(x1, fmax(x2, fmax(x3, x4))));
|
|
|
|
mBounds->bottom = fmax(mBounds->bottom, fmax(y1, fmax(y2, fmax(y3, y4))));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentQuadIndex == mMaxNumberOfQuads) {
|
|
|
|
issueDrawCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
void FontRenderer::setFont(SkPaint* paint, const mat4& matrix) {
|
|
|
|
mCurrentFont = Font::create(this, paint, matrix);
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
2010-10-01 16:36:14 -07:00
|
|
|
|
2010-08-06 14:49:04 -07:00
|
|
|
FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text,
|
2012-07-19 22:48:17 -07:00
|
|
|
uint32_t startIndex, uint32_t len, int numGlyphs, uint32_t radius, const float* positions) {
|
2010-08-13 19:39:53 -07:00
|
|
|
checkInit();
|
|
|
|
|
|
|
|
if (!mCurrentFont) {
|
|
|
|
DropShadow image;
|
|
|
|
image.width = 0;
|
|
|
|
image.height = 0;
|
|
|
|
image.image = NULL;
|
|
|
|
image.penX = 0;
|
|
|
|
image.penY = 0;
|
|
|
|
return image;
|
|
|
|
}
|
2010-08-06 14:49:04 -07:00
|
|
|
|
2011-12-13 22:00:19 -08:00
|
|
|
mDrawn = false;
|
2011-11-28 09:35:09 -08:00
|
|
|
mClip = NULL;
|
|
|
|
mBounds = NULL;
|
|
|
|
|
2010-08-06 14:49:04 -07:00
|
|
|
Rect bounds;
|
2012-07-19 22:48:17 -07:00
|
|
|
mCurrentFont->measure(paint, text, startIndex, len, numGlyphs, &bounds, positions);
|
2011-11-28 09:35:09 -08:00
|
|
|
|
2010-08-13 19:39:53 -07:00
|
|
|
uint32_t paddedWidth = (uint32_t) (bounds.right - bounds.left) + 2 * radius;
|
|
|
|
uint32_t paddedHeight = (uint32_t) (bounds.top - bounds.bottom) + 2 * radius;
|
2011-11-28 09:35:09 -08:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
// Align buffers for renderscript usage
|
|
|
|
if (paddedWidth & (RS_CPU_ALLOCATION_ALIGNMENT - 1)) {
|
|
|
|
paddedWidth += RS_CPU_ALLOCATION_ALIGNMENT - paddedWidth % RS_CPU_ALLOCATION_ALIGNMENT;
|
2010-08-06 14:49:04 -07:00
|
|
|
}
|
2010-08-13 19:39:53 -07:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
int size = paddedWidth * paddedHeight;
|
|
|
|
uint8_t* dataBuffer = (uint8_t*)memalign(RS_CPU_ALLOCATION_ALIGNMENT, size);
|
|
|
|
memset(dataBuffer, 0, size);
|
|
|
|
|
2010-08-06 14:49:04 -07:00
|
|
|
int penX = radius - bounds.left;
|
|
|
|
int penY = radius - bounds.bottom;
|
|
|
|
|
2013-02-22 10:41:36 -08:00
|
|
|
if ((bounds.right > bounds.left) && (bounds.top > bounds.bottom)) {
|
|
|
|
// text has non-whitespace, so draw and blur to create the shadow
|
|
|
|
// NOTE: bounds.isEmpty() can't be used here, since vertical coordinates are inverted
|
|
|
|
// TODO: don't draw pure whitespace in the first place, and avoid needing this check
|
|
|
|
mCurrentFont->render(paint, text, startIndex, len, numGlyphs, penX, penY,
|
|
|
|
Font::BITMAP, dataBuffer, paddedWidth, paddedHeight, NULL, positions);
|
|
|
|
|
|
|
|
blurImage(&dataBuffer, paddedWidth, paddedHeight, radius);
|
|
|
|
}
|
2010-08-06 14:49:04 -07:00
|
|
|
|
|
|
|
DropShadow image;
|
|
|
|
image.width = paddedWidth;
|
|
|
|
image.height = paddedHeight;
|
|
|
|
image.image = dataBuffer;
|
|
|
|
image.penX = penX;
|
|
|
|
image.penY = penY;
|
2011-12-13 22:00:19 -08:00
|
|
|
|
2010-08-06 14:49:04 -07:00
|
|
|
return image;
|
|
|
|
}
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2012-01-18 12:39:17 -08:00
|
|
|
void FontRenderer::initRender(const Rect* clip, Rect* bounds) {
|
2010-07-21 21:33:20 -07:00
|
|
|
checkInit();
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
mDrawn = false;
|
|
|
|
mBounds = bounds;
|
2010-07-22 13:08:20 -07:00
|
|
|
mClip = clip;
|
2012-01-18 12:39:17 -08:00
|
|
|
}
|
2011-11-28 09:35:09 -08:00
|
|
|
|
2012-01-18 12:39:17 -08:00
|
|
|
void FontRenderer::finishRender() {
|
2010-10-27 18:57:51 -07:00
|
|
|
mBounds = NULL;
|
2011-11-28 09:35:09 -08:00
|
|
|
mClip = NULL;
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
if (mCurrentQuadIndex != 0) {
|
|
|
|
issueDrawCommand();
|
|
|
|
}
|
2012-01-18 12:39:17 -08:00
|
|
|
}
|
|
|
|
|
2013-01-08 11:15:30 -08:00
|
|
|
void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix) {
|
|
|
|
Font* font = Font::create(this, paint, matrix);
|
Optimize interactions with glyph cache
There are two fixes here:
- precaching: instead of caching-then-drawing whenever there is a new
glyph, we cache at DisplayList record time. Then when we finally draw that
DisplayList, we just upload the affected texture(s) once, instead of once
per change. This is a huge savings in upload time, especially when there are
larger glyphs being used by the app.
- packing: Previously, glyphs would line up horizontally on each cache line, leaving
potentially tons of space vertically, especially when smaller glyphs got put into cache
lines intended for large glyphs (which can happen when an app uses lots of unique
glyphs, a common case with, for example, chinese/japanese/korean languages). The new
approach packs glyphs vertically as well as horizontally to use the space more efficiently
and provide space for more glyphs in these situations.
Change-Id: I84338aa25db208c7bf13f3f92b4d05ed40c33527
2012-08-09 13:39:02 -07:00
|
|
|
font->precache(paint, text, numGlyphs);
|
|
|
|
}
|
|
|
|
|
2012-01-18 12:39:17 -08:00
|
|
|
bool FontRenderer::renderPosText(SkPaint* paint, const Rect* clip, const char *text,
|
|
|
|
uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y,
|
|
|
|
const float* positions, Rect* bounds) {
|
|
|
|
if (!mCurrentFont) {
|
|
|
|
ALOGE("No font set");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
initRender(clip, bounds);
|
|
|
|
mCurrentFont->render(paint, text, startIndex, len, numGlyphs, x, y, positions);
|
|
|
|
finishRender();
|
2010-10-27 18:57:51 -07:00
|
|
|
|
2012-02-28 18:17:02 -08:00
|
|
|
return mDrawn;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text,
|
|
|
|
uint32_t startIndex, uint32_t len, int numGlyphs, SkPath* path,
|
|
|
|
float hOffset, float vOffset, Rect* bounds) {
|
|
|
|
if (!mCurrentFont) {
|
|
|
|
ALOGE("No font set");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
initRender(clip, bounds);
|
|
|
|
mCurrentFont->render(paint, text, startIndex, len, numGlyphs, path, hOffset, vOffset);
|
|
|
|
finishRender();
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
return mDrawn;
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:22:57 -07:00
|
|
|
void FontRenderer::removeFont(const Font* font) {
|
2013-01-08 11:15:30 -08:00
|
|
|
mActiveFonts.remove(font->getDescription());
|
2012-09-04 15:22:57 -07:00
|
|
|
|
|
|
|
if (mCurrentFont == font) {
|
|
|
|
mCurrentFont = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 17:52:30 -07:00
|
|
|
void FontRenderer::computeGaussianWeights(float* weights, int32_t radius) {
|
|
|
|
// Compute gaussian weights for the blur
|
|
|
|
// e is the euler's number
|
|
|
|
float e = 2.718281828459045f;
|
|
|
|
float pi = 3.1415926535897932f;
|
|
|
|
// g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
|
|
|
|
// x is of the form [-radius .. 0 .. radius]
|
|
|
|
// and sigma varies with radius.
|
|
|
|
// Based on some experimental radius values and sigma's
|
|
|
|
// we approximately fit sigma = f(radius) as
|
2010-08-06 14:49:04 -07:00
|
|
|
// sigma = radius * 0.3 + 0.6
|
2010-08-02 17:52:30 -07:00
|
|
|
// The larger the radius gets, the more our gaussian blur
|
|
|
|
// will resemble a box blur since with large sigma
|
|
|
|
// the gaussian curve begins to lose its shape
|
2011-01-05 15:26:55 -08:00
|
|
|
float sigma = 0.3f * (float) radius + 0.6f;
|
2010-08-02 17:52:30 -07:00
|
|
|
|
|
|
|
// Now compute the coefficints
|
|
|
|
// We will store some redundant values to save some math during
|
|
|
|
// the blur calculations
|
|
|
|
// precompute some values
|
|
|
|
float coeff1 = 1.0f / (sqrt( 2.0f * pi ) * sigma);
|
|
|
|
float coeff2 = - 1.0f / (2.0f * sigma * sigma);
|
|
|
|
|
|
|
|
float normalizeFactor = 0.0f;
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t r = -radius; r <= radius; r ++) {
|
2010-10-01 16:36:14 -07:00
|
|
|
float floatR = (float) r;
|
2010-08-02 17:52:30 -07:00
|
|
|
weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2);
|
|
|
|
normalizeFactor += weights[r + radius];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Now we need to normalize the weights because all our coefficients need to add up to one
|
|
|
|
normalizeFactor = 1.0f / normalizeFactor;
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t r = -radius; r <= radius; r ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
weights[r + radius] *= normalizeFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::horizontalBlur(float* weights, int32_t radius,
|
2010-08-13 19:39:53 -07:00
|
|
|
const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
|
2010-08-02 17:52:30 -07:00
|
|
|
float blurredPixel = 0.0f;
|
|
|
|
float currentPixel = 0.0f;
|
|
|
|
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t y = 0; y < height; y ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
|
|
|
|
const uint8_t* input = source + y * width;
|
|
|
|
uint8_t* output = dest + y * width;
|
|
|
|
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t x = 0; x < width; x ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
blurredPixel = 0.0f;
|
|
|
|
const float* gPtr = weights;
|
|
|
|
// Optimization for non-border pixels
|
2011-01-05 15:26:55 -08:00
|
|
|
if (x > radius && x < (width - radius)) {
|
2010-08-02 17:52:30 -07:00
|
|
|
const uint8_t *i = input + (x - radius);
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int r = -radius; r <= radius; r ++) {
|
2010-10-01 16:36:14 -07:00
|
|
|
currentPixel = (float) (*i);
|
2010-08-02 17:52:30 -07:00
|
|
|
blurredPixel += currentPixel * gPtr[0];
|
|
|
|
gPtr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t r = -radius; r <= radius; r ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
// Stepping left and right away from the pixel
|
|
|
|
int validW = x + r;
|
2011-01-05 15:26:55 -08:00
|
|
|
if (validW < 0) {
|
2010-08-02 17:52:30 -07:00
|
|
|
validW = 0;
|
|
|
|
}
|
2011-01-05 15:26:55 -08:00
|
|
|
if (validW > width - 1) {
|
2010-08-02 17:52:30 -07:00
|
|
|
validW = width - 1;
|
|
|
|
}
|
|
|
|
|
2011-01-05 15:26:55 -08:00
|
|
|
currentPixel = (float) input[validW];
|
2010-08-02 17:52:30 -07:00
|
|
|
blurredPixel += currentPixel * gPtr[0];
|
|
|
|
gPtr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*output = (uint8_t)blurredPixel;
|
|
|
|
output ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontRenderer::verticalBlur(float* weights, int32_t radius,
|
2010-08-13 19:39:53 -07:00
|
|
|
const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
|
2010-08-02 17:52:30 -07:00
|
|
|
float blurredPixel = 0.0f;
|
|
|
|
float currentPixel = 0.0f;
|
|
|
|
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t y = 0; y < height; y ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
uint8_t* output = dest + y * width;
|
|
|
|
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t x = 0; x < width; x ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
blurredPixel = 0.0f;
|
|
|
|
const float* gPtr = weights;
|
|
|
|
const uint8_t* input = source + x;
|
|
|
|
// Optimization for non-border pixels
|
2011-01-05 15:26:55 -08:00
|
|
|
if (y > radius && y < (height - radius)) {
|
2010-08-02 17:52:30 -07:00
|
|
|
const uint8_t *i = input + ((y - radius) * width);
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t r = -radius; r <= radius; r ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
currentPixel = (float)(*i);
|
|
|
|
blurredPixel += currentPixel * gPtr[0];
|
|
|
|
gPtr++;
|
|
|
|
i += width;
|
|
|
|
}
|
|
|
|
} else {
|
2011-01-05 15:26:55 -08:00
|
|
|
for (int32_t r = -radius; r <= radius; r ++) {
|
2010-08-02 17:52:30 -07:00
|
|
|
int validH = y + r;
|
|
|
|
// Clamp to zero and width
|
2011-01-05 15:26:55 -08:00
|
|
|
if (validH < 0) {
|
2010-08-02 17:52:30 -07:00
|
|
|
validH = 0;
|
|
|
|
}
|
2011-01-05 15:26:55 -08:00
|
|
|
if (validH > height - 1) {
|
2010-08-02 17:52:30 -07:00
|
|
|
validH = height - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *i = input + validH * width;
|
2011-01-05 15:26:55 -08:00
|
|
|
currentPixel = (float) (*i);
|
2010-08-02 17:52:30 -07:00
|
|
|
blurredPixel += currentPixel * gPtr[0];
|
|
|
|
gPtr++;
|
|
|
|
}
|
|
|
|
}
|
2011-01-05 15:26:55 -08:00
|
|
|
*output = (uint8_t) blurredPixel;
|
2012-09-04 15:22:57 -07:00
|
|
|
output++;
|
2010-08-02 17:52:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
void FontRenderer::blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius) {
|
|
|
|
if (width * height * radius < RS_MIN_INPUT_CUTOFF) {
|
|
|
|
float *gaussian = new float[2 * radius + 1];
|
|
|
|
computeGaussianWeights(gaussian, radius);
|
2010-08-02 17:52:30 -07:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
uint8_t* scratch = new uint8_t[width * height];
|
|
|
|
|
|
|
|
horizontalBlur(gaussian, radius, *image, scratch, width, height);
|
|
|
|
verticalBlur(gaussian, radius, scratch, *image, width, height);
|
|
|
|
|
|
|
|
delete[] gaussian;
|
|
|
|
delete[] scratch;
|
2013-02-22 10:41:36 -08:00
|
|
|
return;
|
2013-02-13 16:14:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* outImage = (uint8_t*)memalign(RS_CPU_ALLOCATION_ALIGNMENT, width * height);
|
|
|
|
|
|
|
|
if (mRs.get() == 0) {
|
|
|
|
mRs = new RSC::RS();
|
|
|
|
if (!mRs->init(true, true)) {
|
|
|
|
ALOGE("blur RS failed to init");
|
|
|
|
}
|
|
|
|
|
|
|
|
mRsElement = RSC::Element::A_8(mRs);
|
|
|
|
mRsScript = new RSC::ScriptIntrinsicBlur(mRs, mRsElement);
|
|
|
|
}
|
2011-12-12 19:03:35 -08:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
sp<const RSC::Type> t = RSC::Type::create(mRs, mRsElement, width, height, 0);
|
|
|
|
sp<RSC::Allocation> ain = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE,
|
|
|
|
RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, *image);
|
|
|
|
sp<RSC::Allocation> aout = RSC::Allocation::createTyped(mRs, t, RS_ALLOCATION_MIPMAP_NONE,
|
|
|
|
RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, outImage);
|
2011-12-12 19:03:35 -08:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
mRsScript->setRadius(radius);
|
|
|
|
mRsScript->blur(ain, aout);
|
2011-12-12 19:03:35 -08:00
|
|
|
|
2013-02-13 16:14:17 -08:00
|
|
|
// replace the original image's pointer, avoiding a copy back to the original buffer
|
2013-02-20 13:20:03 -08:00
|
|
|
free(*image);
|
2013-02-13 16:14:17 -08:00
|
|
|
*image = outImage;
|
2010-08-02 17:52:30 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|