1 /*
2 * Copyright (C) 2008 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
17 #include <limits.h>
18 #include <errno.h>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
29
30 #include <hardware/hardware.h>
31 #include <hardware/gralloc.h>
32
33 #include "gralloc_priv.h"
34
35
36 /* desktop Linux needs a little help with gettid() */
37 #if defined(ARCH_X86) && !defined(HAVE_ANDROID_OS)
38 #define __KERNEL__
39 # include <linux/unistd.h>
gettid()40 pid_t gettid() { return syscall(__NR_gettid);}
41 #undef __KERNEL__
42 #endif
43
44 /*****************************************************************************/
45
gralloc_map(gralloc_module_t const *,buffer_handle_t handle,void ** vaddr)46 static int gralloc_map(gralloc_module_t const* /*module*/,
47 buffer_handle_t handle,
48 void** vaddr)
49 {
50 private_handle_t* hnd = (private_handle_t*)handle;
51 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
52 size_t size = hnd->size;
53 void* mappedAddress = mmap(0, size,
54 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
55 if (mappedAddress == MAP_FAILED) {
56 ALOGE("Could not mmap %s", strerror(errno));
57 return -errno;
58 }
59 hnd->base = uintptr_t(mappedAddress) + hnd->offset;
60 //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
61 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
62 }
63 *vaddr = (void*)hnd->base;
64 return 0;
65 }
66
gralloc_unmap(gralloc_module_t const *,buffer_handle_t handle)67 static int gralloc_unmap(gralloc_module_t const* /*module*/,
68 buffer_handle_t handle)
69 {
70 private_handle_t* hnd = (private_handle_t*)handle;
71 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
72 void* base = (void*)hnd->base;
73 size_t size = hnd->size;
74 //ALOGD("unmapping from %p, size=%d", base, size);
75 if (munmap(base, size) < 0) {
76 ALOGE("Could not unmap %s", strerror(errno));
77 }
78 }
79 hnd->base = 0;
80 return 0;
81 }
82
83 /*****************************************************************************/
84
gralloc_register_buffer(gralloc_module_t const * module,buffer_handle_t handle)85 int gralloc_register_buffer(gralloc_module_t const* module,
86 buffer_handle_t handle)
87 {
88 if (private_handle_t::validate(handle) < 0)
89 return -EINVAL;
90
91 // *** WARNING WARNING WARNING ***
92 //
93 // If a buffer handle is passed from the process that allocated it to a
94 // different process, and then back to the allocator process, we will
95 // create a second mapping of the buffer. If the process reads and writes
96 // through both mappings, normal memory ordering guarantees may be
97 // violated, depending on the processor cache implementation*.
98 //
99 // If you are deriving a new gralloc implementation from this code, don't
100 // do this. A "real" gralloc should provide a single reference-counted
101 // mapping for each buffer in a process.
102 //
103 // In the current system, there is one case that needs a buffer to be
104 // registered in the same process that allocated it. The SurfaceFlinger
105 // process acts as the IGraphicBufferAlloc Binder provider, so all gralloc
106 // allocations happen in its process. After returning the buffer handle to
107 // the IGraphicBufferAlloc client, SurfaceFlinger free's its handle to the
108 // buffer (unmapping it from the SurfaceFlinger process). If
109 // SurfaceFlinger later acts as the producer end of the buffer queue the
110 // buffer belongs to, it will get a new handle to the buffer in response
111 // to IGraphicBufferProducer::requestBuffer(). Like any buffer handle
112 // received through Binder, the SurfaceFlinger process will register it.
113 // Since it already freed its original handle, it will only end up with
114 // one mapping to the buffer and there will be no problem.
115 //
116 // Currently SurfaceFlinger only acts as a buffer producer for a remote
117 // consumer when taking screenshots and when using virtual displays.
118 //
119 // Eventually, each application should be allowed to make its own gralloc
120 // allocations, solving the problem. Also, this ashmem-based gralloc
121 // should go away, replaced with a real ion-based gralloc.
122 //
123 // * Specifically, associative virtually-indexed caches are likely to have
124 // problems. Most modern L1 caches fit that description.
125
126 private_handle_t* hnd = (private_handle_t*)handle;
127 ALOGD_IF(hnd->pid == getpid(),
128 "Registering a buffer in the process that created it. "
129 "This may cause memory ordering problems.");
130
131 void *vaddr;
132 return gralloc_map(module, handle, &vaddr);
133 }
134
gralloc_unregister_buffer(gralloc_module_t const * module,buffer_handle_t handle)135 int gralloc_unregister_buffer(gralloc_module_t const* module,
136 buffer_handle_t handle)
137 {
138 if (private_handle_t::validate(handle) < 0)
139 return -EINVAL;
140
141 private_handle_t* hnd = (private_handle_t*)handle;
142 if (hnd->base)
143 gralloc_unmap(module, handle);
144
145 return 0;
146 }
147
mapBuffer(gralloc_module_t const * module,private_handle_t * hnd)148 int mapBuffer(gralloc_module_t const* module,
149 private_handle_t* hnd)
150 {
151 void* vaddr;
152 return gralloc_map(module, hnd, &vaddr);
153 }
154
terminateBuffer(gralloc_module_t const * module,private_handle_t * hnd)155 int terminateBuffer(gralloc_module_t const* module,
156 private_handle_t* hnd)
157 {
158 if (hnd->base) {
159 // this buffer was mapped, unmap it now
160 gralloc_unmap(module, hnd);
161 }
162
163 return 0;
164 }
165
gralloc_lock(gralloc_module_t const *,buffer_handle_t handle,int,int,int,int,int,void ** vaddr)166 int gralloc_lock(gralloc_module_t const* /*module*/,
167 buffer_handle_t handle, int /*usage*/,
168 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
169 void** vaddr)
170 {
171 // this is called when a buffer is being locked for software
172 // access. in thin implementation we have nothing to do since
173 // not synchronization with the h/w is needed.
174 // typically this is used to wait for the h/w to finish with
175 // this buffer if relevant. the data cache may need to be
176 // flushed or invalidated depending on the usage bits and the
177 // hardware.
178
179 if (private_handle_t::validate(handle) < 0)
180 return -EINVAL;
181
182 private_handle_t* hnd = (private_handle_t*)handle;
183 *vaddr = (void*)hnd->base;
184 return 0;
185 }
186
gralloc_unlock(gralloc_module_t const *,buffer_handle_t handle)187 int gralloc_unlock(gralloc_module_t const* /*module*/,
188 buffer_handle_t handle)
189 {
190 // we're done with a software buffer. nothing to do in this
191 // implementation. typically this is used to flush the data cache.
192
193 if (private_handle_t::validate(handle) < 0)
194 return -EINVAL;
195 return 0;
196 }
197