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

Refactor GL context creation and headless rendering #6596

Merged
merged 2 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions benchmark/api/query.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <mbgl/benchmark/util.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_backend.hpp>
#include <mbgl/platform/default/offscreen_view.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense as a followup to rename Headless ⇢ Offscreen for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't rename this to have a difference in naming: Offscreen rendering doesn't necessarily imply headless rendering (and vice versa): We can use Offscreen views/texturs in regular rendering as well.

#include <mbgl/platform/default/thread_pool.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/storage/default_file_source.hpp>
Expand All @@ -29,17 +29,15 @@ class QueryBenchmark {
auto image = std::make_unique<SpriteImage>(std::move(decoded), 1.0);
map.addImage("test-icon", std::move(image));

view.resize(1000, 1000);

mbgl::benchmark::render(map);
mbgl::benchmark::render(map, view);
}

util::RunLoop loop;
std::shared_ptr<HeadlessDisplay> display{ std::make_shared<HeadlessDisplay>() };
HeadlessView view{ display, 1 };
HeadlessBackend backend;
OffscreenView view{ backend.getContext(), {{ 1000, 1000 }} };
DefaultFileSource fileSource{ "benchmark/fixtures/api/cache.db", "." };
ThreadPool threadPool{ 4 };
Map map{ view, fileSource, threadPool, MapMode::Still };
Map map{ backend, view.getSize(), 1, fileSource, threadPool, MapMode::Still };
ScreenBox box{{ 0, 0 }, { 1000, 1000 }};
};

Expand Down
8 changes: 5 additions & 3 deletions benchmark/src/mbgl/benchmark/util.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <mbgl/benchmark/util.hpp>
#include <mbgl/platform/default/offscreen_view.hpp>

#include <mbgl/map/map.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/run_loop.hpp>

namespace mbgl {
namespace benchmark {

void render(Map& map) {
void render(Map& map, OffscreenView& view) {
PremultipliedImage result;
map.renderStill([&result](std::exception_ptr, PremultipliedImage&& image) {
result = std::move(image);
map.renderStill(view, [&](std::exception_ptr) {
result = view.readStillImage();
});

while (!result.size()) {
Expand Down
3 changes: 2 additions & 1 deletion benchmark/src/mbgl/benchmark/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace mbgl {

class Map;
class OffscreenView;

namespace benchmark {

void render(Map&);
void render(Map&, OffscreenView&);

} // namespace benchmark
} // namespace mbgl
11 changes: 8 additions & 3 deletions bin/glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <array>

namespace {

std::unique_ptr<GLFWView> view;
GLFWView* view = nullptr;

}

Expand Down Expand Up @@ -104,7 +105,8 @@ int main(int argc, char *argv[]) {
mbgl::Log::Info(mbgl::Event::General, "BENCHMARK MODE: Some optimizations are disabled.");
}

view = std::make_unique<GLFWView>(fullscreen, benchmark);
GLFWView backend(fullscreen, benchmark);
view = &backend;

mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db", ".");

Expand All @@ -118,7 +120,9 @@ int main(int argc, char *argv[]) {

mbgl::ThreadPool threadPool(4);

mbgl::Map map(*view, fileSource, threadPool);
mbgl::Map map(backend, view->getSize(), view->getPixelRatio(), fileSource, threadPool);

backend.setMap(&map);

// Load settings
mbgl::Settings_JSON settings;
Expand Down Expand Up @@ -181,5 +185,6 @@ int main(int argc, char *argv[]) {
"Exit location: --lat=\"%f\" --lon=\"%f\" --zoom=\"%f\" --bearing \"%f\"",
settings.latitude, settings.longitude, settings.zoom, settings.bearing);

view = nullptr;
return 0;
}
14 changes: 7 additions & 7 deletions bin/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>

#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_backend.hpp>
#include <mbgl/platform/default/offscreen_view.hpp>
#include <mbgl/platform/default/thread_pool.hpp>
#include <mbgl/storage/default_file_source.hpp>

Expand All @@ -29,7 +29,6 @@ int main(int argc, char *argv[]) {

int width = 512;
int height = 512;
double pixelRatio = 1.0;
static std::string output = "out.png";
std::string cache_file = "cache.sqlite";
std::string asset_root = ".";
Expand Down Expand Up @@ -84,9 +83,10 @@ int main(int argc, char *argv[]) {
fileSource.setAccessToken(std::string(token));
}

HeadlessView view(pixelRatio, width, height);
HeadlessBackend backend;
OffscreenView view(backend.getContext(), {{ static_cast<uint16_t>(width), static_cast<uint16_t>(height) }});
ThreadPool threadPool(4);
Map map(view, fileSource, threadPool, MapMode::Still);
Map map(backend, view.getSize(), 1, fileSource, threadPool, MapMode::Still);

map.setStyleJSON(style);
map.setClasses(classes);
Expand All @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) {
map.setDebug(debug ? mbgl::MapDebugOptions::TileBorders | mbgl::MapDebugOptions::ParseStatus : mbgl::MapDebugOptions::NoDebug);
}

map.renderStill([&](std::exception_ptr error, PremultipliedImage&& image) {
map.renderStill(view, [&](std::exception_ptr error) {
try {
if (error) {
std::rethrow_exception(error);
Expand All @@ -109,7 +109,7 @@ int main(int argc, char *argv[]) {
exit(1);
}

util::write_file(output, encodePNG(image));
util::write_file(output, encodePNG(view.readStillImage()));
loop.stop();
});

Expand Down
12 changes: 9 additions & 3 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ set(MBGL_CORE_FILES

# gl
include/mbgl/gl/gl.hpp
include/mbgl/gl/implementation.hpp
src/mbgl/gl/attribute.hpp
src/mbgl/gl/context.cpp
src/mbgl/gl/context.hpp
src/mbgl/gl/debugging.cpp
src/mbgl/gl/debugging.hpp
src/mbgl/gl/extension.cpp
src/mbgl/gl/extension.hpp
src/mbgl/gl/framebuffer.hpp
src/mbgl/gl/gl.cpp
src/mbgl/gl/index_buffer.hpp
src/mbgl/gl/object.cpp
src/mbgl/gl/object.hpp
src/mbgl/gl/renderbuffer.hpp
src/mbgl/gl/shader.cpp
src/mbgl/gl/shader.hpp
src/mbgl/gl/state.hpp
Expand Down Expand Up @@ -92,18 +95,19 @@ set(MBGL_CORE_FILES
src/mbgl/layout/symbol_layout.hpp

# map
include/mbgl/map/backend.hpp
include/mbgl/map/camera.hpp
include/mbgl/map/map.hpp
include/mbgl/map/mode.hpp
include/mbgl/map/update.hpp
include/mbgl/map/view.hpp
src/mbgl/map/backend.cpp
src/mbgl/map/change.hpp
src/mbgl/map/map.cpp
src/mbgl/map/transform.cpp
src/mbgl/map/transform.hpp
src/mbgl/map/transform_state.cpp
src/mbgl/map/transform_state.hpp
src/mbgl/map/view.cpp
src/mbgl/map/update.hpp
src/mbgl/map/zoom_history.hpp

# math
Expand All @@ -130,9 +134,11 @@ set(MBGL_CORE_FILES

# platform/default
include/mbgl/platform/default/glfw_view.hpp
include/mbgl/platform/default/headless_backend.hpp
include/mbgl/platform/default/headless_display.hpp
include/mbgl/platform/default/headless_view.hpp
include/mbgl/platform/default/offscreen_view.hpp
include/mbgl/platform/default/settings_json.hpp
include/mbgl/platform/default/thread_pool.hpp

# renderer
src/mbgl/renderer/bucket.hpp
Expand Down
2 changes: 1 addition & 1 deletion cmake/core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ target_include_directories(mbgl-core

target_add_mason_package(mbgl-core PUBLIC geometry)
target_add_mason_package(mbgl-core PUBLIC variant)
target_add_mason_package(mbgl-core PRIVATE unique_resource)
target_add_mason_package(mbgl-core PUBLIC unique_resource)
target_add_mason_package(mbgl-core PRIVATE rapidjson)
target_add_mason_package(mbgl-core PRIVATE boost)
target_add_mason_package(mbgl-core PRIVATE geojson)
Expand Down
14 changes: 14 additions & 0 deletions include/mbgl/gl/implementation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#if defined(__QT__)
#define MBGL_USE_QT 1
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IOS
#define MBGL_USE_EAGL 1
#else
#define MBGL_USE_CGL 1
#endif
#else
#define MBGL_USE_GLX 1
#endif
45 changes: 45 additions & 0 deletions include/mbgl/map/backend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <mbgl/map/change.hpp>

#include <memory>

namespace mbgl {

namespace gl {
class Context;
} // namespace gl

class Backend {
public:
Backend();
virtual ~Backend();

// Returns the backend's context which manages OpenGL state.
gl::Context& getContext();

// Called when the backend's GL context needs to be made active or inactive. These are called,
// as a matched pair, in four situations:
//
// 1. When releasing GL resources during Map destruction
// 2. When calling a CustomLayerInitializeFunction, during Map::addLayer
// 3. When calling a CustomLayerDeinitializeFunction, during Map::removeLayer
// 4. When rendering for Map::renderStill
//
// They are *not* called for Map::render; it is assumed that the correct context is already
// activated prior to calling Map::render.
virtual void activate() = 0;
virtual void deactivate() = 0;

// Called when the map needs to be rendered; the backend should call Map::render() at some point
// in the near future. (Not called for Map::renderStill() mode.)
virtual void invalidate() = 0;

// Notifies a watcher of map x/y/scale/rotation changes.
virtual void notifyMapChange(MapChange change);

private:
const std::unique_ptr<gl::Context> context;
};

} // namespace mbgl
22 changes: 13 additions & 9 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <mbgl/util/optional.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/map/update.hpp>
#include <mbgl/map/mode.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/feature.hpp>
Expand All @@ -19,6 +17,7 @@

namespace mbgl {

class Backend;
class View;
class FileSource;
class Scheduler;
Expand All @@ -33,7 +32,11 @@ class Layer;

class Map : private util::noncopyable {
public:
explicit Map(View&, FileSource&, Scheduler&,
explicit Map(Backend&,
std::array<uint16_t, 2> size,
float pixelRatio,
FileSource&,
Scheduler&,
MapMode mapMode = MapMode::Continuous,
GLContextMode contextMode = GLContextMode::Unique,
ConstrainMode constrainMode = ConstrainMode::HeightOnly,
Expand All @@ -42,14 +45,14 @@ class Map : private util::noncopyable {

// Register a callback that will get called (on the render thread) when all resources have
// been loaded and a complete render occurs.
using StillImageCallback = std::function<void (std::exception_ptr, PremultipliedImage&&)>;
void renderStill(StillImageCallback callback);
using StillImageCallback = std::function<void (std::exception_ptr)>;
void renderStill(View&, StillImageCallback callback);

// Main render function.
void render();
// Triggers a repaint.
void triggerRepaint();

// Notifies the Map that the state has changed and an update might be necessary.
void update(Update update);
// Main render function.
void render(View&);

// Styling
void addClass(const std::string&);
Expand Down Expand Up @@ -133,6 +136,7 @@ class Map : private util::noncopyable {
ViewportMode getViewportMode() const;

// Size
void setSize(const std::array<uint16_t, 2>&);
uint16_t getWidth() const;
uint16_t getHeight() const;

Expand Down
Loading