24 #if defined(ION_PLATFORM_IOS)
25 # include <OpenGLES/EAGL.h>
27 # include <AppKit/NSOpenGL.h>
33 struct Visual::VisualInfo {
35 static void* GetCurrentContext();
36 static void ClearCurrentContext();
38 VisualInfo() : weakContext(NULL), ownedContext(NULL) {}
40 #if defined(ION_PLATFORM_IOS)
41 __weak EAGLContext* weakContext;
42 EAGLContext* ownedContext;
44 __weak NSOpenGLContext* weakContext;
45 NSOpenGLContext* ownedContext;
49 #if defined(ION_PLATFORM_IOS)
50 void* Visual::VisualInfo::GetCurrentContext() {
51 return (__bridge
void*)[EAGLContext currentContext];
55 : visual_(new VisualInfo), type_(type) {
56 EAGLContext* current_context = [EAGLContext currentContext];
58 if (type == kCurrent) {
60 visual_->weakContext = current_context;
63 EAGLSharegroup* share_group =
64 (type == kShare && current_context) ? current_context.sharegroup : NULL;
66 visual_->ownedContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2
67 sharegroup:share_group];
68 visual_->weakContext = visual_->ownedContext;
72 void Visual::VisualInfo::ClearCurrentContext() {
73 [EAGLContext setCurrentContext:nil];
77 EAGLContext *strongContext = visual_->weakContext;
78 if (strongContext == nil) {
79 LOG(
ERROR) <<
"Unable to make Visual current. Unowned context has been released.";
82 const BOOL success = [EAGLContext setCurrentContext:strongContext];
84 LOG(
ERROR) <<
"Unable to make Visual current.";
92 void* Visual::VisualInfo::GetCurrentContext() {
93 return (__bridge
void*)[NSOpenGLContext currentContext];
96 void Visual::VisualInfo::ClearCurrentContext() {
97 [NSOpenGLContext clearCurrentContext];
101 : visual_(new VisualInfo), type_(type) {
103 NSOpenGLContext* current_context = [NSOpenGLContext currentContext];
105 if (type == kCurrent) {
107 visual_->weakContext = current_context;
110 NSOpenGLPixelFormatAttribute pixelAttrs[] = {
111 NSOpenGLPFAColorSize, 24,
112 NSOpenGLPFAAlphaSize, 8,
113 NSOpenGLPFADepthSize, 24,
114 NSOpenGLPFAStencilSize, 8,
115 NSOpenGLPFASampleBuffers, 0,
119 NSOpenGLPixelFormat* pixelFormat =
120 [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelAttrs];
121 NSOpenGLContext* share_context = (type == kShare) ? current_context : NULL;
123 visual_->ownedContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
124 shareContext:share_context];
125 visual_->weakContext = visual_->ownedContext;
130 NSOpenGLContext *strongContext = visual_->weakContext;
131 if (strongContext == nil) {
132 LOG(
ERROR) <<
"Unable to make Visual current. Unowned context has been released.";
135 [strongContext makeCurrentContext];
136 if (strongContext != [NSOpenGLContext currentContext]) {
137 LOG(
ERROR) <<
"Unable to make Visual current.";
149 id_ =
reinterpret_cast<size_t>(visual_->weakContext);
153 return visual_->weakContext != NULL;
157 visual_->weakContext = nil;
158 visual_->ownedContext = nil;
162 visual_->weakContext = nil;
163 visual_->ownedContext = nil;
virtual bool IsValid() const
Returns true if the OpenGL initialization was successful.
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
virtual void UpdateId()
Updates this' ID in a platform dependent way.
Visual()
Constructor for subclasses that make their own contexts.
virtual void TeardownContextNew()
Destroy Visuals of type kNew.
virtual ~Visual()
OpenGL calls should not be made after the Visual is destroyed.
virtual void TeardownContextShared()
Destroy Visuals of type kShare.
Copyright 2016 Google Inc.
virtual bool MakeCurrent() const
Makes this Visual current for this thread and returns whether its associated GL context was successfu...
static void TeardownVisual(Visual *visual)
Responsible for cleaning up |visual's| resources.