diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-08 14:30:42 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-09 13:51:28 -0800 |
commit | bde07c8f0edcc890efd7fd6993f5309d6881c402 (patch) | |
tree | 38464b4f291551d0cece8505941c3763e70bc3ab /test/api | |
parent | 0346cfd865b7f26e92561efdc3fca458c88800e5 (diff) | |
download | qtlocation-mapboxgl-bde07c8f0edcc890efd7fd6993f5309d6881c402.tar.gz |
[core] Custom layers
Diffstat (limited to 'test/api')
-rw-r--r-- | test/api/annotations.cpp | 55 | ||||
-rw-r--r-- | test/api/custom_layer.cpp | 83 |
2 files changed, 100 insertions, 38 deletions
diff --git a/test/api/annotations.cpp b/test/api/annotations.cpp index d3299443f8..1040040220 100644 --- a/test/api/annotations.cpp +++ b/test/api/annotations.cpp @@ -14,41 +14,20 @@ using namespace mbgl; -namespace { - -std::string getFileSourceRoot() { -#ifdef MBGL_ASSET_ZIP - return "test/fixtures/annotations/assets.zip"; -#else - return ""; -#endif -} - -} // namespace - std::shared_ptr<SpriteImage> defaultMarker() { PremultipliedImage image = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); return std::make_shared<SpriteImage>(image.width, image.height, 1.0, std::string(reinterpret_cast<char*>(image.data.get()), image.size())); } -PremultipliedImage render(Map& map) { - std::promise<PremultipliedImage> promise; - map.renderStill([&](std::exception_ptr, PremultipliedImage&& image) { - promise.set_value(std::move(image)); - }); - return promise.get_future().get(); -} - void checkRendering(Map& map, const char * name) { - PremultipliedImage actual = render(map); test::checkImage(std::string("test/fixtures/annotations/") + name + "/", - actual, 0.0002, 0.1); + test::render(map), 0.0002, 0.1); } TEST(Annotations, PointAnnotation) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); @@ -61,7 +40,7 @@ TEST(Annotations, PointAnnotation) { TEST(Annotations, LineAnnotation) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); @@ -80,7 +59,7 @@ TEST(Annotations, LineAnnotation) { TEST(Annotations, FillAnnotation) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); @@ -98,7 +77,7 @@ TEST(Annotations, FillAnnotation) { TEST(Annotations, StyleSourcedShapeAnnotation) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/annotation.json"), ""); @@ -113,14 +92,14 @@ TEST(Annotations, StyleSourcedShapeAnnotation) { TEST(Annotations, AddMultiple) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); map.addAnnotationIcon("default_marker", defaultMarker()); map.addPointAnnotation(PointAnnotation({ 0, -10 }, "default_marker")); - render(map); + test::render(map); map.addPointAnnotation(PointAnnotation({ 0, 10 }, "default_marker")); @@ -130,12 +109,12 @@ TEST(Annotations, AddMultiple) { TEST(Annotations, NonImmediateAdd) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); - render(map); + test::render(map); AnnotationSegments segments = {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }}; @@ -150,14 +129,14 @@ TEST(Annotations, NonImmediateAdd) { TEST(Annotations, RemovePoint) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); map.addAnnotationIcon("default_marker", defaultMarker()); uint32_t point = map.addPointAnnotation(PointAnnotation({ 0, 0 }, "default_marker")); - render(map); + test::render(map); map.removeAnnotation(point); @@ -167,7 +146,7 @@ TEST(Annotations, RemovePoint) { TEST(Annotations, RemoveShape) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); AnnotationSegments segments = {{ {{ { 0, 0 }, { 45, 45 } }} }}; @@ -179,7 +158,7 @@ TEST(Annotations, RemoveShape) { map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); uint32_t shape = map.addShapeAnnotation(ShapeAnnotation(segments, properties)); - render(map); + test::render(map); map.removeAnnotation(shape); @@ -189,26 +168,26 @@ TEST(Annotations, RemoveShape) { TEST(Annotations, ImmediateRemoveShape) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.removeAnnotation(map.addShapeAnnotation(ShapeAnnotation({}, {}))); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); - render(map); + test::render(map); } TEST(Annotations, SwitchStyle) { auto display = std::make_shared<mbgl::HeadlessDisplay>(); HeadlessView view(display, 1); - DefaultFileSource fileSource(nullptr, getFileSourceRoot()); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); Map map(view, fileSource, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); map.addAnnotationIcon("default_marker", defaultMarker()); map.addPointAnnotation(PointAnnotation({ 0, 0 }, "default_marker")); - render(map); + test::render(map); map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); diff --git a/test/api/custom_layer.cpp b/test/api/custom_layer.cpp new file mode 100644 index 0000000000..816a53dd35 --- /dev/null +++ b/test/api/custom_layer.cpp @@ -0,0 +1,83 @@ +#include "../fixtures/util.hpp" + +#include <mbgl/map/map.hpp> +#include <mbgl/platform/default/headless_display.hpp> +#include <mbgl/platform/default/headless_view.hpp> +#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/layer/custom_layer.hpp> +#include <mbgl/util/io.hpp> +#include <mbgl/util/mat4.hpp> + +using namespace mbgl; + +static const GLchar * vertexShaderSource = "attribute vec2 a_pos; void main() { gl_Position = vec4(a_pos, 0, 1); }"; +static const GLchar * fragmentShaderSource = "void main() { gl_FragColor = vec4(0, 1, 0, 1); }"; + +class TestLayer : public CustomLayer { +public: + TestLayer() : CustomLayer("custom") {} + TestLayer(const TestLayer& copy) : CustomLayer(copy) {} // Don't copy GL resources + + ~TestLayer() { + if (program) { + MBGL_CHECK_ERROR(glDeleteBuffers(1, &buffer)); + MBGL_CHECK_ERROR(glDetachShader(program, vertexShader)); + MBGL_CHECK_ERROR(glDetachShader(program, fragmentShader)); + MBGL_CHECK_ERROR(glDeleteShader(vertexShader)); + MBGL_CHECK_ERROR(glDeleteShader(fragmentShader)); + MBGL_CHECK_ERROR(glDeleteProgram(program)); + } + } + + void initialize() override { + program = MBGL_CHECK_ERROR(glCreateProgram()); + vertexShader = MBGL_CHECK_ERROR(glCreateShader(GL_VERTEX_SHADER)); + fragmentShader = MBGL_CHECK_ERROR(glCreateShader(GL_FRAGMENT_SHADER)); + + MBGL_CHECK_ERROR(glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr)); + MBGL_CHECK_ERROR(glCompileShader(vertexShader)); + MBGL_CHECK_ERROR(glAttachShader(program, vertexShader)); + MBGL_CHECK_ERROR(glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr)); + MBGL_CHECK_ERROR(glCompileShader(fragmentShader)); + MBGL_CHECK_ERROR(glAttachShader(program, fragmentShader)); + MBGL_CHECK_ERROR(glLinkProgram(program)); + a_pos = glGetAttribLocation(program, "a_pos"); + + GLfloat background[] = { -1,-1, 1,-1, -1,1, 1,1 }; + MBGL_CHECK_ERROR(glGenBuffers(1, &buffer)); + MBGL_CHECK_ERROR(glBindBuffer(GL_ARRAY_BUFFER, buffer)); + MBGL_CHECK_ERROR(glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), background, GL_STATIC_DRAW)); + } + + void render(const StyleRenderParameters&) const override { + MBGL_CHECK_ERROR(glUseProgram(program)); + MBGL_CHECK_ERROR(glBindBuffer(GL_ARRAY_BUFFER, buffer)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_FLOAT, GL_FALSE, 0, NULL)); + MBGL_CHECK_ERROR(glDisable(GL_STENCIL_TEST)); + MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); + MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); + } + + std::unique_ptr<StyleLayer> clone() const override { + return std::make_unique<TestLayer>(*this); + } + + GLuint program = 0; + GLuint vertexShader = 0; + GLuint fragmentShader = 0; + GLuint buffer = 0; + GLuint a_pos = 0; +}; + +TEST(CustomLayer, Basic) { + auto display = std::make_shared<mbgl::HeadlessDisplay>(); + HeadlessView view(display, 1); + DefaultFileSource fileSource(nullptr, test::getFileSourceRoot()); + + Map map(view, fileSource, MapMode::Still); + map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); + map.addLayer(std::make_unique<TestLayer>()); + + test::checkImage("test/fixtures/custom_layer/basic", test::render(map)); +} |