Tests for host buffers.

This commit is contained in:
Chinmay Garde
2021-06-18 14:00:37 -07:00
committed by Dan Field
parent 37c2842964
commit 7d4462ad1a
8 changed files with 120 additions and 6 deletions

View File

@@ -58,12 +58,17 @@ impeller_component("compositor") {
"../geometry",
"../shader_glue",
]
libs = [ "Metal.framework" ]
}
source_set("compositor_unittests") {
testonly = true
sources = []
sources = [ "host_buffer_unittests.mm" ]
deps = [ "//flutter/testing:testing_lib" ]
deps = [
":compositor",
"//flutter/testing:testing_lib",
]
}

View File

@@ -5,6 +5,7 @@
#pragma once
#include <memory>
#include <type_traits>
#include "flutter/fml/macros.h"
#include "impeller/compositor/buffer.h"
@@ -20,7 +21,14 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
static std::shared_ptr<HostBuffer> Create();
BufferView Emplace(const void* buffer, size_t length);
size_t GetLength() const;
size_t GetReservedLength() const;
template <class T, class = std::enable_if_t<std::is_standard_layout_v<T>>>
[[nodiscard]] BufferView Emplace(const T& t) {
return Emplace(reinterpret_cast<const void*>(&t), sizeof(T), alignof(T));
}
[[nodiscard]] bool Truncate(size_t length);
@@ -36,6 +44,10 @@ class HostBuffer final : public std::enable_shared_from_this<HostBuffer>,
std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
Allocator& allocator) const override;
[[nodiscard]] BufferView Emplace(const void* buffer,
size_t length,
size_t align);
[[nodiscard]] bool Reserve(size_t reserved);
[[nodiscard]] bool ReserveNPOT(size_t reserved);

View File

@@ -4,6 +4,8 @@
#include "impeller/compositor/host_buffer.h"
#include <algorithm>
#include "flutter/fml/logging.h"
#include "impeller/compositor/allocator.h"
@@ -21,7 +23,17 @@ HostBuffer::~HostBuffer() {
::free(buffer_);
}
BufferView HostBuffer::Emplace(const void* buffer, size_t length) {
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) {
auto old_length = length_;
if (!Truncate(length_ + length)) {
return {};
@@ -61,9 +73,13 @@ bool HostBuffer::ReserveNPOT(size_t 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.
@@ -72,8 +88,10 @@ bool HostBuffer::Reserve(size_t reserved) {
FML_LOG(ERROR) << "Allocation failed. Out of memory.";
return false;
}
buffer_ = static_cast<uint8_t*>(new_allocation);
reserved_ = reserved;
return true;
}

View File

@@ -0,0 +1,64 @@
// 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 "flutter/testing/testing.h"
#include "impeller/compositor/host_buffer.h"
namespace impeller {
namespace testing {
TEST(HostBufferTest, TestInitialization) {
ASSERT_TRUE(HostBuffer::Create());
// Newly allocated buffers don't touch the heap till they have to.
ASSERT_EQ(HostBuffer::Create()->GetLength(), 0u);
ASSERT_EQ(HostBuffer::Create()->GetReservedLength(), 0u);
}
TEST(HostBufferTest, CanEmplace) {
struct Length2 {
uint8_t pad[2];
};
static_assert(sizeof(Length2) == 2u);
auto buffer = HostBuffer::Create();
for (size_t i = 0; i < 12500; i++) {
auto view = buffer->Emplace(Length2{});
ASSERT_TRUE(view);
ASSERT_EQ(buffer->GetLength(), (i + 1) * sizeof(Length2));
ASSERT_EQ(view.range, Range(i * sizeof(Length2), 2u));
}
}
TEST(HostBufferTest, CanEmplaceWithAlignment) {
struct Length2 {
uint8_t pad[2];
};
static_assert(sizeof(Length2) == 2);
struct alignas(16) Align16 {
uint8_t pad[2];
};
static_assert(alignof(Align16) == 16);
static_assert(sizeof(Align16) == 16);
auto buffer = HostBuffer::Create();
ASSERT_TRUE(buffer);
{
auto view = buffer->Emplace(Length2{});
ASSERT_TRUE(view);
ASSERT_EQ(buffer->GetLength(), 2u);
ASSERT_EQ(view.range, Range(0u, 2u));
}
{
auto view = buffer->Emplace(Align16{});
ASSERT_TRUE(view);
ASSERT_EQ(buffer->GetLength(), 32u);
ASSERT_EQ(view.range, Range(16u, 16u));
}
}
} // namespace testing
} // namespace impeller

View File

@@ -13,6 +13,15 @@ namespace impeller {
struct Range {
size_t offset = 0;
size_t length = 0;
constexpr Range() {}
constexpr Range(size_t p_offset, size_t p_length)
: offset(p_offset), length(p_length) {}
constexpr bool operator==(const Range& o) const {
return offset == o.offset && length == o.length;
}
};
} // namespace impeller

View File

@@ -71,7 +71,7 @@ class RenderPass {
bool IsValid() const;
HostBuffer& GetHostBuffer();
HostBuffer& GetTransientsBuffer();
bool RecordCommand(Command command);

View File

@@ -129,7 +129,7 @@ RenderPass::RenderPass(id<MTLCommandBuffer> buffer,
RenderPass::~RenderPass() = default;
HostBuffer& RenderPass::GetHostBuffer() {
HostBuffer& RenderPass::GetTransientsBuffer() {
return *transients_buffer_;
}

View File

@@ -35,8 +35,14 @@ bool EntityRenderer::OnIsValid() const {
}
bool EntityRenderer::OnRender(RenderPass& pass) {
shader::BoxVertexInfo::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic({800, 600});
Command cmd;
cmd.pipeline = box_primitive_->GetPipeline();
cmd.vertex_bindings
.buffers[shader::BoxVertexInfo::kUniformUniformBuffer.location] =
pass.GetTransientsBuffer().Emplace(uniforms);
pass.RecordCommand(std::move(cmd));
return true;
}