summaryrefslogtreecommitdiff
path: root/modules/opengles/context.m
blob: 354c74fdb096b783f1ba8f7eb5932ee19de56f06 (plain)
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
/**
 * @file context.m OpenGLES Context for iOS
 *
 * Copyright (C) 2010 Creytiv.com
 */

#include <re.h>
#include <baresip.h>
#include <UIKit/UIKit.h>
#include <QuartzCore/CAEAGLLayer.h>
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
#include "opengles.h"


@interface GlView : UIView
{
	struct vidisp_st *st;
}
@end


static EAGLContext *ctx = NULL;


@implementation GlView


+ (Class)layerClass
{
	return [CAEAGLLayer class];
}


- (id)initWithFrame:(CGRect)frame vidisp:(struct vidisp_st *)vst
{
	self = [super initWithFrame:frame];
	if (!self)
		return nil;

	self.layer.opaque = YES;

	st = vst;

	return self;
}


- (void) render_sel:(id)unused
{
	(void)unused;

	if (!ctx) {
		UIWindow* window = [UIApplication sharedApplication].keyWindow;

		ctx = [EAGLContext alloc];
		[ctx initWithAPI:kEAGLRenderingAPIOpenGLES1];

		[EAGLContext setCurrentContext:ctx];

		[window addSubview:self];
		[window bringSubviewToFront:self];

		[window makeKeyAndVisible];
	}

	if (!st->framebuffer) {

		opengles_addbuffers(st);

		[ctx renderbufferStorage:GL_RENDERBUFFER_OES
		     fromDrawable:(CAEAGLLayer*)self.layer];
	}

	opengles_render(st);

	[ctx presentRenderbuffer:GL_RENDERBUFFER_OES];
}


- (void) dealloc
{
	[ctx release];

	[super dealloc];
}


@end


void context_destroy(struct vidisp_st *st)
{
	[(UIView *)st->view release];
}


int context_init(struct vidisp_st *st)
{
	UIWindow* window = [UIApplication sharedApplication].keyWindow;

	st->view = [[GlView new] initWithFrame:window.bounds vidisp:st];

	return 0;
}


void context_render(struct vidisp_st *st)
{
	UIView *view = st->view;

	[view performSelectorOnMainThread:@selector(render_sel:)
	  withObject:nil
	  waitUntilDone:YES];
}