Painter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 <LibAccelGfx/GL.h>
  8. #include <LibAccelGfx/Painter.h>
  9. #include <LibGfx/ImmutableBitmap.h>
  10. #include <LibGfx/Painter.h>
  11. namespace AccelGfx {
  12. struct ColorComponents {
  13. float red;
  14. float green;
  15. float blue;
  16. float alpha;
  17. };
  18. static ColorComponents gfx_color_to_opengl_color(Gfx::Color color)
  19. {
  20. ColorComponents components;
  21. components.red = static_cast<float>(color.red()) / 255.0f;
  22. components.green = static_cast<float>(color.green()) / 255.0f;
  23. components.blue = static_cast<float>(color.blue()) / 255.0f;
  24. components.alpha = static_cast<float>(color.alpha()) / 255.0f;
  25. return components;
  26. }
  27. Gfx::FloatRect Painter::to_clip_space(Gfx::FloatRect const& screen_rect) const
  28. {
  29. float x = 2.0f * screen_rect.x() / m_target_canvas->size().width() - 1.0f;
  30. float y = -1.0f + 2.0f * screen_rect.y() / m_target_canvas->size().height();
  31. float width = 2.0f * screen_rect.width() / m_target_canvas->size().width();
  32. float height = 2.0f * screen_rect.height() / m_target_canvas->size().height();
  33. return { x, y, width, height };
  34. }
  35. char const* vertex_shader_source = R"(
  36. #version 330 core
  37. in vec2 aVertexPosition;
  38. void main() {
  39. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  40. }
  41. )";
  42. char const* rect_with_rounded_corners_fragment_shader_source = R"(
  43. #version 330 core
  44. uniform vec2 uRectCenter;
  45. uniform vec2 uRectCorner;
  46. uniform vec2 uTopLeftRadius;
  47. uniform vec2 uTopRightRadius;
  48. uniform vec2 uBottomLeftRadius;
  49. uniform vec2 uBottomRightRadius;
  50. uniform vec4 uColor;
  51. out vec4 fragColor;
  52. bool isPointWithinEllipse(vec2 point, vec2 radius) {
  53. vec2 normalizedPoint = point / radius;
  54. return dot(normalizedPoint, normalizedPoint) <= 1.0;
  55. }
  56. void main() {
  57. vec2 p = gl_FragCoord.xy - uRectCenter;
  58. vec2 cornerRadius = vec2(0.0, 0.0);
  59. if (p.x < 0.0 && p.y < 0.0) {
  60. cornerRadius = uTopLeftRadius;
  61. } else if (p.x > 0.0 && p.y < 0.0) {
  62. cornerRadius = uTopRightRadius;
  63. } else if (p.x < 0.0 && p.y > 0.0) {
  64. cornerRadius = uBottomLeftRadius;
  65. } else if (p.x > 0.0 && p.y > 0.0) {
  66. cornerRadius = uBottomRightRadius;
  67. }
  68. vec2 q = abs(p) - (uRectCorner - cornerRadius);
  69. if (q.x < 0 || q.y < 0 || isPointWithinEllipse(q, cornerRadius)) {
  70. fragColor = uColor;
  71. } else {
  72. discard;
  73. }
  74. }
  75. )";
  76. char const* solid_color_fragment_shader_source = R"(
  77. #version 330 core
  78. uniform vec4 uColor;
  79. out vec4 fragColor;
  80. void main() {
  81. fragColor = uColor;
  82. }
  83. )";
  84. char const* blit_vertex_shader_source = R"(
  85. #version 330 core
  86. in vec4 aVertexPosition;
  87. out vec2 vTextureCoord;
  88. void main() {
  89. gl_Position = vec4(aVertexPosition.xy, 0.0, 1.0);
  90. vTextureCoord = aVertexPosition.zw;
  91. }
  92. )";
  93. char const* blit_fragment_shader_source = R"(
  94. #version 330 core
  95. uniform vec4 uColor;
  96. in vec2 vTextureCoord;
  97. uniform sampler2D uSampler;
  98. out vec4 fragColor;
  99. void main() {
  100. fragColor = texture(uSampler, vTextureCoord) * uColor;
  101. }
  102. )";
  103. char const* linear_gradient_vertex_shader_source = R"(
  104. #version 330 core
  105. layout (location = 0) in vec2 aVertexPosition;
  106. layout (location = 1) in vec4 aColor;
  107. out vec4 vColor;
  108. void main() {
  109. gl_Position = vec4(aVertexPosition, 0.0, 1.0);
  110. vColor = aColor;
  111. }
  112. )";
  113. char const* linear_gradient_fragment_shader_source = R"(
  114. #version 330 core
  115. out vec4 FragColor;
  116. in vec4 vColor;
  117. void main() {
  118. FragColor = vec4(vColor);
  119. }
  120. )";
  121. char const* blur_fragment_shader_source = R"(
  122. #version 330 core
  123. uniform vec2 uResolution;
  124. uniform int uRadius;
  125. uniform int uHorizontal;
  126. uniform sampler2D uSampler;
  127. in vec2 vTextureCoord;
  128. out vec4 fragColor;
  129. #define pow2(x) (x * x)
  130. const float pi = atan(1.0) * 4.0;
  131. float gaussian(vec2 i, float sigma) {
  132. return 1.0 / (2.0 * pi * pow2(sigma)) * exp(-((pow2(i.x) + pow2(i.y)) / (2.0 * pow2(sigma))));
  133. }
  134. void main() {
  135. vec2 scale = vec2(1.0) / uResolution.xy;
  136. float sigma = float(uRadius);
  137. vec4 col = vec4(0.0);
  138. float accum = 0.0;
  139. float weight = 0.0;
  140. for (int i = -uRadius; i <= uRadius; i++) {
  141. vec2 offset = vec2(i * uHorizontal, i * (1 - uHorizontal));
  142. weight = gaussian(offset, sigma);
  143. col += texture(uSampler, vTextureCoord + scale * offset) * weight;
  144. accum += weight;
  145. }
  146. fragColor = col / accum;
  147. }
  148. )";
  149. static void set_blending_mode(Painter::BlendingMode blending_mode)
  150. {
  151. switch (blending_mode) {
  152. case Painter::BlendingMode::AlphaAdd:
  153. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  154. break;
  155. case Painter::BlendingMode::AlphaOverride:
  156. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::Zero);
  157. break;
  158. case Painter::BlendingMode::AlphaPreserve:
  159. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::Zero, GL::BlendFactor::One);
  160. break;
  161. default:
  162. VERIFY_NOT_REACHED();
  163. }
  164. }
  165. HashMap<u32, GL::Texture> s_immutable_bitmap_texture_cache;
  166. NonnullOwnPtr<Painter> Painter::create(Context& context, NonnullRefPtr<Canvas> canvas)
  167. {
  168. return make<Painter>(context, canvas);
  169. }
  170. Painter::Painter(Context& context, NonnullRefPtr<Canvas> canvas)
  171. : m_context(context)
  172. , m_target_canvas(canvas)
  173. , m_rectangle_program(Program::create(Program::Name::RectangleProgram, vertex_shader_source, solid_color_fragment_shader_source))
  174. , m_rounded_rectangle_program(Program::create(Program::Name::RoundedRectangleProgram, vertex_shader_source, rect_with_rounded_corners_fragment_shader_source))
  175. , m_blit_program(Program::create(Program::Name::BlitProgram, blit_vertex_shader_source, blit_fragment_shader_source))
  176. , m_linear_gradient_program(Program::create(Program::Name::LinearGradientProgram, linear_gradient_vertex_shader_source, linear_gradient_fragment_shader_source))
  177. , m_blur_program(Program::create(Program::Name::BlurProgram, blit_vertex_shader_source, blur_fragment_shader_source))
  178. {
  179. m_state_stack.empend(State());
  180. state().clip_rect = { { 0, 0 }, m_target_canvas->size() };
  181. bind_target_canvas();
  182. }
  183. Painter::~Painter()
  184. {
  185. }
  186. void Painter::clear(Gfx::Color color)
  187. {
  188. bind_target_canvas();
  189. GL::clear_color(color);
  190. }
  191. void Painter::fill_rect(Gfx::IntRect rect, Gfx::Color color)
  192. {
  193. fill_rect(rect.to_type<float>(), color);
  194. }
  195. static Array<GLfloat, 8> rect_to_vertices(Gfx::FloatRect const& rect)
  196. {
  197. return {
  198. rect.left(),
  199. rect.top(),
  200. rect.left(),
  201. rect.bottom(),
  202. rect.right(),
  203. rect.bottom(),
  204. rect.right(),
  205. rect.top(),
  206. };
  207. }
  208. void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color)
  209. {
  210. bind_target_canvas();
  211. // Draw a filled rect (with `color`) using OpenGL after mapping it through the current transform.
  212. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  213. auto vbo = GL::create_buffer();
  214. GL::upload_to_buffer(vbo, vertices);
  215. auto vao = GL::create_vertex_array();
  216. GL::bind_vertex_array(vao);
  217. GL::bind_buffer(vbo);
  218. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  219. m_rectangle_program.use();
  220. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  221. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  222. GL::set_uniform(color_uniform, red, green, blue, alpha);
  223. GL::set_vertex_attribute(position_attribute, 0, 2);
  224. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  225. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  226. GL::delete_buffer(vbo);
  227. GL::delete_vertex_array(vao);
  228. }
  229. 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, BlendingMode blending_mode)
  230. {
  231. fill_rect_with_rounded_corners(rect.to_type<float>(), color, top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius, blending_mode);
  232. }
  233. 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, BlendingMode blending_mode)
  234. {
  235. bind_target_canvas();
  236. auto transformed_rect = transform().map(rect);
  237. auto vertices = rect_to_vertices(to_clip_space(transformed_rect));
  238. auto vbo = GL::create_buffer();
  239. GL::upload_to_buffer(vbo, vertices);
  240. auto vao = GL::create_vertex_array();
  241. GL::bind_vertex_array(vao);
  242. GL::bind_buffer(vbo);
  243. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  244. m_rounded_rectangle_program.use();
  245. auto position_attribute = m_rounded_rectangle_program.get_attribute_location("aVertexPosition");
  246. GL::set_vertex_attribute(position_attribute, 0, 2);
  247. auto color_uniform = m_rounded_rectangle_program.get_uniform_location("uColor");
  248. GL::set_uniform(color_uniform, red, green, blue, alpha);
  249. auto rect_center_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCenter");
  250. GL::set_uniform(rect_center_uniform, transformed_rect.center().x(), transformed_rect.center().y());
  251. auto rect_corner_uniform = m_rounded_rectangle_program.get_uniform_location("uRectCorner");
  252. GL::set_uniform(rect_corner_uniform, rect.width() / 2, rect.height() / 2);
  253. auto top_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopLeftRadius");
  254. GL::set_uniform(top_left_corner_radius_uniform, top_left_radius.horizontal_radius, top_left_radius.vertical_radius);
  255. auto top_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uTopRightRadius");
  256. GL::set_uniform(top_right_corner_radius_uniform, top_right_radius.horizontal_radius, top_right_radius.vertical_radius);
  257. auto bottom_left_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomLeftRadius");
  258. GL::set_uniform(bottom_left_corner_radius_uniform, bottom_left_radius.horizontal_radius, bottom_left_radius.vertical_radius);
  259. auto bottom_right_corner_radius_uniform = m_rounded_rectangle_program.get_uniform_location("uBottomRightRadius");
  260. GL::set_uniform(bottom_right_corner_radius_uniform, bottom_right_radius.horizontal_radius, bottom_right_radius.vertical_radius);
  261. set_blending_mode(blending_mode);
  262. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  263. GL::delete_buffer(vbo);
  264. GL::delete_vertex_array(vao);
  265. }
  266. void Painter::draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color)
  267. {
  268. draw_line(a.to_type<float>(), b.to_type<float>(), thickness, color);
  269. }
  270. void Painter::draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Color color)
  271. {
  272. bind_target_canvas();
  273. auto midpoint = (a + b) / 2.0f;
  274. auto length = a.distance_from(b);
  275. auto angle = AK::atan2(b.y() - a.y(), b.x() - a.x());
  276. auto offset = Gfx::FloatPoint {
  277. (length / 2) * AK::cos(angle) - (thickness / 2) * AK::sin(angle),
  278. (length / 2) * AK::sin(angle) + (thickness / 2) * AK::cos(angle),
  279. };
  280. auto rect = Gfx::FloatRect(midpoint - offset, { length, thickness });
  281. auto vertices = rect_to_vertices(to_clip_space(transform().map(rect)));
  282. auto vbo = GL::create_buffer();
  283. GL::upload_to_buffer(vbo, vertices);
  284. auto vao = GL::create_vertex_array();
  285. GL::bind_vertex_array(vao);
  286. GL::bind_buffer(vbo);
  287. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  288. m_rectangle_program.use();
  289. auto position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition");
  290. auto color_uniform = m_rectangle_program.get_uniform_location("uColor");
  291. GL::set_uniform(color_uniform, red, green, blue, alpha);
  292. GL::set_vertex_attribute(position_attribute, 0, 2);
  293. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  294. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  295. GL::delete_buffer(vbo);
  296. GL::delete_vertex_array(vao);
  297. }
  298. void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  299. {
  300. draw_scaled_bitmap(dest_rect.to_type<float>(), bitmap, src_rect.to_type<float>(), scaling_mode);
  301. }
  302. void Painter::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& immutable_bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode)
  303. {
  304. draw_scaled_immutable_bitmap(dst_rect.to_type<float>(), immutable_bitmap, src_rect.to_type<float>(), scaling_mode);
  305. }
  306. void Painter::draw_scaled_immutable_bitmap(Gfx::FloatRect const& dst_rect, Gfx::ImmutableBitmap const& immutable_bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  307. {
  308. auto texture = s_immutable_bitmap_texture_cache.get(immutable_bitmap.id());
  309. VERIFY(texture.has_value());
  310. blit_scaled_texture(dst_rect, texture.value(), src_rect, scaling_mode);
  311. }
  312. static Gfx::FloatRect to_texture_space(Gfx::FloatRect rect, Gfx::IntSize image_size)
  313. {
  314. auto x = rect.x() / image_size.width();
  315. auto y = rect.y() / image_size.height();
  316. auto width = rect.width() / image_size.width();
  317. auto height = rect.height() / image_size.height();
  318. return { x, y, width, height };
  319. }
  320. static GL::ScalingMode to_gl_scaling_mode(Painter::ScalingMode scaling_mode)
  321. {
  322. switch (scaling_mode) {
  323. case Painter::ScalingMode::NearestNeighbor:
  324. return GL::ScalingMode::Nearest;
  325. case Painter::ScalingMode::Bilinear:
  326. return GL::ScalingMode::Linear;
  327. default:
  328. VERIFY_NOT_REACHED();
  329. }
  330. }
  331. void Painter::draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode)
  332. {
  333. // FIXME: We should reuse textures across repaints if possible.
  334. auto texture = GL::create_texture();
  335. GL::upload_texture_data(texture, bitmap);
  336. blit_scaled_texture(dst_rect, texture, src_rect, scaling_mode);
  337. GL::delete_texture(texture);
  338. }
  339. void Painter::draw_glyph_run(Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color const& color)
  340. {
  341. bind_target_canvas();
  342. Vector<GLfloat> vertices;
  343. vertices.ensure_capacity(glyph_run.size() * 24);
  344. auto const& glyph_atlas = GlyphAtlas::the();
  345. for (auto const& glyph_or_emoji : glyph_run) {
  346. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  347. auto const& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  348. auto const& font = *glyph.font;
  349. auto code_point = glyph.code_point;
  350. auto point = glyph.position;
  351. auto maybe_texture_rect = glyph_atlas.get_glyph_rect(&font, code_point);
  352. if (!maybe_texture_rect.has_value()) {
  353. continue;
  354. }
  355. auto texture_rect = to_texture_space(maybe_texture_rect.value().to_type<float>(), *glyph_atlas.texture().size);
  356. auto glyph_position = point + Gfx::FloatPoint(font.glyph_left_bearing(code_point), 0);
  357. auto glyph_size = maybe_texture_rect->size().to_type<float>();
  358. auto glyph_rect = transform().map(Gfx::FloatRect { glyph_position, glyph_size });
  359. auto rect_in_clip_space = to_clip_space(glyph_rect);
  360. // p0 --- p1
  361. // | \ |
  362. // | \ |
  363. // | \ |
  364. // p2 --- p3
  365. auto p0 = rect_in_clip_space.top_left();
  366. auto p1 = rect_in_clip_space.top_right();
  367. auto p2 = rect_in_clip_space.bottom_left();
  368. auto p3 = rect_in_clip_space.bottom_right();
  369. auto s0 = texture_rect.top_left();
  370. auto s1 = texture_rect.top_right();
  371. auto s2 = texture_rect.bottom_left();
  372. auto s3 = texture_rect.bottom_right();
  373. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& s1, auto& s2, auto& s3) {
  374. vertices.unchecked_append(p1.x());
  375. vertices.unchecked_append(p1.y());
  376. vertices.unchecked_append(s1.x());
  377. vertices.unchecked_append(s1.y());
  378. vertices.unchecked_append(p2.x());
  379. vertices.unchecked_append(p2.y());
  380. vertices.unchecked_append(s2.x());
  381. vertices.unchecked_append(s2.y());
  382. vertices.unchecked_append(p3.x());
  383. vertices.unchecked_append(p3.y());
  384. vertices.unchecked_append(s3.x());
  385. vertices.unchecked_append(s3.y());
  386. };
  387. add_triangle(p0, p1, p3, s0, s1, s3);
  388. add_triangle(p0, p3, p2, s0, s3, s2);
  389. }
  390. }
  391. auto vbo = GL::create_buffer();
  392. GL::upload_to_buffer(vbo, vertices);
  393. auto vao = GL::create_vertex_array();
  394. GL::bind_vertex_array(vao);
  395. GL::bind_buffer(vbo);
  396. auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color);
  397. m_blit_program.use();
  398. GL::bind_texture(glyph_atlas.texture());
  399. GL::set_texture_scale_mode(GL::ScalingMode::Nearest);
  400. auto position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  401. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  402. GL::set_uniform(color_uniform, red, green, blue, alpha);
  403. GL::set_vertex_attribute(position_attribute, 0, 4);
  404. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  405. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 4);
  406. GL::delete_buffer(vbo);
  407. GL::delete_vertex_array(vao);
  408. }
  409. void Painter::fill_rect_with_linear_gradient(Gfx::IntRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  410. {
  411. fill_rect_with_linear_gradient(rect.to_type<float>(), stops, angle, repeat_length);
  412. }
  413. void Painter::fill_rect_with_linear_gradient(Gfx::FloatRect const& rect, ReadonlySpan<Gfx::ColorStop> stops, float angle, Optional<float> repeat_length)
  414. {
  415. bind_target_canvas();
  416. // FIXME: Implement support for angle and repeat_length
  417. (void)angle;
  418. (void)repeat_length;
  419. Vector<GLfloat> vertices;
  420. Vector<GLfloat> colors;
  421. for (size_t stop_index = 0; stop_index < stops.size() - 1; stop_index++) {
  422. auto const& stop_start = stops[stop_index];
  423. auto const& stop_end = stops[stop_index + 1];
  424. // The gradient is divided into segments that represent linear gradients between adjacent pairs of stops.
  425. auto segment_rect_location = rect.location();
  426. segment_rect_location.set_x(segment_rect_location.x() + stop_start.position * rect.width());
  427. auto segment_rect_width = (stop_end.position - stop_start.position) * rect.width();
  428. auto segment_rect_height = rect.height();
  429. auto segment_rect = transform().map(Gfx::FloatRect { segment_rect_location.x(), segment_rect_location.y(), segment_rect_width, segment_rect_height });
  430. auto rect_in_clip_space = to_clip_space(segment_rect);
  431. // p0 --- p1
  432. // | \ |
  433. // | \ |
  434. // | \ |
  435. // p2 --- p3
  436. auto p0 = rect_in_clip_space.top_left();
  437. auto p1 = rect_in_clip_space.top_right();
  438. auto p2 = rect_in_clip_space.bottom_left();
  439. auto p3 = rect_in_clip_space.bottom_right();
  440. auto c0 = gfx_color_to_opengl_color(stop_start.color);
  441. auto c1 = gfx_color_to_opengl_color(stop_end.color);
  442. auto c2 = gfx_color_to_opengl_color(stop_start.color);
  443. auto c3 = gfx_color_to_opengl_color(stop_end.color);
  444. auto add_triangle = [&](auto& p1, auto& p2, auto& p3, auto& c1, auto& c2, auto& c3) {
  445. vertices.append(p1.x());
  446. vertices.append(p1.y());
  447. colors.append(c1.red * c1.alpha);
  448. colors.append(c1.green * c1.alpha);
  449. colors.append(c1.blue * c1.alpha);
  450. colors.append(c1.alpha);
  451. vertices.append(p2.x());
  452. vertices.append(p2.y());
  453. colors.append(c2.red * c2.alpha);
  454. colors.append(c2.green * c2.alpha);
  455. colors.append(c2.blue * c2.alpha);
  456. colors.append(c2.alpha);
  457. vertices.append(p3.x());
  458. vertices.append(p3.y());
  459. colors.append(c3.red * c3.alpha);
  460. colors.append(c3.green * c3.alpha);
  461. colors.append(c3.blue * c3.alpha);
  462. colors.append(c3.alpha);
  463. };
  464. add_triangle(p0, p1, p3, c0, c1, c3);
  465. add_triangle(p0, p3, p2, c0, c3, c2);
  466. }
  467. auto vao = GL::create_vertex_array();
  468. GL::bind_vertex_array(vao);
  469. auto vbo_vertices = GL::create_buffer();
  470. GL::upload_to_buffer(vbo_vertices, vertices);
  471. auto vbo_colors = GL::create_buffer();
  472. GL::upload_to_buffer(vbo_colors, colors);
  473. m_linear_gradient_program.use();
  474. auto position_attribute = m_linear_gradient_program.get_attribute_location("aVertexPosition");
  475. auto color_attribute = m_linear_gradient_program.get_attribute_location("aColor");
  476. GL::bind_buffer(vbo_vertices);
  477. GL::set_vertex_attribute(position_attribute, 0, 2);
  478. GL::bind_buffer(vbo_colors);
  479. GL::set_vertex_attribute(color_attribute, 0, 4);
  480. GL::enable_blending(GL::BlendFactor::One, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  481. GL::draw_arrays(GL::DrawPrimitive::Triangles, vertices.size() / 2);
  482. GL::delete_buffer(vbo_vertices);
  483. GL::delete_buffer(vbo_colors);
  484. GL::delete_vertex_array(vao);
  485. }
  486. void Painter::save()
  487. {
  488. m_state_stack.append(state());
  489. }
  490. void Painter::restore()
  491. {
  492. VERIFY(!m_state_stack.is_empty());
  493. m_state_stack.take_last();
  494. }
  495. void Painter::set_clip_rect(Gfx::IntRect rect)
  496. {
  497. state().clip_rect = transform().map(rect);
  498. GL::enable_scissor_test(transform().map(rect));
  499. }
  500. void Painter::clear_clip_rect()
  501. {
  502. state().clip_rect = { { 0, 0 }, m_target_canvas->size() };
  503. GL::disable_scissor_test();
  504. }
  505. void Painter::bind_target_canvas()
  506. {
  507. m_target_canvas->bind();
  508. GL::set_viewport({ 0, 0, m_target_canvas->size().width(), m_target_canvas->size().height() });
  509. GL::enable_scissor_test(state().clip_rect);
  510. }
  511. void Painter::flush(Gfx::Bitmap& bitmap)
  512. {
  513. m_target_canvas->bind();
  514. GL::read_pixels({ 0, 0, bitmap.width(), bitmap.height() }, bitmap);
  515. }
  516. void Painter::blit_canvas(Gfx::IntRect const& dst_rect, Canvas const& canvas, float opacity, Optional<Gfx::AffineTransform> affine_transform)
  517. {
  518. blit_canvas(dst_rect.to_type<float>(), canvas, opacity, move(affine_transform));
  519. }
  520. void Painter::blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const& canvas, float opacity, Optional<Gfx::AffineTransform> affine_transform)
  521. {
  522. auto texture = GL::Texture(canvas.framebuffer().texture);
  523. blit_scaled_texture(dst_rect, texture, { { 0, 0 }, canvas.size() }, Painter::ScalingMode::NearestNeighbor, opacity, move(affine_transform));
  524. }
  525. void Painter::blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const& canvas, Gfx::FloatRect const& src_rect, float opacity, Optional<Gfx::AffineTransform> affine_transform, BlendingMode blending_mode)
  526. {
  527. auto texture = GL::Texture(canvas.framebuffer().texture);
  528. blit_scaled_texture(dst_rect, texture, src_rect, Painter::ScalingMode::NearestNeighbor, opacity, move(affine_transform), blending_mode);
  529. }
  530. void Painter::blit_scaled_texture(Gfx::FloatRect const& dst_rect, GL::Texture const& texture, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode, float opacity, Optional<Gfx::AffineTransform> affine_transform, BlendingMode blending_mode)
  531. {
  532. bind_target_canvas();
  533. m_blit_program.use();
  534. auto dst_rect_rotated = dst_rect;
  535. Array<Gfx::FloatPoint, 4> dst_rect_vertices = {
  536. dst_rect_rotated.top_left(),
  537. dst_rect_rotated.bottom_left(),
  538. dst_rect_rotated.bottom_right(),
  539. dst_rect_rotated.top_right(),
  540. };
  541. if (affine_transform.has_value()) {
  542. for (auto& point : dst_rect_vertices)
  543. point = affine_transform->map(point);
  544. }
  545. auto const viewport_width = static_cast<float>(m_target_canvas->size().width());
  546. auto const viewport_height = static_cast<float>(m_target_canvas->size().height());
  547. for (auto& point : dst_rect_vertices) {
  548. point = transform().map(point);
  549. point.set_x(2.0f * point.x() / viewport_width - 1.0f);
  550. point.set_y(-1.0f + 2.0f * point.y() / viewport_height);
  551. }
  552. auto src_rect_in_texture_space = to_texture_space(src_rect, *texture.size);
  553. Vector<GLfloat> vertices;
  554. vertices.ensure_capacity(16);
  555. auto add_vertex = [&](auto const& p, auto const& s) {
  556. vertices.append(p.x());
  557. vertices.append(p.y());
  558. vertices.append(s.x());
  559. vertices.append(s.y());
  560. };
  561. add_vertex(dst_rect_vertices[0], src_rect_in_texture_space.top_left());
  562. add_vertex(dst_rect_vertices[1], src_rect_in_texture_space.bottom_left());
  563. add_vertex(dst_rect_vertices[2], src_rect_in_texture_space.bottom_right());
  564. add_vertex(dst_rect_vertices[3], src_rect_in_texture_space.top_right());
  565. auto vbo = GL::create_buffer();
  566. GL::upload_to_buffer(vbo, vertices);
  567. auto vao = GL::create_vertex_array();
  568. GL::bind_vertex_array(vao);
  569. GL::bind_buffer(vbo);
  570. auto vertex_position_attribute = m_blit_program.get_attribute_location("aVertexPosition");
  571. GL::set_vertex_attribute(vertex_position_attribute, 0, 4);
  572. auto color_uniform = m_blit_program.get_uniform_location("uColor");
  573. GL::set_uniform(color_uniform, 1, 1, 1, opacity);
  574. GL::bind_texture(texture);
  575. auto scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  576. GL::set_texture_scale_mode(scaling_mode_gl);
  577. set_blending_mode(blending_mode);
  578. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  579. GL::delete_buffer(vbo);
  580. GL::delete_vertex_array(vao);
  581. }
  582. void Painter::blit_blurred_texture(Gfx::FloatRect const& dst_rect, GL::Texture const& texture, Gfx::FloatRect const& src_rect, int radius, BlurDirection direction, ScalingMode scaling_mode)
  583. {
  584. bind_target_canvas();
  585. m_blur_program.use();
  586. auto dst_rect_in_clip_space = to_clip_space(transform().map(dst_rect));
  587. auto src_rect_in_texture_space = to_texture_space(src_rect, *texture.size);
  588. Vector<GLfloat> vertices;
  589. vertices.ensure_capacity(16);
  590. auto add_vertex = [&](auto const& p, auto const& s) {
  591. vertices.append(p.x());
  592. vertices.append(p.y());
  593. vertices.append(s.x());
  594. vertices.append(s.y());
  595. };
  596. add_vertex(dst_rect_in_clip_space.top_left(), src_rect_in_texture_space.top_left());
  597. add_vertex(dst_rect_in_clip_space.bottom_left(), src_rect_in_texture_space.bottom_left());
  598. add_vertex(dst_rect_in_clip_space.bottom_right(), src_rect_in_texture_space.bottom_right());
  599. add_vertex(dst_rect_in_clip_space.top_right(), src_rect_in_texture_space.top_right());
  600. auto vbo = GL::create_buffer();
  601. GL::upload_to_buffer(vbo, vertices);
  602. auto vao = GL::create_vertex_array();
  603. GL::bind_vertex_array(vao);
  604. GL::bind_buffer(vbo);
  605. auto vertex_position_attribute = m_blur_program.get_attribute_location("aVertexPosition");
  606. GL::set_vertex_attribute(vertex_position_attribute, 0, 4);
  607. auto resolution_uniform = m_blur_program.get_uniform_location("uResolution");
  608. GL::set_uniform(resolution_uniform, dst_rect.width(), dst_rect.height());
  609. auto radius_uniform = m_blur_program.get_uniform_location("uRadius");
  610. GL::set_uniform(radius_uniform, radius);
  611. auto direction_uniform = m_blur_program.get_uniform_location("uHorizontal");
  612. GL::set_uniform(direction_uniform, direction == BlurDirection::Horizontal ? 1 : 0);
  613. GL::bind_texture(texture);
  614. auto scaling_mode_gl = to_gl_scaling_mode(scaling_mode);
  615. GL::set_texture_scale_mode(scaling_mode_gl);
  616. GL::enable_blending(GL::BlendFactor::SrcAlpha, GL::BlendFactor::OneMinusSrcAlpha, GL::BlendFactor::One, GL::BlendFactor::One);
  617. GL::draw_arrays(GL::DrawPrimitive::TriangleFan, 4);
  618. GL::delete_buffer(vbo);
  619. GL::delete_vertex_array(vao);
  620. }
  621. void Painter::blit_blurred_canvas(Gfx::FloatRect const& dst_rect, Canvas const& canvas, int radius, BlurDirection direction, ScalingMode scaling_mode)
  622. {
  623. blit_blurred_texture(dst_rect, canvas.framebuffer().texture, { { 0, 0 }, canvas.size() }, radius, direction, scaling_mode);
  624. }
  625. void Painter::update_immutable_bitmap_texture_cache(HashMap<u32, Gfx::ImmutableBitmap const*>& immutable_bitmaps)
  626. {
  627. for (auto immutable_bitmap_id : s_immutable_bitmap_texture_cache.keys()) {
  628. if (!immutable_bitmaps.contains(immutable_bitmap_id)) {
  629. auto texture = s_immutable_bitmap_texture_cache.get(immutable_bitmap_id).value();
  630. GL::delete_texture(texture);
  631. s_immutable_bitmap_texture_cache.remove(immutable_bitmap_id);
  632. }
  633. }
  634. for (auto const& [id, immutable_bitmap] : immutable_bitmaps) {
  635. if (s_immutable_bitmap_texture_cache.contains(id))
  636. continue;
  637. auto texture = GL::create_texture();
  638. GL::upload_texture_data(texture, immutable_bitmap->bitmap());
  639. s_immutable_bitmap_texture_cache.set(id, texture);
  640. }
  641. }
  642. }