CRenderContext.cpp 3.3 KB

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