Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/CartoDB/mobile-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mtehver committed May 18, 2017
2 parents e3e179d + 6ee47e1 commit 79fa41b
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 76 deletions.
19 changes: 19 additions & 0 deletions all/native/components/Layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace carto {
std::shared_ptr<MapRenderer> mapRenderer;
{
std::lock_guard<std::mutex> lock(_mutex);
if (!layer) {
throw NullArgumentException("Null layer");
}
if (index < 0 || static_cast<std::size_t>(index) >= _layers.size()) {
throw OutOfRangeException("Layer index out of range");
}
Expand All @@ -69,6 +72,10 @@ namespace carto {
{
std::lock_guard<std::mutex> lock(_mutex);
for (const std::shared_ptr<Layer>& layer : _layers) {
if (!layer) {
throw NullArgumentException("Null layer");
}

if (std::find(layers.begin(), layers.end(), layer) == layers.end()) {
layer->setComponents(std::shared_ptr<CancelableThreadPool>(), std::shared_ptr<CancelableThreadPool>(), std::shared_ptr<Options>(), std::weak_ptr<MapRenderer>(), std::weak_ptr<TouchHandler>());
}
Expand All @@ -94,9 +101,13 @@ namespace carto {
std::shared_ptr<MapRenderer> mapRenderer;
{
std::lock_guard<std::mutex> lock(_mutex);
if (!layer) {
throw NullArgumentException("Null layer");
}
if (index < 0 || static_cast<std::size_t>(index) > _layers.size()) {
throw OutOfRangeException("Layer index out of range");
}

layer->setComponents(_envelopeThreadPool, _tileThreadPool, _options, _mapRenderer, _touchHandler);
_layers.insert(_layers.begin() + index, layer);

Expand All @@ -119,6 +130,10 @@ namespace carto {
{
std::lock_guard<std::mutex> lock(_mutex);
for (const std::shared_ptr<Layer>& layer : layers) {
if (!layer) {
throw NullArgumentException("Null layer");
}

layer->setComponents(_envelopeThreadPool, _tileThreadPool, _options, _mapRenderer, _touchHandler);
_layers.push_back(layer);
}
Expand All @@ -145,6 +160,10 @@ namespace carto {
{
std::lock_guard<std::mutex> lock(_mutex);
for (const std::shared_ptr<Layer>& layer : layers) {
if (!layer) {
throw NullArgumentException("Null layer");
}

auto it = std::remove(_layers.begin(), _layers.end(), layer);
if (it == _layers.end()) {
removedAll = false;
Expand Down
8 changes: 4 additions & 4 deletions all/native/layers/VectorTileLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ namespace carto {
// Create new rendererer, simply drop old one (if exists)
auto renderer = std::make_shared<TileRenderer>(_mapRenderer, _useFBO, _useDepth, _useStencil);
renderer->onSurfaceCreated(shaderManager, textureManager);
renderer->setBackgroundColor(_tileDecoder->getBackgroundColor());
if (_tileDecoder->getBackgroundPattern()) {
renderer->setBackgroundPattern(_tileDecoder->getBackgroundPattern());
}
setRenderer(renderer);
}

bool VectorTileLayer::onDrawFrame(float deltaSeconds, BillboardSorter& billboardSorter, StyleTextureCache& styleCache, const ViewState& viewState) {
updateTileLoadListener();

if (auto renderer = getRenderer()) {
renderer->setBackgroundColor(_tileDecoder->getBackgroundColor());
if (auto backgroundPattern = _tileDecoder->getBackgroundPattern()) {
renderer->setBackgroundPattern(backgroundPattern);
}
renderer->setLabelOrder(static_cast<int>(getLabelRenderOrder()));
renderer->setBuildingOrder(static_cast<int>(getBuildingRenderOrder()));
renderer->setInteractionMode(_vectorTileEventListener.get() ? true : false);
Expand Down
21 changes: 21 additions & 0 deletions all/native/renderers/MapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "renderers/cameraevents/CameraRotationEvent.h"
#include "renderers/cameraevents/CameraTiltEvent.h"
#include "renderers/cameraevents/CameraZoomEvent.h"
#include "renderers/workers/BillboardPlacementWorker.h"
#include "renderers/workers/CullWorker.h"
#include "renderers/workers/RedrawWorker.h"
#include "utils/Const.h"
#include "utils/Log.h"
#include "utils/ThreadUtils.h"
Expand All @@ -39,6 +42,10 @@ namespace carto {
_cullWorker(std::make_shared<CullWorker>()),
_cullThread(),
_backgroundRenderer(*options),
#ifdef TARGET_XAMARIN
_redrawWorker(std::make_shared<RedrawWorker>()),
_redrawThread(),
#endif
_watermarkRenderer(*options),
_billboardSorter(),
_billboardDrawDataBuffer(),
Expand Down Expand Up @@ -69,6 +76,11 @@ namespace carto {
void MapRenderer::init() {
_cullWorker->setComponents(shared_from_this(), _cullWorker);
_cullThread = std::thread(std::ref(*_cullWorker));

#ifdef TARGET_XAMARIN
_redrawWorker->setComponents(shared_from_this(), _redrawWorker);
_redrawThread = std::thread(std::ref(*_redrawWorker));
#endif

_billboardPlacementWorker->setComponents(shared_from_this(), _billboardPlacementWorker);
_billboardPlacementThread = std::thread(std::ref(*_billboardPlacementWorker));
Expand All @@ -80,6 +92,11 @@ namespace carto {
void MapRenderer::deinit() {
_options->unregisterOnChangeListener(_optionsListener);
_optionsListener.reset();

#ifdef TARGET_XAMARIN
_redrawWorker->stop();
_redrawThread.detach();
#endif

_cullWorker->stop();
_cullThread.detach();
Expand Down Expand Up @@ -116,7 +133,11 @@ namespace carto {

if (redrawRequestListener) {
_redrawPending = true;
#ifdef TARGET_XAMARIN
_redrawWorker->init(redrawRequestListener);
#else
redrawRequestListener->onRedrawRequested();
#endif
}
}

Expand Down
9 changes: 7 additions & 2 deletions all/native/renderers/MapRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "renderers/components/BillboardSorter.h"
#include "renderers/components/KineticEventHandler.h"
#include "renderers/WatermarkRenderer.h"
#include "renderers/workers/BillboardPlacementWorker.h"
#include "renderers/workers/CullWorker.h"

#include <atomic>
#include <chrono>
Expand All @@ -43,6 +41,9 @@ namespace carto {
class RayIntersectedElement;
class Options;
class ThreadWorker;
class CullWorker;
class BillboardPlacementWorker;
class RedrawWorker;
class ShaderManager;
class TextureManager;

Expand Down Expand Up @@ -169,6 +170,10 @@ namespace carto {

std::shared_ptr<CullWorker> _cullWorker;
std::thread _cullThread;
#ifdef TARGET_XAMARIN
std::shared_ptr<RedrawWorker> _redrawWorker;
std::thread _redrawThread;
#endif

std::shared_ptr<OptionsListener> _optionsListener;

Expand Down
2 changes: 1 addition & 1 deletion all/native/renderers/workers/CullWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace carto {
CullWorker();
virtual ~CullWorker();

void setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<CullWorker>& cullWorker);
void setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<CullWorker>& worker);

void init(const std::shared_ptr<Layer>& layer, int delayTime);

Expand Down
93 changes: 93 additions & 0 deletions all/native/renderers/workers/RedrawWorker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "RedrawWorker.h"
#include "renderers/MapRenderer.h"
#include "renderers/RedrawRequestListener.h"
#include "utils/Log.h"
#include "utils/ThreadUtils.h"

namespace carto {

RedrawWorker::RedrawWorker() :
_wakeupMap(),
_mapRenderer(),
_stop(false),
_idle(false),
_condition(),
_mutex()
{
}

RedrawWorker::~RedrawWorker() {
}

void RedrawWorker::setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<RedrawWorker>& worker) {
_mapRenderer = mapRenderer;
// When the map component gets destroyed all threads get detatched. Detatched threads need their worker objects to be alive,
// so worker objects need to keep references to themselves, until the loop finishes.
_worker = worker;
}

void RedrawWorker::init(const DirectorPtr<RedrawRequestListener>& listener) {
std::lock_guard<std::mutex> lock(_mutex);

std::chrono::steady_clock::time_point wakeupTime = std::chrono::steady_clock::now();
if (_wakeupMap.find(listener) != _wakeupMap.end()) {
return;
}
_wakeupMap[listener] = wakeupTime;
_idle = false;
_condition.notify_one();
}

void RedrawWorker::stop() {
std::lock_guard<std::mutex> lock(_mutex);
_stop = true;
_condition.notify_all();
}

bool RedrawWorker::isIdle() const {
std::lock_guard<std::mutex> lock(_mutex);
return _idle;
}

void RedrawWorker::operator ()() {
run();
_worker.reset();
}

void RedrawWorker::run() {
ThreadUtils::SetThreadPriority(ThreadPriority::HIGH);
while (true) {
std::vector<DirectorPtr<RedrawRequestListener> > listeners;
{
std::unique_lock<std::mutex> lock(_mutex);

if (_stop) {
return;
}

std::chrono::steady_clock::time_point currentTime = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point wakeupTime = std::chrono::steady_clock::now() + std::chrono::hours(24);
for (auto it = _wakeupMap.begin(); it != _wakeupMap.end(); ) {
if (it->second - currentTime < std::chrono::milliseconds(1)) {
listeners.push_back(it->first);
it = _wakeupMap.erase(it);
} else {
wakeupTime = std::min(wakeupTime, it->second);
it++;
}
}

if (listeners.empty()) {
_idle = _wakeupMap.empty();
_condition.wait_for(lock, wakeupTime - std::chrono::steady_clock::now());
_idle = false;
}
}

for (const DirectorPtr<RedrawRequestListener>& listener : listeners) {
listener->onRedrawRequested();
}
}
}

}
56 changes: 56 additions & 0 deletions all/native/renderers/workers/RedrawWorker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2016 CartoDB. All rights reserved.
* Copying and using this code is allowed only according
* to license terms, as given in https://cartodb.com/terms/
*/

#ifndef _CARTO_REDRAWWORKER_H_
#define _CARTO_REDRAWWORKER_H_

#include "components/DirectorPtr.h"
#include "components/ThreadWorker.h"

#include <chrono>
#include <condition_variable>
#include <map>
#include <memory>
#include <thread>
#include <vector>

namespace carto {
class MapRenderer;
class Options;
class RedrawRequestListener;

class RedrawWorker : public ThreadWorker {
public:
RedrawWorker();
virtual ~RedrawWorker();

void setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<RedrawWorker>& worker);

void init(const DirectorPtr<RedrawRequestListener>& listener);

void stop();

bool isIdle() const;

void operator()();

private:
void run();

std::map<DirectorPtr<RedrawRequestListener>, std::chrono::steady_clock::time_point> _wakeupMap;

std::weak_ptr<MapRenderer> _mapRenderer;
std::shared_ptr<RedrawWorker> _worker;

bool _stop;
bool _idle;
std::condition_variable _condition;
mutable std::mutex _mutex;
};

}

#endif
7 changes: 7 additions & 0 deletions all/native/services/CartoMapsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ namespace carto {
urlTemplateBase += (i == 0 ? "/" : ",") + boost::lexical_cast<std::string>(layerInfos[i].index);
}
std::string urlTemplateSuffix;
if (!_authTokens.empty()) {
std::multimap<std::string, std::string> urlParams;
for (const std::string& authToken : _authTokens) {
urlParams.insert({ _authTokens.size() == 1 ? "auth_token" : "auth_token[]", authToken });
}
urlTemplateSuffix = NetworkUtils::BuildURLFromParameters("", urlParams);
}

// Create layer based on type and flags
if (type == "torque") {
Expand Down
18 changes: 13 additions & 5 deletions all/native/utils/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ namespace carto {
}

std::lock_guard<std::mutex> lock(_Mutex);
OutputLog(LOG_TYPE_ERROR, _Tag, message);
if (_ShowError) {
OutputLog(LOG_TYPE_ERROR, _Tag, message);
}
}

void Log::Warn(const char* message) {
Expand All @@ -131,7 +133,9 @@ namespace carto {
}

std::lock_guard<std::mutex> lock(_Mutex);
OutputLog(LOG_TYPE_WARNING, _Tag, message);
if (_ShowWarn) {
OutputLog(LOG_TYPE_WARNING, _Tag, message);
}
}

void Log::Info(const char* message) {
Expand All @@ -143,7 +147,9 @@ namespace carto {
}

std::lock_guard<std::mutex> lock(_Mutex);
OutputLog(LOG_TYPE_INFO, _Tag, message);
if (_ShowInfo) {
OutputLog(LOG_TYPE_INFO, _Tag, message);
}
}

void Log::Debug(const char* message) {
Expand All @@ -155,7 +161,9 @@ namespace carto {
}

std::lock_guard<std::mutex> lock(_Mutex);
OutputLog(LOG_TYPE_DEBUG, _Tag, message);
if (_ShowDebug) {
OutputLog(LOG_TYPE_DEBUG, _Tag, message);
}
}

Log::Log() {
Expand All @@ -164,7 +172,7 @@ namespace carto {
bool Log::_ShowError = true;
bool Log::_ShowWarn = true;
bool Log::_ShowInfo = true;
bool Log::_ShowDebug = true;
bool Log::_ShowDebug = false;

std::string Log::_Tag = "carto-mobile-sdk";

Expand Down
Loading

0 comments on commit 79fa41b

Please sign in to comment.