AndroidOGLVideo.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "AndroidOGLVideo.h"
  41. #include "../platform/platformVideo_ScriptBinding.h"
  42. //Luma: Tap support
  43. extern void createMouseTapEvent(S32 nbrTaps, S32 x, S32 y);
  44. //extern bool createAccelMoveEvent( UIAccelerationValue *accel );
  45. // TODO: Card Profiling code isn't doing anything.
  46. extern StringTableEntry gScreenOrientation;
  47. extern bool gScreenUpsideDown;
  48. //-Mat we should update the accelereometer once per frame
  49. U32 AccelerometerUpdateMS = sgTimeManagerProcessInterval; // defines period between accelerometer updates updates in Milliseconds
  50. #pragma mark -
  51. //-----------------------------------------------------------------------------------------
  52. // Creates a dummy AGL context, so that naughty objects that call OpenGL before the window
  53. // exists will not crash the game.
  54. // If for some reason we fail to get a default contet, assert -- something's very wrong.
  55. //-----------------------------------------------------------------------------------------
  56. void initDummyAgl(void)
  57. {
  58. }
  59. U32 nAllDevs;
  60. #pragma mark -
  61. //------------------------------------------------------------------------------
  62. OpenGLDevice::OpenGLDevice()
  63. {
  64. // Set the device name:
  65. mDeviceName = "OpenGL";
  66. // macs games are not generally full screen only
  67. mFullScreenOnly = false;
  68. }
  69. //------------------------------------------------------------------------------
  70. void OpenGLDevice::initDevice()
  71. {
  72. //instead of caling enum monitors and enumdisplaymodes on that, we're just going to put in the ones that we know we have
  73. mResolutionList.push_back(Resolution(320, 480, 16));
  74. mResolutionList.push_back(Resolution(480, 320, 16));
  75. mResolutionList.push_back(Resolution(320, 480, 32));
  76. mResolutionList.push_back(Resolution(480, 320, 32));
  77. mResolutionList.push_back(Resolution(640, 960, 16));
  78. mResolutionList.push_back(Resolution(960, 640, 16));
  79. mResolutionList.push_back(Resolution(640, 960, 32));
  80. mResolutionList.push_back(Resolution(960, 640, 32));
  81. mResolutionList.push_back(Resolution(768, 1024, 16));
  82. mResolutionList.push_back(Resolution(1024, 768, 16));
  83. mResolutionList.push_back(Resolution(768, 1024, 32));
  84. mResolutionList.push_back(Resolution(1024, 768, 32));
  85. mResolutionList.push_back(Resolution(1536, 2048, 16));
  86. mResolutionList.push_back(Resolution(2048, 1536, 16));
  87. mResolutionList.push_back(Resolution(1536, 2048, 32));
  88. mResolutionList.push_back(Resolution(2048, 1536, 32));
  89. mResolutionList.push_back(Resolution(640, 1136, 16));
  90. mResolutionList.push_back(Resolution(1136, 640, 16));
  91. mResolutionList.push_back(Resolution(640, 1136, 32));
  92. mResolutionList.push_back(Resolution(1136, 640, 32));
  93. }
  94. //------------------------------------------------------------------------------
  95. // Activate
  96. // this is called once, as a result of createCanvas() in scripts.
  97. // dumps OpenGL driver info for the current screen
  98. // creates an initial window via setScreenMode
  99. bool OpenGLDevice::activate(U32 width, U32 height, U32 bpp, bool fullScreen)
  100. {
  101. Con::printf(" OpenGLDevice activating...");
  102. // gets opengl rendering capabilities of the screen pointed to by platState.hDisplay
  103. // sets up dgl with the capabilities info, & reports opengl status.
  104. getGLCapabilities();
  105. // Create the window or capture fullscreen
  106. if (!setScreenMode(width, height, bpp, fullScreen, true, false))
  107. return false;
  108. // set the displayDevice pref to "OpenGL"
  109. Con::setVariable("$pref::Video::displayDevice", mDeviceName);
  110. // set vertical sync now because it doesnt need setting every time we setScreenMode()
  111. setVerticalSync(!Con::getBoolVariable("$pref::Video::disableVerticalSync"));
  112. return true;
  113. }
  114. //------------------------------------------------------------------------------
  115. // returns TRUE if textures need resurrecting in future...
  116. //------------------------------------------------------------------------------
  117. bool OpenGLDevice::cleanupContextAndWindow()
  118. {
  119. Con::printf("Cleaning up the display device...");
  120. return true;
  121. }
  122. //------------------------------------------------------------------------------
  123. void OpenGLDevice::shutdown()
  124. {
  125. Con::printf("Shutting down the OpenGL display device...");
  126. }
  127. //------------------------------------------------------------------------------
  128. // This is the real workhorse function of the DisplayDevice...
  129. //
  130. bool OpenGLDevice::setScreenMode(U32 width, U32 height, U32 bpp, bool fullScreen, bool forceIt, bool repaint)
  131. {
  132. Con::printf(" set screen mode %i x %i x %i, %s, %s, %s", width, height, bpp,
  133. fullScreen ? "fullscreen" : "windowed",
  134. forceIt ? "force it" : "dont force it",
  135. repaint ? "repaint" : "dont repaint"
  136. );
  137. // validation, early outs --------------------------------------------------
  138. // sanity check. some scripts are liable to pass in bad values.
  139. if (!bpp)
  140. bpp = platState.desktopBitsPixel;
  141. Resolution newRes = Resolution(width, height, bpp);
  142. // if no values changing and we're not forcing a change, kick out. prevents thrashing.
  143. if (!forceIt && smIsFullScreen == fullScreen && smCurrentRes == newRes)
  144. return (true);
  145. // we have a new context, this is now safe to do:
  146. // delete any contexts or windows that exist, and kill the texture manager.
  147. bool needResurrect = cleanupContextAndWindow();
  148. Con::printf(">> Attempting to change display settings to %s %dx%dx%d...",
  149. fullScreen ? "fullscreen" : "windowed", newRes.w, newRes.h, newRes.bpp);
  150. // set torque variables ----------------------------------------------------
  151. // save window size for dgl
  152. Platform::setWindowSize(newRes.w, newRes.h);
  153. // update smIsFullScreen and pref
  154. smIsFullScreen = fullScreen;
  155. Con::setBoolVariable("$pref::Video::fullScreen", smIsFullScreen);
  156. // save resolution
  157. smCurrentRes = newRes;
  158. // save resolution to prefs
  159. char buf[32];
  160. if (fullScreen)
  161. {
  162. dSprintf(buf, sizeof(buf), "%d %d %d", newRes.w, newRes.h, newRes.bpp);
  163. Con::setVariable("$pref::Video::resolution", buf);
  164. }
  165. else
  166. {
  167. dSprintf(buf, sizeof(buf), "%d %d", newRes.w, newRes.h);
  168. Con::setVariable("$pref::Video::windowedRes", buf);
  169. }
  170. // begin rendering again ----------------------------------------------------
  171. if (needResurrect)
  172. {
  173. // Reload the textures gl names
  174. Con::printf("Resurrecting the texture manager...");
  175. Game->textureResurrect();
  176. }
  177. if (repaint)
  178. Con::evaluate("resetCanvas();");
  179. return true;
  180. }
  181. //------------------------------------------------------------------------------
  182. void OpenGLDevice::swapBuffers()
  183. {
  184. eglSwapBuffers(platState.engine->display, platState.engine->surface);
  185. }
  186. //------------------------------------------------------------------------------
  187. const char *OpenGLDevice::getDriverInfo()
  188. {
  189. return NULL;
  190. }
  191. struct AndroidGamma
  192. {
  193. F32 r, g, b;
  194. F32 scale;
  195. };
  196. //------------------------------------------------------------------------------
  197. bool OpenGLDevice::getGammaCorrection(F32 &g)
  198. {
  199. return true;
  200. }
  201. //------------------------------------------------------------------------------
  202. bool OpenGLDevice::setGammaCorrection(F32 g)
  203. {
  204. return true;
  205. }
  206. //------------------------------------------------------------------------------
  207. bool OpenGLDevice::getVerticalSync()
  208. {
  209. return true;
  210. }
  211. //------------------------------------------------------------------------------
  212. bool OpenGLDevice::setVerticalSync(bool on)
  213. {
  214. return true;
  215. }
  216. Resolution Video::getDesktopResolution()
  217. {
  218. return Resolution(_AndroidGetScreenWidth(), _AndroidGetScreenHeight(), ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH);
  219. }
  220. DisplayDevice *OpenGLDevice::create()
  221. {
  222. // set up a dummy default agl context.
  223. // this will be replaced later with the window's context,
  224. // but we need agl_ctx to be valid at all times,
  225. // since some things try to make gl calls before the device is activated.
  226. // create the DisplayDevice
  227. OpenGLDevice *newOGLDevice = new OpenGLDevice();
  228. newOGLDevice->initDevice();
  229. return newOGLDevice;
  230. }