-
Notifications
You must be signed in to change notification settings - Fork 39
Add CAGradientLayer, Allow Layer Flattening for proper masking and opacity compositing #408
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
Open
ephemer
wants to merge
6
commits into
multipleVideos
Choose a base branch
from
flattenLayers
base: multipleVideos
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7e732a
Flatten Layers to allow for proper masking and opacity compositing. A…
ephemer eff0e98
Update Xcode version for CircleCI
ephemer cb7aa6c
Remove x64_64 requirement
ephemer 13cd830
Fix tests
ephemer 56dcbec
Update iPhone
ephemer d8a7f91
Fix Android build
ephemer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| internal import SDL | ||
| private import SDL_gpu | ||
|
|
||
| open class CAGradientLayer: CALayer { | ||
| public var colors: [CGColor] = [] { | ||
| didSet { | ||
| _colors = colors.map { | ||
| SIMD4<CGFloat>( | ||
| x: CGFloat($0.redValue) / 255, | ||
| y: CGFloat($0.greenValue) / 255, | ||
| z: CGFloat($0.blueValue) / 255, | ||
| w: CGFloat($0.alphaValue) / 255 | ||
| ) | ||
| } | ||
| setNeedsDisplay() | ||
| } | ||
| } | ||
|
|
||
| private var _colors: [SIMD4<CGFloat>] = [] | ||
|
|
||
| public var locations: [Float] = [] { | ||
| didSet { setNeedsDisplay() } | ||
| } | ||
|
|
||
| public var startPoint = CGPoint(x: 0.5, y: 0.0) { | ||
| didSet { setNeedsDisplay() } | ||
| } | ||
|
|
||
| public var endPoint = CGPoint(x: 0.5, y: 1.0) { | ||
| didSet { setNeedsDisplay() } | ||
| } | ||
|
|
||
| @_optimize(speed) | ||
| override open func display() { | ||
| super.display() | ||
|
|
||
| if bounds.width.isZero || bounds.height.isZero { | ||
| return | ||
| } | ||
|
|
||
| if locations.count < colors.count { | ||
| if colors.count == 1 { | ||
| backgroundColor = colors[0] | ||
| colors = [] | ||
| locations = [] | ||
| } else { | ||
| locations = colors.indices.map { Float($0) / Float(colors.count - 1) } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if colors.isEmpty { | ||
| contents = nil | ||
| return | ||
| } | ||
|
|
||
| let width: Int32 | ||
| let height: Int32 | ||
|
|
||
| if startPoint.x == endPoint.x { | ||
| width = 1 | ||
| height = Int32(bounds.height * self.contentsScale) | ||
| } else if startPoint.y == endPoint.y { | ||
| width = Int32(bounds.width * self.contentsScale) | ||
| height = 1 | ||
| } else { | ||
| // pessimistic case where we need to fill the entire thing | ||
| width = Int32(bounds.width * self.contentsScale) | ||
| height = Int32(bounds.height * self.contentsScale) | ||
| } | ||
|
|
||
| guard let surface = SDL_CreateRGBSurfaceWithFormat( | ||
| 0, // surface flags are always 0 in SDL2 | ||
| width, | ||
| height, | ||
| 32, // bit depth | ||
| UInt32(SDL_PIXELFORMAT_RGBA32) | ||
| ) else { | ||
| return | ||
| } | ||
|
|
||
| SDL_LockSurface(surface) | ||
|
|
||
| let startPoint = SIMD2(x: self.startPoint.x, y: self.startPoint.y) | ||
| let endPoint = SIMD2(x: self.endPoint.x, y: self.endPoint.y) | ||
|
|
||
| for y in 0 ..< Int(height) { | ||
| let pixelPosY = y * Int(surface.pointee.pitch) | ||
|
|
||
| for x in 0 ..< Int(width) { | ||
| let pixel = surface.pointee.pixels.assumingMemoryBound(to: UInt8.self) | ||
| .advanced(by: pixelPosY) | ||
| .advanced(by: x * Int(surface.pointee.format.pointee.BytesPerPixel)) | ||
|
|
||
| let p = SIMD2(x: CGFloat(x) / bounds.width, y: CGFloat(y) / bounds.height) | ||
|
|
||
| // Direction of gradient line | ||
| let dir = endPoint - startPoint | ||
| let len2 = dot(dir, dir) | ||
|
|
||
| var t: CGFloat = 0.0 | ||
| if len2 > 0.00001 { | ||
| // Project (p - startPoint) onto the gradient direction | ||
| t = dot(p - startPoint, dir) / len2 | ||
| } | ||
|
|
||
| t = max(0.0, min(t, 1.0)) | ||
| let color = sampleGradient(t) | ||
|
|
||
| let normalized = color * 255 | ||
| pixel[0] = UInt8(clamping: Int(normalized.x)) | ||
| pixel[1] = UInt8(clamping: Int(normalized.y)) | ||
| pixel[2] = UInt8(clamping: Int(normalized.z)) | ||
| pixel[3] = UInt8(clamping: Int(normalized.w)) | ||
| } | ||
| } | ||
|
|
||
| SDL_UnlockSurface(surface) | ||
|
|
||
| if | ||
| let contents, | ||
| contents.width != Int(bounds.width) || | ||
| contents.height != Int(bounds.height) | ||
| { | ||
| contents.replacePixels( | ||
| with: surface.pointee.pixels.assumingMemoryBound(to: UInt8.self), | ||
| bytesPerPixel: Int(surface.pointee.format.pointee.BytesPerPixel) | ||
| ) | ||
| } else { | ||
| contents = CGImage(surface: surface) | ||
| } | ||
| } | ||
|
|
||
| @_optimize(speed) | ||
| func sampleGradient(_ position: CGFloat) -> SIMD4<CGFloat> { | ||
| precondition(colors.count >= 2) | ||
| precondition(locations.count >= 2) | ||
|
|
||
| let t = Float(max(0.0, min(position, 1.0))) | ||
|
|
||
| if t <= locations.first! { | ||
| return _colors.first! | ||
| } else if t >= locations.last! { | ||
| return _colors.last! | ||
| } | ||
|
|
||
| // Find stops [i, i+1] containing t | ||
| for i in 0 ..< (colors.count - 1) { | ||
| let loc0 = locations[i] | ||
| let loc1 = locations[i + 1] | ||
|
|
||
| if t >= loc0 && t <= loc1 { | ||
| let localT = (t - loc0) / (loc1 - loc0) | ||
| return mix(_colors[i], _colors[i + 1], t: CGFloat(localT)) | ||
| } | ||
| } | ||
|
|
||
| return _colors.last! | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // GLSL-style mix for SIMD4 | ||
| @inline(__always) | ||
| func mix(_ a: SIMD4<CGFloat>, _ b: SIMD4<CGFloat>, t: CGFloat) -> SIMD4<CGFloat> { | ||
| return a + (b - a) * t | ||
| } | ||
|
|
||
| @inline(__always) | ||
| func dot(_ a: SIMD2<CGFloat>, _ b: SIMD2<CGFloat>) -> CGFloat { | ||
| return a.x * b.x + a.y * b.y | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,8 @@ struct ContentsGravityTransformation { | |
| /// Warning, this assumes `layer` has `contents` and will crash otherwise! | ||
| init(for layer: CALayer) { | ||
| let scaledContents = CGSize( | ||
| width: CGFloat(layer.contents!.width) / layer.contentsScale, | ||
| height: CGFloat(layer.contents!.height) / layer.contentsScale | ||
| width: (layer.contents.map { CGFloat($0.width) } ?? layer.bounds.width) / layer.contentsScale, | ||
| height: (layer.contents.map { CGFloat($0.height) } ?? layer.bounds.height) / layer.contentsScale | ||
|
Comment on lines
+25
to
+26
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. turns out this is actually unused but it prevents the crash mentioned on line 22 (I could remove that comment now I guess) |
||
| ) | ||
|
|
||
| let bounds = layer.bounds | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change dramatically improves video performance on macOS – esp. in debug builds