Merge "minui: Add GRSurface::Clone()." am: 948790fa4c
am: 24b89ffd25
Change-Id: I889882310398dd6968d0034c55d6562802ccc616
This commit is contained in:
@@ -40,6 +40,9 @@ class GRSurface {
|
|||||||
static std::unique_ptr<GRSurface> Create(int width, int height, int row_bytes, int pixel_bytes,
|
static std::unique_ptr<GRSurface> Create(int width, int height, int row_bytes, int pixel_bytes,
|
||||||
size_t data_size);
|
size_t data_size);
|
||||||
|
|
||||||
|
// Clones the current GRSurface instance (i.e. an image).
|
||||||
|
std::unique_ptr<GRSurface> Clone() const;
|
||||||
|
|
||||||
virtual uint8_t* data() {
|
virtual uint8_t* data() {
|
||||||
return data_;
|
return data_;
|
||||||
}
|
}
|
||||||
@@ -59,6 +62,7 @@ class GRSurface {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t* data_{ nullptr };
|
uint8_t* data_{ nullptr };
|
||||||
|
size_t data_size_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(GRSurface);
|
DISALLOW_COPY_AND_ASSIGN(GRSurface);
|
||||||
};
|
};
|
||||||
|
|||||||
+8
-2
@@ -44,13 +44,19 @@ std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_byte
|
|||||||
static constexpr size_t kSurfaceDataAlignment = 8;
|
static constexpr size_t kSurfaceDataAlignment = 8;
|
||||||
// Cannot use std::make_unique to access non-public ctor.
|
// Cannot use std::make_unique to access non-public ctor.
|
||||||
auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes));
|
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;
|
(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;
|
if (result->data_ == nullptr) return nullptr;
|
||||||
return result;
|
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() {
|
GRSurface::~GRSurface() {
|
||||||
if (data_ != nullptr) {
|
if (data_ != nullptr) {
|
||||||
free(data_);
|
free(data_);
|
||||||
|
|||||||
@@ -15,8 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <vector>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
@@ -30,3 +31,14 @@ TEST(GRSurfaceTest, Create_aligned) {
|
|||||||
ASSERT_EQ(0, reinterpret_cast<uintptr_t>(surface->data()) % kSurfaceDataAlignment);
|
ASSERT_EQ(0, reinterpret_cast<uintptr_t>(surface->data()) % kSurfaceDataAlignment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(GRSurfaceTest, Clone) {
|
||||||
|
static constexpr size_t kImageSize = 10 * 50;
|
||||||
|
auto image = GRSurface::Create(50, 10, 50, 1, kImageSize);
|
||||||
|
for (auto i = 0; i < kImageSize; i++) {
|
||||||
|
image->data()[i] = rand() % 128;
|
||||||
|
}
|
||||||
|
auto image_copy = image->Clone();
|
||||||
|
ASSERT_EQ(std::vector(image->data(), image->data() + kImageSize),
|
||||||
|
std::vector(image_copy->data(), image_copy->data() + kImageSize));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user