protect _text member of Text3DOverlay with mutex

This commit is contained in:
David Kelly 2017-06-02 14:59:27 -07:00
parent d480123e93
commit f9e1f02888
2 changed files with 22 additions and 10 deletions

View file

@ -23,12 +23,13 @@ const float LINE_SCALE_RATIO = 1.2f;
QString const Text3DOverlay::TYPE = "text3d";
Text3DOverlay::Text3DOverlay() {
Text3DOverlay::Text3DOverlay() : _mutex() {
_textRenderer = TextRenderer3D::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
_geometryId = DependencyManager::get<GeometryCache>()->allocateID();
}
Text3DOverlay::Text3DOverlay(const Text3DOverlay* text3DOverlay) :
_mutex(),
Billboard3DOverlay(text3DOverlay),
_text(text3DOverlay->_text),
_backgroundColor(text3DOverlay->_backgroundColor),
@ -51,6 +52,16 @@ Text3DOverlay::~Text3DOverlay() {
}
}
const QString Text3DOverlay::getText() const {
QMutexLocker lock(&_mutex);
return _text;
}
void Text3DOverlay::setText(const QString& text) {
QMutexLocker lock(&_mutex);
_text = text;
}
xColor Text3DOverlay::getBackgroundColor() {
if (_colorPulse == 0.0f) {
return _backgroundColor;
@ -125,7 +136,7 @@ void Text3DOverlay::render(RenderArgs* args) {
// FIXME: Factor out textRenderer so that Text3DOverlay overlay parts can be grouped by pipeline
// for a gpu performance increase. Currently,
// Text renderer sets its own pipeline,
_textRenderer->draw(batch, 0, 0, _text, textColor, glm::vec2(-1.0f), getDrawInFront());
_textRenderer->draw(batch, 0, 0, getText(), textColor, glm::vec2(-1.0f), getDrawInFront());
// so before we continue, we must reset the pipeline
batch.setPipeline(args->_pipeline->pipeline);
args->_pipeline->prepare(batch);
@ -188,7 +199,7 @@ void Text3DOverlay::setProperties(const QVariantMap& properties) {
QVariant Text3DOverlay::getProperty(const QString& property) {
if (property == "text") {
return _text;
return getText();
}
if (property == "textAlpha") {
return _textAlpha;
@ -231,7 +242,7 @@ QSizeF Text3DOverlay::textSize(const QString& text) const {
return QSizeF(extents.x, extents.y) * pointToWorldScale;
}
bool Text3DOverlay::findRayIntersection(const glm::vec3 &origin, const glm::vec3 &direction, float &distance,
bool Text3DOverlay::findRayIntersection(const glm::vec3 &origin, const glm::vec3 &direction, float &distance,
BoxFace &face, glm::vec3& surfaceNormal) {
Transform transform = getTransform();
applyTransformTo(transform, true);

View file

@ -12,14 +12,14 @@
#define hifi_Text3DOverlay_h
#include <QString>
#include <QtCore/QMutex>
#include "Billboard3DOverlay.h"
class TextRenderer3D;
class Text3DOverlay : public Billboard3DOverlay {
Q_OBJECT
public:
static QString const TYPE;
virtual QString getType() const override { return TYPE; }
@ -34,7 +34,7 @@ public:
virtual const render::ShapeKey getShapeKey() override;
// getters
const QString& getText() const { return _text; }
const QString getText() const;
float getLineHeight() const { return _lineHeight; }
float getLeftMargin() const { return _leftMargin; }
float getTopMargin() const { return _topMargin; }
@ -45,7 +45,7 @@ public:
float getBackgroundAlpha() { return getAlpha(); }
// setters
void setText(const QString& text) { _text = text; }
void setText(const QString& text);
void setTextAlpha(float alpha) { _textAlpha = alpha; }
void setLineHeight(float value) { _lineHeight = value; }
void setLeftMargin(float margin) { _leftMargin = margin; }
@ -58,15 +58,16 @@ public:
QSizeF textSize(const QString& test) const; // Meters
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
BoxFace& face, glm::vec3& surfaceNormal) override;
virtual Text3DOverlay* createClone() const override;
private:
TextRenderer3D* _textRenderer = nullptr;
QString _text;
mutable QMutex _mutex; // used to make get/setText threadsafe, mutable so can be used in const functions
xColor _backgroundColor = xColor { 0, 0, 0 };
float _textAlpha { 1.0f };
float _lineHeight { 1.0f };