1 /*
2  * Copyright (C) 2020 Samsung Electronics Co. Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VENDOR_GRAPHIC_BUFFER_H_
18 #define VENDOR_GRAPHIC_BUFFER_H_
19 
20 #include <cstdint>
21 #include <ui/GraphicTypes.h>
22 #include <ui/GraphicBufferMapper.h>
23 #include <ui/GraphicBufferAllocator.h>
24 #include <ui/Rect.h>
25 #include <ui/PixelFormat.h>
26 
27 /* Gralloc1 usages enum is provided here to ensure backward compatibility
28  * This enum will be deprecated in Android S so please consider switching to
29  * BufferUsage::<USAGE> found in
30  * hardware/interfaces/1.0(or 1.1, 1.2)/types.hal
31  */
32 #include <hardware/gralloc1.h>
33 
34 namespace vendor {
35 namespace graphics {
36 
37 /* Android default usages */
38 typedef android::hardware::graphics::common::V1_2::BufferUsage BufferUsage;
39 
40 enum VendorGraphicBufferUsage {
41 /* S.LSI specific usages */
42 	NO_AFBC                         = 1ULL << 29,
43 	MFC                             = 1ULL << 50,  /* identical to GRALLOC_USAGE_GOOGLE_IP_MFC */
44 	ROIINFO                         = 1ULL << 52,
45 	AFBC_PADDING                    = 1ULL << 53,
46 	FORCE_BACKBUFFER                = 1ULL << 54,
47 	FRONTBUFFER                     = 1ULL << 55,
48 	NOZEROED                        = 1ULL << 58,
49 	PRIVATE_NONSECURE               = 1ULL << 59,
50 	VIDEO_PRIVATE_DATA              = 1ULL << 60,
51 /* Google-specific usages */
52 	CAMERA_STATS                    = 1ULL << 30,
53 	TPU_OUTPUT                      = 1ULL << 31,
54 	TPU_INPUT                       = 1ULL << 62
55 };
56 
57 
58 /* A wrapper class to gralloc private handle */
59 class VendorGraphicBufferMeta {
60 private:
VendorGraphicBufferMeta()61 	VendorGraphicBufferMeta() {};
62 
63 public:
64 	enum
65 	{
66 		PRIV_FLAGS_USES_2PRIVATE_DATA = 1U << 4,
67 		PRIV_FLAGS_USES_3PRIVATE_DATA = 1U << 5,
68 	};
69 
70 	union {
71 		int fds[3];
72 
73 		struct {
74 			int fd  = -1;
75 			int fd1 = -1;
76 			int fd2 = -1;
77 		};
78 	};
79 
80 	union {
81 		int sizes[3];
82 
83 		struct {
84 			int size  = 0;
85 			int size1 = 0;
86 			int size2 = 0;
87 		};
88 	};
89 
90 	union {
91 		uint32_t offsets[3];
92 
93 		struct {
94 			uint32_t offset  = 0;
95 			uint32_t offset1 = 0;
96 			uint32_t offset2 = 0;
97 		};
98 	};
99 
100 	union
101 	{
102 		uint32_t format;
103 		uint64_t internal_format = 0llu;
104 	};
105 	int frameworkFormat = 0;
106 
107 	int width  = 0;
108 	int height = 0;
109 	uint32_t stride  = 0;
110 	uint32_t vstride = 0;
111 
112 	uint64_t producer_usage = 0llu;
113 	uint64_t consumer_usage = 0llu;
114 
115 	int flags = 0;
116 
117 	uint64_t unique_id = 0;
118 
119 	VendorGraphicBufferMeta(const buffer_handle_t handle);
120 
121 	void init(const buffer_handle_t handle);
122 
123 	static int get_fd(buffer_handle_t, int num);
124 	static int get_size(buffer_handle_t, int num);
125 	static uint32_t get_format(buffer_handle_t);
126 	static uint64_t get_internal_format(buffer_handle_t);
127 	static uint64_t get_frameworkFormat(buffer_handle_t);
128 	static int get_width(buffer_handle_t);
129 	static int get_height(buffer_handle_t);
130 	static uint32_t get_stride(buffer_handle_t);
131 	static uint32_t get_vstride(buffer_handle_t);
132 	static uint64_t get_producer_usage(buffer_handle_t);
133 	static uint64_t get_consumer_usage(buffer_handle_t);
134 	static uint64_t get_flags(buffer_handle_t);
135 
136 	static uint64_t get_usage(buffer_handle_t);
137 	static int is_afbc(buffer_handle_t);
138 	static int is_sbwc(buffer_handle_t);
139 	static void* get_video_metadata(buffer_handle_t);
140 
141 	/* get_video_metadata_roiinfo is only supported with gralloc4
142 	 * When gralloc3 is used, will always return nullptr
143 	 */
144 	static void* get_video_metadata_roiinfo(buffer_handle_t);
145 	static int get_video_metadata_fd(buffer_handle_t);
146 	static int get_dataspace(buffer_handle_t);
147 	static int set_dataspace(buffer_handle_t hnd, android_dataspace_t dataspace);
148 
149 	static buffer_handle_t import_buffer(buffer_handle_t);
150 	static int free_buffer(buffer_handle_t);
151 };
152 
153 
154 /* Mapper extension class to allow locking with 64-bit usages */
155 class VendorGraphicBufferMapper : public android::GraphicBufferMapper
156 {
157 public:
get()158 	static inline VendorGraphicBufferMapper& get()
159 	{
160 		return static_cast<VendorGraphicBufferMapper&>(getInstance());
161 	}
162 
163 	android::status_t lock64(buffer_handle_t handle, uint64_t usage, const android::Rect& bounds,
164 		void** vaddr, int32_t* outBytesPerPixel = nullptr,
165 		int32_t* outBytesPerStride = nullptr);
166 
167 	android::status_t lockYCbCr64(buffer_handle_t handle,
168 		uint64_t usage, const android::Rect& bounds, android_ycbcr *ycbcr);
169 };
170 
171 typedef class android::GraphicBufferAllocator VendorGraphicBufferAllocator;
172 
173 } /* namespace graphics */
174 } /* namespace vendor */
175 
176 #endif /* GRALLOC_PRIV_INTERFACE_H_ */
177