LedFrameGrabber.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "LedFrameGrabber.h"
  2. #include <cstring>
  3. #include "WallpaperEngine/Led/LedLayout.h"
  4. #include "WallpaperEngine/Logging/Log.h"
  5. namespace WallpaperEngine::Render {
  6. namespace {
  7. const char* kVertex = R"(#version 330
  8. out vec2 v_TexCoord;
  9. void main () {
  10. vec2 corner = vec2 ((gl_VertexID << 1) & 2, gl_VertexID & 2);
  11. v_TexCoord = corner;
  12. gl_Position = vec4 (corner * 2.0 - 1.0, 0.0, 1.0);
  13. }
  14. )";
  15. const char* kFragment = R"(#version 330
  16. uniform sampler2D g_Texture0;
  17. uniform vec2 u_Texel;
  18. in vec2 v_TexCoord;
  19. out vec4 fragColor;
  20. void main () {
  21. vec2 step = u_Texel * 2.0;
  22. vec4 albedo = texture (g_Texture0, v_TexCoord - step) + texture (g_Texture0, v_TexCoord + step)
  23. + texture (g_Texture0, v_TexCoord + vec2 (-step.x, step.y)) + texture (g_Texture0, v_TexCoord + vec2 (step.x, -step.y));
  24. fragColor = vec4 (albedo.rgb * 0.25, 1.0);
  25. }
  26. )";
  27. GLuint compile (const GLenum type, const char* source) {
  28. const GLuint shader = glCreateShader (type);
  29. glShaderSource (shader, 1, &source, nullptr);
  30. glCompileShader (shader);
  31. GLint compiled = GL_FALSE;
  32. glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
  33. if (compiled == GL_FALSE) {
  34. char log[1024] = {};
  35. glGetShaderInfoLog (shader, sizeof (log) - 1, nullptr, log);
  36. sLog.error ("LED frame shader failed to compile: ", log);
  37. }
  38. return shader;
  39. }
  40. struct SavedState {
  41. GLint drawFramebuffer = 0, readFramebuffer = 0, program = 0, vao = 0, activeTexture = 0, texture = 0,
  42. sampler = 0;
  43. GLint packBuffer = 0;
  44. GLint viewport[4] = {};
  45. GLboolean colorMask[4] = {};
  46. GLboolean blend = GL_FALSE, depth = GL_FALSE, cull = GL_FALSE, scissor = GL_FALSE, stencil = GL_FALSE;
  47. GLboolean framebufferSrgb = GL_FALSE;
  48. SavedState () {
  49. glGetIntegerv (GL_DRAW_FRAMEBUFFER_BINDING, &drawFramebuffer);
  50. glGetIntegerv (GL_READ_FRAMEBUFFER_BINDING, &readFramebuffer);
  51. glGetIntegerv (GL_CURRENT_PROGRAM, &program);
  52. glGetIntegerv (GL_VERTEX_ARRAY_BINDING, &vao);
  53. glGetIntegerv (GL_ACTIVE_TEXTURE, &activeTexture);
  54. glActiveTexture (GL_TEXTURE0);
  55. glGetIntegerv (GL_TEXTURE_BINDING_2D, &texture);
  56. glGetIntegerv (GL_SAMPLER_BINDING, &sampler);
  57. glGetIntegerv (GL_PIXEL_PACK_BUFFER_BINDING, &packBuffer);
  58. glGetIntegerv (GL_VIEWPORT, viewport);
  59. glGetBooleanv (GL_COLOR_WRITEMASK, colorMask);
  60. blend = glIsEnabled (GL_BLEND);
  61. depth = glIsEnabled (GL_DEPTH_TEST);
  62. cull = glIsEnabled (GL_CULL_FACE);
  63. scissor = glIsEnabled (GL_SCISSOR_TEST);
  64. stencil = glIsEnabled (GL_STENCIL_TEST);
  65. framebufferSrgb = glIsEnabled (GL_FRAMEBUFFER_SRGB);
  66. }
  67. ~SavedState () {
  68. const auto set = [] (const GLenum capability, const GLboolean on) {
  69. if (on) {
  70. glEnable (capability);
  71. } else {
  72. glDisable (capability);
  73. }
  74. };
  75. glBindBuffer (GL_PIXEL_PACK_BUFFER, packBuffer);
  76. glBindFramebuffer (GL_DRAW_FRAMEBUFFER, drawFramebuffer);
  77. glBindFramebuffer (GL_READ_FRAMEBUFFER, readFramebuffer);
  78. glUseProgram (program);
  79. glBindVertexArray (vao);
  80. glActiveTexture (GL_TEXTURE0);
  81. glBindTexture (GL_TEXTURE_2D, texture);
  82. glBindSampler (0, sampler);
  83. glActiveTexture (activeTexture);
  84. glViewport (viewport[0], viewport[1], viewport[2], viewport[3]);
  85. glColorMask (colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
  86. set (GL_BLEND, blend);
  87. set (GL_DEPTH_TEST, depth);
  88. set (GL_CULL_FACE, cull);
  89. set (GL_SCISSOR_TEST, scissor);
  90. set (GL_STENCIL_TEST, stencil);
  91. set (GL_FRAMEBUFFER_SRGB, framebufferSrgb);
  92. }
  93. };
  94. }
  95. LedFrameGrabber::~LedFrameGrabber () {
  96. if (!this->m_created) {
  97. return;
  98. }
  99. glDeleteProgram (this->m_program);
  100. glDeleteTextures (1, &this->m_target);
  101. glDeleteFramebuffers (1, &this->m_framebuffer);
  102. glDeleteSamplers (1, &this->m_sampler);
  103. glDeleteVertexArrays (1, &this->m_vao);
  104. glDeleteBuffers (2, this->m_buffers);
  105. }
  106. void LedFrameGrabber::create () {
  107. this->m_created = true;
  108. const GLuint vertex = compile (GL_VERTEX_SHADER, kVertex);
  109. const GLuint fragment = compile (GL_FRAGMENT_SHADER, kFragment);
  110. this->m_program = glCreateProgram ();
  111. glAttachShader (this->m_program, vertex);
  112. glAttachShader (this->m_program, fragment);
  113. glLinkProgram (this->m_program);
  114. glDeleteShader (vertex);
  115. glDeleteShader (fragment);
  116. glUseProgram (this->m_program);
  117. glUniform1i (glGetUniformLocation (this->m_program, "g_Texture0"), 0);
  118. this->m_texelLocation = glGetUniformLocation (this->m_program, "u_Texel");
  119. glGenTextures (1, &this->m_target);
  120. glBindTexture (GL_TEXTURE_2D, this->m_target);
  121. glTexImage2D (
  122. GL_TEXTURE_2D, 0, GL_RGBA8, Led::kFrameWidth, Led::kFrameHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr
  123. );
  124. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  125. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  126. glGenFramebuffers (1, &this->m_framebuffer);
  127. glBindFramebuffer (GL_FRAMEBUFFER, this->m_framebuffer);
  128. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->m_target, 0);
  129. glGenSamplers (1, &this->m_sampler);
  130. glSamplerParameteri (this->m_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  131. glSamplerParameteri (this->m_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  132. glSamplerParameteri (this->m_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  133. glSamplerParameteri (this->m_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  134. glGenVertexArrays (1, &this->m_vao);
  135. glGenBuffers (2, this->m_buffers);
  136. for (const GLuint buffer : this->m_buffers) {
  137. glBindBuffer (GL_PIXEL_PACK_BUFFER, buffer);
  138. glBufferData (GL_PIXEL_PACK_BUFFER, Led::kFrameWidth * Led::kFrameHeight * 4, nullptr, GL_STREAM_READ);
  139. }
  140. }
  141. bool LedFrameGrabber::grab (const GLuint texture, const int width, const int height, std::vector<uint8_t>& out) {
  142. if (texture == 0 || width <= 0 || height <= 0) {
  143. return false;
  144. }
  145. const SavedState saved;
  146. if (!this->m_created) {
  147. this->create ();
  148. }
  149. glDisable (GL_BLEND);
  150. glDisable (GL_DEPTH_TEST);
  151. glDisable (GL_CULL_FACE);
  152. glDisable (GL_SCISSOR_TEST);
  153. glDisable (GL_STENCIL_TEST);
  154. glDisable (GL_FRAMEBUFFER_SRGB);
  155. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  156. glBindFramebuffer (GL_FRAMEBUFFER, this->m_framebuffer);
  157. glViewport (0, 0, Led::kFrameWidth, Led::kFrameHeight);
  158. glUseProgram (this->m_program);
  159. glUniform2f (this->m_texelLocation, 1.0f / static_cast<float> (width), 1.0f / static_cast<float> (height));
  160. glActiveTexture (GL_TEXTURE0);
  161. glBindTexture (GL_TEXTURE_2D, texture);
  162. glBindSampler (0, this->m_sampler);
  163. glBindVertexArray (this->m_vao);
  164. glDrawArrays (GL_TRIANGLES, 0, 3);
  165. const int current = this->m_slot;
  166. const int previous = 1 - current;
  167. glReadBuffer (GL_COLOR_ATTACHMENT0);
  168. glBindBuffer (GL_PIXEL_PACK_BUFFER, this->m_buffers[current]);
  169. glReadPixels (0, 0, Led::kFrameWidth, Led::kFrameHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  170. bool produced = false;
  171. if (this->m_pending) {
  172. glBindBuffer (GL_PIXEL_PACK_BUFFER, this->m_buffers[previous]);
  173. if (const auto* rgba = static_cast<const uint8_t*> (glMapBuffer (GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
  174. rgba != nullptr) {
  175. out.resize (Led::kFrameBytes);
  176. for (int row = 0; row < Led::kFrameHeight; row++) {
  177. const uint8_t* from = rgba + static_cast<size_t> (row) * Led::kFrameWidth * 4;
  178. uint8_t* to = out.data () + static_cast<size_t> (row) * Led::kFrameWidth * 3;
  179. for (int column = 0; column < Led::kFrameWidth; column++) {
  180. to[column * 3] = from[column * 4];
  181. to[column * 3 + 1] = from[column * 4 + 1];
  182. to[column * 3 + 2] = from[column * 4 + 2];
  183. }
  184. }
  185. glUnmapBuffer (GL_PIXEL_PACK_BUFFER);
  186. produced = true;
  187. }
  188. }
  189. this->m_pending = true;
  190. this->m_slot = previous;
  191. return produced;
  192. }
  193. }