Painter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. #include <AK/QuickSort.h>
  8. #include <LibAccelGfx/GL.h>
  9. #include <LibAccelGfx/Painter.h>
  10. #include <LibGfx/Color.h>
  11. #include <LibGfx/Painter.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_target_bitmap->width() - 1.0f;
  31. float y = -1.0f + 2.0f * screen_rect.y() / m_target_bitmap->height();
  32. float width = 2.0f * screen_rect.width() / m_target_bitmap->width();
  33. float height = 2.0f * screen_rect.height() / m_target_bitmap->height();
  34. return { x, y, width, height };
  35. }
  36. char const* vertex_shader_source = R"(
  37. #version 330 core
  38. in vec2 aVertexPosition;
  39. void main() {
  40. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  41. }
  42. )";
  43. char const* solid_color_fragment_shader_source = R"(
  44. #version 330 core
  45. uniform vec4 uColor;
  46. out vec4 fragColor;
  47. void main() {
  48. fragColor = uColor;
  49. }
  50. )";
  51. char const* blit_vertex_shader_source = R"(
  52. #version 330 core
  53. in vec4 aVertexPosition;
  54. out 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. #version 330 core
  62. uniform vec4 uColor;
  63. in vec2 vTextureCoord;
  64. uniform sampler2D uSampler;
  65. out vec4 fragColor;
  66. void main() {
  67. fragColor = texture(uSampler, vTextureCoord) * uColor;
  68. }
  69. )";
  70. OwnPtr<Painter> Painter::create()
  71. {
  72. auto& context = Context::the();
  73. return make<Painter>(context);
  74. }
  75. Painter::Painter(Context& context)
  76. : m_context(context)
  77. , m_rectangle_program(Program::create(vertex_shader_source, solid_color_fragment_shader_source))
  78. , m_blit_program(Program::create(blit_vertex_shader_source, blit_fragment_shader_source))
  79. , m_glyphs_texture(GL::create_texture())
  80. {
  81. m_state_stack.empend(State());
  82. }
  83. Painter::~Painter()
  84. {
  85. flush();
  86. }
  87. void Painter::clear(Gfx::Color color)
  88. {
  89. GL::clear_color(color);
  90. }
  91. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  92. {
  93. fill_rect(rect.to_type<float>(), color);
  94. }
  95. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  96. {
  97. return {
  98. rect.left(),
  99. rect.top(),
  100. rect.left(),
  101. rect.bottom(),
  102. rect.right(),
  103. rect.bottom(),
  104. rect.right(),
  105. rect.top(),
  106. };
  107. }
  108. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  109. {
  110. // Draw a filled rect (with `color`) using OpenGL after mapping it through the current transform.
  111. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  112. auto vbo = GL::create_buffer();
  113. GL::upload_to_buffer(vbo, vertices);
  114. auto vao = GL::create_vertex_array();
  115. GL::bind_vertex_array(vao);
  116. GL::bind_buffer(vbo);
  117. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  118. m_rectangle_program.use();
  119. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  120. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  121. GL::set_uniform(color_uniform, red, green, blue, alpha);
  122. GL::set_vertex_attribute(position_attribute, 0, 2);
  123. GL::enable_blending();
  124. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  125. GL::delete_buffer(vbo);
  126. GL::delete_vertex_array(vao);
  127. }
  128. void Painter::draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color)
  129. {
  130. draw_line(a.to_type<float>(), b.to_type<float>(), thickness, color);
  131. }
  132. void Painter::draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Color color)
  133. {
  134. auto midpoint = (a + b) / 2.0f;
  135. auto length = a.distance_from(b);
  136. auto angle = AK::atan2(b.y() - a.y(), b.x() - a.x());
  137. auto offset = Gfx::FloatPoint {
  138. (length / 2) * AK::cos(angle) - (thickness / 2) * AK::sin(angle),
  139. (length / 2) * AK::sin(angle) + (thickness / 2) * AK::cos(angle),
  140. };
  141. auto rect = Gfx::FloatRect(midpoint - offset, { length, thickness });
  142. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  143. auto vbo = GL::create_buffer();
  144. GL::upload_to_buffer(vbo, vertices);
  145. auto vao = GL::create_vertex_array();
  146. GL::bind_vertex_array(vao);
  147. GL::bind_buffer(vbo);
  148. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  149. m_rectangle_program.use();
  150. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  151. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  152. GL::set_uniform(color_uniform, red, green, blue, alpha);
  153. GL::set_vertex_attribute(position_attribute, 0, 2);
  154. GL::enable_blending();
  155. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  156. GL::delete_buffer(vbo);
  157. GL::delete_vertex_array(vao);
  158. }
  159. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  160. {
  161. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  162. }
  163. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  164. {
  165. auto x = rect.x() / image_size.width();
  166. auto y = rect.y() / image_size.height();
  167. auto width = rect.width() / image_size.width();
  168. auto height = rect.height() / image_size.height();
  169. return { x, y, width, height };
  170. }
  171. static GL::ScalingMode to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  172. {
  173. switch (scaling_mode) {
  174. case Painter::ScalingMode::NearestNeighbor:
  175. return GL::ScalingMode::Nearest;
  176. case Painter::ScalingMode::Bilinear:
  177. return GL::ScalingMode::Linear;
  178. default:
  179. VERIFY_NOT_REACHED();
  180. }
  181. }
  182. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  183. {
  184. m_blit_program.use();
  185. // FIXME: We should reuse textures across repaints if possible.
  186. auto texture = GL::create_texture();
  187. GL::upload_texture_data(texture, bitmap);
  188. auto scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  189. GL::set_texture_scale_mode(scaling_mode_gl);
  190. auto dst_rect_in_clip_space = to_clip_space(transform().map(dst_rect));
  191. auto src_rect_in_texture_space = to_texture_space(src_rect, bitmap.size());
  192. Vector<GLfloat> vertices;
  193. vertices.ensure_capacity(16);
  194. auto add_vertex = [&](auto const& p, auto const& s) {
  195. vertices.append(p.x());
  196. vertices.append(p.y());
  197. vertices.append(s.x());
  198. vertices.append(s.y());
  199. };
  200. add_vertex(dst_rect_in_clip_space.top_left(), src_rect_in_texture_space.top_left());
  201. add_vertex(dst_rect_in_clip_space.bottom_left(), src_rect_in_texture_space.bottom_left());
  202. add_vertex(dst_rect_in_clip_space.bottom_right(), src_rect_in_texture_space.bottom_right());
  203. add_vertex(dst_rect_in_clip_space.top_right(), src_rect_in_texture_space.top_right());
  204. auto vbo = GL::create_buffer();
  205. GL::upload_to_buffer(vbo, vertices);
  206. auto vao = GL::create_vertex_array();
  207. GL::bind_vertex_array(vao);
  208. GL::bind_buffer(vbo);
  209. auto vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  210. GL::set_vertex_attribute(vertex_position_attribute, 0, 4);
  211. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  212. GL::set_uniform(color_uniform, 1, 1, 1, 1);
  213. GL::enable_blending();
  214. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  215. GL::delete_texture(texture);
  216. GL::delete_buffer(vbo);
  217. GL::delete_vertex_array(vao);
  218. }
  219. void Painter::prepare_glyph_texture(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_glyphs)
  220. {
  221. HashMap<GlyphsTextureKey, NonnullRefPtr<Gfx::Bitmap>> glyph_bitmaps;
  222. for (auto const& [font, code_points] : unique_glyphs) {
  223. for (auto const& code_point : code_points) {
  224. auto glyph = font->glyph(code_point);
  225. auto atlas_key = GlyphsTextureKey { font, code_point };
  226. glyph_bitmaps.set(atlas_key, *glyph.bitmap());
  227. }
  228. }
  229. if (glyph_bitmaps.is_empty())
  230. return;
  231. Vector<GlyphsTextureKey> glyphs_sorted_by_height;
  232. glyphs_sorted_by_height.ensure_capacity(glyph_bitmaps.size());
  233. for (auto const& [atlas_key, bitmap] : glyph_bitmaps) {
  234. glyphs_sorted_by_height.append(atlas_key);
  235. }
  236. quick_sort(glyphs_sorted_by_height, [&](auto const& a, auto const& b) {
  237. auto const& bitmap_a = *glyph_bitmaps.get(a);
  238. auto const& bitmap_b = *glyph_bitmaps.get(b);
  239. return bitmap_a->height() > bitmap_b->height();
  240. });
  241. int current_x = 0;
  242. int current_y = 0;
  243. int row_height = 0;
  244. int texture_width = 512;
  245. int padding = 1;
  246. for (auto const& glyphs_texture_key : glyphs_sorted_by_height) {
  247. auto const& bitmap = *glyph_bitmaps.get(glyphs_texture_key);
  248. if (current_x + bitmap->width() > texture_width) {
  249. current_x = 0;
  250. current_y += row_height + padding;
  251. row_height = 0;
  252. }
  253. m_glyphs_texture_map.set(glyphs_texture_key, { current_x, current_y, bitmap->width(), bitmap->height() });
  254. current_x += bitmap->width() + padding;
  255. row_height = max(row_height, bitmap->height());
  256. }
  257. auto glyphs_texture_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { texture_width, current_y + row_height }));
  258. auto glyphs_texure_painter = Gfx::Painter(*glyphs_texture_bitmap);
  259. for (auto const& [glyphs_texture_key, glyph_bitmap] : glyph_bitmaps) {
  260. auto rect = m_glyphs_texture_map.get(glyphs_texture_key).value();
  261. glyphs_texure_painter.blit({ rect.x(), rect.y() }, glyph_bitmap, glyph_bitmap->rect());
  262. }
  263. m_glyphs_texture_size = glyphs_texture_bitmap->size();
  264. GL::upload_texture_data(m_glyphs_texture, *glyphs_texture_bitmap);
  265. }
  266. void Painter::draw_glyph_run(Vector<Gfx::DrawGlyphOrEmoji> const& glyph_run, Color const& color)
  267. {
  268. Vector<GLfloat> vertices;
  269. for (auto& glyph_or_emoji : glyph_run) {
  270. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  271. auto& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  272. auto const* font = glyph.font;
  273. auto code_point = glyph.code_point;
  274. auto point = glyph.position;
  275. auto maybe_texture_rect = m_glyphs_texture_map.get(GlyphsTextureKey { font, code_point });
  276. VERIFY(maybe_texture_rect.has_value());
  277. auto texture_rect = to_texture_space(maybe_texture_rect.value().to_type<float>(), m_glyphs_texture_size);
  278. auto glyph_position = point + Gfx::FloatPoint(font->glyph_left_bearing(code_point), 0);
  279. auto glyph_size = maybe_texture_rect->size().to_type<float>();
  280. auto rect_in_clip_space = to_clip_space({ glyph_position, glyph_size });
  281. // p0 --- p1
  282. // | \ |
  283. // | \ |
  284. // | \ |
  285. // p2 --- p3
  286. auto p0 = rect_in_clip_space.top_left();
  287. auto p1 = rect_in_clip_space.top_right();
  288. auto p2 = rect_in_clip_space.bottom_left();
  289. auto p3 = rect_in_clip_space.bottom_right();
  290. auto s0 = texture_rect.top_left();
  291. auto s1 = texture_rect.top_right();
  292. auto s2 = texture_rect.bottom_left();
  293. auto s3 = texture_rect.bottom_right();
  294. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& s1, auto& s2, auto& s3) {
  295. vertices.append(p1.x());
  296. vertices.append(p1.y());
  297. vertices.append(s1.x());
  298. vertices.append(s1.y());
  299. vertices.append(p2.x());
  300. vertices.append(p2.y());
  301. vertices.append(s2.x());
  302. vertices.append(s2.y());
  303. vertices.append(p3.x());
  304. vertices.append(p3.y());
  305. vertices.append(s3.x());
  306. vertices.append(s3.y());
  307. };
  308. add_triangle(p0, p1, p3, s0, s1, s3);
  309. add_triangle(p0, p3, p2, s0, s3, s2);
  310. }
  311. }
  312. auto vbo = GL::create_buffer();
  313. GL::upload_to_buffer(vbo, vertices);
  314. auto vao = GL::create_vertex_array();
  315. GL::bind_vertex_array(vao);
  316. GL::bind_buffer(vbo);
  317. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  318. m_blit_program.use();
  319. GL::bind_texture(m_glyphs_texture);
  320. GL::set_texture_scale_mode(GL::ScalingMode::Nearest);
  321. auto position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  322. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  323. GL::set_uniform(color_uniform, red, green, blue, alpha);
  324. GL::set_vertex_attribute(position_attribute, 0, 4);
  325. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 4);
  326. GL::delete_buffer(vbo);
  327. GL::delete_vertex_array(vao);
  328. }
  329. void Painter::save()
  330. {
  331. m_state_stack.append(state());
  332. }
  333. void Painter::restore()
  334. {
  335. VERIFY(!m_state_stack.is_empty());
  336. m_state_stack.take_last();
  337. }
  338. void Painter::set_clip_rect(Gfx::IntRect rect)
  339. {
  340. GL::enable_scissor_test(rect);
  341. }
  342. void Painter::clear_clip_rect()
  343. {
  344. GL::disable_scissor_test();
  345. }
  346. void Painter::set_target_bitmap(Gfx::Bitmap& bitmap)
  347. {
  348. if (m_target_framebuffer.has_value()) {
  349. GL::delete_framebuffer(*m_target_framebuffer);
  350. m_target_framebuffer = {};
  351. }
  352. m_target_framebuffer = GL::create_framebuffer(bitmap.size());
  353. GL::bind_framebuffer(*m_target_framebuffer);
  354. GL::set_viewport({ 0, 0, bitmap.width(), bitmap.height() });
  355. m_target_bitmap = bitmap;
  356. }
  357. void Painter::flush()
  358. {
  359. VERIFY(m_target_bitmap.has_value());
  360. GL::bind_framebuffer(*m_target_framebuffer);
  361. GL::read_pixels({ 0, 0, m_target_bitmap->width(), m_target_bitmap->height() }, *m_target_bitmap);
  362. }
  363. }