#include #include #include #include #include #include using namespace mbgl; Raster::Raster(gl::TexturePool& texturePool_) : texturePool(texturePool_) {} Raster::~Raster() { if (textured) { texturePool.releaseTextureID(textureID); textureID = 0; } } bool Raster::isLoaded() const { std::lock_guard lock(mtx); return loaded; } void Raster::load(PremultipliedImage image) { assert(image.data.get()); img = std::move(image); width = GLsizei(img.width); height = GLsizei(img.height); std::lock_guard lock(mtx); loaded = true; } void Raster::bind(bool linear, gl::GLObjectStore& glObjectStore) { if (!width || !height) { Log::Error(Event::OpenGL, "trying to bind texture without dimension"); return; } if (img.data && !textured) { upload(glObjectStore); } else if (textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, textureID)); } GLint 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; } } void Raster::upload(gl::GLObjectStore& glObjectStore) { if (img.data && !textured) { textureID = texturePool.getTextureID(glObjectStore); MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, textureID)); #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.data.get())); img.data.reset(); textured = true; } }