1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25 
26 
27 #include <assert.h>
28 #include <stdlib.h>
29 
30 #include <iostream>
31 
32 #include "glproc.hpp"
33 #include "glws.hpp"
34 #include "retrace.hpp"
35 
36 
37 namespace glws {
38 
39 
40 bool
checkExtension(const char * extName,const char * extString)41 checkExtension(const char *extName, const char *extString)
42 {
43     assert(extName);
44 
45     if (!extString) {
46         return false;
47     }
48 
49     const char *p = extString;
50     const char *q = extName;
51     char c;
52     do {
53         c = *p++;
54         if (c == '\0' || c == ' ') {
55             if (q && *q == '\0') {
56                 return true;
57             } else {
58                 q = extName;
59             }
60         } else {
61             if (q && *q == c) {
62                 ++q;
63             } else {
64                 q = 0;
65             }
66         }
67     } while (c);
68     return false;
69 }
70 
71 
72 void
copySubBuffer(int x,int y,int width,int height)73 Drawable::copySubBuffer(int x, int y, int width, int height) {
74     std::cerr << "warning: copySubBuffer not yet implemented\n";
75 }
76 
77 
78 void
initialize(void)79 Context::initialize(void)
80 {
81     assert(!initialized);
82 
83     actualProfile = glfeatures::getCurrentContextProfile();
84     actualExtensions.getCurrentContextExtensions(actualProfile);
85     actualFeatures.load(actualProfile, actualExtensions);
86 
87     /* Ensure we got a matching profile.
88      *
89      * In particular on MacOSX, there is no way to specify specific versions, so this is all we can do.
90      *
91      * Also, see if OpenGL ES can be handled through ARB_ES*_compatibility.
92      */
93     glfeatures::Profile expectedProfile = profile;
94     if (retrace::contextCheck && !actualProfile.matches(expectedProfile)) {
95         if (expectedProfile.api == glfeatures::API_GLES &&
96             actualProfile.api == glfeatures::API_GL &&
97             ((expectedProfile.major == 2 && actualExtensions.has("GL_ARB_ES2_compatibility")) ||
98              (expectedProfile.major == 3 && actualExtensions.has("GL_ARB_ES3_compatibility")))) {
99             std::cerr << "warning: context mismatch:"
100                       << " expected " << expectedProfile << ","
101                       << " but got " << actualProfile << " with GL_ARB_ES" << expectedProfile.major << "_compatibility\n";
102         } else {
103             std::cerr << "error: context mismatch: expected " << expectedProfile << ", but got " << actualProfile << "\n";
104             exit(1);
105         }
106     }
107 
108     initialized = true;
109 }
110 
111 
112 } /* namespace glws */
113