Skip to content

Commit

Permalink
Benchmark app frame time improvements (#1776)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
alexcristici and pre-commit-ci[bot] authored Nov 3, 2023
1 parent 827ced7 commit 8ad5f9c
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 102 deletions.
3 changes: 2 additions & 1 deletion include/mbgl/map/map_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class MapObserver {
RenderMode mode;
bool needsRepaint; // In continous mode, shows that there are ongoig transitions.
bool placementChanged;
double frameTime;
double frameEncodingTime;
double frameRenderingTime;
};

virtual void onCameraWillChange(CameraChangeMode) {}
Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/renderer/renderer_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ class RendererObserver {
virtual void onDidFinishRenderingFrame(RenderMode, bool /*repaint*/, bool /*placementChanged*/) {}

/// End of frame, booleans flags that a repaint is required and that placement changed.
virtual void onDidFinishRenderingFrame(RenderMode mode, bool repaint, bool placementChanged, double /*frameTime*/) {
virtual void onDidFinishRenderingFrame(RenderMode mode,
bool repaint,
bool placementChanged,
double /*frameEncodingTime*/,
double /*frameRenderingTime*/) {
onDidFinishRenderingFrame(mode, repaint, placementChanged);
}

Expand Down
2 changes: 1 addition & 1 deletion platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ objc_library(

objc_library(
name = "ios-benchapp",
copts = MAPLIBRE_FLAGS,
copts = CPP_FLAGS + MAPLIBRE_FLAGS,
srcs = ["//platform/ios/benchmark:ios_benchmark_srcs"],
hdrs = ["//platform/ios/benchmark:ios_benchmark_hdrs"],
includes = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class ForwardingRendererObserver : public RendererObserver {
void onDidFinishRenderingFrame(RenderMode mode,
bool repaintNeeded,
bool placementChanged,
double frameTime) override {
void (RendererObserver::*f)(RenderMode, bool, bool, double) = &RendererObserver::onDidFinishRenderingFrame;
delegate.invoke(f, mode, repaintNeeded, placementChanged, frameTime);
double frameEncodingTime,
double frameRenderingTime) override {
void (RendererObserver::*f)(
RenderMode, bool, bool, double, double) = &RendererObserver::onDidFinishRenderingFrame;
delegate.invoke(f, mode, repaintNeeded, placementChanged, frameEncodingTime, frameRenderingTime);
}

void onDidFinishRenderingMap() override { delegate.invoke(&RendererObserver::onDidFinishRenderingMap); }
Expand Down
5 changes: 4 additions & 1 deletion platform/default/include/mbgl/gfx/headless_frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class HeadlessFrontend : public RendererFrontend {
float pixelRatio_,
gfx::HeadlessBackend::SwapBehaviour swapBehavior = gfx::HeadlessBackend::SwapBehaviour::NoFlush,
gfx::ContextMode mode = gfx::ContextMode::Unique,
const std::optional<std::string>& localFontFamily = std::nullopt);
const std::optional<std::string>& localFontFamily = std::nullopt,
bool invalidateOnUpdate_ = true);
~HeadlessFrontend() override;

void reset() override;
Expand All @@ -56,6 +57,7 @@ class HeadlessFrontend : public RendererFrontend {
PremultipliedImage readStillImage();
RenderResult render(Map&);
void renderOnce(Map&);
void renderFrame();

std::optional<TransformState> getTransformState() const;

Expand All @@ -66,6 +68,7 @@ class HeadlessFrontend : public RendererFrontend {
std::atomic<double> frameTime;
std::unique_ptr<gfx::HeadlessBackend> backend;
util::AsyncTask asyncInvalidate;
bool invalidateOnUpdate;

std::unique_ptr<Renderer> renderer;
std::shared_ptr<UpdateParameters> updateParameters;
Expand Down
44 changes: 25 additions & 19 deletions platform/default/src/mbgl/gfx/headless_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,17 @@ HeadlessFrontend::HeadlessFrontend(Size size_,
float pixelRatio_,
gfx::HeadlessBackend::SwapBehaviour swapBehavior,
const gfx::ContextMode contextMode,
const std::optional<std::string>& localFontFamily)
const std::optional<std::string>& localFontFamily,
bool invalidateOnUpdate_)
: size(size_),
pixelRatio(pixelRatio_),
frameTime(0),
backend(gfx::HeadlessBackend::Create(
{static_cast<uint32_t>(size.width * pixelRatio), static_cast<uint32_t>(size.height * pixelRatio)},
swapBehavior,
contextMode)),
asyncInvalidate([this] {
if (renderer && updateParameters) {
auto startTime = mbgl::util::MonotonicTimer::now();
gfx::BackendScope guard{*getBackend()};

// onStyleImageMissing might be called during a render. The user
// implemented method could trigger a call to
// MLNRenderFrontend#update which overwrites `updateParameters`.
// Copy the shared pointer here so that the parameters aren't
// destroyed while `render(...)` is still using them.
auto updateParameters_ = updateParameters;
renderer->render(updateParameters_);

auto endTime = mbgl::util::MonotonicTimer::now();
frameTime = (endTime - startTime).count();
}
}),
asyncInvalidate([this] { renderFrame(); }),
invalidateOnUpdate(invalidateOnUpdate_),
renderer(std::make_unique<Renderer>(*getBackend(), pixelRatio, localFontFamily)) {}

HeadlessFrontend::~HeadlessFrontend() = default;
Expand All @@ -57,7 +43,9 @@ void HeadlessFrontend::reset() {

void HeadlessFrontend::update(std::shared_ptr<UpdateParameters> updateParameters_) {
updateParameters = updateParameters_;
asyncInvalidate.send();
if (invalidateOnUpdate) {
asyncInvalidate.send();
}
}

void HeadlessFrontend::setObserver(RendererObserver& observer_) {
Expand Down Expand Up @@ -169,6 +157,24 @@ void HeadlessFrontend::renderOnce(Map&) {
util::RunLoop::Get()->runOnce();
}

void HeadlessFrontend::renderFrame() {
if (renderer && updateParameters) {
auto startTime = mbgl::util::MonotonicTimer::now();
gfx::BackendScope guard{*getBackend()};

// onStyleImageMissing might be called during a render. The user
// implemented method could trigger a call to
// MLNRenderFrontend#update which overwrites `updateParameters`.
// Copy the shared pointer here so that the parameters aren't
// destroyed while `render(...)` is still using them.
auto updateParameters_ = updateParameters;
renderer->render(updateParameters_);

auto endTime = mbgl::util::MonotonicTimer::now();
frameTime = (endTime - startTime).count();
}
}

std::optional<TransformState> HeadlessFrontend::getTransformState() const {
if (updateParameters) {
return updateParameters->transformState;
Expand Down
14 changes: 9 additions & 5 deletions platform/default/src/mbgl/map/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class ForwardingRendererObserver final : public RendererObserver {
void onDidFinishRenderingFrame(RenderMode mode,
bool repaintNeeded,
bool placementChanged,
double frameTime) override {
void (RendererObserver::*f)(RenderMode, bool, bool, double) = &RendererObserver::onDidFinishRenderingFrame;
delegate.invoke(f, mode, repaintNeeded, placementChanged, frameTime);
double frameEncodingTime,
double frameRenderingTime) override {
void (RendererObserver::*f)(
RenderMode, bool, bool, double, double) = &RendererObserver::onDidFinishRenderingFrame;
delegate.invoke(f, mode, repaintNeeded, placementChanged, frameEncodingTime, frameRenderingTime);
}

void onStyleImageMissing(const std::string& image, const StyleImageMissingCallback& cb) override {
Expand Down Expand Up @@ -84,11 +86,13 @@ class SnapshotterRenderer final : public RendererObserver {
void onDidFinishRenderingFrame(RenderMode mode,
bool repaintNeeded,
bool placementChanged,
double frameTime) override {
double frameEncodingTime,
double frameRenderingTime) override {
if (mode == RenderMode::Full && hasPendingStillImageRequest) {
stillImage = frontend.readStillImage();
}
rendererObserver->onDidFinishRenderingFrame(mode, repaintNeeded, placementChanged, frameTime);
rendererObserver->onDidFinishRenderingFrame(
mode, repaintNeeded, placementChanged, frameEncodingTime, frameRenderingTime);
}

void onStyleImageMissing(const std::string& id, const StyleImageMissingCallback& done) override {
Expand Down
4 changes: 2 additions & 2 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2478,9 +2478,9 @@ - (void)updateHUD {
return features;
}

- (void)mapViewDidFinishRenderingFrame:(MLNMapView *)mapView fullyRendered:(BOOL)fullyRendered frameTime:(double)frameTime {
- (void)mapViewDidFinishRenderingFrame:(MLNMapView *)mapView fullyRendered:(BOOL)fullyRendered frameEncodingTime:(double)frameEncodingTime frameRenderingTime:(double)frameRenderingTime {
if (self.frameTimeGraphEnabled) {
[self.frameTimeGraphView updatePathWithFrameDuration:frameTime];
[self.frameTimeGraphView updatePathWithFrameDuration:frameEncodingTime];
}
}

Expand Down
4 changes: 2 additions & 2 deletions platform/ios/benchmark/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ load("//platform/ios/bazel:macros.bzl", "info_plist")
filegroup(
name = "ios_benchmark_srcs",
srcs = [
"main.m",
"MBXBenchAppDelegate.m",
"main.mm",
"MBXBenchAppDelegate.mm",
"MBXBenchViewController.mm",
"locations.cpp",
],
Expand Down
Loading

0 comments on commit 8ad5f9c

Please sign in to comment.