1 /*
2  * Copyright (C) 2016, 2018-2019 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef MALI_GRALLOC_MODULE_H_
19 #define MALI_GRALLOC_MODULE_H_
20 
21 #include <system/window.h>
22 #include <linux/fb.h>
23 #include <pthread.h>
24 #include <log/log.h>
25 
26 #if GRALLOC_VERSION_MAJOR == 0
27 #include <hardware/gralloc.h>
28 #elif GRALLOC_VERSION_MAJOR == 1
29 #include <hardware/gralloc1.h>
30 #else
31 #if HIDL_COMMON_VERSION_SCALED == 100
32 #include <android/hardware/graphics/common/1.0/types.h>
33 #elif HIDL_COMMON_VERSION_SCALED == 110
34 #include <android/hardware/graphics/common/1.1/types.h>
35 #endif
36 #endif
37 
38 typedef enum
39 {
40 	MALI_DPY_TYPE_UNKNOWN = 0,
41 	MALI_DPY_TYPE_CLCD,
42 	MALI_DPY_TYPE_HDLCD
43 } mali_dpy_type;
44 
45 #if GRALLOC_VERSION_MAJOR <= 1
46 extern hw_module_methods_t mali_gralloc_module_methods;
47 #endif
48 
49 #if GRALLOC_VERSION_MAJOR == 1
50 typedef struct
51 {
52 	struct hw_module_t common;
53 } gralloc_module_t;
54 #endif
55 
56 #if GRALLOC_VERSION_MAJOR == 0
57 int gralloc_register_buffer(gralloc_module_t const *module, buffer_handle_t handle);
58 int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle);
59 int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h,
60                         void **vaddr);
61 int gralloc_lock_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w,
62                               int h, android_ycbcr *ycbcr);
63 int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle);
64 int gralloc_lock_async(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w,
65                               int h, void **vaddr, int32_t fence_fd);
66 int gralloc_lock_ycbcr_async(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t,
67                                     int w, int h, android_ycbcr *ycbcr, int32_t fence_fd);
68 int gralloc_unlock_async(gralloc_module_t const *module, buffer_handle_t handle, int32_t *fence_fd);
69 #endif
70 
71 struct private_module_t
72 {
73 #if GRALLOC_VERSION_MAJOR <= 1
74 	gralloc_module_t base;
75 #endif
76 
77 	struct private_handle_t *framebuffer;
78 	uint32_t flags;
79 	uint32_t numBuffers;
80 	uint32_t bufferMask;
81 	pthread_mutex_t lock;
82 	buffer_handle_t currentBuffer;
83 	mali_dpy_type dpy_type;
84 
85 	struct fb_var_screeninfo info;
86 	struct fb_fix_screeninfo finfo;
87 	float xdpi;
88 	float ydpi;
89 	float fps;
90 	int swapInterval;
91 	uint64_t fbdev_format;
92 	int ionfd;
93 
94 #ifdef __cplusplus
95 	/* Never intended to be used from C code */
96 	enum
97 	{
98 		// flag to indicate we'll post this buffer
99 		PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
100 	};
101 #endif
102 
103 #ifdef __cplusplus
104 	#define INIT_ZERO(obj) (memset(&(obj), 0, sizeof((obj))))
private_module_tprivate_module_t105 	private_module_t()
106 	{
107 	#if GRALLOC_VERSION_MAJOR <= 1
108 		base.common.tag = HARDWARE_MODULE_TAG;
109 		#if GRALLOC_VERSION_MAJOR == 1
110 		base.common.version_major = GRALLOC_MODULE_API_VERSION_1_0;
111 		ALOGI("Arm Module v1.0");
112 		#else
113 		base.common.version_major = GRALLOC_MODULE_API_VERSION_0_3;
114 		ALOGI("Arm Module v0.3");
115 		#endif
116 
117 		base.common.version_minor = 0;
118 		base.common.id = GRALLOC_HARDWARE_MODULE_ID;
119 		base.common.name = "Graphics Memory Allocator Module";
120 		base.common.author = "ARM Ltd.";
121 		base.common.methods = &mali_gralloc_module_methods;
122 		base.common.dso = NULL;
123 		INIT_ZERO(base.common.reserved);
124 
125 	#if GRALLOC_VERSION_MAJOR == 0
126 		base.registerBuffer = gralloc_register_buffer;
127 		base.unregisterBuffer = gralloc_unregister_buffer;
128 		base.lock = gralloc_lock;
129 		base.lock_ycbcr = gralloc_lock_ycbcr;
130 		base.unlock = gralloc_unlock;
131 		base.lockAsync = gralloc_lock_async;
132 		base.lockAsync_ycbcr = gralloc_lock_ycbcr_async;
133 		base.unlockAsync = gralloc_unlock_async;
134 		base.perform = NULL;
135 		INIT_ZERO(base.reserved_proc);
136 		#endif
137 	#endif
138 
139 		framebuffer = NULL;
140 		flags = 0;
141 		numBuffers = 0;
142 		bufferMask = 0;
143 		pthread_mutex_init(&(lock), NULL);
144 		currentBuffer = NULL;
145 		INIT_ZERO(info);
146 		INIT_ZERO(finfo);
147 		xdpi = 0.0f;
148 		ydpi = 0.0f;
149 		fps = 0.0f;
150 		swapInterval = 1;
151 		ionfd = -1;
152 	}
153 	#undef INIT_ZERO
154 #endif /* For #ifdef __cplusplus */
155 };
156 
157 typedef struct private_module_t mali_gralloc_module;
158 
159 #endif /* MALI_GRALLOC_MODULE_H_ */
160