Skip to content

Commit

Permalink
[drape] Moved Glyph out of GlyphManager (organicmaps#7860)
Browse files Browse the repository at this point in the history
* [drape] Moved Glyph out of GlyphManager

Also did small refactoring in a GlyphManager

Signed-off-by: Alexander Borsuk <[email protected]>
  • Loading branch information
biodranik authored Apr 9, 2024
1 parent 59672f5 commit 187c386
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 161 deletions.
1 change: 1 addition & 0 deletions drape/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(SRC
gl_includes.hpp
glsl_func.hpp
glsl_types.hpp
glyph.hpp
glyph_generator.cpp
glyph_generator.hpp
glyph_manager.cpp
Expand Down
4 changes: 2 additions & 2 deletions drape/drape_tests/glyph_mng_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class GlyphRenderer

void RenderGlyphs(QPaintDevice * device)
{
std::vector<dp::GlyphManager::Glyph> glyphs;
std::vector<dp::Glyph> glyphs;
auto generateGlyph = [this, &glyphs](strings::UniChar c)
{
dp::GlyphManager::Glyph g = m_mng->GetGlyph(c, dp::kDynamicGlyphSize);
dp::Glyph g = m_mng->GetGlyph(c, dp::kDynamicGlyphSize);
glyphs.push_back(dp::GlyphManager::GenerateGlyph(g, m_mng->GetSdfScale()));
g.m_image.Destroy();
};
Expand Down
4 changes: 2 additions & 2 deletions drape/font_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ ref_ptr<Texture::ResourceInfo> GlyphIndex::MapResource(GlyphKey const & key, boo

newResource = true;

GlyphManager::Glyph glyph = m_mng->GetGlyph(key.GetUnicodePoint(), key.GetFixedSize());
Glyph glyph = m_mng->GetGlyph(key.GetUnicodePoint(), key.GetFixedSize());
m2::RectU r;
if (!m_packer.PackGlyph(glyph.m_image.m_width, glyph.m_image.m_height, r))
{
Expand Down Expand Up @@ -239,7 +239,7 @@ void GlyphIndex::UploadResources(ref_ptr<dp::GraphicsContext> context, ref_ptr<T

for (size_t i = 0; i < pendingNodes.size(); ++i)
{
GlyphManager::Glyph & glyph = pendingNodes[i].second;
Glyph & glyph = pendingNodes[i].second;
m2::RectU const rect = pendingNodes[i].first;
m2::PointU const zeroPoint = rect.LeftBottom();
if (glyph.m_image.m_width == 0 || glyph.m_image.m_height == 0 || rect.SizeX() == 0 || rect.SizeY() == 0)
Expand Down
25 changes: 10 additions & 15 deletions drape/font_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GlyphKey : public Texture::Key
, m_fixedSize(fixedSize)
{}

Texture::ResourceType GetType() const { return Texture::ResourceType::Glyph; }
Texture::ResourceType GetType() const override { return Texture::ResourceType::Glyph; }
strings::UniChar GetUnicodePoint() const { return m_unicodePoint; }
int GetFixedSize() const { return m_fixedSize; }

Expand All @@ -57,20 +57,18 @@ class GlyphKey : public Texture::Key

class GlyphInfo : public Texture::ResourceInfo
{
using Base = Texture::ResourceInfo;

public:
GlyphInfo(m2::RectF const & texRect, GlyphManager::GlyphMetrics const & metrics)
: Base(texRect)
GlyphInfo(m2::RectF const & texRect, GlyphMetrics const & metrics)
: ResourceInfo(texRect)
, m_metrics(metrics)
{}
~GlyphInfo() override = default;

Texture::ResourceType GetType() const override { return Texture::ResourceType::Glyph; }
GlyphManager::GlyphMetrics const & GetMetrics() const { return m_metrics; }
GlyphMetrics const & GetMetrics() const { return m_metrics; }

private:
GlyphManager::GlyphMetrics m_metrics;
GlyphMetrics m_metrics;
};

class GlyphIndex : public GlyphGenerator::Listener
Expand Down Expand Up @@ -103,7 +101,7 @@ class GlyphIndex : public GlyphGenerator::Listener
ref_ptr<GlyphGenerator> m_generator;

using ResourceMapping = std::map<GlyphKey, GlyphInfo>;
using PendingNode = std::pair<m2::RectU, GlyphManager::Glyph>;
using PendingNode = std::pair<m2::RectU, Glyph>;
using PendingNodes = std::vector<PendingNode>;

ResourceMapping m_index;
Expand All @@ -113,21 +111,18 @@ class GlyphIndex : public GlyphGenerator::Listener

class FontTexture : public DynamicTexture<GlyphIndex, GlyphKey, Texture::ResourceType::Glyph>
{
using TBase = DynamicTexture<GlyphIndex, GlyphKey, Texture::ResourceType::Glyph>;
public:
FontTexture(m2::PointU const & size, ref_ptr<GlyphManager> glyphMng,
ref_ptr<GlyphGenerator> glyphGenerator, ref_ptr<HWTextureAllocator> allocator)
: m_index(size, glyphMng, glyphGenerator)
{
TBase::DynamicTextureParams params{size, TextureFormat::Alpha,
TextureFilter::Linear, true /* m_usePixelBuffer */};
TBase::Init(allocator, make_ref(&m_index), params);
DynamicTextureParams const params{size, TextureFormat::Alpha, TextureFilter::Linear, true /* m_usePixelBuffer */};
Init(allocator, make_ref(&m_index), params);
}

~FontTexture() override { TBase::Reset(); }
~FontTexture() override { Reset(); }

std::vector<ref_ptr<ResourceInfo>> FindResources(std::vector<GlyphKey> const & keys,
bool & hasNewResources)
std::vector<ref_ptr<ResourceInfo>> FindResources(std::vector<GlyphKey> const & keys, bool & hasNewResources) const
{
ASSERT(m_indexer != nullptr, ());
return m_indexer->MapResources(keys, hasNewResources);
Expand Down
52 changes: 52 additions & 0 deletions drape/glyph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "base/assert.hpp"
#include "base/shared_buffer_manager.hpp"
#include "base/string_utils.hpp"

namespace dp
{
struct GlyphMetrics
{
float m_xAdvance;
float m_yAdvance;
float m_xOffset;
float m_yOffset;
bool m_isValid;
};

struct GlyphImage
{
~GlyphImage()
{
ASSERT_NOT_EQUAL(m_data.use_count(), 1, ("Probably you forgot to call Destroy()"));
}

void Destroy()
{
if (m_data != nullptr)
{
SharedBufferManager::instance().freeSharedBuffer(m_data->size(), m_data);
m_data = nullptr;
}
}

uint32_t m_width;
uint32_t m_height;

uint32_t m_bitmapRows;
int m_bitmapPitch;

SharedBufferManager::shared_buffer_ptr_t m_data;
};

struct Glyph
{
GlyphMetrics m_metrics;
GlyphImage m_image;
int m_fontIndex;
strings::UniChar m_code;
int m_fixedSize;
};
} // namespace dp

6 changes: 2 additions & 4 deletions drape/glyph_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ void GlyphGenerator::UnregisterListener(ref_ptr<GlyphGenerator::Listener> listen
m_listeners.erase(listener);
}

void GlyphGenerator::GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect,
GlyphManager::Glyph & glyph)
void GlyphGenerator::GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect, Glyph & glyph)
{
GenerateGlyph(listener, GlyphGenerationData(rect, glyph));
}
Expand All @@ -60,8 +59,7 @@ void GlyphGenerator::GenerateGlyph(ref_ptr<Listener> listener, GlyphGenerationDa
GenerateGlyphs(listener, {std::move(data)});
}

void GlyphGenerator::GenerateGlyphs(ref_ptr<Listener> listener,
GlyphGenerationDataArray && generationData)
void GlyphGenerator::GenerateGlyphs(ref_ptr<Listener> listener, GlyphGenerationDataArray && generationData)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_listeners.find(listener) == m_listeners.end())
Expand Down
6 changes: 3 additions & 3 deletions drape/glyph_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class GlyphGenerator
struct GlyphGenerationData
{
m2::RectU m_rect;
GlyphManager::Glyph m_glyph;
Glyph m_glyph;

GlyphGenerationData() = default;
GlyphGenerationData(m2::RectU const & rect, GlyphManager::Glyph const & glyph)
GlyphGenerationData(m2::RectU const & rect, Glyph const & glyph)
: m_rect(rect), m_glyph(glyph)
{}

Expand Down Expand Up @@ -69,7 +69,7 @@ class GlyphGenerator
void RegisterListener(ref_ptr<Listener> listener);
void UnregisterListener(ref_ptr<Listener> listener);

void GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect, GlyphManager::Glyph & glyph);
void GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect, Glyph & glyph);
void GenerateGlyph(ref_ptr<Listener> listener, GlyphGenerationData && data);
void GenerateGlyphs(ref_ptr<Listener> listener, GlyphGenerationDataArray && generationData);
bool IsSuspended() const;
Expand Down
Loading

0 comments on commit 187c386

Please sign in to comment.