1 /*
2 * Copyright (c) 2019, 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     decode_resource_auto_lock.h
24 //! \brief    Defines the interface for resource auto lock
25 //! \details  The interface implements the auto lock and auto unlock function.
26 //!
27 
28 #ifndef __DECODE_RESOURCE_AUTO_LOCK_H__
29 #define __DECODE_RESOURCE_AUTO_LOCK_H__
30 #include "decode_allocator.h"
31 
32 namespace decode
33 {
34 
35 class ResourceAutoLock
36 {
37 public:
38     //!
39     //! \brief  Constructor
40     //!
ResourceAutoLock(DecodeAllocator * allocator,PMOS_RESOURCE resource)41     ResourceAutoLock(DecodeAllocator* allocator, PMOS_RESOURCE resource):
42         m_allocator(allocator),
43         m_resource(resource)
44     {
45     }
46 
47     //!
48     //! \brief  Destructor
49     //!
~ResourceAutoLock()50     virtual ~ResourceAutoLock()
51     {
52         if (m_allocator != nullptr && m_resource != nullptr)
53         {
54             m_allocator->UnLock(m_resource);
55         }
56     }
57 
LockResourceForWrite()58     void* LockResourceForWrite()
59     {
60         if (m_allocator == nullptr || m_resource == nullptr)
61         {
62             return nullptr;
63         }
64         return m_allocator->LockResourceForWrite(m_resource);
65     }
66 
LockResourceWithNoOverwrite()67     void* LockResourceWithNoOverwrite()
68     {
69         if (m_allocator == nullptr || m_resource == nullptr)
70         {
71             return nullptr;
72         }
73 
74         return m_allocator->LockResourceWithNoOverwrite(m_resource);
75     }
76 
LockResourceForRead()77     void* LockResourceForRead()
78     {
79         if (m_allocator == nullptr || m_resource == nullptr)
80         {
81             return nullptr;
82         }
83         return m_allocator->LockResourceForRead(m_resource);
84     }
85 
86 private:
87     DecodeAllocator* m_allocator;
88     PMOS_RESOURCE    m_resource;
89 
90 MEDIA_CLASS_DEFINE_END(decode__ResourceAutoLock)
91 };
92 
93 }  // namespace decode
94 #endif  // !__DECODE_RESOURCE_AUTO_LOCK_H__