Serenity Operating System
at hosted 236 lines 6.9 kB view raw
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#pragma once 28 29#include <AK/Forward.h> 30#include <AK/RefCounted.h> 31#include <AK/RefPtr.h> 32#include <LibGfx/Color.h> 33#include <LibGfx/Forward.h> 34#include <LibGfx/Rect.h> 35 36namespace Gfx { 37 38enum class BitmapFormat { 39 Invalid, 40 RGB32, 41 RGBA32, 42 Indexed8 43}; 44 45class Bitmap : public RefCounted<Bitmap> { 46public: 47 static NonnullRefPtr<Bitmap> create(BitmapFormat, const Size&); 48 static NonnullRefPtr<Bitmap> create_purgeable(BitmapFormat, const Size&); 49 static NonnullRefPtr<Bitmap> create_wrapper(BitmapFormat, const Size&, size_t pitch, RGBA32*); 50 static RefPtr<Bitmap> load_from_file(const StringView& path); 51 static NonnullRefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&); 52 53 NonnullRefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const; 54 55 ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const; 56 57 ~Bitmap(); 58 59 RGBA32* scanline(int y); 60 const RGBA32* scanline(int y) const; 61 62 u8* bits(int y); 63 const u8* bits(int y) const; 64 65 Rect rect() const { return { {}, m_size }; } 66 Size size() const { return m_size; } 67 int width() const { return m_size.width(); } 68 int height() const { return m_size.height(); } 69 size_t pitch() const { return m_pitch; } 70 int shbuf_id() const; 71 72 SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); } 73 const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); } 74 75 unsigned bpp() const 76 { 77 switch (m_format) { 78 case BitmapFormat::Indexed8: 79 return 8; 80 case BitmapFormat::RGB32: 81 case BitmapFormat::RGBA32: 82 return 32; 83 default: 84 ASSERT_NOT_REACHED(); 85 case BitmapFormat::Invalid: 86 return 0; 87 } 88 } 89 90 void fill(Color); 91 92 bool has_alpha_channel() const { return m_format == BitmapFormat::RGBA32; } 93 BitmapFormat format() const { return m_format; } 94 95 void set_mmap_name(const StringView&); 96 97 size_t size_in_bytes() const { return m_pitch * m_size.height(); } 98 99 Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); } 100 void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); } 101 102 template<BitmapFormat> 103 Color get_pixel(int x, int y) const 104 { 105 (void)x; 106 (void)y; 107 ASSERT_NOT_REACHED(); 108 } 109 110 Color get_pixel(int x, int y) const; 111 112 Color get_pixel(const Point& position) const 113 { 114 return get_pixel(position.x(), position.y()); 115 } 116 117 template<BitmapFormat> 118 void set_pixel(int x, int y, Color) 119 { 120 (void)x; 121 (void)y; 122 ASSERT_NOT_REACHED(); 123 } 124 125 void set_pixel(int x, int y, Color); 126 127 void set_pixel(const Point& position, Color color) 128 { 129 set_pixel(position.x(), position.y(), color); 130 } 131 132 bool is_purgeable() const { return m_purgeable; } 133 bool is_volatile() const { return m_volatile; } 134 void set_volatile(); 135 [[nodiscard]] bool set_nonvolatile(); 136 137private: 138 enum class Purgeable { No, 139 Yes }; 140 Bitmap(BitmapFormat, const Size&, Purgeable); 141 Bitmap(BitmapFormat, const Size&, size_t pitch, RGBA32*); 142 Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&); 143 144 Size m_size; 145 RGBA32* m_data { nullptr }; 146 RGBA32* m_palette { nullptr }; 147 size_t m_pitch { 0 }; 148 BitmapFormat m_format { BitmapFormat::Invalid }; 149 bool m_needs_munmap { false }; 150 bool m_purgeable { false }; 151 bool m_volatile { false }; 152 RefPtr<SharedBuffer> m_shared_buffer; 153}; 154 155inline RGBA32* Bitmap::scanline(int y) 156{ 157 return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch))); 158} 159 160inline const RGBA32* Bitmap::scanline(int y) const 161{ 162 return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch))); 163} 164 165inline const u8* Bitmap::bits(int y) const 166{ 167 return reinterpret_cast<const u8*>(scanline(y)); 168} 169 170inline u8* Bitmap::bits(int y) 171{ 172 return reinterpret_cast<u8*>(scanline(y)); 173} 174 175template<> 176inline Color Bitmap::get_pixel<BitmapFormat::RGB32>(int x, int y) const 177{ 178 return Color::from_rgb(scanline(y)[x]); 179} 180 181template<> 182inline Color Bitmap::get_pixel<BitmapFormat::RGBA32>(int x, int y) const 183{ 184 return Color::from_rgba(scanline(y)[x]); 185} 186 187template<> 188inline Color Bitmap::get_pixel<BitmapFormat::Indexed8>(int x, int y) const 189{ 190 return Color::from_rgba(m_palette[bits(y)[x]]); 191} 192 193inline Color Bitmap::get_pixel(int x, int y) const 194{ 195 switch (m_format) { 196 case BitmapFormat::RGB32: 197 return get_pixel<BitmapFormat::RGB32>(x, y); 198 case BitmapFormat::RGBA32: 199 return get_pixel<BitmapFormat::RGBA32>(x, y); 200 case BitmapFormat::Indexed8: 201 return get_pixel<BitmapFormat::Indexed8>(x, y); 202 default: 203 ASSERT_NOT_REACHED(); 204 return {}; 205 } 206} 207 208template<> 209inline void Bitmap::set_pixel<BitmapFormat::RGB32>(int x, int y, Color color) 210{ 211 scanline(y)[x] = color.value(); 212} 213 214template<> 215inline void Bitmap::set_pixel<BitmapFormat::RGBA32>(int x, int y, Color color) 216{ 217 scanline(y)[x] = color.value(); 218} 219 220inline void Bitmap::set_pixel(int x, int y, Color color) 221{ 222 switch (m_format) { 223 case BitmapFormat::RGB32: 224 set_pixel<BitmapFormat::RGB32>(x, y, color); 225 break; 226 case BitmapFormat::RGBA32: 227 set_pixel<BitmapFormat::RGBA32>(x, y, color); 228 break; 229 case BitmapFormat::Indexed8: 230 ASSERT_NOT_REACHED(); 231 default: 232 ASSERT_NOT_REACHED(); 233 } 234} 235 236}