From 1759603e128ad0a08f4a25009b82695420ec2840 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 18 May 2017 09:37:21 -0700 Subject: [core] Simplify and fix sprite atlas coordinate calculations * Always return image metrics exclusive of padding * Work with integer coordinates whenever possible * Eliminate redundant SpriteAtlasElement members * Fix asymmetric re-padding in getIconQuad when pixelRatio != 1 * Add explanatory comments --- src/mbgl/sprite/sprite_atlas.hpp | 42 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'src/mbgl/sprite/sprite_atlas.hpp') diff --git a/src/mbgl/sprite/sprite_atlas.hpp b/src/mbgl/sprite/sprite_atlas.hpp index 52749d389b..3629ed6eb1 100644 --- a/src/mbgl/sprite/sprite_atlas.hpp +++ b/src/mbgl/sprite/sprite_atlas.hpp @@ -20,15 +20,32 @@ class Context; class SpriteAtlasElement { public: - SpriteAtlasElement(Rect, const style::Image::Impl&, float pixelRatio); + SpriteAtlasElement(Rect, const style::Image::Impl&); - Rect pos; bool sdf; + float pixelRatio; + Rect textureRect; + + std::array tl() const { + return {{ + textureRect.x, + textureRect.y + }}; + } + + std::array br() const { + return {{ + static_cast(textureRect.x + textureRect.w), + static_cast(textureRect.y + textureRect.h) + }}; + } - float relativePixelRatio; - std::array size; - std::array tl; - std::array br; + std::array displaySize() const { + return {{ + textureRect.w / pixelRatio, + textureRect.h / pixelRatio, + }}; + } }; using IconMap = std::unordered_map; @@ -64,7 +81,14 @@ public: void getIcons(IconRequestor& requestor); void removeRequestor(IconRequestor& requestor); + // Ensure that the atlas contains the named image suitable for rendering as an icon, and + // return its metrics. The image will be padded on each side with a one pixel wide transparent + // strip, but the returned metrics are exclusive of this padding. optional getIcon(const std::string& name); + + // Ensure that the atlas contains the named image suitable for rendering as an pattern, and + // return its metrics. The image will be padded on each side with a one pixel wide copy of + // pixels from the opposite side, but the returned metrics are exclusive of this padding. optional getPattern(const std::string& name); // Binds the atlas texture to the GPU, and uploads data if it is out of date. @@ -74,8 +98,7 @@ public: // the texture is only bound when the data is out of date (=dirty). void upload(gl::Context&, gl::TextureUnit unit); - Size getSize() const { return size; } - float getPixelRatio() const { return pixelRatio; } + Size getPixelSize() const; // Only for use in tests. const PremultipliedImage& getAtlasImage() const { @@ -83,8 +106,7 @@ public: } private: - const Size size; - const float pixelRatio; + const Size pixelSize; bool loaded = false; struct Entry { -- cgit v1.2.1