Add extra observer hooks for interesting internal processes (#2694)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <mbgl/actor/scheduler.hpp>
|
||||
#include <mbgl/gfx/backend.hpp>
|
||||
#include <mbgl/gfx/command_encoder.hpp>
|
||||
#include <mbgl/gfx/context_observer.hpp>
|
||||
#include <mbgl/gfx/draw_scope.hpp>
|
||||
#include <mbgl/gfx/program.hpp>
|
||||
#include <mbgl/gfx/renderbuffer.hpp>
|
||||
@@ -51,10 +52,15 @@ using UniqueDrawableBuilder = std::unique_ptr<DrawableBuilder>;
|
||||
using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
ContextObserver nullObserver;
|
||||
}
|
||||
|
||||
class Context {
|
||||
protected:
|
||||
Context(uint32_t maximumVertexBindingCount_)
|
||||
: maximumVertexBindingCount(maximumVertexBindingCount_) {}
|
||||
: maximumVertexBindingCount(maximumVertexBindingCount_),
|
||||
observer(&nullObserver) {}
|
||||
|
||||
public:
|
||||
static constexpr const uint32_t minimumRequiredVertexBindingCount = 8;
|
||||
@@ -66,6 +72,8 @@ public:
|
||||
Context& operator=(const Context& other) = delete;
|
||||
virtual ~Context() = default;
|
||||
|
||||
virtual void setObserver(ContextObserver* observer_) { observer = observer_ ? observer_ : &nullObserver; }
|
||||
|
||||
virtual void beginFrame() = 0;
|
||||
virtual void endFrame() = 0;
|
||||
|
||||
@@ -173,6 +181,7 @@ protected:
|
||||
virtual std::unique_ptr<DrawScopeResource> createDrawScopeResource() = 0;
|
||||
|
||||
gfx::RenderingStats stats;
|
||||
ContextObserver* observer;
|
||||
};
|
||||
|
||||
} // namespace gfx
|
||||
|
||||
19
include/mbgl/gfx/context_observer.hpp
Normal file
19
include/mbgl/gfx/context_observer.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/backend.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace gfx {
|
||||
|
||||
class ContextObserver {
|
||||
public:
|
||||
virtual void onPreCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onPostCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onShaderCompileFailed(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
};
|
||||
|
||||
} // namespace gfx
|
||||
} // namespace mbgl
|
||||
@@ -1,7 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/tile/tile_operation.hpp>
|
||||
#include <mbgl/tile/tile_id.hpp>
|
||||
#include <mbgl/style/source.hpp>
|
||||
#include <mbgl/style/image.hpp>
|
||||
#include <mbgl/style/sprite.hpp>
|
||||
#include <mbgl/text/glyph.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@@ -65,7 +70,25 @@ public:
|
||||
virtual bool onCanRemoveUnusedStyleImage(const std::string&) { return true; }
|
||||
// Observe this event to easily mutate or observe shaders as soon
|
||||
// as the registry becomes available.
|
||||
virtual void onRegisterShaders(gfx::ShaderRegistry&) {};
|
||||
virtual void onRegisterShaders(gfx::ShaderRegistry&) {}
|
||||
|
||||
// Shaders compilation
|
||||
virtual void onPreCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onPostCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onShaderCompileFailed(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
|
||||
// Glyph requests
|
||||
virtual void onGlyphsLoaded(const FontStack&, const GlyphRange&) {}
|
||||
virtual void onGlyphsError(const FontStack&, const GlyphRange&, std::exception_ptr) {}
|
||||
virtual void onGlyphsRequested(const FontStack&, const GlyphRange&) {}
|
||||
|
||||
// Tile requests
|
||||
virtual void onTileAction(TileOperation, const OverscaledTileID&, const std::string&) {}
|
||||
|
||||
// Sprite requests
|
||||
virtual void onSpriteLoaded(const std::optional<style::Sprite>&) {}
|
||||
virtual void onSpriteError(const std::optional<style::Sprite>&, std::exception_ptr) {}
|
||||
virtual void onSpriteRequested(const std::optional<style::Sprite>&) {}
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -66,7 +66,8 @@ public:
|
||||
BufferResource createBuffer(
|
||||
const void* data, std::size_t size, gfx::BufferUsageType usage, bool isIndexBuffer, bool persistent) const;
|
||||
|
||||
UniqueShaderProgram createProgram(std::string name,
|
||||
UniqueShaderProgram createProgram(shaders::BuiltIn shaderID,
|
||||
std::string name,
|
||||
std::string_view source,
|
||||
std::string_view vertexName,
|
||||
std::string_view fragmentName,
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/tile/tile_id.hpp>
|
||||
#include <mbgl/util/font_stack.hpp>
|
||||
#include <mbgl/text/glyph_range.hpp>
|
||||
#include <mbgl/tile/tile_operation.hpp>
|
||||
#include <mbgl/gfx/backend.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
@@ -54,6 +61,17 @@ public:
|
||||
|
||||
// Entry point for custom shader registration
|
||||
virtual void onRegisterShaders(gfx::ShaderRegistry&) {};
|
||||
virtual void onPreCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onPostCompileShader(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
virtual void onShaderCompileFailed(shaders::BuiltIn, gfx::Backend::Type, const std::string&) {}
|
||||
|
||||
// Glyph loading
|
||||
virtual void onGlyphsLoaded(const FontStack&, const GlyphRange&) {}
|
||||
virtual void onGlyphsError(const FontStack&, const GlyphRange&, std::exception_ptr) {}
|
||||
virtual void onGlyphsRequested(const FontStack&, const GlyphRange&) {}
|
||||
|
||||
// Tile loading
|
||||
virtual void onTileAction(TileOperation, const OverscaledTileID&, const std::string&) {}
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -14,7 +14,7 @@ class ShaderGroupGL final : public gfx::ShaderGroup {
|
||||
public:
|
||||
ShaderGroupGL(const ProgramParameters& programParameters_)
|
||||
: gfx::ShaderGroup(),
|
||||
programParameters(programParameters_) {}
|
||||
programParameters(programParameters_.withProgramType(ShaderID)) {}
|
||||
~ShaderGroupGL() noexcept override = default;
|
||||
|
||||
gfx::ShaderPtr getOrCreateShader(gfx::Context& context,
|
||||
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
auto& context = static_cast<Context&>(gfxContext);
|
||||
const auto shaderSource = std::string(shaders::prelude) + source;
|
||||
shader = context.createProgram(
|
||||
shaderName, shaderSource, vertMain, fragMain, programParameters, additionalDefines);
|
||||
ShaderID, shaderName, shaderSource, vertMain, fragMain, programParameters, additionalDefines);
|
||||
assert(shader);
|
||||
if (!shader || !registerShader(shader, shaderName)) {
|
||||
assert(false);
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
addAdditionalDefines(propertiesAsUniforms, additionalDefines);
|
||||
|
||||
auto& context = static_cast<Context&>(gfxContext);
|
||||
shader = context.createProgram(shaderName, vert, frag, programParameters, additionalDefines);
|
||||
shader = context.createProgram(ShaderID, shaderName, vert, frag, programParameters, additionalDefines);
|
||||
assert(shader);
|
||||
if (!shader || !registerShader(shader, shaderName)) {
|
||||
assert(false);
|
||||
|
||||
@@ -51,12 +51,14 @@ using UniqueShaderProgram = std::unique_ptr<ShaderProgram>;
|
||||
|
||||
class ShaderProgram final : public gfx::ShaderProgramBase {
|
||||
public:
|
||||
ShaderProgram(const std::string& name,
|
||||
ShaderProgram(shaders::BuiltIn shaderID,
|
||||
const std::string& name,
|
||||
const std::string_view& vertex,
|
||||
const std::string_view& fragment,
|
||||
const ProgramParameters& programParameters,
|
||||
const mbgl::unordered_map<std::string, std::string>& additionalDefines,
|
||||
RendererBackend& backend);
|
||||
RendererBackend& backend,
|
||||
gfx::ContextObserver& observer);
|
||||
~ShaderProgram() noexcept override;
|
||||
|
||||
static constexpr std::string_view Name{"GenericVulkanShader"};
|
||||
|
||||
18
include/mbgl/style/sprite.hpp
Normal file
18
include/mbgl/style/sprite.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
class Sprite {
|
||||
public:
|
||||
Sprite(std::string, std::string);
|
||||
~Sprite();
|
||||
|
||||
std::string id;
|
||||
std::string spriteURL;
|
||||
};
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
135
include/mbgl/text/glyph.hpp
Normal file
135
include/mbgl/text/glyph.hpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/text/glyph_range.hpp>
|
||||
#include <mbgl/util/bitmask_operations.hpp>
|
||||
#include <mbgl/util/font_stack.hpp>
|
||||
#include <mbgl/util/rect.hpp>
|
||||
#include <mbgl/util/traits.hpp>
|
||||
#include <mbgl/util/immutable.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
using GlyphID = char16_t;
|
||||
using GlyphIDs = std::set<GlyphID>;
|
||||
|
||||
// Note: this only works for the BMP
|
||||
GlyphRange getGlyphRange(GlyphID glyph);
|
||||
|
||||
struct GlyphMetrics {
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
int32_t left = 0;
|
||||
int32_t top = 0;
|
||||
uint32_t advance = 0;
|
||||
};
|
||||
|
||||
inline bool operator==(const GlyphMetrics& lhs, const GlyphMetrics& rhs) {
|
||||
return lhs.width == rhs.width && lhs.height == rhs.height && lhs.left == rhs.left && lhs.top == rhs.top &&
|
||||
lhs.advance == rhs.advance;
|
||||
}
|
||||
|
||||
class Glyph {
|
||||
public:
|
||||
// We're using this value throughout the Mapbox GL ecosystem. If this is
|
||||
// different, the glyphs also need to be reencoded.
|
||||
static constexpr const uint8_t borderSize = 3;
|
||||
|
||||
GlyphID id = 0;
|
||||
|
||||
// A signed distance field of the glyph with a border (see above).
|
||||
AlphaImage bitmap;
|
||||
|
||||
// Glyph metrics
|
||||
GlyphMetrics metrics;
|
||||
};
|
||||
|
||||
using Glyphs = std::map<GlyphID, std::optional<Immutable<Glyph>>>;
|
||||
using GlyphMap = std::map<FontStackHash, Glyphs>;
|
||||
|
||||
class PositionedGlyph {
|
||||
public:
|
||||
explicit PositionedGlyph(GlyphID glyph_,
|
||||
float x_,
|
||||
float y_,
|
||||
bool vertical_,
|
||||
FontStackHash font_,
|
||||
float scale_,
|
||||
Rect<uint16_t> rect_,
|
||||
GlyphMetrics metrics_,
|
||||
std::optional<std::string> imageID_,
|
||||
std::size_t sectionIndex_ = 0)
|
||||
: glyph(glyph_),
|
||||
x(x_),
|
||||
y(y_),
|
||||
vertical(vertical_),
|
||||
font(font_),
|
||||
scale(scale_),
|
||||
rect(rect_),
|
||||
metrics(metrics_),
|
||||
imageID(std::move(imageID_)),
|
||||
sectionIndex(sectionIndex_) {}
|
||||
|
||||
GlyphID glyph = 0;
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
bool vertical = false;
|
||||
FontStackHash font = 0;
|
||||
float scale = 0.0;
|
||||
Rect<uint16_t> rect;
|
||||
GlyphMetrics metrics;
|
||||
std::optional<std::string> imageID;
|
||||
// Maps positioned glyph to TaggedString section
|
||||
std::size_t sectionIndex;
|
||||
};
|
||||
|
||||
enum class WritingModeType : uint8_t;
|
||||
|
||||
struct PositionedLine {
|
||||
std::vector<PositionedGlyph> positionedGlyphs;
|
||||
float lineOffset = 0.0;
|
||||
};
|
||||
|
||||
class Shaping {
|
||||
public:
|
||||
Shaping() = default;
|
||||
explicit Shaping(float x, float y, WritingModeType writingMode_)
|
||||
: top(y),
|
||||
bottom(y),
|
||||
left(x),
|
||||
right(x),
|
||||
writingMode(writingMode_) {}
|
||||
std::vector<PositionedLine> positionedLines;
|
||||
float top = 0;
|
||||
float bottom = 0;
|
||||
float left = 0;
|
||||
float right = 0;
|
||||
WritingModeType writingMode;
|
||||
explicit operator bool() const {
|
||||
return std::any_of(positionedLines.begin(), positionedLines.end(), [](const auto& line) {
|
||||
return !line.positionedGlyphs.empty();
|
||||
});
|
||||
}
|
||||
// The y offset *should* be part of the font metadata.
|
||||
static constexpr int32_t yOffset = -17;
|
||||
bool verticalizable = false;
|
||||
bool iconsInText = false;
|
||||
};
|
||||
|
||||
enum class WritingModeType : uint8_t {
|
||||
None = 0,
|
||||
Horizontal = 1 << 0,
|
||||
Vertical = 1 << 1,
|
||||
};
|
||||
|
||||
using GlyphDependencies = std::map<FontStack, GlyphIDs>;
|
||||
using GlyphRangeDependencies = std::map<FontStack, std::unordered_set<GlyphRange>>;
|
||||
|
||||
} // end namespace mbgl
|
||||
26
include/mbgl/text/glyph_range.hpp
Normal file
26
include/mbgl/text/glyph_range.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
#include <unordered_set>
|
||||
#include <mbgl/util/hash.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
using GlyphRange = std::pair<uint16_t, uint16_t>;
|
||||
|
||||
constexpr uint32_t GLYPHS_PER_GLYPH_RANGE = 256;
|
||||
constexpr uint32_t GLYPH_RANGES_PER_FONT_STACK = 256;
|
||||
// 256 - 126 ranges skipped w/ i18n::allowsFixedWidthGlyphGeneration
|
||||
constexpr uint32_t NON_IDEOGRAPH_GLYPH_RANGES_PER_FONT_STACK = 130;
|
||||
|
||||
} // end namespace mbgl
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<mbgl::GlyphRange> {
|
||||
std::size_t operator()(const mbgl::GlyphRange& range) const { return mbgl::util::hash(range.first, range.second); }
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
16
include/mbgl/tile/tile_operation.hpp
Normal file
16
include/mbgl/tile/tile_operation.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
namespace mbgl {
|
||||
enum class TileOperation : uint8_t {
|
||||
RequestedFromCache, ///< A read request from the cache
|
||||
RequestedFromNetwork, ///< A read request from the online source
|
||||
LoadFromNetwork, ///< Tile data from the network has been retrieved
|
||||
LoadFromCache, ///< Tile data from the cache has been retrieved
|
||||
StartParse, ///< Background processing of tile data has been initiated
|
||||
EndParse, ///< Background processing of tile data has been completed
|
||||
Error, ///< An error occurred while loading the tile
|
||||
Cancelled, ///< Loading of a tile was cancelled
|
||||
NullOp, ///< No operation has taken place
|
||||
};
|
||||
} // namespace mbgl
|
||||
50
include/mbgl/util/hash.hpp
Normal file
50
include/mbgl/util/hash.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/ignore.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
template <class T>
|
||||
void hash_combine(std::size_t& seed, const T& v) noexcept {
|
||||
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
||||
}
|
||||
|
||||
template <class... Args>
|
||||
std::size_t hash(Args&&... args) noexcept {
|
||||
std::size_t seed = 0;
|
||||
ignore({(hash_combine(seed, args), 0)...});
|
||||
return seed;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template <typename TIter>
|
||||
using TIterVal = std::enable_if_t<std::is_integral_v<typename TIter::value_type>, typename TIter::value_type>;
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
constexpr T factor() noexcept {
|
||||
// Default factor prime value from 64-bit FNV hash.
|
||||
if constexpr (sizeof(T) == 8) return 1099511628211;
|
||||
// for 32-bit hash
|
||||
return 16777619;
|
||||
}
|
||||
|
||||
/// Generate a hash key from a collection of integer values which doesn't depend on their order.
|
||||
/// Adapted from https://stackoverflow.com/a/76993810/135138
|
||||
template <typename TIter, typename TKey = detail::TIterVal<TIter>, TKey factor = factor<TKey>()>
|
||||
TKey order_independent_hash(std::remove_const_t<TIter> cur, const TIter end) noexcept {
|
||||
detail::TIterVal<TIter> sum = 0, product = 1;
|
||||
for (; cur != end; ++cur) {
|
||||
const auto& value = *cur;
|
||||
sum += value;
|
||||
product *= (1 + value) * factor;
|
||||
}
|
||||
return sum ^ product;
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
28
include/mbgl/util/rect.hpp
Normal file
28
include/mbgl/util/rect.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
template <typename T>
|
||||
struct Rect {
|
||||
Rect() = default;
|
||||
Rect(T x_, T y_, T w_, T h_)
|
||||
: x(x_),
|
||||
y(y_),
|
||||
w(w_),
|
||||
h(h_) {}
|
||||
T x = 0, y = 0;
|
||||
T w = 0, h = 0;
|
||||
|
||||
template <typename Number>
|
||||
Rect operator*(Number value) const {
|
||||
return Rect(x * value, y * value, w * value, h * value);
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
bool operator==(const R& r) const {
|
||||
return x == r.x && y == r.y && w == r.w && h == r.h;
|
||||
}
|
||||
|
||||
bool hasArea() const { return w != 0 && h != 0; }
|
||||
};
|
||||
} // namespace mbgl
|
||||
@@ -60,7 +60,8 @@ public:
|
||||
|
||||
BufferResource createBuffer(const void* data, std::size_t size, std::uint32_t usage, bool persistent) const;
|
||||
|
||||
UniqueShaderProgram createProgram(std::string name,
|
||||
UniqueShaderProgram createProgram(shaders::BuiltIn shaderID,
|
||||
std::string name,
|
||||
const std::string_view vertex,
|
||||
const std::string_view fragment,
|
||||
const ProgramParameters& programParameters,
|
||||
|
||||
Reference in New Issue
Block a user