[core] convert FrameHistory to use managed texture handling

This commit is contained in:
Konstantin Käfer
2016-10-27 13:01:31 -07:00
parent ed155460d3
commit e4e9b3a2f0
2 changed files with 21 additions and 62 deletions

View File

@@ -3,11 +3,13 @@
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/gl.hpp>
#include <cassert>
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<float> 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

View File

@@ -3,8 +3,9 @@
#include <array>
#include <mbgl/platform/platform.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/gl/texture.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/optional.hpp>
namespace mbgl {
@@ -23,20 +24,17 @@ public:
void upload(gl::Context&, uint32_t);
private:
const int width = 256;
const int height = 1;
std::array<TimePoint, 256> changeTimes;
std::array<uint8_t, 256> changeOpacities;
std::array<uint8_t, 256> 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<gl::UniqueTexture> texture;
mbgl::optional<gl::Texture> texture;
};
} // namespace mbgl