Skip to content

Commit cda97b9

Browse files
committed
wip: include paparazzi
- add zmq for prime_server - return image result move prime_server functions to worker
1 parent dc52a96 commit cda97b9

20 files changed

+1799
-50
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ if(BENCHMARK)
101101
message(STATUS "Build with benchmarks")
102102
add_subdirectory(${PROJECT_SOURCE_DIR}/bench)
103103
endif()
104+
105+
if(PAPARAZZI)
106+
add_subdirectory(${PROJECT_SOURCE_DIR}/paparazzi)
107+
endif()

core/src/gl/framebuffer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ void FrameBuffer::apply(RenderState& _rs, GLuint _handle, glm::vec2 _viewport, g
4848
// Enable depth testing
4949
_rs.depthMask(GL_TRUE);
5050

51+
_rs.colorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
52+
5153
// Setup raster state
5254
_rs.culling(GL_TRUE);
5355
_rs.cullFace(GL_BACK);

paparazzi/CMakeLists.txt

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
### Worker
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/prime_server.cmake)
4+
5+
# use, i.e. don't skip the full RPATH for the build tree
6+
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
7+
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
8+
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
9+
SET(CMAKE_INSTALL_RPATH ${CMAKE_BINARY_DIR}/lib ${CMAKE_INSTALL_RPATH})
10+
11+
# the RPATH to be used when installing, but only if it's not a system directory
12+
# LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
13+
# IF("${isSystemDir}" STREQUAL "-1")
14+
# SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
15+
# ENDIF("${isSystemDir}" STREQUAL "-1")
16+
17+
# if (USE_SYSTEM_OSMESA_LIBS)
18+
# include(FindPkgConfig)
19+
# pkg_check_modules(OSMesa REQUIRED osmesa)
20+
# else ()
21+
# include(${PROJECT_SOURCE_DIR}/toolchains/mesa.cmake)
22+
# endif()
23+
24+
25+
add_executable(paparazzi_worker
26+
src/worker.cpp
27+
src/paparazzi.cpp
28+
src/stb_image.cpp)
29+
30+
target_include_directories(paparazzi_worker
31+
PUBLIC
32+
src/tools
33+
${PROJECT_SOURCE_DIR}/platforms/common
34+
${PROJECT_SOURCE_DIR}/platforms/linux/src
35+
${OSMesa_INCLUDE_DIRS}
36+
${prime_server_INCLUDE_DIRS})
37+
38+
target_link_libraries(paparazzi_worker
39+
core
40+
headless_context
41+
-lpthread
42+
-lOSMesa
43+
-lGL
44+
${prime_server_LIBRARIES}
45+
${PRIME_SERVER_DEPS_LIBRARIES}
46+
)
47+
48+
target_compile_definitions(paparazzi_worker
49+
PRIVATE
50+
PLATFORM_HEADLESS=1)
51+
52+
target_link_libraries(paparazzi_worker
53+
platform_linux)
54+
55+
add_dependencies(paparazzi_worker external-prime_server)
56+
57+
### Proxy
58+
59+
add_executable(paparazzi_proxy
60+
src/proxy.cpp)
61+
62+
target_include_directories(paparazzi_proxy
63+
PUBLIC
64+
${prime_server_INCLUDE_DIRS})
65+
66+
target_link_libraries(paparazzi_proxy
67+
${prime_server_LIBRARIES}
68+
${PRIME_SERVER_DEPS_LIBRARIES}
69+
)
70+
71+
add_dependencies(paparazzi_proxy external-prime_server)

paparazzi/prime_server.cmake

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
include(FindPkgConfig)
2+
3+
pkg_check_modules(PRIME_SERVER_DEPS REQUIRED libzmq>=4.1.3 libczmq>=3.0 libcurl>=7.22.0)
4+
5+
include(ExternalProject)
6+
7+
ExternalProject_Add(
8+
external-prime_server
9+
10+
GIT_REPOSITORY "https://github.com/kevinkreiser/prime_server.git"
11+
12+
UPDATE_COMMAND ""
13+
PATCH_COMMAND ""
14+
15+
BUILD_IN_SOURCE 1
16+
SOURCE_DIR "${CMAKE_BINARY_DIR}/prime_server"
17+
#BINARY_DIR "${CMAKE_BINARY_DIR}/prime_server-build"
18+
19+
CONFIGURE_COMMAND "${CMAKE_BINARY_DIR}/prime_server/autogen.sh" && "${CMAKE_BINARY_DIR}/prime_server/configure" --prefix=<INSTALL_DIR>
20+
21+
BUILD_COMMAND make
22+
23+
TEST_COMMAND ""
24+
25+
INSTALL_COMMAND make install
26+
)
27+
28+
set(external-prime_server_CXXFLAGS "-lpthread")
29+
30+
ExternalProject_Get_Property(external-prime_server install_dir)
31+
32+
ExternalProject_Add_Step(
33+
external-prime_server CopyToBin
34+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${install_dir}/bin ${CMAKE_BINARY_DIR}/bin
35+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${install_dir}/lib ${CMAKE_BINARY_DIR}/lib
36+
DEPENDEES install
37+
)
38+
39+
set(prime_server_INCLUDE_DIRS "${install_dir}/include")
40+
set(prime_server_LIBRARIES "${CMAKE_SHARED_LIBRARY_PREFIX}prime_server${CMAKE_SHARED_LIBRARY_SUFFIX}")
41+
42+
link_directories(${CMAKE_BINARY_DIR}/lib)

paparazzi/src/paparazzi.cpp

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include "paparazzi.h"
2+
3+
#define AA_SCALE 1.0
4+
#define IMAGE_DEPTH 4
5+
6+
#if PLATFORM_LINUX
7+
#include "platform_linux.h"
8+
#elif PLATFORM_OSX
9+
#include "platform_osx.h"
10+
#endif
11+
12+
#include "log.h"
13+
#include "gl/texture.h"
14+
15+
#include "hash-library/md5.h"
16+
17+
#include <functional>
18+
#include <csignal>
19+
#include <fstream>
20+
#include <sstream>
21+
#include <sys/time.h>
22+
#include <unistd.h>
23+
#include "glm/trigonometric.hpp"
24+
25+
#include "stb_image_write.h"
26+
27+
28+
using namespace Tangram;
29+
30+
unsigned long long timeStart;
31+
32+
double getTime() {
33+
static struct timeval tv;
34+
35+
gettimeofday(&tv, NULL);
36+
return ((unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000) * 0.001;
37+
}
38+
39+
Paparazzi::Paparazzi()
40+
: m_scene(""),
41+
m_lat(0.0),
42+
m_lon(0.0),
43+
m_zoom(0.0f),
44+
m_rotation(0.0f),
45+
m_tilt(0.0),
46+
m_width(0),
47+
m_height(0) {
48+
49+
m_glContext = std::make_unique<HeadlessContext>();
50+
if (!m_glContext->init()) {
51+
throw std::runtime_error("Could not initialize GL context");
52+
}
53+
m_glContext->resize(1, 1);
54+
if (!m_glContext->makeCurrent()) {
55+
throw std::runtime_error("Could not activate GL context");
56+
}
57+
58+
#if PLATFORM_LINUX
59+
UrlClient::Options urlClientOptions;
60+
urlClientOptions.numberOfThreads = 10;
61+
auto platform = std::make_shared<LinuxPlatform>(urlClientOptions);
62+
#elif PLATFORM_OSX
63+
auto platform = std::make_shared<OSXPlatform>();
64+
#endif
65+
66+
timeStart = getTime();
67+
68+
m_map = std::unique_ptr<Tangram::Map>(new Tangram::Map(platform));
69+
70+
setScene("scene.yaml");
71+
72+
m_map->setupGL();
73+
m_map->setPixelScale(AA_SCALE);
74+
75+
//m_map->resize(m_width*AA_SCALE, m_height*AA_SCALE);
76+
//m_glContext->setScale(AA_SCALE);
77+
//setSize(m_width, m_height, 1.0);
78+
79+
Tangram::setDebugFlag(DebugFlags::tile_bounds, true);
80+
81+
logMsg("Done Init!\n");
82+
83+
}
84+
85+
Paparazzi::~Paparazzi() {
86+
}
87+
88+
void Paparazzi::setSize (const int &_width, const int &_height, const float &_density) {
89+
if (_density*_width == m_width && _density*_height == m_height &&
90+
_density*AA_SCALE == m_map->getPixelScale()) { return; }
91+
92+
m_width = _width*_density;
93+
m_height = _height*_density;
94+
95+
// Setup the size of the image
96+
if (_density*AA_SCALE != m_map->getPixelScale()) {
97+
m_map->setPixelScale(_density*AA_SCALE);
98+
}
99+
m_map->resize(m_width*AA_SCALE, m_height*AA_SCALE);
100+
101+
m_glContext->resize(m_width, m_height);
102+
}
103+
104+
void Paparazzi::setZoom(const float &_zoom) {
105+
if (_zoom == m_zoom) { return; }
106+
m_zoom = _zoom;
107+
m_map->setZoom(_zoom);
108+
}
109+
110+
void Paparazzi::setTilt(const float &_deg) {
111+
if (_deg == m_tilt) { return; }
112+
m_tilt = _deg;
113+
m_map->setTilt(glm::radians(m_tilt));
114+
}
115+
void Paparazzi::setRotation(const float &_deg) {
116+
if (_deg == m_rotation) { return; }
117+
118+
m_rotation = _deg;
119+
m_map->setRotation(glm::radians(m_rotation));
120+
}
121+
122+
void Paparazzi::setPosition(const double &_lon, const double &_lat) {
123+
if (_lon == m_lon && _lat == m_lat) { return; }
124+
125+
m_lon = _lon;
126+
m_lat = _lat;
127+
m_map->setPosition(m_lon, m_lat);
128+
}
129+
130+
void Paparazzi::setScene(const std::string &_url) {
131+
if (_url == m_scene) { return; }
132+
m_scene = _url;
133+
m_map->loadScene(m_scene.c_str(), false,
134+
{SceneUpdate("global.sdk_mapzen_api_key", m_apiKey)});
135+
}
136+
137+
void Paparazzi::setSceneContent(const std::string &_yaml_content) {
138+
MD5 md5;
139+
std::string md5_scene = md5(_yaml_content);
140+
141+
if (md5_scene == m_scene) { return; }
142+
m_scene = md5_scene;
143+
144+
// TODO:
145+
// - This is waiting for LoadSceneConfig to be implemented in Tangram::Map
146+
// Once that's done there is no need to save the file.
147+
std::string name = "cache/"+md5_scene+".yaml";
148+
std::ofstream out(name.c_str());
149+
out << _yaml_content.c_str();
150+
out.close();
151+
152+
m_map->loadScene(name.c_str(), false, {SceneUpdate("global.sdk_mapzen_api_key", m_apiKey)});
153+
}
154+
155+
bool Paparazzi::update(int32_t _maxWaitTime) {
156+
double startTime = getTime();
157+
float delta = 0.0;
158+
159+
while (_maxWaitTime < 0 || delta < _maxWaitTime) {
160+
161+
bool bFinish = m_map->update(10.);
162+
delta = float(getTime() - startTime);
163+
164+
if (bFinish) {
165+
logMsg("Update: Finish!\n");
166+
return true;
167+
}
168+
usleep(10000);
169+
}
170+
logMsg("Update: Done waiting...\n");
171+
return false;
172+
}
173+
174+
void Paparazzi::render(std::string& _image) {
175+
m_glContext->makeCurrent();
176+
177+
m_map->render();
178+
179+
GL::finish();
180+
181+
stbi_write_png_to_func([](void *context, void *data, int size) {
182+
static_cast<std::string*>(context)->append(static_cast<const char*>(data), size);
183+
},
184+
&_image,
185+
m_glContext->width(),
186+
m_glContext->height(),
187+
IMAGE_DEPTH,
188+
m_glContext->buffer(),
189+
m_glContext->width() * IMAGE_DEPTH);
190+
}

paparazzi/src/paparazzi.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
6+
#include "tangram.h"
7+
#include "headlessContext.h"
8+
9+
class Paparazzi {
10+
public:
11+
Paparazzi();
12+
~Paparazzi();
13+
14+
void setSize(const int &_width, const int &_height, const float &_density);
15+
void setZoom(const float &_zoom);
16+
void setTilt(const float &_deg);
17+
void setRotation(const float &_deg);
18+
void setScene(const std::string &_url);
19+
void setSceneContent(const std::string &_yaml_content);
20+
void setPosition(const double &_lon, const double &_lat);
21+
void render(std::string& _image);
22+
bool update(int32_t _maxWaitTime = -1);
23+
24+
void setApiKey(const std::string &_apiKey) { m_apiKey = _apiKey; }
25+
26+
protected:
27+
28+
std::string m_scene;
29+
std::string m_apiKey;
30+
double m_lat;
31+
double m_lon;
32+
float m_zoom;
33+
float m_rotation;
34+
float m_tilt;
35+
int m_width;
36+
int m_height;
37+
38+
std::unique_ptr<Tangram::Map> m_map;
39+
std::unique_ptr<Tangram::HeadlessContext> m_glContext;
40+
};

0 commit comments

Comments
 (0)