/opengles/src/Config.cpp

http://ftk.googlecode.com/ · C++ · 446 lines · 315 code · 83 blank · 48 comment · 19 complexity · 90f7ae1c6403c657b613a9f7ff1e1311 MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // Config.cpp Configuration Management Class
  4. // for 3D Rendering Library
  5. //
  6. // --------------------------------------------------------------------------
  7. //
  8. // 08-14-2003 Hans-Martin Will initial version
  9. //
  10. // --------------------------------------------------------------------------
  11. //
  12. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  13. //
  14. // Redistribution and use in source and binary forms, with or without
  15. // modification, are permitted provided that the following conditions are
  16. // met:
  17. //
  18. // * Redistributions of source code must retain the above copyright
  19. // notice, this list of conditions and the following disclaimer.
  20. // * Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the distribution.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  29. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. // THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // ==========================================================================
  37. #include "stdafx.h"
  38. #include "Config.h"
  39. using namespace EGL;
  40. // --------------------------------------------------------------------------
  41. // Table of Supported Configurations
  42. // --------------------------------------------------------------------------
  43. namespace {
  44. const EGL::Config s_AllConfigurations[] = {
  45. // ----------------------------------------------------------------------
  46. // Initial default configuration
  47. // RGB 565, as PBuffer or Windows surface
  48. // ----------------------------------------------------------------------
  49. Config(
  50. 32, // EGLint bufferSize,
  51. 5, // EGLint redSize,
  52. 6, // EGLint greenSize,
  53. 5, // EGLint blueSize,
  54. 8, // EGLint alphaSize,
  55. EGL_SLOW_CONFIG,// EGLint configCaveat,
  56. 1, // EGLint configID,
  57. 16, // EGLint depthSize,
  58. 0, // EGLint level,
  59. 1024, // EGLint maxPBufferWidth,
  60. 1024, // EGLint maxPBufferHeight,
  61. 1024 * 1024, // EGLint mxPBufferPixels,
  62. EGL_FALSE, // EGLint nativeRenderable,
  63. 0, // EGLint nativeVisualID,
  64. EGL_NONE, // EGLint nativeVisualType,
  65. 0, // EGLint sampleBuffers,
  66. 0, // EGLint samples,
  67. 32, // EGLint stencilSize,
  68. EGL_PBUFFER_BIT | EGL_WINDOW_BIT,// EGLint surfaceType,
  69. EGL_NONE, // EGLint transparentType,
  70. 0, // EGLint transparentRedValue,
  71. 0, // EGLint transparentGreenValue,
  72. 0, // EGLint transparentBlueValue,
  73. 240, // EGLint width,
  74. 320 // EGLint height
  75. )
  76. };
  77. // total number of supported configurations
  78. const int s_NumConfigurations = 1;
  79. };
  80. Config :: Config(
  81. EGLint bufferSize,
  82. EGLint redSize,
  83. EGLint greenSize,
  84. EGLint blueSize,
  85. EGLint alphaSize,
  86. EGLint configCaveat,
  87. EGLint configID,
  88. EGLint depthSize,
  89. EGLint level,
  90. EGLint maxPBufferWidth,
  91. EGLint maxPBufferHeight,
  92. EGLint mxPBufferPixels,
  93. EGLint nativeRenderable,
  94. EGLint nativeVisualID,
  95. EGLint nativeVisualType,
  96. EGLint sampleBuffers,
  97. EGLint samples,
  98. EGLint stencilSize,
  99. EGLint surfaceType,
  100. EGLint transparentType,
  101. EGLint transparentRedValue,
  102. EGLint transparentGreenValue,
  103. EGLint transparentBlueValue,
  104. EGLint width,
  105. EGLint height):
  106. m_BufferSize(bufferSize),
  107. m_RedSize(redSize),
  108. m_GreenSize(greenSize),
  109. m_BlueSize(blueSize),
  110. m_AlphaSize(alphaSize),
  111. m_ConfigCaveat(configCaveat),
  112. m_ConfigID(configID),
  113. m_DepthSize(depthSize),
  114. m_Level(level),
  115. m_MaxPBufferWidth(maxPBufferWidth),
  116. m_MaxPBufferHeight(maxPBufferHeight),
  117. m_MaxPBufferPixels(mxPBufferPixels),
  118. m_NativeRenderable(nativeRenderable),
  119. m_NativeVisualID(nativeVisualID),
  120. m_NativeVisualType(nativeVisualType),
  121. m_SampleBuffers(sampleBuffers),
  122. m_Samples(samples),
  123. m_StencilSize(stencilSize),
  124. m_SurfaceType(surfaceType),
  125. m_TransparentType(transparentType),
  126. m_TransparentRedValue(transparentRedValue),
  127. m_TransparentGreenValue(transparentGreenValue),
  128. m_TransparentBlueValue(transparentBlueValue),
  129. m_Width(width),
  130. m_Height(height)
  131. {
  132. }
  133. Config :: Config (const Config &other, const EGLint * attribList, const EGLint * validAttributes) {
  134. m_BufferSize = other.m_BufferSize;
  135. m_RedSize = other.m_RedSize;
  136. m_GreenSize = other.m_GreenSize;
  137. m_BlueSize = other.m_BlueSize;
  138. m_AlphaSize = other.m_AlphaSize;
  139. m_ConfigCaveat = other.m_ConfigCaveat;
  140. m_ConfigID = other.m_ConfigID;
  141. m_DepthSize = other.m_DepthSize;
  142. m_Level = other.m_Level;
  143. m_MaxPBufferWidth = other.m_MaxPBufferWidth;
  144. m_MaxPBufferHeight = other.m_MaxPBufferHeight;
  145. m_MaxPBufferPixels = other.m_MaxPBufferPixels;
  146. m_NativeRenderable = other.m_NativeRenderable;
  147. m_NativeVisualID = other.m_NativeVisualID;
  148. m_NativeVisualType = other.m_NativeVisualType;
  149. m_SampleBuffers = other.m_SampleBuffers;
  150. m_Samples = other.m_Samples;
  151. m_StencilSize = other.m_StencilSize;
  152. m_SurfaceType = other.m_SurfaceType;
  153. m_TransparentType = other.m_TransparentType;
  154. m_TransparentRedValue = other.m_TransparentRedValue;
  155. m_TransparentGreenValue = other.m_TransparentGreenValue;
  156. m_TransparentBlueValue = other.m_TransparentBlueValue;
  157. m_Width = other.m_Width;
  158. m_Height = other.m_Height;
  159. if (attribList != 0 && validAttributes != 0) {
  160. while (*attribList != EGL_NONE) {
  161. if (IsValidAttribute(validAttributes, *attribList)) {
  162. SetConfigAttrib(attribList[0], attribList[1]);
  163. }
  164. attribList += 2;
  165. }
  166. }
  167. }
  168. Config & Config :: operator=(const Config & other) {
  169. m_BufferSize = other.m_BufferSize;
  170. m_RedSize = other.m_RedSize;
  171. m_GreenSize = other.m_GreenSize;
  172. m_BlueSize = other.m_BlueSize;
  173. m_AlphaSize = other.m_AlphaSize;
  174. m_ConfigCaveat = other.m_ConfigCaveat;
  175. m_ConfigID = other.m_ConfigID;
  176. m_DepthSize = other.m_DepthSize;
  177. m_Level = other.m_Level;
  178. m_MaxPBufferWidth = other.m_MaxPBufferWidth;
  179. m_MaxPBufferHeight = other.m_MaxPBufferHeight;
  180. m_MaxPBufferPixels = other.m_MaxPBufferPixels;
  181. m_NativeRenderable = other.m_NativeRenderable;
  182. m_NativeVisualID = other.m_NativeVisualID;
  183. m_NativeVisualType = other.m_NativeVisualType;
  184. m_SampleBuffers = other.m_SampleBuffers;
  185. m_Samples = other.m_Samples;
  186. m_StencilSize = other.m_StencilSize;
  187. m_SurfaceType = other.m_SurfaceType;
  188. m_TransparentType = other.m_TransparentType;
  189. m_TransparentRedValue = other.m_TransparentRedValue;
  190. m_TransparentGreenValue = other.m_TransparentGreenValue;
  191. m_TransparentBlueValue = other.m_TransparentBlueValue;
  192. m_Width = other.m_Width;
  193. m_Height = other.m_Height;
  194. return *this;
  195. }
  196. EGLint Config :: GetConfigAttrib(EGLint attribute) const {
  197. switch (attribute) {
  198. case EGL_BUFFER_SIZE:
  199. return m_BufferSize;
  200. case EGL_ALPHA_SIZE:
  201. return m_AlphaSize;
  202. case EGL_BLUE_SIZE:
  203. return m_BlueSize;
  204. case EGL_GREEN_SIZE:
  205. return m_GreenSize;
  206. case EGL_RED_SIZE:
  207. return m_RedSize;
  208. case EGL_DEPTH_SIZE:
  209. return m_DepthSize;
  210. case EGL_STENCIL_SIZE:
  211. return m_StencilSize;
  212. case EGL_CONFIG_CAVEAT:
  213. return m_ConfigCaveat;
  214. case EGL_CONFIG_ID:
  215. return m_ConfigID;
  216. case EGL_LEVEL:
  217. return m_Level;
  218. case EGL_MAX_PBUFFER_HEIGHT:
  219. return m_MaxPBufferHeight;
  220. case EGL_MAX_PBUFFER_PIXELS:
  221. return m_MaxPBufferPixels;
  222. case EGL_MAX_PBUFFER_WIDTH:
  223. return m_MaxPBufferWidth;
  224. case EGL_NATIVE_RENDERABLE:
  225. return m_NativeRenderable;
  226. case EGL_NATIVE_VISUAL_ID:
  227. return m_NativeVisualID;
  228. case EGL_NATIVE_VISUAL_TYPE:
  229. return m_NativeVisualType;
  230. case EGL_SAMPLES:
  231. return m_Samples;
  232. case EGL_SAMPLE_BUFFERS:
  233. return m_SampleBuffers;
  234. case EGL_SURFACE_TYPE:
  235. return m_SurfaceType;
  236. case EGL_TRANSPARENT_TYPE:
  237. return m_TransparentType;
  238. case EGL_TRANSPARENT_BLUE_VALUE:
  239. return m_TransparentBlueValue;
  240. case EGL_TRANSPARENT_GREEN_VALUE:
  241. return m_TransparentGreenValue;
  242. case EGL_TRANSPARENT_RED_VALUE:
  243. return m_TransparentRedValue;
  244. case EGL_WIDTH:
  245. return m_Width;
  246. case EGL_HEIGHT:
  247. return m_Height;
  248. default:
  249. return EGL_NONE;
  250. }
  251. }
  252. void Config :: SetConfigAttrib(EGLint attribute, EGLint value) {
  253. switch (attribute) {
  254. case EGL_BUFFER_SIZE:
  255. m_BufferSize = value;
  256. break;
  257. case EGL_ALPHA_SIZE:
  258. m_AlphaSize = value;
  259. break;
  260. case EGL_BLUE_SIZE:
  261. m_BlueSize = value;
  262. break;
  263. case EGL_GREEN_SIZE:
  264. m_GreenSize = value;
  265. break;
  266. case EGL_RED_SIZE:
  267. m_RedSize = value;
  268. break;
  269. case EGL_DEPTH_SIZE:
  270. m_DepthSize = value;
  271. break;
  272. case EGL_STENCIL_SIZE:
  273. m_StencilSize = value;
  274. break;
  275. case EGL_CONFIG_CAVEAT:
  276. m_ConfigCaveat = value;
  277. break;
  278. case EGL_CONFIG_ID:
  279. m_ConfigID = value;
  280. break;
  281. case EGL_LEVEL:
  282. m_Level = value;
  283. break;
  284. case EGL_MAX_PBUFFER_HEIGHT:
  285. m_MaxPBufferHeight = value;
  286. break;
  287. case EGL_MAX_PBUFFER_PIXELS:
  288. m_MaxPBufferPixels = value;
  289. break;
  290. case EGL_MAX_PBUFFER_WIDTH:
  291. m_MaxPBufferWidth = value;
  292. break;
  293. case EGL_NATIVE_RENDERABLE:
  294. m_NativeRenderable = value;
  295. break;
  296. case EGL_NATIVE_VISUAL_ID:
  297. m_NativeVisualID = value;
  298. break;
  299. case EGL_NATIVE_VISUAL_TYPE:
  300. m_NativeVisualType = value;
  301. break;
  302. case EGL_SAMPLES:
  303. m_Samples = value;
  304. break;
  305. case EGL_SAMPLE_BUFFERS:
  306. m_SampleBuffers = value;
  307. break;
  308. case EGL_SURFACE_TYPE:
  309. m_SurfaceType = value;
  310. break;
  311. case EGL_TRANSPARENT_TYPE:
  312. m_TransparentType = value;
  313. break;
  314. case EGL_TRANSPARENT_BLUE_VALUE:
  315. m_TransparentBlueValue = value;
  316. break;
  317. case EGL_TRANSPARENT_GREEN_VALUE:
  318. m_TransparentGreenValue = value;
  319. break;
  320. case EGL_TRANSPARENT_RED_VALUE:
  321. m_TransparentRedValue = value;
  322. break;
  323. case EGL_WIDTH:
  324. m_Width = value;
  325. break;
  326. case EGL_HEIGHT:
  327. m_Height = value;
  328. break;
  329. }
  330. }
  331. bool Config :: IsValidAttribute(const EGLint * validAttributes, EGLint attribute) {
  332. if (validAttributes == 0) {
  333. return false;
  334. }
  335. while (*validAttributes != EGL_NONE) {
  336. if (*validAttributes == attribute) {
  337. return true;
  338. }
  339. validAttributes += 2;
  340. }
  341. return false;
  342. }
  343. EGLBoolean Config :: GetConfigs(EGLConfig * result, EGLint configSize, EGLint * numConfig) {
  344. if (result == 0) {
  345. // special case: inquire number of configurations available
  346. *numConfig = s_NumConfigurations;
  347. return EGL_TRUE;
  348. }
  349. if (configSize > s_NumConfigurations) {
  350. configSize = s_NumConfigurations;
  351. }
  352. for (int index = 0; index < configSize; ++index) {
  353. result[index] = &s_AllConfigurations[index];
  354. }
  355. *numConfig = configSize;
  356. return EGL_TRUE;
  357. }
  358. EGLBoolean Config :: ChooseConfig(const EGLint * attribList, EGLConfig * result, EGLint configSize, EGLint * numConfig) {
  359. // for now, as we only have one configuration available, just return that.
  360. // to be a correct implementation, we will have at least to verify that
  361. // the attributes specified match that.
  362. return GetConfigs(result, configSize, numConfig);
  363. }