Painter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "Painter.h"
  2. #include "Font.h"
  3. #include "GraphicsBitmap.h"
  4. #include <AK/Assertions.h>
  5. #include <AK/StdLibExtras.h>
  6. #ifdef LIBGUI
  7. #include <LibGUI/GWidget.h>
  8. #include <LibGUI/GWindow.h>
  9. #include <LibC/gui.h>
  10. #include <LibC/stdio.h>
  11. #endif
  12. #define DEBUG_WIDGET_UNDERDRAW
  13. Painter::Painter(GraphicsBitmap& bitmap)
  14. {
  15. m_font = &Font::default_font();
  16. m_target = &bitmap;
  17. m_clip_rect = { { 0, 0 }, bitmap.size() };
  18. }
  19. #ifdef LIBGUI
  20. Painter::Painter(GWidget& widget)
  21. : m_font(&widget.font())
  22. {
  23. GUI_WindowBackingStoreInfo backing;
  24. int rc = gui_get_window_backing_store(widget.window()->window_id(), &backing);
  25. if (rc < 0) {
  26. perror("gui_get_window_backing_store");
  27. exit(1);
  28. }
  29. m_backing_store_id = backing.backing_store_id;
  30. m_target = GraphicsBitmap::create_wrapper(backing.size, backing.pixels);
  31. ASSERT(m_target);
  32. m_window = widget.window();
  33. m_translation.move_by(widget.relative_position());
  34. // NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store.
  35. m_clip_rect = widget.relative_rect();
  36. m_clip_rect.intersect(m_target->rect());
  37. #ifdef DEBUG_WIDGET_UNDERDRAW
  38. // If the widget is not opaque, let's not mess it up with debugging color.
  39. if (widget.fill_with_background_color() && m_window->main_widget() != &widget)
  40. fill_rect(widget.rect(), Color::Red);
  41. #endif
  42. }
  43. #endif
  44. Painter::~Painter()
  45. {
  46. #ifdef LIBGUI
  47. m_target = nullptr;
  48. int rc = gui_release_window_backing_store(m_backing_store_id);
  49. ASSERT(rc == 0);
  50. #endif
  51. }
  52. void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color)
  53. {
  54. auto rect = a_rect;
  55. rect.move_by(m_translation);
  56. rect.intersect(m_clip_rect);
  57. if (rect.is_empty())
  58. return;
  59. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  60. const unsigned dst_skip = m_target->width();
  61. for (int i = rect.height() - 1; i >= 0; --i) {
  62. for (int j = 0; j < rect.width(); ++j)
  63. set_pixel_with_draw_op(dst[j], color.value());
  64. dst += dst_skip;
  65. }
  66. }
  67. void Painter::fill_rect(const Rect& a_rect, Color color)
  68. {
  69. if (m_draw_op != DrawOp::Copy) {
  70. fill_rect_with_draw_op(a_rect, color);
  71. return;
  72. }
  73. auto rect = a_rect;
  74. rect.move_by(m_translation);
  75. rect.intersect(m_clip_rect);
  76. if (rect.is_empty())
  77. return;
  78. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  79. const unsigned dst_skip = m_target->width();
  80. for (int i = rect.height() - 1; i >= 0; --i) {
  81. fast_dword_fill(dst, color.value(), rect.width());
  82. dst += dst_skip;
  83. }
  84. }
  85. void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
  86. {
  87. #ifdef NO_FPU
  88. return fill_rect(a_rect, gradient_start);
  89. #endif
  90. auto rect = a_rect;
  91. rect.move_by(m_translation);
  92. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  93. if (clipped_rect.is_empty())
  94. return;
  95. int x_offset = clipped_rect.x() - rect.x();
  96. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  97. const unsigned dst_skip = m_target->width();
  98. float increment = (1.0/((rect.width())/255.0));
  99. int r2 = gradient_start.red();
  100. int g2 = gradient_start.green();
  101. int b2 = gradient_start.blue();
  102. int r1 = gradient_end.red();
  103. int g1 = gradient_end.green();
  104. int b1 = gradient_end.blue();
  105. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  106. float c = x_offset * increment;
  107. for (int j = 0; j < clipped_rect.width(); ++j) {
  108. dst[j] = Color(
  109. r1 / 255.0 * c + r2 / 255.0 * (255 - c),
  110. g1 / 255.0 * c + g2 / 255.0 * (255 - c),
  111. b1 / 255.0 * c + b2 / 255.0 * (255 - c)
  112. ).value();
  113. c += increment;
  114. }
  115. dst += dst_skip;
  116. }
  117. }
  118. void Painter::draw_rect(const Rect& a_rect, Color color, bool rough)
  119. {
  120. Rect rect = a_rect;
  121. rect.move_by(m_translation);
  122. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  123. if (clipped_rect.is_empty())
  124. return;
  125. int min_y = clipped_rect.top();
  126. int max_y = clipped_rect.bottom();
  127. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  128. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  129. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  130. fast_dword_fill(m_target->scanline(rect.top()) + start_x, color.value(), width);
  131. ++min_y;
  132. }
  133. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  134. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  135. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  136. fast_dword_fill(m_target->scanline(rect.bottom()) + start_x, color.value(), width);
  137. --max_y;
  138. }
  139. bool draw_left_side = rect.left() >= clipped_rect.left();
  140. bool draw_right_side = rect.right() == clipped_rect.right();
  141. if (draw_left_side && draw_right_side) {
  142. // Specialized loop when drawing both sides.
  143. for (int y = min_y; y <= max_y; ++y) {
  144. auto* bits = m_target->scanline(y);
  145. bits[rect.left()] = color.value();
  146. bits[rect.right()] = color.value();
  147. }
  148. } else {
  149. for (int y = min_y; y <= max_y; ++y) {
  150. auto* bits = m_target->scanline(y);
  151. if (draw_left_side)
  152. bits[rect.left()] = color.value();
  153. if (draw_right_side)
  154. bits[rect.right()] = color.value();
  155. }
  156. }
  157. }
  158. void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
  159. {
  160. Rect rect { p, bitmap.size() };
  161. rect.move_by(m_translation);
  162. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  163. if (clipped_rect.is_empty())
  164. return;
  165. const int first_row = clipped_rect.top() - rect.top();
  166. const int last_row = clipped_rect.bottom() - rect.top();
  167. const int first_column = clipped_rect.left() - rect.left();
  168. const int last_column = clipped_rect.right() - rect.left();
  169. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  170. const size_t dst_skip = m_target->width();
  171. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  172. const size_t bitmap_skip = bitmap.width();
  173. for (int row = first_row; row <= last_row; ++row) {
  174. for (int j = 0; j <= (last_column - first_column); ++j) {
  175. char fc = bitmap_row[j];
  176. if (fc == '#')
  177. dst[j] = color.value();
  178. }
  179. bitmap_row += bitmap_skip;
  180. dst += dst_skip;
  181. }
  182. }
  183. void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color)
  184. {
  185. Rect dst_rect { p, bitmap.size() };
  186. dst_rect.move_by(m_translation);
  187. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  188. if (clipped_rect.is_empty())
  189. return;
  190. const int first_row = clipped_rect.top() - dst_rect.top();
  191. const int last_row = clipped_rect.bottom() - dst_rect.top();
  192. const int first_column = clipped_rect.left() - dst_rect.left();
  193. const int last_column = clipped_rect.right() - dst_rect.left();
  194. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  195. const size_t dst_skip = m_target->width();
  196. for (int row = first_row; row <= last_row; ++row) {
  197. for (int j = 0; j <= (last_column - first_column); ++j) {
  198. if (bitmap.bit_at(j + first_column, row))
  199. dst[j] = color.value();
  200. }
  201. dst += dst_skip;
  202. }
  203. }
  204. void Painter::blit_with_alpha(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  205. {
  206. Rect dst_rect(position, src_rect.size());
  207. dst_rect.move_by(m_translation);
  208. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  209. if (clipped_rect.is_empty())
  210. return;
  211. const int first_row = clipped_rect.top() - dst_rect.top();
  212. const int last_row = clipped_rect.bottom() - dst_rect.top();
  213. const int first_column = clipped_rect.left() - dst_rect.left();
  214. const int last_column = clipped_rect.right() - dst_rect.left();
  215. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  216. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  217. const size_t dst_skip = m_target->width();
  218. const unsigned src_skip = source.width();
  219. for (int row = first_row; row <= last_row; ++row) {
  220. for (int x = 0; x <= (last_column - first_column); ++x) {
  221. byte alpha = Color(src[x]).alpha();
  222. if (alpha == 0xff)
  223. dst[x] = src[x];
  224. else if (!alpha)
  225. continue;
  226. else
  227. dst[x] = Color(dst[x]).blend(src[x]).value();
  228. }
  229. dst += dst_skip;
  230. src += src_skip;
  231. }
  232. }
  233. void Painter::blit(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  234. {
  235. Rect dst_rect(position, src_rect.size());
  236. dst_rect.move_by(m_translation);
  237. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  238. if (clipped_rect.is_empty())
  239. return;
  240. const int first_row = clipped_rect.top() - dst_rect.top();
  241. const int last_row = clipped_rect.bottom() - dst_rect.top();
  242. const int first_column = clipped_rect.left() - dst_rect.left();
  243. const int last_column = clipped_rect.right() - dst_rect.left();
  244. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  245. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  246. const size_t dst_skip = m_target->width();
  247. const unsigned src_skip = source.width();
  248. for (int row = first_row; row <= last_row; ++row) {
  249. fast_dword_copy(dst, src, dst_rect.width());
  250. dst += dst_skip;
  251. src += src_skip;
  252. }
  253. }
  254. FLATTEN void Painter::draw_glyph(const Point& point, char ch, Color color)
  255. {
  256. draw_bitmap(point, font().glyph_bitmap(ch), color);
  257. }
  258. void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
  259. {
  260. Point point;
  261. if (alignment == TextAlignment::TopLeft) {
  262. point = rect.location();
  263. } else if (alignment == TextAlignment::CenterLeft) {
  264. point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
  265. } else if (alignment == TextAlignment::CenterRight) {
  266. int text_width = text.length() * font().glyph_width();
  267. point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
  268. } else if (alignment == TextAlignment::Center) {
  269. int text_width = text.length() * font().glyph_width();
  270. point = rect.center();
  271. point.move_by(-(text_width / 2), -(font().glyph_height() / 2));
  272. } else {
  273. ASSERT_NOT_REACHED();
  274. }
  275. for (unsigned i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
  276. byte ch = text[i];
  277. if (ch == ' ')
  278. continue;
  279. draw_glyph(point, ch, color);
  280. }
  281. }
  282. void Painter::set_pixel(const Point& p, Color color)
  283. {
  284. auto point = p;
  285. point.move_by(m_translation);
  286. if (!m_clip_rect.contains(point))
  287. return;
  288. m_target->scanline(point.y())[point.x()] = color.value();
  289. }
  290. ALWAYS_INLINE void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
  291. {
  292. if (m_draw_op == DrawOp::Copy)
  293. pixel = color.value();
  294. else if (m_draw_op == DrawOp::Xor)
  295. pixel ^= color.value();
  296. }
  297. void Painter::draw_line(const Point& p1, const Point& p2, Color color)
  298. {
  299. auto point1 = p1;
  300. point1.move_by(m_translation);
  301. auto point2 = p2;
  302. point2.move_by(m_translation);
  303. // Special case: vertical line.
  304. if (point1.x() == point2.x()) {
  305. const int x = point1.x();
  306. if (x < m_clip_rect.left() || x > m_clip_rect.right())
  307. return;
  308. if (point1.y() > point2.y())
  309. swap(point1, point2);
  310. if (point1.y() > m_clip_rect.bottom())
  311. return;
  312. if (point2.y() < m_clip_rect.top())
  313. return;
  314. int min_y = max(point1.y(), m_clip_rect.top());
  315. int max_y = min(point2.y(), m_clip_rect.bottom());
  316. for (int y = min_y; y <= max_y; ++y)
  317. set_pixel_with_draw_op(m_target->scanline(y)[x], color);
  318. return;
  319. }
  320. if (point1.x() > point2.x())
  321. swap(point1, point2);
  322. // Special case: horizontal line.
  323. if (point1.y() == point2.y()) {
  324. const int y = point1.y();
  325. if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
  326. return;
  327. if (point1.x() > point2.x())
  328. swap(point1, point2);
  329. if (point1.x() > m_clip_rect.right())
  330. return;
  331. if (point2.x() < m_clip_rect.left())
  332. return;
  333. int min_x = max(point1.x(), m_clip_rect.left());
  334. int max_x = min(point2.x(), m_clip_rect.right());
  335. auto* pixels = m_target->scanline(point1.y());
  336. if (m_draw_op == DrawOp::Copy) {
  337. fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
  338. } else {
  339. for (int x = min_x; x <= max_x; ++x)
  340. set_pixel_with_draw_op(pixels[x], color);
  341. }
  342. return;
  343. }
  344. // FIXME: Implement clipping below.
  345. ASSERT_NOT_REACHED();
  346. #if 0
  347. const double dx = point2.x() - point1.x();
  348. const double dy = point2.y() - point1.y();
  349. const double delta_error = fabs(dy / dx);
  350. double error = 0;
  351. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  352. int y = point1.y();
  353. for (int x = point1.x(); x <= point2.x(); ++x) {
  354. m_target->scanline(y)[x] = color.value();
  355. error += delta_error;
  356. if (error >= 0.5) {
  357. y = (double)y + y_step;
  358. error -= 1.0;
  359. }
  360. }
  361. #endif
  362. }
  363. void Painter::draw_focus_rect(const Rect& rect)
  364. {
  365. Rect focus_rect = rect;
  366. focus_rect.move_by(1, 1);
  367. focus_rect.set_width(focus_rect.width() - 2);
  368. focus_rect.set_height(focus_rect.height() - 2);
  369. draw_rect(focus_rect, Color(96, 96, 192));
  370. }
  371. void Painter::set_clip_rect(const Rect& rect)
  372. {
  373. m_clip_rect = Rect::intersection(rect, m_target->rect());
  374. }
  375. void Painter::clear_clip_rect()
  376. {
  377. m_clip_rect = m_target->rect();
  378. }