2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-08 07:05:14 -04:00
|
|
|
#include <AK/Memory.h>
|
2020-02-14 17:02:47 -05:00
|
|
|
#include <AK/SharedBuffer.h>
|
2020-02-14 19:03:37 -05:00
|
|
|
#include <AK/String.h>
|
2020-02-06 06:07:05 -05:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 06:04:00 -05:00
|
|
|
#include <LibGfx/PNGLoader.h>
|
2020-03-29 13:04:05 -04:00
|
|
|
#include <LibGfx/ShareableBitmap.h>
|
2019-02-07 17:13:47 -05:00
|
|
|
#include <errno.h>
|
2019-06-07 05:46:55 -04:00
|
|
|
#include <fcntl.h>
|
2019-02-07 17:13:47 -05:00
|
|
|
#include <stdio.h>
|
2019-06-07 05:46:55 -04:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2019-02-07 17:13:47 -05:00
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
static bool size_would_overflow(BitmapFormat format, const Size& size)
|
2019-02-11 03:47:10 -05:00
|
|
|
{
|
2020-04-15 05:57:24 -04:00
|
|
|
if (size.width() < 0 || size.height() < 0)
|
|
|
|
return true;
|
|
|
|
size_t bpp = Bitmap::bpp_for_format(format);
|
|
|
|
size_t result = 0;
|
|
|
|
if (__builtin_mul_overflow((size_t)size.width(), (size_t)size.height(), &result))
|
|
|
|
return true;
|
|
|
|
if (__builtin_mul_overflow(result, bpp, &result))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const Size& size)
|
|
|
|
{
|
|
|
|
if (size_would_overflow(format, size))
|
|
|
|
return nullptr;
|
2020-02-06 05:56:38 -05:00
|
|
|
return adopt(*new Bitmap(format, size, Purgeable::No));
|
2019-02-11 03:47:10 -05:00
|
|
|
}
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const Size& size)
|
2019-12-18 14:50:05 -05:00
|
|
|
{
|
2020-04-15 05:57:24 -04:00
|
|
|
if (size_would_overflow(format, size))
|
|
|
|
return nullptr;
|
2020-02-06 05:56:38 -05:00
|
|
|
return adopt(*new Bitmap(format, size, Purgeable::Yes));
|
2019-12-18 14:50:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:02:47 -05:00
|
|
|
Bitmap::Bitmap(BitmapFormat format, const Size& size, Purgeable purgeable)
|
2019-02-11 03:47:10 -05:00
|
|
|
: m_size(size)
|
2019-05-06 08:04:54 -04:00
|
|
|
, m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
|
2019-02-18 19:42:53 -05:00
|
|
|
, m_format(format)
|
2019-12-18 14:50:05 -05:00
|
|
|
, m_purgeable(purgeable == Purgeable::Yes)
|
2019-02-11 03:47:10 -05:00
|
|
|
{
|
2020-02-11 03:18:40 -05:00
|
|
|
ASSERT(!m_size.is_empty());
|
2020-04-15 05:57:24 -04:00
|
|
|
ASSERT(!size_would_overflow(format, size));
|
2020-02-14 17:02:47 -05:00
|
|
|
if (format == BitmapFormat::Indexed8)
|
2019-05-06 13:32:56 -04:00
|
|
|
m_palette = new RGBA32[256];
|
2019-12-18 14:50:05 -05:00
|
|
|
int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
|
|
|
|
m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
|
2019-02-16 06:22:00 -05:00
|
|
|
ASSERT(m_data && m_data != (void*)-1);
|
2019-04-26 12:25:05 -04:00
|
|
|
m_needs_munmap = true;
|
2019-02-16 06:22:00 -05:00
|
|
|
}
|
2019-01-14 14:00:42 -05:00
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
|
2019-01-14 14:00:42 -05:00
|
|
|
{
|
2020-04-15 05:57:24 -04:00
|
|
|
if (size_would_overflow(format, size))
|
|
|
|
return nullptr;
|
2020-02-06 05:56:38 -05:00
|
|
|
return adopt(*new Bitmap(format, size, pitch, data));
|
2019-01-14 14:00:42 -05:00
|
|
|
}
|
2019-01-08 21:51:34 -05:00
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
|
2019-03-21 19:19:53 -04:00
|
|
|
{
|
|
|
|
return load_png(path);
|
|
|
|
}
|
|
|
|
|
2020-02-14 17:02:47 -05:00
|
|
|
Bitmap::Bitmap(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
|
2019-01-08 21:51:34 -05:00
|
|
|
: m_size(size)
|
2019-01-09 23:36:32 -05:00
|
|
|
, m_data(data)
|
2019-08-19 07:29:19 -04:00
|
|
|
, m_pitch(pitch)
|
2019-02-18 19:42:53 -05:00
|
|
|
, m_format(format)
|
2019-01-08 21:51:34 -05:00
|
|
|
{
|
2020-04-15 05:57:24 -04:00
|
|
|
ASSERT(!size_would_overflow(format, size));
|
2020-02-14 17:02:47 -05:00
|
|
|
if (format == BitmapFormat::Indexed8)
|
2019-09-14 05:19:49 -04:00
|
|
|
m_palette = new RGBA32[256];
|
2019-01-08 20:06:04 -05:00
|
|
|
}
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
|
2019-02-16 06:13:43 -05:00
|
|
|
{
|
2020-04-15 05:57:24 -04:00
|
|
|
if (size_would_overflow(format, size))
|
|
|
|
return nullptr;
|
2020-02-06 05:56:38 -05:00
|
|
|
return adopt(*new Bitmap(format, move(shared_buffer), size));
|
2019-02-16 06:13:43 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:02:47 -05:00
|
|
|
Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
|
2019-02-16 06:13:43 -05:00
|
|
|
: m_size(size)
|
2019-03-08 06:22:55 -05:00
|
|
|
, m_data((RGBA32*)shared_buffer->data())
|
2019-05-06 08:04:54 -04:00
|
|
|
, m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
|
2019-02-18 19:42:53 -05:00
|
|
|
, m_format(format)
|
2019-03-08 06:22:55 -05:00
|
|
|
, m_shared_buffer(move(shared_buffer))
|
2019-02-16 06:13:43 -05:00
|
|
|
{
|
2020-02-14 17:02:47 -05:00
|
|
|
ASSERT(format != BitmapFormat::Indexed8);
|
2020-04-15 05:57:24 -04:00
|
|
|
ASSERT(!size_would_overflow(format, size));
|
2019-02-16 06:13:43 -05:00
|
|
|
}
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
|
2020-04-12 06:19:18 -04:00
|
|
|
{
|
|
|
|
auto w = this->width();
|
|
|
|
auto h = this->height();
|
|
|
|
|
|
|
|
auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
|
2020-04-15 05:57:24 -04:00
|
|
|
if (!new_bitmap)
|
|
|
|
return nullptr;
|
2020-04-12 06:19:18 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < w; i++) {
|
|
|
|
for (int j = 0; j < h; j++) {
|
|
|
|
Color color;
|
|
|
|
if (rotation_direction == Gfx::RotationDirection::Left)
|
|
|
|
color = this->get_pixel(w - i - 1, j);
|
|
|
|
else
|
|
|
|
color = this->get_pixel(i, h - j - 1);
|
|
|
|
|
|
|
|
new_bitmap->set_pixel(j, i, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_bitmap;
|
|
|
|
}
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
|
2020-04-12 06:19:18 -04:00
|
|
|
{
|
|
|
|
auto w = this->width();
|
|
|
|
auto h = this->height();
|
|
|
|
|
|
|
|
auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
|
2020-04-15 05:57:24 -04:00
|
|
|
if (!new_bitmap)
|
|
|
|
return nullptr;
|
2020-04-12 06:19:18 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < w; i++) {
|
|
|
|
for (int j = 0; j < h; j++) {
|
|
|
|
Color color = this->get_pixel(i, j);
|
|
|
|
if (orientation == Orientation::Vertical)
|
|
|
|
new_bitmap->set_pixel(i, h - j - 1, color);
|
|
|
|
else
|
|
|
|
new_bitmap->set_pixel(w - i - 1, j, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_bitmap;
|
|
|
|
}
|
|
|
|
|
2020-04-15 05:57:24 -04:00
|
|
|
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
|
2019-12-08 11:07:44 -05:00
|
|
|
{
|
|
|
|
if (m_shared_buffer)
|
|
|
|
return *this;
|
|
|
|
auto buffer = SharedBuffer::create_with_size(size_in_bytes());
|
2020-02-06 05:56:38 -05:00
|
|
|
auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
|
2020-04-15 05:57:24 -04:00
|
|
|
if (!bitmap)
|
|
|
|
return nullptr;
|
2019-12-08 11:07:44 -05:00
|
|
|
memcpy(buffer->data(), scanline(0), size_in_bytes());
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
Bitmap::~Bitmap()
|
2019-01-08 20:06:04 -05:00
|
|
|
{
|
2019-04-26 12:25:05 -04:00
|
|
|
if (m_needs_munmap) {
|
2019-05-06 08:04:54 -04:00
|
|
|
int rc = munmap(m_data, size_in_bytes());
|
2019-04-26 12:25:05 -04:00
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-08 21:51:34 -05:00
|
|
|
m_data = nullptr;
|
2019-06-07 05:46:55 -04:00
|
|
|
delete[] m_palette;
|
2019-01-08 20:06:04 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
void Bitmap::set_mmap_name(const StringView& name)
|
2019-04-30 07:46:03 -04:00
|
|
|
{
|
|
|
|
ASSERT(m_needs_munmap);
|
2019-07-08 09:38:44 -04:00
|
|
|
::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
|
2019-04-30 07:46:03 -04:00
|
|
|
}
|
2019-06-10 13:29:50 -04:00
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
void Bitmap::fill(Color color)
|
2019-06-10 13:29:50 -04:00
|
|
|
{
|
2020-02-14 17:02:47 -05:00
|
|
|
ASSERT(m_format == BitmapFormat::RGB32 || m_format == BitmapFormat::RGBA32);
|
2019-06-10 13:29:50 -04:00
|
|
|
for (int y = 0; y < height(); ++y) {
|
|
|
|
auto* scanline = this->scanline(y);
|
2019-07-03 15:17:35 -04:00
|
|
|
fast_u32_fill(scanline, color.value(), width());
|
2019-06-10 13:29:50 -04:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 14:50:05 -05:00
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
void Bitmap::set_volatile()
|
2019-12-18 14:50:05 -05:00
|
|
|
{
|
|
|
|
ASSERT(m_purgeable);
|
|
|
|
if (m_volatile)
|
|
|
|
return;
|
|
|
|
int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("madvise(MADV_SET_VOLATILE)");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
m_volatile = true;
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
[[nodiscard]] bool Bitmap::set_nonvolatile()
|
2019-12-18 14:50:05 -05:00
|
|
|
{
|
|
|
|
ASSERT(m_purgeable);
|
|
|
|
if (!m_volatile)
|
|
|
|
return true;
|
|
|
|
int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("madvise(MADV_SET_NONVOLATILE)");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
m_volatile = false;
|
|
|
|
return rc == 0;
|
|
|
|
}
|
2020-02-06 05:56:38 -05:00
|
|
|
|
2020-02-28 05:45:19 -05:00
|
|
|
int Bitmap::shbuf_id() const
|
2020-02-14 17:02:47 -05:00
|
|
|
{
|
2020-02-28 05:45:19 -05:00
|
|
|
return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
|
2020-02-14 17:02:47 -05:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:04:05 -04:00
|
|
|
ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
|
|
|
|
{
|
|
|
|
auto bitmap = to_bitmap_backed_by_shared_buffer();
|
2020-04-15 05:57:24 -04:00
|
|
|
if (!bitmap)
|
|
|
|
return {};
|
2020-03-29 13:04:05 -04:00
|
|
|
if (peer_pid > 0)
|
|
|
|
bitmap->shared_buffer()->share_with(peer_pid);
|
|
|
|
return ShareableBitmap(*bitmap);
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:56:38 -05:00
|
|
|
}
|