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/Memory.h>
28#include <AK/SharedBuffer.h>
29#include <AK/String.h>
30#include <LibGfx/Bitmap.h>
31#include <LibGfx/PNGLoader.h>
32#include <LibGfx/ShareableBitmap.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <sys/mman.h>
37#include <unistd.h>
38
39namespace Gfx {
40
41NonnullRefPtr<Bitmap> Bitmap::create(BitmapFormat format, const Size& size)
42{
43 return adopt(*new Bitmap(format, size, Purgeable::No));
44}
45
46NonnullRefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const Size& size)
47{
48 return adopt(*new Bitmap(format, size, Purgeable::Yes));
49}
50
51Bitmap::Bitmap(BitmapFormat format, const Size& size, Purgeable purgeable)
52 : m_size(size)
53 , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
54 , m_format(format)
55 , m_purgeable(purgeable == Purgeable::Yes)
56{
57 ASSERT(!m_size.is_empty());
58 if (format == BitmapFormat::Indexed8)
59 m_palette = new RGBA32[256];
60 int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
61 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());
62 ASSERT(m_data && m_data != (void*)-1);
63 m_needs_munmap = true;
64}
65
66NonnullRefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
67{
68 return adopt(*new Bitmap(format, size, pitch, data));
69}
70
71RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
72{
73 return load_png(path);
74}
75
76Bitmap::Bitmap(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
77 : m_size(size)
78 , m_data(data)
79 , m_pitch(pitch)
80 , m_format(format)
81{
82 if (format == BitmapFormat::Indexed8)
83 m_palette = new RGBA32[256];
84}
85
86NonnullRefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
87{
88 return adopt(*new Bitmap(format, move(shared_buffer), size));
89}
90
91Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
92 : m_size(size)
93 , m_data((RGBA32*)shared_buffer->data())
94 , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
95 , m_format(format)
96 , m_shared_buffer(move(shared_buffer))
97{
98 ASSERT(format != BitmapFormat::Indexed8);
99}
100
101NonnullRefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
102{
103 if (m_shared_buffer)
104 return *this;
105 auto buffer = SharedBuffer::create_with_size(size_in_bytes());
106 auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
107 memcpy(buffer->data(), scanline(0), size_in_bytes());
108 return bitmap;
109}
110
111Bitmap::~Bitmap()
112{
113 if (m_needs_munmap) {
114 int rc = munmap(m_data, size_in_bytes());
115 ASSERT(rc == 0);
116 }
117 m_data = nullptr;
118 delete[] m_palette;
119}
120
121void Bitmap::set_mmap_name(const StringView& name)
122{
123 ASSERT(m_needs_munmap);
124 ::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
125}
126
127void Bitmap::fill(Color color)
128{
129 ASSERT(m_format == BitmapFormat::RGB32 || m_format == BitmapFormat::RGBA32);
130 for (int y = 0; y < height(); ++y) {
131 auto* scanline = this->scanline(y);
132 fast_u32_fill(scanline, color.value(), width());
133 }
134}
135
136void Bitmap::set_volatile()
137{
138 ASSERT(m_purgeable);
139 if (m_volatile)
140 return;
141 int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
142 if (rc < 0) {
143 perror("madvise(MADV_SET_VOLATILE)");
144 ASSERT_NOT_REACHED();
145 }
146 m_volatile = true;
147}
148
149[[nodiscard]] bool Bitmap::set_nonvolatile()
150{
151 ASSERT(m_purgeable);
152 if (!m_volatile)
153 return true;
154 int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
155 if (rc < 0) {
156 perror("madvise(MADV_SET_NONVOLATILE)");
157 ASSERT_NOT_REACHED();
158 }
159 m_volatile = false;
160 return rc == 0;
161}
162
163int Bitmap::shbuf_id() const
164{
165 return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
166}
167
168ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
169{
170 auto bitmap = to_bitmap_backed_by_shared_buffer();
171 if (peer_pid > 0)
172 bitmap->shared_buffer()->share_with(peer_pid);
173 return ShareableBitmap(*bitmap);
174}
175
176}