sfwrapper.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2009 Alistair John Strachan <alistair@devzero.co.uk>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "sfwrapper.h"
  20. #include <ui/Rect.h>
  21. #include <ui/Surface.h>
  22. #include <ui/PixelFormat.h>
  23. #include <ui/DisplayInfo.h>
  24. #include <ui/SurfaceComposerClient.h>
  25. #include <binder/ProcessState.h>
  26. #include <binder/IPCThreadState.h>
  27. // Android has a util.h too
  28. #include "../../src/util.h"
  29. extern "C"
  30. {
  31. /* FIXME: Hack, remove! */
  32. void *__dso_handle = NULL;
  33. }
  34. namespace android {
  35. static sp<SurfaceComposerClient> mSession;
  36. static sp<SurfaceControl> mSurfaceControl;
  37. static sp<Surface> mSurface;
  38. extern "C" bool SurfaceFlingerInitialize(int target_width, int target_height,
  39. int depth, bool fullscreen)
  40. {
  41. if(mSession == NULL)
  42. {
  43. ProcessState::self()->startThreadPool();
  44. mSession = new SurfaceComposerClient();
  45. if(mSession == NULL)
  46. {
  47. warn("Failed to create SurfaceFlinger session\n");
  48. return false;
  49. }
  50. }
  51. if(mSurfaceControl == NULL)
  52. {
  53. uint32_t flags = 0;
  54. DisplayInfo dinfo;
  55. status_t status;
  56. int pixelFormat;
  57. status = mSession->getDisplayInfo(0, &dinfo);
  58. if(status)
  59. {
  60. warn("Failed to obtain display information\n");
  61. return false;
  62. }
  63. if((unsigned int)target_width > dinfo.w)
  64. target_width = dinfo.w;
  65. if((unsigned int)target_height > dinfo.h)
  66. target_height = dinfo.h;
  67. if(depth == 32)
  68. pixelFormat = PIXEL_FORMAT_RGBA_8888;
  69. else
  70. pixelFormat = PIXEL_FORMAT_RGB_565;
  71. // FIXME: Don't assume GL renderers
  72. flags |= ISurfaceComposer::eHardware | ISurfaceComposer::eGPU;
  73. mSurfaceControl = mSession->createSurface(getpid(), 0,
  74. target_width, target_height, pixelFormat, flags);
  75. if(mSurfaceControl == NULL)
  76. {
  77. warn("Failed to create surface control\n");
  78. return false;
  79. }
  80. debug("Asked SurfaceFlinger for a %dx%d (format %d) surface\n",
  81. target_width, target_height, pixelFormat);
  82. if(fullscreen)
  83. {
  84. mSession->openTransaction();
  85. mSurfaceControl->setLayer(0x40000000);
  86. mSession->closeTransaction();
  87. }
  88. }
  89. if(mSurface == NULL)
  90. {
  91. mSurface = mSurfaceControl->getSurface();
  92. assert(mSurface != NULL);
  93. }
  94. return true;
  95. }
  96. extern "C" void SurfaceFlingerDeinitialize(void)
  97. {
  98. if(mSurface != NULL)
  99. {
  100. mSurface.clear();
  101. mSurface = NULL;
  102. }
  103. if(mSurfaceControl != NULL)
  104. {
  105. mSurfaceControl.clear();
  106. mSurfaceControl = NULL;
  107. }
  108. if(mSession != NULL)
  109. {
  110. mSession = NULL;
  111. IPCThreadState::self()->stopProcess();
  112. }
  113. }
  114. extern "C" NativeWindowType SurfaceFlingerGetNativeWindow(void)
  115. {
  116. assert(mSurface != NULL);
  117. return mSurface.get();
  118. }
  119. extern "C" void SurfaceFlingerSetSwapRectangle(int x, int y, int w, int h)
  120. {
  121. assert(mSurface != NULL);
  122. const android::Rect r(x, y, 0 + w, 0 + h);
  123. mSurface->setSwapRectangle(r);
  124. debug("Set swap rectangle (%d,%d) [%d,%d]\n", r.left, r.top, r.width(), r.height());
  125. }
  126. } // namespace android