AndroidOGLVideo.cpp 10 KB

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