/
XBMCEAGLView.mm
369 lines (324 loc) · 10.3 KB
/
XBMCEAGLView.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (C) 2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
//hack around problem with xbmc's typedef int BOOL
// and obj-c's typedef unsigned char BOOL
#define BOOL XBMC_BOOL
#include <sys/resource.h>
#include <signal.h>
#include "system.h"
#include "AdvancedSettings.h"
#include "FileItem.h"
#include "Application.h"
#include "WindowingFactory.h"
#include "VideoReferenceClock.h"
#include "utils/log.h"
#include "utils/TimeUtils.h"
#include "Util.h"
#undef BOOL
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import "XBMCEAGLView.h"
#import "XBMCController.h"
#import "AutoPool.h"
//--------------------------------------------------------------
@interface XBMCEAGLView (PrivateMethods)
- (void) setContext:(EAGLContext *)newContext;
- (void) createFramebuffer;
- (void) deleteFramebuffer;
- (void) runDisplayLink;
@end
@implementation XBMCEAGLView
@synthesize animating;
@synthesize xbmcAlive;
@synthesize pause;
// You must implement this method
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
//--------------------------------------------------------------
- (id)initWithFrame:(CGRect)frame
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
if ((self = [super initWithFrame:frame]))
{
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat,
//kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
EAGLContext *aContext = [[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!aContext)
NSLog(@"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(@"Failed to set ES context current");
self.context = aContext;
[aContext release];
animating = FALSE;
xbmcAlive = FALSE;
pause = FALSE;
[self setContext:context];
[self createFramebuffer];
[self setFramebuffer];
displayLink = nil;
}
return self;
}
//--------------------------------------------------------------
- (void) dealloc
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[self deleteFramebuffer];
[context release];
[super dealloc];
}
//--------------------------------------------------------------
- (EAGLContext *)context
{
return context;
}
//--------------------------------------------------------------
- (void)setContext:(EAGLContext *)newContext
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
if (context != newContext)
{
[self deleteFramebuffer];
[context release];
context = [newContext retain];
[EAGLContext setCurrentContext:nil];
}
}
//--------------------------------------------------------------
- (void)createFramebuffer
{
if (context && !defaultFramebuffer)
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[EAGLContext setCurrentContext:context];
// Create default framebuffer object.
glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
// Create color render buffer and allocate backing store.
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, framebufferWidth, framebufferHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}
//--------------------------------------------------------------
- (void) deleteFramebuffer
{
if (context && !pause)
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[EAGLContext setCurrentContext:context];
if (defaultFramebuffer)
{
glDeleteFramebuffers(1, &defaultFramebuffer);
defaultFramebuffer = 0;
}
if (colorRenderbuffer)
{
glDeleteRenderbuffers(1, &colorRenderbuffer);
colorRenderbuffer = 0;
}
if (depthRenderbuffer)
{
glDeleteRenderbuffers(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
}
}
//--------------------------------------------------------------
- (void) setFramebuffer
{
if (context && !pause)
{
if ([EAGLContext currentContext] != context)
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
CGRect rect = self.bounds;
if(rect.size.height > rect.size.width) {
glViewport(0, 0, rect.size.height, rect.size.width);
glScissor(0, 0, rect.size.height, rect.size.width);
}
else
{
glViewport(0, 0, rect.size.width, rect.size.height);
glScissor(0, 0, rect.size.width, rect.size.height);
}
}
}
//--------------------------------------------------------------
- (bool) presentFramebuffer
{
bool success = FALSE;
if (context && !pause)
{
if ([EAGLContext currentContext] != context)
[EAGLContext setCurrentContext:context];
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
success = [context presentRenderbuffer:GL_RENDERBUFFER];
}
return success;
}
//--------------------------------------------------------------
- (void) pauseAnimation
{
pause = TRUE;
}
//--------------------------------------------------------------
- (void) resumeAnimation
{
pause = FALSE;
}
//--------------------------------------------------------------
- (void) startAnimation
{
if (!animating && context)
{
animating = TRUE;
CWinEventsIOS::Init();
// kick off an animation thread
animationThreadLock = [[NSConditionLock alloc] initWithCondition: FALSE];
animationThread = [[NSThread alloc] initWithTarget:self
selector:@selector(runAnimation:)
object:animationThreadLock];
[animationThread start];
[self initDisplayLink];
}
}
//--------------------------------------------------------------
- (void) stopAnimation
{
if (animating && context)
{
[self deinitDisplayLink];
animating = FALSE;
if (!g_application.m_bStop)
g_application.Stop(0);
// wait for animation thread to die
if ([animationThread isFinished] == NO)
[animationThreadLock lockWhenCondition:TRUE];
CWinEventsIOS::DeInit();
}
}
//--------------------------------------------------------------
- (void) runAnimation:(id) arg
{
CCocoaAutoPool outerpool;
// signal we are alive
NSConditionLock* myLock = arg;
[myLock lock];
#ifdef _DEBUG
g_advancedSettings.m_logLevel = LOG_LEVEL_DEBUG;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_DEBUG;
#else
g_advancedSettings.m_logLevel = LOG_LEVEL_NORMAL;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_NORMAL;
#endif
// Prevent child processes from becoming zombies on exit if not waited upon. See also Util::Command
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDWAIT;
sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
setlocale(LC_NUMERIC, "C");
g_advancedSettings.Initialize();
g_advancedSettings.m_startFullScreen = true;
//g_application.SetStandAlone(true);
g_application.Preflight();
if (g_application.Create())
{
xbmcAlive = TRUE;
try
{
CCocoaAutoPool innerpool;
g_application.Run();
}
catch(...)
{
NSLog(@"%sException caught on main loop. Exiting", __PRETTY_FUNCTION__);
}
}
else
{
NSLog(@"%sUnable to create application", __PRETTY_FUNCTION__);
}
// signal we are dead
[myLock unlockWithCondition:TRUE];
// grrr, xbmc does not shutdown properly and leaves
// several classes in an indeterminant state, we must exit and
// reload Lowtide/AppleTV, boo.
[g_xbmcController enableScreenSaver];
[g_xbmcController enableSystemSleep];
exit(0);
//[g_xbmcController applicationDidExit];
}
//--------------------------------------------------------------
- (void) runDisplayLink;
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
displayFPS = 1.0 / ([displayLink duration] * [displayLink frameInterval]);
if (animationThread && [animationThread isExecuting] == YES)
{
if (g_VideoReferenceClock)
g_VideoReferenceClock.VblankHandler(CurrentHostCounter(), displayFPS);
}
[pool release];
}
//--------------------------------------------------------------
- (void) initDisplayLink
{
displayLink = [NSClassFromString(@"CADisplayLink")
displayLinkWithTarget:self
selector:@selector(runDisplayLink)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
displayFPS = 1.0 / ([displayLink duration] * [displayLink frameInterval]);
}
//--------------------------------------------------------------
- (void) deinitDisplayLink
{
[displayLink invalidate];
displayLink = nil;
}
//--------------------------------------------------------------
- (double) getDisplayLinkFPS;
{
return displayFPS;
}
//--------------------------------------------------------------
@end