AndroidOGLVideo.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Portions taken from OpenGL Full Screen.c sample from Apple Computer, Inc.
  23. // (that's where many of the lead helper functions originated from, but code
  24. // has been significantly changed & revised.)
  25. #include "platformAndroid/platformAndroid.h"
  26. #include "platformAndroid/platformGL.h"
  27. #include "platformAndroid/AndroidOGLVideo.h"
  28. #include "console/console.h"
  29. #include "math/mPoint.h"
  30. #include "platform/event.h"
  31. #include "game/gameInterface.h"
  32. #include "console/consoleInternal.h"
  33. #include "console/ast.h"
  34. #include "io/fileStream.h"
  35. #include "platformAndroid/AndroidUtil.h"
  36. #include "platformAndroid/AndroidEvents.h"
  37. #include "graphics/dgl.h"
  38. #include "debug/profiler.h"
  39. #include "platformAndroid/T2DActivity.h"
  40. //Luma: Tap support
  41. extern void createMouseTapEvent(S32 nbrTaps, S32 x, S32 y);
  42. //extern bool createAccelMoveEvent( UIAccelerationValue *accel );
  43. // TODO: Card Profiling code isn't doing anything.
  44. extern StringTableEntry gScreenOrientation;
  45. extern bool gScreenUpsideDown;
  46. //-Mat we should update the accelereometer once per frame
  47. U32 AccelerometerUpdateMS = sgTimeManagerProcessInterval; // defines period between accelerometer updates updates in Milliseconds
  48. #pragma mark -
  49. //-----------------------------------------------------------------------------------------
  50. // Creates a dummy AGL context, so that naughty objects that call OpenGL before the window
  51. // exists will not crash the game.
  52. // If for some reason we fail to get a default contet, assert -- something's very wrong.
  53. //-----------------------------------------------------------------------------------------
  54. void initDummyAgl(void)
  55. {
  56. }
  57. U32 nAllDevs;
  58. #pragma mark -
  59. //------------------------------------------------------------------------------
  60. OpenGLDevice::OpenGLDevice()
  61. {
  62. // Set the device name:
  63. mDeviceName = "OpenGL";
  64. // macs games are not generally full screen only
  65. mFullScreenOnly = false;
  66. }
  67. //------------------------------------------------------------------------------
  68. void OpenGLDevice::initDevice()
  69. {
  70. //instead of caling enum monitors and enumdisplaymodes on that, we're just going to put in the ones that we know we have
  71. mResolutionList.push_back(Resolution(320, 480, 16));
  72. mResolutionList.push_back(Resolution(480, 320, 16));
  73. mResolutionList.push_back(Resolution(320, 480, 32));
  74. mResolutionList.push_back(Resolution(480, 320, 32));
  75. mResolutionList.push_back(Resolution(640, 960, 16));
  76. mResolutionList.push_back(Resolution(960, 640, 16));
  77. mResolutionList.push_back(Resolution(640, 960, 32));
  78. mResolutionList.push_back(Resolution(960, 640, 32));
  79. mResolutionList.push_back(Resolution(768, 1024, 16));
  80. mResolutionList.push_back(Resolution(1024, 768, 16));
  81. mResolutionList.push_back(Resolution(768, 1024, 32));
  82. mResolutionList.push_back(Resolution(1024, 768, 32));
  83. mResolutionList.push_back(Resolution(1536, 2048, 16));
  84. mResolutionList.push_back(Resolution(2048, 1536, 16));
  85. mResolutionList.push_back(Resolution(1536, 2048, 32));
  86. mResolutionList.push_back(Resolution(2048, 1536, 32));
  87. mResolutionList.push_back(Resolution(640, 1136, 16));
  88. mResolutionList.push_back(Resolution(1136, 640, 16));
  89. mResolutionList.push_back(Resolution(640, 1136, 32));
  90. mResolutionList.push_back(Resolution(1136, 640, 32));
  91. }
  92. //------------------------------------------------------------------------------
  93. // Activate
  94. // this is called once, as a result of createCanvas() in scripts.
  95. // dumps OpenGL driver info for the current screen
  96. // creates an initial window via setScreenMode
  97. bool OpenGLDevice::activate(U32 width, U32 height, U32 bpp, bool fullScreen)
  98. {
  99. Con::printf(" OpenGLDevice activating...");
  100. // gets opengl rendering capabilities of the screen pointed to by platState.hDisplay
  101. // sets up dgl with the capabilities info, & reports opengl status.
  102. getGLCapabilities();
  103. // Create the window or capture fullscreen
  104. if (!setScreenMode(width, height, bpp, fullScreen, true, false))
  105. return false;
  106. // set the displayDevice pref to "OpenGL"
  107. Con::setVariable("$pref::Video::displayDevice", mDeviceName);
  108. // set vertical sync now because it doesnt need setting every time we setScreenMode()
  109. setVerticalSync(!Con::getBoolVariable("$pref::Video::disableVerticalSync"));
  110. return true;
  111. }
  112. //------------------------------------------------------------------------------
  113. // returns TRUE if textures need resurrecting in future...
  114. //------------------------------------------------------------------------------
  115. bool OpenGLDevice::cleanupContextAndWindow()
  116. {
  117. Con::printf("Cleaning up the display device...");
  118. return true;
  119. }
  120. //------------------------------------------------------------------------------
  121. void OpenGLDevice::shutdown()
  122. {
  123. Con::printf("Shutting down the OpenGL display device...");
  124. }
  125. //------------------------------------------------------------------------------
  126. // This is the real workhorse function of the DisplayDevice...
  127. //
  128. bool OpenGLDevice::setScreenMode(U32 width, U32 height, U32 bpp, bool fullScreen, bool forceIt, bool repaint)
  129. {
  130. Con::printf(" set screen mode %i x %i x %i, %s, %s, %s", width, height, bpp,
  131. fullScreen ? "fullscreen" : "windowed",
  132. forceIt ? "force it" : "dont force it",
  133. repaint ? "repaint" : "dont repaint"
  134. );
  135. // validation, early outs --------------------------------------------------
  136. // sanity check. some scripts are liable to pass in bad values.
  137. if (!bpp)
  138. bpp = platState.desktopBitsPixel;
  139. Resolution newRes = Resolution(width, height, bpp);
  140. // if no values changing and we're not forcing a change, kick out. prevents thrashing.
  141. if (!forceIt && smIsFullScreen == fullScreen && smCurrentRes == newRes)
  142. return (true);
  143. // we have a new context, this is now safe to do:
  144. // delete any contexts or windows that exist, and kill the texture manager.
  145. bool needResurrect = cleanupContextAndWindow();
  146. Con::printf(">> Attempting to change display settings to %s %dx%dx%d...",
  147. fullScreen ? "fullscreen" : "windowed", newRes.w, newRes.h, newRes.bpp);
  148. // set torque variables ----------------------------------------------------
  149. // save window size for dgl
  150. Platform::setWindowSize(newRes.w, newRes.h);
  151. // update smIsFullScreen and pref
  152. smIsFullScreen = fullScreen;
  153. Con::setBoolVariable("$pref::Video::fullScreen", smIsFullScreen);
  154. // save resolution
  155. smCurrentRes = newRes;
  156. // save resolution to prefs
  157. char buf[32];
  158. if (fullScreen)
  159. {
  160. dSprintf(buf, sizeof(buf), "%d %d %d", newRes.w, newRes.h, newRes.bpp);
  161. Con::setVariable("$pref::Video::resolution", buf);
  162. }
  163. else
  164. {
  165. dSprintf(buf, sizeof(buf), "%d %d", newRes.w, newRes.h);
  166. Con::setVariable("$pref::Video::windowedRes", buf);
  167. }
  168. // begin rendering again ----------------------------------------------------
  169. if (needResurrect)
  170. {
  171. // Reload the textures gl names
  172. Con::printf("Resurrecting the texture manager...");
  173. Game->textureResurrect();
  174. }
  175. if (repaint)
  176. Video::resetCanvas();
  177. return true;
  178. }
  179. //------------------------------------------------------------------------------
  180. void OpenGLDevice::swapBuffers()
  181. {
  182. eglSwapBuffers(platState.engine->display, platState.engine->surface);
  183. }
  184. //------------------------------------------------------------------------------
  185. const char *OpenGLDevice::getDriverInfo()
  186. {
  187. return NULL;
  188. }
  189. struct AndroidGamma
  190. {
  191. F32 r, g, b;
  192. F32 scale;
  193. };
  194. //------------------------------------------------------------------------------
  195. bool OpenGLDevice::getGammaCorrection(F32 &g)
  196. {
  197. return true;
  198. }
  199. //------------------------------------------------------------------------------
  200. bool OpenGLDevice::setGammaCorrection(F32 g)
  201. {
  202. return true;
  203. }
  204. //------------------------------------------------------------------------------
  205. bool OpenGLDevice::getVerticalSync()
  206. {
  207. return true;
  208. }
  209. //------------------------------------------------------------------------------
  210. bool OpenGLDevice::setVerticalSync(bool on)
  211. {
  212. return true;
  213. }
  214. Resolution Video::getDesktopResolution()
  215. {
  216. return Resolution(_AndroidGetScreenWidth(), _AndroidGetScreenHeight(), ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH);
  217. }
  218. DisplayDevice *OpenGLDevice::create()
  219. {
  220. // set up a dummy default agl context.
  221. // this will be replaced later with the window's context,
  222. // but we need agl_ctx to be valid at all times,
  223. // since some things try to make gl calls before the device is activated.
  224. // create the DisplayDevice
  225. OpenGLDevice *newOGLDevice = new OpenGLDevice();
  226. newOGLDevice->initDevice();
  227. return newOGLDevice;
  228. }