[core] Render from the main thread

Do not create a thread for the MapContext anymore.
This commit is contained in:
John Firebaugh
2015-09-01 15:07:17 +03:00
parent 18d8e80f52
commit 204c7fee03
35 changed files with 268 additions and 525 deletions

View File

@@ -12,7 +12,6 @@
#include <mbgl/layer/custom_layer.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/math.hpp>
namespace mbgl {
@@ -20,46 +19,24 @@ namespace mbgl {
Map::Map(View& view_, FileSource& fileSource, MapMode mapMode, GLContextMode contextMode, ConstrainMode constrainMode)
: view(view_),
transform(std::make_unique<Transform>(view, constrainMode)),
context(std::make_unique<util::Thread<MapContext>>(
util::ThreadContext{"Map", util::ThreadType::Map, util::ThreadPriority::Regular},
context(std::make_unique<MapContext>(
view, fileSource, mapMode, contextMode, view.getPixelRatio())),
data(&context->invokeSync<MapData&>(&MapContext::getData))
data(&context->getData())
{
view.initialize(this);
update(Update::Dimensions);
}
Map::~Map() {
resume();
context->invokeSync(&MapContext::cleanup);
}
void Map::pause() {
assert(data->mode == MapMode::Continuous);
std::unique_lock<std::mutex> lockPause(data->mutexPause);
if (!data->paused) {
context->invoke(&MapContext::pause);
data->condPause.wait(lockPause, [&]{ return data->paused; });
}
}
bool Map::isPaused() {
return data->paused;
}
void Map::resume() {
std::unique_lock<std::mutex> lockPause(data->mutexPause);
data->paused = false;
data->condPause.notify_all();
context->cleanup();
}
void Map::renderStill(StillImageCallback callback) {
context->invoke(&MapContext::renderStill, transform->getState(),
context->renderStill(transform->getState(),
FrameData { view.getFramebufferSize(), Clock::now() }, callback);
}
void Map::renderSync() {
void Map::render() {
if (renderState == RenderState::never) {
view.notifyMapChange(MapChangeWillStartRenderingMap);
}
@@ -67,8 +44,7 @@ void Map::renderSync() {
view.notifyMapChange(MapChangeWillStartRenderingFrame);
const Update flags = transform->updateTransitions(Clock::now());
const bool fullyLoaded = context->invokeSync<bool>(
&MapContext::renderSync, transform->getState(), FrameData { view.getFramebufferSize(), Clock::now() });
const bool fullyLoaded = context->renderSync(transform->getState(), FrameData { view.getFramebufferSize(), Clock::now() });
view.notifyMapChange(fullyLoaded ?
MapChangeDidFinishRenderingFrameFullyRendered :
@@ -94,7 +70,7 @@ void Map::renderSync() {
void Map::update(Update flags) {
if (flags & Update::Dimensions) transform->resize(view.getSize());
context->invoke(&MapContext::triggerUpdate, transform->getState(), flags);
context->triggerUpdate(transform->getState(), flags);
}
#pragma mark - Style
@@ -102,21 +78,21 @@ void Map::update(Update flags) {
void Map::setStyleURL(const std::string &url) {
loading = true;
view.notifyMapChange(MapChangeWillStartLoadingMap);
context->invoke(&MapContext::setStyleURL, url);
context->setStyleURL(url);
}
void Map::setStyleJSON(const std::string& json, const std::string& base) {
loading = true;
view.notifyMapChange(MapChangeWillStartLoadingMap);
context->invoke(&MapContext::setStyleJSON, json, base);
context->setStyleJSON(json, base);
}
std::string Map::getStyleURL() const {
return context->invokeSync<std::string>(&MapContext::getStyleURL);
return context->getStyleURL();
}
std::string Map::getStyleJSON() const {
return context->invokeSync<std::string>(&MapContext::getStyleJSON);
return context->getStyleJSON();
}
#pragma mark - Transitions
@@ -436,15 +412,15 @@ LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {
#pragma mark - Annotations
void Map::addAnnotationIcon(const std::string& name, std::shared_ptr<const SpriteImage> sprite) {
context->invoke(&MapContext::addAnnotationIcon, name, sprite);
context->addAnnotationIcon(name, sprite);
}
void Map::removeAnnotationIcon(const std::string& name) {
context->invoke(&MapContext::removeAnnotationIcon, name);
context->removeAnnotationIcon(name);
}
double Map::getTopOffsetPixelsForAnnotationIcon(const std::string& symbol) {
return context->invokeSync<double>(&MapContext::getTopOffsetPixelsForAnnotationIcon, symbol);
return context->getTopOffsetPixelsForAnnotationIcon(symbol);
}
AnnotationID Map::addPointAnnotation(const PointAnnotation& annotation) {
@@ -489,17 +465,21 @@ AnnotationIDs Map::getPointAnnotationsInBounds(const LatLngBounds& bounds) {
void Map::addCustomLayer(const std::string& id,
CustomLayerInitializeFunction initialize,
CustomLayerRenderFunction render,
CustomLayerRenderFunction render_,
CustomLayerDeinitializeFunction deinitialize,
void* context_,
const char* before) {
context->invoke(&MapContext::addLayer,
std::make_unique<CustomLayer>(id, initialize, render, deinitialize, context_),
view.activate();
context->addLayer(
std::make_unique<CustomLayer>(id, initialize, render_, deinitialize, context_),
before ? std::string(before) : optional<std::string>());
view.deactivate();
}
void Map::removeCustomLayer(const std::string& id) {
context->invoke(&MapContext::removeLayer, id);
view.activate();
context->removeLayer(id);
view.deactivate();
}
#pragma mark - Toggles
@@ -519,39 +499,39 @@ MapDebugOptions Map::getDebug() const {
}
bool Map::isFullyLoaded() const {
return context->invokeSync<bool>(&MapContext::isLoaded);
return context->isLoaded();
}
void Map::addClass(const std::string& className, const PropertyTransition& properties) {
context->invoke(&MapContext::addClass, className, properties);
context->addClass(className, properties);
}
void Map::removeClass(const std::string& className, const PropertyTransition& properties) {
context->invoke(&MapContext::removeClass, className, properties);
context->removeClass(className, properties);
}
void Map::setClasses(const std::vector<std::string>& classNames, const PropertyTransition& properties) {
context->invoke(&MapContext::setClasses, classNames, properties);
context->setClasses(classNames, properties);
}
bool Map::hasClass(const std::string& className) const {
return context->invokeSync<bool>(&MapContext::hasClass, className);
return context->hasClass(className);
}
std::vector<std::string> Map::getClasses() const {
return context->invokeSync<std::vector<std::string>>(&MapContext::getClasses);
return context->getClasses();
}
void Map::setSourceTileCacheSize(size_t size) {
context->invoke(&MapContext::setSourceTileCacheSize, size);
context->setSourceTileCacheSize(size);
}
void Map::onLowMemory() {
context->invoke(&MapContext::onLowMemory);
context->onLowMemory();
}
void Map::dumpDebugLogs() const {
context->invokeSync(&MapContext::dumpDebugLogs);
context->dumpDebugLogs();
}
} // namespace mbgl