• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "display_gralloc.h"
16 #include <cerrno>
17 #include "allocator_manager.h"
18 #include "display_common.h"
19 using namespace OHOS::HDI::DISPLAY;
AllocMem(const AllocInfo * info,BufferHandle ** handle)20 static int32_t AllocMem(const AllocInfo *info, BufferHandle **handle)
21 {
22     DISPLAY_LOGD();
23     DISPLAY_CHK_RETURN((info == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is nullptr"));
24     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
25     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(info->usage);
26     CHECK_NULLPOINTER_RETURN_VALUE(allocPtr, DISPLAY_NULL_PTR);
27     return allocPtr->AllocMem(*info, handle);
28 }
29 
FreeMem(BufferHandle * handle)30 static void FreeMem(BufferHandle *handle)
31 {
32     DISPLAY_LOGD();
33     DISPLAY_CHK_RETURN_NOT_VALUE((handle == nullptr), DISPLAY_LOGE("handle is nullptr"));
34     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(handle->usage);
35     CHECK_NULLPOINTER_RETURN(allocPtr);
36     allocPtr->FreeMem(handle);
37 }
38 
Mmap(BufferHandle * handle)39 static void *Mmap(BufferHandle *handle)
40 {
41     DISPLAY_LOGD();
42     DISPLAY_CHK_RETURN((handle == nullptr), nullptr, DISPLAY_LOGE("handle is nullptr"));
43     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(handle->usage);
44     CHECK_NULLPOINTER_RETURN_VALUE(allocPtr, nullptr);
45     return allocPtr->Mmap(*handle);
46 }
47 
Unmap(BufferHandle * handle)48 static int32_t Unmap(BufferHandle *handle)
49 {
50     DISPLAY_LOGD();
51     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
52     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(handle->usage);
53     CHECK_NULLPOINTER_RETURN_VALUE(allocPtr, DISPLAY_NULL_PTR);
54     return allocPtr->Unmap(*handle);
55 }
56 
FlushCache(BufferHandle * handle)57 static int32_t FlushCache(BufferHandle *handle)
58 {
59     DISPLAY_LOGD();
60     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
61     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(handle->usage);
62     CHECK_NULLPOINTER_RETURN_VALUE(allocPtr, DISPLAY_NULL_PTR);
63     return allocPtr->FlushCache(*handle);
64 }
65 
InvalidateCache(BufferHandle * handle)66 static int32_t InvalidateCache(BufferHandle *handle)
67 {
68     DISPLAY_LOGD();
69     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
70     Allocator* allocPtr = AllocatorManager::GetInstance().GetAllocator(handle->usage);
71     CHECK_NULLPOINTER_RETURN_VALUE(allocPtr, DISPLAY_NULL_PTR);
72     return allocPtr->InvalidateCache(*handle);
73 }
74 
75 extern "C" {
GrallocInitialize(GrallocFuncs ** funcs)76 int32_t GrallocInitialize(GrallocFuncs **funcs)
77 {
78     DISPLAY_LOGD();
79     DISPLAY_CHK_RETURN((funcs == nullptr), DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
80     GrallocFuncs *grallocFuncs = (GrallocFuncs *)malloc(sizeof(GrallocFuncs));
81     DISPLAY_CHK_RETURN((grallocFuncs == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("malloc failed"));
82     (void)memset_s(grallocFuncs, sizeof(GrallocFuncs), 0, sizeof(GrallocFuncs));
83     // initialize gralloc
84     int ret = AllocatorManager::GetInstance().Init();
85     if (ret != DISPLAY_SUCCESS) {
86         DISPLAY_LOGE("failed to initialize allocator manager");
87         free(grallocFuncs);
88         grallocFuncs = nullptr;
89         return DISPLAY_FAILURE;
90     }
91     grallocFuncs->AllocMem = AllocMem;
92     grallocFuncs->FreeMem = FreeMem;
93     grallocFuncs->Mmap = Mmap;
94     grallocFuncs->Unmap = Unmap;
95     grallocFuncs->InvalidateCache = InvalidateCache;
96     grallocFuncs->FlushCache = FlushCache;
97     *funcs = grallocFuncs;
98     DISPLAY_LOGD("gralloc initialize success");
99     return DISPLAY_SUCCESS;
100 }
101 
GrallocUninitialize(GrallocFuncs * funcs)102 int32_t GrallocUninitialize(GrallocFuncs *funcs)
103 {
104     DISPLAY_LOGD();
105     DISPLAY_CHK_RETURN(funcs == nullptr, DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
106     free(funcs);
107     AllocatorManager::GetInstance().DeInit();
108     return DISPLAY_SUCCESS;
109 }
110 }