Make pixelRatio constant across a Map object lifetime

also moves framebuffer size out of TransformState into its own object
This commit is contained in:
Konstantin Käfer
2015-07-01 12:39:21 +02:00
parent 3a07cb2f1e
commit 675ddddee7
40 changed files with 308 additions and 214 deletions

View File

@@ -15,10 +15,11 @@ namespace mbgl {
Map::Map(View& view_, FileSource& fileSource, MapMode mode)
: view(view_),
transform(std::make_unique<Transform>(view)),
data(std::make_unique<MapData>(mode)),
data(std::make_unique<MapData>(mode, view.getPixelRatio())),
context(std::make_unique<util::Thread<MapContext>>(util::ThreadContext{"Map", util::ThreadType::Map, util::ThreadPriority::Regular}, view, fileSource, *data))
{
view.initialize(this);
update(Update::Dimensions);
}
Map::~Map() {
@@ -43,7 +44,8 @@ void Map::resume() {
}
void Map::renderStill(StillImageCallback callback) {
context->invoke(&MapContext::renderStill, transform->getState(), callback);
context->invoke(&MapContext::renderStill, transform->getState(),
FrameData{ view.getFramebufferSize() }, callback);
}
void Map::renderSync() {
@@ -53,8 +55,8 @@ void Map::renderSync() {
view.notifyMapChange(MapChangeWillStartRenderingFrame);
MapContext::RenderResult result =
context->invokeSync<MapContext::RenderResult>(&MapContext::renderSync, transform->getState());
MapContext::RenderResult result = context->invokeSync<MapContext::RenderResult>(
&MapContext::renderSync, transform->getState(), FrameData{ view.getFramebufferSize() });
view.notifyMapChange(result.fullyLoaded ?
MapChangeDidFinishRenderingFrameFullyRendered :
@@ -75,6 +77,10 @@ void Map::renderSync() {
}
void Map::update(Update update_) {
if (update_ == Update::Dimensions) {
transform->resize(view.getSize());
}
context->invoke(&MapContext::triggerUpdate, transform->getState(), update_);
}
@@ -96,15 +102,6 @@ std::string Map::getStyleJSON() const {
return context->invokeSync<std::string>(&MapContext::getStyleJSON);
}
#pragma mark - Size
void Map::resize(uint16_t width, uint16_t height, float ratio) {
if (transform->resize(width, height, ratio, width * ratio, height * ratio)) {
context->invoke(&MapContext::resize, width, height, ratio);
update();
}
}
#pragma mark - Transitions
void Map::cancelTransitions() {
@@ -185,7 +182,7 @@ void Map::fitBounds(AnnotationSegment segment, EdgeInsets padding, Duration dura
if (segment.empty()) {
return;
}
// Calculate the bounds of the possibly rotated shape with respect to the viewport.
vec2<> nePixel = {-INFINITY, -INFINITY};
vec2<> swPixel = {INFINITY, INFINITY};

View File

@@ -79,10 +79,6 @@ void MapContext::pause() {
view.activate();
}
void MapContext::resize(uint16_t width, uint16_t height, float ratio) {
view.resize(width, height, ratio);
}
void MapContext::triggerUpdate(const TransformState& state, const Update u) {
transformState = state;
updated |= static_cast<UpdateType>(u);
@@ -266,14 +262,14 @@ void MapContext::update() {
if (data.mode == MapMode::Continuous) {
view.invalidate();
} else if (callback && style->isLoaded()) {
renderSync(transformState);
renderSync(transformState, frameData);
}
}
updated = static_cast<UpdateType>(Update::Nothing);
}
void MapContext::renderStill(const TransformState& state, StillImageCallback fn) {
void MapContext::renderStill(const TransformState& state, const FrameData& frame, StillImageCallback fn) {
if (!fn) {
Log::Error(Event::General, "StillImageCallback not set");
return;
@@ -301,12 +297,13 @@ void MapContext::renderStill(const TransformState& state, StillImageCallback fn)
callback = fn;
transformState = state;
frameData = frame;
updated |= static_cast<UpdateType>(Update::RenderStill);
asyncUpdate->send();
}
MapContext::RenderResult MapContext::renderSync(const TransformState& state) {
MapContext::RenderResult MapContext::renderSync(const TransformState& state, const FrameData& frame) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
// Style was not loaded yet.
@@ -320,12 +317,12 @@ MapContext::RenderResult MapContext::renderSync(const TransformState& state) {
glObjectStore.performCleanup();
if (!painter) {
painter = std::make_unique<Painter>();
painter = std::make_unique<Painter>(data.pixelRatio);
painter->setup();
}
painter->setDebug(data.getDebug());
painter->render(*style, transformState, data.getAnimationTime());
painter->render(*style, transformState, frame, data.getAnimationTime());
if (data.mode == MapMode::Still) {
callback(nullptr, view.readStillImage());

View File

@@ -26,6 +26,11 @@ class StillImage;
struct LatLng;
struct LatLngBounds;
struct FrameData {
std::array<uint16_t, 2> framebufferSize;
};
class MapContext : public Style::Observer {
public:
MapContext(View&, FileSource&, MapData&);
@@ -38,13 +43,11 @@ public:
void pause();
void resize(uint16_t width, uint16_t height, float ratio);
using StillImageCallback = std::function<void(std::exception_ptr, std::unique_ptr<const StillImage>)>;
void triggerUpdate(const TransformState&, Update = Update::Nothing);
void renderStill(const TransformState&, StillImageCallback callback);
RenderResult renderSync(const TransformState&);
void renderStill(const TransformState&, const FrameData&, StillImageCallback callback);
RenderResult renderSync(const TransformState&, const FrameData&);
void setStyleURL(const std::string&);
void setStyleJSON(const std::string& json, const std::string& base);
@@ -95,6 +98,7 @@ private:
StillImageCallback callback;
size_t sourceCacheSize;
TransformState transformState;
FrameData frameData;
};
}

View File

@@ -20,7 +20,8 @@ class MapData {
using Lock = std::lock_guard<std::mutex>;
public:
inline MapData(MapMode mode_) : mode(mode_) {
inline MapData(MapMode mode_, const float pixelRatio_) : mode(mode_), pixelRatio(pixelRatio_) {
assert(pixelRatio > 0);
setAnimationTime(TimePoint::min());
setDefaultTransitionDuration(Duration::zero());
}
@@ -77,7 +78,7 @@ public:
}
inline void setDefaultTransitionDuration(Duration duration) {
defaultTransitionDuration = duration;
};
}
util::exclusive<AnnotationManager> getAnnotationManager() {
return util::exclusive<AnnotationManager>(
@@ -87,6 +88,7 @@ public:
public:
const MapMode mode;
const float pixelRatio;
private:
mutable std::mutex annotationManagerMutex;

View File

@@ -292,11 +292,11 @@ TileData::State Source::addTile(MapData& data,
if (info.type == SourceType::Vector) {
auto tileData = std::make_shared<VectorTileData>(normalized_id, style, info,
transformState.getAngle(), data.getCollisionDebug());
tileData->request(transformState.getPixelRatio(), callback);
tileData->request(data.pixelRatio, callback);
new_tile.data = tileData;
} else if (info.type == SourceType::Raster) {
auto tileData = std::make_shared<RasterTileData>(normalized_id, texturePool, info, style.workers);
tileData->request(transformState.getPixelRatio(), callback);
tileData->request(data.pixelRatio, callback);
new_tile.data = tileData;
} else if (info.type == SourceType::Annotations) {
new_tile.data = std::make_shared<LiveTileData>(normalized_id,

View File

@@ -34,18 +34,13 @@ Transform::Transform(View &view_)
#pragma mark - Map View
bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio,
const uint16_t fb_w, const uint16_t fb_h) {
if (state.width != w || state.height != h || state.pixelRatio != ratio ||
state.framebuffer[0] != fb_w || state.framebuffer[1] != fb_h) {
bool Transform::resize(const std::array<uint16_t, 2> size) {
if (state.width != size[0] || state.height != size[1]) {
view.notifyMapChange(MapChangeRegionWillChange);
state.width = w;
state.height = h;
state.pixelRatio = ratio;
state.framebuffer[0] = fb_w;
state.framebuffer[1] = fb_h;
state.width = size[0];
state.height = size[1];
state.constrain(state.scale, state.y);
view.notifyMapChange(MapChangeRegionDidChange);

View File

@@ -20,9 +20,7 @@ public:
Transform(View&);
// Map view
// Note: width * ratio does not necessarily equal fb_width
bool resize(uint16_t width, uint16_t height, float ratio,
uint16_t fb_width, uint16_t fb_height);
bool resize(std::array<uint16_t, 2> size);
// Position
void moveBy(double dx, double dy, Duration = Duration::zero());

View File

@@ -74,23 +74,6 @@ uint16_t TransformState::getHeight() const {
return height;
}
uint16_t TransformState::getFramebufferWidth() const {
return framebuffer[0];
}
uint16_t TransformState::getFramebufferHeight() const {
return framebuffer[1];
}
const std::array<uint16_t, 2> TransformState::getFramebufferDimensions() const {
return framebuffer;
}
float TransformState::getPixelRatio() const {
return pixelRatio;
}
#pragma mark - Position
const LatLng TransformState::getLatLng() const {

View File

@@ -27,10 +27,6 @@ public:
bool hasSize() const;
uint16_t getWidth() const;
uint16_t getHeight() const;
uint16_t getFramebufferWidth() const;
uint16_t getFramebufferHeight() const;
const std::array<uint16_t, 2> getFramebufferDimensions() const;
float getPixelRatio() const;
float worldSize() const;
float lngX(float lon) const;
@@ -73,12 +69,7 @@ private:
// logical dimensions
uint16_t width = 0, height = 0;
// physical (framebuffer) dimensions
std::array<uint16_t, 2> framebuffer = {{ 0, 0 }};
// map scale factor
float pixelRatio = 0;
private:
// animation state
bool rotating = false;
bool scaling = false;

View File

@@ -11,10 +11,6 @@ void View::initialize(Map *map_) {
map = map_;
}
void View::resize(uint16_t, uint16_t, float) {
// no-op
}
std::unique_ptr<StillImage> View::readStillImage() {
return nullptr;
}

View File

@@ -2,6 +2,7 @@
#include <mbgl/map/source.hpp>
#include <mbgl/map/tile.hpp>
#include <mbgl/map/map_context.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/gl/debugging.hpp>
@@ -40,7 +41,7 @@
using namespace mbgl;
Painter::Painter() {
Painter::Painter(const float pixelRatio_) : pixelRatio(pixelRatio_) {
}
Painter::~Painter() {
@@ -104,8 +105,8 @@ void Painter::setupShaders() {
}
void Painter::resize() {
if (gl_viewport != state.getFramebufferDimensions()) {
gl_viewport = state.getFramebufferDimensions();
if (gl_viewport != frame.framebufferSize) {
gl_viewport = frame.framebufferSize;
assert(gl_viewport[0] > 0 && gl_viewport[1] > 0);
MBGL_CHECK_ERROR(glViewport(0, 0, gl_viewport[0], gl_viewport[1]));
}
@@ -164,8 +165,9 @@ void Painter::prepareTile(const Tile& tile) {
config.stencilFunc = { GL_EQUAL, ref, mask };
}
void Painter::render(const Style& style, TransformState state_, TimePoint time) {
void Painter::render(const Style& style, TransformState state_, const FrameData& frame_, TimePoint time) {
state = state_;
frame = frame_;
glyphAtlas = style.glyphAtlas.get();
spriteAtlas = style.spriteAtlas.get();

View File

@@ -2,6 +2,7 @@
#define MBGL_RENDERER_PAINTER
#include <mbgl/map/transform_state.hpp>
#include <mbgl/map/map_context.hpp>
#include <mbgl/renderer/frame_history.hpp>
#include <mbgl/renderer/bucket.hpp>
@@ -31,6 +32,7 @@ class SpriteAtlas;
class GlyphAtlas;
class LineAtlas;
class Source;
struct FrameData;
class DebugBucket;
@@ -79,7 +81,7 @@ struct RenderItem {
class Painter : private util::noncopyable {
public:
Painter();
Painter(float pixelRatio);
~Painter();
void setup();
@@ -93,6 +95,7 @@ public:
void render(const Style& style,
TransformState state,
const FrameData& frame,
TimePoint time);
// Renders debug information for a tile.
@@ -118,6 +121,7 @@ public:
void createPrerendered(RasterBucket& bucket, const StyleLayer &layer_desc, const TileID& id);
// Adjusts the dimensions of the OpenGL viewport
void resize();
// Changes whether debug information is drawn onto the map
@@ -187,7 +191,10 @@ public:
}();
private:
const float pixelRatio;
TransformState state;
FrameData frame;
bool debug = false;
int indent = 0;

View File

@@ -28,7 +28,7 @@ void Painter::renderDebugText(DebugBucket& bucket, const mat4 &matrix) {
// Draw white outline
plainShader->u_color = {{ 1.0f, 1.0f, 1.0f, 1.0f }};
lineWidth(4.0f * state.getPixelRatio());
lineWidth(4.0f * pixelRatio);
bucket.drawLines(*plainShader);
#ifndef GL_ES_VERSION_2_0
@@ -39,7 +39,7 @@ void Painter::renderDebugText(DebugBucket& bucket, const mat4 &matrix) {
// Draw black text.
plainShader->u_color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
lineWidth(2.0f * state.getPixelRatio());
lineWidth(2.0f * pixelRatio);
bucket.drawLines(*plainShader);
config.depthTest = true;
@@ -60,6 +60,6 @@ void Painter::renderDebugFrame(const mat4 &matrix) {
// draw tile outline
tileBorderArray.bind(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0));
plainShader->u_color = {{ 1.0f, 0.0f, 0.0f, 1.0f }};
lineWidth(4.0f * state.getPixelRatio());
lineWidth(4.0f * pixelRatio);
MBGL_CHECK_ERROR(glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)tileBorderBuffer.index()));
}

View File

@@ -52,8 +52,8 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
// Draw the entire line
outlineShader->u_world = {{
static_cast<float>(state.getFramebufferWidth()),
static_cast<float>(state.getFramebufferHeight())
static_cast<float>(frame.framebufferSize[0]),
static_cast<float>(frame.framebufferSize[1])
}};
config.depthRange = { strata, 1.0f };
bucket.drawVertices(*outlineShader);
@@ -128,8 +128,8 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
// Draw the entire line
outlineShader->u_world = {{
static_cast<float>(state.getFramebufferWidth()),
static_cast<float>(state.getFramebufferHeight())
static_cast<float>(frame.framebufferSize[0]),
static_cast<float>(frame.framebufferSize[1])
}};
config.depthRange = { strata + strata_epsilon + strata_epsilon, 1.0f };

View File

@@ -26,7 +26,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float antialiasing = 1 / state.getPixelRatio();
float antialiasing = 1.0 / pixelRatio;
float blur = properties.blur + antialiasing;
float edgeWidth = properties.width / 2.0;
@@ -50,7 +50,6 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
color[2] *= properties.opacity;
color[3] *= properties.opacity;
float ratio = state.getPixelRatio();
mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor);
config.depthRange = { strata, 1.0f };
@@ -62,7 +61,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linesdfShader->u_matrix = vtxMatrix;
linesdfShader->u_exmatrix = extrudeMatrix;
linesdfShader->u_linewidth = {{ outset, inset }};
linesdfShader->u_ratio = ratio;
linesdfShader->u_ratio = pixelRatio;
linesdfShader->u_blur = blur;
linesdfShader->u_color = color;
@@ -81,7 +80,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linesdfShader->u_patternscale_b = {{ scaleXB, scaleYB }};
linesdfShader->u_tex_y_b = posB.y;
linesdfShader->u_image = 0;
linesdfShader->u_sdfgamma = lineAtlas->width / (properties.dash_line_width * std::min(posA.width, posB.width) * 256.0 * state.getPixelRatio()) / 2;
linesdfShader->u_sdfgamma = lineAtlas->width / (properties.dash_line_width * std::min(posA.width, posB.width) * 256.0 * pixelRatio) / 2;
linesdfShader->u_mix = properties.dash_array.t;
bucket.drawLineSDF(*linesdfShader);
@@ -97,7 +96,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linepatternShader->u_matrix = vtxMatrix;
linepatternShader->u_exmatrix = extrudeMatrix;
linepatternShader->u_linewidth = {{ outset, inset }};
linepatternShader->u_ratio = ratio;
linepatternShader->u_ratio = pixelRatio;
linepatternShader->u_blur = blur;
linepatternShader->u_pattern_size_a = {{imagePosA.size[0] * factor * properties.image.fromScale, imagePosA.size[1]}};
@@ -121,7 +120,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
lineShader->u_matrix = vtxMatrix;
lineShader->u_exmatrix = extrudeMatrix;
lineShader->u_linewidth = {{ outset, inset }};
lineShader->u_ratio = ratio;
lineShader->u_ratio = pixelRatio;
lineShader->u_blur = blur;
lineShader->u_color = color;

View File

@@ -60,7 +60,7 @@ void Painter::renderSDF(SymbolBucket &bucket,
// The default gamma value has to be adjust for the current pixelratio so that we're not
// drawing blurry font on retina screens.
const float gamma = 0.105 * sdfFontSize / fontSize / state.getPixelRatio();
const float gamma = 0.105 * sdfFontSize / fontSize / pixelRatio;
const float sdfPx = 8.0f;
const float blurOffset = 1.19f;

View File

@@ -1,5 +1,6 @@
#include <mbgl/style/style.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/map/map_data.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/style/style_layer.hpp>
@@ -67,12 +68,11 @@ Style::~Style() {
void Style::update(MapData& data,
const TransformState& transform,
TexturePool& texturePool) {
const float pixelRatio = transform.getPixelRatio();
if (!sprite || !sprite->hasPixelRatio(pixelRatio)) {
sprite = std::make_unique<Sprite>(spriteURL, pixelRatio);
if (!sprite || !sprite->hasPixelRatio(data.pixelRatio)) {
sprite = std::make_unique<Sprite>(spriteURL, data.pixelRatio);
sprite->setObserver(this);
spriteAtlas->resize(pixelRatio);
spriteAtlas->resize(data.pixelRatio);
spriteAtlas->setSprite(sprite);
}