Selaa lähdekoodia

LibGfx: Add release callback for Bitmap

In upcoming changes, bitmap is going to be used to wrap the memory of
the IOSurface, and we will want to release the corresponding IOSurface
along with the bitmap.
Aliaksandr Kalenik 1 vuosi sitten
vanhempi
commit
3110411c60
2 muutettua tiedostoa jossa 12 lisäystä ja 10 poistoa
  1. 9 7
      Userland/Libraries/LibGfx/Bitmap.cpp
  2. 3 3
      Userland/Libraries/LibGfx/Bitmap.h

+ 9 - 7
Userland/Libraries/LibGfx/Bitmap.cpp

@@ -84,14 +84,16 @@ Bitmap::Bitmap(BitmapFormat format, IntSize size, BackingStore const& backing_st
     VERIFY(!size_would_overflow(format, size));
     VERIFY(!size_would_overflow(format, size));
     VERIFY(m_data);
     VERIFY(m_data);
     VERIFY(backing_store.size_in_bytes == size_in_bytes());
     VERIFY(backing_store.size_in_bytes == size_in_bytes());
-    m_data_is_malloced = true;
+    m_destruction_callback = [data = m_data, size_in_bytes = this->size_in_bytes()] {
+        kfree_sized(data, size_in_bytes);
+    };
 }
 }
 
 
-ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_wrapper(BitmapFormat format, IntSize size, size_t pitch, void* data)
+ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_wrapper(BitmapFormat format, IntSize size, size_t pitch, void* data, Function<void()>&& destruction_callback)
 {
 {
     if (size_would_overflow(format, size))
     if (size_would_overflow(format, size))
         return Error::from_string_literal("Gfx::Bitmap::create_wrapper size overflow");
         return Error::from_string_literal("Gfx::Bitmap::create_wrapper size overflow");
-    return adopt_ref(*new Bitmap(format, size, pitch, data));
+    return adopt_ref(*new Bitmap(format, size, pitch, data, move(destruction_callback)));
 }
 }
 
 
 ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, Optional<IntSize> ideal_size)
 ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, Optional<IntSize> ideal_size)
@@ -118,11 +120,12 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_bytes(ReadonlyBytes bytes, Opti
     return Error::from_string_literal("Gfx::Bitmap unable to load from file");
     return Error::from_string_literal("Gfx::Bitmap unable to load from file");
 }
 }
 
 
-Bitmap::Bitmap(BitmapFormat format, IntSize size, size_t pitch, void* data)
+Bitmap::Bitmap(BitmapFormat format, IntSize size, size_t pitch, void* data, Function<void()>&& destruction_callback)
     : m_size(size)
     : m_size(size)
     , m_data(data)
     , m_data(data)
     , m_pitch(pitch)
     , m_pitch(pitch)
     , m_format(format)
     , m_format(format)
+    , m_destruction_callback(move(destruction_callback))
 {
 {
     VERIFY(pitch >= minimum_pitch(size.width(), format));
     VERIFY(pitch >= minimum_pitch(size.width(), format));
     VERIFY(!size_would_overflow(format, size));
     VERIFY(!size_would_overflow(format, size));
@@ -368,9 +371,8 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::to_bitmap_backed_by_anonymous_buffer() co
 
 
 Bitmap::~Bitmap()
 Bitmap::~Bitmap()
 {
 {
-    if (m_data_is_malloced) {
-        kfree_sized(m_data, size_in_bytes());
-    }
+    if (m_destruction_callback)
+        m_destruction_callback();
     m_data = nullptr;
     m_data = nullptr;
 }
 }
 
 

+ 3 - 3
Userland/Libraries/LibGfx/Bitmap.h

@@ -63,7 +63,7 @@ class Bitmap : public RefCounted<Bitmap> {
 public:
 public:
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize);
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize);
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize);
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize);
-    [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, size_t pitch, void*);
+    [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, size_t pitch, void*, Function<void()>&& destruction_callback = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, Optional<IntSize> ideal_size = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, Optional<IntSize> ideal_size = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path, Optional<IntSize> ideal_size = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path, Optional<IntSize> ideal_size = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_bytes(ReadonlyBytes, Optional<IntSize> ideal_size = {}, Optional<ByteString> mine_type = {});
     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_bytes(ReadonlyBytes, Optional<IntSize> ideal_size = {}, Optional<ByteString> mine_type = {});
@@ -160,7 +160,7 @@ public:
 
 
 private:
 private:
     Bitmap(BitmapFormat, IntSize, BackingStore const&);
     Bitmap(BitmapFormat, IntSize, BackingStore const&);
-    Bitmap(BitmapFormat, IntSize, size_t pitch, void*);
+    Bitmap(BitmapFormat, IntSize, size_t pitch, void*, Function<void()>&& destruction_callback);
     Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize);
     Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize);
 
 
     static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size);
     static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size);
@@ -169,8 +169,8 @@ private:
     void* m_data { nullptr };
     void* m_data { nullptr };
     size_t m_pitch { 0 };
     size_t m_pitch { 0 };
     BitmapFormat m_format { BitmapFormat::Invalid };
     BitmapFormat m_format { BitmapFormat::Invalid };
-    bool m_data_is_malloced { false };
     Core::AnonymousBuffer m_buffer;
     Core::AnonymousBuffer m_buffer;
+    Function<void()> m_destruction_callback;
 };
 };
 
 
 ALWAYS_INLINE u8* Bitmap::scanline_u8(int y)
 ALWAYS_INLINE u8* Bitmap::scanline_u8(int y)