Painter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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* 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(vertex_shader_source, solid_color_fragment_shader_source))
  130. , m_rounded_rectangle_program(Program::create(vertex_shader_source, rect_with_rounded_corners_fragment_shader_source))
  131. , m_blit_program(Program::create(blit_vertex_shader_source, blit_fragment_shader_source))
  132. , m_linear_gradient_program(Program::create(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. flush();
  140. }
  141. void Painter::clear(Gfx::Color color)
  142. {
  143. GL::clear_color(color);
  144. }
  145. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  146. {
  147. fill_rect(rect.to_type<float>(), color);
  148. }
  149. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  150. {
  151. return {
  152. rect.left(),
  153. rect.top(),
  154. rect.left(),
  155. rect.bottom(),
  156. rect.right(),
  157. rect.bottom(),
  158. rect.right(),
  159. rect.top(),
  160. };
  161. }
  162. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  163. {
  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. auto transformed_rect = transform().map(rect);
  189. auto vertices = rect_to_vertices(to_clip_space(transformed_rect));
  190. auto vbo = GL::create_buffer();
  191. GL::upload_to_buffer(vbo, vertices);
  192. auto vao = GL::create_vertex_array();
  193. GL::bind_vertex_array(vao);
  194. GL::bind_buffer(vbo);
  195. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  196. m_rounded_rectangle_program.use();
  197. auto position_attribute = m_rounded_rectangle_program.get_attribute_location("aVertexPosition");
  198. GL::set_vertex_attribute(position_attribute, 0, 2);
  199. auto color_uniform = m_rounded_rectangle_program.get_uniform_location("uColor");
  200. GL::set_uniform(color_uniform, red, green, blue, alpha);
  201. auto rect_center_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCenter");
  202. GL::set_uniform(rect_center_uniform, transformed_rect.center().x(), transformed_rect.center().y());
  203. auto rect_corner_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCorner");
  204. GL::set_uniform(rect_corner_uniform, rect.width() / 2, rect.height() / 2);
  205. auto top_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopLeftRadius");
  206. GL::set_uniform(top_left_corner_radius_uniform, top_left_radius.horizontal_radius, top_left_radius.vertical_radius);
  207. auto top_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopRightRadius");
  208. GL::set_uniform(top_right_corner_radius_uniform, top_right_radius.horizontal_radius, top_right_radius.vertical_radius);
  209. auto bottom_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomLeftRadius");
  210. GL::set_uniform(bottom_left_corner_radius_uniform, bottom_left_radius.horizontal_radius, bottom_left_radius.vertical_radius);
  211. auto bottom_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomRightRadius");
  212. GL::set_uniform(bottom_right_corner_radius_uniform, bottom_right_radius.horizontal_radius, bottom_right_radius.vertical_radius);
  213. GL::enable_blending();
  214. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  215. GL::delete_buffer(vbo);
  216. GL::delete_vertex_array(vao);
  217. }
  218. void Painter::draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color)
  219. {
  220. draw_line(a.to_type<float>(), b.to_type<float>(), thickness, color);
  221. }
  222. void Painter::draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Color color)
  223. {
  224. auto midpoint = (a + b) / 2.0f;
  225. auto length = a.distance_from(b);
  226. auto angle = AK::atan2(b.y() - a.y(), b.x() - a.x());
  227. auto offset = Gfx::FloatPoint {
  228. (length / 2) * AK::cos(angle) - (thickness / 2) * AK::sin(angle),
  229. (length / 2) * AK::sin(angle) + (thickness / 2) * AK::cos(angle),
  230. };
  231. auto rect = Gfx::FloatRect(midpoint - offset, { length, thickness });
  232. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  233. auto vbo = GL::create_buffer();
  234. GL::upload_to_buffer(vbo, vertices);
  235. auto vao = GL::create_vertex_array();
  236. GL::bind_vertex_array(vao);
  237. GL::bind_buffer(vbo);
  238. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  239. m_rectangle_program.use();
  240. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  241. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  242. GL::set_uniform(color_uniform, red, green, blue, alpha);
  243. GL::set_vertex_attribute(position_attribute, 0, 2);
  244. GL::enable_blending();
  245. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  246. GL::delete_buffer(vbo);
  247. GL::delete_vertex_array(vao);
  248. }
  249. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  250. {
  251. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  252. }
  253. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  254. {
  255. auto x = rect.x() / image_size.width();
  256. auto y = rect.y() / image_size.height();
  257. auto width = rect.width() / image_size.width();
  258. auto height = rect.height() / image_size.height();
  259. return { x, y, width, height };
  260. }
  261. static GL::ScalingMode to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  262. {
  263. switch (scaling_mode) {
  264. case Painter::ScalingMode::NearestNeighbor:
  265. return GL::ScalingMode::Nearest;
  266. case Painter::ScalingMode::Bilinear:
  267. return GL::ScalingMode::Linear;
  268. default:
  269. VERIFY_NOT_REACHED();
  270. }
  271. }
  272. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  273. {
  274. m_blit_program.use();
  275. // FIXME: We should reuse textures across repaints if possible.
  276. auto texture = GL::create_texture();
  277. GL::upload_texture_data(texture, bitmap);
  278. auto scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  279. GL::set_texture_scale_mode(scaling_mode_gl);
  280. auto dst_rect_in_clip_space = to_clip_space(transform().map(dst_rect));
  281. auto src_rect_in_texture_space = to_texture_space(src_rect, bitmap.size());
  282. Vector<GLfloat> vertices;
  283. vertices.ensure_capacity(16);
  284. auto add_vertex = [&](auto const& p, auto const& s) {
  285. vertices.append(p.x());
  286. vertices.append(p.y());
  287. vertices.append(s.x());
  288. vertices.append(s.y());
  289. };
  290. add_vertex(dst_rect_in_clip_space.top_left(), src_rect_in_texture_space.top_left());
  291. add_vertex(dst_rect_in_clip_space.bottom_left(), src_rect_in_texture_space.bottom_left());
  292. add_vertex(dst_rect_in_clip_space.bottom_right(), src_rect_in_texture_space.bottom_right());
  293. add_vertex(dst_rect_in_clip_space.top_right(), src_rect_in_texture_space.top_right());
  294. auto vbo = GL::create_buffer();
  295. GL::upload_to_buffer(vbo, vertices);
  296. auto vao = GL::create_vertex_array();
  297. GL::bind_vertex_array(vao);
  298. GL::bind_buffer(vbo);
  299. auto vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  300. GL::set_vertex_attribute(vertex_position_attribute, 0, 4);
  301. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  302. GL::set_uniform(color_uniform, 1, 1, 1, 1);
  303. GL::enable_blending();
  304. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  305. GL::delete_texture(texture);
  306. GL::delete_buffer(vbo);
  307. GL::delete_vertex_array(vao);
  308. }
  309. void Painter::prepare_glyph_texture(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_glyphs)
  310. {
  311. HashMap<GlyphsTextureKey, NonnullRefPtr<Gfx::Bitmap>> glyph_bitmaps;
  312. for (auto const& [font, code_points] : unique_glyphs) {
  313. for (auto const& code_point : code_points) {
  314. auto glyph = font->glyph(code_point);
  315. if (glyph.bitmap()) {
  316. auto atlas_key = GlyphsTextureKey { font, code_point };
  317. glyph_bitmaps.set(atlas_key, *glyph.bitmap());
  318. }
  319. }
  320. }
  321. if (glyph_bitmaps.is_empty())
  322. return;
  323. Vector<GlyphsTextureKey> glyphs_sorted_by_height;
  324. glyphs_sorted_by_height.ensure_capacity(glyph_bitmaps.size());
  325. for (auto const& [atlas_key, bitmap] : glyph_bitmaps) {
  326. glyphs_sorted_by_height.append(atlas_key);
  327. }
  328. quick_sort(glyphs_sorted_by_height, [&](auto const& a, auto const& b) {
  329. auto const& bitmap_a = *glyph_bitmaps.get(a);
  330. auto const& bitmap_b = *glyph_bitmaps.get(b);
  331. return bitmap_a->height() > bitmap_b->height();
  332. });
  333. int current_x = 0;
  334. int current_y = 0;
  335. int row_height = 0;
  336. int texture_width = 512;
  337. int padding = 1;
  338. for (auto const& glyphs_texture_key : glyphs_sorted_by_height) {
  339. auto const& bitmap = *glyph_bitmaps.get(glyphs_texture_key);
  340. if (current_x + bitmap->width() > texture_width) {
  341. current_x = 0;
  342. current_y += row_height + padding;
  343. row_height = 0;
  344. }
  345. m_glyphs_texture_map.set(glyphs_texture_key, { current_x, current_y, bitmap->width(), bitmap->height() });
  346. current_x += bitmap->width() + padding;
  347. row_height = max(row_height, bitmap->height());
  348. }
  349. auto glyphs_texture_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { texture_width, current_y + row_height }));
  350. auto glyphs_texure_painter = Gfx::Painter(*glyphs_texture_bitmap);
  351. for (auto const& [glyphs_texture_key, glyph_bitmap] : glyph_bitmaps) {
  352. auto rect = m_glyphs_texture_map.get(glyphs_texture_key).value();
  353. glyphs_texure_painter.blit({ rect.x(), rect.y() }, glyph_bitmap, glyph_bitmap->rect());
  354. }
  355. m_glyphs_texture_size = glyphs_texture_bitmap->size();
  356. GL::upload_texture_data(m_glyphs_texture, *glyphs_texture_bitmap);
  357. }
  358. void Painter::draw_glyph_run(Vector<Gfx::DrawGlyphOrEmoji> const& glyph_run, Color const& color)
  359. {
  360. Vector<GLfloat> vertices;
  361. vertices.ensure_capacity(glyph_run.size() * 24);
  362. for (auto& glyph_or_emoji : glyph_run) {
  363. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  364. auto& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  365. auto const* font = glyph.font;
  366. auto code_point = glyph.code_point;
  367. auto point = glyph.position;
  368. auto maybe_texture_rect = m_glyphs_texture_map.get(GlyphsTextureKey { font, code_point });
  369. if (!maybe_texture_rect.has_value()) {
  370. continue;
  371. }
  372. auto texture_rect = to_texture_space(maybe_texture_rect.value().to_type<float>(), m_glyphs_texture_size);
  373. auto glyph_position = point + Gfx::FloatPoint(font->glyph_left_bearing(code_point), 0);
  374. auto glyph_size = maybe_texture_rect->size().to_type<float>();
  375. auto glyph_rect = transform().map(Gfx::FloatRect { glyph_position, glyph_size });
  376. auto rect_in_clip_space = to_clip_space(glyph_rect);
  377. // p0 --- p1
  378. // | \ |
  379. // | \ |
  380. // | \ |
  381. // p2 --- p3
  382. auto p0 = rect_in_clip_space.top_left();
  383. auto p1 = rect_in_clip_space.top_right();
  384. auto p2 = rect_in_clip_space.bottom_left();
  385. auto p3 = rect_in_clip_space.bottom_right();
  386. auto s0 = texture_rect.top_left();
  387. auto s1 = texture_rect.top_right();
  388. auto s2 = texture_rect.bottom_left();
  389. auto s3 = texture_rect.bottom_right();
  390. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& s1, auto& s2, auto& s3) {
  391. vertices.unchecked_append(p1.x());
  392. vertices.unchecked_append(p1.y());
  393. vertices.unchecked_append(s1.x());
  394. vertices.unchecked_append(s1.y());
  395. vertices.unchecked_append(p2.x());
  396. vertices.unchecked_append(p2.y());
  397. vertices.unchecked_append(s2.x());
  398. vertices.unchecked_append(s2.y());
  399. vertices.unchecked_append(p3.x());
  400. vertices.unchecked_append(p3.y());
  401. vertices.unchecked_append(s3.x());
  402. vertices.unchecked_append(s3.y());
  403. };
  404. add_triangle(p0, p1, p3, s0, s1, s3);
  405. add_triangle(p0, p3, p2, s0, s3, s2);
  406. }
  407. }
  408. auto vbo = GL::create_buffer();
  409. GL::upload_to_buffer(vbo, vertices);
  410. auto vao = GL::create_vertex_array();
  411. GL::bind_vertex_array(vao);
  412. GL::bind_buffer(vbo);
  413. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  414. m_blit_program.use();
  415. GL::bind_texture(m_glyphs_texture);
  416. GL::set_texture_scale_mode(GL::ScalingMode::Nearest);
  417. auto position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  418. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  419. GL::set_uniform(color_uniform, red, green, blue, alpha);
  420. GL::set_vertex_attribute(position_attribute, 0, 4);
  421. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 4);
  422. GL::delete_buffer(vbo);
  423. GL::delete_vertex_array(vao);
  424. }
  425. void Painter::fill_rect_with_linear_gradient(Gfx::IntRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  426. {
  427. fill_rect_with_linear_gradient(rect.to_type<float>(), stops, angle, repeat_length);
  428. }
  429. void Painter::fill_rect_with_linear_gradient(Gfx::FloatRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  430. {
  431. // FIXME: Implement support for angle and repeat_length
  432. (void)angle;
  433. (void)repeat_length;
  434. Vector<GLfloat> vertices;
  435. Vector<GLfloat> colors;
  436. for (size_t stop_index = 0; stop_index < stops.size() - 1; stop_index++) {
  437. auto const& stop_start = stops[stop_index];
  438. auto const& stop_end = stops[stop_index + 1];
  439. // The gradient is divided into segments that represent linear gradients between adjacent pairs of stops.
  440. auto segment_rect_location = rect.location();
  441. segment_rect_location.set_x(segment_rect_location.x() + stop_start.position * rect.width());
  442. auto segment_rect_width = (stop_end.position - stop_start.position) * rect.width();
  443. auto segment_rect_height = rect.height();
  444. auto segment_rect = transform().map(Gfx::FloatRect { segment_rect_location.x(), segment_rect_location.y(), segment_rect_width, segment_rect_height });
  445. auto rect_in_clip_space = to_clip_space(segment_rect);
  446. // p0 --- p1
  447. // | \ |
  448. // | \ |
  449. // | \ |
  450. // p2 --- p3
  451. auto p0 = rect_in_clip_space.top_left();
  452. auto p1 = rect_in_clip_space.top_right();
  453. auto p2 = rect_in_clip_space.bottom_left();
  454. auto p3 = rect_in_clip_space.bottom_right();
  455. auto c0 = gfx_color_to_opengl_color(stop_start.color);
  456. auto c1 = gfx_color_to_opengl_color(stop_end.color);
  457. auto c2 = gfx_color_to_opengl_color(stop_start.color);
  458. auto c3 = gfx_color_to_opengl_color(stop_end.color);
  459. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& c1, auto& c2, auto& c3) {
  460. vertices.append(p1.x());
  461. vertices.append(p1.y());
  462. colors.append(c1.red);
  463. colors.append(c1.green);
  464. colors.append(c1.blue);
  465. colors.append(c1.alpha);
  466. vertices.append(p2.x());
  467. vertices.append(p2.y());
  468. colors.append(c2.red);
  469. colors.append(c2.green);
  470. colors.append(c2.blue);
  471. colors.append(c2.alpha);
  472. vertices.append(p3.x());
  473. vertices.append(p3.y());
  474. colors.append(c3.red);
  475. colors.append(c3.green);
  476. colors.append(c3.blue);
  477. colors.append(c3.alpha);
  478. };
  479. add_triangle(p0, p1, p3, c0, c1, c3);
  480. add_triangle(p0, p3, p2, c0, c3, c2);
  481. }
  482. auto vao = GL::create_vertex_array();
  483. GL::bind_vertex_array(vao);
  484. auto vbo_vertices = GL::create_buffer();
  485. GL::upload_to_buffer(vbo_vertices, vertices);
  486. auto vbo_colors = GL::create_buffer();
  487. GL::upload_to_buffer(vbo_colors, colors);
  488. m_linear_gradient_program.use();
  489. auto position_attribute = m_linear_gradient_program.get_attribute_location("aVertexPosition");
  490. auto color_attribute = m_linear_gradient_program.get_attribute_location("aColor");
  491. GL::bind_buffer(vbo_vertices);
  492. GL::set_vertex_attribute(position_attribute, 0, 2);
  493. GL::bind_buffer(vbo_colors);
  494. GL::set_vertex_attribute(color_attribute, 0, 4);
  495. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 2);
  496. }
  497. void Painter::save()
  498. {
  499. m_state_stack.append(state());
  500. }
  501. void Painter::restore()
  502. {
  503. VERIFY(!m_state_stack.is_empty());
  504. m_state_stack.take_last();
  505. }
  506. void Painter::set_clip_rect(Gfx::IntRect rect)
  507. {
  508. GL::enable_scissor_test(transform().map(rect));
  509. }
  510. void Painter::clear_clip_rect()
  511. {
  512. GL::disable_scissor_test();
  513. }
  514. void Painter::set_target_bitmap(Gfx::Bitmap& bitmap)
  515. {
  516. if (m_target_framebuffer.has_value()) {
  517. GL::delete_framebuffer(*m_target_framebuffer);
  518. m_target_framebuffer = {};
  519. }
  520. m_target_framebuffer = GL::create_framebuffer(bitmap.size());
  521. GL::bind_framebuffer(*m_target_framebuffer);
  522. GL::set_viewport({ 0, 0, bitmap.width(), bitmap.height() });
  523. m_target_bitmap = bitmap;
  524. }
  525. void Painter::flush()
  526. {
  527. VERIFY(m_target_bitmap.has_value());
  528. GL::bind_framebuffer(*m_target_framebuffer);
  529. GL::read_pixels({ 0, 0, m_target_bitmap->width(), m_target_bitmap->height() }, *m_target_bitmap);
  530. }
  531. }