未验证 提交 8d8c20d4 编写于 作者: O openharmony_ci 提交者: Gitee

!866 矢量字体内存对齐

Merge pull request !866 from Lizhiqi/font_opt
......@@ -43,7 +43,7 @@ config("graphic_define_config") {
if (defined(board_toolchain_type) && board_toolchain_type == "iccarm") {
cflags = [
"--diag_suppress",
"Pa084,Pa093,Pa137,Pe161,Pe177,Pe186,Pe223,Pe226,Pe366,Pe367,Pe550,Pe940",
"Pa084,Pa093,Pa137,Pe161,Pe177,Pe186,Pe223,Pe226,Pe366,Pe367,Pe550,Pe940,Pe068",
]
cflags_cc = cflags
}
......
......@@ -89,13 +89,9 @@ uint16_t DrawLabel::DrawTextOneLine(BufferInfo& gfxDstBuffer, const LabelLineInf
uint8_t* fontMap = fontEngine->GetBitmap(letterInfo.letter, glyphNode, letterInfo.fontId, letterInfo.fontSize,
letterInfo.shapingId);
if (fontMap != nullptr) {
#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
if (TypedText::IsColourWord(letterInfo.letter, fontId, fontSize)) {
#else
uint8_t weight = UIFont::GetInstance()->GetFontWeight(glyphNode.fontId);
// 16: rgb565->16 rgba8888->32 font with rgba
if (weight >= 16) {
#endif
DrawUtils::GetInstance()->DrawColorLetter(gfxDstBuffer, letterInfo, fontMap, glyphNode);
} else {
letterInfo.offsetY = labelLine.ellipsisOssetY == 0 ? offsetPosY : labelLine.ellipsisOssetY;
......
......@@ -16,9 +16,10 @@
#ifndef FONT_RAM_ALLOCATOR_H
#define FONT_RAM_ALLOCATOR_H
#include <cstdint>
#include "font/ui_font_allocator.h"
#include "gfx_utils/heap_base.h"
#include <cstdint>
namespace OHOS {
class FontRamAllocator : public HeapBase {
......
......@@ -292,31 +292,6 @@ int16_t GlyphsFile::GetFontHeight(uint16_t fontId)
return glyphInfo.fontHeader->fontHeight;
}
namespace {
void RearrangeBitmap(BufferInfo& bufInfo, uint32_t fileSz)
{
uint32_t word = bufInfo.width;
word = BIT_TO_BYTE(word * DrawUtils::GetPxSizeByColorMode(bufInfo.mode));
if (bufInfo.stride <= word) {
return;
}
uint8_t* bitmap = reinterpret_cast<uint8_t*>(bufInfo.virAddr);
uint32_t suffixLen = bufInfo.stride - word;
uint8_t* rdestBuf = bitmap + bufInfo.stride * bufInfo.height;
uint8_t* rsrcBuf = bitmap + fileSz;
/* Rearrange bitmap in local buffer */
for (uint32_t row = 0; row < bufInfo.height; row++) {
rdestBuf -= suffixLen;
(void)memset_s(rdestBuf, suffixLen, 0, suffixLen);
for (uint32_t i = 0; i < word; i++) {
*(--rdestBuf) = *(--rsrcBuf);
}
}
BaseGfxEngine::GetInstance()->MemoryBarrier();
}
} // namespace
int8_t GlyphsFile::GetBitmap(GlyphNode& node, BufferInfo& bufInfo)
{
......@@ -345,7 +320,7 @@ int8_t GlyphsFile::GetBitmap(GlyphNode& node, BufferInfo& bufInfo)
GRAPHIC_LOGE("GlyphsFile::GetBitmap read failed");
return INVALID_RET_VALUE;
}
RearrangeBitmap(bufInfo, size);
UIFontAllocator::RearrangeBitmap(bufInfo, size, false);
node.dataFlag = node.fontId;
return RET_VALUE_OK;
......
......@@ -15,6 +15,10 @@
#include "font/ui_font_allocator.h"
#include "draw/draw_utils.h"
#include "engines/gfx/gfx_engine_manager.h"
#include "font/ui_font.h"
#include "font/ui_font_cache_manager.h"
#include "gfx_utils/graphic_buffer.h"
namespace OHOS {
......@@ -27,14 +31,19 @@ UIFontAllocator::~UIFontAllocator() {}
void UIFontAllocator::SetRamAddr(uint8_t* ram, uint32_t size)
{
UI_ADDR_ALIGN(ram, size);
struct Chunk* chunk = nullptr;
ram_ = ram;
if (size <= sizeof(struct Chunk) * 2) { // 2 : head and tail two chunk
ramSize_ = 0;
return;
}
if (ram == nullptr) {
ramSize_ = 0;
return;
}
UI_ADDR_ALIGN(ram, size);
ram_ = ram;
struct Chunk* chunk = nullptr;
ramSize_ = size - sizeof(struct Chunk) * 2; // head and tail two chunk
chunk = reinterpret_cast<struct Chunk*>(ram_);
chunk->next = size - sizeof(struct Chunk);
......@@ -166,4 +175,48 @@ void UIFontAllocator::Free(void* addr)
freeSize_ += chunk->next - (reinterpret_cast<uint8_t*>(chunk) - ram_);
CombineFree(chunk);
}
BufferInfo UIFontAllocator::GetCacheBuffer(uint16_t fontId,
uint32_t unicode,
ColorMode mode,
GlyphNode& glyphNode,
bool hasMetric)
{
BufferInfo bufInfo{Rect(), 0, nullptr, nullptr, glyphNode.cols, glyphNode.rows, mode, 0};
bufInfo.stride = BIT_TO_BYTE(bufInfo.width * DrawUtils::GetPxSizeByColorMode(bufInfo.mode));
BaseGfxEngine::GetInstance()->AdjustLineStride(bufInfo);
uint32_t bitmapSize = bufInfo.stride * bufInfo.height;
if (hasMetric) {
bitmapSize += sizeof(Metric);
}
bufInfo.virAddr = reinterpret_cast<void*>(UIFontCacheManager::GetInstance()->GetSpace(fontId, unicode, bitmapSize));
return bufInfo;
}
void UIFontAllocator::RearrangeBitmap(BufferInfo& bufInfo, uint32_t fileSz, bool hasMetric)
{
uint32_t word = bufInfo.width;
word = BIT_TO_BYTE(word * DrawUtils::GetPxSizeByColorMode(bufInfo.mode));
if (bufInfo.stride <= word) {
return;
}
uint8_t* bitmap = reinterpret_cast<uint8_t*>(bufInfo.virAddr);
if (hasMetric) {
bitmap += sizeof(Metric);
}
uint32_t suffixLen = bufInfo.stride - word;
uint8_t* rdestBuf = bitmap + bufInfo.stride * bufInfo.height;
uint8_t* rsrcBuf = bitmap + fileSz;
/* Rearrange bitmap in local buffer */
for (uint32_t row = 0; row < bufInfo.height; row++) {
rdestBuf -= suffixLen;
(void)memset_s(rdestBuf, suffixLen, 0, suffixLen);
for (uint32_t i = 0; i < word; i++) {
*(--rdestBuf) = *(--rsrcBuf);
}
}
BaseGfxEngine::GetInstance()->MemoryBarrier();
}
} // namespace OHOS
......@@ -18,6 +18,7 @@
#include <stdint.h>
#include "font/ui_font_header.h"
#include "gfx_utils/graphic_buffer.h"
namespace OHOS {
......@@ -50,6 +51,11 @@ public:
void Free(void* addr);
static BufferInfo
GetCacheBuffer(uint16_t fontId, uint32_t unicode, ColorMode mode, GlyphNode& glyphNode, bool hasMetric);
static void RearrangeBitmap(BufferInfo& bufInfo, uint32_t fileSz, bool hasMetric);
private:
void CombineFree(Chunk* cache);
......
......@@ -228,18 +228,6 @@ uint8_t* UIFontBitmap::GetCacheBitmap(uint16_t fontId, uint32_t unicode)
return UIFontCacheManager::GetInstance()->GetBitmap(fontId, unicode);
}
BufferInfo UIFontBitmap::GetCacheBuffer(uint16_t fontId, uint32_t unicode, GlyphNode& glyphNode)
{
ColorMode mode = UIFont::GetInstance()->GetColorType(fontId);
BufferInfo bufInfo{Rect(), 0, nullptr, nullptr, glyphNode.cols, glyphNode.rows, mode, 0};
bufInfo.stride = BIT_TO_BYTE(bufInfo.width * DrawUtils::GetPxSizeByColorMode(bufInfo.mode));
BaseGfxEngine::GetInstance()->AdjustLineStride(bufInfo);
uint32_t bitmapSize = bufInfo.stride * bufInfo.height;
bufInfo.virAddr = reinterpret_cast<void*>(UIFontCacheManager::GetInstance()->GetSpace(fontId, unicode, bitmapSize));
return bufInfo;
}
void UIFontBitmap::PutCacheSpace(uint8_t* addr)
{
UIFontCacheManager::GetInstance()->PutSpace(addr);
......@@ -275,8 +263,8 @@ uint8_t* UIFontBitmap::SearchInFont(uint32_t unicode, GlyphNode& glyphNode, uint
if (glyphNode.kernOff <= glyphNode.dataOff) {
return nullptr;
}
BufferInfo bufInfo = GetCacheBuffer(fontId, unicode, glyphNode);
ColorMode mode = UIFont::GetInstance()->GetColorType(fontId);
BufferInfo bufInfo = UIFontAllocator::GetCacheBuffer(fontId, unicode, mode, glyphNode, false);
ret = dynamicFont_.GetBitmap(unicode, bufInfo, fontId);
if (ret == RET_VALUE_OK) {
return reinterpret_cast<uint8_t*>(bufInfo.virAddr);
......
......@@ -54,6 +54,8 @@ public:
ListHead lruHead;
uint32_t fontId; // bitmap font: fontId vector font: fontKey ttfId + fontsize
uint32_t unicode;
uint32_t reserve1;
uint32_t reserve2;
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
TextStyle textStyle;
#endif
......
......@@ -16,6 +16,7 @@
#include "font/ui_font_cache_manager.h"
#include "font/font_ram_allocator.h"
#include "gfx_utils/graphic_log.h"
#include "securec.h"
namespace OHOS {
UIFontCacheManager::UIFontCacheManager() : bitmapCache_(nullptr) {}
......@@ -72,7 +73,10 @@ void UIFontCacheManager::BitmapCacheInit()
return;
}
memset(bitmapCacheAddr, 0, bitmapCacheSize_);
if (memset_s(bitmapCacheAddr, bitmapCacheSize_, 0, bitmapCacheSize_)!= EOK) {
GRAPHIC_LOGE("UIFontCacheManager::BitmapCacheInit memset failed");
return;
}
bitmapCache_ = new UIFontCache(bitmapCacheAddr, bitmapCacheSize_);
}
......
......@@ -867,35 +867,59 @@ void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode)
#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING
SetFace(faceInfo, unicode, TEXT_STYLE_NORMAL);
#else
Metric f;
f.advance = static_cast<uint16_t>(faceInfo.face->glyph->advance.x / FONT_PIXEL_IN_POINT);
f.left = faceInfo.face->glyph->bitmap_left;
f.top = faceInfo.face->glyph->bitmap_top;
f.cols = faceInfo.face->glyph->bitmap.width;
f.rows = faceInfo.face->glyph->bitmap.rows;
Metric* f = reinterpret_cast<Metric*>(UIMalloc(sizeof(Metric)));
if (f == nullptr) {
return;
}
f->advance = static_cast<uint16_t>(faceInfo.face->glyph->advance.x / FONT_PIXEL_IN_POINT);
f->left = faceInfo.face->glyph->bitmap_left;
f->top = faceInfo.face->glyph->bitmap_top;
f->cols = faceInfo.face->glyph->bitmap.width;
f->rows = faceInfo.face->glyph->bitmap.rows;
// cache glyph
SaveGlyphNode(unicode, faceInfo.key, &f);
SaveGlyphNode(unicode, faceInfo.key, f);
int16_t pixSize = 1;
int16_t pixSize;
ColorMode mode;
if (faceInfo.face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
pixSize = 0x04; // 4 Byte
mode = ARGB8888;
}
uint32_t bitmapSize = faceInfo.face->glyph->bitmap.width * faceInfo.face->glyph->bitmap.rows * pixSize;
// cache bitmap
uint8_t* bitmap = UIFontCacheManager::GetInstance()->GetSpace(faceInfo.key, unicode, bitmapSize + sizeof(Metric));
if (bitmap != nullptr) {
if (memcpy_s(bitmap, sizeof(Metric), &f, sizeof(Metric)) != EOK) {
UIFontCacheManager::GetInstance()->PutSpace(bitmap);
else {
pixSize = 1;
mode = A8;
}
GlyphNode glyphNode;
glyphNode.left = f->left;
glyphNode.top = f->top;
glyphNode.cols = f->cols;
glyphNode.rows = f->rows;
glyphNode.advance = f->advance;
glyphNode.unicode = unicode;
glyphNode.fontId = faceInfo.key;
BufferInfo bufInfo = UIFontAllocator::GetCacheBuffer(faceInfo.key, unicode, mode, glyphNode, true);
uint32_t bitmapSize = bufInfo.stride * bufInfo.height;
uint32_t rawSize = glyphNode.cols * glyphNode.rows * pixSize;
if (bufInfo.virAddr != nullptr) {
if (memcpy_s(bufInfo.virAddr, sizeof(Metric), f, sizeof(Metric)) != EOK) {
UIFontCacheManager::GetInstance()->PutSpace(reinterpret_cast<uint8_t*>(bufInfo.virAddr));
UIFree(f);
return;
}
if ((faceInfo.face->glyph->bitmap.buffer != nullptr) &&
(memcpy_s(bitmap + sizeof(Metric), bitmapSize, faceInfo.face->glyph->bitmap.buffer, bitmapSize) != EOK)) {
UIFontCacheManager::GetInstance()->PutSpace(bitmap);
(memcpy_s(reinterpret_cast<uint8_t*>(bufInfo.virAddr) + sizeof(Metric), bitmapSize,
faceInfo.face->glyph->bitmap.buffer, rawSize) != EOK)) {
UIFontCacheManager::GetInstance()->PutSpace(reinterpret_cast<uint8_t*>(bufInfo.virAddr));
UIFree(f);
return;
}
UIFontAllocator::RearrangeBitmap(bufInfo, rawSize, true);
ClearFontGlyph(faceInfo.face);
}
UIFree(f);
#endif
}
......
......@@ -53,7 +53,6 @@ public:
protected:
int8_t GetDynamicFontBitmap(uint32_t unicode, BufferInfo& bufInfo, uint16_t fontId);
uint8_t* GetCacheBitmap(uint16_t fontId, uint32_t unicode);
BufferInfo GetCacheBuffer(uint16_t fontId, uint32_t unicode, GlyphNode& glyphNode);
void PutCacheSpace(uint8_t* addr);
int16_t GetDynamicFontWidth(uint32_t unicode, uint16_t fontId);
uint32_t offset_;
......
......@@ -81,14 +81,6 @@ private:
const char* ttcName;
FT_Stream stream;
};
struct Metric {
int left;
int top;
int cols;
int rows;
int advance;
uint8_t buf[0];
};
TtcInfo ttcInfos_[FONT_TTC_MAX] = {};
void SetFace(FaceInfo& faceInfo, uint32_t unicode);
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
......
......@@ -18,6 +18,7 @@
#include <string>
#include "graphic_config.h"
#include "gfx_utils/common_macros.h"
namespace OHOS {
#pragma pack(1)
#define BIT_TO_BYTE(x) (((x) + 0x7) >> 3)
......@@ -413,6 +414,15 @@ struct TtfInfo {
uint8_t shaping;
};
struct UI_STRUCT_ALIGN Metric {
int left;
int top;
int cols;
int rows;
int advance;
uint8_t buf[0];
};
#pragma pack()
} // namespace OHOS
#endif /* UI_FONT_HEADER_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册