1 /* 2 * Copyright (C) 2014-2017 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 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> 25 #include "mali_gralloc_private_interface_types.h" 26 27 // private gralloc buffer manipulation API 28 29 struct 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; 38 mali_hdr_info hdr_info; 39 } __attribute__((packed)); 40 41 typedef struct attr_region attr_region; 42 43 /* 44 * Allocate shared memory for attribute storage. Only to be 45 * used by gralloc internally. 46 * 47 * Return 0 on success. 48 */ 49 int gralloc_buffer_attr_allocate(struct private_handle_t *hnd); 50 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 */ 57 int gralloc_buffer_attr_free(struct private_handle_t *hnd); 58 59 /* 60 * Map the attribute storage area before attempting to 61 * read/write from it. 62 * 63 * Return 0 on success. 64 */ 65 static inline int gralloc_buffer_attr_map(struct private_handle_t *hnd, int readwrite) 66 { 67 int rval = -1; 68 int prot_flags = PROT_READ; 69 70 if (!hnd) 71 { 72 goto out; 73 } 74 75 if (hnd->share_attr_fd < 0) 76 { 77 ALOGE("Shared attribute region not available to be mapped"); 78 goto out; 79 } 80 81 if (readwrite) 82 { 83 prot_flags |= PROT_WRITE; 84 } 85 86 hnd->attr_base = mmap(NULL, PAGE_SIZE, prot_flags, MAP_SHARED, hnd->share_attr_fd, 0); 87 88 if (hnd->attr_base == MAP_FAILED) 89 { 90 ALOGE("Failed to mmap shared attribute region err=%s", strerror(errno)); 91 goto out; 92 } 93 94 rval = 0; 95 96 out: 97 return rval; 98 } 99 100 /* 101 * Unmap the attribute storage area when done with it. 102 * 103 * Return 0 on success. 104 */ 105 static inline int gralloc_buffer_attr_unmap(struct private_handle_t *hnd) 106 { 107 int rval = -1; 108 109 if (!hnd) 110 { 111 goto out; 112 } 113 114 if (hnd->attr_base != MAP_FAILED) 115 { 116 if (munmap(hnd->attr_base, PAGE_SIZE) == 0) 117 { 118 hnd->attr_base = MAP_FAILED; 119 rval = 0; 120 } 121 } 122 123 out: 124 return rval; 125 } 126 127 /* 128 * Read or write an attribute from/to the storage area. 129 * 130 * Return 0 on success. 131 */ 132 static inline int gralloc_buffer_attr_write(struct private_handle_t *hnd, buf_attr attr, int *val) 133 { 134 int rval = -1; 135 136 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST) 137 { 138 goto out; 139 } 140 141 if (hnd->attr_base != MAP_FAILED) 142 { 143 attr_region *region = (attr_region *)hnd->attr_base; 144 145 switch (attr) 146 { 147 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: 148 memcpy(®ion->crop_top, val, sizeof(int) * 4); 149 rval = 0; 150 break; 151 152 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: 153 region->use_yuv_transform = *val; 154 rval = 0; 155 break; 156 157 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(®ion->hdr_info, val, sizeof(mali_hdr_info)); 164 rval = 0; 165 break; 166 } 167 } 168 169 out: 170 return rval; 171 } 172 173 static inline int gralloc_buffer_attr_read(struct private_handle_t *hnd, buf_attr attr, int *val) 174 { 175 int rval = -1; 176 177 if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST) 178 { 179 goto out; 180 } 181 182 if (hnd->attr_base != MAP_FAILED) 183 { 184 attr_region *region = (attr_region *)hnd->attr_base; 185 186 switch (attr) 187 { 188 case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: 189 memcpy(val, ®ion->crop_top, sizeof(int) * 4); 190 rval = 0; 191 break; 192 193 case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: 194 *val = region->use_yuv_transform; 195 rval = 0; 196 break; 197 198 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, ®ion->hdr_info, sizeof(mali_hdr_info)); 205 rval = 0; 206 break; 207 } 208 } 209 210 out: 211 return rval; 212 } 213 214 #endif /* GRALLOC_BUFFER_PRIV_H_ */ 215