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

Commit

Permalink
[qt] Implement CustomLayerHost using a forwarding wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Asheem Mamoowala committed Mar 30, 2018
1 parent 6bca0f0 commit 46383bb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
12 changes: 8 additions & 4 deletions platform/qt/include/qmapbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ struct Q_MAPBOXGL_EXPORT CustomLayerRenderParameters {
double zoom;
double bearing;
double pitch;
double altitude;
double fieldOfView;
};

typedef void (*CustomLayerInitializeFunction)(void* context) ;
typedef void (*CustomLayerRenderFunction)(void* context, const CustomLayerRenderParameters&);
typedef void (*CustomLayerDeinitializeFunction)(void* context);
class Q_MAPBOXGL_EXPORT CustomLayerHostInterface {
public:
virtual ~CustomLayerHostInterface() = default;
virtual void initialize() = 0;
virtual void render(const CustomLayerRenderParameters&) = 0;
virtual void deinitialize() = 0;
};

} // namespace QMapbox

Expand Down
5 changes: 1 addition & 4 deletions platform/qt/include/qmapboxgl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ class Q_MAPBOXGL_EXPORT QMapboxGL : public QObject
void removeImage(const QString &name);

void addCustomLayer(const QString &id,
QMapbox::CustomLayerInitializeFunction,
QMapbox::CustomLayerRenderFunction,
QMapbox::CustomLayerDeinitializeFunction,
void* context,
QScopedPointer<QMapbox::CustomLayerHostInterface>& host,
const QString& before = QString());
void addLayer(const QVariantMap &params, const QString& before = QString());
bool layerExists(const QString &id);
Expand Down
22 changes: 2 additions & 20 deletions platform/qt/src/qmapbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,9 @@ namespace QMapbox {
*/

/*!
\typedef QMapbox::CustomLayerDeinitializeFunction
\class QMapbox::CustomLayerHostInterface
Represents a callback to be called when destroying a custom layer.
\warning This is used for delegating the rendering of a layer to the user of
this API and is not officially supported. Use at your own risk.
*/

/*!
\typedef QMapbox::CustomLayerInitializeFunction
Represents a callback to be called when initializing a custom layer.
\warning This is used for delegating the rendering of a layer to the user of
this API and is not officially supported. Use at your own risk.
*/

/*!
\typedef QMapbox::CustomLayerRenderFunction
Represents a callback to be called on each render pass for a custom layer.
Represents a host interface to be implemented for rendering custom layers.
\warning This is used for delegating the rendering of a layer to the user of
this API and is not officially supported. Use at your own risk.
Expand Down
43 changes: 33 additions & 10 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,20 +1343,43 @@ void QMapboxGL::removeSource(const QString& id)
this API and is not officially supported. Use at your own risk.
*/
void QMapboxGL::addCustomLayer(const QString &id,
QMapbox::CustomLayerInitializeFunction initFn,
QMapbox::CustomLayerRenderFunction renderFn,
QMapbox::CustomLayerDeinitializeFunction deinitFn,
void *context,
QScopedPointer<QMapbox::CustomLayerHostInterface>& host,
const QString& before)
{
class HostWrapper : public mbgl::style::CustomLayerHost {
public:
QScopedPointer<QMapbox::CustomLayerHostInterface> ptr;
HostWrapper(QScopedPointer<QMapbox::CustomLayerHostInterface>& p)
: ptr(p.take()) {
}

void initialize() {
ptr->initialize();
}

void render(const mbgl::style::CustomLayerRenderParameters& params) {
QMapbox::CustomLayerRenderParameters renderParams;
renderParams.width = params.width;
renderParams.height = params.height;
renderParams.latitude = params.latitude;
renderParams.longitude = params.longitude;
renderParams.zoom = params.zoom;
renderParams.bearing = params.bearing;
renderParams.pitch = params.pitch;
renderParams.fieldOfView = params.fieldOfView;
ptr->render(renderParams);
}

void contextLost() { }

void deinitialize() {
ptr->deinitialize();
}
};

d_ptr->mapObj->getStyle().addLayer(std::make_unique<mbgl::style::CustomLayer>(
id.toStdString(),
reinterpret_cast<mbgl::style::CustomLayerInitializeFunction>(initFn),
// This cast is safe as long as both mbgl:: and QMapbox::
// CustomLayerRenderParameters members remains the same.
(mbgl::style::CustomLayerRenderFunction)renderFn,
reinterpret_cast<mbgl::style::CustomLayerDeinitializeFunction>(deinitFn),
context),
std::make_unique<HostWrapper>(host)),
before.isEmpty() ? mbgl::optional<std::string>() : mbgl::optional<std::string>(before.toStdString()));
}

Expand Down

0 comments on commit 46383bb

Please sign in to comment.