blob: 20d98418e5a0e62fc44ed19a6a70639eb4ef31b0 [file] [log] [blame]
Mathias Agopiana8a75162009-04-10 14:24:31 -07001/*
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>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070020#include <unistd.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070021#include <string.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070022
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
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070035
Mathias Agopiana8a75162009-04-10 14:24:31 -070036/*****************************************************************************/
37
Colin Crossfebaaa92014-02-05 18:21:09 -080038static int gralloc_map(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -070039 buffer_handle_t handle,
40 void** vaddr)
41{
Mathias Agopian51156652009-06-09 18:55:49 -070042 private_handle_t* hnd = (private_handle_t*)handle;
43 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070044 size_t size = hnd->size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070045 void* mappedAddress = mmap(0, size,
46 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
Mathias Agopian51156652009-06-09 18:55:49 -070047 if (mappedAddress == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +000048 ALOGE("Could not mmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070049 return -errno;
50 }
Colin Crossfebaaa92014-02-05 18:21:09 -080051 hnd->base = uintptr_t(mappedAddress) + hnd->offset;
Steve Blockcee85012011-12-20 16:25:12 +000052 //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070053 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
Mathias Agopiana8a75162009-04-10 14:24:31 -070054 }
Mathias Agopian51156652009-06-09 18:55:49 -070055 *vaddr = (void*)hnd->base;
56 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070057}
58
Colin Crossfebaaa92014-02-05 18:21:09 -080059static int gralloc_unmap(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -070060 buffer_handle_t handle)
61{
Mathias Agopian51156652009-06-09 18:55:49 -070062 private_handle_t* hnd = (private_handle_t*)handle;
63 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopianbd80b382009-07-07 17:53:43 -070064 void* base = (void*)hnd->base;
65 size_t size = hnd->size;
Steve Blockcee85012011-12-20 16:25:12 +000066 //ALOGD("unmapping from %p, size=%d", base, size);
Mathias Agopianbd80b382009-07-07 17:53:43 -070067 if (munmap(base, size) < 0) {
Steve Block60d056b2012-01-08 10:17:53 +000068 ALOGE("Could not unmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070069 }
Mathias Agopiana8a75162009-04-10 14:24:31 -070070 }
Mathias Agopian51156652009-06-09 18:55:49 -070071 hnd->base = 0;
72 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070073}
74
Mathias Agopian51156652009-06-09 18:55:49 -070075/*****************************************************************************/
76
Mathias Agopian988b8bd2009-05-04 14:26:56 -070077int gralloc_register_buffer(gralloc_module_t const* module,
78 buffer_handle_t handle)
79{
80 if (private_handle_t::validate(handle) < 0)
81 return -EINVAL;
Mathias Agopian51156652009-06-09 18:55:49 -070082
Jesse Hall0d8f81a2013-03-28 16:51:25 -070083 // *** WARNING WARNING WARNING ***
84 //
85 // If a buffer handle is passed from the process that allocated it to a
86 // different process, and then back to the allocator process, we will
87 // create a second mapping of the buffer. If the process reads and writes
88 // through both mappings, normal memory ordering guarantees may be
89 // violated, depending on the processor cache implementation*.
90 //
91 // If you are deriving a new gralloc implementation from this code, don't
92 // do this. A "real" gralloc should provide a single reference-counted
93 // mapping for each buffer in a process.
94 //
95 // In the current system, there is one case that needs a buffer to be
96 // registered in the same process that allocated it. The SurfaceFlinger
97 // process acts as the IGraphicBufferAlloc Binder provider, so all gralloc
98 // allocations happen in its process. After returning the buffer handle to
99 // the IGraphicBufferAlloc client, SurfaceFlinger free's its handle to the
100 // buffer (unmapping it from the SurfaceFlinger process). If
101 // SurfaceFlinger later acts as the producer end of the buffer queue the
102 // buffer belongs to, it will get a new handle to the buffer in response
103 // to IGraphicBufferProducer::requestBuffer(). Like any buffer handle
104 // received through Binder, the SurfaceFlinger process will register it.
105 // Since it already freed its original handle, it will only end up with
106 // one mapping to the buffer and there will be no problem.
107 //
108 // Currently SurfaceFlinger only acts as a buffer producer for a remote
109 // consumer when taking screenshots and when using virtual displays.
110 //
111 // Eventually, each application should be allowed to make its own gralloc
112 // allocations, solving the problem. Also, this ashmem-based gralloc
113 // should go away, replaced with a real ion-based gralloc.
114 //
115 // * Specifically, associative virtually-indexed caches are likely to have
116 // problems. Most modern L1 caches fit that description.
117
118 private_handle_t* hnd = (private_handle_t*)handle;
119 ALOGD_IF(hnd->pid == getpid(),
120 "Registering a buffer in the process that created it. "
121 "This may cause memory ordering problems.");
122
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700123 void *vaddr;
124 return gralloc_map(module, handle, &vaddr);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700125}
126
127int gralloc_unregister_buffer(gralloc_module_t const* module,
128 buffer_handle_t handle)
129{
130 if (private_handle_t::validate(handle) < 0)
131 return -EINVAL;
132
Mathias Agopianf96b2062009-12-14 18:27:09 -0800133 private_handle_t* hnd = (private_handle_t*)handle;
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700134 if (hnd->base)
135 gralloc_unmap(module, handle);
136
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700137 return 0;
138}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700139
Mathias Agopianf96b2062009-12-14 18:27:09 -0800140int mapBuffer(gralloc_module_t const* module,
141 private_handle_t* hnd)
142{
143 void* vaddr;
144 return gralloc_map(module, hnd, &vaddr);
145}
146
Mathias Agopianbd80b382009-07-07 17:53:43 -0700147int terminateBuffer(gralloc_module_t const* module,
148 private_handle_t* hnd)
149{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800150 if (hnd->base) {
Mathias Agopianbd80b382009-07-07 17:53:43 -0700151 // this buffer was mapped, unmap it now
Mathias Agopianf96b2062009-12-14 18:27:09 -0800152 gralloc_unmap(module, hnd);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700153 }
154
155 return 0;
156}
157
Colin Crossfebaaa92014-02-05 18:21:09 -0800158int gralloc_lock(gralloc_module_t const* /*module*/,
159 buffer_handle_t handle, int /*usage*/,
160 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700161 void** vaddr)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700162{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800163 // this is called when a buffer is being locked for software
164 // access. in thin implementation we have nothing to do since
165 // not synchronization with the h/w is needed.
166 // typically this is used to wait for the h/w to finish with
167 // this buffer if relevant. the data cache may need to be
168 // flushed or invalidated depending on the usage bits and the
169 // hardware.
170
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700171 if (private_handle_t::validate(handle) < 0)
172 return -EINVAL;
173
Mathias Agopian51156652009-06-09 18:55:49 -0700174 private_handle_t* hnd = (private_handle_t*)handle;
Mathias Agopianf96b2062009-12-14 18:27:09 -0800175 *vaddr = (void*)hnd->base;
176 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700177}
178
Colin Crossfebaaa92014-02-05 18:21:09 -0800179int gralloc_unlock(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700180 buffer_handle_t handle)
181{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800182 // we're done with a software buffer. nothing to do in this
183 // implementation. typically this is used to flush the data cache.
184
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700185 if (private_handle_t::validate(handle) < 0)
186 return -EINVAL;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700187 return 0;
188}