forked from zhanxiaokai/iOS-OpenGLRenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreviewView.mm
executable file
·197 lines (177 loc) · 6.71 KB
/
PreviewView.mm
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
//
// PreviewView.m
// OpenGLRenderer
//
// Created by apple on 2017/2/9.
// Copyright © 2017年 xiaokai.zhan. All rights reserved.
//
#import "PreviewView.h"
#import "RGBAFrameCopier.h"
#import "png_decoder.h"
#import "rgba_frame.h"
@interface PreviewView()
@property (atomic) BOOL readyToRender;
@property (nonatomic, assign) BOOL shouldEnableOpenGL;
@property (nonatomic, strong) NSLock *shouldEnableOpenGLLock;
@end
@implementation PreviewView
{
dispatch_queue_t _contextQueue;
EAGLContext* _context;
GLuint _displayFramebuffer;
GLuint _renderbuffer;
GLint _backingWidth;
GLint _backingHeight;
BOOL _stopping;
RGBAFrameCopier* _frameCopier;
RGBAFrame* _frame;
}
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
- (id) initWithFrame:(CGRect)frame filePath:(NSString*) filePath;
{
self = [super initWithFrame:frame];
if (self) {
_shouldEnableOpenGLLock = [NSLock new];
[_shouldEnableOpenGLLock lock];
_shouldEnableOpenGL = [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
[_shouldEnableOpenGLLock unlock];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
CAEAGLLayer *eaglLayer = (CAEAGLLayer*) self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
_contextQueue = dispatch_queue_create("com.changba.video_player.videoRenderQueue", NULL);
dispatch_sync(_contextQueue, ^{
_context = [self buildEAGLContext];
if (!_context || ![EAGLContext setCurrentContext:_context]) {
NSLog(@"Setup EAGLContext Failed...");
}
if(![self createDisplayFramebuffer]){
NSLog(@"create Dispaly Framebuffer failed...");
}
_frame = [self getRGBAFrame:filePath];
_frameCopier = [[RGBAFrameCopier alloc] init];
if (![_frameCopier prepareRender:_frame->width height:_frame->height]) {
NSLog(@"RGBAFrameCopier prepareRender failed...");
}
self.readyToRender = YES;
});
}
return self;
}
- (void) render;
{
if(_stopping){
return;
}
dispatch_async(_contextQueue, ^{
if(_frame) {
[self.shouldEnableOpenGLLock lock];
if (!self.readyToRender || !self.shouldEnableOpenGL) {
glFinish();
[self.shouldEnableOpenGLLock unlock];
return;
}
[self.shouldEnableOpenGLLock unlock];
[EAGLContext setCurrentContext:_context];
glBindFramebuffer(GL_FRAMEBUFFER, _displayFramebuffer);
glViewport(0, _backingHeight - _backingWidth - 75, _backingWidth, _backingWidth);
[_frameCopier renderFrame:_frame->pixels];
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
});
}
- (RGBAFrame*) getRGBAFrame:(NSString*) pngFilePath;
{
PngPicDecoder* decoder = new PngPicDecoder();
char* pngPath = (char*)[pngFilePath cStringUsingEncoding:NSUTF8StringEncoding];
decoder->openFile(pngPath);
RawImageData data = decoder->getRawImageData();
RGBAFrame* frame = new RGBAFrame();
frame->width = data.width;
frame->height = data.height;
int expectLength = data.width * data.height * 4;
uint8_t * pixels = new uint8_t[expectLength];
memset(pixels, 0, sizeof(uint8_t) * expectLength);
int pixelsLength = MIN(expectLength, data.size);
memcpy(pixels, (byte*) data.data, pixelsLength);
frame->pixels = pixels;
decoder->releaseRawImageData(&data);
decoder->closeFile();
delete decoder;
return frame;
}
- (EAGLContext*) buildEAGLContext
{
return [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
}
- (BOOL) createDisplayFramebuffer;
{
BOOL ret = TRUE;
glGenFramebuffers(1, &_displayFramebuffer);
glGenRenderbuffers(1, &_renderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _displayFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"failed to make complete framebuffer object %x", status);
return FALSE;
}
GLenum glError = glGetError();
if (GL_NO_ERROR != glError) {
NSLog(@"failed to setup GL %x", glError);
return FALSE;
}
return ret;
}
- (void) destroy;
{
_stopping = true;
dispatch_sync(_contextQueue, ^{
if(_frameCopier) {
[_frameCopier releaseRender];
}
if (_displayFramebuffer) {
glDeleteFramebuffers(1, &_displayFramebuffer);
_displayFramebuffer = 0;
}
if (_renderbuffer) {
glDeleteRenderbuffers(1, &_renderbuffer);
_renderbuffer = 0;
}
if ([EAGLContext currentContext] == _context) {
[EAGLContext setCurrentContext:nil];
}
});
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (_contextQueue) {
_contextQueue = nil;
}
_frameCopier = nil;
_context = nil;
}
- (void)applicationWillResignActive:(NSNotification *)notification {
[self.shouldEnableOpenGLLock lock];
self.shouldEnableOpenGL = NO;
[self.shouldEnableOpenGLLock unlock];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
[self.shouldEnableOpenGLLock lock];
self.shouldEnableOpenGL = YES;
[self.shouldEnableOpenGLLock unlock];
}
@end