#include #include #include #include #include #include #include using namespace mbgl; Raster::Raster(TexturePool& texturePool_) : texturePool(texturePool_) {} Raster::~Raster() { if (textured) { texturePool.removeTextureID(texture); } } bool Raster::isLoaded() const { std::lock_guard lock(mtx); return loaded; } bool Raster::load(const std::string &data) { img = util::make_unique(data); width = img->getWidth(); height = img->getHeight(); std::lock_guard lock(mtx); if (img->getData()) { loaded = true; } return loaded; } void Raster::bind(bool linear) { if (!width || !height) { fprintf(stderr, "trying to bind texture without dimension\n"); return; } if (img && !textured) { texture = texturePool.getTextureID(); MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); #ifndef GL_ES_VERSION_2_0 MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); #endif 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)); MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData())); img.reset(); textured = true; } else if (textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } GLuint new_filter = linear ? GL_LINEAR : GL_NEAREST; if (new_filter != this->filter) { MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, new_filter)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, new_filter)); filter = new_filter; } } // overload ::bind for prerendered raster textures void Raster::bind(const GLuint custom_texture) { if (img && !textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture)); 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)); MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData())); img.reset(); textured = true; } else if (textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture)); } GLuint new_filter = GL_LINEAR; if (new_filter != this->filter) { MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, new_filter)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, new_filter)); filter = new_filter; } } void Raster::beginFadeInTransition() { std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); fade_transition = std::make_shared>(opacity, 1.0, opacity, start, std::chrono::milliseconds(250)); } bool Raster::needsTransition() const { return fade_transition != nullptr; } void Raster::updateTransitions(std::chrono::steady_clock::time_point now) { if (fade_transition->update(now) == util::transition::complete) { fade_transition = nullptr; } }