1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
19 #include <limits.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdarg.h>
25 
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 
31 #include <cutils/log.h>
32 #include <cutils/atomic.h>
33 #include <utils/Trace.h>
34 
35 #include <hardware/hardware.h>
36 #include <hardware/gralloc.h>
37 
38 #include "gralloc_priv.h"
39 #include "gr.h"
40 #include "alloc_controller.h"
41 #include "memalloc.h"
42 #include <qdMetaData.h>
43 
44 
45 using namespace gralloc;
46 /*****************************************************************************/
47 
48 // Return the type of allocator -
49 // these are used for mapping/unmapping
getAllocator(int flags)50 static IMemAlloc* getAllocator(int flags)
51 {
52     IMemAlloc* memalloc;
53     IAllocController* alloc_ctrl = IAllocController::getInstance();
54     memalloc = alloc_ctrl->getAllocator(flags);
55     return memalloc;
56 }
57 
gralloc_map_metadata(buffer_handle_t handle)58 static int gralloc_map_metadata(buffer_handle_t handle) {
59     private_handle_t* hnd = (private_handle_t*)handle;
60     hnd->base_metadata = 0;
61     IMemAlloc* memalloc = getAllocator(hnd->flags) ;
62     void *mappedAddress = MAP_FAILED;
63     unsigned int size = 0;
64     if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
65         mappedAddress = MAP_FAILED;
66         size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
67         int ret = memalloc->map_buffer(&mappedAddress, size,
68                                        hnd->offset_metadata, hnd->fd_metadata);
69         if(ret || mappedAddress == MAP_FAILED) {
70             ALOGE("Could not mmap metadata for handle %p, fd=%d (%s)",
71                   hnd, hnd->fd_metadata, strerror(errno));
72             return -errno;
73         }
74         hnd->base_metadata = uint64_t(mappedAddress) + hnd->offset_metadata;
75     }
76     return 0;
77 }
78 
gralloc_map(gralloc_module_t const * module,buffer_handle_t handle)79 static int gralloc_map(gralloc_module_t const* module,
80                        buffer_handle_t handle)
81 {
82     ATRACE_CALL();
83     if(!module)
84         return -EINVAL;
85 
86     private_handle_t* hnd = (private_handle_t*)handle;
87     unsigned int size = 0;
88     int err = 0;
89     IMemAlloc* memalloc = getAllocator(hnd->flags) ;
90     void *mappedAddress = MAP_FAILED;
91     hnd->base = 0;
92 
93     // Dont map framebuffer and secure buffers
94     if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
95         !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
96         size = hnd->size;
97         err = memalloc->map_buffer(&mappedAddress, size,
98                                        hnd->offset, hnd->fd);
99         if(err || mappedAddress == MAP_FAILED) {
100             ALOGE("Could not mmap handle %p, fd=%d (%s)",
101                   handle, hnd->fd, strerror(errno));
102             return -errno;
103         }
104 
105         hnd->base = uint64_t(mappedAddress) + hnd->offset;
106     } else {
107         // Cannot map secure buffers or framebuffers, but still need to map
108         // metadata for secure buffers.
109         // If mapping a secure buffers fails, the framework needs to get
110         // an error code.
111         err = -EINVAL;
112     }
113 
114     //Allow mapping of metadata for all buffers including secure ones, but not
115     //of framebuffer
116     int metadata_err = gralloc_map_metadata(handle);
117     if(!err) {
118         err = metadata_err;
119     }
120     return err;
121 }
122 
gralloc_unmap(gralloc_module_t const * module,buffer_handle_t handle)123 static int gralloc_unmap(gralloc_module_t const* module,
124                          buffer_handle_t handle)
125 {
126     ATRACE_CALL();
127     int err = -EINVAL;
128     if(!module)
129         return err;
130 
131     private_handle_t* hnd = (private_handle_t*)handle;
132     IMemAlloc* memalloc = getAllocator(hnd->flags) ;
133     if(!memalloc)
134         return err;
135 
136     if(hnd->base) {
137         err = memalloc->unmap_buffer((void*)hnd->base, hnd->size, hnd->offset);
138         if (err) {
139             ALOGE("Could not unmap memory at address %p, %s", hnd->base,
140                     strerror(errno));
141             return -errno;
142         }
143         hnd->base = 0;
144     }
145 
146     if(hnd->base_metadata) {
147         unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
148         err = memalloc->unmap_buffer((void*)hnd->base_metadata,
149                 size, hnd->offset_metadata);
150         if (err) {
151             ALOGE("Could not unmap memory at address %p, %s",
152                     hnd->base_metadata, strerror(errno));
153             return -errno;
154         }
155         hnd->base_metadata = 0;
156     }
157 
158     return 0;
159 }
160 
161 /*****************************************************************************/
162 
163 static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
164 
165 /*****************************************************************************/
166 
gralloc_register_buffer(gralloc_module_t const * module,buffer_handle_t handle)167 int gralloc_register_buffer(gralloc_module_t const* module,
168                             buffer_handle_t handle)
169 {
170     ATRACE_CALL();
171     if (!module || private_handle_t::validate(handle) < 0)
172         return -EINVAL;
173     // The base address received via IPC is invalid in this process
174     // Reset it to 0 here since it will be mapped in lock()
175     private_handle_t* hnd = (private_handle_t*)handle;
176     hnd->base = 0;
177     return gralloc_map_metadata(handle);
178 }
179 
gralloc_unregister_buffer(gralloc_module_t const * module,buffer_handle_t handle)180 int gralloc_unregister_buffer(gralloc_module_t const* module,
181                               buffer_handle_t handle)
182 {
183     ATRACE_CALL();
184     if (!module || private_handle_t::validate(handle) < 0)
185         return -EINVAL;
186 
187     /*
188      * If the buffer has been mapped during a lock operation, it's time
189      * to un-map it. It's an error to be here with a locked buffer.
190      * NOTE: the framebuffer is handled differently and is never unmapped.
191      * Also base and base_metadata are reset.
192      */
193     return gralloc_unmap(module, handle);
194 }
195 
terminateBuffer(gralloc_module_t const * module,private_handle_t * hnd)196 int terminateBuffer(gralloc_module_t const* module,
197                     private_handle_t* hnd)
198 {
199     ATRACE_CALL();
200     if(!module)
201         return -EINVAL;
202 
203     /*
204      * If the buffer has been mapped during a lock operation, it's time
205      * to un-map it. It's an error to be here with a locked buffer.
206      * NOTE: the framebuffer is handled differently and is never unmapped.
207      * Also base and base_metadata are reset.
208      */
209     return gralloc_unmap(module, hnd);
210 }
211 
gralloc_map_and_invalidate(gralloc_module_t const * module,buffer_handle_t handle,int usage)212 static int gralloc_map_and_invalidate (gralloc_module_t const* module,
213                                        buffer_handle_t handle, int usage)
214 {
215     ATRACE_CALL();
216     if (!module || private_handle_t::validate(handle) < 0)
217         return -EINVAL;
218 
219     int err = 0;
220     private_handle_t* hnd = (private_handle_t*)handle;
221     if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
222         if (hnd->base == 0) {
223             // we need to map for real
224             pthread_mutex_t* const lock = &sMapLock;
225             pthread_mutex_lock(lock);
226             err = gralloc_map(module, handle);
227             pthread_mutex_unlock(lock);
228         }
229         if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and
230                 hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) {
231             //Invalidate if CPU reads in software and there are non-CPU
232             //writers. No need to do this for the metadata buffer as it is
233             //only read/written in software.
234             if ((usage & GRALLOC_USAGE_SW_READ_MASK) and
235                     (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER))
236             {
237                 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
238                 err = memalloc->clean_buffer((void*)hnd->base,
239                         hnd->size, hnd->offset, hnd->fd,
240                         CACHE_INVALIDATE);
241             }
242             //Mark the buffer to be flushed after CPU write.
243             if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
244                 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
245             }
246         }
247     }
248 
249     return err;
250 }
251 
gralloc_lock(gralloc_module_t const * module,buffer_handle_t handle,int usage,int,int,int,int,void ** vaddr)252 int gralloc_lock(gralloc_module_t const* module,
253                  buffer_handle_t handle, int usage,
254                  int /*l*/, int /*t*/, int /*w*/, int /*h*/,
255                  void** vaddr)
256 {
257     ATRACE_CALL();
258     private_handle_t* hnd = (private_handle_t*)handle;
259     int err = gralloc_map_and_invalidate(module, handle, usage);
260     if(!err)
261         *vaddr = (void*)hnd->base;
262     return err;
263 }
264 
gralloc_lock_ycbcr(gralloc_module_t const * module,buffer_handle_t handle,int usage,int,int,int,int,struct android_ycbcr * ycbcr)265 int gralloc_lock_ycbcr(gralloc_module_t const* module,
266                  buffer_handle_t handle, int usage,
267                  int /*l*/, int /*t*/, int /*w*/, int /*h*/,
268                  struct android_ycbcr *ycbcr)
269 {
270     ATRACE_CALL();
271     private_handle_t* hnd = (private_handle_t*)handle;
272     int err = gralloc_map_and_invalidate(module, handle, usage);
273     if(!err)
274         err = getYUVPlaneInfo(hnd, ycbcr);
275     return err;
276 }
277 
gralloc_unlock(gralloc_module_t const * module,buffer_handle_t handle)278 int gralloc_unlock(gralloc_module_t const* module,
279                    buffer_handle_t handle)
280 {
281     ATRACE_CALL();
282     if (!module || private_handle_t::validate(handle) < 0)
283         return -EINVAL;
284 
285     int err = 0;
286     private_handle_t* hnd = (private_handle_t*)handle;
287 
288     IMemAlloc* memalloc = getAllocator(hnd->flags);
289     if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
290         err = memalloc->clean_buffer((void*)hnd->base,
291                 hnd->size, hnd->offset, hnd->fd,
292                 CACHE_CLEAN);
293         hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
294     }
295 
296     return err;
297 }
298 
299 /*****************************************************************************/
300 
gralloc_perform(struct gralloc_module_t const * module,int operation,...)301 int gralloc_perform(struct gralloc_module_t const* module,
302                     int operation, ... )
303 {
304     int res = -EINVAL;
305     va_list args;
306     if(!module)
307         return res;
308 
309     va_start(args, operation);
310     switch (operation) {
311         case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
312             {
313                 int fd = va_arg(args, int);
314                 unsigned int size = va_arg(args, unsigned int);
315                 unsigned int offset = va_arg(args, unsigned int);
316                 void* base = va_arg(args, void*);
317                 int width = va_arg(args, int);
318                 int height = va_arg(args, int);
319                 int format = va_arg(args, int);
320 
321                 native_handle_t** handle = va_arg(args, native_handle_t**);
322                 private_handle_t* hnd = (private_handle_t*)native_handle_create(
323                     private_handle_t::sNumFds, private_handle_t::sNumInts());
324                 hnd->magic = private_handle_t::sMagic;
325                 hnd->fd = fd;
326                 hnd->flags =  private_handle_t::PRIV_FLAGS_USES_ION;
327                 hnd->size = size;
328                 hnd->offset = offset;
329                 hnd->base = uint64_t(base) + offset;
330                 hnd->gpuaddr = 0;
331                 hnd->width = width;
332                 hnd->height = height;
333                 hnd->format = format;
334                 *handle = (native_handle_t *)hnd;
335                 res = 0;
336                 break;
337 
338             }
339         case GRALLOC_MODULE_PERFORM_GET_STRIDE:
340             {
341                 int width   = va_arg(args, int);
342                 int format  = va_arg(args, int);
343                 int *stride = va_arg(args, int *);
344                 int alignedw = 0, alignedh = 0;
345                 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
346                         0, format, 0, alignedw, alignedh);
347                 *stride = alignedw;
348                 res = 0;
349             } break;
350 
351         case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE:
352             {
353                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
354                 int *stride = va_arg(args, int *);
355                 if (private_handle_t::validate(hnd)) {
356                     return res;
357                 }
358                 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
359                 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
360                     *stride = metadata->bufferDim.sliceWidth;
361                 } else {
362                     *stride = hnd->width;
363                 }
364                 res = 0;
365             } break;
366 
367         case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE:
368             {
369                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
370                 int *stride = va_arg(args, int *);
371                 int *height = va_arg(args, int *);
372                 if (private_handle_t::validate(hnd)) {
373                     return res;
374                 }
375                 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
376                 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
377                     *stride = metadata->bufferDim.sliceWidth;
378                     *height = metadata->bufferDim.sliceHeight;
379                 } else {
380                     *stride = hnd->width;
381                     *height = hnd->height;
382                 }
383                 res = 0;
384             } break;
385 
386         case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES:
387             {
388                 int width   = va_arg(args, int);
389                 int height  = va_arg(args, int);
390                 int format  = va_arg(args, int);
391                 int usage   = va_arg(args, int);
392                 int *alignedWidth = va_arg(args, int *);
393                 int *alignedHeight = va_arg(args, int *);
394                 int *tileEnabled = va_arg(args,int *);
395                 *tileEnabled = isMacroTileEnabled(format, usage);
396                 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
397                         height, format, usage, *alignedWidth, *alignedHeight);
398                 res = 0;
399             } break;
400 
401         case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE:
402             {
403                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
404                 int *color_space = va_arg(args, int *);
405                 if (private_handle_t::validate(hnd)) {
406                     return res;
407                 }
408                 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
409                 if(metadata && metadata->operation & UPDATE_COLOR_SPACE) {
410                     *color_space = metadata->colorSpace;
411                     res = 0;
412                 }
413             } break;
414 
415         case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO:
416             {
417                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
418                 android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *);
419                 if (!private_handle_t::validate(hnd)) {
420                     res = getYUVPlaneInfo(hnd, ycbcr);
421                 }
422             } break;
423 
424         case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO:
425             {
426                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
427                 int *map_secure_buffer = va_arg(args, int *);
428                 if (private_handle_t::validate(hnd)) {
429                     return res;
430                 }
431                 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
432                 if(metadata && metadata->operation & MAP_SECURE_BUFFER) {
433                     *map_secure_buffer = metadata->mapSecureBuffer;
434                     res = 0;
435                 } else {
436                     *map_secure_buffer = 0;
437                 }
438             } break;
439 
440         default:
441             break;
442     }
443     va_end(args);
444     return res;
445 }
446