Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Move atlas ownership to Style
Browse files Browse the repository at this point in the history
This follows gl-js and just makes sense -- whenever the style changes
the atlases should be blown away.

Refs #957
  • Loading branch information
jfirebaugh committed Mar 13, 2015
1 parent c36522e commit bffee07
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 161 deletions.
14 changes: 0 additions & 14 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
namespace mbgl {

class Painter;
class GlyphStore;
class LayerDescription;
class Sprite;
class Style;
Expand All @@ -33,9 +32,6 @@ class StyleSource;
class TexturePool;
class FileSource;
class View;
class GlyphAtlas;
class SpriteAtlas;
class LineAtlas;
class Environment;

class Map : private util::noncopyable {
Expand Down Expand Up @@ -153,7 +149,6 @@ class Map : private util::noncopyable {
void resize(uint16_t width, uint16_t height, float ratio = 1);
void resize(uint16_t width, uint16_t height, float ratio, uint16_t fbWidth, uint16_t fbHeight);

util::ptr<Sprite> getSprite();
uv::worker& getWorker();

// Checks if render thread needs to pause
Expand All @@ -163,8 +158,6 @@ class Map : private util::noncopyable {
void setup();

void updateTiles();
void updateSources();
void updateSources(const util::ptr<StyleLayerGroup> &group);

// Prepares a map render by updating the tiles we need for the current view, as well as updating
// the stylesheet.
Expand Down Expand Up @@ -213,11 +206,6 @@ class Map : private util::noncopyable {
FileSource& fileSource;

util::ptr<Style> style;
const std::unique_ptr<GlyphAtlas> glyphAtlas;
util::ptr<GlyphStore> glyphStore;
const std::unique_ptr<SpriteAtlas> spriteAtlas;
util::ptr<Sprite> sprite;
const std::unique_ptr<LineAtlas> lineAtlas;
util::ptr<TexturePool> texturePool;

const std::unique_ptr<Painter> painter;
Expand All @@ -231,8 +219,6 @@ class Map : private util::noncopyable {

bool debug = false;
std::chrono::steady_clock::time_point animationTime = std::chrono::steady_clock::time_point::min();

std::set<util::ptr<StyleSource>> activeSources;
};

}
Expand Down
101 changes: 10 additions & 91 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <mbgl/map/environment.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/renderer/painter.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/util/transition.hpp>
Expand All @@ -13,19 +12,14 @@
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/texture_pool.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/exception.hpp>

#include <algorithm>
Expand Down Expand Up @@ -65,12 +59,8 @@ Map::Map(View& view_, FileSource& fileSource_)
mapThread(mainThread),
transform(view_),
fileSource(fileSource_),
glyphAtlas(util::make_unique<GlyphAtlas>(1024, 1024)),
glyphStore(std::make_shared<GlyphStore>(*env)),
spriteAtlas(util::make_unique<SpriteAtlas>(512, 512)),
lineAtlas(util::make_unique<LineAtlas>(512, 512)),
texturePool(std::make_shared<TexturePool>()),
painter(util::make_unique<Painter>(*spriteAtlas, *glyphAtlas, *lineAtlas))
painter(util::make_unique<Painter>())
{
view.initialize(this);
}
Expand All @@ -81,9 +71,6 @@ Map::~Map() {
}

// Explicitly reset all pointers.
activeSources.clear();
sprite.reset();
glyphStore.reset();
style.reset();
texturePool.reset();
workers.reset();
Expand Down Expand Up @@ -114,7 +101,6 @@ void Map::start(bool startPaused) {
// Remove all of these to make sure they are destructed in the correct thread.
style.reset();
workers.reset();
activeSources.clear();

terminating = true;

Expand Down Expand Up @@ -351,36 +337,23 @@ void Map::setStyleURL(const std::string &url) {
void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
// TODO: Make threadsafe.
styleJSON.swap(newStyleJSON);
sprite.reset();

if (!style) {
style = std::make_shared<Style>();
style = std::make_shared<Style>(*env);
}

style->base = base;
style->loadJSON((const uint8_t *)styleJSON.c_str());
style->cascadeClasses(classes);
style->setDefaultTransitionDuration(defaultTransitionDuration);

const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken());
glyphStore->setURL(glyphURL);

triggerUpdate();
}

std::string Map::getStyleJSON() const {
return styleJSON;
}

util::ptr<Sprite> Map::getSprite() {
const float pixelRatio = state.getPixelRatio();
const std::string &sprite_url = style->getSpriteURL();
if (!sprite || sprite->pixelRatio != pixelRatio) {
sprite = Sprite::Create(sprite_url, pixelRatio, *env);
}

return sprite;
}


#pragma mark - Size

Expand Down Expand Up @@ -604,64 +577,11 @@ std::chrono::steady_clock::duration Map::getDefaultTransitionDuration() {
return defaultTransitionDuration;
}

void Map::updateSources() {
assert(std::this_thread::get_id() == mapThread);

// First, disable all existing sources.
for (const auto& source : activeSources) {
source->enabled = false;
}

// Then, reenable all of those that we actually use when drawing this layer.
updateSources(style->layers);

// Then, construct or destroy the actual source object, depending on enabled state.
for (const auto& source : activeSources) {
if (source->enabled) {
if (!source->source) {
source->source = std::make_shared<Source>(source->info);
source->source->load(*this, *env);
}
} else {
source->source.reset();
}
}

// Finally, remove all sources that are disabled.
util::erase_if(activeSources, [](util::ptr<StyleSource> source){
return !source->enabled;
});
}

void Map::updateSources(const util::ptr<StyleLayerGroup> &group) {
assert(std::this_thread::get_id() == mapThread);
if (!group) {
return;
}
for (const util::ptr<StyleLayer> &layer : group->layers) {
if (!layer) continue;
if (layer->bucket && layer->bucket->style_source) {
(*activeSources.emplace(layer->bucket->style_source).first)->enabled = true;
}
}
}

void Map::updateTiles() {
assert(std::this_thread::get_id() == mapThread);
for (const auto &source : activeSources) {
source->source->update(*this, *env, getWorker(), style, *glyphAtlas, *glyphStore,
*spriteAtlas, getSprite(), *texturePool, [this]() {
assert(std::this_thread::get_id() == mapThread);
triggerUpdate();
});
}
}

void Map::prepare() {
assert(std::this_thread::get_id() == mapThread);

if (!style) {
style = std::make_shared<Style>();
style = std::make_shared<Style>(*env);

env->request({ Resource::Kind::JSON, styleURL}, [&](const Response &res) {
if (res.status == Response::Successful) {
Expand All @@ -688,14 +608,13 @@ void Map::prepare() {
state = transform.currentState();

animationTime = std::chrono::steady_clock::now();
updateSources();
style->updateProperties(state.getNormalizedZoom(), animationTime);

// Allow the sprite atlas to potentially pull new sprite images if needed.
spriteAtlas->resize(state.getPixelRatio());
spriteAtlas->setSprite(getSprite());
style->updateSources(*this, *env, *workers, *texturePool, [this]() {
assert(std::this_thread::get_id() == mapThread);
triggerUpdate();
});

updateTiles();
style->updateProperties(state.getNormalizedZoom(), animationTime);

if (mode == Mode::Continuous) {
view.invalidate();
Expand All @@ -705,7 +624,7 @@ void Map::prepare() {
void Map::render() {
assert(std::this_thread::get_id() == mapThread);
assert(painter);
painter->render(*style, activeSources,
painter->render(*style, style->activeSources,
state, animationTime);
// Schedule another rerender when we definitely need a next frame.
if (transform.needsTransition() || style->hasTransitions()) {
Expand Down
16 changes: 8 additions & 8 deletions src/mbgl/renderer/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ using namespace mbgl;

#define BUFFER_OFFSET(i) ((char *)nullptr + (i))

Painter::Painter(SpriteAtlas& spriteAtlas_, GlyphAtlas& glyphAtlas_, LineAtlas& lineAtlas_)
: spriteAtlas(spriteAtlas_)
, glyphAtlas(glyphAtlas_)
, lineAtlas(lineAtlas_)
{
Painter::Painter() {
}

Painter::~Painter() {
Expand Down Expand Up @@ -219,6 +215,10 @@ void Painter::render(const Style& style, const std::set<util::ptr<StyleSource>>&
TransformState state_, std::chrono::steady_clock::time_point time) {
state = state_;

glyphAtlas = style.glyphAtlas.get();
spriteAtlas = style.spriteAtlas.get();
lineAtlas = style.lineAtlas.get();

clear();
resize();
changeMatrix();
Expand Down Expand Up @@ -377,8 +377,8 @@ void Painter::renderBackground(const StyleLayer &layer_desc) {
if ((properties.opacity >= 1.0f) != (pass == RenderPass::Opaque))
return;

SpriteAtlasPosition imagePosA = spriteAtlas.getPosition(properties.image.from, true);
SpriteAtlasPosition imagePosB = spriteAtlas.getPosition(properties.image.to, true);
SpriteAtlasPosition imagePosA = spriteAtlas->getPosition(properties.image.from, true);
SpriteAtlasPosition imagePosB = spriteAtlas->getPosition(properties.image.to, true);
float zoomFraction = state.getZoomFraction();

useProgram(patternShader->program);
Expand Down Expand Up @@ -427,7 +427,7 @@ void Painter::renderBackground(const StyleLayer &layer_desc) {

backgroundBuffer.bind();
patternShader->bind(0);
spriteAtlas.bind(true);
spriteAtlas->bind(true);
} else {
Color color = properties.color;
color[0] *= properties.opacity;
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/renderer/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class RasterTileData;

class Painter : private util::noncopyable {
public:
Painter(SpriteAtlas&, GlyphAtlas&, LineAtlas&);
Painter();
~Painter();

void setup();
Expand Down Expand Up @@ -195,9 +195,9 @@ class Painter : private util::noncopyable {
public:
FrameHistory frameHistory;

SpriteAtlas& spriteAtlas;
GlyphAtlas& glyphAtlas;
LineAtlas& lineAtlas;
SpriteAtlas* spriteAtlas;
GlyphAtlas* glyphAtlas;
LineAtlas* lineAtlas;

std::unique_ptr<PlainShader> plainShader;
std::unique_ptr<OutlineShader> outlineShader;
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/renderer/painter_fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
if (pattern) {
// Image fill.
if (pass == RenderPass::Translucent) {
const SpriteAtlasPosition posA = spriteAtlas.getPosition(properties.image.from, true);
const SpriteAtlasPosition posB = spriteAtlas.getPosition(properties.image.to, true);
const SpriteAtlasPosition posA = spriteAtlas->getPosition(properties.image.from, true);
const SpriteAtlasPosition posB = spriteAtlas->getPosition(properties.image.to, true);
float factor = 8.0 / std::pow(2, state.getIntegerZoom() - id.z);

mat3 patternMatrixA;
Expand All @@ -88,7 +88,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
patternShader->u_patternmatrix_b = patternMatrixB;

MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0));
spriteAtlas.bind(true);
spriteAtlas->bind(true);

// Draw the actual triangles into the color & stencil buffer.
depthRange(strata, 1.0f);
Expand Down
14 changes: 7 additions & 7 deletions src/mbgl/renderer/painter_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linesdfShader->u_blur = blur;
linesdfShader->u_color = color;

LinePatternPos posA = lineAtlas.getDashPosition(properties.dash_array.from, layout.cap == CapType::Round);
LinePatternPos posB = lineAtlas.getDashPosition(properties.dash_array.to, layout.cap == CapType::Round);
lineAtlas.bind();
LinePatternPos posA = lineAtlas->getDashPosition(properties.dash_array.from, layout.cap == CapType::Round);
LinePatternPos posB = lineAtlas->getDashPosition(properties.dash_array.to, layout.cap == CapType::Round);
lineAtlas->bind();

float patternratio = std::pow(2.0, std::floor(std::log2(state.getScale())) - id.z) / 8.0;
float scaleXA = patternratio / posA.width / properties.dash_line_width / properties.dash_array.fromScale;
Expand All @@ -98,14 +98,14 @@ 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 * state.getPixelRatio()) / 2;
linesdfShader->u_mix = properties.dash_array.t;

bucket.drawLineSDF(*linesdfShader);

} else if (properties.image.from.size()) {
SpriteAtlasPosition imagePosA = spriteAtlas.getPosition(properties.image.from, true);
SpriteAtlasPosition imagePosB = spriteAtlas.getPosition(properties.image.to, true);
SpriteAtlasPosition imagePosA = spriteAtlas->getPosition(properties.image.from, true);
SpriteAtlasPosition imagePosB = spriteAtlas->getPosition(properties.image.to, true);

float factor = 8.0 / std::pow(2, state.getIntegerZoom() - id.z);

Expand All @@ -127,7 +127,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linepatternShader->u_opacity = properties.opacity;

MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0));
spriteAtlas.bind(true);
spriteAtlas->bind(true);
MBGL_CHECK_ERROR(glDepthRange(strata + strata_epsilon, 1.0f)); // may or may not matter

bucket.drawLinePatterns(*linepatternShader);
Expand Down
Loading

0 comments on commit bffee07

Please sign in to comment.