WIP on host buffer allocations.

This commit is contained in:
Chinmay Garde 2021-05-28 15:23:35 -07:00 committed by Dan Field
parent 97eee62317
commit 47540e614e
7 changed files with 193 additions and 0 deletions

View File

@ -24,6 +24,8 @@ impeller_component("compositor") {
"pipeline_descriptor.mm",
"pipeline_library.h",
"pipeline_library.mm",
"range.cc",
"range.h",
"renderer.h",
"renderer.mm",
"shader_function.h",

View File

@ -4,11 +4,36 @@
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/compositor/allocator.h"
#include "impeller/compositor/range.h"
namespace impeller {
class BufferBase {
public:
virtual uint8_t* GetMapping() const = 0;
virtual size_t GetLength() const = 0;
};
class BufferView : public BufferBase {
public:
uint8_t* GetMapping() const {
return parent_->GetMapping() + range_in_parent_.offset;
}
size_t GetLength() const { return range_in_parent_.length; }
private:
std::shared_ptr<BufferBase> parent_;
Range range_in_parent_;
FML_DISALLOW_COPY_AND_ASSIGN(BufferView);
};
class Buffer {
public:
~Buffer();
@ -29,4 +54,33 @@ class Buffer {
FML_DISALLOW_COPY_AND_ASSIGN(Buffer);
};
class HostBuffer : public std::enable_shared_from_this<HostBuffer>,
public BufferBase {
public:
std::shared_ptr<HostBuffer> Create();
std::shared_ptr<BufferView> Emplace(size_t length);
~HostBuffer();
uint8_t* GetMapping() const override { return buffer_; }
size_t GetLength() const override { return length_; }
[[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);
HostBuffer();
FML_DISALLOW_COPY_AND_ASSIGN(HostBuffer);
};
} // namespace impeller

View File

@ -4,6 +4,8 @@
#include "impeller/compositor/buffer.h"
#include "flutter/fml/logging.h"
namespace impeller {
Buffer::Buffer(id<MTLBuffer> buffer,
@ -14,4 +16,65 @@ Buffer::Buffer(id<MTLBuffer> buffer,
Buffer::~Buffer() = default;
std::shared_ptr<HostBuffer> HostBuffer::Create() {
return std::shared_ptr<HostBuffer>(new HostBuffer());
}
HostBuffer::HostBuffer() = default;
HostBuffer::~HostBuffer() {
::free(buffer_);
}
std::shared_ptr<BufferView> HostBuffer::Emplace(size_t length) {
if (!Truncate(length_ + length)) {
return nullptr;
}
}
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) {
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 memory.";
return false;
}
buffer_ = static_cast<uint8_t*>(new_allocation);
reserved_ = reserved;
return true;
}
} // namespace impeller

View File

@ -0,0 +1,13 @@
// 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/compositor/command_buffer.h"
namespace impeller {
Class::Class() = default;
Class::~Class() = default;
} // namespace impeller

View File

@ -0,0 +1,32 @@
// 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.
#pragma once
#include <functional>
#include "flutter/fml/macros.h"
namespace impeller {
class CommandBuffer {
public:
~CommandBuffer();
enum class CommitResult {
kPending,
kError,
kCompleted,
};
using CommitCallback = std::function<void(CommitResult)>;
void Commit(CommitCallback callback);
private:
CommandBuffer();
FML_DISALLOW_COPY_AND_ASSIGN(CommandBuffer);
};
} // namespace impeller

View File

@ -0,0 +1,11 @@
// 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/compositor/range.h"
namespace impeller {
//
} // namespace impeller

View File

@ -0,0 +1,18 @@
// 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.
#pragma once
#include <cstddef>
#include "flutter/fml/macros.h"
namespace impeller {
struct Range {
size_t offset = 0;
size_t length = 0;
};
} // namespace impeller