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