CRenderContext.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "common.h"
  2. #include <iostream>
  3. #include <GL/glew.h>
  4. #include "CRenderContext.h"
  5. #include "CVideo.h"
  6. #include "CWallpaper.h"
  7. namespace WallpaperEngine::Render
  8. {
  9. CRenderContext::CRenderContext (
  10. Drivers::CVideoDriver& driver, Input::CInputContext& input,
  11. CWallpaperApplication& app
  12. ) :
  13. m_defaultWallpaper (nullptr),
  14. m_driver (driver),
  15. m_app (app),
  16. m_input (input),
  17. m_textureCache (new CTextureCache (*this))
  18. {
  19. }
  20. void CRenderContext::render ()
  21. {
  22. bool firstFrame = true;
  23. bool renderFrame = true;
  24. for (const auto& cur : this->getOutput ().getViewports ())
  25. {
  26. cur.second->makeCurrent ();
  27. #if !NDEBUG
  28. std::string str = "Rendering to output " + cur.first;
  29. glPushDebugGroup (GL_DEBUG_SOURCE_APPLICATION, 0, -1, str.c_str ());
  30. #endif /* DEBUG */
  31. // search the background in the viewport selection
  32. auto ref = this->m_wallpapers.find (cur.first);
  33. // render the background
  34. if (ref != this->m_wallpapers.end ())
  35. ref->second->render (cur.second->viewport, this->getOutput ().renderVFlip (), renderFrame, firstFrame);
  36. else
  37. this->m_defaultWallpaper->render (
  38. cur.second->viewport, this->getOutput ().renderVFlip (), renderFrame, firstFrame
  39. );
  40. // scenes need to render a new frame for each viewport as they produce different results
  41. // but videos should only be rendered once per group of viewports
  42. firstFrame = false;
  43. #if !NDEBUG
  44. glPopDebugGroup ();
  45. #endif /* DEBUG */
  46. cur.second->swapOutput ();
  47. }
  48. // read the full texture into the image
  49. if (this->getOutput ().haveImageBuffer ())
  50. glReadPixels (
  51. 0, 0, this->getOutput ().getFullWidth (), this->getOutput ().getFullHeight (), GL_BGRA, GL_UNSIGNED_BYTE,
  52. this->getOutput ().getImageBuffer ()
  53. );
  54. // update the output with the given image
  55. this->getOutput ().updateRender ();
  56. // finally swap buffers
  57. this->m_driver.swapBuffers ();
  58. }
  59. void CRenderContext::setDefaultWallpaper (CWallpaper* wallpaper)
  60. {
  61. this->m_defaultWallpaper = wallpaper;
  62. }
  63. void CRenderContext::setWallpaper (const std::string& display, CWallpaper* wallpaper)
  64. {
  65. this->m_wallpapers.insert_or_assign (display, wallpaper);
  66. }
  67. Input::CInputContext& CRenderContext::getInputContext () const
  68. {
  69. return this->m_input;
  70. }
  71. const CWallpaperApplication& CRenderContext::getApp () const
  72. {
  73. return this->m_app;
  74. }
  75. const Drivers::CVideoDriver& CRenderContext::getDriver () const
  76. {
  77. return this->m_driver;
  78. }
  79. const Drivers::Output::COutput& CRenderContext::getOutput () const
  80. {
  81. return this->m_driver.getOutput ();
  82. }
  83. const ITexture* CRenderContext::resolveTexture (const std::string& name)
  84. {
  85. return this->m_textureCache->resolve (name);
  86. }
  87. }