Split allocation into its base file.
This commit is contained in:
@@ -6,6 +6,8 @@ import("../tools/impeller.gni")
|
||||
|
||||
impeller_component("base") {
|
||||
sources = [
|
||||
"allocation.cc",
|
||||
"allocation.h",
|
||||
"base.h",
|
||||
"config.h",
|
||||
"strings.cc",
|
||||
|
||||
82
engine/src/flutter/impeller/base/allocation.cc
Normal file
82
engine/src/flutter/impeller/base/allocation.cc
Normal file
@@ -0,0 +1,82 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/base/allocation.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Allocation::Allocation() = default;
|
||||
|
||||
Allocation::~Allocation() {
|
||||
::free(buffer_);
|
||||
}
|
||||
|
||||
uint8_t* Allocation::GetBuffer() const {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
size_t Allocation::GetLength() const {
|
||||
return length_;
|
||||
}
|
||||
|
||||
size_t Allocation::GetReservedLength() const {
|
||||
return reserved_;
|
||||
}
|
||||
|
||||
bool Allocation::Truncate(size_t length) {
|
||||
if (!ReserveNPOT(length)) {
|
||||
return false;
|
||||
}
|
||||
length_ = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t NextPowerOfTwoSize(uint32_t x) {
|
||||
if (x == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
--x;
|
||||
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
bool Allocation::ReserveNPOT(size_t reserved) {
|
||||
return Reserve(NextPowerOfTwoSize(reserved));
|
||||
}
|
||||
|
||||
bool Allocation::Reserve(size_t reserved) {
|
||||
// Reserve at least one page of data.
|
||||
reserved = std::max<size_t>(4096u, reserved);
|
||||
|
||||
if (reserved == reserved_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto new_allocation = ::realloc(buffer_, reserved);
|
||||
if (!new_allocation) {
|
||||
// If new length is zero, a minimum non-zero sized allocation is returned.
|
||||
// So this check will not trip and this routine will indicate success as
|
||||
// expected.
|
||||
FML_LOG(ERROR) << "Allocation failed. Out of host memory.";
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer_ = static_cast<uint8_t*>(new_allocation);
|
||||
reserved_ = reserved;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
38
engine/src/flutter/impeller/base/allocation.h
Normal file
38
engine/src/flutter/impeller/base/allocation.h
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class Allocation {
|
||||
public:
|
||||
Allocation();
|
||||
|
||||
~Allocation();
|
||||
|
||||
uint8_t* GetBuffer() const;
|
||||
|
||||
size_t GetLength() const;
|
||||
|
||||
size_t GetReservedLength() const;
|
||||
|
||||
[[nodiscard]] bool Truncate(size_t length);
|
||||
|
||||
private:
|
||||
uint8_t* buffer_ = nullptr;
|
||||
size_t length_ = 0;
|
||||
size_t reserved_ = 0;
|
||||
|
||||
[[nodiscard]] bool Reserve(size_t reserved);
|
||||
|
||||
[[nodiscard]] bool ReserveNPOT(size_t reserved);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Allocation);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/base/allocation.h"
|
||||
#include "impeller/compositor/buffer.h"
|
||||
#include "impeller/compositor/buffer_view.h"
|
||||
#include "impeller/compositor/platform.h"
|
||||
@@ -17,19 +18,16 @@
|
||||
namespace impeller {
|
||||
|
||||
class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
|
||||
public Allocation,
|
||||
public Buffer {
|
||||
public:
|
||||
static std::shared_ptr<HostBuffer> Create();
|
||||
|
||||
// |Buffer|
|
||||
virtual ~HostBuffer();
|
||||
|
||||
static std::shared_ptr<HostBuffer> Create();
|
||||
|
||||
void SetLabel(std::string label);
|
||||
|
||||
size_t GetLength() const;
|
||||
|
||||
size_t GetReservedLength() const;
|
||||
|
||||
template <class T, class = std::enable_if_t<std::is_standard_layout_v<T>>>
|
||||
[[nodiscard]] BufferView EmplaceUniform(const T& t) {
|
||||
return Emplace(reinterpret_cast<const void*>(&t), sizeof(T),
|
||||
@@ -45,14 +43,9 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
|
||||
size_t length,
|
||||
size_t align);
|
||||
|
||||
[[nodiscard]] bool Truncate(size_t length);
|
||||
|
||||
private:
|
||||
mutable std::shared_ptr<DeviceBuffer> device_buffer_;
|
||||
mutable size_t device_buffer_generation_ = 0u;
|
||||
uint8_t* buffer_ = nullptr;
|
||||
size_t length_ = 0;
|
||||
size_t reserved_ = 0;
|
||||
size_t generation_ = 1u;
|
||||
std::string label_;
|
||||
|
||||
@@ -62,10 +55,6 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
|
||||
|
||||
[[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
|
||||
|
||||
[[nodiscard]] bool Reserve(size_t reserved);
|
||||
|
||||
[[nodiscard]] bool ReserveNPOT(size_t reserved);
|
||||
|
||||
HostBuffer();
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(HostBuffer);
|
||||
|
||||
@@ -20,31 +20,21 @@ std::shared_ptr<HostBuffer> HostBuffer::Create() {
|
||||
|
||||
HostBuffer::HostBuffer() = default;
|
||||
|
||||
HostBuffer::~HostBuffer() {
|
||||
::free(buffer_);
|
||||
}
|
||||
HostBuffer::~HostBuffer() = default;
|
||||
|
||||
void HostBuffer::SetLabel(std::string label) {
|
||||
label_ = std::move(label);
|
||||
}
|
||||
|
||||
size_t HostBuffer::GetLength() const {
|
||||
return length_;
|
||||
}
|
||||
|
||||
size_t HostBuffer::GetReservedLength() const {
|
||||
return reserved_;
|
||||
}
|
||||
|
||||
BufferView HostBuffer::Emplace(const void* buffer,
|
||||
size_t length,
|
||||
size_t align) {
|
||||
if (align == 0 || (length_ % align) == 0) {
|
||||
if (align == 0 || (GetLength() % align) == 0) {
|
||||
return Emplace(buffer, length);
|
||||
}
|
||||
|
||||
{
|
||||
auto pad = Emplace(nullptr, align - (length_ % align));
|
||||
auto pad = Emplace(nullptr, align - (GetLength() % align));
|
||||
if (!pad) {
|
||||
return {};
|
||||
}
|
||||
@@ -54,75 +44,24 @@ BufferView HostBuffer::Emplace(const void* buffer,
|
||||
}
|
||||
|
||||
BufferView HostBuffer::Emplace(const void* buffer, size_t length) {
|
||||
auto old_length = length_;
|
||||
if (!Truncate(length_ + length)) {
|
||||
auto old_length = GetLength();
|
||||
if (!Truncate(GetLength() + length)) {
|
||||
return {};
|
||||
}
|
||||
FML_DCHECK(buffer_);
|
||||
FML_DCHECK(GetBuffer());
|
||||
generation_++;
|
||||
if (buffer) {
|
||||
::memmove(buffer_ + old_length, buffer, length);
|
||||
::memmove(GetBuffer() + old_length, buffer, length);
|
||||
}
|
||||
return BufferView{shared_from_this(), Range{old_length, length}};
|
||||
}
|
||||
|
||||
bool HostBuffer::Truncate(size_t length) {
|
||||
if (!ReserveNPOT(length)) {
|
||||
return false;
|
||||
}
|
||||
length_ = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t NextPowerOfTwoSize(uint32_t x) {
|
||||
if (x == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
--x;
|
||||
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
bool HostBuffer::ReserveNPOT(size_t reserved) {
|
||||
return Reserve(NextPowerOfTwoSize(reserved));
|
||||
}
|
||||
|
||||
bool HostBuffer::Reserve(size_t reserved) {
|
||||
// Reserve at least one page of data.
|
||||
reserved = std::max<size_t>(4096u, reserved);
|
||||
|
||||
if (reserved == reserved_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto new_allocation = ::realloc(buffer_, reserved);
|
||||
if (!new_allocation) {
|
||||
// If new length is zero, a minimum non-zero sized allocation is returned.
|
||||
// So this check will not trip and this routine will indicate success as
|
||||
// expected.
|
||||
FML_LOG(ERROR) << "Allocation failed. Out of host memory.";
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer_ = static_cast<uint8_t*>(new_allocation);
|
||||
reserved_ = reserved;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<const DeviceBuffer> HostBuffer::GetDeviceBuffer(
|
||||
Allocator& allocator) const {
|
||||
if (generation_ == device_buffer_generation_) {
|
||||
return device_buffer_;
|
||||
}
|
||||
auto new_buffer = allocator.CreateBufferWithCopy(buffer_, length_);
|
||||
auto new_buffer = allocator.CreateBufferWithCopy(GetBuffer(), GetLength());
|
||||
if (!new_buffer) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ std::optional<TextureDescriptor> TextureDescriptor::MakeFromImageResult(
|
||||
const auto pixel_format =
|
||||
FormatForImageResultComponents(result.GetComponents());
|
||||
if (!pixel_format.has_value()) {
|
||||
FML_DLOG(ERROR) << "Unknown image format.";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,10 @@ test_fixtures("fixtures") {
|
||||
fixtures = [
|
||||
"sample.vert",
|
||||
"types.h",
|
||||
"image.png",
|
||||
"airplane.jpg",
|
||||
"bay_bridge.jpg",
|
||||
"boston.jpg",
|
||||
"embarcadero.jpg",
|
||||
"kalimba.jpg",
|
||||
]
|
||||
}
|
||||
|
||||
BIN
engine/src/flutter/impeller/fixtures/airplane.jpg
Normal file
BIN
engine/src/flutter/impeller/fixtures/airplane.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
BIN
engine/src/flutter/impeller/fixtures/bay_bridge.jpg
Normal file
BIN
engine/src/flutter/impeller/fixtures/bay_bridge.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 136 KiB |
BIN
engine/src/flutter/impeller/fixtures/boston.jpg
Normal file
BIN
engine/src/flutter/impeller/fixtures/boston.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 195 KiB |
BIN
engine/src/flutter/impeller/fixtures/embarcadero.jpg
Normal file
BIN
engine/src/flutter/impeller/fixtures/embarcadero.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 113 KiB |
BIN
engine/src/flutter/impeller/fixtures/kalimba.jpg
Normal file
BIN
engine/src/flutter/impeller/fixtures/kalimba.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
@@ -47,7 +47,7 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
ASSERT_TRUE(vertex_buffer);
|
||||
|
||||
// Contents texture.
|
||||
Image image(flutter::testing::OpenFixtureAsMapping("image.png"));
|
||||
Image image(flutter::testing::OpenFixtureAsMapping("bay_bridge.jpg"));
|
||||
auto result = image.Decode();
|
||||
ASSERT_TRUE(result.IsValid());
|
||||
auto texture_descriptor = TextureDescriptor::MakeFromImageResult(result);
|
||||
|
||||
Reference in New Issue
Block a user