Fix for multi-threaded access to maps in DebugDraw.

This commit is contained in:
Anthony J. Thibault 2017-03-02 14:42:06 -08:00
parent 644e29a43d
commit bc256f3e8f
2 changed files with 36 additions and 4 deletions

View file

@ -10,6 +10,8 @@
#include "DebugDraw.h"
#include "SharedUtil.h"
using Lock = std::unique_lock<std::mutex>;
DebugDraw& DebugDraw::getInstance() {
static DebugDraw* instance = globalInstance<DebugDraw>("com.highfidelity.DebugDraw");
return *instance;
@ -25,22 +27,50 @@ DebugDraw::~DebugDraw() {
// world space line, drawn only once
void DebugDraw::drawRay(const glm::vec3& start, const glm::vec3& end, const glm::vec4& color) {
Lock lock(_mapMutex);
_rays.push_back(Ray(start, end, color));
}
void DebugDraw::addMarker(const QString& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color) {
Lock lock(_mapMutex);
_markers[key] = MarkerInfo(rotation, position, color);
}
void DebugDraw::removeMarker(const QString& key) {
Lock lock(_mapMutex);
_markers.erase(key);
}
void DebugDraw::addMyAvatarMarker(const QString& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color) {
Lock lock(_mapMutex);
_myAvatarMarkers[key] = MarkerInfo(rotation, position, color);
}
void DebugDraw::removeMyAvatarMarker(const QString& key) {
Lock lock(_mapMutex);
_myAvatarMarkers.erase(key);
}
//
// accessors used by renderer
//
DebugDraw::MarkerMap DebugDraw::getMarkerMap() const {
Lock lock(_mapMutex);
return _markers;
}
DebugDraw::MarkerMap DebugDraw::getMyAvatarMarkerMap() const {
Lock lock(_mapMutex);
return _myAvatarMarkers;
}
DebugDraw::Rays DebugDraw::getRays() const {
Lock lock(_mapMutex);
return _rays;
}
void DebugDraw::clearRays() {
Lock lock(_mapMutex);
_rays.clear();
}

View file

@ -10,6 +10,7 @@
#ifndef hifi_DebugDraw_h
#define hifi_DebugDraw_h
#include <mutex>
#include <unordered_map>
#include <tuple>
#include <string>
@ -87,16 +88,17 @@ public:
// accessors used by renderer
//
const MarkerMap& getMarkerMap() const { return _markers; }
const MarkerMap& getMyAvatarMarkerMap() const { return _myAvatarMarkers; }
MarkerMap getMarkerMap() const;
MarkerMap getMyAvatarMarkerMap() const;
void updateMyAvatarPos(const glm::vec3& pos) { _myAvatarPos = pos; }
const glm::vec3& getMyAvatarPos() const { return _myAvatarPos; }
void updateMyAvatarRot(const glm::quat& rot) { _myAvatarRot = rot; }
const glm::quat& getMyAvatarRot() const { return _myAvatarRot; }
const Rays getRays() const { return _rays; }
void clearRays() { _rays.clear(); }
Rays getRays() const;
void clearRays();
protected:
mutable std::mutex _mapMutex;
MarkerMap _markers;
MarkerMap _myAvatarMarkers;
glm::quat _myAvatarRot;