diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-05-06 14:04:54 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-06 14:04:54 +0200 |
commit | cec16105cc5bbbb7743b769e65377fc91fb918b1 (patch) | |
tree | 5cc421a9003acfac6cc8c07ccb6877e2e157b4c8 | |
parent | fafdda8902e25e39c0e8484252a8e17271ed4538 (diff) | |
download | serenity-cec16105cc5bbbb7743b769e65377fc91fb918b1.zip |
Make sure all GraphicsBitmap scanlines are 16-byte aligned.
This is a prerequisite for some optimizations.
-rw-r--r-- | AK/StdLibExtras.h | 5 | ||||
-rw-r--r-- | LibGUI/GWindow.cpp | 3 | ||||
-rw-r--r-- | SharedGraphics/GraphicsBitmap.cpp | 17 | ||||
-rw-r--r-- | SharedGraphics/GraphicsBitmap.h | 2 | ||||
-rw-r--r-- | SharedGraphics/Painter.cpp | 26 |
5 files changed, 29 insertions, 24 deletions
diff --git a/AK/StdLibExtras.h b/AK/StdLibExtras.h index 6a282b98ab..d090ad861d 100644 --- a/AK/StdLibExtras.h +++ b/AK/StdLibExtras.h @@ -41,6 +41,11 @@ extern "C" void* mmx_memcpy(void* to, const void* from, size_t); ); } +inline constexpr dword round_up_to_power_of_two(dword value, dword power_of_two) +{ + return ((value - 1) & ~ (power_of_two - 1)) + power_of_two; +} + namespace AK { template<typename T> diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 1dff4f2f74..3d301952f2 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -473,7 +473,8 @@ Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size) { ASSERT(GEventLoop::server_pid()); ASSERT(!size.is_empty()); - size_t size_in_bytes = size.area() * sizeof(RGBA32); + size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16); + size_t size_in_bytes = size.height() * pitch; auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes); ASSERT(shared_buffer); auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32; diff --git a/SharedGraphics/GraphicsBitmap.cpp b/SharedGraphics/GraphicsBitmap.cpp index 160d39762f..d6c9feaf6d 100644 --- a/SharedGraphics/GraphicsBitmap.cpp +++ b/SharedGraphics/GraphicsBitmap.cpp @@ -14,11 +14,10 @@ Retained<GraphicsBitmap> GraphicsBitmap::create(Format format, const Size& size) GraphicsBitmap::GraphicsBitmap(Format format, const Size& size) : m_size(size) - , m_pitch(size.width() * sizeof(RGBA32)) + , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16)) , m_format(format) { - size_t size_in_bytes = size.area() * sizeof(RGBA32); - m_data = (RGBA32*)mmap(nullptr, size_in_bytes, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); + m_data = (RGBA32*)mmap(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); ASSERT(m_data && m_data != (void*)-1); m_needs_munmap = true; set_mmap_name(String::format("GraphicsBitmap [%dx%d]", width(), height()).characters()); @@ -45,7 +44,7 @@ RetainPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(Format format, const St GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, RGBA32* data) : m_size(size) , m_data(data) - , m_pitch(size.width() * sizeof(RGBA32)) + , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16)) , m_format(format) { } @@ -53,7 +52,7 @@ GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, RGBA32* data) GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, MappedFile&& mapped_file) : m_size(size) , m_data((RGBA32*)mapped_file.pointer()) - , m_pitch(size.width() * sizeof(RGBA32)) + , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16)) , m_format(format) , m_mapped_file(move(mapped_file)) { @@ -67,7 +66,7 @@ Retained<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(Format format GraphicsBitmap::GraphicsBitmap(Format format, Retained<SharedBuffer>&& shared_buffer, const Size& size) : m_size(size) , m_data((RGBA32*)shared_buffer->data()) - , m_pitch(size.width() * sizeof(RGBA32)) + , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16)) , m_format(format) , m_shared_buffer(move(shared_buffer)) { @@ -76,8 +75,7 @@ GraphicsBitmap::GraphicsBitmap(Format format, Retained<SharedBuffer>&& shared_bu GraphicsBitmap::~GraphicsBitmap() { if (m_needs_munmap) { - size_t size_in_bytes = m_size.area() * sizeof(RGBA32); - int rc = munmap(m_data, size_in_bytes); + int rc = munmap(m_data, size_in_bytes()); ASSERT(rc == 0); } m_data = nullptr; @@ -86,6 +84,5 @@ GraphicsBitmap::~GraphicsBitmap() void GraphicsBitmap::set_mmap_name(const String& name) { ASSERT(m_needs_munmap); - size_t size_in_bytes = m_size.area() * sizeof(RGBA32); - ::set_mmap_name(m_data, size_in_bytes, name.characters()); + ::set_mmap_name(m_data, size_in_bytes(), name.characters()); } diff --git a/SharedGraphics/GraphicsBitmap.h b/SharedGraphics/GraphicsBitmap.h index ba7f3fd667..de8630bdf9 100644 --- a/SharedGraphics/GraphicsBitmap.h +++ b/SharedGraphics/GraphicsBitmap.h @@ -35,6 +35,8 @@ public: void set_mmap_name(const String&); + size_t size_in_bytes() const { return m_pitch * m_size.height() * sizeof(RGBA32); } + private: GraphicsBitmap(Format, const Size&); GraphicsBitmap(Format, const Size&, RGBA32*); diff --git a/SharedGraphics/Painter.cpp b/SharedGraphics/Painter.cpp index 356a6585a4..32fe9ab1a1 100644 --- a/SharedGraphics/Painter.cpp +++ b/SharedGraphics/Painter.cpp @@ -29,7 +29,7 @@ void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color) return; RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); - const unsigned dst_skip = m_target->width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); for (int i = rect.height() - 1; i >= 0; --i) { for (int j = 0; j < rect.width(); ++j) @@ -52,7 +52,7 @@ void Painter::fill_rect(const Rect& a_rect, Color color) ASSERT(m_target->rect().contains(rect)); RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); - const unsigned dst_skip = m_target->width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); for (int i = rect.height() - 1; i >= 0; --i) { fast_dword_fill(dst, color.value(), rect.width()); @@ -73,7 +73,7 @@ void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, int x_offset = clipped_rect.x() - rect.x(); RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left(); - const unsigned dst_skip = m_target->width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); float increment = (1.0/((rect.width())/255.0)); @@ -153,7 +153,7 @@ void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color c const int first_column = clipped_rect.left() - rect.left(); const int last_column = clipped_rect.right() - rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); - const size_t dst_skip = m_target->width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column]; const size_t bitmap_skip = bitmap.width(); @@ -179,7 +179,7 @@ void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color const int first_column = clipped_rect.left() - dst_rect.left(); const int last_column = clipped_rect.right() - dst_rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); - const size_t dst_skip = m_target->width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); for (int row = first_row; row <= last_row; ++row) { for (int j = 0; j <= (last_column - first_column); ++j) { @@ -213,8 +213,8 @@ void Painter::blit_with_opacity(const Point& position, const GraphicsBitmap& sou const int last_column = clipped_rect.right() - dst_rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; - const size_t dst_skip = m_target->width(); - const unsigned src_skip = source.width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); + const unsigned src_skip = source.pitch() / sizeof(RGBA32); for (int row = first_row; row <= last_row; ++row) { for (int x = 0; x <= (last_column - first_column); ++x) { @@ -241,8 +241,8 @@ void Painter::blit_dimmed(const Point& position, const GraphicsBitmap& source, c const int last_column = clipped_rect.right() - dst_rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; - const size_t dst_skip = m_target->width(); - const unsigned src_skip = source.width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); + const size_t src_skip = source.pitch() / sizeof(RGBA32); for (int row = first_row; row <= last_row; ++row) { for (int x = 0; x <= (last_column - first_column); ++x) { @@ -274,8 +274,8 @@ void Painter::blit_with_alpha(const Point& position, const GraphicsBitmap& sourc const int last_column = clipped_rect.right() - dst_rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; - const size_t dst_skip = m_target->width(); - const unsigned src_skip = source.width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); + const size_t src_skip = source.pitch() / sizeof(RGBA32); for (int row = first_row; row <= last_row; ++row) { for (int x = 0; x <= (last_column - first_column); ++x) { @@ -309,8 +309,8 @@ void Painter::blit(const Point& position, const GraphicsBitmap& source, const Re const int first_column = clipped_rect.left() - dst_rect.left(); RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; - const size_t dst_skip = m_target->width(); - const unsigned src_skip = source.width(); + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); + const size_t src_skip = source.pitch() / sizeof(RGBA32); for (int row = first_row; row <= last_row; ++row) { fast_dword_copy(dst, src, clipped_rect.width()); |