add macOS implementation
This commit is contained in:
103
macos/include/filaflat/ChunkContainer.h
Normal file
103
macos/include/filaflat/ChunkContainer.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAFLAT_CHUNK_CONTAINER_H
|
||||
#define TNT_FILAFLAT_CHUNK_CONTAINER_H
|
||||
|
||||
|
||||
#include <utils/compiler.h>
|
||||
|
||||
#include <filament/MaterialChunkType.h>
|
||||
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
namespace filaflat {
|
||||
|
||||
using ShaderContent = utils::FixedCapacityVector<uint8_t>;
|
||||
using BlobDictionary = utils::FixedCapacityVector<ShaderContent>;
|
||||
|
||||
class Unflattener;
|
||||
|
||||
// Allows to build a map of chunks in a Package and get direct individual access based on chunk ID.
|
||||
class UTILS_PUBLIC ChunkContainer {
|
||||
public:
|
||||
using Type = filamat::ChunkType;
|
||||
|
||||
ChunkContainer(void const* data, size_t size) : mData(data), mSize(size) {}
|
||||
|
||||
~ChunkContainer() noexcept;
|
||||
|
||||
// Must be called before trying to access any of the chunk. Fails and return false ONLY if
|
||||
// an incomplete chunk is found or if a chunk with bogus size is found.
|
||||
bool parse() noexcept;
|
||||
|
||||
typedef struct {
|
||||
const uint8_t* start;
|
||||
size_t size;
|
||||
} ChunkDesc;
|
||||
|
||||
typedef struct {
|
||||
Type type;
|
||||
ChunkDesc desc;
|
||||
} Chunk;
|
||||
|
||||
size_t getChunkCount() const noexcept {
|
||||
return mChunks.size();
|
||||
}
|
||||
|
||||
Chunk getChunk(size_t index) const noexcept {
|
||||
auto it = mChunks.begin();
|
||||
std::advance(it, index);
|
||||
return { it->first, it->second };
|
||||
}
|
||||
|
||||
std::pair<uint8_t const*, uint8_t const*> getChunkRange(Type type) const noexcept {
|
||||
ChunkDesc const* pChunkDesc;
|
||||
bool success = hasChunk(type, &pChunkDesc);
|
||||
if (success) {
|
||||
return { pChunkDesc->start, pChunkDesc->start + pChunkDesc->size };
|
||||
}
|
||||
return { nullptr, nullptr };
|
||||
}
|
||||
|
||||
bool hasChunk(Type type, ChunkDesc const** pChunkDesc = nullptr) const noexcept {
|
||||
auto& chunks = mChunks;
|
||||
auto pos = chunks.find(type);
|
||||
if (pos != chunks.end()) {
|
||||
if (pChunkDesc) {
|
||||
*pChunkDesc = &pos.value();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void const* getData() const { return mData; }
|
||||
|
||||
size_t getSize() const { return mSize; }
|
||||
|
||||
private:
|
||||
bool parseChunk(Unflattener& unflattener);
|
||||
|
||||
void const* mData;
|
||||
size_t mSize;
|
||||
tsl::robin_map<Type, ChunkContainer::ChunkDesc> mChunks;
|
||||
};
|
||||
|
||||
} // namespace filaflat
|
||||
#endif
|
||||
32
macos/include/filaflat/DictionaryReader.h
Normal file
32
macos/include/filaflat/DictionaryReader.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAFLAT_DICTIONARY_READER_H
|
||||
#define TNT_FILAFLAT_DICTIONARY_READER_H
|
||||
|
||||
#include <filaflat/ChunkContainer.h>
|
||||
|
||||
namespace filaflat {
|
||||
|
||||
struct DictionaryReader {
|
||||
static bool unflatten(ChunkContainer const& container,
|
||||
ChunkContainer::Type dictionaryTag,
|
||||
BlobDictionary& dictionary);
|
||||
};
|
||||
|
||||
} // namespace filaflat
|
||||
|
||||
#endif // TNT_FILAFLAT_DICTIONARY_READER_H
|
||||
70
macos/include/filaflat/MaterialChunk.h
Normal file
70
macos/include/filaflat/MaterialChunk.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMAT_MATERIAL_CHUNK_H
|
||||
#define TNT_FILAMAT_MATERIAL_CHUNK_H
|
||||
|
||||
#include <filament/MaterialChunkType.h>
|
||||
|
||||
#include <filaflat/ChunkContainer.h>
|
||||
#include <filaflat/Unflattener.h>
|
||||
|
||||
#include <private/filament/Variant.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
namespace filaflat {
|
||||
|
||||
class MaterialChunk {
|
||||
public:
|
||||
using Variant = filament::Variant;
|
||||
|
||||
explicit MaterialChunk(ChunkContainer const& container);
|
||||
~MaterialChunk() noexcept;
|
||||
|
||||
// call this once after container.parse() has been called
|
||||
bool initialize(filamat::ChunkType materialTag);
|
||||
|
||||
// call this as many times as needed
|
||||
// populates "shaderContent" with the requested shader, or returns false on failure.
|
||||
bool getShader(ShaderContent& shaderContent, BlobDictionary const& dictionary,
|
||||
uint8_t shaderModel, Variant variant, uint8_t stage);
|
||||
|
||||
// These methods are for debugging purposes only (matdbg)
|
||||
// @{
|
||||
static void decodeKey(uint32_t key, uint8_t* model, Variant::type_t* variant, uint8_t* stage);
|
||||
const tsl::robin_map<uint32_t, uint32_t>& getOffsets() const { return mOffsets; }
|
||||
// @}
|
||||
|
||||
private:
|
||||
ChunkContainer const& mContainer;
|
||||
filamat::ChunkType mMaterialTag = filamat::ChunkType::Unknown;
|
||||
Unflattener mUnflattener;
|
||||
const uint8_t* mBase = nullptr;
|
||||
tsl::robin_map<uint32_t, uint32_t> mOffsets;
|
||||
|
||||
bool getTextShader(Unflattener unflattener,
|
||||
BlobDictionary const& dictionary, ShaderContent& shaderContent,
|
||||
uint8_t shaderModel, Variant variant, uint8_t stage);
|
||||
|
||||
bool getSpirvShader(
|
||||
BlobDictionary const& dictionary, ShaderContent& shaderContent,
|
||||
uint8_t shaderModel, Variant variant, uint8_t stage);
|
||||
};
|
||||
|
||||
} // namespace filamat
|
||||
|
||||
#endif // TNT_FILAMAT_MATERIAL_CHUNK_H
|
||||
163
macos/include/filaflat/Unflattener.h
Normal file
163
macos/include/filaflat/Unflattener.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAFLAT_UNFLATTENER_H
|
||||
#define TNT_FILAFLAT_UNFLATTENER_H
|
||||
|
||||
#include <utils/compiler.h>
|
||||
#include <utils/CString.h>
|
||||
|
||||
#include <private/filament/Variant.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace filaflat {
|
||||
|
||||
// Allow read operation from an Unflattenable. All READ operation MUST go through the Unflattener
|
||||
// since it checks boundaries before readind. All read operations return values MUST be verified,
|
||||
// never assume a read will succeed.
|
||||
class UTILS_PUBLIC Unflattener {
|
||||
public:
|
||||
Unflattener() noexcept = default;
|
||||
|
||||
Unflattener(const uint8_t* src, const uint8_t* end)
|
||||
: mSrc(src), mCursor(src), mEnd(end) {
|
||||
assert_invariant(src && end);
|
||||
}
|
||||
|
||||
Unflattener(Unflattener const& rhs) = default;
|
||||
|
||||
~Unflattener() noexcept = default;
|
||||
|
||||
bool hasData() const noexcept {
|
||||
return mCursor < mEnd;
|
||||
}
|
||||
|
||||
inline bool willOverflow(size_t size) const noexcept {
|
||||
return (mCursor + size) > mEnd;
|
||||
}
|
||||
|
||||
void skipAlignmentPadding() {
|
||||
const uint8_t padSize = (8 - (intptr_t(mCursor) % 8)) % 8;
|
||||
mCursor += padSize;
|
||||
assert_invariant(0 == (intptr_t(mCursor) % 8));
|
||||
}
|
||||
|
||||
bool read(bool* b) noexcept {
|
||||
if (willOverflow(1)) {
|
||||
return false;
|
||||
}
|
||||
*b = mCursor[0];
|
||||
mCursor += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read(uint8_t* i) noexcept {
|
||||
if (willOverflow(1)) {
|
||||
return false;
|
||||
}
|
||||
*i = mCursor[0];
|
||||
mCursor += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read(filament::Variant* v) noexcept {
|
||||
return read(&v->key);
|
||||
}
|
||||
|
||||
bool read(uint16_t* i) noexcept {
|
||||
if (willOverflow(2)) {
|
||||
return false;
|
||||
}
|
||||
*i = 0;
|
||||
*i |= mCursor[0];
|
||||
*i |= mCursor[1] << 8;
|
||||
mCursor += 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read(uint32_t* i) noexcept {
|
||||
if (willOverflow(4)) {
|
||||
return false;
|
||||
}
|
||||
*i = 0;
|
||||
*i |= mCursor[0];
|
||||
*i |= mCursor[1] << 8;
|
||||
*i |= mCursor[2] << 16;
|
||||
*i |= mCursor[3] << 24;
|
||||
mCursor += 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read(uint64_t* i) noexcept {
|
||||
if (willOverflow(8)) {
|
||||
return false;
|
||||
}
|
||||
*i = 0;
|
||||
*i |= static_cast<int64_t>(mCursor[0]);
|
||||
*i |= static_cast<int64_t>(mCursor[1]) << 8;
|
||||
*i |= static_cast<int64_t>(mCursor[2]) << 16;
|
||||
*i |= static_cast<int64_t>(mCursor[3]) << 24;
|
||||
*i |= static_cast<int64_t>(mCursor[4]) << 32;
|
||||
*i |= static_cast<int64_t>(mCursor[5]) << 40;
|
||||
*i |= static_cast<int64_t>(mCursor[6]) << 48;
|
||||
*i |= static_cast<int64_t>(mCursor[7]) << 56;
|
||||
mCursor += 8;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read(utils::CString* s) noexcept;
|
||||
|
||||
bool read(const char** blob, size_t* size) noexcept;
|
||||
|
||||
bool read(const char** s) noexcept;
|
||||
|
||||
bool read(float* f) noexcept {
|
||||
if (willOverflow(4)) {
|
||||
return false;
|
||||
}
|
||||
uint32_t i;
|
||||
i = 0;
|
||||
i |= mCursor[0];
|
||||
i |= mCursor[1] << 8;
|
||||
i |= mCursor[2] << 16;
|
||||
i |= mCursor[3] << 24;
|
||||
*f = reinterpret_cast<float&>(i);
|
||||
mCursor += 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
const uint8_t* getCursor() const noexcept {
|
||||
return mCursor;
|
||||
}
|
||||
|
||||
void setCursor(const uint8_t* cursor) noexcept {
|
||||
if (mSrc <= cursor && cursor < mEnd) {
|
||||
mCursor = cursor;
|
||||
} else {
|
||||
mCursor = mEnd;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t* mSrc = nullptr;
|
||||
const uint8_t* mCursor = nullptr;
|
||||
const uint8_t* mEnd = nullptr;
|
||||
};
|
||||
|
||||
} //namespace filaflat
|
||||
|
||||
#endif // TNT_FILAFLAT_UNFLATTENER_H
|
||||
Reference in New Issue
Block a user