Split out the Qt font drawer in its own file.

This commit is contained in:
Henrik Rydgård committed 2017-06-04 11:52:14 +02:00
1 parent 8d3b18478c
commit 6e98dbb574
9 files changed
+268 -172

No files matched your search

+4
View File
@@ -863,6 +863,10 @@ add_library(native STATIC
ext/native/gfx_es2/draw_buffer.h
ext/native/gfx_es2/draw_text.cpp
ext/native/gfx_es2/draw_text.h
ext/native/gfx_es2/draw_text_win.cpp
ext/native/gfx_es2/draw_text_win.h
ext/native/gfx_es2/draw_text_qt.cpp
ext/native/gfx_es2/draw_text_qt.h
ext/native/gfx_es2/gpu_features.cpp
ext/native/gfx_es2/gpu_features.h
ext/native/gfx_es2/glsl_program.cpp
+3 -2
View File
@@ -7,6 +7,7 @@
#include "util/text/utf8.h"
#include "gfx_es2/draw_text.h"
#include "gfx_es2/draw_text_win.h"
#include "gfx_es2/draw_text_qt.h"
TextDrawer::TextDrawer(Draw::DrawContext *draw) : draw_(draw) {
// These probably shouldn't be state.
@@ -39,11 +40,11 @@ float TextDrawer::CalculateDPIScale() {
return scale;
}
TextDrawer *CreateTextDrawer(Draw::DrawContext *draw) {
TextDrawer *TextDrawer::Create(Draw::DrawContext *draw) {
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
return new TextDrawerWin32(draw);
#elif defined(USING_QT_UI)
return new TextDrawerWin32(draw);
return new TextDrawerQt(draw);
#else
return nullptr;
#endif
+6 -5
View File
@@ -49,7 +49,6 @@ enum {
class TextDrawer {
public:
TextDrawer(Draw::DrawContext *draw);
virtual ~TextDrawer();
virtual uint32_t SetFont(const char *fontName, int size, int flags) = 0;
@@ -64,7 +63,12 @@ public:
float CalculateDPIScale();
// Factory function that selects implementation.
static TextDrawer *Create(Draw::DrawContext *draw);
protected:
TextDrawer(Draw::DrawContext *draw);
Draw::DrawContext *draw_;
virtual void ClearCache() = 0;
virtual void RecreateFonts() = 0; // On DPI change
@@ -86,7 +90,4 @@ protected:
float MeasureWidth(const char *str, size_t bytes) override;
TextDrawer *drawer_;
};
TextDrawer *CreateTextDrawer(Draw::DrawContext *draw);
};
+205
View File
@@ -0,0 +1,205 @@
#include "base/display.h"
#include "base/logging.h"
#include "base/stringutil.h"
#include "thin3d/thin3d.h"
#include "util/hash/hash.h"
#include "util/text/wrap_text.h"
#include "util/text/utf8.h"
#include "gfx_es2/draw_text.h"
#include "gfx_es2/draw_text_qt.h"
#if defined(USING_QT_UI)
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <QtGui/QFontMetrics>
#include <QtOpenGL/QGLWidget>
TextDrawerQt::TextDrawerQt(Draw::DrawContext *draw) : TextDrawer(draw) {
}
TextDrawerQt::~TextDrawerQt() {
ClearCache();
}
uint32_t TextDrawerQt::SetFont(const char *fontName, int size, int flags) {
// We will only use the default font
uint32_t fontHash = 0; //hash::Fletcher((const uint8_t *)fontName, strlen(fontName));
fontHash ^= size;
fontHash ^= flags << 10;
auto iter = fontMap_.find(fontHash);
if (iter != fontMap_.end()) {
fontHash_ = fontHash;
return fontHash;
}
QFont* font = new QFont();
font->setPixelSize(size + 6);
fontMap_[fontHash] = font;
fontHash_ = fontHash;
return fontHash;
}
void TextDrawerQt::SetFont(uint32_t fontHandle) {
}
void TextDrawerQt::RecreateFonts() {
}
void TextDrawerQt::MeasureString(const char *str, size_t len, float *w, float *h) {
QFont* font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(str, (int)len));
*w = (float)size.width() * fontScaleX_;
*h = (float)size.height() * fontScaleY_;
}
void TextDrawerQt::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) {
std::string toMeasure = std::string(str, len);
if (align & FLAG_WRAP_TEXT) {
bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0;
WrapString(toMeasure, toMeasure.c_str(), rotated ? bounds.h : bounds.w);
}
QFont* font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(toMeasure.c_str(), (int)toMeasure.size()));
*w = (float)size.width() * fontScaleX_;
*h = (float)size.height() * fontScaleY_;
}
void TextDrawerQt::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) {
using namespace Draw;
if (!strlen(str))
return;
uint32_t stringHash = hash::Fletcher((const uint8_t *)str, strlen(str));
uint32_t entryHash = stringHash ^ fontHash_ ^ (align << 24);
target.Flush(true);
TextStringEntry *entry;
auto iter = cache_.find(entryHash);
if (iter != cache_.end()) {
entry = iter->second.get();
entry->lastUsedFrame = frameCount_;
draw_->BindTexture(0, entry->texture);
} else {
QFont *font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(str));
QImage image((size.width() + 3) & ~3, (size.height() + 3) & ~3, QImage::Format_ARGB32_Premultiplied);
if (image.isNull()) {
return;
}
image.fill(0);
QPainter painter;
painter.begin(&image);
painter.setFont(*font);
painter.setPen(color);
// TODO: Involve ALIGN_HCENTER (bounds etc.)
painter.drawText(image.rect(), Qt::AlignTop | Qt::AlignLeft, QString::fromUtf8(str).replace("&&", "&"));
painter.end();
entry = new TextStringEntry();
entry->bmWidth = entry->width = image.width();
entry->bmHeight = entry->height = image.height();
entry->lastUsedFrame = frameCount_;
TextureDesc desc{};
desc.type = TextureType::LINEAR2D;
desc.format = Draw::DataFormat::B4G4R4A4_UNORM_PACK16;
desc.width = entry->bmWidth;
desc.height = entry->bmHeight;
desc.depth = 1;
desc.mipLevels = 1;
uint16_t *bitmapData = new uint16_t[entry->bmWidth * entry->bmHeight];
for (int x = 0; x < entry->bmWidth; x++) {
for (int y = 0; y < entry->bmHeight; y++) {
bitmapData[entry->bmWidth * y + x] = 0xfff0 | image.pixel(x, y) >> 28;
}
}
desc.initData.push_back((uint8_t *)bitmapData);
entry->texture = draw_->CreateTexture(desc);
delete[] bitmapData;
cache_[entryHash] = std::unique_ptr<TextStringEntry>(entry);
}
float w = entry->bmWidth * fontScaleX_;
float h = entry->bmHeight * fontScaleY_;
DrawBuffer::DoAlign(align, &x, &y, &w, &h);
target.DrawTexRect(x, y, x + w, y + h, 0.0f, 0.0f, 1.0f, 1.0f, color);
target.Flush(true);
}
void TextDrawerQt::ClearCache() {
for (auto &iter : cache_) {
if (iter.second->texture)
iter.second->texture->Release();
}
cache_.clear();
sizeCache_.clear();
}
void TextDrawerQt::DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) {
float x = bounds.x;
float y = bounds.y;
if (align & ALIGN_HCENTER) {
x = bounds.centerX();
} else if (align & ALIGN_RIGHT) {
x = bounds.x2();
}
if (align & ALIGN_VCENTER) {
y = bounds.centerY();
} else if (align & ALIGN_BOTTOM) {
y = bounds.y2();
}
std::string toDraw = str;
if (align & FLAG_WRAP_TEXT) {
bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0;
WrapString(toDraw, str, rotated ? bounds.h : bounds.w);
}
DrawString(target, toDraw.c_str(), x, y, color, align);
}
void TextDrawerQt::OncePerFrame() {
frameCount_++;
// If DPI changed (small-mode, future proper monitor DPI support), drop everything.
float newDpiScale = CalculateDPIScale();
if (newDpiScale != dpiScale_) {
dpiScale_ = newDpiScale;
ClearCache();
RecreateFonts();
}
// Drop old strings. Use a prime number to reduce clashing with other rhythms
if (frameCount_ % 23 == 0) {
for (auto iter = cache_.begin(); iter != cache_.end();) {
if (frameCount_ - iter->second->lastUsedFrame > 100) {
if (iter->second->texture)
iter->second->texture->Release();
cache_.erase(iter++);
} else {
iter++;
}
}
for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) {
if (frameCount_ - iter->second->lastUsedFrame > 100) {
sizeCache_.erase(iter++);
} else {
iter++;
}
}
}
}
#endif
+35
View File
@@ -1 +1,36 @@
#pragma once
#include "ppsspp_config.h"
#include <map>
#include "gfx_es2/draw_text.h"
#if defined(USING_QT_UI)
class TextDrawerQt : public TextDrawer {
public:
TextDrawerQt(Draw::DrawContext *draw);
~TextDrawerQt();
uint32_t SetFont(const char *fontName, int size, int flags) override;
void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once.
void MeasureString(const char *str, size_t len, float *w, float *h) override;
void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override;
void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override;
void DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) override;
// Use for housekeeping like throwing out old strings.
void OncePerFrame() override;
protected:
void ClearCache() override;
void RecreateFonts() override; // On DPI change
uint32_t fontHash_;
std::map<uint32_t, QFont *> fontMap_;
// The key is the CityHash of the string xor the fontHash_.
std::map<uint32_t, std::unique_ptr<TextStringEntry>> cache_;
std::map<uint32_t, std::unique_ptr<TextMeasureEntry>> sizeCache_;
};
#endif
+3 -150
View File
@@ -324,155 +324,6 @@ void TextDrawerWin32::RecreateFonts() {
}
}
#else
TextDrawerWin32::TextDrawerWin32(Draw::DrawContext *thin3d) : draw_(thin3d), ctx_(NULL) {
fontScaleX_ = 1.0f;
fontScaleY_ = 1.0f;
}
TextDrawerWin32::~TextDrawerWin32() {
ClearCache();
}
uint32_t TextDrawerWin32::SetFont(const char *fontName, int size, int flags) {
#ifdef USING_QT_UI
// We will only use the default font
uint32_t fontHash = 0; //hash::Fletcher((const uint8_t *)fontName, strlen(fontName));
fontHash ^= size;
fontHash ^= flags << 10;
auto iter = fontMap_.find(fontHash);
if (iter != fontMap_.end()) {
fontHash_ = fontHash;
return fontHash;
}
QFont* font = new QFont();
font->setPixelSize(size + 6);
fontMap_[fontHash] = font;
fontHash_ = fontHash;
return fontHash;
#else
ELOG("System fonts not supported on this platform");
return 0;
#endif
}
void TextDrawerWin32::SetFont(uint32_t fontHandle) {
}
void TextDrawerWin32::RecreateFonts() {
}
void TextDrawerWin32::MeasureString(const char *str, float *w, float *h) {
MeasureString(str, strlen(str), w, h);
}
void TextDrawerWin32::MeasureString(const char *str, size_t len, float *w, float *h) {
#ifdef USING_QT_UI
QFont* font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(str, (int)len));
*w = (float)size.width() * fontScaleX_;
*h = (float)size.height() * fontScaleY_;
#else
*w = 0;
*h = 0;
#endif
}
void TextDrawerWin32::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) {
std::string toMeasure = std::string(str, len);
if (align & FLAG_WRAP_TEXT) {
bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0;
WrapString(toMeasure, toMeasure.c_str(), rotated ? bounds.h : bounds.w);
}
#ifdef USING_QT_UI
QFont* font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(toMeasure.c_str(), (int)toMeasure.size()));
*w = (float)size.width() * fontScaleX_;
*h = (float)size.height() * fontScaleY_;
#else
*w = 0;
*h = 0;
#endif
}
void TextDrawerWin32::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) {
using namespace Draw;
if (!strlen(str))
return;
#ifdef USING_QT_UI
uint32_t stringHash = hash::Fletcher((const uint8_t *)str, strlen(str));
uint32_t entryHash = stringHash ^ fontHash_ ^ (align << 24);
target.Flush(true);
TextStringEntry *entry;
auto iter = cache_.find(entryHash);
if (iter != cache_.end()) {
entry = iter->second.get();
entry->lastUsedFrame = frameCount_;
draw_->BindTexture(0, entry->texture);
} else {
QFont *font = fontMap_.find(fontHash_)->second;
QFontMetrics fm(*font);
QSize size = fm.size(0, QString::fromUtf8(str));
QImage image((size.width() + 3) & ~3, (size.height() + 3) & ~3, QImage::Format_ARGB32_Premultiplied);
if (image.isNull()) {
return;
}
image.fill(0);
QPainter painter;
painter.begin(&image);
painter.setFont(*font);
painter.setPen(color);
// TODO: Involve ALIGN_HCENTER (bounds etc.)
painter.drawText(image.rect(), Qt::AlignTop | Qt::AlignLeft, QString::fromUtf8(str).replace("&&", "&"));
painter.end();
entry = new TextStringEntry();
entry->bmWidth = entry->width = image.width();
entry->bmHeight = entry->height = image.height();
entry->lastUsedFrame = frameCount_;
TextureDesc desc{};
desc.type = TextureType::LINEAR2D;
desc.format = Draw::DataFormat::B4G4R4A4_UNORM_PACK16;
desc.width = entry->bmWidth;
desc.height = entry->bmHeight;
desc.depth = 1;
desc.mipLevels = 1;
uint16_t *bitmapData = new uint16_t[entry->bmWidth * entry->bmHeight];
for (int x = 0; x < entry->bmWidth; x++) {
for (int y = 0; y < entry->bmHeight; y++) {
bitmapData[entry->bmWidth * y + x] = 0xfff0 | image.pixel(x, y) >> 28;
}
}
desc.initData.push_back((uint8_t *)bitmapData);
entry->texture = draw_->CreateTexture(desc);
delete[] bitmapData;
cache_[entryHash] = std::unique_ptr<TextStringEntry>(entry);
}
float w = entry->bmWidth * fontScaleX_;
float h = entry->bmHeight * fontScaleY_;
DrawBuffer::DoAlign(align, &x, &y, &w, &h);
target.DrawTexRect(x, y, x + w, y + h, 0.0f, 0.0f, 1.0f, 1.0f, color);
target.Flush(true);
#endif
}
#endif
void TextDrawerWin32::ClearCache() {
for (auto &iter : cache_) {
if (iter.second->texture)
@@ -535,4 +386,6 @@ void TextDrawerWin32::OncePerFrame() {
}
}
}
}
}
#endif
+1 -1
View File
@@ -10,7 +10,7 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
class TextDrawerContext;
struct TextDrawerContext;
// Internal struct but all details in .cpp file (pimpl to avoid pulling in excessive headers here)
class TextDrawerFontContext;
+2 -5
View File
@@ -6,10 +6,7 @@
#include "gfx_es2/draw_buffer.h"
#include "gfx_es2/draw_text.h"
UIContext::UIContext()
: ui_pipeline_(0), uitexture_(0), uidrawbuffer_(0), uidrawbufferTop_(0) {
fontScaleX_ = 1.0f;
fontScaleY_ = 1.0f;
UIContext::UIContext() {
fontStyle_ = new UI::FontStyle();
bounds_ = Bounds(0, 0, dp_xres, dp_yres);
}
@@ -28,7 +25,7 @@ void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pi
ui_pipeline_notex_ = uipipenotex;
uidrawbuffer_ = uidrawbuffer;
uidrawbufferTop_ = uidrawbufferTop;
textDrawer_ = CreateTextDrawer(thin3d); // May return nullptr
textDrawer_ = TextDrawer::Create(thin3d); // May return nullptr if no implementation is available for this platform.
}
void UIContext::FrameSetup(Draw::Texture *uiTexture) {
+9 -9
View File
@@ -94,18 +94,18 @@ private:
Draw::DrawContext *draw_;
Bounds bounds_;
float fontScaleX_;
float fontScaleY_;
UI::FontStyle *fontStyle_;
TextDrawer *textDrawer_;
float fontScaleX_ = 1.0f;
float fontScaleY_ = 1.0f;
UI::FontStyle *fontStyle_ = nullptr;
TextDrawer *textDrawer_ = nullptr;
Draw::SamplerState *sampler_;
Draw::Pipeline *ui_pipeline_;
Draw::Pipeline *ui_pipeline_notex_;
Draw::Texture *uitexture_;
Draw::Pipeline *ui_pipeline_ = nullptr;
Draw::Pipeline *ui_pipeline_notex_ = nullptr;
Draw::Texture *uitexture_ = nullptr;
DrawBuffer *uidrawbuffer_;
DrawBuffer *uidrawbufferTop_;
DrawBuffer *uidrawbuffer_ = nullptr;
DrawBuffer *uidrawbufferTop_ = nullptr;
std::vector<Bounds> scissorStack_;
std::vector<UITransform> transformStack_;