1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     codechal_allocator.h
24 //! \brief    A generic resource allocator
25 //!
26 
27 #ifndef __CODECHAL_ALLOCATOR_H__
28 #define __CODECHAL_ALLOCATOR_H__
29 
30 #include "codechal.h"
31 #include <map>
32 
33 //!
34 //! This class provides a generic resource allocation service.
35 //! So far support 3 types: 1D buffer, 2D surface, and batch buffer
36 //!
37 class CodechalAllocator
38 {
39 public:
40     //!
41     //! \brief    Constructor
42     //!
43     CodechalAllocator(MOS_INTERFACE*);
44 
45     //!
46     //! \brief    Destructor
47     //!
48     virtual ~CodechalAllocator();
49 
50 protected:
51     //!
52     //! \brief    Resource's unique ID contains different level of info, search/match can be performed
53     //!           based on different level
54     //!
55     //! \return   resource's 64-bit tag
56     //!
57     enum Match
58     {
59         matchLevel0 = 0,
60         matchLevel1 = 1,
61         matchAll = 2
62     };
63 
64     //!
65     //! \brief    Get resource's 64-bit tag according to resource's ID and match level
66     //!
67     //! \return   resource's 64-bit tag
68     //!
69     uint64_t GetResourceTag(uint16_t resourceID, Match level);
70 
71     //!
72     //! \brief    Get resource's address/pointer according to resource's ID and match level
73     //!
74     //! \return   resource's address/pointer, NULL if resource does not exist in the list
75     //!
76     void* GetResourcePointer(uint16_t resourceID, Match level);
77 
78     //!
79     //! \brief    Allocate 1D buffer
80     //!
81     //! \return   pointer to 1D buffer
82     //!
83     void* Allocate1DBuffer(uint64_t resourceTag, uint32_t size,
84         bool zeroOnAllocation = false, const char *bufName = nullptr);
85 
86     //!
87     //! \brief    Allocate 2D buffer
88     //!
89     //! \return   pointer to 2D buffer
90     //!
91     void* Allocate2DBuffer(
92         uint64_t resourceTag, uint32_t width, uint32_t height, MOS_FORMAT format,
93         MOS_TILE_TYPE tile, bool zeroOnAllocation = false, const char *bufName = nullptr);
94 
95     //!
96     //! \brief    Allocate batch buffer
97     //!
98     //! \return   pointer to batch buffer
99     //!
100     void* AllocateBatchBuffer(uint64_t resourceTag, uint32_t size, bool zeroOnAllocation = false);
101 
102     //!
103     //! \brief    Release a resource
104     //!
105     void ReleaseResource(uint16_t resourceID, Match level);
106 
107 private:
108     CodechalAllocator(const CodechalAllocator&) = delete;
109     CodechalAllocator& operator=(const CodechalAllocator&) = delete;
110 
111     bool Is1DBuffer(uint64_t resourceTag);
112     bool Is2DBuffer(uint64_t resourceTag);
113     bool IsBatchBuffer(uint64_t resourceTag);
114     void ClearResource(MOS_RESOURCE*, size_t);
115     void Deallocate(uint64_t tag, void* pointer);
116 
117     //!
118     //! \brief    Get resource's ID according to different level info for purpose of match at different level
119     //!           Derivde class to implement this
120     //!
121     //! \return   resource's 16-bit ID containing corresponding level-identifiable info
122     //!
123     virtual uint16_t GetResourceID(uint64_t resourceTag, Match level) = 0;
124 
125     MOS_INTERFACE*                      m_osInterface = nullptr;            //!< OS interface
126     std::map<uint64_t, void*>           m_resourceList{};                   //!< list of <tag, pointer>
127 };
128 
129 #endif  // __CODECHAL_ALLOCATOR_H__