John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1 | /* |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2014-2017 ARM Limited. All rights reserved. |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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> |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 25 | #include "mali_gralloc_private_interface_types.h" |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 38 | mali_hdr_info hdr_info; |
| 39 | } __attribute__((packed)); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 40 | |
| 41 | typedef struct attr_region attr_region; |
| 42 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Allocate shared memory for attribute storage. Only to be |
| 45 | * used by gralloc internally. |
| 46 | * |
| 47 | * Return 0 on success. |
| 48 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 49 | int gralloc_buffer_attr_allocate(struct private_handle_t *hnd); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 57 | int gralloc_buffer_attr_free(struct private_handle_t *hnd); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Map the attribute storage area before attempting to |
| 61 | * read/write from it. |
| 62 | * |
| 63 | * Return 0 on success. |
| 64 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 65 | static inline int gralloc_buffer_attr_map(struct private_handle_t *hnd, int readwrite) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 66 | { |
| 67 | int rval = -1; |
| 68 | int prot_flags = PROT_READ; |
| 69 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 70 | if (!hnd) |
| 71 | { |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 72 | goto out; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 73 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 74 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 75 | if (hnd->share_attr_fd < 0) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 76 | { |
| 77 | ALOGE("Shared attribute region not available to be mapped"); |
| 78 | goto out; |
| 79 | } |
| 80 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 81 | if (readwrite) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 82 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 83 | prot_flags |= PROT_WRITE; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 89 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 90 | ALOGE("Failed to mmap shared attribute region err=%s", strerror(errno)); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 105 | static inline int gralloc_buffer_attr_unmap(struct private_handle_t *hnd) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 106 | { |
| 107 | int rval = -1; |
| 108 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 109 | if (!hnd) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 110 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | if (hnd->attr_base != MAP_FAILED) |
| 115 | { |
| 116 | if (munmap(hnd->attr_base, PAGE_SIZE) == 0) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 132 | static inline int gralloc_buffer_attr_write(struct private_handle_t *hnd, buf_attr attr, int *val) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 133 | { |
| 134 | int rval = -1; |
| 135 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 136 | if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 137 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 138 | goto out; |
| 139 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 140 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 141 | if (hnd->attr_base != MAP_FAILED) |
| 142 | { |
| 143 | attr_region *region = (attr_region *)hnd->attr_base; |
| 144 | |
| 145 | switch (attr) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 146 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 147 | case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: |
| 148 | memcpy(®ion->crop_top, val, sizeof(int) * 4); |
| 149 | rval = 0; |
| 150 | break; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 151 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 152 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: |
| 153 | region->use_yuv_transform = *val; |
| 154 | rval = 0; |
| 155 | break; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 156 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | out: |
| 170 | return rval; |
| 171 | } |
| 172 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 173 | static inline int gralloc_buffer_attr_read(struct private_handle_t *hnd, buf_attr attr, int *val) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 174 | { |
| 175 | int rval = -1; |
| 176 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 177 | if (!hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 178 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 179 | goto out; |
| 180 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 181 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 182 | if (hnd->attr_base != MAP_FAILED) |
| 183 | { |
| 184 | attr_region *region = (attr_region *)hnd->attr_base; |
| 185 | |
| 186 | switch (attr) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 187 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 188 | case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: |
| 189 | memcpy(val, ®ion->crop_top, sizeof(int) * 4); |
| 190 | rval = 0; |
| 191 | break; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 192 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 193 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: |
| 194 | *val = region->use_yuv_transform; |
| 195 | rval = 0; |
| 196 | break; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 197 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame^] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | out: |
| 211 | return rval; |
| 212 | } |
| 213 | |
| 214 | #endif /* GRALLOC_BUFFER_PRIV_H_ */ |