diff --git a/src/mbgl/renderer/frame_history.cpp b/src/mbgl/renderer/frame_history.cpp index daf24c8c3..d77c89a1b 100644 --- a/src/mbgl/renderer/frame_history.cpp +++ b/src/mbgl/renderer/frame_history.cpp @@ -3,11 +3,13 @@ #include #include +#include + namespace mbgl { FrameHistory::FrameHistory() { changeOpacities.fill(0); - opacities.fill(0); + std::fill(opacities.data.get(), opacities.data.get() + opacities.bytes(), 0); } void FrameHistory::record(const TimePoint& now, float zoom, const Duration& duration) { @@ -18,7 +20,7 @@ void FrameHistory::record(const TimePoint& now, float zoom, const Duration& dura changeTimes.fill(now); for (int16_t z = 0; z <= zoomIndex; z++) { - opacities[z] = 255u; + opacities.data[z] = 255u; } firstFrame = false; } @@ -26,12 +28,12 @@ void FrameHistory::record(const TimePoint& now, float zoom, const Duration& dura if (zoomIndex < previousZoomIndex) { for (int16_t z = zoomIndex + 1; z <= previousZoomIndex; z++) { changeTimes[z] = now; - changeOpacities[z] = opacities[z]; + changeOpacities[z] = opacities.data[z]; } } else { for (int16_t z = zoomIndex; z > previousZoomIndex; z--) { changeTimes[z] = now; - changeOpacities[z] = opacities[z]; + changeOpacities[z] = opacities.data[z]; } } @@ -39,13 +41,13 @@ void FrameHistory::record(const TimePoint& now, float zoom, const Duration& dura std::chrono::duration timeDiff = now - changeTimes[z]; int32_t opacityChange = (duration == Milliseconds(0) ? 1 : (timeDiff / duration)) * 255; if (z <= zoomIndex) { - opacities[z] = util::min(255, changeOpacities[z] + opacityChange); + opacities.data[z] = util::min(255, changeOpacities[z] + opacityChange); } else { - opacities[z] = util::max(0, changeOpacities[z] - opacityChange); + opacities.data[z] = util::max(0, changeOpacities[z] - opacityChange); } } - changed = true; + dirty = true; if (zoomIndex != previousZoomIndex) { previousZoomIndex = zoomIndex; @@ -60,58 +62,17 @@ bool FrameHistory::needsAnimation(const Duration& duration) const { } void FrameHistory::upload(gl::Context& context, uint32_t unit) { - - if (changed) { - const bool first = !texture; - bind(context, unit); - - if (first) { - MBGL_CHECK_ERROR(glTexImage2D( - GL_TEXTURE_2D, // GLenum target - 0, // GLint level - GL_ALPHA, // GLint internalformat - width, // GLsizei width - height, // GLsizei height - 0, // GLint border - GL_ALPHA, // GLenum format - GL_UNSIGNED_BYTE, // GLenum type - opacities.data() - )); - } else { - MBGL_CHECK_ERROR(glTexSubImage2D( - GL_TEXTURE_2D, // GLenum target - 0, // GLint level - 0, // GLint xoffset - 0, // GLint yoffset - width, // GLsizei width - height, // GLsizei height - GL_ALPHA, // GLenum format - GL_UNSIGNED_BYTE, // GLenum type - opacities.data() - )); - } - - changed = false; - + if (!texture) { + texture = context.createTexture(opacities, unit); + } else if (dirty) { + context.updateTexture(*texture, opacities, unit); } + dirty = false; } void FrameHistory::bind(gl::Context& context, uint32_t unit) { - if (!texture) { - texture = context.createTexture(); - context.activeTexture = unit; - context.texture[unit] = *texture; -#if not MBGL_USE_GLES2 - 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(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); - } else if (context.texture[unit] != *texture) { - context.activeTexture = unit; - context.texture[unit] = *texture; - } + upload(context, unit); + context.bindTexture(*texture, unit); } } // namespace mbgl diff --git a/src/mbgl/renderer/frame_history.hpp b/src/mbgl/renderer/frame_history.hpp index 063930af2..d8fd06d27 100644 --- a/src/mbgl/renderer/frame_history.hpp +++ b/src/mbgl/renderer/frame_history.hpp @@ -3,8 +3,9 @@ #include #include -#include +#include #include +#include #include namespace mbgl { @@ -23,20 +24,17 @@ public: void upload(gl::Context&, uint32_t); private: - const int width = 256; - const int height = 1; - std::array changeTimes; std::array changeOpacities; - std::array opacities; + const AlphaImage opacities{ { 256, 1 } }; int16_t previousZoomIndex = 0; TimePoint previousTime = TimePoint::min(); TimePoint time = TimePoint::min(); bool firstFrame = true; - bool changed = true; + bool dirty = true; - mbgl::optional texture; + mbgl::optional texture; }; } // namespace mbgl