[core] convert GlyphAtlas to use managed texture handling
This commit is contained in:
@@ -101,7 +101,7 @@ void Painter::renderSymbol(PaintParameters& parameters,
|
||||
|
||||
auto values = layer.impl->textPropertyValues(layout);
|
||||
|
||||
const Size texsize { glyphAtlas->width, glyphAtlas->height };
|
||||
const Size texsize = glyphAtlas->getSize()x§;
|
||||
|
||||
if (values.hasHalo()) {
|
||||
draw(parameters.shaders.symbolGlyph,
|
||||
|
||||
@@ -38,7 +38,7 @@ static Observer nullObserver;
|
||||
|
||||
Style::Style(FileSource& fileSource_, float pixelRatio)
|
||||
: fileSource(fileSource_),
|
||||
glyphAtlas(std::make_unique<GlyphAtlas>(2048, 2048, fileSource)),
|
||||
glyphAtlas(std::make_unique<GlyphAtlas>(Size{ 2048, 2048 }, fileSource)),
|
||||
spriteAtlas(std::make_unique<SpriteAtlas>(Size{ 1024, 1024 }, pixelRatio)),
|
||||
lineAtlas(std::make_unique<LineAtlas>(256, 512)),
|
||||
observer(&nullObserver) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <mbgl/text/glyph_atlas.hpp>
|
||||
#include <mbgl/text/glyph_atlas_observer.hpp>
|
||||
#include <mbgl/text/glyph_pbf.hpp>
|
||||
#include <mbgl/gl/gl.hpp>
|
||||
#include <mbgl/gl/context.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
@@ -13,13 +12,11 @@ namespace mbgl {
|
||||
|
||||
static GlyphAtlasObserver nullObserver;
|
||||
|
||||
GlyphAtlas::GlyphAtlas(uint16_t width_, uint16_t height_, FileSource& fileSource_)
|
||||
: width(width_),
|
||||
height(height_),
|
||||
fileSource(fileSource_),
|
||||
GlyphAtlas::GlyphAtlas(const Size size, FileSource& fileSource_)
|
||||
: fileSource(fileSource_),
|
||||
observer(&nullObserver),
|
||||
bin(width_, height_),
|
||||
data(std::make_unique<uint8_t[]>(width_ * height_)),
|
||||
bin(size.width, size.height),
|
||||
image(size),
|
||||
dirty(true) {
|
||||
}
|
||||
|
||||
@@ -148,18 +145,18 @@ Rect<uint16_t> GlyphAtlas::addGlyph(uintptr_t tileUID,
|
||||
return rect;
|
||||
}
|
||||
|
||||
assert(rect.x + rect.w <= width);
|
||||
assert(rect.y + rect.h <= height);
|
||||
assert(rect.x + rect.w <= image.size.width);
|
||||
assert(rect.y + rect.h <= image.size.height);
|
||||
|
||||
face.emplace(glyph.id, GlyphValue { rect, tileUID });
|
||||
|
||||
// Copy the bitmap
|
||||
const uint8_t* source = reinterpret_cast<const uint8_t*>(glyph.bitmap.data());
|
||||
for (uint32_t y = 0; y < buffered_height; y++) {
|
||||
uint32_t y1 = width * (rect.y + y + padding) + rect.x + padding;
|
||||
uint32_t y1 = image.size.width * (rect.y + y + padding) + rect.x + padding;
|
||||
uint32_t y2 = buffered_width * y;
|
||||
for (uint32_t x = 0; x < buffered_width; x++) {
|
||||
data[y1 + x] = source[y2 + x];
|
||||
image.data[y1 + x] = source[y2 + x];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,9 +178,9 @@ void GlyphAtlas::removeGlyphs(uintptr_t tileUID) {
|
||||
const Rect<uint16_t>& rect = value.rect;
|
||||
|
||||
// Clear out the bitmap.
|
||||
uint8_t *target = data.get();
|
||||
uint8_t *target = image.data.get();
|
||||
for (uint32_t y = 0; y < rect.h; y++) {
|
||||
uint32_t y1 = width * (rect.y + y) + rect.x;
|
||||
uint32_t y1 = image.size.width * (rect.y + y) + rect.x;
|
||||
for (uint32_t x = 0; x < rect.w; x++) {
|
||||
target[y1 + x] = 0;
|
||||
}
|
||||
@@ -203,60 +200,25 @@ void GlyphAtlas::removeGlyphs(uintptr_t tileUID) {
|
||||
}
|
||||
}
|
||||
|
||||
Size GlyphAtlas::getSize() const {
|
||||
return image.size;
|
||||
}
|
||||
|
||||
void GlyphAtlas::upload(gl::Context& context, gl::TextureUnit unit) {
|
||||
if (dirty) {
|
||||
const bool first = !texture;
|
||||
bind(context, unit);
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
context.activeTexture = unit;
|
||||
if (first) {
|
||||
MBGL_CHECK_ERROR(glTexImage2D(
|
||||
GL_TEXTURE_2D, // GLenum target
|
||||
0, // GLint level
|
||||
GL_ALPHA, // GLint internalformat
|
||||
width, // GLsizei width
|
||||
height, // GLsizei height
|
||||
0, // GLint border
|
||||
GL_ALPHA, // GLenum format
|
||||
GL_UNSIGNED_BYTE, // GLenum type
|
||||
data.get() // const GLvoid* data
|
||||
));
|
||||
} else {
|
||||
MBGL_CHECK_ERROR(glTexSubImage2D(
|
||||
GL_TEXTURE_2D, // GLenum target
|
||||
0, // GLint level
|
||||
0, // GLint xoffset
|
||||
0, // GLint yoffset
|
||||
width, // GLsizei width
|
||||
height, // GLsizei height
|
||||
GL_ALPHA, // GLenum format
|
||||
GL_UNSIGNED_BYTE, // GLenum type
|
||||
data.get() // const GLvoid* data
|
||||
));
|
||||
}
|
||||
|
||||
dirty = false;
|
||||
if (!texture) {
|
||||
texture = context.createTexture(image, unit);
|
||||
} else if (dirty) {
|
||||
context.updateTexture(*texture, image, unit);
|
||||
}
|
||||
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
void GlyphAtlas::bind(gl::Context& context, gl::TextureUnit unit) {
|
||||
if (!texture) {
|
||||
texture = context.createTexture();
|
||||
context.activeTexture = unit;
|
||||
context.texture[unit] = *texture;
|
||||
#if not MBGL_USE_GLES2
|
||||
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
|
||||
#endif
|
||||
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
|
||||
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
|
||||
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
|
||||
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
|
||||
} else if (context.texture[unit] != *texture) {
|
||||
context.activeTexture = unit;
|
||||
context.texture[unit] = *texture;
|
||||
}
|
||||
upload(context, unit);
|
||||
context.bindTexture(*texture, unit, gl::TextureFilter::Linear);
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <mbgl/util/font_stack.hpp>
|
||||
#include <mbgl/util/exclusive.hpp>
|
||||
#include <mbgl/util/work_queue.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/gl/texture.hpp>
|
||||
#include <mbgl/gl/object.hpp>
|
||||
|
||||
#include <atomic>
|
||||
@@ -30,7 +32,7 @@ class Context;
|
||||
|
||||
class GlyphAtlas : public util::noncopyable {
|
||||
public:
|
||||
GlyphAtlas(uint16_t width, uint16_t height, FileSource&);
|
||||
GlyphAtlas(Size, FileSource&);
|
||||
~GlyphAtlas();
|
||||
|
||||
util::exclusive<GlyphSet> getGlyphSet(const FontStack&);
|
||||
@@ -66,8 +68,7 @@ public:
|
||||
// the texture is only bound when the data is out of date (=dirty).
|
||||
void upload(gl::Context&, gl::TextureUnit unit);
|
||||
|
||||
const uint16_t width;
|
||||
const uint16_t height;
|
||||
Size getSize() const;
|
||||
|
||||
private:
|
||||
void requestGlyphRange(const FontStack&, const GlyphRange&);
|
||||
@@ -100,9 +101,9 @@ private:
|
||||
std::mutex mtx;
|
||||
BinPack<uint16_t> bin;
|
||||
std::unordered_map<FontStack, std::map<uint32_t, GlyphValue>, FontStackHash> index;
|
||||
const std::unique_ptr<uint8_t[]> data;
|
||||
const AlphaImage image;
|
||||
std::atomic<bool> dirty;
|
||||
mbgl::optional<gl::UniqueTexture> texture;
|
||||
mbgl::optional<gl::Texture> texture;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
util::RunLoop loop;
|
||||
StubFileSource fileSource;
|
||||
StubStyleObserver observer;
|
||||
GlyphAtlas glyphAtlas { 32, 32, fileSource };
|
||||
GlyphAtlas glyphAtlas{ { 32, 32 }, fileSource };
|
||||
|
||||
void run(const std::string& url, const FontStack& fontStack, const GlyphRangeSet& glyphRanges) {
|
||||
// Squelch logging.
|
||||
|
||||
Reference in New Issue
Block a user