diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-10-26 18:06:36 -0700 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-11-01 18:41:52 +0100 |
commit | ed155460d3ec3777bfd95ce3d40c809e583b07de (patch) | |
tree | ce362d4154cc300b75d78ff8051ebf782834f044 /src/mbgl/geometry | |
parent | 7b50cac49f353524457e16d3f342299e0886e963 (diff) | |
download | qtlocation-mapboxgl-ed155460d3ec3777bfd95ce3d40c809e583b07de.tar.gz |
[core] convert LineAtlas to use managed texture handling
Diffstat (limited to 'src/mbgl/geometry')
-rw-r--r-- | src/mbgl/geometry/line_atlas.cpp | 83 | ||||
-rw-r--r-- | src/mbgl/geometry/line_atlas.hpp | 13 |
2 files changed, 30 insertions, 66 deletions
diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 50e82cc015..5e11fe5c49 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -11,10 +11,8 @@ namespace mbgl { -LineAtlas::LineAtlas(uint16_t w, uint16_t h) - : width(w), - height(h), - data(std::make_unique<char[]>(w * h)), +LineAtlas::LineAtlas(const Size size) + : image(size), dirty(true) { } @@ -40,11 +38,11 @@ LinePatternPos LineAtlas::getDashPosition(const std::vector<float>& dasharray, } LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatternCap patternCap) { - int n = patternCap == LinePatternCap::Round ? 7 : 0; - int dashheight = 2 * n + 1; + const uint8_t n = patternCap == LinePatternCap::Round ? 7 : 0; + const uint8_t dashheight = 2 * n + 1; const uint8_t offset = 128; - if (nextRow + dashheight > height) { + if (nextRow + dashheight > image.size.height) { Log::Warning(Event::OpenGL, "line atlas bitmap overflow"); return LinePatternPos(); } @@ -54,7 +52,7 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte length += part; } - float stretch = width / length; + float stretch = image.size.width / length; float halfWidth = stretch * 0.5; // If dasharray has an odd length, both the first and last parts // are dashes and should be joined seamlessly. @@ -62,7 +60,7 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte for (int y = -n; y <= n; y++) { int row = nextRow + n + y; - int index = width * row; + int index = image.size.width * row; float left = 0; float right = dasharray[0]; @@ -72,7 +70,7 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte left -= dasharray.back(); } - for (int x = 0; x < width; x++) { + for (uint32_t x = 0; x < image.size.width; x++) { while (right < x / stretch) { left = right; @@ -104,13 +102,13 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte signedDistance = int((inside ? 1 : -1) * dist); } - data[index + x] = fmax(0, fmin(255, signedDistance + offset)); + image.data[index + x] = fmax(0, fmin(255, signedDistance + offset)); } } LinePatternPos position; - position.y = (0.5 + nextRow + n) / height; - position.height = (2.0 * n) / height; + position.y = (0.5 + nextRow + n) / image.size.height; + position.height = (2.0 * n) / image.size.height; position.width = length; nextRow += dashheight; @@ -120,59 +118,24 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte return position; } -void LineAtlas::upload(gl::Context& context, gl::TextureUnit unit) { - if (dirty) { - bind(context, unit); - } +Size LineAtlas::getSize() const { + return image.size; } -void LineAtlas::bind(gl::Context& context, gl::TextureUnit unit) { - bool first = false; +void LineAtlas::upload(gl::Context& context, gl::TextureUnit unit) { if (!texture) { - texture = context.createTexture(); - context.activeTexture = unit; - context.texture[unit] = *texture; - 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_REPEAT)); - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - first = true; - } else if (context.texture[unit] != *texture) { - context.activeTexture = unit; - context.texture[unit] = *texture; + texture = context.createTexture(image, unit); + } else if (dirty) { + context.updateTexture(*texture, image, unit); } - if (dirty) { - 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 *pixels - )); - } - + dirty = false; +} - dirty = false; - } +void LineAtlas::bind(gl::Context& context, gl::TextureUnit unit) { + upload(context, unit); + context.bindTexture(*texture, unit, gl::TextureFilter::Linear, gl::TextureMipMap::No, + gl::TextureWrap::Repeat, gl::TextureWrap::Clamp); } } // namespace mbgl diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 66a2343a42..b1e7a670c1 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -1,6 +1,8 @@ #pragma once +#include <mbgl/gl/texture.hpp> #include <mbgl/gl/object.hpp> +#include <mbgl/util/image.hpp> #include <mbgl/util/optional.hpp> #include <vector> @@ -27,7 +29,7 @@ enum class LinePatternCap : bool { class LineAtlas { public: - LineAtlas(uint16_t width, uint16_t height); + LineAtlas(Size); ~LineAtlas(); // Binds the atlas texture to the GPU, and uploads data if it is out of date. @@ -40,14 +42,13 @@ public: LinePatternPos getDashPosition(const std::vector<float>&, LinePatternCap); LinePatternPos addDash(const std::vector<float>& dasharray, LinePatternCap); - const uint16_t width; - const uint16_t height; + Size getSize() const; private: - const std::unique_ptr<char[]> data; + const AlphaImage image; bool dirty; - mbgl::optional<gl::UniqueTexture> texture; - int nextRow = 0; + mbgl::optional<gl::Texture> texture; + uint32_t nextRow = 0; std::unordered_map<size_t, LinePatternPos> positions; }; |