diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index 7b77d7b35..825c02658 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -22,8 +22,10 @@ using namespace mbgl; SpriteAtlas::SpriteAtlas(dimension width_, dimension height_, float pixelRatio_, SpriteStore& store_) : width(width_), height(height_), - store(store_), + pixelWidth(std::ceil(width * pixelRatio_)), + pixelHeight(std::ceil(height * pixelRatio_)), pixelRatio(pixelRatio_), + store(store_), bin(width_, height_), dirty(true) { } @@ -103,10 +105,8 @@ SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repea void SpriteAtlas::allocate() { if (!data) { - dimension w = static_cast(width * pixelRatio); - dimension h = static_cast(height * pixelRatio); - data = std::make_unique(w * h); - std::fill(data.get(), data.get() + w * h, 0); + data = std::make_unique(pixelWidth * pixelHeight); + std::fill(data.get(), data.get() + pixelWidth * pixelHeight, 0); } } @@ -121,12 +121,11 @@ void SpriteAtlas::copy(const Holder& holder, const bool wrap) { allocate(); uint32_t *const dstData = data.get(); - const vec2 dstSize { static_cast(width * pixelRatio), - static_cast(height * pixelRatio) }; - const Rect dstPos { static_cast((offset + dst.x) * pixelRatio), - static_cast((offset + dst.y) * pixelRatio), - static_cast(dst.originalW * pixelRatio), - static_cast(dst.originalH * pixelRatio) }; + const vec2 dstSize{ pixelWidth, pixelHeight }; + const Rect dstPos{ static_cast((offset + dst.x) * pixelRatio), + static_cast((offset + dst.y) * pixelRatio), + static_cast(dst.originalW * pixelRatio), + static_cast(dst.originalH * pixelRatio) }; util::bilinearScale(srcData, srcSize, srcPos, dstData, dstSize, dstPos, wrap); @@ -230,8 +229,8 @@ void SpriteAtlas::bind(bool linear) { GL_TEXTURE_2D, // GLenum target 0, // GLint level GL_RGBA, // GLint internalformat - width * pixelRatio, // GLsizei width - height * pixelRatio, // GLsizei height + pixelWidth, // GLsizei width + pixelHeight, // GLsizei height 0, // GLint border GL_RGBA, // GLenum format GL_UNSIGNED_BYTE, // GLenum type @@ -244,8 +243,8 @@ void SpriteAtlas::bind(bool linear) { 0, // GLint level 0, // GLint xoffset 0, // GLint yoffset - width * pixelRatio, // GLsizei width - height * pixelRatio, // GLsizei height + pixelWidth, // GLsizei width + pixelHeight, // GLsizei height GL_RGBA, // GLenum format GL_UNSIGNED_BYTE, // GLenum type data.get() // const GLvoid *pixels @@ -256,8 +255,7 @@ void SpriteAtlas::bind(bool linear) { #ifndef GL_ES_VERSION_2_0 // platform::showColorDebugImage("Sprite Atlas", reinterpret_cast(data.get()), - // width * pixelRatio, height * pixelRatio, width * pixelRatio, - // height * pixelRatio); + // pixelWidth, pixelHeight, pixelWidth, pixelHeight); #endif } }; diff --git a/src/mbgl/geometry/sprite_atlas.hpp b/src/mbgl/geometry/sprite_atlas.hpp index 88e532f5f..6642e04bf 100644 --- a/src/mbgl/geometry/sprite_atlas.hpp +++ b/src/mbgl/geometry/sprite_atlas.hpp @@ -61,17 +61,18 @@ public: // the texture is only bound when the data is out of date (=dirty). void upload(); - inline float getWidth() const { return width; } - inline float getHeight() const { return height; } - inline float getTextureWidth() const { return width * pixelRatio; } - inline float getTextureHeight() const { return height * pixelRatio; } + inline dimension getWidth() const { return width; } + inline dimension getHeight() const { return height; } + inline dimension getTextureWidth() const { return pixelWidth; } + inline dimension getTextureHeight() const { return pixelHeight; } inline float getPixelRatio() const { return pixelRatio; } inline const uint32_t* getData() const { return data.get(); } - const dimension width = 0; - const dimension height = 0; - private: + const dimension width, height; + const dimension pixelWidth, pixelHeight; + const float pixelRatio; + struct Holder : private util::noncopyable { inline Holder(const std::shared_ptr&, const Rect&); inline Holder(Holder&&); @@ -87,7 +88,6 @@ private: std::recursive_mutex mtx; SpriteStore& store; - const float pixelRatio; BinPack bin; std::map images; std::set uninitialized; diff --git a/test/annotations/sprite_atlas.cpp b/test/annotations/sprite_atlas.cpp index 92091a16c..5fc867daf 100644 --- a/test/annotations/sprite_atlas.cpp +++ b/test/annotations/sprite_atlas.cpp @@ -82,3 +82,14 @@ TEST(Annotations, SpriteAtlas) { // "test/fixtures/annotations/atlas1.png", // util::compress_png(atlas.getTextureWidth(), atlas.getTextureHeight(), atlas.getData())); } + +TEST(Annotations, SpriteAtlasSize) { + SpriteStore store; + SpriteAtlas atlas(63, 112, 1.4, store); + + EXPECT_DOUBLE_EQ(1.4f, atlas.getPixelRatio()); + EXPECT_EQ(63, atlas.getWidth()); + EXPECT_EQ(112, atlas.getHeight()); + EXPECT_EQ(89, atlas.getTextureWidth()); + EXPECT_EQ(157, atlas.getTextureHeight()); +}