1/*
2 * Copyright (c) 2021-2023 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 DISPLAY_GRALLOC_GBM_H
17#define DISPLAY_GRALLOC_GBM_H
18#include "buffer_handle.h"
19#include "hdf_dlist.h"
20#include "hdf_log.h"
21#include "v1_0/display_buffer_type.h"
22
23namespace OHOS {
24namespace HDI {
25namespace DISPLAY {
26using namespace OHOS::HDI::Display::Buffer::V1_0;
27
28using GrallocManager = struct {
29    struct gbm_device *gbmDevice;
30    int drmFd;
31    struct DListHead gbmBoHead;
32    int32_t referCount;
33};
34
35using GbmBoList = struct {
36    struct DListHead entry;
37    struct gbm_bo *bo;
38    int fd;
39};
40
41int32_t GbmAllocMem(const AllocInfo *info, BufferHandle **buffer);
42void GbmFreeMem(BufferHandle *buffer);
43void *GbmMmap(BufferHandle *buffer);
44int32_t GbmUnmap(BufferHandle *buffer);
45int32_t GbmInvalidateCache(BufferHandle *buffer);
46int32_t GbmFlushCache(BufferHandle *buffer);
47int32_t GbmGrallocUninitialize(void);
48int32_t GbmGrallocInitialize(void);
49
50#ifdef GRALLOC_LOCK_DEBUG
51#define GRALLOC_LOCK(format, ...)                                                                                    \
52    do {                                                                                                             \
53        HDF_LOGE("[%{public}s@%{public}s:%{public}d]" format "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
54        pthread_mutex_lock(&g_lock);                                                                                 \
55    } while (0)
56
57#define GRALLOC_UNLOCK(format, ...)                                                                                  \
58    do {                                                                                                             \
59        HDF_LOGE("[%{public}s@%{public}s:%{public}d]" format "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
60        pthread_mutex_unlock(&g_lock);                                                                               \
61    } while (0)
62#else
63#define GRALLOC_LOCK(format, ...)    \
64    do {                             \
65        pthread_mutex_lock(&g_lock); \
66    } while (0)
67
68#define GRALLOC_UNLOCK(format, ...)    \
69    do {                               \
70        pthread_mutex_unlock(&g_lock); \
71    } while (0)
72#endif
73} // namespace DISPLAY
74} // namespace HDI
75} // namespace OHOS
76#endif // DISPLAY_GRALLOC_GBM_H
77