From 7b50cac49f353524457e16d3f342299e0886e963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 26 Oct 2016 17:32:07 -0700 Subject: [PATCH] [core] convert GlyphAtlas to use managed texture handling --- src/mbgl/renderer/painter_symbol.cpp | 2 +- src/mbgl/style/style.cpp | 2 +- src/mbgl/text/glyph_atlas.cpp | 84 ++++++++-------------------- src/mbgl/text/glyph_atlas.hpp | 11 ++-- test/text/glyph_atlas.test.cpp | 2 +- 5 files changed, 32 insertions(+), 69 deletions(-) diff --git a/src/mbgl/renderer/painter_symbol.cpp b/src/mbgl/renderer/painter_symbol.cpp index a00389ce2..e13c04120 100644 --- a/src/mbgl/renderer/painter_symbol.cpp +++ b/src/mbgl/renderer/painter_symbol.cpp @@ -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, diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index c33a10517..bcc062f82 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -38,7 +38,7 @@ static Observer nullObserver; Style::Style(FileSource& fileSource_, float pixelRatio) : fileSource(fileSource_), - glyphAtlas(std::make_unique(2048, 2048, fileSource)), + glyphAtlas(std::make_unique(Size{ 2048, 2048 }, fileSource)), spriteAtlas(std::make_unique(Size{ 1024, 1024 }, pixelRatio)), lineAtlas(std::make_unique(256, 512)), observer(&nullObserver) { diff --git a/src/mbgl/text/glyph_atlas.cpp b/src/mbgl/text/glyph_atlas.cpp index 2f8c44db5..031e89d13 100644 --- a/src/mbgl/text/glyph_atlas.cpp +++ b/src/mbgl/text/glyph_atlas.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -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(width_ * height_)), + bin(size.width, size.height), + image(size), dirty(true) { } @@ -148,18 +145,18 @@ Rect 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(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& 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 lock(mtx); - std::lock_guard 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 diff --git a/src/mbgl/text/glyph_atlas.hpp b/src/mbgl/text/glyph_atlas.hpp index 84875bdd7..550ca4cc1 100644 --- a/src/mbgl/text/glyph_atlas.hpp +++ b/src/mbgl/text/glyph_atlas.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -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 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 bin; std::unordered_map, FontStackHash> index; - const std::unique_ptr data; + const AlphaImage image; std::atomic dirty; - mbgl::optional texture; + mbgl::optional texture; }; } // namespace mbgl diff --git a/test/text/glyph_atlas.test.cpp b/test/text/glyph_atlas.test.cpp index 8634cbe91..e8564cc69 100644 --- a/test/text/glyph_atlas.test.cpp +++ b/test/text/glyph_atlas.test.cpp @@ -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.