1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <limits.h>
17 #include <errno.h>
18 #include <pthread.h>
19 #include <unistd.h>
20 #include <string.h>
21 
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 
26 #include <cutils/hashmap.h>
27 #include <log/log.h>
28 #include <cutils/atomic.h>
29 
30 #include <hardware/hardware.h>
31 #include <hardware/gralloc.h>
32 #include <system/graphics.h>
33 
34 #include "gralloc_vsoc_priv.h"
35 #include "region_registry.h"
36 
37 #define DEBUG_REFERENCES 1
38 #define DEBUG_MAX_LOCK_LEVEL 20
39 
40 /*****************************************************************************/
41 
gralloc_register_buffer(gralloc_module_t const *,buffer_handle_t handle)42 int gralloc_register_buffer(gralloc_module_t const* /*module*/,
43                             buffer_handle_t handle) {
44   if (private_handle_t::validate(handle) < 0) {
45     return -EINVAL;
46   }
47 
48   private_handle_t* hnd = (private_handle_t*)handle;
49   if (reference_region(__FUNCTION__, hnd)) {
50     return 0;
51   } else {
52     return -EIO;
53   }
54 }
55 
gralloc_unregister_buffer(gralloc_module_t const *,buffer_handle_t handle)56 int gralloc_unregister_buffer(gralloc_module_t const* /*module*/,
57                               buffer_handle_t handle) {
58   if (private_handle_t::validate(handle) < 0) {
59     return -EINVAL;
60   }
61   private_handle_t* hnd = (private_handle_t*)handle;
62   return unreference_region("gralloc_unregister_buffer", hnd);
63 }
64 
gralloc_lock(gralloc_module_t const *,buffer_handle_t handle,int,int,int,int,int,void ** vaddr)65 int gralloc_lock(
66     gralloc_module_t const* /*module*/, buffer_handle_t handle, int /*usage*/,
67     int /*l*/, int /*t*/, int /*w*/, int /*h*/,
68     void** vaddr) {
69   if (private_handle_t::validate(handle) < 0) {
70     return -EINVAL;
71   }
72   if (!vaddr) {
73     return -EINVAL;
74   }
75   private_handle_t* hnd = (private_handle_t*)handle;
76 #if DEBUG_REFERENCES
77   if (hnd->lock_level > DEBUG_MAX_LOCK_LEVEL) {
78     LOG_FATAL("%s: unbalanced lock detected. lock level = %d",
79               __FUNCTION__, hnd->lock_level);
80   }
81   ++hnd->lock_level;
82 #endif
83   void* base = reference_region("gralloc_lock", hnd);
84   *vaddr = reinterpret_cast<unsigned char*>(base)
85       + hnd->frame_offset;
86   return 0;
87 }
88 
gralloc_unlock(gralloc_module_t const *,buffer_handle_t handle)89 int gralloc_unlock(
90     gralloc_module_t const* /*module*/, buffer_handle_t handle) {
91   if (private_handle_t::validate(handle) < 0) {
92     return -EINVAL;
93   }
94   private_handle_t* hnd = (private_handle_t*) handle;
95 #if DEBUG_REFERENCES
96   if (hnd->lock_level <= 0) {
97     LOG_FATAL("%s unbalanced unlock detected. lock level = %d",
98               __FUNCTION__, hnd->lock_level);
99   }
100   --hnd->lock_level;
101 #endif
102   unreference_region("gralloc_unlock", hnd);
103   return 0;
104 }
105 
gralloc_lock_ycbcr(gralloc_module_t const *,buffer_handle_t handle,int,int,int,int,int,struct android_ycbcr * ycbcr)106 int gralloc_lock_ycbcr(
107     gralloc_module_t const* /*module*/, buffer_handle_t handle, int /*usage*/,
108     int /*l*/, int /*t*/, int /*w*/, int /*h*/,
109     struct android_ycbcr* ycbcr) {
110   if (private_handle_t::validate(handle) < 0) {
111     return -EINVAL;
112   }
113   private_handle_t* hnd = (private_handle_t*)handle;
114 #if DEBUG_REFERENCES
115   if (hnd->lock_level > DEBUG_MAX_LOCK_LEVEL) {
116     LOG_FATAL("%s: unbalanced lock detected. lock level = %d",
117               __FUNCTION__, hnd->lock_level);
118   }
119   ++hnd->lock_level;
120 #endif
121   void* base = reference_region("gralloc_lock_ycbcr", hnd);
122   formatToYcbcr(hnd->format, hnd->x_res, hnd->y_res, base, ycbcr);
123   return 0;
124 }
125 
gralloc_get_transport_size(struct gralloc_module_t const *,buffer_handle_t handle,uint32_t * outNumFds,uint32_t * outNumInts)126 int32_t gralloc_get_transport_size(struct gralloc_module_t const* /*module*/,
127                                    buffer_handle_t handle,
128                                    uint32_t *outNumFds,
129                                    uint32_t *outNumInts) {
130   if (private_handle_t::validate(handle) < 0) {
131     return 2; // GRALLOC1_ERROR_BAD_HANDLE
132   }
133   private_handle_t* hnd = (private_handle_t*)handle;
134   *outNumFds = hnd->numFds;
135   *outNumInts = hnd->numInts;
136   return 0;
137 }
138 
gralloc_validate_buffer_size(struct gralloc_module_t const *,buffer_handle_t handle,uint32_t w,uint32_t h,int32_t format,int,uint32_t stride)139 int32_t gralloc_validate_buffer_size(struct gralloc_module_t const* /*device*/,
140                                      buffer_handle_t handle,
141                                      uint32_t w,
142                                      uint32_t h,
143                                      int32_t format,
144                                      int /*usage*/,
145                                      uint32_t stride) {
146   if (private_handle_t::validate(handle) < 0) {
147     return 2; // GRALLOC1_ERROR_BAD_HANDLE
148   }
149   private_handle_t* hnd = (private_handle_t*)handle;
150   if (format != hnd->format ||
151       w > hnd->x_res ||
152       h > hnd->y_res ||
153       stride > hnd->stride_in_pixels) {
154     return 3; // GRALLOC1_ERROR_BAD_VALUE
155   }
156   return 0;
157 }
158