-
Notifications
You must be signed in to change notification settings - Fork 71
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
Improve hit test performance by optimizing Y axis #177
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ public enum HitTargetSnapshotUtility { | |
var viewToColorMap: [UIView: UIColor] = [:] | ||
let pixelWidth: CGFloat = 1 / UIScreen.main.scale | ||
|
||
func drawScanLine( | ||
func drawScanLineSegment( | ||
for hitView: UIView?, | ||
startingAtX: CGFloat, | ||
endingAtX: CGFloat, | ||
|
@@ -95,27 +95,23 @@ public enum HitTargetSnapshotUtility { | |
|
||
let touchOffset = pixelWidth / 2 | ||
|
||
// Step through every pixel along the Y axis. | ||
for y in stride(from: bounds.minY, to: bounds.maxY, by: pixelWidth) { | ||
typealias ScanLine = [(xRange: ClosedRange<CGFloat>, view: UIView?)] | ||
|
||
func scanLine(y: CGFloat) -> ScanLine { | ||
var scanLine: ScanLine = [] | ||
var lastHit: (CGFloat, UIView?)? = nil | ||
|
||
// Step through every pixel along the X axis. | ||
for x in stride(from: bounds.minX, to: bounds.maxX, by: pixelWidth) { | ||
let hitView = view.hitTest(CGPoint(x: x + touchOffset, y: y + touchOffset), with: nil) | ||
let hitView = view.hitTest(CGPoint(x: x + touchOffset, y: y), with: nil) | ||
|
||
if let lastHit = lastHit, hitView == lastHit.1 { | ||
// We're still hitting the same view. Keep scanning. | ||
continue | ||
|
||
} else if let previousHit = lastHit { | ||
// We've moved on to a new view, so draw the scan line for the previous view. | ||
drawScanLine( | ||
for: previousHit.1, | ||
startingAtX: previousHit.0, | ||
endingAtX: x, | ||
y: y, | ||
lineHeight: pixelWidth | ||
) | ||
scanLine.append(((previousHit.0...x), previousHit.1)) | ||
lastHit = (x, hitView) | ||
|
||
} else { | ||
|
@@ -126,15 +122,65 @@ public enum HitTargetSnapshotUtility { | |
|
||
// Finish the scan line if necessary. | ||
if let lastHit = lastHit, let lastHitView = lastHit.1 { | ||
drawScanLine( | ||
for: lastHitView, | ||
startingAtX: lastHit.0, | ||
endingAtX: bounds.maxX, | ||
scanLine.append(((lastHit.0...bounds.maxX), lastHitView)) | ||
} | ||
|
||
return scanLine | ||
} | ||
|
||
func drawScanLine(_ scanLine: ScanLine, y: CGFloat, lineHeight: CGFloat) { | ||
for segment in scanLine { | ||
drawScanLineSegment( | ||
for: segment.view, | ||
startingAtX: segment.xRange.lowerBound, | ||
endingAtX: segment.xRange.upperBound, | ||
y: y, | ||
lineHeight: pixelWidth | ||
lineHeight: lineHeight | ||
) | ||
} | ||
} | ||
|
||
func scanLinesEqual(_ a: ScanLine, _ b: ScanLine) -> Bool { | ||
return a.count == b.count | ||
&& zip(a, b).allSatisfy { aSegment, bSegment in | ||
aSegment.xRange == bSegment.xRange && aSegment.view === bSegment.view | ||
} | ||
} | ||
|
||
// In some cases striding by 1/3 can result in the `to` value being included due to a floating point rouding | ||
// error, in particular when dealing with bounds with a negative y origin. By striding to a value slightly | ||
// less than the desired stop (small enough to be less than the density of any screen in the foreseeable | ||
// future), we can avoid this rounding problem. | ||
let stopEpsilon: CGFloat = 0.0001 | ||
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. If we ever see screens with density high enough that this is an issue, I will eat my futuristic hat 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. Maybe by the iPhone C |
||
|
||
// Step through every full point along the Y axis and check if it's equal to the above line. If so, draw the | ||
// line at a full point width. If not, step through the pixel lines and draw each individually. | ||
var previousScanLine: (y: CGFloat, scanLine: ScanLine)? = nil | ||
for y in stride(from: bounds.minY, to: bounds.maxY, by: 1) { | ||
let fullScanLine = scanLine(y: y + touchOffset) | ||
|
||
if let previousScanLine = previousScanLine, scanLinesEqual(fullScanLine, previousScanLine.scanLine) { | ||
drawScanLine(previousScanLine.scanLine, y: previousScanLine.y, lineHeight: 1) | ||
} else if let previousScanLine = previousScanLine { | ||
drawScanLine(previousScanLine.scanLine, y: previousScanLine.y, lineHeight: pixelWidth) | ||
for lineY in stride(from: previousScanLine.y + pixelWidth, to: y - stopEpsilon, by: pixelWidth) { | ||
drawScanLine(scanLine(y: lineY + touchOffset), y: lineY, lineHeight: pixelWidth) | ||
} | ||
} else { | ||
// No-op. We'll draw this on the next iteration. | ||
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. nice |
||
} | ||
|
||
previousScanLine = (y, fullScanLine) | ||
} | ||
|
||
// Draw the final full scan line and any trailing pixel lines (if the bounds.height isn't divisible by 1). | ||
if let previousScanLine = previousScanLine { | ||
drawScanLine(previousScanLine.scanLine, y: previousScanLine.y, lineHeight: pixelWidth) | ||
|
||
for lineY in stride(from: previousScanLine.y + pixelWidth, to: bounds.maxY, by: pixelWidth) { | ||
drawScanLine(scanLine(y: lineY + touchOffset), y: lineY, lineHeight: pixelWidth) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
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 brings the run time to almost (1 / scale factor), so in this case around a 65% improvement for a 3x device over the original implementation in #119.
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.
Increasing the
maxPermissibleMissedRegionHeight
to larger values continues to improve the run time, but you get diminishing returns.2 => 10.259 sec
4 => 8.317 sec