From 267a903dd006220e83fd1a09dbc85ec0895ba16c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 25 Jan 2019 02:09:29 +0100 Subject: [PATCH] Terminal: Redraw entire line if any of its characters are dirty. This means we only have to do one fill_rect() per line and the whole process ends up being ~10% faster than before. Also added a read_tsc() syscall to give userspace access to the TSC. --- Kernel/Process.cpp | 10 ++++ Kernel/Process.h | 1 + Kernel/Syscall.cpp | 2 + Kernel/Syscall.h | 1 + LibC/unistd.cpp | 6 +++ LibC/unistd.h | 1 + SharedGraphics/Color.h | 5 ++ Terminal/Terminal.cpp | 109 +++++++++++++++++++++++++++++------------ Terminal/Terminal.h | 8 +-- 9 files changed, 108 insertions(+), 35 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 69801b9896..2582637591 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2081,3 +2081,13 @@ int Process::sys$unlink(const char* pathname) return error; return 0; } + +int Process::sys$read_tsc(dword* lsw, dword* msw) +{ + if (!validate_write_typed(lsw)) + return -EFAULT; + if (!validate_write_typed(msw)) + return -EFAULT; + read_tsc(*lsw, *msw); + return 0; +} diff --git a/Kernel/Process.h b/Kernel/Process.h index 39f9f681f7..87321a6660 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -194,6 +194,7 @@ public: clock_t sys$times(tms*); int sys$utime(const char* pathname, const struct utimbuf*); int sys$unlink(const char* pathname); + int sys$read_tsc(dword* lsw, dword* msw); int gui$create_window(const GUI_WindowParameters*); int gui$destroy_window(int window_id); diff --git a/Kernel/Syscall.cpp b/Kernel/Syscall.cpp index d8c73308e4..122a559b20 100644 --- a/Kernel/Syscall.cpp +++ b/Kernel/Syscall.cpp @@ -213,6 +213,8 @@ static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2, return current->gui$set_window_rect((int)arg1, (const GUI_Rect*)arg2); case Syscall::SC_gui_get_window_rect: return current->gui$get_window_rect((int)arg1, (GUI_Rect*)arg2); + case Syscall::SC_read_tsc: + return current->sys$read_tsc((dword*)arg1, (dword*)arg2); default: kprintf("<%u> int0x80: Unknown function %u requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3); break; diff --git a/Kernel/Syscall.h b/Kernel/Syscall.h index 663ff3b7cd..4bcd15d0e8 100644 --- a/Kernel/Syscall.h +++ b/Kernel/Syscall.h @@ -70,6 +70,7 @@ __ENUMERATE_SYSCALL(select) \ __ENUMERATE_SYSCALL(unlink) \ __ENUMERATE_SYSCALL(poll) \ + __ENUMERATE_SYSCALL(read_tsc) \ __ENUMERATE_SYSCALL(gui_create_window) \ __ENUMERATE_SYSCALL(gui_destroy_window) \ __ENUMERATE_SYSCALL(gui_get_window_backing_store) \ diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp index 3ed19e5fe5..afccf18fb8 100644 --- a/LibC/unistd.cpp +++ b/LibC/unistd.cpp @@ -306,4 +306,10 @@ void sync() syscall(SC_sync); } +int read_tsc(unsigned* lsw, unsigned* msw) +{ + int rc = syscall(SC_read_tsc, lsw, msw); + __RETURN_WITH_ERRNO(rc, rc, -1); +} + } diff --git a/LibC/unistd.h b/LibC/unistd.h index d4875a287b..28b308567e 100644 --- a/LibC/unistd.h +++ b/LibC/unistd.h @@ -8,6 +8,7 @@ __BEGIN_DECLS extern char** environ; +int read_tsc(unsigned* lsw, unsigned* msw); inline int getpagesize() { return 4096; } pid_t fork(); int execve(const char* filename, char* const argv[], char* const envp[]); diff --git a/SharedGraphics/Color.h b/SharedGraphics/Color.h index bf302e65bc..11758c6f2c 100644 --- a/SharedGraphics/Color.h +++ b/SharedGraphics/Color.h @@ -4,6 +4,11 @@ typedef dword RGBA32; +inline constexpr dword make_rgb(byte r, byte g, byte b) +{ + return ((r << 16) | (g << 8) | b); +} + class Color { public: enum NamedColor { diff --git a/Terminal/Terminal.cpp b/Terminal/Terminal.cpp index 45d434b944..ae0e68c5a9 100644 --- a/Terminal/Terminal.cpp +++ b/Terminal/Terminal.cpp @@ -10,6 +10,33 @@ //#define TERMINAL_DEBUG +struct Stopwatch { +public: + Stopwatch(const char* name) + : m_name(name) + { + read_tsc(&m_start_lsw, &m_start_msw); + } + + ~Stopwatch() + { + dword end_lsw; + dword end_msw; + read_tsc(&end_lsw, &end_msw); + if (m_start_msw != end_msw) { + dbgprintf("stopwatch: differing msw, no result for %s\n", m_name); + } + dword diff = end_lsw - m_start_lsw; + dbgprintf("Stopwatch(%s): %u ticks\n", m_name, diff); + } + +private: + const char* m_name { nullptr }; + dword m_start_lsw { 0 }; + dword m_start_msw { 0 }; +}; + + void Terminal::create_window() { m_pixel_width = m_columns * font().glyph_width() + m_inset * 2; @@ -73,6 +100,7 @@ Terminal::Line::~Line() void Terminal::Line::clear() { + dirty = true; memset(characters, ' ', length); for (word i = 0 ; i < length; ++i) attributes[i].reset(); @@ -144,26 +172,25 @@ enum ANSIColor : byte { static inline Color ansi_color(unsigned color) { - switch (color) { - case ANSIColor::Black: return Color(0, 0, 0); - case ANSIColor::Red: return Color(225, 56, 43); - case ANSIColor::Green: return Color(57, 181, 74); - case ANSIColor::Brown: return Color(255, 199, 6); - case ANSIColor::Blue: return Color(0, 111, 184); - case ANSIColor::Magenta: return Color(118, 38, 113); - case ANSIColor::Cyan: return Color(44, 181, 233); - case ANSIColor::LightGray: return Color(204, 204, 204); - case ANSIColor::DarkGray: return Color(128, 128, 128); - case ANSIColor::BrightRed: return Color(255, 0, 0); - case ANSIColor::BrightGreen: return Color(0, 255, 0); - case ANSIColor::Yellow: return Color(255, 255, 0); - case ANSIColor::BrightBlue: return Color(0, 0, 255); - case ANSIColor::BrightMagenta: return Color(255, 0, 255); - case ANSIColor::BrightCyan: return Color(0, 255, 255); - case ANSIColor::White: return Color(255, 255, 255); - } - ASSERT_NOT_REACHED(); - return Color::White; + static const RGBA32 s_ansi_color[16] = { + make_rgb(0, 0, 0), // Black + make_rgb(225, 56, 43), // Red + make_rgb(57, 181, 74), // Green + make_rgb(255, 199, 6), // Brown + make_rgb(0, 11, 184), // Blue + make_rgb(118, 38, 113), // Magenta + make_rgb(44, 181, 233), // Cyan + make_rgb(204, 204, 204), // LightGray + make_rgb(128, 128, 128), // DarkGray + make_rgb(255, 0, 0), // BrightRed + make_rgb(0, 255, 0), // BrightGreen + make_rgb(255, 255, 0), // Yellow + make_rgb(0, 0, 255), // BrightBlue + make_rgb(255, 0, 255), // BrightMagenta + make_rgb(0, 255, 255), // BrightCyan + make_rgb(255, 255, 255), // White + }; + return s_ansi_color[color]; } void Terminal::escape$m(const Vector& params) @@ -176,7 +203,7 @@ void Terminal::escape$m(const Vector& params) break; case 1: // Bold - m_current_attribute.bold = true; + //m_current_attribute.bold = true; break; case 30: case 31: @@ -516,8 +543,22 @@ inline Terminal::Attribute& Terminal::attribute_at(word row, word column) return line(row).attributes[column]; } +bool Terminal::Line::has_only_one_background_color() const +{ + if (!length) + return true; + // FIXME: Cache this result? + auto color = attributes[0].background_color; + for (size_t i = 1; i < length; ++i) { + if (attributes[i].background_color != color) + return false; + } + return true; +} + void Terminal::paint() { + Stopwatch sw("Terminal::paint"); Rect rect { 0, 0, m_pixel_width, m_pixel_height }; Painter painter(*m_backing); @@ -535,21 +576,27 @@ void Terminal::paint() scanlines_to_copy * m_pixel_width ); m_need_full_invalidation = true; - attribute_at(max(0, m_cursor_row - m_rows_to_scroll_backing_store), m_cursor_column).dirty = true; + line(max(0, m_cursor_row - m_rows_to_scroll_backing_store)).dirty = true; } m_rows_to_scroll_backing_store = 0; for (word row = 0; row < m_rows; ++row) { + auto& line = this->line(row); + if (!line.dirty) + continue; + line.dirty = false; + bool has_only_one_background_color = line.has_only_one_background_color(); + if (has_only_one_background_color) + painter.fill_rect(row_rect(row), line.attributes[0].background_color); for (word column = 0; column < m_columns; ++column) { - auto& attribute = attribute_at(row, column); - if (!attribute.dirty) - continue; - attribute.dirty = false; - line(row).needs_invalidation = true; - char ch = line(row).characters[column]; + auto& attribute = line.attributes[column]; + line.needs_invalidation = true; + char ch = line.characters[column]; auto character_rect = glyph_rect(row, column); - auto character_background = ansi_color(attribute.background_color); - painter.fill_rect(character_rect, character_background); + if (!has_only_one_background_color) { + auto character_background = ansi_color(attribute.background_color); + painter.fill_rect(character_rect, character_background); + } if (ch == ' ') continue; painter.draw_glyph(character_rect.location(), ch, ansi_color(attribute.foreground_color)); @@ -604,5 +651,5 @@ void Terminal::set_in_active_window(bool b) void Terminal::invalidate_cursor() { - attribute_at(m_cursor_row, m_cursor_column).dirty = true; + line(m_cursor_row).dirty = true; } diff --git a/Terminal/Terminal.h b/Terminal/Terminal.h index d44bcb3e0c..151a610ae3 100644 --- a/Terminal/Terminal.h +++ b/Terminal/Terminal.h @@ -52,22 +52,22 @@ private: { foreground_color = 7; background_color = 0; - bold = false; - dirty = true; + //bold = false; } unsigned foreground_color : 4; unsigned background_color : 4; - bool bold : 1; - bool dirty : 1; + //bool bold : 1; }; struct Line { explicit Line(word columns); ~Line(); void clear(); + bool has_only_one_background_color() const; byte* characters { nullptr }; Attribute* attributes { nullptr }; bool needs_invalidation { false }; + bool dirty { false }; word length { 0 }; }; Line& line(size_t index) { ASSERT(index < m_rows); return *m_lines[index]; }