Skip to content

Commit

Permalink
Create EngineWrapper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Feb 21, 2024
1 parent bbcd3f7 commit d8ad127
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_library(
../cpp/SurfaceProvider.cpp
../cpp/Listener.cpp
../cpp/jsi/Promise.cpp
../cpp/core/EngineWrapper.cpp
# Java JNI
src/main/cpp/AndroidFilamentProxy.cpp
src/main/cpp/AndroidSurface.cpp
Expand Down
4 changes: 4 additions & 0 deletions package/cpp/Listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace margelo {

Listener::Listener(std::function<void()> remove) : _remove(std::move(remove)), _isRemoved(false) {}

Listener::~Listener() {
remove();
}

void Listener::remove() {
if (_isRemoved) {
return;
Expand Down
1 change: 1 addition & 0 deletions package/cpp/Listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace margelo {
class Listener {
public:
explicit Listener(std::function<void()> remove);
~Listener();
void remove();

private:
Expand Down
16 changes: 9 additions & 7 deletions package/cpp/SurfaceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ namespace margelo {
class SurfaceProvider {
public:
using TOnCreate = std::function<void(std::shared_ptr<Surface> surface)>;
using TOnChange = std::function<void(std::shared_ptr<Surface> surface, int width, int height)>;
using TOnResize = std::function<void(std::shared_ptr<Surface> surface, int width, int height)>;
using TOnDestroy = std::function<void(std::shared_ptr<Surface>)>;

struct Callback {
TOnCreate onSurfaceCreated;
TOnResize onSurfaceSizeChanged;
TOnDestroy onSurfaceDestroyed;
};

public:
Listener addOnSurfaceCreatedListener(TOnCreate callback);
Listener addOnSurfaceChangedListener(TOnChange callback);
Listener addOnSurfaceDestroyedListener(TOnDestroy callback);
Listener addOnSurfaceChangedListener(Callback callback);

virtual std::shared_ptr<Surface> getSurfaceOrNull() = 0;

Expand All @@ -35,9 +39,7 @@ class SurfaceProvider {
template <typename TListener> Listener addListenerToList(std::vector<TListener>& list, TListener listener);

private:
std::vector<TOnCreate> _onCreateListeners;
std::vector<TOnChange> _onChangeListeners;
std::vector<TOnDestroy> _onDestroyListeners;
std::vector<Callback> _callbacks;
std::mutex _mutex;
};

Expand Down
56 changes: 56 additions & 0 deletions package/cpp/core/EngineWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Created by Marc Rousavy on 21.02.24.
//

#include "EngineWrapper.h"

#include <filament/Engine.h>
#include <filament/SwapChain.h>

namespace margelo {

EngineWrapper::EngineWrapper() {
_engine = Engine::create(filament::Engine::Backend::OPENGL);
}

EngineWrapper::~EngineWrapper() {
if (_swapChain) {
_engine->destroy(_swapChain);
}
_engine->destroy(&_engine);
}

void EngineWrapper::setSurfaceProvider(std::shared_ptr<SurfaceProvider> surfaceProvider) {
_surfaceProvider = surfaceProvider;
std::shared_ptr<Surface> surface = surfaceProvider->getSurfaceOrNull();
if (surface != nullptr) {
setSurface(surface);
}

Listener listener = surfaceProvider->addOnSurfaceChangedListener(SurfaceProvider::Callback {
.onSurfaceCreated = [=] (std::shared_ptr<Surface> surface) {
this->setSurface(surface);
},
.onSurfaceDestroyed = [=] (std::shared_ptr<Surface> surface) {
this->destroySurface();
}
});
_listener = std::make_unique<Listener>(std::move(listener));
}

void EngineWrapper::setSurface(std::shared_ptr<Surface> surface) {
void* nativeWindow = surface->getSurface();
if (_swapChain == nullptr || _swapChain->getNativeWindow() != nativeWindow) {
destroySurface();
_swapChain = _engine->createSwapChain(nativeWindow);
}
}

void EngineWrapper::destroySurface() {
if (_swapChain != nullptr) {
_engine->destroy(_swapChain);
_swapChain = nullptr;
}
}

} // margelo
34 changes: 34 additions & 0 deletions package/cpp/core/EngineWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Marc Rousavy on 21.02.24.
//

#pragma once

#include <filament/Engine.h>
#include <filament/SwapChain.h>
#include "Surface.h"
#include "SurfaceProvider.h"

namespace margelo {

using namespace filament;

class EngineWrapper {
public:
explicit EngineWrapper();
~EngineWrapper();

void setSurfaceProvider(std::shared_ptr<SurfaceProvider> surfaceProvider);

private:
void setSurface(std::shared_ptr<Surface> surface);
void destroySurface();

private:
Engine* _engine;
SwapChain* _swapChain;
std::shared_ptr<SurfaceProvider> _surfaceProvider;
std::unique_ptr<Listener> _listener;
};

} // margelo

0 comments on commit d8ad127

Please sign in to comment.