forked from rFlex/SCRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCContext.m
155 lines (125 loc) · 4.91 KB
/
SCContext.m
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
//
// SCContext.m
// SCRecorder
//
// Created by Simon CORSIN on 28/05/15.
// Copyright (c) 2015 rFlex. All rights reserved.
//
#import "SCContext.h"
@implementation SCContext
NSString *__nonnull const SCContextOptionsCGContextKey = @"CGContext";
NSString *__nonnull const SCContextOptionsEAGLContextKey = @"EAGLContext";
NSString *__nonnull const SCContextOptionsMTLDeviceKey = @"MTLDevice";
static NSDictionary *SCContextCreateCIContextOptions() {
return @{kCIContextWorkingColorSpace : [NSNull null], kCIContextOutputColorSpace : [NSNull null]};
}
- (instancetype)initWithSoftwareRenderer:(BOOL)softwareRenderer {
self = [super init];
if (self) {
NSMutableDictionary *options = SCContextCreateCIContextOptions().mutableCopy;
options[kCIContextUseSoftwareRenderer] = @(softwareRenderer);
_CIContext = [CIContext contextWithOptions:options];
if (softwareRenderer) {
_type = SCContextTypeCPU;
} else {
_type = SCContextTypeDefault;
}
}
return self;
}
- (instancetype)initWithCGContextRef:(CGContextRef)contextRef {
self = [super init];
if (self) {
_CIContext = [CIContext contextWithCGContext:contextRef options:SCContextCreateCIContextOptions()];
_type = SCContextTypeCoreGraphics;
}
return self;
}
- (instancetype)initWithEAGLContext:(EAGLContext *)context {
self = [super init];
if (self) {
_EAGLContext = context;
_CIContext = [CIContext contextWithEAGLContext:_EAGLContext options:SCContextCreateCIContextOptions()];
_type = SCContextTypeEAGL;
}
return self;
}
- (instancetype)initWithMTLDevice:(id<MTLDevice>)device {
self = [super init];
if (self) {
_MTLDevice = device;
_CIContext = [CIContext contextWithMTLDevice:device options:SCContextCreateCIContextOptions()];
_type = SCContextTypeMetal;
}
return self;
}
+ (BOOL)supportsType:(SCContextType)contextType {
id CIContextClass = [CIContext class];
switch (contextType) {
case SCContextTypeMetal:
return [CIContextClass respondsToSelector:@selector(contextWithMTLDevice:options:)];
case SCContextTypeCoreGraphics:
return [CIContextClass respondsToSelector:@selector(contextWithCGContext:options:)];
case SCContextTypeEAGL:
return [CIContextClass respondsToSelector:@selector(contextWithEAGLContext:options:)];
case SCContextTypeAuto:
case SCContextTypeDefault:
case SCContextTypeCPU:
return YES;
}
return NO;
}
+ (SCContextType)suggestedContextType {
// On iOS 9.0, Metal does not behave nicely with gaussian blur filters
// if ([SCContext supportsType:SCContextTypeMetal]) {
// return SCContextTypeMetal;
// } else
if ([SCContext supportsType:SCContextTypeEAGL]) {
return SCContextTypeEAGL;
} else if ([SCContext supportsType:SCContextTypeCoreGraphics]) {
return SCContextTypeCoreGraphics;
} else {
return SCContextTypeDefault;
}
}
+ (SCContext *)contextWithType:(SCContextType)contextType options:(NSDictionary *)options {
switch (contextType) {
case SCContextTypeAuto:
return [SCContext contextWithType:[SCContext suggestedContextType] options:options];
case SCContextTypeMetal: {
id<MTLDevice> device = options[SCContextOptionsMTLDeviceKey];
if (device == nil) {
device = MTLCreateSystemDefaultDevice();
}
return [[SCContext alloc] initWithMTLDevice:device];
}
case SCContextTypeCoreGraphics: {
CGContextRef context = (__bridge CGContextRef)(options[SCContextOptionsCGContextKey]);
if (context == nil) {
[NSException raise:@"MissingCGContext" format:@"SCContextTypeCoreGraphics needs to have a CGContext attached to the SCContextOptionsCGContextKey in the options"];
}
return [[SCContext alloc] initWithCGContextRef:context];
}
case SCContextTypeCPU:
return [[SCContext alloc] initWithSoftwareRenderer:YES];
case SCContextTypeDefault:
return [[SCContext alloc] initWithSoftwareRenderer:NO];
case SCContextTypeEAGL: {
EAGLContext *context = options[SCContextOptionsEAGLContextKey];
if (context == nil) {
static dispatch_once_t onceToken;
static EAGLSharegroup *shareGroup;
dispatch_once(&onceToken, ^{
shareGroup = [EAGLSharegroup new];
});
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:shareGroup];
}
return [[SCContext alloc] initWithEAGLContext:context];
}
default:
[NSException raise:@"InvalidContextType" format:@"Invalid context type %d", (int)contextType];
break;
}
return nil;
}
@end