add test for fractional pixel ratios
This commit is contained in:
@@ -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<dimension>(width * pixelRatio);
|
||||
dimension h = static_cast<dimension>(height * pixelRatio);
|
||||
data = std::make_unique<uint32_t[]>(w * h);
|
||||
std::fill(data.get(), data.get() + w * h, 0);
|
||||
data = std::make_unique<uint32_t[]>(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<uint32_t> dstSize { static_cast<unsigned int>(width * pixelRatio),
|
||||
static_cast<unsigned int>(height * pixelRatio) };
|
||||
const Rect<uint32_t> dstPos { static_cast<uint32_t>((offset + dst.x) * pixelRatio),
|
||||
static_cast<uint32_t>((offset + dst.y) * pixelRatio),
|
||||
static_cast<uint32_t>(dst.originalW * pixelRatio),
|
||||
static_cast<uint32_t>(dst.originalH * pixelRatio) };
|
||||
const vec2<uint32_t> dstSize{ pixelWidth, pixelHeight };
|
||||
const Rect<uint32_t> dstPos{ static_cast<uint32_t>((offset + dst.x) * pixelRatio),
|
||||
static_cast<uint32_t>((offset + dst.y) * pixelRatio),
|
||||
static_cast<uint32_t>(dst.originalW * pixelRatio),
|
||||
static_cast<uint32_t>(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<const char*>(data.get()),
|
||||
// width * pixelRatio, height * pixelRatio, width * pixelRatio,
|
||||
// height * pixelRatio);
|
||||
// pixelWidth, pixelHeight, pixelWidth, pixelHeight);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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 SpriteImage>&, const Rect<dimension>&);
|
||||
inline Holder(Holder&&);
|
||||
@@ -87,7 +88,6 @@ private:
|
||||
|
||||
std::recursive_mutex mtx;
|
||||
SpriteStore& store;
|
||||
const float pixelRatio;
|
||||
BinPack<dimension> bin;
|
||||
std::map<Key, Holder> images;
|
||||
std::set<std::string> uninitialized;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user