Debian Codegraph @

libs/gltfio/src/DracoCache.h

raw
1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef GLTFIO_DRACO_CACHE_H
18 #define GLTFIO_DRACO_CACHE_H
19
20 #include <cgltf.h>
21
22 #include <tsl/robin_map.h>
23
24 #include <memory>
25
26 #ifndef GLTFIO_DRACO_SUPPORTED
27 #define GLTFIO_DRACO_SUPPORTED 0
28 #endif
29
30 namespace gltfio {
31
32 class DracoMesh;
33
34 // Manages a set of Draco meshes that can be looked up using cgltf_buffer_view.
35 //
36 // The cache key is the buffer view that holds the compressed data. This allows the loader to
37 // avoid duplicated work when a single Draco mesh is referenced from multiple primitives.
38 class DracoCache {
39 public:
40 DracoMesh* findOrCreateMesh(const cgltf_buffer_view* key);
41 private:
42 tsl::robin_map<const cgltf_buffer_view*, std::unique_ptr<DracoMesh>> mCache;
43 };
44
45 // Decodes a Draco mesh upon construction and retains the results.
46 //
47 // The DracoMesh API leverages cgltf accessor structs in a way that bears explanation. These are
48 // read / write parameters that tell the decoder where to write the decoded data, and what format
49 // is desired. The buffer_view in the accessor should be null unless decompressed data is already
50 // loaded. This tells the decoder that it should create a buffer_view and a buffer. The buffer
51 // view, the buffer, and the buffer's data are all automatically freed when DracoMesh is destroyed.
52 //
53 // Note that in the gltfio architecture, the AssetLoader has the job of constructing VertexBuffer
54 // objects while the ResourceLoader has the job of populating them asychronously. This means that
55 // our Draco decoder relies on the accessor fields being 100% correct. If we had to be robust
56 // against faulty accessor information, we would need to replace the VertexBuffer object that was
57 // created in the AssetLoader, which would be a messy process.
58 class DracoMesh {
59 public:
60 static DracoMesh* decode(const uint8_t* compressedData, size_t compressedSize);
61 bool getFaceIndices(cgltf_accessor* destination) const;
62 bool getVertexAttributes(uint32_t attributeId, cgltf_accessor* destination) const;
63 ~DracoMesh();
64 private:
65 DracoMesh(struct DracoMeshDetails* details);
66 std::unique_ptr<struct DracoMeshDetails> mDetails;
67 };
68
69 } // namespace gltfio
70
71 #endif // GLTFIO_DRACO_CACHE_H