Skip to content

Commit

Permalink
Linux version is now working again, including the SimpleVideoFilter s…
Browse files Browse the repository at this point in the history
…ample application. This time, full Swift Package Manager support is available.
  • Loading branch information
BradLarson committed Jun 8, 2019
1 parent bf19a4e commit ffd7b5a
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Exclude the build directory
build/*
examples/FilterShowcase/build*
examples/Linux-OpenGL/SimpleVideoFilter/.build
.build
.swiftpm

# Exclude temp nibs and swap files
*~.nib
Expand Down
36 changes: 27 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swift-tools-version:4.2

import PackageDescription

#if os(macOS)
let platformDepedencies: [Package.Dependency] = []
let platformExcludes = ["iOS", "Linux", "Operations/Shaders"]
let platformTargets: [Target] = [
.target(
name: "GPUImage",
path: "framework/Source",
exclude: platformExcludes)]
#elseif os(iOS)
let platformDepedencies: [Package.Dependency] = []
let platformExcludes = ["Linux", "Mac", "Operations/Shaders"]
let platformTargets: [Target] = [
.target(
name: "GPUImage",
path: "framework/Source",
exclude: platformExcludes)]
#elseif os(Linux)
// TODO: Add back in RPi support
let platformDepedencies: [Package.Dependency] = [.package(url: "https://github.com/BradLarson/COpenGL.git", from: "1.0.2"), .package(url: "https://github.com/BradLarson/CFreeGLUT.git", from: "1.0.1"), .package(url: "https://github.com/BradLarson/CVideo4Linux.git", from: "1.0.2")]
// TODO: Move the remote system library packages into this project
let platformDepedencies: [Package.Dependency] = [
.package(url: "https://github.com/BradLarson/COpenGL.git", from: "1.0.2"),
.package(url: "https://github.com/BradLarson/CFreeGLUT.git", from: "1.0.1"),
.package(url: "https://github.com/BradLarson/CVideo4Linux.git", from: "1.0.2")]
let platformExcludes = ["iOS", "Mac", "Operations/Shaders", "Linux/RPiRenderWindow.swift", "Linux/OpenGLContext-RPi.swift", "Linux/V4LSupplement"]
let platformTargets: [Target] = [
.target(
name: "V4LSupplement",
path: "framework/Source/Linux/V4LSupplement"),
.target(
name: "GPUImage",
dependencies: ["V4LSupplement"],
path: "framework/Source",
exclude: platformExcludes)]
#endif


Expand All @@ -24,10 +46,6 @@ let package = Package(
targets: ["GPUImage"]),
],
dependencies: platformDepedencies,
targets: [
.target(
name: "GPUImage",
path: "framework/Source",
exclude: platformExcludes),
]
targets: platformTargets,
swiftLanguageVersions: [.v4]
)
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ Note that you may need to build your project once to parse and build the GPUImag

## Using GPUImage in a Linux application ##

Eventually, this project will support the Swift Package Manager, which will make it trivial to use with a Linux project. Unfortunately, that's not yet the case, so it can take a little work to get this to build for a Linux project.
This project supports the Swift Package Manager, so you should be able to add it as a dependency in your Package.swift file like the following:

Right now, there are two build scripts in the framework directory, one named compile-LinuxGL.sh and one named compile-RPi.sh. The former builds the framework for a Linux target using OpenGL and the latter builds for the Raspberry Pi. I can add other targets as I test them, but I've only gotten this operational in desktop Ubuntu, on Ubuntu running on a Jetson TK1 development board, and on Raspbian running on a Raspberry Pi 2 and Pi 3.
```
.package(url: "https://github.com/BradLarson/GPUImage2.git", from: "0.0.1"),
```

Before compiling the framework, you'll need to get Swift up and running on your system. For desktop Ubuntu installs, you can follow Apple's guidelines on <a href="https://swift.org/download/">their Downloads page</a>. Those instructions also worked for me on the Jetson TK1 dev board.
along with an

```
import GPUImage
```

For ARM Linux devices like the Raspberry Pi, follow <a href="http://dev.iachieved.it/iachievedit/open-source-swift-on-raspberry-pi-2/">these steps exactly</a> to get a working Swift compiler installed. Pay close attention to the steps for getting Clang-2.6 installed and the use of update-alternatives. These are the steps I used to go from stock Raspbian to a Swift install on
in your application code.

I have noticed that Swift 2.2 compiler snapshots newer than January 11 or so are missing Foundation, which I need for the framework, so maybe go with a snaphot earlier than that.
Before compiling the framework, you'll need to get Swift up and running on your system. For desktop Ubuntu installs, you can follow Apple's guidelines on <a href="https://swift.org/download/">their Downloads page</a>.

After Swift, you'll need to install Video4Linux to get access to standard USB webcams as inputs:

Expand All @@ -76,13 +82,15 @@ On the Raspberry Pi, you'll need to make sure that the Broadcom Videocore header
sudo apt-get install libraspberrypi-dev
```

For desktop Linux and other OpenGL devices (Jetson TK1), you'll need to make sure GLUT and the OpenGL headers are installed. The framework currently uses GLUT for its output. GLUT can be used on the Raspberry Pi via the new experimental OpenGL support there, but I've found that it's significantly slower than using the OpenGL ES APIs and the Videocore interface that ships with the Pi. Also, if you enable the OpenGL support you currently lock yourself out of using the Videocore interface.
For desktop Linux and other OpenGL devices (Jetson family), you'll need to make sure GLUT and the OpenGL headers are installed. The framework currently uses GLUT for its output. GLUT can be used on the Raspberry Pi via the new experimental OpenGL support there, but I've found that it's significantly slower than using the OpenGL ES APIs and the Videocore interface that ships with the Pi. Also, if you enable the OpenGL support you currently lock yourself out of using the Videocore interface.

Once all of that is set up, to build the framework go to the /framework directory and run the appropriate build script. This will compile and generate a Swift module and a shared library for the framework. Copy the shared library into a system-accessible library path, like /usr/lib.
Once all of that is set up, you can use

To build any of the sample applications, go to the examples/ subdirectory for that example (examples are platform-specific) and run the compile.sh build script to compile the example. The framework must be built before any example application.
```
swift build
```

As Swift becomes incorporated into more platforms, and as I add support for the Swift Package Manager, these Linux build steps will become much easier. My setup is kind of a hack at present.
in the main GPUImage directory to build the framework, or do the same in the examples/Linux-OpenGL/SimpleVideoFilter directory. This will build a sample application that filters live video from a USB camera and displays the results in real time to the screen. The application itself will be contained within the .build directory and its platform-specific subdirectories. Look for the SimpleVideoFilter binary and run that.

## Performing common tasks ##

Expand Down
4 changes: 4 additions & 0 deletions examples/Linux-OpenGL/SimpleVideoFilter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
34 changes: 34 additions & 0 deletions examples/Linux-OpenGL/SimpleVideoFilter/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"object": {
"pins": [
{
"package": "CFreeGLUT",
"repositoryURL": "https://github.com/BradLarson/CFreeGLUT.git",
"state": {
"branch": null,
"revision": "efe8d33674300c032662f6031b84c93147b4a8b7",
"version": "1.0.1"
}
},
{
"package": "COpenGL",
"repositoryURL": "https://github.com/BradLarson/COpenGL.git",
"state": {
"branch": null,
"revision": "328a86b37fcfc177b1b74c987f5f2198731fbdb2",
"version": "1.0.2"
}
},
{
"package": "CVideo4Linux",
"repositoryURL": "https://github.com/BradLarson/CVideo4Linux.git",
"state": {
"branch": null,
"revision": "9598a1a7ce569119152572644debdca48c2d2a92",
"version": "1.0.2"
}
}
]
},
"version": 1
}
16 changes: 16 additions & 0 deletions examples/Linux-OpenGL/SimpleVideoFilter/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// swift-tools-version:4.2

import PackageDescription

let package = Package(
name: "SimpleVideoFilter",
dependencies: [
.package(path: "../../../../GPUImage2")
],
targets: [
.target(
name: "SimpleVideoFilter",
dependencies: ["GPUImage"],
path: "Sources")
]
)
1 change: 0 additions & 1 deletion examples/Linux-OpenGL/SimpleVideoFilter/compile.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -384,6 +385,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -431,6 +433,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
METAL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -450,7 +453,6 @@
PRODUCT_NAME = FilterShowcase;
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -469,7 +471,6 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?) -> Bool {
return true
}
}
Expand Down
2 changes: 0 additions & 2 deletions framework/GPUImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,6 @@
PRODUCT_NAME = GPUImage;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -1898,7 +1897,6 @@
PRODUCT_NAME = GPUImage;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
101 changes: 0 additions & 101 deletions framework/LinuxCompilationGenerator.sh

This file was deleted.

13 changes: 8 additions & 5 deletions framework/Source/Linux/GLUTRenderWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public class GLUTRenderWindow: ImageConsumer {


public init(width:UInt32, height:UInt32, title:String) {
var localArgc = Process.argc
glutInit(&localArgc, Process.unsafeArgv)
// var localArgc = Process.argc
// glutInit(&localArgc, Process.unsafeArgv)

var input1:Int32 = 1
glutInit(&input1, nil)
glutInitDisplayMode(UInt32(GLUT_DOUBLE))
glutInitWindowSize(Int32(width), Int32(height))
glutInitWindowPosition(100,100)
Expand All @@ -28,7 +31,7 @@ public class GLUTRenderWindow: ImageConsumer {
// glutReshapeFunc(void (*func)(int width, int height) // Maybe use this to get window reshape events
}

public func newFramebufferAvailable(framebuffer:Framebuffer, fromSourceIndex:UInt) {
public func newFramebufferAvailable(_ framebuffer:Framebuffer, fromSourceIndex:UInt) {
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), 0)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), 0)

Expand All @@ -39,12 +42,12 @@ public class GLUTRenderWindow: ImageConsumer {
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GLenum(GL_COLOR_BUFFER_BIT))

renderQuadWithShader(self.displayShader, vertices:verticallyInvertedImageVertices, inputTextures:[framebuffer.texturePropertiesForTargetOrientation(.Portrait)])
renderQuadWithShader(self.displayShader, vertices:verticallyInvertedImageVertices, inputTextures:[framebuffer.texturePropertiesForTargetOrientation(.portrait)])
framebuffer.unlock()
glutSwapBuffers()
}

public func loopWithFunction(idleFunction:() -> ()) {
public func loopWithFunction(_ idleFunction:@escaping () -> ()) {
loopFunction = idleFunction
glutIdleFunc(glutCallbackFunction)
glutMainLoop()
Expand Down

0 comments on commit ffd7b5a

Please sign in to comment.