summaryrefslogtreecommitdiff
path: root/ext/eglgles/gstegladaptation_eagl.m
blob: 69d3f602d4a190a675127cffabb88b06a1f6d77f (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
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
/*
 * GStreamer EGL/GLES Sink Adaptation for IOS
 * Copyright (C) 2013 Collabora Ltd.
 *   @author: Thiago Santos <thiago.sousa.santos@collabora.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Alternatively, the contents of this file may be used under the
 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
 * which case the following provisions apply instead of the ones
 * mentioned above:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#include <OpenGLES/ES2/gl.h>

#include "gstegladaptation.h"

struct _GstEaglContext
{
  EAGLContext *eagl_context;
  GLuint framebuffer;
  GLuint color_renderbuffer;

  UIView *window;
  UIView *used_window;
};

void
gst_egl_adaptation_context_init (GstEglAdaptationContext * ctx)
{
  ctx->eaglctx = g_new0 (GstEaglContext, 1);
}

void
gst_egl_adaptation_context_deinit (GstEglAdaptationContext * ctx)
{
  g_free (ctx->eaglctx);
}

gboolean
gst_egl_adaptation_init_display (GstEglAdaptationContext * ctx)
{
  /* NOP - the display should be initialized by the application */
  return TRUE;
}

void
gst_egl_adaptation_context_terminate_display (GstEglAdaptationContext * ctx)
{
  /* NOP */
}

void
gst_egl_adaptation_context_bind_API (GstEglAdaptationContext * ctx)
{
  /* NOP */
}

gboolean
gst_egl_adaptation_create_native_window (GstEglAdaptationContext * ctx, gint width, gint height, gpointer * own_window_data)
{
  return FALSE;
}

void
gst_egl_adaptation_destroy_native_window (GstEglAdaptationContext * ctx, gpointer * own_window_data)
{
}

gboolean
gst_egl_adaptation_create_egl_context (GstEglAdaptationContext * ctx)
{
  EAGLContext *context;
  context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  if (context == nil) {
    GST_ERROR_OBJECT (ctx->element, "Failed to create EAGL GLES2 context");
    return FALSE;
  }
  ctx->eaglctx->eagl_context = context;

  return TRUE;
}

gboolean
gst_egl_adaptation_context_make_current (GstEglAdaptationContext * ctx,
    gboolean bind)
{
  if (bind && ctx->eaglctx->eagl_context) {
    EAGLContext *cur_ctx = [EAGLContext currentContext];

    if (cur_ctx == ctx->eaglctx->eagl_context) {
      GST_DEBUG_OBJECT (ctx->element,
          "Already attached the context to thread %p", g_thread_self ());
      return TRUE;
    }

    GST_DEBUG_OBJECT (ctx->element, "Attaching context to thread %p",
        g_thread_self ());
    if ([EAGLContext setCurrentContext: ctx->eaglctx->eagl_context] == NO) {
      got_gl_error ("setCurrentContext");
      GST_ERROR_OBJECT (ctx->element, "Couldn't bind context");
      return FALSE;
    }
  } else {
    GST_DEBUG_OBJECT (ctx->element, "Detaching context from thread %p",
        g_thread_self ());
    if ([EAGLContext setCurrentContext: nil] == NO) {
      got_gl_error ("setCurrentContext");
      GST_ERROR_OBJECT (ctx->element, "Couldn't unbind context");
      return FALSE;
    }
  }

  return TRUE;
}

gboolean
gst_egl_adaptation_create_surface (GstEglAdaptationContext * ctx)
{
  GLuint framebuffer;
  GLuint colorRenderbuffer;
  GLint width;
  GLint height;
  GLuint depthRenderbuffer;
  GLenum status;
  CAEAGLLayer *eaglLayer = (CAEAGLLayer *)[ctx->eaglctx->window layer];

  /* Allocate framebuffer */
  glGenFramebuffers(1, &framebuffer);
  glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
 
  /* Allocate color render buffer */
  glGenRenderbuffers(1, &colorRenderbuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  [ctx->eaglctx->eagl_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer];
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
      GL_RENDERBUFFER, colorRenderbuffer);

  /* Get renderbuffer width/height */
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);

  /* allocate depth render buffer */
  glGenRenderbuffers(1, &depthRenderbuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  GL_RENDERBUFFER, depthRenderbuffer);

  /* check creation status */
  status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  if(status != GL_FRAMEBUFFER_COMPLETE) {
    NSLog(@"failed to make complete framebuffer object %x", status);
    return FALSE;
  }

  ctx->eaglctx->framebuffer = framebuffer;
  ctx->eaglctx->color_renderbuffer = colorRenderbuffer;

  return TRUE;
}

gboolean
_gst_egl_choose_config (GstEglAdaptationContext * ctx, gboolean try_only, gint * num_configs)
{
  CAEAGLLayer *eaglLayer = (CAEAGLLayer *)[ctx->eaglctx->window layer];
  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
                        kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                        nil];
  [eaglLayer setOpaque:YES];
  [eaglLayer setDrawableProperties:dict];

  if (num_configs)
    *num_configs = 1;
}

void
gst_egl_adaptation_query_buffer_preserved (GstEglAdaptationContext * ctx)
{
  CAEAGLLayer *eaglLayer = (CAEAGLLayer *)[ctx->eaglctx->window layer];
  NSDictionary *dict = [eaglLayer drawableProperties];

  ctx->buffer_preserved = FALSE;
  if ([dict objectForKey: kEAGLDrawablePropertyRetainedBacking] != nil) {
    NSNumber n = [dict objectForKey: kEAGLDrawablePropertyRetainedBacking];
    ctx->buffer_preserved = n != [NSNumber numberWithBool:NO];
  } else {
    GST_DEBUG_OBJECT (ctx->element, "No information about buffer preserving in layer properties");
  }
}

void
gst_egl_adaptation_query_par (GstEglAdaptationContext * ctx)
{
  /* TODO how can we check this? */
  ctx->pixel_aspect_ratio = 1;
  ctx->pixel_aspect_ratio_d = 1;
}

gboolean
gst_egl_adaptation_context_update_surface_dimensions (GstEglAdaptationContext *
    ctx)
{
  GLint width;
  GLint height;

  /* Get renderbuffer width/height */
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);

  if (width != ctx->surface_width || height != ctx->surface_height) {
    ctx->surface_width = width;
    ctx->surface_height = height;
    GST_INFO_OBJECT (ctx->element, "Got surface of %dx%d pixels", width,
        height);
    return TRUE;
  }

  return FALSE;
}

void
gst_egl_adaptation_context_init_egl_exts (GstEglAdaptationContext * ctx)
{
  NSString *extensionsString = [NSString stringWithCString:glGetString(GL_EXTENSIONS) encoding: NSASCIIStringEncoding];

  GST_DEBUG_OBJECT (ctx->element, "Available GL extensions: %s\n",
      GST_STR_NULL ([extensionsString UTF8String]));
}

void
gst_egl_adaptation_destroy_surface (GstEglAdaptationContext * ctx)
{
  if (ctx->eaglctx->framebuffer) {
    glDeleteFrameBuffers (1, &ctx->eaglctx->framebuffer);
    ctx->eaglctx->framebuffer = 0;
    ctx->have_surface = FALSE;
  }
}

void
gst_egl_adaptation_destroy_context (GstEglAdaptationContext * ctx)
{
  if (ctx->eaglctx->eagl_context) {
    [ctx->eaglctx->eagl_context dealloc];
    ctx->eaglctx->eagl_context = NULL;
  }
}

gboolean
gst_egl_adaptation_context_swap_buffers (GstEglAdaptationContext * ctx)
{
  glBindRenderbuffer(GL_RENDERBUFFER, ctx->eaglctx->color_renderbuffer);
  [ctx->eaglctx->eagl_context presentRenderbuffer:GL_RENDERBUFFER];

  return TRUE;
}

void
gst_egl_adaptation_context_set_window (GstEglAdaptationContext * ctx, guintptr window)
{
  ctx->eaglctx->window = window;
}

void
gst_egl_adaptation_context_update_used_window (GstEglAdaptationContext * ctx)
{
  ctx->eaglctx->used_window = ctx->eaglctx->window;
}

guintptr
gst_egl_adaptation_context_get_window (GstEglAdaptationContext * ctx)
{
  return ctx->eaglctx->window ? [ctx->eaglctx->window id] : 0;
}