gles2_debayer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "gles2_debayer.h"
  2. #include "camera.h"
  3. #include "gl_util.h"
  4. #include <stdlib.h>
  5. #define VERTEX_ATTRIBUTE 0
  6. #define TEX_COORD_ATTRIBUTE 1
  7. struct _GLES2Debayer {
  8. int format;
  9. GLuint frame_buffer;
  10. GLuint program;
  11. GLuint uniform_transform;
  12. GLuint uniform_pixel_size;
  13. GLuint uniform_padding_ratio;
  14. GLuint uniform_texture;
  15. GLuint uniform_color_matrix;
  16. GLuint uniform_row_length;
  17. GLuint quad;
  18. };
  19. GLES2Debayer *
  20. gles2_debayer_new(int format)
  21. {
  22. uint32_t pixfmt = libmegapixels_format_to_v4l_pixfmt(format);
  23. if (pixfmt != V4L2_PIX_FMT_SBGGR8 && pixfmt != V4L2_PIX_FMT_SGBRG8 &&
  24. pixfmt != V4L2_PIX_FMT_SGRBG8 && pixfmt != V4L2_PIX_FMT_SRGGB8 &&
  25. pixfmt != V4L2_PIX_FMT_SBGGR10P && pixfmt != V4L2_PIX_FMT_SGBRG10P &&
  26. pixfmt != V4L2_PIX_FMT_SGRBG10P && pixfmt != V4L2_PIX_FMT_SRGGB10P) {
  27. return NULL;
  28. }
  29. GLuint frame_buffer;
  30. glGenFramebuffers(1, &frame_buffer);
  31. check_gl();
  32. char format_def[64];
  33. snprintf(format_def,
  34. 64,
  35. "#define CFA_%s\n#define BITS_%d\n",
  36. libmegapixels_format_cfa_pattern(format),
  37. libmegapixels_format_bits_per_pixel(format));
  38. const GLchar *def[1] = { format_def };
  39. GLuint shaders[] = {
  40. gl_util_load_shader("/org/postmarketos/Megapixels/debayer.vert",
  41. GL_VERTEX_SHADER,
  42. NULL,
  43. 0),
  44. gl_util_load_shader("/org/postmarketos/Megapixels/debayer.frag",
  45. GL_FRAGMENT_SHADER,
  46. def,
  47. 1),
  48. };
  49. GLuint program = gl_util_link_program(shaders, 2);
  50. glBindAttribLocation(program, VERTEX_ATTRIBUTE, "vert");
  51. glBindAttribLocation(program, TEX_COORD_ATTRIBUTE, "tex_coord");
  52. check_gl();
  53. GLES2Debayer *self = malloc(sizeof(GLES2Debayer));
  54. self->format = format;
  55. self->frame_buffer = frame_buffer;
  56. self->program = program;
  57. self->uniform_transform = glGetUniformLocation(self->program, "transform");
  58. self->uniform_pixel_size = glGetUniformLocation(self->program, "pixel_size");
  59. self->uniform_padding_ratio =
  60. glGetUniformLocation(self->program, "padding_ratio");
  61. self->uniform_texture = glGetUniformLocation(self->program, "texture");
  62. self->uniform_color_matrix =
  63. glGetUniformLocation(self->program, "color_matrix");
  64. if (libmegapixels_format_bits_per_pixel(self->format) == 10)
  65. self->uniform_row_length =
  66. glGetUniformLocation(self->program, "row_length");
  67. check_gl();
  68. self->quad = gl_util_new_quad();
  69. return self;
  70. }
  71. void
  72. gles2_debayer_free(GLES2Debayer *self)
  73. {
  74. glDeleteFramebuffers(1, &self->frame_buffer);
  75. glDeleteProgram(self->program);
  76. free(self);
  77. }
  78. void
  79. gles2_debayer_use(GLES2Debayer *self)
  80. {
  81. assert(self != NULL);
  82. glUseProgram(self->program);
  83. check_gl();
  84. gl_util_bind_quad(self->quad);
  85. }
  86. void
  87. gles2_debayer_configure(GLES2Debayer *self,
  88. const uint32_t dst_width,
  89. const uint32_t dst_height,
  90. const uint32_t src_width,
  91. const uint32_t src_height,
  92. const uint32_t rotation,
  93. const bool mirrored,
  94. const float *colormatrix,
  95. const uint8_t blacklevel)
  96. {
  97. glViewport(0, 0, (int)dst_width, (int)dst_height);
  98. check_gl();
  99. GLfloat rotation_list[4] = { 0, -1, 0, 1 };
  100. int rotation_index = 4 - (int)rotation / 90;
  101. GLfloat sin_rot = rotation_list[rotation_index];
  102. GLfloat cos_rot = rotation_list[(rotation_index + 1) % 4];
  103. GLfloat scale_x = mirrored ? 1 : -1;
  104. GLfloat matrix[9] = {
  105. // clang-format off
  106. cos_rot * scale_x, sin_rot, 0,
  107. -sin_rot * scale_x, cos_rot, 0,
  108. 0, 0, 1,
  109. // clang-format on
  110. };
  111. glUniformMatrix3fv(self->uniform_transform, 1, GL_FALSE, matrix);
  112. check_gl();
  113. GLfloat pixel_size_x = 1.0f / src_width;
  114. GLfloat pixel_size_y = 1.0f / src_height;
  115. glUniform2f(self->uniform_pixel_size, pixel_size_x, pixel_size_y);
  116. check_gl();
  117. if (colormatrix) {
  118. GLfloat transposed[9];
  119. for (int i = 0; i < 3; ++i)
  120. for (int j = 0; j < 3; ++j)
  121. transposed[i + j * 3] = colormatrix[j + i * 3];
  122. glUniformMatrix3fv(
  123. self->uniform_color_matrix, 1, GL_FALSE, transposed);
  124. } else {
  125. static const GLfloat identity[9] = {
  126. // clang-format off
  127. 1, 0, 0,
  128. 0, 1, 0,
  129. 0, 0, 1,
  130. // clang-format on
  131. };
  132. glUniformMatrix3fv(
  133. self->uniform_color_matrix, 1, GL_FALSE, identity);
  134. }
  135. check_gl();
  136. GLuint row_length =
  137. libmegapixels_mode_width_to_bytes(self->format, src_width);
  138. if (libmegapixels_format_bits_per_pixel(self->format) == 10) {
  139. assert(src_width % 4 == 0);
  140. glUniform1f(self->uniform_row_length, row_length);
  141. check_gl();
  142. }
  143. GLuint padding_bytes =
  144. libmegapixels_mode_width_to_padding(self->format, src_width);
  145. GLfloat padding_ratio = (float)row_length / (row_length + padding_bytes);
  146. glUniform1f(self->uniform_padding_ratio, padding_ratio);
  147. }
  148. void
  149. gles2_debayer_process(GLES2Debayer *self, GLuint dst_id, GLuint source_id)
  150. {
  151. glBindFramebuffer(GL_FRAMEBUFFER, self->frame_buffer);
  152. glBindTexture(GL_TEXTURE_2D, dst_id);
  153. glFramebufferTexture2D(
  154. GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_id, 0);
  155. check_gl();
  156. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  157. glActiveTexture(GL_TEXTURE0);
  158. glBindTexture(GL_TEXTURE_2D, source_id);
  159. glUniform1i(self->uniform_texture, 0);
  160. check_gl();
  161. gl_util_draw_quad(self->quad);
  162. }