Merge branch 'master' of github.com:mapbox/mapbox-gl-native
This commit is contained in:
20
include/llmr/util/error.hpp
Normal file
20
include/llmr/util/error.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef LLMR_UTIL_ERROR
|
||||
#define LLMR_UTIL_ERROR
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace llmr {
|
||||
namespace error {
|
||||
|
||||
struct style_parse : std::exception {
|
||||
inline style_parse(size_t offset, const char *msg) : offset(offset), msg(msg) {}
|
||||
inline const char* what() const noexcept { return msg.c_str(); }
|
||||
const size_t offset;
|
||||
const std::string msg;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,6 +7,7 @@ namespace llmr {
|
||||
namespace util {
|
||||
|
||||
void write_file(const std::string &filename, const std::string &data);
|
||||
std::string read_file(const std::string &filename);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <llmr/style/style_bucket.hpp>
|
||||
#include <llmr/util/constants.hpp>
|
||||
#include <llmr/util/time.hpp>
|
||||
#include <llmr/util/error.hpp>
|
||||
#include <csscolorparser/csscolorparser.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
@@ -102,6 +103,9 @@ void Style::loadJSON(const uint8_t *const data) {
|
||||
|
||||
rapidjson::Document doc;
|
||||
doc.Parse<0>((const char *const)data);
|
||||
if (doc.HasParseError()) {
|
||||
throw error::style_parse(doc.GetErrorOffset(), doc.GetParseError());
|
||||
}
|
||||
|
||||
StyleParser parser;
|
||||
parser.parse(const_cast<const rapidjson::Document &>(doc));
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#include <llmr/util/io.hpp>
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
void llmr::util::write_file(const std::string &filename, const std::string &data) {
|
||||
namespace llmr {
|
||||
namespace util {
|
||||
|
||||
void write_file(const std::string &filename, const std::string &data) {
|
||||
FILE *fd = fopen(filename.c_str(), "wb");
|
||||
if (fd) {
|
||||
fwrite(data.data(), sizeof(std::string::value_type), data.size(), fd);
|
||||
@@ -11,3 +17,17 @@ void llmr::util::write_file(const std::string &filename, const std::string &data
|
||||
fprintf(stderr, "Failed to open file\n");
|
||||
}
|
||||
}
|
||||
|
||||
std::string read_file(const std::string &filename) {
|
||||
std::ifstream file(filename);
|
||||
if (file.good()) {
|
||||
std::stringstream data;
|
||||
data << file.rdbuf();
|
||||
return data.str();
|
||||
} else {
|
||||
throw std::runtime_error("Cannot read file " + filename);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
BIN
test/fixtures/result/out.png
vendored
BIN
test/fixtures/result/out.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 267 KiB |
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
BIN
test/fixtures/styles/0.actual.png
vendored
Normal file
BIN
test/fixtures/styles/0.actual.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
BIN
test/fixtures/styles/0.expected.png
vendored
Normal file
BIN
test/fixtures/styles/0.expected.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
8
test/fixtures/styles/0.info.json
vendored
Normal file
8
test/fixtures/styles/0.info.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"zoom": 0,
|
||||
"latitude": 0,
|
||||
"longitude": 0,
|
||||
"angle": 0,
|
||||
"width": 512,
|
||||
"height": 512
|
||||
}
|
||||
34
test/fixtures/styles/0.style.json
vendored
Normal file
34
test/fixtures/styles/0.style.json
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"version": 3,
|
||||
"sources": {
|
||||
"mapbox": {
|
||||
"type": "vector",
|
||||
"url": "tiles/%d-%d-%d.vector.pbf",
|
||||
"tileSize": 512,
|
||||
"maxZoom": 14
|
||||
}
|
||||
},
|
||||
"layers": [
|
||||
{
|
||||
"id": "background",
|
||||
"render": {
|
||||
"type": "background"
|
||||
},
|
||||
"style": {
|
||||
"background-color": "red"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "water",
|
||||
"source": "mapbox",
|
||||
"source-layer": "water",
|
||||
"render": {
|
||||
"type": "fill"
|
||||
},
|
||||
"style": {
|
||||
"fill-color": "blue",
|
||||
"fill-antialias": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -3,12 +3,10 @@
|
||||
#include <llmr/map/map.hpp>
|
||||
#include <llmr/util/image.hpp>
|
||||
#include <llmr/util/io.hpp>
|
||||
#include <llmr/util/timer.hpp>
|
||||
#include <llmr/util/std.hpp>
|
||||
|
||||
#include "../common/headless_view.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
const std::string base_directory = []{
|
||||
std::string fn = __FILE__;
|
||||
@@ -16,11 +14,27 @@ const std::string base_directory = []{
|
||||
return fn;
|
||||
}();
|
||||
|
||||
TEST(Headless, initialize) {
|
||||
const unsigned int width = 1024;
|
||||
const unsigned int height = 768;
|
||||
class HeadlessTest : public ::testing::TestWithParam<const char *> {};
|
||||
|
||||
llmr::util::timer timer;
|
||||
TEST_P(HeadlessTest, render) {
|
||||
const char *base = GetParam();
|
||||
|
||||
const std::string style = llmr::util::read_file(base_directory + "/fixtures/styles/" + base + ".style.json");
|
||||
const std::string info = llmr::util::read_file(base_directory + "/fixtures/styles/" + base + ".info.json");
|
||||
const std::string expected_image = base_directory + "/fixtures/styles/" + base + ".expected.png";
|
||||
const std::string actual_image = base_directory + "/fixtures/styles/" + base + ".actual.png";
|
||||
|
||||
// Parse settings.
|
||||
rapidjson::Document doc;
|
||||
doc.Parse<0>((const char *const)info.c_str());
|
||||
ASSERT_EQ(false, doc.HasParseError());
|
||||
|
||||
const double zoom = doc.HasMember("zoom") ? doc["zoom"].GetDouble() : 0;
|
||||
const double angle = doc.HasMember("angle") ? doc["angle"].GetDouble() : 0;
|
||||
const double longitude = doc.HasMember("longitude") ? doc["longitude"].GetDouble() : 0;
|
||||
const double latitude = doc.HasMember("latitude") ? doc["latitude"].GetDouble() : 0;
|
||||
const unsigned int width = doc.HasMember("width") ? doc["width"].GetUint() : 0;
|
||||
const unsigned int height = doc.HasMember("height") ? doc["height"].GetUint() : 0;
|
||||
|
||||
// Setup OpenGL
|
||||
llmr::HeadlessView view;
|
||||
@@ -29,42 +43,27 @@ TEST(Headless, initialize) {
|
||||
view.resize(width, height);
|
||||
map.resize(width, height);
|
||||
|
||||
timer.report("map setup");
|
||||
map.setStyleJSON(style);
|
||||
|
||||
std::ifstream stylefile("./style.min.js");
|
||||
ASSERT_TRUE(stylefile.good());
|
||||
std::stringstream stylejson;
|
||||
stylejson << stylefile.rdbuf();
|
||||
|
||||
map.setStyleJSON(stylejson.str());
|
||||
|
||||
timer.report("map style");
|
||||
|
||||
map.setLonLatZoom(0, 0, 2);
|
||||
map.setAngle(0);
|
||||
map.setLonLatZoom(longitude, latitude, zoom);
|
||||
map.setAngle(angle);
|
||||
map.setDebug(false);
|
||||
|
||||
// Run the loop. It will terminate when we don't have any further listeners.
|
||||
map.run();
|
||||
|
||||
timer.report("map loop");
|
||||
const std::unique_ptr<uint32_t[]> pixels(new uint32_t[width * height]);
|
||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get());
|
||||
|
||||
uint32_t *pixels = new uint32_t[width * height];
|
||||
const std::string image = llmr::util::compress_png(width, height, pixels.get(), true);
|
||||
llmr::util::write_file(actual_image, image);
|
||||
|
||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
std::string result = llmr::util::compress_png(width, height, pixels, true);
|
||||
llmr::util::write_file("out.png", result);
|
||||
|
||||
std::ifstream imagefile(base_directory + "/fixtures/result/out.png");
|
||||
ASSERT_TRUE(imagefile.good());
|
||||
std::stringstream image;
|
||||
image << imagefile.rdbuf();
|
||||
llmr::util::Image reference(image.str(), true);
|
||||
|
||||
ASSERT_EQ(reference.getWidth(), width);
|
||||
ASSERT_EQ(reference.getHeight(), height);
|
||||
ASSERT_EQ(0, std::memcmp(pixels, reference.getData(), width * height * 4));
|
||||
|
||||
delete[] pixels;
|
||||
const std::string expected_image_data(llmr::util::read_file(expected_image));
|
||||
const llmr::util::Image expected(expected_image_data, true);
|
||||
ASSERT_EQ(width, expected.getWidth());
|
||||
ASSERT_EQ(height, expected.getHeight());
|
||||
ASSERT_EQ(0, std::memcmp(pixels.get(), expected.getData(), width * height * 4));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(Headless, HeadlessTest,
|
||||
::testing::Values("0"));
|
||||
|
||||
Reference in New Issue
Block a user