• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
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 
16 #ifndef GRAPHIC_LITE_BUFFER_MANAGER_H
17 #define GRAPHIC_LITE_BUFFER_MANAGER_H
18 
19 #include <map>
20 #include "display_gralloc.h"
21 #include "surface_buffer_impl.h"
22 #include "surface_type.h"
23 
24 namespace OHOS {
25 /**
26  * @brief Buffer Manager abstract class. Provide allocate, free, map, unmap buffer attr ability.
27  *        It needs vendor to adapte it. Default Hisi support shm and physical memory.
28  */
29 class BufferManager {
30 public:
31     /**
32      * @brief Buffer Manager Shared Memory Single Instance.
33      *        It depends kernel supported shm.
34      * @returns BufferManager pointer.
35      */
36     static BufferManager* GetInstance();
37 
38     /**
39      * @brief Buffer Manager Init.
40      * @returns Whether Buffer manager init succeed or not.
41      */
42     bool Init();
43 
44     /**
45      * @brief Allocate buffer for producer.
46      * @param [in] size, alloc buffer size.
47      * @param [in] usage, alloc buffer usage.
48      * @returns buffer poiter.
49      */
50     SurfaceBufferImpl* AllocBuffer(uint32_t size, uint32_t usage);
51 
52     /**
53      * @brief Allocate buffer for producer.
54      * @param [in] width, alloc buffer width.
55      * @param [in] height, alloc buffer height.
56      * @param [in] format, alloc buffer format.
57      * @param [in] usage, alloc buffer usage.
58      * @returns buffer poiter.
59      */
60     SurfaceBufferImpl* AllocBuffer(uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
61 
62     /**
63      * @brief Free the buffer.
64      * @param [in] SurfaceBufferImpl double pointer, free the buffer size.
65      */
66     void FreeBuffer(SurfaceBufferImpl** buffer);
67 
68     /**
69      * @brief Flush the buffer.
70      * @param [in] Flush SurfaceBufferImpl cache to physical memory.
71      * @returns 0 is succeed; other is failed.
72      */
73     int32_t FlushCache(SurfaceBufferImpl& buffer) const;
74 
75     /**
76      * @brief Map the buffer for producer.
77      * @param [in] SurfaceBufferImpl, need to map.
78      * @returns Whether map buffer succeed or not.
79      */
80     bool MapBuffer(SurfaceBufferImpl& buffer) const;
81 
82     /**
83      * @brief Unmap the buffer, which producer could not writed data.
84      * @param [in] SurfaceBufferImpl, need to unmap.
85      */
86     void UnmapBuffer(SurfaceBufferImpl& buffer) const;
87 
88 protected:
89     BufferHandle* AllocateBufferHandle(SurfaceBufferImpl& buffer) const;
90     SurfaceBufferImpl* AllocBuffer(AllocInfo info);
91     bool ConvertUsage(uint64_t& destUsage, uint32_t srcUsage) const;
92     bool ConvertFormat(PixelFormat& destFormat, uint32_t srcFormat) const;
93 
94 private:
BufferManager()95     BufferManager() : grallocFucs_(nullptr) {}
~BufferManager()96     ~BufferManager() {}
97 
98     GrallocFuncs* grallocFucs_;
99     struct BufferKey {
100         int32_t key;
101         uint64_t phyAddr;
102         bool operator< (const BufferKey &x) const
103         {
104             return (key < x.key) || (key == x.key && phyAddr < x.phyAddr);
105         }
106     };
107     std::map<BufferKey, BufferHandle*> bufferHandleMap_;
108 };
109 } // end namespace
110 #endif