blob: 516781cb3133f0b650b148275a9bf9ddce3e4b82
1 | /* |
2 | * Copyright (C) 2014 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 | |
26 | // private gralloc buffer manipulation API |
27 | |
28 | struct attr_region |
29 | { |
30 | /* Rectangle to be cropped from the full frame (Origin in top-left corner!) */ |
31 | int crop_top; |
32 | int crop_left; |
33 | int crop_height; |
34 | int crop_width; |
35 | int use_yuv_transform; |
36 | int use_sparse_alloc; |
37 | } __attribute__ ((packed)); |
38 | |
39 | typedef struct attr_region attr_region; |
40 | |
41 | enum |
42 | { |
43 | /* CROP_RECT and YUV_TRANS are intended to be |
44 | * written by producers and read by consumers. |
45 | * A producer should write these parameters before |
46 | * it queues a buffer to the consumer. |
47 | */ |
48 | |
49 | /* CROP RECT, defined as an int array of top, left, height, width. Origin in top-left corner */ |
50 | GRALLOC_ARM_BUFFER_ATTR_CROP_RECT = 1, |
51 | |
52 | /* Set if the AFBC format used a YUV transform before compressing */ |
53 | GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS = 2, |
54 | |
55 | /* Set if the AFBC format uses sparse allocation */ |
56 | GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC = 3, |
57 | GRALLOC_ARM_BUFFER_ATTR_LAST |
58 | }; |
59 | |
60 | typedef uint32_t buf_attr; |
61 | |
62 | /* |
63 | * Allocate shared memory for attribute storage. Only to be |
64 | * used by gralloc internally. |
65 | * |
66 | * Return 0 on success. |
67 | */ |
68 | int gralloc_buffer_attr_allocate( struct private_handle_t *hnd ); |
69 | |
70 | /* |
71 | * Frees the shared memory allocated for attribute storage. |
72 | * Only to be used by gralloc internally. |
73 | |
74 | * Return 0 on success. |
75 | */ |
76 | int gralloc_buffer_attr_free( struct private_handle_t *hnd ); |
77 | |
78 | /* |
79 | * Map the attribute storage area before attempting to |
80 | * read/write from it. |
81 | * |
82 | * Return 0 on success. |
83 | */ |
84 | static inline int gralloc_buffer_attr_map( struct private_handle_t *hnd, int readwrite) |
85 | { |
86 | int rval = -1; |
87 | int prot_flags = PROT_READ; |
88 | |
89 | if( !hnd ) |
90 | goto out; |
91 | |
92 | if( hnd->share_attr_fd < 0 ) |
93 | { |
94 | ALOGE("Shared attribute region not available to be mapped"); |
95 | goto out; |
96 | } |
97 | |
98 | if( readwrite ) |
99 | { |
100 | prot_flags |= PROT_WRITE; |
101 | } |
102 | |
103 | hnd->attr_base = mmap( NULL, PAGE_SIZE, prot_flags, MAP_SHARED, hnd->share_attr_fd, 0 ); |
104 | if(hnd->attr_base == MAP_FAILED) |
105 | { |
106 | ALOGE("Failed to mmap shared attribute region err=%s",strerror(errno)); |
107 | goto out; |
108 | } |
109 | |
110 | rval = 0; |
111 | |
112 | out: |
113 | return rval; |
114 | } |
115 | |
116 | /* |
117 | * Unmap the attribute storage area when done with it. |
118 | * |
119 | * Return 0 on success. |
120 | */ |
121 | static inline int gralloc_buffer_attr_unmap( struct private_handle_t *hnd ) |
122 | { |
123 | int rval = -1; |
124 | |
125 | if( !hnd ) |
126 | goto out; |
127 | |
128 | if( hnd->attr_base != MAP_FAILED ) |
129 | { |
130 | if ( munmap( hnd->attr_base, PAGE_SIZE ) == 0 ) |
131 | { |
132 | hnd->attr_base = MAP_FAILED; |
133 | rval = 0; |
134 | } |
135 | } |
136 | |
137 | out: |
138 | return rval; |
139 | } |
140 | |
141 | /* |
142 | * Read or write an attribute from/to the storage area. |
143 | * |
144 | * Return 0 on success. |
145 | */ |
146 | static inline int gralloc_buffer_attr_write( struct private_handle_t *hnd, buf_attr attr, int *val ) |
147 | { |
148 | int rval = -1; |
149 | |
150 | if( !hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST ) |
151 | goto out; |
152 | |
153 | if( hnd->attr_base != MAP_FAILED ) |
154 | { |
155 | attr_region *region = (attr_region *) hnd->attr_base; |
156 | |
157 | switch( attr ) |
158 | { |
159 | case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: |
160 | memcpy( ®ion->crop_top, val, sizeof(int)*4 ); |
161 | rval = 0; |
162 | break; |
163 | |
164 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: |
165 | region->use_yuv_transform = *val; |
166 | rval = 0; |
167 | break; |
168 | |
169 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC: |
170 | region->use_sparse_alloc = *val; |
171 | rval = 0; |
172 | break; |
173 | } |
174 | } |
175 | |
176 | out: |
177 | return rval; |
178 | } |
179 | |
180 | static inline int gralloc_buffer_attr_read( struct private_handle_t *hnd, buf_attr attr, int *val ) |
181 | { |
182 | int rval = -1; |
183 | |
184 | if( !hnd || !val || attr >= GRALLOC_ARM_BUFFER_ATTR_LAST ) |
185 | goto out; |
186 | |
187 | if( hnd->attr_base != MAP_FAILED ) |
188 | { |
189 | attr_region *region = (attr_region *) hnd->attr_base; |
190 | |
191 | switch( attr ) |
192 | { |
193 | case GRALLOC_ARM_BUFFER_ATTR_CROP_RECT: |
194 | memcpy( val, ®ion->crop_top, sizeof(int)*4 ); |
195 | rval = 0; |
196 | break; |
197 | |
198 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_YUV_TRANS: |
199 | *val = region->use_yuv_transform; |
200 | rval = 0; |
201 | break; |
202 | |
203 | case GRALLOC_ARM_BUFFER_ATTR_AFBC_SPARSE_ALLOC: |
204 | *val = region->use_sparse_alloc; |
205 | rval = 0; |
206 | break; |
207 | } |
208 | } |
209 | |
210 | out: |
211 | return rval; |
212 | } |
213 | |
214 | #endif /* GRALLOC_BUFFER_PRIV_H_ */ |
215 |