Painter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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_canvas->size().width() - 1.0f;
  31. float y = -1.0f + 2.0f * screen_rect.y() / m_target_canvas->size().height();
  32. float width = 2.0f * screen_rect.width() / m_target_canvas->size().width();
  33. float height = 2.0f * screen_rect.height() / m_target_canvas->size().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* rect_with_rounded_corners_fragment_shader_source = R"(
  44. #version 330 core
  45. uniform vec2 uRectCenter;
  46. uniform vec2 uRectCorner;
  47. uniform vec2 uTopLeftRadius;
  48. uniform vec2 uTopRightRadius;
  49. uniform vec2 uBottomLeftRadius;
  50. uniform vec2 uBottomRightRadius;
  51. uniform vec4 uColor;
  52. out vec4 fragColor;
  53. bool isPointWithinEllipse(vec2 point, vec2 radius) {
  54. vec2 normalizedPoint = point / radius;
  55. return dot(normalizedPoint, normalizedPoint) <= 1.0;
  56. }
  57. void main() {
  58. vec2 p = gl_FragCoord.xy - uRectCenter;
  59. vec2 cornerRadius = vec2(0.0, 0.0);
  60. if (p.x < 0.0 && p.y < 0.0) {
  61. cornerRadius = uTopLeftRadius;
  62. } else if (p.x > 0.0 && p.y < 0.0) {
  63. cornerRadius = uTopRightRadius;
  64. } else if (p.x < 0.0 && p.y > 0.0) {
  65. cornerRadius = uBottomLeftRadius;
  66. } else if (p.x > 0.0 && p.y > 0.0) {
  67. cornerRadius = uBottomRightRadius;
  68. }
  69. vec2 q = abs(p) - (uRectCorner - cornerRadius);
  70. if (q.x < 0 || q.y < 0 || isPointWithinEllipse(q, cornerRadius)) {
  71. fragColor = uColor;
  72. } else {
  73. discard;
  74. }
  75. }
  76. )";
  77. char const* solid_color_fragment_shader_source = R"(
  78. #version 330 core
  79. uniform vec4 uColor;
  80. out vec4 fragColor;
  81. void main() {
  82. fragColor = uColor;
  83. }
  84. )";
  85. char const* blit_vertex_shader_source = R"(
  86. #version 330 core
  87. in vec4 aVertexPosition;
  88. out vec2 vTextureCoord;
  89. void main() {
  90. gl_Position = vec4(aVertexPosition.xy, 0.0, 1.0);
  91. vTextureCoord = aVertexPosition.zw;
  92. }
  93. )";
  94. char const* blit_fragment_shader_source = R"(
  95. #version 330 core
  96. uniform vec4 uColor;
  97. in vec2 vTextureCoord;
  98. uniform sampler2D uSampler;
  99. out vec4 fragColor;
  100. void main() {
  101. fragColor = texture(uSampler, vTextureCoord) * uColor;
  102. }
  103. )";
  104. char const* linear_gradient_vertex_shader_source = R"(
  105. #version 330 core
  106. layout (location = 0) in vec2 aVertexPosition;
  107. layout (location = 1) in vec4 aColor;
  108. out vec4 vColor;
  109. void main() {
  110. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  111. vColor = aColor;
  112. }
  113. )";
  114. char const* linear_gradient_fragment_shader_source = R"(
  115. #version 330 core
  116. out vec4 FragColor;
  117. in vec4 vColor;
  118. void main() {
  119. FragColor = vec4(vColor);
  120. }
  121. )";
  122. OwnPtr<Painter> Painter::create()
  123. {
  124. auto& context = Context::the();
  125. return make<Painter>(context);
  126. }
  127. Painter::Painter(Context& context)
  128. : m_context(context)
  129. , m_rectangle_program(Program::create(Program::Name::RectangleProgram, vertex_shader_source, solid_color_fragment_shader_source))
  130. , m_rounded_rectangle_program(Program::create(Program::Name::RoundedRectangleProgram, vertex_shader_source, rect_with_rounded_corners_fragment_shader_source))
  131. , m_blit_program(Program::create(Program::Name::BlitProgram, blit_vertex_shader_source, blit_fragment_shader_source))
  132. , m_linear_gradient_program(Program::create(Program::Name::LinearGradientProgram, linear_gradient_vertex_shader_source, linear_gradient_fragment_shader_source))
  133. , m_glyphs_texture(GL::create_texture())
  134. {
  135. m_state_stack.empend(State());
  136. }
  137. Painter::~Painter()
  138. {
  139. }
  140. void Painter::clear(Gfx::Color color)
  141. {
  142. GL::clear_color(color);
  143. }
  144. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  145. {
  146. fill_rect(rect.to_type<float>(), color);
  147. }
  148. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  149. {
  150. return {
  151. rect.left(),
  152. rect.top(),
  153. rect.left(),
  154. rect.bottom(),
  155. rect.right(),
  156. rect.bottom(),
  157. rect.right(),
  158. rect.top(),
  159. };
  160. }
  161. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  162. {
  163. bind_target_canvas();
  164. // Draw a filled rect (with `color`) using OpenGL after mapping it through the current transform.
  165. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  166. auto vbo = GL::create_buffer();
  167. GL::upload_to_buffer(vbo, vertices);
  168. auto vao = GL::create_vertex_array();
  169. GL::bind_vertex_array(vao);
  170. GL::bind_buffer(vbo);
  171. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  172. m_rectangle_program.use();
  173. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  174. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  175. GL::set_uniform(color_uniform, red, green, blue, alpha);
  176. GL::set_vertex_attribute(position_attribute, 0, 2);
  177. GL::enable_blending();
  178. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  179. GL::delete_buffer(vbo);
  180. GL::delete_vertex_array(vao);
  181. }
  182. void Painter::fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color const& color, CornerRadius const& top_left_radius, CornerRadius const& top_right_radius, CornerRadius const& bottom_left_radius, CornerRadius const& bottom_right_radius)
  183. {
  184. fill_rect_with_rounded_corners(rect.to_type<float>(), color, top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius);
  185. }
  186. void Painter::fill_rect_with_rounded_corners(Gfx::FloatRect const& rect, Color const& color, CornerRadius const& top_left_radius, CornerRadius const& top_right_radius, CornerRadius const& bottom_left_radius, CornerRadius const& bottom_right_radius)
  187. {
  188. bind_target_canvas();
  189. auto transformed_rect = transform().map(rect);
  190. auto vertices = rect_to_vertices(to_clip_space(transformed_rect));
  191. auto vbo = GL::create_buffer();
  192. GL::upload_to_buffer(vbo, vertices);
  193. auto vao = GL::create_vertex_array();
  194. GL::bind_vertex_array(vao);
  195. GL::bind_buffer(vbo);
  196. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  197. m_rounded_rectangle_program.use();
  198. auto position_attribute = m_rounded_rectangle_program.get_attribute_location("aVertexPosition");
  199. GL::set_vertex_attribute(position_attribute, 0, 2);
  200. auto color_uniform = m_rounded_rectangle_program.get_uniform_location("uColor");
  201. GL::set_uniform(color_uniform, red, green, blue, alpha);
  202. auto rect_center_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCenter");
  203. GL::set_uniform(rect_center_uniform, transformed_rect.center().x(), transformed_rect.center().y());
  204. auto rect_corner_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCorner");
  205. GL::set_uniform(rect_corner_uniform, rect.width() / 2, rect.height() / 2);
  206. auto top_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopLeftRadius");
  207. GL::set_uniform(top_left_corner_radius_uniform, top_left_radius.horizontal_radius, top_left_radius.vertical_radius);
  208. auto top_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopRightRadius");
  209. GL::set_uniform(top_right_corner_radius_uniform, top_right_radius.horizontal_radius, top_right_radius.vertical_radius);
  210. auto bottom_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomLeftRadius");
  211. GL::set_uniform(bottom_left_corner_radius_uniform, bottom_left_radius.horizontal_radius, bottom_left_radius.vertical_radius);
  212. auto bottom_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomRightRadius");
  213. GL::set_uniform(bottom_right_corner_radius_uniform, bottom_right_radius.horizontal_radius, bottom_right_radius.vertical_radius);
  214. GL::enable_blending();
  215. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  216. GL::delete_buffer(vbo);
  217. GL::delete_vertex_array(vao);
  218. }
  219. void Painter::draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color)
  220. {
  221. draw_line(a.to_type<float>(), b.to_type<float>(), thickness, color);
  222. }
  223. void Painter::draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Color color)
  224. {
  225. bind_target_canvas();
  226. auto midpoint = (a + b) / 2.0f;
  227. auto length = a.distance_from(b);
  228. auto angle = AK::atan2(b.y() - a.y(), b.x() - a.x());
  229. auto offset = Gfx::FloatPoint {
  230. (length / 2) * AK::cos(angle) - (thickness / 2) * AK::sin(angle),
  231. (length / 2) * AK::sin(angle) + (thickness / 2) * AK::cos(angle),
  232. };
  233. auto rect = Gfx::FloatRect(midpoint - offset, { length, thickness });
  234. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  235. auto vbo = GL::create_buffer();
  236. GL::upload_to_buffer(vbo, vertices);
  237. auto vao = GL::create_vertex_array();
  238. GL::bind_vertex_array(vao);
  239. GL::bind_buffer(vbo);
  240. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  241. m_rectangle_program.use();
  242. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  243. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  244. GL::set_uniform(color_uniform, red, green, blue, alpha);
  245. GL::set_vertex_attribute(position_attribute, 0, 2);
  246. GL::enable_blending();
  247. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  248. GL::delete_buffer(vbo);
  249. GL::delete_vertex_array(vao);
  250. }
  251. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  252. {
  253. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  254. }
  255. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  256. {
  257. auto x = rect.x() / image_size.width();
  258. auto y = rect.y() / image_size.height();
  259. auto width = rect.width() / image_size.width();
  260. auto height = rect.height() / image_size.height();
  261. return { x, y, width, height };
  262. }
  263. static GL::ScalingMode to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  264. {
  265. switch (scaling_mode) {
  266. case Painter::ScalingMode::NearestNeighbor:
  267. return GL::ScalingMode::Nearest;
  268. case Painter::ScalingMode::Bilinear:
  269. return GL::ScalingMode::Linear;
  270. default:
  271. VERIFY_NOT_REACHED();
  272. }
  273. }
  274. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  275. {
  276. bind_target_canvas();
  277. m_blit_program.use();
  278. // FIXME: We should reuse textures across repaints if possible.
  279. auto texture = GL::create_texture();
  280. GL::upload_texture_data(texture, bitmap);
  281. auto scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  282. GL::set_texture_scale_mode(scaling_mode_gl);
  283. auto dst_rect_in_clip_space = to_clip_space(transform().map(dst_rect));
  284. auto src_rect_in_texture_space = to_texture_space(src_rect, bitmap.size());
  285. Vector<GLfloat> vertices;
  286. vertices.ensure_capacity(16);
  287. auto add_vertex = [&](auto const& p, auto const& s) {
  288. vertices.append(p.x());
  289. vertices.append(p.y());
  290. vertices.append(s.x());
  291. vertices.append(s.y());
  292. };
  293. add_vertex(dst_rect_in_clip_space.top_left(), src_rect_in_texture_space.top_left());
  294. add_vertex(dst_rect_in_clip_space.bottom_left(), src_rect_in_texture_space.bottom_left());
  295. add_vertex(dst_rect_in_clip_space.bottom_right(), src_rect_in_texture_space.bottom_right());
  296. add_vertex(dst_rect_in_clip_space.top_right(), src_rect_in_texture_space.top_right());
  297. auto vbo = GL::create_buffer();
  298. GL::upload_to_buffer(vbo, vertices);
  299. auto vao = GL::create_vertex_array();
  300. GL::bind_vertex_array(vao);
  301. GL::bind_buffer(vbo);
  302. auto vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  303. GL::set_vertex_attribute(vertex_position_attribute, 0, 4);
  304. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  305. GL::set_uniform(color_uniform, 1, 1, 1, 1);
  306. GL::enable_blending();
  307. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  308. GL::delete_texture(texture);
  309. GL::delete_buffer(vbo);
  310. GL::delete_vertex_array(vao);
  311. }
  312. void Painter::prepare_glyph_texture(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_glyphs)
  313. {
  314. HashMap<GlyphsTextureKey, NonnullRefPtr<Gfx::Bitmap>> glyph_bitmaps;
  315. for (auto const& [font, code_points] : unique_glyphs) {
  316. for (auto const& code_point : code_points) {
  317. auto glyph = font->glyph(code_point);
  318. if (glyph.bitmap()) {
  319. auto atlas_key = GlyphsTextureKey { font, code_point };
  320. glyph_bitmaps.set(atlas_key, *glyph.bitmap());
  321. }
  322. }
  323. }
  324. if (glyph_bitmaps.is_empty())
  325. return;
  326. Vector<GlyphsTextureKey> glyphs_sorted_by_height;
  327. glyphs_sorted_by_height.ensure_capacity(glyph_bitmaps.size());
  328. for (auto const& [atlas_key, bitmap] : glyph_bitmaps) {
  329. glyphs_sorted_by_height.append(atlas_key);
  330. }
  331. quick_sort(glyphs_sorted_by_height, [&](auto const& a, auto const& b) {
  332. auto const& bitmap_a = *glyph_bitmaps.get(a);
  333. auto const& bitmap_b = *glyph_bitmaps.get(b);
  334. return bitmap_a->height() > bitmap_b->height();
  335. });
  336. int current_x = 0;
  337. int current_y = 0;
  338. int row_height = 0;
  339. int texture_width = 512;
  340. int padding = 1;
  341. for (auto const& glyphs_texture_key : glyphs_sorted_by_height) {
  342. auto const& bitmap = *glyph_bitmaps.get(glyphs_texture_key);
  343. if (current_x + bitmap->width() > texture_width) {
  344. current_x = 0;
  345. current_y += row_height + padding;
  346. row_height = 0;
  347. }
  348. m_glyphs_texture_map.set(glyphs_texture_key, { current_x, current_y, bitmap->width(), bitmap->height() });
  349. current_x += bitmap->width() + padding;
  350. row_height = max(row_height, bitmap->height());
  351. }
  352. auto glyphs_texture_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { texture_width, current_y + row_height }));
  353. auto glyphs_texure_painter = Gfx::Painter(*glyphs_texture_bitmap);
  354. for (auto const& [glyphs_texture_key, glyph_bitmap] : glyph_bitmaps) {
  355. auto rect = m_glyphs_texture_map.get(glyphs_texture_key).value();
  356. glyphs_texure_painter.blit({ rect.x(), rect.y() }, glyph_bitmap, glyph_bitmap->rect());
  357. }
  358. m_glyphs_texture_size = glyphs_texture_bitmap->size();
  359. GL::upload_texture_data(m_glyphs_texture, *glyphs_texture_bitmap);
  360. }
  361. void Painter::draw_glyph_run(Vector<Gfx::DrawGlyphOrEmoji> const& glyph_run, Color const& color)
  362. {
  363. bind_target_canvas();
  364. Vector<GLfloat> vertices;
  365. vertices.ensure_capacity(glyph_run.size() * 24);
  366. for (auto& glyph_or_emoji : glyph_run) {
  367. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  368. auto& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  369. auto const* font = glyph.font;
  370. auto code_point = glyph.code_point;
  371. auto point = glyph.position;
  372. auto maybe_texture_rect = m_glyphs_texture_map.get(GlyphsTextureKey { font, code_point });
  373. if (!maybe_texture_rect.has_value()) {
  374. continue;
  375. }
  376. auto texture_rect = to_texture_space(maybe_texture_rect.value().to_type<float>(), m_glyphs_texture_size);
  377. auto glyph_position = point + Gfx::FloatPoint(font->glyph_left_bearing(code_point), 0);
  378. auto glyph_size = maybe_texture_rect->size().to_type<float>();
  379. auto glyph_rect = transform().map(Gfx::FloatRect { glyph_position, glyph_size });
  380. auto rect_in_clip_space = to_clip_space(glyph_rect);
  381. // p0 --- p1
  382. // | \ |
  383. // | \ |
  384. // | \ |
  385. // p2 --- p3
  386. auto p0 = rect_in_clip_space.top_left();
  387. auto p1 = rect_in_clip_space.top_right();
  388. auto p2 = rect_in_clip_space.bottom_left();
  389. auto p3 = rect_in_clip_space.bottom_right();
  390. auto s0 = texture_rect.top_left();
  391. auto s1 = texture_rect.top_right();
  392. auto s2 = texture_rect.bottom_left();
  393. auto s3 = texture_rect.bottom_right();
  394. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& s1, auto& s2, auto& s3) {
  395. vertices.unchecked_append(p1.x());
  396. vertices.unchecked_append(p1.y());
  397. vertices.unchecked_append(s1.x());
  398. vertices.unchecked_append(s1.y());
  399. vertices.unchecked_append(p2.x());
  400. vertices.unchecked_append(p2.y());
  401. vertices.unchecked_append(s2.x());
  402. vertices.unchecked_append(s2.y());
  403. vertices.unchecked_append(p3.x());
  404. vertices.unchecked_append(p3.y());
  405. vertices.unchecked_append(s3.x());
  406. vertices.unchecked_append(s3.y());
  407. };
  408. add_triangle(p0, p1, p3, s0, s1, s3);
  409. add_triangle(p0, p3, p2, s0, s3, s2);
  410. }
  411. }
  412. auto vbo = GL::create_buffer();
  413. GL::upload_to_buffer(vbo, vertices);
  414. auto vao = GL::create_vertex_array();
  415. GL::bind_vertex_array(vao);
  416. GL::bind_buffer(vbo);
  417. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  418. m_blit_program.use();
  419. GL::bind_texture(m_glyphs_texture);
  420. GL::set_texture_scale_mode(GL::ScalingMode::Nearest);
  421. auto position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  422. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  423. GL::set_uniform(color_uniform, red, green, blue, alpha);
  424. GL::set_vertex_attribute(position_attribute, 0, 4);
  425. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 4);
  426. GL::delete_buffer(vbo);
  427. GL::delete_vertex_array(vao);
  428. }
  429. void Painter::fill_rect_with_linear_gradient(Gfx::IntRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  430. {
  431. fill_rect_with_linear_gradient(rect.to_type<float>(), stops, angle, repeat_length);
  432. }
  433. void Painter::fill_rect_with_linear_gradient(Gfx::FloatRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  434. {
  435. bind_target_canvas();
  436. // FIXME: Implement support for angle and repeat_length
  437. (void)angle;
  438. (void)repeat_length;
  439. Vector<GLfloat> vertices;
  440. Vector<GLfloat> colors;
  441. for (size_t stop_index = 0; stop_index < stops.size() - 1; stop_index++) {
  442. auto const& stop_start = stops[stop_index];
  443. auto const& stop_end = stops[stop_index + 1];
  444. // The gradient is divided into segments that represent linear gradients between adjacent pairs of stops.
  445. auto segment_rect_location = rect.location();
  446. segment_rect_location.set_x(segment_rect_location.x() + stop_start.position * rect.width());
  447. auto segment_rect_width = (stop_end.position - stop_start.position) * rect.width();
  448. auto segment_rect_height = rect.height();
  449. auto segment_rect = transform().map(Gfx::FloatRect { segment_rect_location.x(), segment_rect_location.y(), segment_rect_width, segment_rect_height });
  450. auto rect_in_clip_space = to_clip_space(segment_rect);
  451. // p0 --- p1
  452. // | \ |
  453. // | \ |
  454. // | \ |
  455. // p2 --- p3
  456. auto p0 = rect_in_clip_space.top_left();
  457. auto p1 = rect_in_clip_space.top_right();
  458. auto p2 = rect_in_clip_space.bottom_left();
  459. auto p3 = rect_in_clip_space.bottom_right();
  460. auto c0 = gfx_color_to_opengl_color(stop_start.color);
  461. auto c1 = gfx_color_to_opengl_color(stop_end.color);
  462. auto c2 = gfx_color_to_opengl_color(stop_start.color);
  463. auto c3 = gfx_color_to_opengl_color(stop_end.color);
  464. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& c1, auto& c2, auto& c3) {
  465. vertices.append(p1.x());
  466. vertices.append(p1.y());
  467. colors.append(c1.red);
  468. colors.append(c1.green);
  469. colors.append(c1.blue);
  470. colors.append(c1.alpha);
  471. vertices.append(p2.x());
  472. vertices.append(p2.y());
  473. colors.append(c2.red);
  474. colors.append(c2.green);
  475. colors.append(c2.blue);
  476. colors.append(c2.alpha);
  477. vertices.append(p3.x());
  478. vertices.append(p3.y());
  479. colors.append(c3.red);
  480. colors.append(c3.green);
  481. colors.append(c3.blue);
  482. colors.append(c3.alpha);
  483. };
  484. add_triangle(p0, p1, p3, c0, c1, c3);
  485. add_triangle(p0, p3, p2, c0, c3, c2);
  486. }
  487. auto vao = GL::create_vertex_array();
  488. GL::bind_vertex_array(vao);
  489. auto vbo_vertices = GL::create_buffer();
  490. GL::upload_to_buffer(vbo_vertices, vertices);
  491. auto vbo_colors = GL::create_buffer();
  492. GL::upload_to_buffer(vbo_colors, colors);
  493. m_linear_gradient_program.use();
  494. auto position_attribute = m_linear_gradient_program.get_attribute_location("aVertexPosition");
  495. auto color_attribute = m_linear_gradient_program.get_attribute_location("aColor");
  496. GL::bind_buffer(vbo_vertices);
  497. GL::set_vertex_attribute(position_attribute, 0, 2);
  498. GL::bind_buffer(vbo_colors);
  499. GL::set_vertex_attribute(color_attribute, 0, 4);
  500. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 2);
  501. }
  502. void Painter::save()
  503. {
  504. m_state_stack.append(state());
  505. }
  506. void Painter::restore()
  507. {
  508. VERIFY(!m_state_stack.is_empty());
  509. m_state_stack.take_last();
  510. }
  511. void Painter::set_clip_rect(Gfx::IntRect rect)
  512. {
  513. GL::enable_scissor_test(transform().map(rect));
  514. }
  515. void Painter::clear_clip_rect()
  516. {
  517. GL::disable_scissor_test();
  518. }
  519. void Painter::bind_target_canvas()
  520. {
  521. m_target_canvas->bind();
  522. GL::set_viewport({ 0, 0, m_target_canvas->size().width(), m_target_canvas->size().height() });
  523. }
  524. void Painter::set_target_canvas(NonnullRefPtr<Canvas> canvas)
  525. {
  526. m_target_canvas = canvas;
  527. canvas->bind();
  528. GL::set_viewport({ 0, 0, canvas->size().width(), canvas->size().height() });
  529. }
  530. void Painter::flush(Gfx::Bitmap& bitmap)
  531. {
  532. m_target_canvas->bind();
  533. GL::read_pixels({ 0, 0, bitmap.width(), bitmap.height() }, bitmap);
  534. }
  535. }