diff options
Diffstat (limited to 'minui')
-rw-r--r-- | minui/include/minui/minui.h | 4 | ||||
-rw-r--r-- | minui/resources.cpp | 10 |
2 files changed, 12 insertions, 2 deletions
diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index d6881e9a0..0b499e621 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -40,6 +40,9 @@ class GRSurface { static std::unique_ptr<GRSurface> Create(int width, int height, int row_bytes, int pixel_bytes, size_t data_size); + // Clones the current GRSurface instance (i.e. an image). + std::unique_ptr<GRSurface> Clone() const; + virtual uint8_t* data() { return data_; } @@ -59,6 +62,7 @@ class GRSurface { private: uint8_t* data_{ nullptr }; + size_t data_size_; DISALLOW_COPY_AND_ASSIGN(GRSurface); }; diff --git a/minui/resources.cpp b/minui/resources.cpp index 9c9af0242..9027bc668 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -44,13 +44,19 @@ std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_byte static constexpr size_t kSurfaceDataAlignment = 8; // Cannot use std::make_unique to access non-public ctor. auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes)); - size_t aligned_size = + result->data_size_ = (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment; - result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, aligned_size)); + result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_)); if (result->data_ == nullptr) return nullptr; return result; } +std::unique_ptr<GRSurface> GRSurface::Clone() const { + auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes, data_size_); + memcpy(result->data_, data_, data_size_); + return result; +} + GRSurface::~GRSurface() { if (data_ != nullptr) { free(data_); |