Skip to content

Commit

Permalink
SCContext are loaded lazily to improve SCImageView and SCFilterSelect…
Browse files Browse the repository at this point in the history
…orView instantiation speed.
  • Loading branch information
rFlex committed May 29, 2015
1 parent 95c6a69 commit afde95a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Library/Sources/SCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
@property (readonly, nonatomic) CIContext *CIContext;
@property (readonly, nonatomic) EAGLContext *EAGLContext;

+ (SCContext *)context;

@end
18 changes: 16 additions & 2 deletions Library/Sources/SCContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,31 @@
@implementation SCContext

- (id)init {
return [self initWithSharegroup:nil];
}

- (id)initWithSharegroup:(EAGLSharegroup *)shareGroup {
self = [super init];

if (self) {
_EAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_EAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:shareGroup];

NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null], kCIContextOutputColorSpace : [NSNull null] };

_CIContext = [CIContext contextWithEAGLContext:_EAGLContext options:options];
}

return self;
}

+ (SCContext *)context {
static dispatch_once_t onceToken;
static EAGLSharegroup *shareGroup;
dispatch_once(&onceToken, ^{
shareGroup = [EAGLSharegroup new];
});

return [[SCContext alloc] initWithSharegroup:shareGroup];
}

@end
18 changes: 13 additions & 5 deletions Library/Sources/SCFilterSelectorView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
}

- (void)commonInit {
SCContext *context = [SCContext new];
EAGLContext *EAGLContext = context.EAGLContext;
_glkView = [[GLKView alloc] initWithFrame:self.bounds context:EAGLContext];
_glkView = [[GLKView alloc] initWithFrame:self.bounds context:nil];
_glkView.backgroundColor = [UIColor clearColor];

_CIContext = context.CIContext;

_glkView.delegate = self;

_sampleBufferHolder = [SCSampleBufferHolder new];

[self addSubview:_glkView];
}

- (void)_loadContext {
if (_CIContext == nil) {
SCContext *context = [SCContext context];
_CIContext = context.CIContext;
_glkView.context = context.EAGLContext;
}
}

- (void)layoutSubviews {
[super layoutSubviews];

Expand Down Expand Up @@ -138,6 +142,10 @@ - (UIImage *)currentlyDisplayedImageWithScale:(CGFloat)scale orientation:(UIImag

- (void)setCIImage:(CIImage *)CIImage {
_CIImage = CIImage;

if (CIImage != nil) {
[self _loadContext];
}
[_glkView setNeedsDisplay];
}

Expand Down
17 changes: 13 additions & 4 deletions Library/Sources/SCImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
- (void)commonInit {
self.preferredCIImageTransform = CGAffineTransformIdentity;

SCContext *context = [SCContext new];
_CIContext = context.CIContext;
self.context = context.EAGLContext;

_sampleBufferHolder = [SCSampleBufferHolder new];
}

- (void)_loadContext {
if (_CIContext == nil) {
SCContext *context = [SCContext context];
_CIContext = context.CIContext;
self.context = context.EAGLContext;
}
}

- (void)drawRect:(CGRect)rect {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -78,6 +82,7 @@ - (void)drawRect:(CGRect)rect {

- (void)setImageBySampleBuffer:(CMSampleBufferRef)sampleBuffer {
_sampleBufferHolder.sampleBuffer = sampleBuffer;

[self setNeedsDisplay];
}

Expand All @@ -88,6 +93,10 @@ - (void)setImageByUIImage:(UIImage *)image {
- (void)setCIImage:(CIImage *)CIImage {
_CIImage = CIImage;

if (CIImage != nil) {
[self _loadContext];
}

[self setNeedsDisplay];
}

Expand Down
4 changes: 2 additions & 2 deletions SCRecorder.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SCRecorder"
s.version = "2.4.3"
s.version = "2.4.5"
s.summary = "The camera engine that is complete, for real."

s.description = <<-DESC
Expand All @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.license = 'Apache License, Version 2.0'
s.author = { "Simon CORSIN" => "[email protected]" }
s.platform = :ios, '6.0'
s.source = { :git => "https://github.com/rFlex/SCRecorder.git", :tag => "v2.4.3" }
s.source = { :git => "https://github.com/rFlex/SCRecorder.git", :tag => "v2.4.5" }
s.source_files = 'Library/Sources/*.{h,m}'
s.public_header_files = 'Library/Sources/*.h'
s.requires_arc = true
Expand Down

0 comments on commit afde95a

Please sign in to comment.