Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/SharedBuffer.h>
28#include <AK/String.h>
29#include <LibGfx/Bitmap.h>
30#include <LibGfx/PNGLoader.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <sys/mman.h>
35#include <unistd.h>
36
37namespace Gfx {
38
39NonnullRefPtr<Bitmap> Bitmap::create(BitmapFormat format, const Size& size)
40{
41 return adopt(*new Bitmap(format, size, Purgeable::No));
42}
43
44NonnullRefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const Size& size)
45{
46 return adopt(*new Bitmap(format, size, Purgeable::Yes));
47}
48
49Bitmap::Bitmap(BitmapFormat format, const Size& size, Purgeable purgeable)
50 : m_size(size)
51 , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
52 , m_format(format)
53 , m_purgeable(purgeable == Purgeable::Yes)
54{
55 ASSERT(!m_size.is_empty());
56 if (format == BitmapFormat::Indexed8)
57 m_palette = new RGBA32[256];
58 int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
59 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());
60 ASSERT(m_data && m_data != (void*)-1);
61 m_needs_munmap = true;
62}
63
64NonnullRefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
65{
66 return adopt(*new Bitmap(format, size, pitch, data));
67}
68
69RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
70{
71 return load_png(path);
72}
73
74Bitmap::Bitmap(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
75 : m_size(size)
76 , m_data(data)
77 , m_pitch(pitch)
78 , m_format(format)
79{
80 if (format == BitmapFormat::Indexed8)
81 m_palette = new RGBA32[256];
82}
83
84NonnullRefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
85{
86 return adopt(*new Bitmap(format, move(shared_buffer), size));
87}
88
89Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
90 : m_size(size)
91 , m_data((RGBA32*)shared_buffer->data())
92 , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
93 , m_format(format)
94 , m_shared_buffer(move(shared_buffer))
95{
96 ASSERT(format != BitmapFormat::Indexed8);
97}
98
99NonnullRefPtr<Bitmap> Bitmap::to_shareable_bitmap() const
100{
101 if (m_shared_buffer)
102 return *this;
103 auto buffer = SharedBuffer::create_with_size(size_in_bytes());
104 auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
105 memcpy(buffer->data(), scanline(0), size_in_bytes());
106 return bitmap;
107}
108
109Bitmap::~Bitmap()
110{
111 if (m_needs_munmap) {
112 int rc = munmap(m_data, size_in_bytes());
113 ASSERT(rc == 0);
114 }
115 m_data = nullptr;
116 delete[] m_palette;
117}
118
119void Bitmap::set_mmap_name(const StringView& name)
120{
121 ASSERT(m_needs_munmap);
122 ::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
123}
124
125void Bitmap::fill(Color color)
126{
127 ASSERT(m_format == BitmapFormat::RGB32 || m_format == BitmapFormat::RGBA32);
128 for (int y = 0; y < height(); ++y) {
129 auto* scanline = this->scanline(y);
130 fast_u32_fill(scanline, color.value(), width());
131 }
132}
133
134void Bitmap::set_volatile()
135{
136 ASSERT(m_purgeable);
137 if (m_volatile)
138 return;
139 int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
140 if (rc < 0) {
141 perror("madvise(MADV_SET_VOLATILE)");
142 ASSERT_NOT_REACHED();
143 }
144 m_volatile = true;
145}
146
147[[nodiscard]] bool Bitmap::set_nonvolatile()
148{
149 ASSERT(m_purgeable);
150 if (!m_volatile)
151 return true;
152 int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
153 if (rc < 0) {
154 perror("madvise(MADV_SET_NONVOLATILE)");
155 ASSERT_NOT_REACHED();
156 }
157 m_volatile = false;
158 return rc == 0;
159}
160
161int Bitmap::shared_buffer_id() const
162{
163 return m_shared_buffer ? m_shared_buffer->shared_buffer_id() : -1;
164}
165
166}