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_encode_allocator.h
24 //! \brief    Resource allocator for all buffer/surface used by encoder
25 //!
26 
27 #ifndef __CODECHAL_ENCODE_ALLOCATOR_H__
28 #define __CODECHAL_ENCODE_ALLOCATOR_H__
29 
30 #include "codechal_allocator.h"
31 
32 //!
33 //! \enum     ResourceName
34 //! \brief    Resource name
35 //!
36 enum ResourceName
37 {
38     // common VDEnc buffer
39     vdencStats = 0,
40     vdencIntraRowStoreScratch = vdencStats + 1,
41     vdencEnd = vdencIntraRowStoreScratch,
42 
43     // common PAK buffer
44     pakStats = vdencEnd + 1,
45     pakInfo = pakStats + 1,
46     pakEnd = pakInfo,
47 
48     // HEVC buffer
49     hevcMbCodeBuffer = pakEnd + 1,
50     brcInputForEncKernel = hevcMbCodeBuffer + 1,
51 
52     // tracked buffer
53     trackedBuffer = 512,
54     cscSurface = trackedBuffer,
55     mbCodeBuffer = cscSurface + 32,
56     mvDataBuffer = mbCodeBuffer + 32,
57     mvTemporalBuffer = mvDataBuffer + 32,
58     ds4xSurface = mvTemporalBuffer + 32,
59     ds2xSurface = ds4xSurface + 32,
60     ds16xSurface = ds2xSurface + 32,
61     ds32xSurface = ds16xSurface + 32,
62     ds4xRecon = ds32xSurface + 32,
63     ds8xRecon = ds4xRecon + 32,
64     trackedBufferEnd = ds8xRecon,
65 
66     // recycled buffer
67     recycledBuffer = trackedBufferEnd + 32,
68     brcReadSurface = recycledBuffer,
69     recycledBufferEnd = brcReadSurface
70 };
71 
72 typedef union _RESOURCE_TAG
73 {
74     struct
75     {
76         union
77         {
78             struct
79             {
80                 uint16_t        trackedRecycleBufferIndex : MOS_BITFIELD_RANGE(0, 4);
81                 uint16_t        trackedRecycleBufferName : MOS_BITFIELD_RANGE(5, 10);
82                 uint16_t        codec : MOS_BITFIELD_RANGE(11, 13);
83                 uint16_t        type : MOS_BITFIELD_RANGE(14, 15);
84             };
85             uint16_t            typeID;
86         };
87         uint16_t                format : MOS_BITFIELD_RANGE(0, 3);
88         uint16_t                tile : MOS_BITFIELD_RANGE(4, 6);
89         uint16_t                zeroOnAllocation : MOS_BITFIELD_BIT(7);
90         uint16_t                reserved : MOS_BITFIELD_RANGE(8, 15);
91         union
92         {
93             struct
94             {
95                 uint16_t        width;
96                 uint16_t        height;
97             };
98             uint32_t            size;
99         };
100     };
101     uint64_t                    tag;
102 }RESOURCE_TAG, *PRESOURCE_TAG;
103 
104 //!
105 //! \class    CodechalEncodeAllocator
106 //! \brief    This class provides resource management for all codecs to
107 //!           obtain buffer/surface/resource used by EU kernel and HW.
108 //!
109 class CodechalEncodeAllocator : public CodechalAllocator
110 {
111 public:
112     //!
113     //! \brief    Allocate resource
114     //!
115     //! \return   pointer to resource, NULL if error
116     //!
117     void* AllocateResource(
118         uint32_t codec, uint32_t width, uint32_t height, ResourceName name, const char *bufName, uint8_t index = 0,
119         bool zeroOnAllocation = false, MOS_FORMAT format = Format_Buffer, MOS_TILE_TYPE tile = MOS_TILE_LINEAR, uint32_t dwMemType = 0);
120 
121     //!
122     //! \brief    Return address/pointer of a resource already allocated
123     //!
124     //! \return   pointer to resource, NULL if the resource not allcoated yet
125     //!
126     void* GetResource(uint32_t codec, ResourceName name, uint8_t index = 0);
127 
128     //!
129     //! \brief    Return size of allocated resource
130     //!
131     //! \return   size of allocated resource
132     //!
133     uint32_t GetResourceSize(uint32_t codec, ResourceName name, uint8_t index = 0);
134 
135     //!
136     //! \brief    Release a resource
137     //!
138     void ReleaseResource(uint32_t codec, ResourceName name, uint8_t index = 0);
139 
140     //!
141     //! \brief    Constructor
142     //!
143     CodechalEncodeAllocator(CodechalEncoderState* encoder);
144 
145     //!
146     //! \brief    Destructor
147     //!
148     ~CodechalEncodeAllocator();
149 
150 private:
151     CodechalEncodeAllocator(const CodechalEncodeAllocator&) = delete;
152     CodechalEncodeAllocator& operator=(const CodechalEncodeAllocator&) = delete;
153 
154     uint16_t MosToAllocatorCodec(uint32_t);
155     uint16_t MosToAllocatorFormat(MOS_FORMAT format);
156     uint16_t MosToAllocatorTile(MOS_TILE_TYPE type);
157 
IsTrackedBuffer(ResourceName name)158     inline bool IsTrackedBuffer(ResourceName name)
159     {
160         return (trackedBuffer <= name && name <= trackedBufferEnd);
161     }
162 
IsRecycleBuffer(ResourceName name)163     inline bool IsRecycleBuffer(ResourceName name)
164     {
165         return (recycledBuffer <= name && name <= recycledBufferEnd);
166     }
167 
168     uint16_t SetResourceID(uint32_t codec, ResourceName name, uint8_t index);
169     virtual uint16_t GetResourceID(uint64_t resourceTag, Match level) override;
170 
171     CodechalEncoderState*           m_encoder = nullptr;                //!< Pointer to ENCODER base class
172 
173     static const uint16_t           m_bufIndexMask = (1 << 5) - 1;
174     static const uint16_t           m_bufNameMask = (1 << 14) - 1;
175 };
176 #endif  // __CODECHAL_ENCODE_ALLOCATOR_H__
177