[iOS] iOS context + factory

This commit is contained in:
Dmitry Kunin 2013-12-25 17:33:33 +03:00 committed by Alex Zolotarev
parent 7ad770671b
commit 512bc05d31
4 changed files with 160 additions and 38 deletions

View file

@ -1,14 +1,34 @@
//
// iosOGLContext.h
// Maps
//
// Created by Dmitry Kunin on 25.12.13.
// Copyright (c) 2013 MapsWithMe. All rights reserved.
//
#pragma once
#ifndef __Maps__iosOGLContext__
#define __Maps__iosOGLContext__
#import "../../../../drape/oglcontext.hpp"
#include <iostream>
#import <QuartzCore/CAEAGLLayer.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#endif /* defined(__Maps__iosOGLContext__) */
class iosOGLContext : public OGLContext
{
public:
iosOGLContext(CAEAGLLayer * layer, iosOGLContext * contextToShareWith, bool needBuffers = false);
~iosOGLContext();
virtual void makeCurrent();
virtual void present();
virtual void setDefaultFramebuffer();
private:
CAEAGLLayer * m_layer;
EAGLContext * m_nativeContext;
void initBuffers();
void destroyBuffers();
//{@ Buffers
bool m_needBuffers;
bool m_hasBuffers;
GLuint m_renderBufferId;
GLuint m_depthBufferId;
GLuint m_frameBufferId;
//@} buffers
};

View file

@ -1,9 +1,95 @@
//
// iosOGLContext.cpp
// Maps
//
// Created by Dmitry Kunin on 25.12.13.
// Copyright (c) 2013 MapsWithMe. All rights reserved.
//
#import "iosOGLContext.h"
#import "../../../../base/assert.hpp"
#include "iosOGLContext.h"
iosOGLContext::iosOGLContext(CAEAGLLayer * layer, iosOGLContext * contextToShareWith, bool needBuffers)
: m_layer(layer)
, m_nativeContext(NULL)
, m_needBuffers(needBuffers)
, m_hasBuffers(false)
, m_renderBufferId(0)
, m_depthBufferId(0)
, m_frameBufferId(0)
{
if (contextToShareWith != NULL)
{
m_nativeContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2
sharegroup: contextToShareWith->m_nativeContext.sharegroup];
}
else
m_nativeContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
}
iosOGLContext::~iosOGLContext()
{
destroyBuffers();
}
void iosOGLContext::makeCurrent()
{
ASSERT(m_nativeContext != NULL, ());
[EAGLContext setCurrentContext: m_nativeContext];
if (m_needBuffers && !m_hasBuffers)
initBuffers();
}
void iosOGLContext::present()
{
ASSERT(m_nativeContext != NULL, ());
[m_nativeContext presentRenderbuffer: GL_RENDERBUFFER];
}
void iosOGLContext::setDefaultFramebuffer()
{
ASSERT(m_frameBufferId, ());
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
}
void iosOGLContext::initBuffers()
{
ASSERT(m_needBuffers, ());
if (!m_hasBuffers)
{
// Color
glGenRenderbuffers(1, &m_renderBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, m_renderBufferId);
[m_nativeContext renderbufferStorage:GL_RENDERBUFFER fromDrawable: m_layer];
// color
// Depth
GLint width = 0;
GLint height = 0;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
glGenRenderbuffers(1, &m_depthBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
// depth
// Framebuffer
glGenFramebuffers(1, &m_frameBufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderBufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferId);
GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
ASSERT(fbStatus == GL_FRAMEBUFFER_COMPLETE, ("Incomplete framebuffer:", fbStatus));
// framebuffer
m_hasBuffers = true;
}
}
void iosOGLContext::destroyBuffers()
{
if (m_needBuffers && m_hasBuffers)
{
glDeleteFramebuffers(1, &m_frameBufferId);
glDeleteBuffers(1, &m_renderBufferId);
glDeleteBuffers(1, &m_depthBufferId);
m_hasBuffers = false;
}
}

View file

@ -1,14 +1,17 @@
//
// iosOGLContextFactory.h
// Maps
//
// Created by Dmitry Kunin on 25.12.13.
// Copyright (c) 2013 MapsWithMe. All rights reserved.
//
#pragma once
#ifndef __Maps__iosOGLContextFactory__
#define __Maps__iosOGLContextFactory__
#import "iosOGLContext.h"
#import "../../../../drape/oglcontextfactory.hpp"
#include <iostream>
class iosOGLContextFactory: public OGLContextFactory
{
iosOGLContextFactory(CAEAGLLayer * layer);
#endif /* defined(__Maps__iosOGLContextFactory__) */
virtual OGLContext * getDrawContext();
virtual OGLContext * getResourcesUploadContext();
private:
CAEAGLLayer * m_layer;
iosOGLContext * m_drawContext;
iosOGLContext * m_uploadContext;
};

View file

@ -1,9 +1,22 @@
//
// iosOGLContextFactory.cpp
// Maps
//
// Created by Dmitry Kunin on 25.12.13.
// Copyright (c) 2013 MapsWithMe. All rights reserved.
//
#import "iosOGLContextFactory.h"
#include "iosOGLContextFactory.h"
iosOGLContextFactory::iosOGLContextFactory(CAEAGLLayer * layer)
: m_layer(layer)
, m_drawContext(NULL)
, m_uploadContext(NULL)
{}
OGLContext * iosOGLContextFactory::getDrawContext()
{
if (m_drawContext == NULL)
m_drawContext = new iosOGLContext(m_layer, m_uploadContext, true);
return m_drawContext;
}
OGLContext * iosOGLContextFactory::getResourcesUploadContext()
{
if (m_uploadContext == NULL)
m_uploadContext = new iosOGLContext(m_layer, m_drawContext, false);
return m_uploadContext;
}