0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / OpenGl / OpenGl_Context_1.mm
1 // Created on: 2012-11-12
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
17
18 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
19
20 #import <TargetConditionals.h>
21
22 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
23   #import <UIKit/UIKit.h>
24 #else
25   #import <Cocoa/Cocoa.h>
26 #endif
27
28 #include <OpenGl_GlCore11.hxx>
29 #include <OpenGl_Context.hxx>
30 #include <OpenGl_FrameBuffer.hxx>
31
32 #include <Standard_ProgramError.hxx>
33
34 // =======================================================================
35 // function : IsCurrent
36 // purpose  :
37 // =======================================================================
38 Standard_Boolean OpenGl_Context::IsCurrent() const
39 {
40 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
41   return myGContext != NULL
42       && [EAGLContext     currentContext] == myGContext;
43 #else
44   return myGContext != NULL
45       && [NSOpenGLContext currentContext] == myGContext;
46 #endif
47 }
48
49 // =======================================================================
50 // function : MakeCurrent
51 // purpose  :
52 // =======================================================================
53 Standard_Boolean OpenGl_Context::MakeCurrent()
54 {
55   if (myGContext == NULL)
56   {
57     Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
58     return Standard_False;
59   }
60
61 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
62   return [EAGLContext setCurrentContext: myGContext] == YES;
63 #else
64   [myGContext makeCurrentContext];
65   return Standard_True;
66 #endif
67 }
68
69 // =======================================================================
70 // function : SwapBuffers
71 // purpose  :
72 // =======================================================================
73 void OpenGl_Context::SwapBuffers()
74 {
75   if (myGContext == NULL)
76   {
77     return;
78   }
79
80 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
81   if (myDefaultFbo.IsNull()
82   || !myDefaultFbo->IsValid()
83   ||  myDefaultFbo->ColorRenderBuffer() == 0)
84   {
85     return;
86   }
87
88   ::glBindRenderbuffer (GL_RENDERBUFFER, myDefaultFbo->ColorRenderBuffer());
89   [myGContext presentRenderbuffer: GL_RENDERBUFFER];
90   //::glBindRenderbuffer (GL_RENDERBUFFER, 0);
91 #else
92   glFinish();
93   [myGContext flushBuffer];
94 #endif
95 }
96
97 // =======================================================================
98 // function : Init
99 // purpose  :
100 // =======================================================================
101 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
102 {
103   if (myIsInitialized)
104   {
105     return Standard_True;
106   }
107
108 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
109   myGContext = [EAGLContext     currentContext];
110 #else
111   myGContext = [NSOpenGLContext currentContext];
112 #endif
113   if (myGContext == NULL)
114   {
115     return Standard_False;
116   }
117
118   init (theIsCoreProfile);
119   myIsInitialized = Standard_True;
120   return Standard_True;
121 }
122
123 #endif // __APPLE__