1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_RESOURCEMEMORYALLOCATION_H_
16 #define DAWNNATIVE_RESOURCEMEMORYALLOCATION_H_
17 
18 #include <cstdint>
19 
20 namespace dawn_native {
21 
22     class ResourceHeapBase;
23 
24     // Allocation method determines how memory was sub-divided.
25     // Used by the device to get the allocator that was responsible for the allocation.
26     enum class AllocationMethod {
27 
28         // Memory not sub-divided.
29         kDirect,
30 
31         // Memory sub-divided using one or more blocks of various sizes.
32         kSubAllocated,
33 
34         // Memory was allocated outside of Dawn.
35         kExternal,
36 
37         // Memory not allocated or freed.
38         kInvalid
39     };
40 
41     // Metadata that describes how the allocation was allocated.
42     struct AllocationInfo {
43         // AllocationInfo contains a separate offset to not confuse block vs memory offsets.
44         // The block offset is within the entire allocator memory range and only required by the
45         // buddy sub-allocator to get the corresponding memory. Unlike the block offset, the
46         // allocation offset is always local to the memory.
47         uint64_t mBlockOffset = 0;
48 
49         AllocationMethod mMethod = AllocationMethod::kInvalid;
50     };
51 
52     // Handle into a resource heap pool.
53     class ResourceMemoryAllocation {
54       public:
55         ResourceMemoryAllocation();
56         ResourceMemoryAllocation(const AllocationInfo& info,
57                                  uint64_t offset,
58                                  ResourceHeapBase* resourceHeap,
59                                  uint8_t* mappedPointer = nullptr);
60         virtual ~ResourceMemoryAllocation() = default;
61 
62         ResourceHeapBase* GetResourceHeap() const;
63         uint64_t GetOffset() const;
64         uint8_t* GetMappedPointer() const;
65         AllocationInfo GetInfo() const;
66 
67         virtual void Invalidate();
68 
69       private:
70         AllocationInfo mInfo;
71         uint64_t mOffset;
72         ResourceHeapBase* mResourceHeap;
73         uint8_t* mMappedPointer;
74     };
75 }  // namespace dawn_native
76 
77 #endif  // DAWNNATIVE_RESOURCEMEMORYALLOCATION_H_
78