Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bind function to avoid texture copy #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions example-Basic/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void ofApp::setup(){

mainOutputSyphonServer.setName("Screen Output");
individualTextureSyphonServer.setName("Texture Output");
bindedTextureSyphonServer.setName("binded Texture Output");

mClient.setup();

Expand All @@ -34,7 +35,8 @@ void ofApp::update(){

//--------------------------------------------------------------
void ofApp::draw(){


if(bindedTextureSyphonServer.tryToBindForSize(800, 600)){
// Clear with alpha, so we can capture via syphon and composite elsewhere should we want.
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down Expand Up @@ -121,10 +123,22 @@ void ofApp::draw(){

mClient.draw(50, 50);

mainOutputSyphonServer.publishScreen();

individualTextureSyphonServer.publishTexture(&tex);




bindedTextureSyphonServer.unbindAndPublish();
}



individualTextureSyphonServer.publishTexture(&tex);


glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
bindedTextureSyphonServer.getCurrentTexture().draw(0,0);
mainOutputSyphonServer.publishScreen();

ofDrawBitmapString("Note this text is not captured by Syphon since it is drawn after publishing.\nYou can use this to hide your GUI for example.", 150,500);
}

Expand Down
1 change: 1 addition & 0 deletions example-Basic/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ofApp : public ofBaseApp{

ofxSyphonServer mainOutputSyphonServer;
ofxSyphonServer individualTextureSyphonServer;
ofxSyphonServer bindedTextureSyphonServer;

ofxSyphonClient mClient;
};
Expand Down
13 changes: 9 additions & 4 deletions src/ofxSyphonServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
#include "ofMain.h"

class ofxSyphonServer {
public:
public:
Copy link
Collaborator

Choose a reason for hiding this comment

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

avoid formatting-only changes please (also further down)

ofxSyphonServer();
~ofxSyphonServer();
void setName (string n);
string getName();
void publishScreen();
void publishTexture(ofTexture* inputTexture);
void publishTexture(GLuint id, GLenum target, GLsizei width, GLsizei height, bool isFlipped);
protected:
void publishTexture(ofTexture* inputTexture);
void publishTexture(GLuint id, GLenum target, GLsizei width, GLsizei height, bool isFlipped);
bool tryToBindForSize(int w,int h);
void unbindAndPublish();
ofTexture & getCurrentTexture();
void updateCurrentTexture();
protected:
ofTexture mTex;
void *mSyphon;
};
68 changes: 67 additions & 1 deletion src/ofxSyphonServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,70 @@
[(SyphonServer *)mSyphon publishFrameTexture:id textureTarget:target imageRegion:NSMakeRect(0, 0, width, height) textureDimensions:NSMakeSize(width, height) flipped:!isFlipped];
[pool drain];

}
}

bool ofxSyphonServer::tryToBindForSize(int w,int h){
bool res;
@autoreleasepool{

if (!mSyphon)
{
mSyphon = [[SyphonServer alloc] initWithName:@"Untitled" context:CGLGetCurrentContext() options:nil];
}

res = [(SyphonServer *)mSyphon bindToDrawFrameOfSize:NSMakeSize( w,h)];
if(res){
ofPushMatrix();
ofPushView();
ofViewport(0,0,w,h,false);
ofSetupScreenOrtho(w,h);

}
}

return res;
}

void ofxSyphonServer::unbindAndPublish(){
if (mSyphon){
@autoreleasepool{
[(SyphonServer *)mSyphon unbindAndPublish];

updateCurrentTexture();
ofPopView();
ofPopMatrix();
}
}
}

ofTexture & ofxSyphonServer::getCurrentTexture(){
return mTex;
}
void ofxSyphonServer::updateCurrentTexture(){

if(!mSyphon) return ;
@autoreleasepool{
SyphonImage * Img =[(SyphonServer *)mSyphon newFrameImage] ;
Copy link
Collaborator

@bangnoise bangnoise May 3, 2018

Choose a reason for hiding this comment

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

This is a leak - newFrameImage returns the image retained, and it needs to live for the lifetime of mTex (or until the next call to updateCurrentTexture()) but then be released

NSSize texSize = [(SyphonImage*)Img textureSize];
mTex.setUseExternalTextureID([Img textureName]);


mTex.texData.textureTarget = GL_TEXTURE_RECTANGLE_ARB; // Syphon always outputs rect textures.
mTex.texData.width = texSize.width;
mTex.texData.height = texSize.height;
mTex.texData.tex_w = texSize.width;
mTex.texData.tex_h = texSize.height;
mTex.texData.tex_t = texSize.width;
mTex.texData.tex_u = texSize.height;
mTex.texData.glInternalFormat = GL_RGBA;
#if (OF_VERSION_MAJOR == 0) && (OF_VERSION_MINOR < 8)
mTex.texData.glType = GL_RGBA;
mTex.texData.pixelType = GL_UNSIGNED_BYTE;
#endif
mTex.texData.bFlipTexture = YES;
mTex.texData.bAllocated = YES;


}

}