-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathUIScreen.swift
251 lines (213 loc) · 8.52 KB
/
UIScreen.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// UIScreen.swift
// UIKit
//
// Created by Chris on 08.08.17.
// Copyright © 2017 flowkey. All rights reserved.
//
internal import SDL
internal import SDL_gpu
extension SDLWindowFlags: @retroactive OptionSet {}
public extension UIScreen {
@MainActor
internal(set) static var main: UIScreen! {
didSet { CALayer.layerTreeIsDirty = true }
}
}
@MainActor
public final class UIScreen {
// If we could use `private` members from an extension this would be private
// Keep that in mind when using it: i.e. if possible, don't ;)
internal var rawPointer: UnsafeMutablePointer<GPU_Target>!
public var bounds: CGRect {
didSet {
let newWidth = UInt16(bounds.width.rounded())
let newHeight = UInt16(bounds.height.rounded())
GPU_SetWindowResolution(newWidth, newHeight)
GPU_SetVirtualResolution(rawPointer, newWidth, newHeight)
}
}
nonisolated public let scale: CGFloat
private init(renderTarget: UnsafeMutablePointer<GPU_Target>!, bounds: CGRect, scale: CGFloat) {
self.rawPointer = renderTarget
self.bounds = bounds
self.scale = scale
}
convenience init() {
guard UIScreen.main == nil else {
// This is a problem because it means something else in the logic is probably incorrect
// For example, if you are restarting the app, you should ensure the previous app is fully deinited first
fatalError("Tried to reinit UIScreen.main when there is already a screen inited")
}
#if DEBUG
GPU_SetDebugLevel(GPU_DEBUG_LEVEL_MAX)
#endif
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best")
#if os(Android)
// height/width are determined by the window when fullscreen:
var size = CGSize.zero
let options: SDLWindowFlags = SDL_WINDOW_FULLSCREEN
#else
var size = CGSize.samsungGalaxyJ5.landscape
let options: SDLWindowFlags = [
SDL_WINDOW_ALLOW_HIGHDPI,
SDL_WINDOW_RESIZABLE,
]
#endif
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)
GPU_SetPreInitFlags(GPU_INIT_DISABLE_VSYNC)
#if os(Android)
let androidScreenDimension = getAndroidScreenDimension()
#endif
if options.contains(SDL_WINDOW_FULLSCREEN), let displayMode = SDLDisplayMode.current {
// Fix fullscreen resolution on Mac and make Android easier to reason about:
// There is an inconsistency between Mac and Android when setting SDL_WINDOW_FULLSCREEN
// The easiest solution is just to work in 1:1 pixels
GPU_SetPreInitFlags(GPU_GetPreInitFlags() | GPU_INIT_DISABLE_AUTO_VIRTUAL_RESOLUTION)
#if os(Android)
size = CGSize(width: CGFloat(androidScreenDimension.width), height: CGFloat(androidScreenDimension.height))
#else
size = CGSize(width: CGFloat(displayMode.w), height: CGFloat(displayMode.h))
#endif
}
guard let gpuTarget = GPU_Init(UInt16(size.width), UInt16(size.height), UInt32(GPU_DEFAULT_INIT_FLAGS) | options.rawValue) else {
print(SDLError())
fatalError("GPU_Init failed")
}
#if os(Android)
let scale = androidScreenDimension.scale
GPU_SetVirtualResolution(gpuTarget, UInt16(size.width / scale), UInt16(size.height / scale))
size.width /= scale
size.height /= scale
#else
// Mac:
let scale = CGFloat(gpuTarget.pointee.base_h) / CGFloat(gpuTarget.pointee.h)
#endif
if size == .zero {
preconditionFailure("You need window dimensions to run")
}
self.init(
renderTarget: gpuTarget,
bounds: CGRect(origin: .zero, size: size),
scale: scale
)
// Fixes video surface visibility with transparent & opaque views in SDLSurface above
// by changing the alpha blend function to: src-alpha * (1 - dst-alpha) + dst-alpha
setShapeBlending(true)
setShapeBlendMode(GPU_BLEND_NORMAL_FACTOR_ALPHA)
clearErrors() // by now we have handled any errors we might have wanted to
}
deinit {
MainActor.assumeIsolated {
UIView.completePendingAnimations()
UIView.layersWithAnimations.removeAll()
UIView.currentAnimationPrototype = nil
UIEvent.activeEvents.removeAll()
FontRenderer.cleanupSession()
}
guard let rawPointer = self.rawPointer else {
return
}
defer { GPU_Quit() }
guard let gpuContext = rawPointer.pointee.context else {
assertionFailure("glRenderer gpuContext not found")
return
}
let existingWindowID = gpuContext.pointee.windowID
let existingWindow = SDL_GetWindowFromID(existingWindowID)
SDL_DestroyWindow(existingWindow)
}
// Should be in UIScreen+render.swift but you can't store properties in an extension..
var clippingRect: CGRect? {
didSet { didSetClippingRect() }
}
}
// XXX: This can be removed entirely when the new touch handling for Android lands
extension UIScreen {
func absolutePointInOwnCoordinates(x inputX: CGFloat, y inputY: CGFloat) -> CGPoint {
#if os(macOS)
// Here SDL scales our touch events for us, which means we need a special case for it:
return CGPoint(x: inputX, y: inputY)
#else
// On all other platforms, we scale the touch events to the screen size manually:
return CGPoint(x: inputX / scale, y: inputY / scale)
#endif
}
}
#if os(macOS)
import class AppKit.NSWindow
extension UIScreen {
var nsWindow: NSWindow {
let sdlWindowID = rawPointer.pointee.context.pointee.windowID
let sdlWindow = SDL_GetWindowFromID(sdlWindowID)
var info = SDL_SysWMinfo()
var version = SDL_version()
SDL_GetVersion(&version)
info.version.major = version.major
info.version.minor = version.minor
info.version.patch = version.patch
SDL_GetWindowWMInfo(sdlWindow, &info)
return info.info.cocoa.window.takeUnretainedValue()
}
}
#endif
#if os(Android)
import JNI
fileprivate func getAndroidScreenDimension() -> (width: CGFloat, height: CGFloat, scale: CGFloat) {
guard
let dimension = try? jni.call(
"getScreenDimension",
on: getSDLView(),
returningObjectType: "org.libsdl.app.ScreenDimension"
),
let width: JavaFloat = try? jni.GetField("width", from: dimension),
let height: JavaFloat = try? jni.GetField("height", from: dimension),
let scale: JavaFloat = try? jni.GetField("scale", from: dimension)
else { return(0, 0, 2) }
return (CGFloat(width), CGFloat(height), CGFloat(scale))
}
#endif
@MainActor
extension UIScreen {
/// Used in tests only and doesn't actually render anything
static func dummyScreen(
bounds: CGRect = CGRect(origin: .zero, size: .samsungGalaxyTab10),
scale: CGFloat
) -> UIScreen {
return UIScreen(
renderTarget: nil,
bounds: bounds,
scale: scale
)
}
}
@MainActor
extension CGSize {
// smartphones:
nonisolated static let samsungGalaxyJ5 = CGSize(width: 1280 / 2.0, height: 720 / 2.0)
nonisolated static let samsungGalaxyS5 = CGSize(width: 1920 / 3.0, height: 1080 / 3.0)
nonisolated static let samsungGalaxyS7 = CGSize(width: 2560 / 4.0, height: 1440 / 4.0) // 1080p 1.5x Retina
nonisolated static let samsungGalaxyS8 = CGSize(width: 2960 / 4.0, height: 1440 / 4.0)
// tablets:
nonisolated static let nexus9 = CGSize(width: 2048 / 2.0, height: 1536 / 2.0)
nonisolated static let huaweiM3lite = CGSize(width: 1920 / 2.0, height: 1200 / 2.0)
nonisolated static let samsungGalaxyTabS_T800 = CGSize(width: 2560 / 2.0, height: 1600 / 2.0)
nonisolated static let samsungGalaxyTab10 = CGSize(width: 1280 / 1.0, height: 800 / 1.0)
nonisolated static let samsungGalaxyTabA_T380 = CGSize(width: 1280 / 1.0, height: 800 / 1.0)
nonisolated static let samsungGalaxyTabA_T580 = CGSize(width: 1920 / 1.0, height: 1200 / 1.0)
// change orientation if needed
var landscape: CGSize {
if self.width >= self.height {
return self
} else {
return CGSize(width: self.height, height: self.width)
}
}
var portrait: CGSize {
if self.width <= self.height {
return self
} else {
return CGSize(width: self.height, height: self.width)
}
}
}