Painter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #define GL_GLEXT_PROTOTYPES
  7. #include "Painter.h"
  8. #include "Canvas.h"
  9. #include <GL/gl.h>
  10. #include <GL/glext.h>
  11. #include <LibGfx/Color.h>
  12. namespace AccelGfx {
  13. struct ColorComponents {
  14. float red;
  15. float green;
  16. float blue;
  17. float alpha;
  18. };
  19. static ColorComponents gfx_color_to_opengl_color(Gfx::Color color)
  20. {
  21. ColorComponents components;
  22. components.red = static_cast<float>(color.red()) / 255.0f;
  23. components.green = static_cast<float>(color.green()) / 255.0f;
  24. components.blue = static_cast<float>(color.blue()) / 255.0f;
  25. components.alpha = static_cast<float>(color.alpha()) / 255.0f;
  26. return components;
  27. }
  28. Gfx::FloatRect Painter::to_clip_space(Gfx::FloatRect const& screen_rect) const
  29. {
  30. float x = 2.0f * screen_rect.x() / m_canvas->width() - 1.0f;
  31. float y = -1.0f + 2.0f * screen_rect.y() / m_canvas->height();
  32. float width = 2.0f * screen_rect.width() / m_canvas->width();
  33. float height = 2.0f * screen_rect.height() / m_canvas->height();
  34. return { x, y, width, height };
  35. }
  36. char const* vertex_shader_source = R"(
  37. attribute vec2 aVertexPosition;
  38. void main() {
  39. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  40. }
  41. )";
  42. char const* solid_color_fragment_shader_source = R"(
  43. precision mediump float;
  44. uniform vec4 uColor;
  45. void main() {
  46. gl_FragColor = uColor;
  47. }
  48. )";
  49. char const* blit_vertex_shader_source = R"(
  50. attribute vec4 aVertexPosition;
  51. attribute vec2 aTextureCoord;
  52. varying vec2 vTextureCoord;
  53. void main() {
  54. gl_Position = aVertexPosition;
  55. vTextureCoord = aTextureCoord;
  56. }
  57. )";
  58. char const* blit_fragment_shader_source = R"(
  59. precision mediump float;
  60. varying vec2 vTextureCoord;
  61. uniform sampler2D uSampler;
  62. void main() {
  63. gl_FragColor = texture2D(uSampler, vTextureCoord);
  64. }
  65. )";
  66. OwnPtr<Painter> Painter::create()
  67. {
  68. auto& context = Context::the();
  69. return make<Painter>(context);
  70. }
  71. Painter::Painter(Context& context)
  72. : m_context(context)
  73. , m_rectangle_program(Program::create(vertex_shader_source, solid_color_fragment_shader_source))
  74. , m_blit_program(Program::create(blit_vertex_shader_source, blit_fragment_shader_source))
  75. {
  76. m_state_stack.empend(State());
  77. }
  78. Painter::~Painter()
  79. {
  80. flush();
  81. }
  82. void Painter::clear(Gfx::Color color)
  83. {
  84. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  85. glClearColor(red, green, blue, alpha);
  86. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87. }
  88. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  89. {
  90. fill_rect(rect.to_type<float>(), color);
  91. }
  92. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  93. {
  94. return {
  95. rect.left(),
  96. rect.top(),
  97. rect.left(),
  98. rect.bottom(),
  99. rect.right(),
  100. rect.bottom(),
  101. rect.right(),
  102. rect.top(),
  103. };
  104. }
  105. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  106. {
  107. // Draw a filled rect (with `color`) using OpenGL after mapping it through the current transform.
  108. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  109. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  110. m_rectangle_program.use();
  111. GLuint position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  112. GLuint color_uniform = m_rectangle_program.get_uniform_location("uColor");
  113. glUniform4f(color_uniform, red, green, blue, alpha);
  114. glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), vertices.data());
  115. glEnableVertexAttribArray(position_attribute);
  116. glEnable(GL_BLEND);
  117. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  118. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  119. }
  120. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  121. {
  122. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  123. }
  124. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  125. {
  126. auto x = rect.x() / image_size.width();
  127. auto y = rect.y() / image_size.height();
  128. auto width = rect.width() / image_size.width();
  129. auto height = rect.height() / image_size.height();
  130. return { x, y, width, height };
  131. }
  132. static GLenum to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  133. {
  134. switch (scaling_mode) {
  135. case Painter::ScalingMode::NearestNeighbor:
  136. return GL_NEAREST;
  137. case Painter::ScalingMode::Bilinear:
  138. return GL_LINEAR;
  139. default:
  140. VERIFY_NOT_REACHED();
  141. }
  142. }
  143. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  144. {
  145. m_blit_program.use();
  146. GLuint texture;
  147. // FIXME: We should reuse textures across repaints if possible.
  148. glGenTextures(1, &texture);
  149. glBindTexture(GL_TEXTURE_2D, texture);
  150. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap.width(), bitmap.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, bitmap.scanline(0));
  151. GLenum scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  153. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  154. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, scaling_mode_gl);
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, scaling_mode_gl);
  156. auto vertices = rect_to_vertices(to_clip_space(transform().map(dst_rect)));
  157. auto texture_coordinates = rect_to_vertices(to_texture_space(src_rect, bitmap.size()));
  158. GLuint vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  159. glVertexAttribPointer(vertex_position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), vertices.data());
  160. glEnableVertexAttribArray(vertex_position_attribute);
  161. GLuint texture_coord_attribute = m_blit_program.get_attribute_location("aTextureCoord");
  162. glVertexAttribPointer(texture_coord_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), texture_coordinates.data());
  163. glEnableVertexAttribArray(texture_coord_attribute);
  164. glEnable(GL_BLEND);
  165. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  166. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  167. glDeleteTextures(1, &texture);
  168. }
  169. void Painter::save()
  170. {
  171. m_state_stack.append(state());
  172. }
  173. void Painter::restore()
  174. {
  175. VERIFY(!m_state_stack.is_empty());
  176. m_state_stack.take_last();
  177. }
  178. void Painter::flush()
  179. {
  180. m_canvas->flush();
  181. }
  182. }