Painter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #define GL_GLEXT_PROTOTYPES
  8. #include "Painter.h"
  9. #include "Canvas.h"
  10. #include <AK/QuickSort.h>
  11. #include <GL/gl.h>
  12. #include <GL/glext.h>
  13. #include <LibGfx/Color.h>
  14. #include <LibGfx/Painter.h>
  15. namespace AccelGfx {
  16. struct ColorComponents {
  17. float red;
  18. float green;
  19. float blue;
  20. float alpha;
  21. };
  22. static ColorComponents gfx_color_to_opengl_color(Gfx::Color color)
  23. {
  24. ColorComponents components;
  25. components.red = static_cast<float>(color.red()) / 255.0f;
  26. components.green = static_cast<float>(color.green()) / 255.0f;
  27. components.blue = static_cast<float>(color.blue()) / 255.0f;
  28. components.alpha = static_cast<float>(color.alpha()) / 255.0f;
  29. return components;
  30. }
  31. Gfx::FloatRect Painter::to_clip_space(Gfx::FloatRect const& screen_rect) const
  32. {
  33. float x = 2.0f * screen_rect.x() / m_canvas->width() - 1.0f;
  34. float y = -1.0f + 2.0f * screen_rect.y() / m_canvas->height();
  35. float width = 2.0f * screen_rect.width() / m_canvas->width();
  36. float height = 2.0f * screen_rect.height() / m_canvas->height();
  37. return { x, y, width, height };
  38. }
  39. char const* vertex_shader_source = R"(
  40. attribute vec2 aVertexPosition;
  41. void main() {
  42. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  43. }
  44. )";
  45. char const* solid_color_fragment_shader_source = R"(
  46. precision mediump float;
  47. uniform vec4 uColor;
  48. void main() {
  49. gl_FragColor = uColor;
  50. }
  51. )";
  52. char const* blit_vertex_shader_source = R"(
  53. attribute vec4 aVertexPosition;
  54. varying vec2 vTextureCoord;
  55. void main() {
  56. gl_Position = vec4(aVertexPosition.xy, 0.0, 1.0);
  57. vTextureCoord = aVertexPosition.zw;
  58. }
  59. )";
  60. char const* blit_fragment_shader_source = R"(
  61. precision mediump float;
  62. uniform vec4 uColor;
  63. varying vec2 vTextureCoord;
  64. uniform sampler2D uSampler;
  65. void main() {
  66. gl_FragColor = texture2D(uSampler, vTextureCoord) * uColor;
  67. }
  68. )";
  69. OwnPtr<Painter> Painter::create()
  70. {
  71. auto& context = Context::the();
  72. return make<Painter>(context);
  73. }
  74. Painter::Painter(Context& context)
  75. : m_context(context)
  76. , m_rectangle_program(Program::create(vertex_shader_source, solid_color_fragment_shader_source))
  77. , m_blit_program(Program::create(blit_vertex_shader_source, blit_fragment_shader_source))
  78. {
  79. m_state_stack.empend(State());
  80. glGenTextures(1, &m_glyphs_texture);
  81. }
  82. Painter::~Painter()
  83. {
  84. flush();
  85. }
  86. void Painter::clear(Gfx::Color color)
  87. {
  88. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  89. glClearColor(red, green, blue, alpha);
  90. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  91. }
  92. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  93. {
  94. fill_rect(rect.to_type<float>(), color);
  95. }
  96. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  97. {
  98. return {
  99. rect.left(),
  100. rect.top(),
  101. rect.left(),
  102. rect.bottom(),
  103. rect.right(),
  104. rect.bottom(),
  105. rect.right(),
  106. rect.top(),
  107. };
  108. }
  109. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  110. {
  111. // Draw a filled rect (with `color`) using OpenGL after mapping it through the current transform.
  112. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  113. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  114. m_rectangle_program.use();
  115. GLuint position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  116. GLuint color_uniform = m_rectangle_program.get_uniform_location("uColor");
  117. glUniform4f(color_uniform, red, green, blue, alpha);
  118. glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), vertices.data());
  119. glEnableVertexAttribArray(position_attribute);
  120. glEnable(GL_BLEND);
  121. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  122. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  123. }
  124. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  125. {
  126. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  127. }
  128. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  129. {
  130. auto x = rect.x() / image_size.width();
  131. auto y = rect.y() / image_size.height();
  132. auto width = rect.width() / image_size.width();
  133. auto height = rect.height() / image_size.height();
  134. return { x, y, width, height };
  135. }
  136. static GLenum to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  137. {
  138. switch (scaling_mode) {
  139. case Painter::ScalingMode::NearestNeighbor:
  140. return GL_NEAREST;
  141. case Painter::ScalingMode::Bilinear:
  142. return GL_LINEAR;
  143. default:
  144. VERIFY_NOT_REACHED();
  145. }
  146. }
  147. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  148. {
  149. m_blit_program.use();
  150. GLuint texture;
  151. // FIXME: We should reuse textures across repaints if possible.
  152. glGenTextures(1, &texture);
  153. glBindTexture(GL_TEXTURE_2D, texture);
  154. glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, bitmap.width(), bitmap.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, bitmap.scanline(0));
  155. GLenum scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  156. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  157. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  158. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, scaling_mode_gl);
  159. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, scaling_mode_gl);
  160. auto dst_rect_in_clip_space = to_clip_space(transform().map(dst_rect));
  161. auto src_rect_in_texture_space = to_texture_space(src_rect, bitmap.size());
  162. Vector<GLfloat> vertices;
  163. vertices.ensure_capacity(16);
  164. auto add_vertex = [&](auto const& p, auto const& s) {
  165. vertices.append(p.x());
  166. vertices.append(p.y());
  167. vertices.append(s.x());
  168. vertices.append(s.y());
  169. };
  170. add_vertex(dst_rect_in_clip_space.top_left(), src_rect_in_texture_space.top_left());
  171. add_vertex(dst_rect_in_clip_space.bottom_left(), src_rect_in_texture_space.bottom_left());
  172. add_vertex(dst_rect_in_clip_space.bottom_right(), src_rect_in_texture_space.bottom_right());
  173. add_vertex(dst_rect_in_clip_space.top_right(), src_rect_in_texture_space.top_right());
  174. GLuint vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  175. glVertexAttribPointer(vertex_position_attribute, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices.data());
  176. glEnableVertexAttribArray(vertex_position_attribute);
  177. GLuint color_uniform = m_blit_program.get_uniform_location("uColor");
  178. glUniform4f(color_uniform, 1, 1, 1, 1);
  179. glEnable(GL_BLEND);
  180. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  181. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  182. glDeleteTextures(1, &texture);
  183. }
  184. void Painter::prepare_glyph_texture(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_glyphs)
  185. {
  186. HashMap<GlyphsTextureKey, NonnullRefPtr<Gfx::Bitmap>> glyph_bitmaps;
  187. for (auto const& [font, code_points] : unique_glyphs) {
  188. for (auto const& code_point : code_points) {
  189. auto glyph = font->glyph(code_point);
  190. auto atlas_key = GlyphsTextureKey { font, code_point };
  191. glyph_bitmaps.set(atlas_key, *glyph.bitmap());
  192. }
  193. }
  194. if (glyph_bitmaps.is_empty())
  195. return;
  196. Vector<GlyphsTextureKey> glyphs_sorted_by_height;
  197. glyphs_sorted_by_height.ensure_capacity(glyph_bitmaps.size());
  198. for (auto const& [atlas_key, bitmap] : glyph_bitmaps) {
  199. glyphs_sorted_by_height.append(atlas_key);
  200. }
  201. quick_sort(glyphs_sorted_by_height, [&](auto const& a, auto const& b) {
  202. auto const& bitmap_a = *glyph_bitmaps.get(a);
  203. auto const& bitmap_b = *glyph_bitmaps.get(b);
  204. return bitmap_a->height() > bitmap_b->height();
  205. });
  206. int current_x = 0;
  207. int current_y = 0;
  208. int row_height = 0;
  209. int texture_width = 512;
  210. for (auto const& glyphs_texture_key : glyphs_sorted_by_height) {
  211. auto const& bitmap = *glyph_bitmaps.get(glyphs_texture_key);
  212. if (current_x + bitmap->width() > texture_width) {
  213. current_x = 0;
  214. current_y += row_height;
  215. row_height = 0;
  216. }
  217. m_glyphs_texture_map.set(glyphs_texture_key, { current_x, current_y, bitmap->width(), bitmap->height() });
  218. current_x += bitmap->width();
  219. row_height = max(row_height, bitmap->height());
  220. }
  221. auto glyphs_texture_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { texture_width, current_y + row_height }));
  222. auto glyphs_texure_painter = Gfx::Painter(*glyphs_texture_bitmap);
  223. for (auto const& [glyphs_texture_key, glyph_bitmap] : glyph_bitmaps) {
  224. auto rect = m_glyphs_texture_map.get(glyphs_texture_key).value();
  225. glyphs_texure_painter.blit({ rect.x(), rect.y() }, glyph_bitmap, glyph_bitmap->rect());
  226. }
  227. m_glyphs_texture_size = glyphs_texture_bitmap->size();
  228. glBindTexture(GL_TEXTURE_2D, m_glyphs_texture);
  229. glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, glyphs_texture_bitmap->width(), glyphs_texture_bitmap->height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, glyphs_texture_bitmap->scanline(0));
  230. }
  231. void Painter::draw_glyph_run(Vector<Gfx::DrawGlyphOrEmoji> const& glyph_run, Color const& color)
  232. {
  233. Vector<GLfloat> vertices;
  234. for (auto& glyph_or_emoji : glyph_run) {
  235. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  236. auto& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  237. auto const* font = glyph.font;
  238. auto code_point = glyph.code_point;
  239. auto point = glyph.position;
  240. auto maybe_texture_rect = m_glyphs_texture_map.get(GlyphsTextureKey { font, code_point });
  241. VERIFY(maybe_texture_rect.has_value());
  242. auto texture_rect = to_texture_space(maybe_texture_rect.value().to_type<float>(), m_glyphs_texture_size);
  243. auto glyph_position = point + Gfx::FloatPoint(font->glyph_left_bearing(code_point), 0);
  244. auto glyph_size = maybe_texture_rect->size().to_type<float>();
  245. auto rect_in_clip_space = to_clip_space({ glyph_position, glyph_size });
  246. // p0 --- p1
  247. // | \ |
  248. // | \ |
  249. // | \ |
  250. // p2 --- p3
  251. auto p0 = rect_in_clip_space.top_left();
  252. auto p1 = rect_in_clip_space.top_right();
  253. auto p2 = rect_in_clip_space.bottom_left();
  254. auto p3 = rect_in_clip_space.bottom_right();
  255. auto s0 = texture_rect.top_left();
  256. auto s1 = texture_rect.top_right();
  257. auto s2 = texture_rect.bottom_left();
  258. auto s3 = texture_rect.bottom_right();
  259. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& s1, auto& s2, auto& s3) {
  260. vertices.append(p1.x());
  261. vertices.append(p1.y());
  262. vertices.append(s1.x());
  263. vertices.append(s1.y());
  264. vertices.append(p2.x());
  265. vertices.append(p2.y());
  266. vertices.append(s2.x());
  267. vertices.append(s2.y());
  268. vertices.append(p3.x());
  269. vertices.append(p3.y());
  270. vertices.append(s3.x());
  271. vertices.append(s3.y());
  272. };
  273. add_triangle(p0, p1, p3, s0, s1, s3);
  274. add_triangle(p0, p3, p2, s0, s3, s2);
  275. }
  276. }
  277. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  278. m_blit_program.use();
  279. glBindTexture(GL_TEXTURE_2D, m_glyphs_texture);
  280. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  281. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  282. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  283. GLuint position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  284. GLuint color_uniform = m_blit_program.get_uniform_location("uColor");
  285. glUniform4f(color_uniform, red, green, blue, alpha);
  286. glVertexAttribPointer(position_attribute, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices.data());
  287. glEnableVertexAttribArray(position_attribute);
  288. glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 4);
  289. }
  290. void Painter::save()
  291. {
  292. m_state_stack.append(state());
  293. }
  294. void Painter::restore()
  295. {
  296. VERIFY(!m_state_stack.is_empty());
  297. m_state_stack.take_last();
  298. }
  299. void Painter::flush()
  300. {
  301. m_canvas->flush();
  302. }
  303. }