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