blob: 122ee2a68e416d47c78bc98a74a8facb50e95697 [file] [log] [blame]
John Stultz16100f62017-05-03 11:12:18 -07001/*
John Stultz18814f62018-02-22 16:02:49 -08002 * Copyright (C) 2014-2017 ARM Limited. All rights reserved.
John Stultz16100f62017-05-03 11:12:18 -07003 *
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
19#ifndef GRALLOC_BUFFER_PRIV_H_
20#define GRALLOC_BUFFER_PRIV_H_
21
22#include "gralloc_priv.h"
23#include <errno.h>
24#include <string.h>
John Stultz18814f62018-02-22 16:02:49 -080025#include "mali_gralloc_private_interface_types.h"
John Stultz16100f62017-05-03 11:12:18 -070026
27// private gralloc buffer manipulation API
28
29struct attr_region
30{
31 /* Rectangle to be cropped from the full frame (Origin in top-left corner!) */
32 int crop_top;
33 int crop_left;
34 int crop_height;
35 int crop_width;
36 int use_yuv_transform;
37 int use_sparse_alloc;
John Stultz18814f62018-02-22 16:02:49 -080038 mali_hdr_info hdr_info;
39} __attribute__((packed));
John Stultz16100f62017-05-03 11:12:18 -070040
41typedef struct attr_region attr_region;
42
John Stultz16100f62017-05-03 11:12:18 -070043/*
44 * Allocate shared memory for attribute storage. Only to be
45 * used by gralloc internally.
46 *
47 * Return 0 on success.
48 */
John Stultz18814f62018-02-22 16:02:49 -080049int gralloc_buffer_attr_allocate(struct private_handle_t *hnd);
John Stultz16100f62017-05-03 11:12:18 -070050
51/*
52 * Frees the shared memory allocated for attribute storage.
53 * Only to be used by gralloc internally.
54
55 * Return 0 on success.
56 */
John Stultz18814f62018-02-22 16:02:49 -080057int gralloc_buffer_attr_free(struct private_handle_t *hnd);
John Stultz16100f62017-05-03 11:12:18 -070058
59/*
60 * Map the attribute storage area before attempting to
61 * read/write from it.
62 *
63 * Return 0 on success.
64 */
John Stultz18814f62018-02-22 16:02:49 -080065static inline int gralloc_buffer_attr_map(struct private_handle_t *hnd, int readwrite)
John Stultz16100f62017-05-03 11:12:18 -070066{
67 int rval = -1;
68 int prot_flags = PROT_READ;
69
John Stultz18814f62018-02-22 16:02:49 -080070 if (!hnd)
71 {
John Stultz16100f62017-05-03 11:12:18 -070072 goto out;
John Stultz18814f62018-02-22 16:02:49 -080073 }
John Stultz16100f62017-05-03 11:12:18 -070074
John Stultz18814f62018-02-22 16:02:49 -080075 if (hnd->share_attr_fd < 0)
John Stultz16100f62017-05-03 11:12:18 -070076 {
77 ALOGE("Shared attribute region not available to be mapped");
78 goto out;
79 }
80
John Stultz18814f62018-02-22 16:02:49 -080081 if (readwrite)
John Stultz16100f62017-05-03 11:12:18 -070082 {
John Stultz18814f62018-02-22 16:02:49 -080083 prot_flags |= PROT_WRITE;
John Stultz16100f62017-05-03 11:12:18 -070084 }
85
John Stultz18814f62018-02-22 16:02:49 -080086 hnd->attr_base = mmap(NULL, PAGE_SIZE, prot_flags, MAP_SHARED, hnd->share_attr_fd, 0);
87
88 if (hnd->attr_base == MAP_FAILED)
John Stultz16100f62017-05-03 11:12:18 -070089 {
John Stultz18814f62018-02-22 16:02:49 -080090 ALOGE("Failed to mmap shared attribute region err=%s", strerror(errno));
John Stultz16100f62017-05-03 11:12:18 -070091 goto out;
92 }
93
94 rval = 0;
95
96out:
97 return rval;
98}
99
100/*
101 * Unmap the attribute storage area when done with it.
102 *
103 * Return 0 on success.
104 */
John Stultz18814f62018-02-22 16:02:49 -0800105static inline int gralloc_buffer_attr_unmap(struct private_handle_t *hnd)
John Stultz16100f62017-05-03 11:12:18 -0700106{
107 int rval = -1;
108
John Stultz18814f62018-02-22 16:02:49 -0800109 if (!hnd)
John Stultz16100f62017-05-03 11:12:18 -0700110 {
John Stultz18814f62018-02-22 16:02:49 -0800111 goto out;
112 }
113
114 if (hnd->attr_base != MAP_FAILED)
115 {
116 if (munmap(hnd->attr_base, PAGE_SIZE) == 0)
John Stultz16100f62017-05-03 11:12:18 -0700117 {
118 hnd->attr_base = MAP_FAILED;
119 rval = 0;
120 }
121 }
122
123out:
124 return rval;
125}
126
127/*
128 * Read or write an attribute from/to the storage area.
129 *
130 * Return 0 on success.
131 */
John Stultz18814f62018-02-22 16:02:49 -0800132static inline int gralloc_buffer_attr_write(struct private_handle_t *hnd, buf_attr attr, int *val)
John Stultz16100f62017-05-03 11:12:18 -0700133{
134 int rval = -1;
135
John Stultz18814f62018-02-22 16:02:49 -0800136 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
John Stultz16100f62017-05-03 11:12:18 -0700137 {
John Stultz18814f62018-02-22 16:02:49 -0800138 goto out;
139 }
John Stultz16100f62017-05-03 11:12:18 -0700140
John Stultz18814f62018-02-22 16:02:49 -0800141 if (hnd->attr_base != MAP_FAILED)
142 {
143 attr_region *region = (attr_region *)hnd->attr_base;
144
145 switch (attr)
John Stultz16100f62017-05-03 11:12:18 -0700146 {
John Stultz18814f62018-02-22 16:02:49 -0800147 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
148 memcpy(&region->crop_top, val, sizeof(int) * 4);
149 rval = 0;
150 break;
John Stultz16100f62017-05-03 11:12:18 -0700151
John Stultz18814f62018-02-22 16:02:49 -0800152 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
153 region->use_yuv_transform = *val;
154 rval = 0;
155 break;
John Stultz16100f62017-05-03 11:12:18 -0700156
John Stultz18814f62018-02-22 16:02:49 -0800157 case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
158 region->use_sparse_alloc = *val;
159 rval = 0;
160 break;
161
162 case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
163 memcpy(&region->hdr_info, val, sizeof(mali_hdr_info));
164 rval = 0;
165 break;
John Stultz16100f62017-05-03 11:12:18 -0700166 }
167 }
168
169out:
170 return rval;
171}
172
John Stultz18814f62018-02-22 16:02:49 -0800173static inline int gralloc_buffer_attr_read(struct private_handle_t *hnd, buf_attr attr, int *val)
John Stultz16100f62017-05-03 11:12:18 -0700174{
175 int rval = -1;
176
John Stultz18814f62018-02-22 16:02:49 -0800177 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST)
John Stultz16100f62017-05-03 11:12:18 -0700178 {
John Stultz18814f62018-02-22 16:02:49 -0800179 goto out;
180 }
John Stultz16100f62017-05-03 11:12:18 -0700181
John Stultz18814f62018-02-22 16:02:49 -0800182 if (hnd->attr_base != MAP_FAILED)
183 {
184 attr_region *region = (attr_region *)hnd->attr_base;
185
186 switch (attr)
John Stultz16100f62017-05-03 11:12:18 -0700187 {
John Stultz18814f62018-02-22 16:02:49 -0800188 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT:
189 memcpy(val, &region->crop_top, sizeof(int) * 4);
190 rval = 0;
191 break;
John Stultz16100f62017-05-03 11:12:18 -0700192
John Stultz18814f62018-02-22 16:02:49 -0800193 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS:
194 *val = region->use_yuv_transform;
195 rval = 0;
196 break;
John Stultz16100f62017-05-03 11:12:18 -0700197
John Stultz18814f62018-02-22 16:02:49 -0800198 case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC:
199 *val = region->use_sparse_alloc;
200 rval = 0;
201 break;
202
203 case GRALLOC_ARM_BUFFER_ATTR_HDR_INFO:
204 memcpy(val, &region->hdr_info, sizeof(mali_hdr_info));
205 rval = 0;
206 break;
John Stultz16100f62017-05-03 11:12:18 -0700207 }
208 }
209
210out:
211 return rval;
212}
213
214#endif /* GRALLOC_BUFFER_PRIV_H_ */