Skip to content

Commit

Permalink
Added shared texture dispose mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
redrezo committed Apr 9, 2019
1 parent 2c0b989 commit 63211f8
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 28 deletions.
1 change: 1 addition & 0 deletions native/DriftFX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(DriftFX-Headers
set(DriftFX-Sources
src/Context.cpp
src/JNINativeSurface.cpp
src/NativeSurface.h
src/NativeSurface.cpp
src/NativeSurfaceAPI.cpp
src/NativeSurfaceRegistry.cpp
Expand Down
4 changes: 2 additions & 2 deletions native/DriftFX/src/JNINativeSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void JNINativeSurface::Initialize() {

jFrameDataClass = ResolveClass(env, "org/eclipse/fx/drift/internal/JNINativeSurface$FrameData");
jNativeSurface_Present2Method = ResolveMethod(env, jNativeSurfaceClass, "present", "(Lorg/eclipse/fx/drift/internal/JNINativeSurface$FrameData;)V");
jNativeSurface_Present3Method = ResolveMethod(env, jNativeSurfaceClass, "present", "(JJIII)V");
jNativeSurface_Present3Method = ResolveMethod(env, jNativeSurfaceClass, "present", "(JJJIII)V");

jFrameData_d3dShareHandleField = ResolveField(env, jFrameDataClass, "d3dShareHandle", "J");
jFrameData_widthField = ResolveField(env, jFrameDataClass, "width", "I");
Expand Down Expand Up @@ -95,7 +95,7 @@ void JNINativeSurface::Present(FrameData frameData) {
LogDebug("going to call present")
JNIEnv *env = JNIHelper::GetJNIEnv(true);

env->CallVoidMethod(jNativeSurfaceInstance, jNativeSurface_Present3Method, frameData.d3dSharedHandle, frameData.ioSurfaceHandle, frameData.glTextureName, frameData.width, frameData.height);
env->CallVoidMethod(jNativeSurfaceInstance, jNativeSurface_Present3Method, frameData.id, frameData.d3dSharedHandle, frameData.ioSurfaceHandle, frameData.glTextureName, frameData.width, frameData.height);
// LogDebug("got jni env: " << env)
//
// jobject instance = env->NewObject(jFrameDataClass, jFrameData_constructor);
Expand Down
43 changes: 23 additions & 20 deletions native/DriftFX/src/NativeSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ using namespace driftfx::internal::prism;

NativeSurface::NativeSurface(JNINativeSurface* api) :
api(api),
context(nullptr),
lastPresented(nullptr),
toDispose(nullptr) {
context(nullptr) {
LogDebug("NativeSurface constructor")

}
Expand All @@ -43,14 +41,30 @@ void NativeSurface::Initialize() {
context = PrismBridge::Get()->GetDefaultContext()->CreateSharedContext();
}

void NativeSurface::Cleanup() {
void NativeSurface::DisposeSharedTexture(long long id) {
toDisposeMutex.lock();

SharedTexture* texture = (SharedTexture*) id;
toDispose.push_back(texture);

LogDebug("clean textures");
if (toDispose != nullptr) {
delete toDispose;
toDispose = nullptr;
toDisposeMutex.unlock();
}

void NativeSurface::DisposeSharedTextures() {
LogDebug("Disposing shared textures");
toDisposeMutex.lock();
for (std::vector<SharedTexture*>::iterator it = toDispose.begin(); it != toDispose.end(); ++it) {
SharedTexture* tex = (*it);
LogDebug(" - " << tex);
delete tex;
}
toDispose.clear();
toDisposeMutex.unlock();
}

void NativeSurface::Cleanup() {
LogDebug("clean textures");
DisposeSharedTextures();

// // TODO send some kind of signal to tell FX we are going to dispose our textures
FrameData* frameData = new FrameData();
Expand All @@ -66,11 +80,6 @@ void NativeSurface::Cleanup() {
// NOTE: since textures know their context and set it current upon deletion
// we must ensure that all textures from a context are deleted before the context is deleted!

if (lastPresented != nullptr) {
delete lastPresented;
lastPresented = nullptr;
}

LogDebug("clean GLContext");
delete context;
context = nullptr;
Expand Down Expand Up @@ -100,10 +109,7 @@ RenderTarget* NativeSurface::Acquire() {

RenderTarget* NativeSurface::Acquire(unsigned int width, unsigned int height) {
LogDebug("acquire0");
if (toDispose != nullptr) {
delete toDispose;
toDispose = nullptr;
}
DisposeSharedTextures();

PrismBridge* bridge = PrismBridge::Get();
// in case the system was destroyed
Expand Down Expand Up @@ -139,9 +145,6 @@ void NativeSurface::Present(RenderTarget* target, PresentationHint hint) {
api->Present(*frameData);

delete frameData;

toDispose = lastPresented;
lastPresented = texture;
}

Context* NativeSurface::GetFxContext() {
Expand Down
13 changes: 11 additions & 2 deletions native/DriftFX/src/NativeSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <DriftFX/DriftFXSurface.h>
#include <DriftFX/GL/GLContext.h>

#include <vector>
#include <mutex>

#include "JNINativeSurface.h"

namespace driftfx {
Expand Down Expand Up @@ -79,6 +82,8 @@ class NativeSurface : public DriftFXSurface {
*/
virtual void UpdateSize(int width, int height);

virtual void DisposeSharedTexture(long long id);

static NativeSurface* Create(JNINativeSurface* api);

virtual Context* GetFxContext();
Expand All @@ -93,10 +98,14 @@ class NativeSurface : public DriftFXSurface {
volatile unsigned int width;
volatile unsigned int height;



private:

SharedTexture* lastPresented;
SharedTexture* toDispose;
std::mutex toDisposeMutex;
std::vector<SharedTexture*> toDispose;

void DisposeSharedTextures();
};

}
Expand Down
7 changes: 7 additions & 0 deletions native/DriftFX/src/NativeSurfaceAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ extern "C" JNIEXPORT void JNICALL Java_org_eclipse_fx_drift_internal_NativeAPI_n
}


extern "C" JNIEXPORT void JNICALL Java_org_eclipse_fx_drift_internal_NativeAPI_nDisposeFrameData(JNIEnv* env, jclass cls, jlong surfaceId, jlong frameDataId) {
LogDebug("dispose frame data " << surfaceId << ": " << frameDataId);
NativeSurface* surface = NativeSurfaceRegistry::Get()->Get((long) surfaceId);
surface->DisposeSharedTexture((long long) frameDataId);
}


5 changes: 3 additions & 2 deletions native/DriftFX/src/SharedTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ using namespace gl;
namespace internal {

struct FrameData {
long d3dSharedHandle;
long ioSurfaceHandle;
long long id;
long long d3dSharedHandle;
long long ioSurfaceHandle;
int glTextureName;
int width;
int height;
Expand Down
3 changes: 2 additions & 1 deletion native/DriftFX/src/prism/d3d/D3DSharedTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ bool D3DSharedTexture::Unlock() {

FrameData* D3DSharedTexture::CreateFrameData() {
FrameData* data = new FrameData();
data->d3dSharedHandle = (long) (long long) d3dTexture->GetShareHandle();
data->id = (long long) this;
data->d3dSharedHandle = (long long) d3dTexture->GetShareHandle();
data->height = GetHeight();
data->width = GetWidth();
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ bool IOSurfaceSharedTexture::Unlock() {
FrameData* IOSurfaceSharedTexture::CreateFrameData() {

FrameData* data = new FrameData();
data->id = (long long) this;
data->width = GetWidth();
data->height = GetHeight();
data->glTextureName = fxTexture->Name();
Expand Down
1 change: 1 addition & 0 deletions native/DriftFX/src/prism/es2/glx/GLXSharedTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ FrameData* GLXSharedTexture::CreateFrameData() {


FrameData* data = new FrameData();
data->id = (long long) this;
data->width = GetWidth();
data->height = GetHeight();
data->glTextureName = glTexture->Name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ public class NGDriftFXSurface extends NGNode {
private Texture currentTexture;

public void present(FrameData frame) {
FrameData oldData = currentFrameData;
currentFrameData = frame;
if (oldData != null) {
dispose(oldData);
}
}

private void dispose(FrameData frame) {
NativeAPI.disposeFrameData(nativeSurfaceHandle, frame);
}

public NGDriftFXSurface(DriftFXSurface node, long nativeSurfaceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
public class JNINativeSurface {

public static class FrameData {
public long frameId;

public long d3dShareHandle;
public long ioSurfaceHandle;
public int width;
Expand Down Expand Up @@ -69,8 +71,9 @@ public JNINativeSurface(Consumer<FrameData> presentFrame) {
this.presentFrame = presentFrame;
}

public void present(long handle, long ioSurfaceHandle, int textureName, int width, int height) {
public void present(long id, long handle, long ioSurfaceHandle, int textureName, int width, int height) {
FrameData dat = new FrameData();
dat.frameId = id;
dat.d3dShareHandle = handle;
dat.ioSurfaceHandle = ioSurfaceHandle;
dat.textureName = textureName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.fx.drift.internal;

import org.eclipse.fx.drift.internal.JNINativeSurface.FrameData;

import com.sun.prism.Texture;

//Note: this implementation is against internal JavafX API
Expand Down Expand Up @@ -81,6 +83,11 @@ public static void Destroy() {
nDestroy();
}

private static native void nDisposeFrameData(long nativeSurfaceHandle, long frameDataId);
public static void disposeFrameData(long nativeSurfaceHandle, FrameData frameData) {
nDisposeFrameData(nativeSurfaceHandle, frameData.frameId);
}

private static native long nCreateNativeSurface(JNINativeSurface surface);
public static long createNativeSurface(JNINativeSurface surface) {
return nCreateNativeSurface(surface);
Expand Down

0 comments on commit 63211f8

Please sign in to comment.